• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kio/bookmarks
 

kio/bookmarks

  • kio
  • bookmarks
kbookmarkmenu.cc
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 // vim: set ts=4 sts=4 sw=4 et:
3 /* This file is part of the KDE project
4  Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kbookmarkmenu.h"
23 #include "kbookmarkmenu_p.h"
24 #include "kbookmarkimporter.h"
25 #include "kbookmarkimporter_opera.h"
26 #include "kbookmarkimporter_ie.h"
27 #include "kbookmarkdrag.h"
28 
29 #include <kapplication.h>
30 #include <kconfig.h>
31 #include <kdebug.h>
32 #include <kdialogbase.h>
33 #include <kiconloader.h>
34 #include <klineedit.h>
35 #include <klocale.h>
36 #include <kmessagebox.h>
37 #include <kpopupmenu.h>
38 #include <kstdaccel.h>
39 #include <kstdaction.h>
40 #include <kstringhandler.h>
41 
42 #include <tqclipboard.h>
43 #include <tqfile.h>
44 #include <tqheader.h>
45 #include <tqlabel.h>
46 #include <tqlayout.h>
47 #include <tqlineedit.h>
48 #include <tqlistview.h>
49 #include <tqpushbutton.h>
50 
51 #include <dptrtemplate.h>
52 
53 template class TQPtrList<KBookmarkMenu>;
54 
55 static TQString makeTextNodeMod(KBookmark bk, const TQString &m_nodename, const TQString &m_newText) {
56  TQDomNode subnode = bk.internalElement().namedItem(m_nodename);
57  if (subnode.isNull()) {
58  subnode = bk.internalElement().ownerDocument().createElement(m_nodename);
59  bk.internalElement().appendChild(subnode);
60  }
61 
62  if (subnode.firstChild().isNull()) {
63  TQDomText domtext = subnode.ownerDocument().createTextNode("");
64  subnode.appendChild(domtext);
65  }
66 
67  TQDomText domtext = subnode.firstChild().toText();
68 
69  TQString m_oldText = domtext.data();
70  domtext.setData(m_newText);
71 
72  return m_oldText;
73 }
74 
75 /********************************************************************/
76 /********************************************************************/
77 /********************************************************************/
78 
79 KBookmarkMenu::KBookmarkMenu( KBookmarkManager* mgr,
80  KBookmarkOwner * _owner, KPopupMenu * _parentMenu,
81  KActionCollection *collec, bool _isRoot, bool _add,
82  const TQString & parentAddress )
83  : TQObject(),
84  m_bIsRoot(_isRoot), m_bAddBookmark(_add),
85  m_bAddShortcuts(true),
86  m_pManager(mgr), m_pOwner(_owner),
87  m_parentMenu( _parentMenu ),
88  m_actionCollection( collec ),
89  m_parentAddress( parentAddress )
90 {
91  m_parentMenu->setKeyboardShortcutsEnabled( true );
92 
93  m_lstSubMenus.setAutoDelete( true );
94  m_actions.setAutoDelete( true );
95 
96  if (m_actionCollection)
97  {
98  m_actionCollection->setHighlightingEnabled(true);
99  disconnect( m_actionCollection, TQT_SIGNAL( actionHighlighted( KAction * ) ), 0, 0 );
100  connect( m_actionCollection, TQT_SIGNAL( actionHighlighted( KAction * ) ),
101  this, TQT_SLOT( slotActionHighlighted( KAction * ) ) );
102  }
103 
104  m_bNSBookmark = m_parentAddress.isNull();
105  if ( !m_bNSBookmark ) // not for the netscape bookmark
106  {
107  //kdDebug(7043) << "KBookmarkMenu::KBookmarkMenu " << this << " address : " << m_parentAddress << endl;
108 
109  connect( _parentMenu, TQT_SIGNAL( aboutToShow() ),
110  TQT_SLOT( slotAboutToShow() ) );
111 
112  if ( KBookmarkSettings::self()->m_contextmenu )
113  {
114  (void) _parentMenu->contextMenu();
115  connect( _parentMenu, TQT_SIGNAL( aboutToShowContextMenu(KPopupMenu*, int, TQPopupMenu*) ),
116  this, TQT_SLOT( slotAboutToShowContextMenu(KPopupMenu*, int, TQPopupMenu*) ));
117  }
118 
119  if ( m_bIsRoot )
120  {
121  connect( m_pManager, TQT_SIGNAL( changed(const TQString &, const TQString &) ),
122  TQT_SLOT( slotBookmarksChanged(const TQString &) ) );
123  }
124  }
125 
126  // add entries that possibly have a shortcut, so they are available _before_ first popup
127  if ( m_bIsRoot )
128  {
129  if ( m_bAddBookmark )
130  {
131  addAddBookmark();
132  if ( extOwner() )
133  addAddBookmarksList(); // FIXME
134  }
135 
136  addEditBookmarks();
137  }
138 
139  m_bDirty = true;
140 }
141 
142 KBookmarkMenu::~KBookmarkMenu()
143 {
144  //kdDebug(7043) << "KBookmarkMenu::~KBookmarkMenu() " << this << endl;
145  TQPtrListIterator<KAction> it( m_actions );
146  for (; it.current(); ++it )
147  it.current()->unplugAll();
148 
149  m_lstSubMenus.clear();
150  m_actions.clear();
151 }
152 
153 void KBookmarkMenu::ensureUpToDate()
154 {
155  slotAboutToShow();
156 }
157 
158 void KBookmarkMenu::slotAboutToShow()
159 {
160  // Did the bookmarks change since the last time we showed them ?
161  if ( m_bDirty )
162  {
163  m_bDirty = false;
164  refill();
165  }
166 }
167 
168 TQString KBookmarkMenu::s_highlightedAddress;
169 TQString KBookmarkMenu::s_highlightedImportType;
170 TQString KBookmarkMenu::s_highlightedImportLocation;
171 
172 void KBookmarkMenu::slotActionHighlighted( KAction* action )
173 {
174  if (action->isA("KBookmarkActionMenu") || action->isA("KBookmarkAction"))
175  {
176  s_highlightedAddress = action->property("address").toString();
177  //kdDebug() << "KBookmarkMenu::slotActionHighlighted" << s_highlightedAddress << endl;
178  }
179  else if (action->isA("KImportedBookmarksActionMenu"))
180  {
181  s_highlightedImportType = action->property("type").toString();
182  s_highlightedImportLocation = action->property("location").toString();
183  }
184  else
185  {
186  s_highlightedAddress = TQString::null;
187  s_highlightedImportType = TQString::null;
188  s_highlightedImportLocation = TQString::null;
189  }
190 }
191 
192 /********************************************************************/
193 /********************************************************************/
194 /********************************************************************/
195 
196 class KBookmarkMenuRMBAssoc : public dPtrTemplate<KBookmarkMenu, RMB> { };
197 template<> TQPtrDict<RMB>* dPtrTemplate<KBookmarkMenu, RMB>::d_ptr = 0;
198 
199 static RMB* rmbSelf(KBookmarkMenu *m) { return KBookmarkMenuRMBAssoc::d(m); }
200 
201 // TODO check via dcop before making any changes to the bookmarks file???
202 
203 void RMB::begin_rmb_action(KBookmarkMenu *self)
204 {
205  RMB *s = rmbSelf(self);
206  s->recv = self;
207  s->m_parentAddress = self->m_parentAddress;
208  s->s_highlightedAddress = KBookmarkMenu::s_highlightedAddress;
209  s->m_pManager = self->m_pManager;
210  s->m_pOwner = self->m_pOwner;
211  s->m_parentMenu = self->m_parentMenu;
212 }
213 
214 bool RMB::invalid( int val )
215 {
216  bool valid = true;
217 
218  if (val == 1)
219  s_highlightedAddress = m_parentAddress;
220 
221  if (s_highlightedAddress.isNull())
222  valid = false;
223 
224  return !valid;
225 }
226 
227 KBookmark RMB::atAddress(const TQString & address)
228 {
229  KBookmark bookmark = m_pManager->findByAddress( address );
230  Q_ASSERT(!bookmark.isNull());
231  return bookmark;
232 }
233 
234 void KBookmarkMenu::slotAboutToShowContextMenu( KPopupMenu*, int, TQPopupMenu* contextMenu )
235 {
236  //kdDebug(7043) << "KBookmarkMenu::slotAboutToShowContextMenu" << s_highlightedAddress << endl;
237  if (s_highlightedAddress.isNull())
238  {
239  KPopupMenu::contextMenuFocus()->hideContextMenu();
240  return;
241  }
242  contextMenu->clear();
243  fillContextMenu( contextMenu, s_highlightedAddress, 0 );
244 }
245 
246 void RMB::fillContextMenu( TQPopupMenu* contextMenu, const TQString & address, int val )
247 {
248  KBookmark bookmark = atAddress(address);
249 
250  int id;
251 
252  // binner:
253  // "Add Bookmark Here" when pointing at a bookmark looks strange and if you
254  // call it you have to close and reopen the menu to see an entry was added?
255  //
256  // TODO rename these, but, message freeze... umm...
257 
258 // if (bookmark.isGroup()) {
259  id = contextMenu->insertItem( SmallIcon("bookmark_add"), i18n( "Add Bookmark Here" ), recv, TQT_SLOT(slotRMBActionInsert(int)) );
260  contextMenu->setItemParameter( id, val );
261 /* }
262  else
263  {
264  id = contextMenu->insertItem( SmallIcon("bookmark_add"), i18n( "Add Bookmark Here" ), recv, TQT_SLOT(slotRMBActionInsert(int)) );
265  contextMenu->setItemParameter( id, val );
266  }*/
267 }
268 
269 void RMB::fillContextMenu2( TQPopupMenu* contextMenu, const TQString & address, int val )
270 {
271  KBookmark bookmark = atAddress(address);
272 
273  int id;
274 
275  if (bookmark.isGroup()) {
276  id = contextMenu->insertItem( i18n( "Open Folder in Bookmark Editor" ), recv, TQT_SLOT(slotRMBActionEditAt(int)) );
277  contextMenu->setItemParameter( id, val );
278  contextMenu->insertSeparator();
279  id = contextMenu->insertItem( SmallIcon("editdelete"), i18n( "Delete Folder" ), recv, TQT_SLOT(slotRMBActionRemove(int)) );
280  contextMenu->setItemParameter( id, val );
281  contextMenu->insertSeparator();
282  id = contextMenu->insertItem( i18n( "Properties" ), recv, TQT_SLOT(slotRMBActionProperties(int)) );
283  contextMenu->setItemParameter( id, val );
284  }
285  else
286  {
287  id = contextMenu->insertItem( i18n( "Copy Link Address" ), recv, TQT_SLOT(slotRMBActionCopyLocation(int)) );
288  contextMenu->setItemParameter( id, val );
289  contextMenu->insertSeparator();
290  id = contextMenu->insertItem( SmallIcon("editdelete"), i18n( "Delete Bookmark" ), recv, TQT_SLOT(slotRMBActionRemove(int)) );
291  contextMenu->setItemParameter( id, val );
292  contextMenu->insertSeparator();
293  id = contextMenu->insertItem( i18n( "Properties" ), recv, TQT_SLOT(slotRMBActionProperties(int)) );
294  contextMenu->setItemParameter( id, val );
295  }
296 }
297 
298 void RMB::slotRMBActionEditAt( int val )
299 {
300  kdDebug(7043) << "KBookmarkMenu::slotRMBActionEditAt" << s_highlightedAddress << endl;
301  if (invalid(val)) { hidePopup(); return; }
302 
303  KBookmark bookmark = atAddress(s_highlightedAddress);
304 
305  m_pManager->slotEditBookmarksAtAddress( s_highlightedAddress );
306 }
307 
308 void RMB::slotRMBActionProperties( int val )
309 {
310  kdDebug(7043) << "KBookmarkMenu::slotRMBActionProperties" << s_highlightedAddress << endl;
311  if (invalid(val)) { hidePopup(); return; }
312 
313  KBookmark bookmark = atAddress(s_highlightedAddress);
314 
315  TQString folder = bookmark.isGroup() ? TQString::null : bookmark.url().pathOrURL();
316  KBookmarkEditDialog dlg( bookmark.fullText(), folder,
317  m_pManager, KBookmarkEditDialog::ModifyMode, 0,
318  0, 0, i18n("Bookmark Properties") );
319  if ( dlg.exec() != KDialogBase::Accepted )
320  return;
321 
322  makeTextNodeMod(bookmark, "title", dlg.finalTitle());
323  if ( !dlg.finalUrl().isNull() )
324  {
325  KURL u = KURL::fromPathOrURL(dlg.finalUrl());
326  bookmark.internalElement().setAttribute("href", u.url(0, 106));
327  }
328 
329  kdDebug(7043) << "Requested move to " << dlg.finalAddress() << "!" << endl;
330 
331  KBookmarkGroup parentBookmark = atAddress(m_parentAddress).toGroup();
332  m_pManager->emitChanged( parentBookmark );
333 }
334 
335 void RMB::slotRMBActionInsert( int val )
336 {
337  kdDebug(7043) << "KBookmarkMenu::slotRMBActionInsert" << s_highlightedAddress << endl;
338  if (invalid(val)) { hidePopup(); return; }
339 
340  TQString url = m_pOwner->currentURL();
341  if (url.isEmpty())
342  {
343  KMessageBox::error( 0L, i18n("Cannot add bookmark with empty URL."));
344  return;
345  }
346  TQString title = m_pOwner->currentTitle();
347  if (title.isEmpty())
348  title = url;
349 
350  KBookmark bookmark = atAddress( s_highlightedAddress );
351 
352  // TODO use unique title
353 
354  if (bookmark.isGroup())
355  {
356  KBookmarkGroup parentBookmark = bookmark.toGroup();
357  Q_ASSERT(!parentBookmark.isNull());
358  parentBookmark.addBookmark( m_pManager, title, KURL( url ) );
359  m_pManager->emitChanged( parentBookmark );
360  }
361  else
362  {
363  KBookmarkGroup parentBookmark = bookmark.parentGroup();
364  Q_ASSERT(!parentBookmark.isNull());
365  KBookmark newBookmark = parentBookmark.addBookmark( m_pManager, title, KURL( url ) );
366  parentBookmark.moveItem( newBookmark, parentBookmark.previous(bookmark) );
367  m_pManager->emitChanged( parentBookmark );
368  }
369 }
370 
371 void RMB::slotRMBActionRemove( int val )
372 {
373  //kdDebug(7043) << "KBookmarkMenu::slotRMBActionRemove" << s_highlightedAddress << endl;
374  if (invalid(val)) { hidePopup(); return; }
375 
376  KBookmark bookmark = atAddress( s_highlightedAddress );
377  bool folder = bookmark.isGroup();
378 
379  if (KMessageBox::warningContinueCancel(
380  m_parentMenu,
381  folder ? i18n("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bookmark.text())
382  : i18n("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bookmark.text()),
383  folder ? i18n("Bookmark Folder Deletion")
384  : i18n("Bookmark Deletion"),
385  KStdGuiItem::del())
386  != KMessageBox::Continue
387  )
388  return;
389 
390  KBookmarkGroup parentBookmark = atAddress( m_parentAddress ).toGroup();
391  parentBookmark.deleteBookmark( bookmark );
392  m_pManager->emitChanged( parentBookmark );
393  if (m_parentMenu)
394  m_parentMenu->hide();
395 }
396 
397 void RMB::slotRMBActionCopyLocation( int val )
398 {
399  //kdDebug(7043) << "KBookmarkMenu::slotRMBActionCopyLocation" << s_highlightedAddress << endl;
400  if (invalid(val)) { hidePopup(); return; }
401 
402  KBookmark bookmark = atAddress( s_highlightedAddress );
403 
404  if ( !bookmark.isGroup() )
405  {
406  kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
407  TQClipboard::Selection );
408  kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
409  TQClipboard::Clipboard );
410  }
411 }
412 
413 void RMB::hidePopup() {
414  KPopupMenu::contextMenuFocus()->hideContextMenu();
415 }
416 
417 /********************************************************************/
418 /********************************************************************/
419 /********************************************************************/
420 
421 void KBookmarkMenu::fillContextMenu( TQPopupMenu* contextMenu, const TQString & address, int val )
422 {
423  RMB::begin_rmb_action(this);
424  rmbSelf(this)->fillContextMenu(contextMenu, address, val);
425  emit aboutToShowContextMenu( rmbSelf(this)->atAddress(address), contextMenu);
426  rmbSelf(this)->fillContextMenu2(contextMenu, address, val);
427 }
428 
429 void KBookmarkMenu::slotRMBActionEditAt( int val )
430 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionEditAt( val ); }
431 
432 void KBookmarkMenu::slotRMBActionProperties( int val )
433 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionProperties( val ); }
434 
435 void KBookmarkMenu::slotRMBActionInsert( int val )
436 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionInsert( val ); }
437 
438 void KBookmarkMenu::slotRMBActionRemove( int val )
439 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionRemove( val ); }
440 
441 void KBookmarkMenu::slotRMBActionCopyLocation( int val )
442 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionCopyLocation( val ); }
443 
444 void KBookmarkMenu::slotBookmarksChanged( const TQString & groupAddress )
445 {
446  if (m_bNSBookmark)
447  return;
448 
449  if ( groupAddress == m_parentAddress )
450  {
451  //kdDebug(7043) << "KBookmarkMenu::slotBookmarksChanged -> setting m_bDirty on " << groupAddress << endl;
452  m_bDirty = true;
453  }
454  else
455  {
456  // Iterate recursively into child menus
457  TQPtrListIterator<KBookmarkMenu> it( m_lstSubMenus );
458  for (; it.current(); ++it )
459  {
460  it.current()->slotBookmarksChanged( groupAddress );
461  }
462  }
463 }
464 
465 void KBookmarkMenu::refill()
466 {
467  //kdDebug(7043) << "KBookmarkMenu::refill()" << endl;
468  m_lstSubMenus.clear();
469 
470  TQPtrListIterator<KAction> it( m_actions );
471  for (; it.current(); ++it )
472  it.current()->unplug( m_parentMenu );
473 
474  m_parentMenu->clear();
475  m_actions.clear();
476 
477  fillBookmarkMenu();
478  m_parentMenu->adjustSize();
479 }
480 
481 void KBookmarkMenu::addAddBookmarksList()
482 {
483  if (!kapp->authorizeKAction("bookmarks"))
484  return;
485 
486  TQString title = i18n( "Bookmark Tabs as Folder..." );
487 
488  KAction * paAddBookmarksList = new KAction( title,
489  "bookmarks_list_add",
490  0,
491  this,
492  TQT_SLOT( slotAddBookmarksList() ),
493  m_actionCollection, m_bIsRoot ? "add_bookmarks_list" : 0 );
494 
495  paAddBookmarksList->setToolTip( i18n( "Add a folder of bookmarks for all open tabs." ) );
496 
497  paAddBookmarksList->plug( m_parentMenu );
498  m_actions.append( paAddBookmarksList );
499 }
500 
501 void KBookmarkMenu::addAddBookmark()
502 {
503  if (!kapp->authorizeKAction("bookmarks"))
504  return;
505 
506  TQString title = i18n( "Add Bookmark" );
507 
508  KAction * paAddBookmarks = new KAction( title,
509  "bookmark_add",
510  m_bIsRoot && m_bAddShortcuts ? KStdAccel::addBookmark() : KShortcut(),
511  this,
512  TQT_SLOT( slotAddBookmark() ),
513  m_actionCollection, m_bIsRoot ? "add_bookmark" : 0 );
514 
515  paAddBookmarks->setToolTip( i18n( "Add a bookmark for the current document" ) );
516 
517  paAddBookmarks->plug( m_parentMenu );
518  m_actions.append( paAddBookmarks );
519 }
520 
521 void KBookmarkMenu::addEditBookmarks()
522 {
523  if (!kapp->authorizeKAction("bookmarks"))
524  return;
525 
526  KAction * m_paEditBookmarks = KStdAction::editBookmarks( m_pManager, TQT_SLOT( slotEditBookmarks() ),
527  m_actionCollection, "edit_bookmarks" );
528  m_paEditBookmarks->plug( m_parentMenu );
529  m_paEditBookmarks->setToolTip( i18n( "Edit your bookmark collection in a separate window" ) );
530  m_actions.append( m_paEditBookmarks );
531 }
532 
533 void KBookmarkMenu::addNewFolder()
534 {
535  if (!kapp->authorizeKAction("bookmarks"))
536  return;
537 
538  TQString title = i18n( "&New Bookmark Folder..." );
539  int p;
540  while ( ( p = title.find( '&' ) ) >= 0 )
541  title.remove( p, 1 );
542 
543  KAction * paNewFolder = new KAction( title,
544  "folder_new", //"folder",
545  0,
546  this,
547  TQT_SLOT( slotNewFolder() ),
548  m_actionCollection );
549 
550  paNewFolder->setToolTip( i18n( "Create a new bookmark folder in this menu" ) );
551 
552  paNewFolder->plug( m_parentMenu );
553  m_actions.append( paNewFolder );
554 }
555 
556 void KBookmarkMenu::fillBookmarkMenu()
557 {
558  if (!kapp->authorizeKAction("bookmarks"))
559  return;
560 
561  if ( m_bIsRoot )
562  {
563  if ( m_bAddBookmark )
564  {
565  addAddBookmark();
566  if ( extOwner() )
567  addAddBookmarksList(); // FIXME
568  }
569 
570  addEditBookmarks();
571 
572  if ( m_bAddBookmark && !KBookmarkSettings::self()->m_advancedaddbookmark )
573  addNewFolder();
574  }
575 
576  if ( m_bIsRoot
577  && KBookmarkManager::userBookmarksFile() == m_pManager->path() )
578  {
579  bool haveSep = false;
580 
581  TQValueList<TQString> keys = KBookmarkMenu::dynamicBookmarksList();
582  TQValueList<TQString>::const_iterator it;
583  for ( it = keys.begin(); it != keys.end(); ++it )
584  {
585  DynMenuInfo info;
586  info = showDynamicBookmarks((*it));
587 
588  if ( !info.show || !TQFile::exists( info.location ) )
589  continue;
590 
591  if (!haveSep)
592  {
593  m_parentMenu->insertSeparator();
594  haveSep = true;
595  }
596 
597  KActionMenu * actionMenu;
598  actionMenu = new KImportedBookmarksActionMenu(
599  info.name, info.type,
600  m_actionCollection, "kbookmarkmenu" );
601 
602  actionMenu->setProperty( "type", info.type );
603  actionMenu->setProperty( "location", info.location );
604 
605  actionMenu->plug( m_parentMenu );
606  m_actions.append( actionMenu );
607 
608  KBookmarkMenu *subMenu =
609  new KBookmarkMenu( m_pManager, m_pOwner, actionMenu->popupMenu(),
610  m_actionCollection, false,
611  m_bAddBookmark, TQString::null );
612  connect( subMenu, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ),
613  this, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ));
614  m_lstSubMenus.append(subMenu);
615 
616  connect(actionMenu->popupMenu(), TQT_SIGNAL(aboutToShow()), subMenu, TQT_SLOT(slotNSLoad()));
617  }
618  }
619 
620  KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
621  Q_ASSERT(!parentBookmark.isNull());
622  bool separatorInserted = false;
623  for ( KBookmark bm = parentBookmark.first(); !bm.isNull(); bm = parentBookmark.next(bm) )
624  {
625  TQString text = KStringHandler::csqueeze(bm.fullText(), 60);
626  text.replace( '&', "&&" );
627  if ( !separatorInserted && m_bIsRoot) {
628  // inserted before the first konq bookmark, to avoid the separator if no konq bookmark
629  m_parentMenu->insertSeparator();
630  separatorInserted = true;
631  }
632  if ( !bm.isGroup() )
633  {
634  if ( bm.isSeparator() )
635  {
636  m_parentMenu->insertSeparator();
637  }
638  else
639  {
640  //kdDebug(7043) << "Creating URL bookmark menu item for " << bm.text() << endl;
641  KAction * action = new KBookmarkAction( text, bm.icon(), 0, m_actionCollection, 0 );
642  connect(action, TQT_SIGNAL( activated ( KAction::ActivationReason, TQt::ButtonState )),
643  this, TQT_SLOT( slotBookmarkSelected( KAction::ActivationReason, TQt::ButtonState ) ));
644 
645  action->setProperty( "url", bm.url().url() );
646  action->setProperty( "address", bm.address() );
647 
648  action->setToolTip( bm.url().pathOrURL() );
649 
650  action->plug( m_parentMenu );
651  m_actions.append( action );
652  }
653  }
654  else
655  {
656  //kdDebug(7043) << "Creating bookmark submenu named " << bm.text() << endl;
657  KActionMenu * actionMenu = new KBookmarkActionMenu( text, bm.icon(),
658  m_actionCollection,
659  "kbookmarkmenu" );
660  actionMenu->setProperty( "address", bm.address() );
661  actionMenu->plug( m_parentMenu );
662  m_actions.append( actionMenu );
663 
664  KBookmarkMenu *subMenu = new KBookmarkMenu( m_pManager, m_pOwner, actionMenu->popupMenu(),
665  m_actionCollection, false,
666  m_bAddBookmark,
667  bm.address() );
668 
669  connect(subMenu, TQT_SIGNAL( aboutToShowContextMenu( const KBookmark &, TQPopupMenu * ) ),
670  this, TQT_SIGNAL( aboutToShowContextMenu( const KBookmark &, TQPopupMenu * ) ));
671  connect(subMenu, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ),
672  this, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ));
673  m_lstSubMenus.append( subMenu );
674  }
675  }
676 
677  if ( !m_bIsRoot && m_bAddBookmark )
678  {
679  if ( m_parentMenu->count() > 0 )
680  m_parentMenu->insertSeparator();
681 
682  if ( KBookmarkSettings::self()->m_quickactions )
683  {
684  KActionMenu * actionMenu = new KActionMenu( i18n("Quick Actions"), m_actionCollection, 0L );
685  fillContextMenu( actionMenu->popupMenu(), m_parentAddress, 1 );
686  actionMenu->plug( m_parentMenu );
687  m_actions.append( actionMenu );
688  }
689  else
690  {
691  addAddBookmark();
692  if ( extOwner() )
693  addAddBookmarksList(); // FIXME
694  addNewFolder();
695  }
696  }
697 }
698 
699 void KBookmarkMenu::slotAddBookmarksList()
700 {
701  KExtendedBookmarkOwner *extOwner = dynamic_cast<KExtendedBookmarkOwner*>(m_pOwner);
702  if (!extOwner)
703  {
704  kdWarning() << "erm, sorry ;-)" << endl;
705  return;
706  }
707 
708  KExtendedBookmarkOwner::QStringPairList list;
709  extOwner->fillBookmarksList( list );
710 
711  KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
712  Q_ASSERT(!parentBookmark.isNull());
713  KBookmarkGroup group = parentBookmark.createNewFolder( m_pManager );
714  if ( group.isNull() )
715  return; // user canceled i guess
716 
717  KExtendedBookmarkOwner::QStringPairList::const_iterator it;
718  for ( it = list.begin(); it != list.end(); ++it )
719  group.addBookmark( m_pManager, (*it).first, KURL((*it).second) );
720 
721  m_pManager->emitChanged( parentBookmark );
722 }
723 
724 
725 void KBookmarkMenu::slotAddBookmark()
726 {
727  KBookmarkGroup parentBookmark;
728  parentBookmark = m_pManager->addBookmarkDialog(m_pOwner->currentURL(), m_pOwner->currentTitle(), m_parentAddress);
729  if (!parentBookmark.isNull())
730  m_pManager->emitChanged( parentBookmark );
731 }
732 
733 void KBookmarkMenu::slotNewFolder()
734 {
735  if ( !m_pOwner ) return; // this view doesn't handle bookmarks...
736  KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
737  Q_ASSERT(!parentBookmark.isNull());
738  KBookmarkGroup group = parentBookmark.createNewFolder( m_pManager );
739  if ( !group.isNull() )
740  {
741  KBookmarkGroup parentGroup = group.parentGroup();
742  m_pManager->emitChanged( parentGroup );
743  }
744 }
745 
746 void KBookmarkMenu::slotBookmarkSelected( KAction::ActivationReason /*reason*/, TQt::ButtonState state )
747 {
748  kdDebug(7043) << "KBookmarkMenu::slotBookmarkSelected()" << endl;
749  if ( !m_pOwner ) return; // this view doesn't handle bookmarks...
750  const KAction* action = dynamic_cast<const KAction *>(sender());
751  if(action)
752  {
753  const TQString& url = sender()->property("url").toString();
754  m_pOwner->openBookmarkURL( url );
755  emit openBookmark( url, state );
756  }
757 }
758 
759 void KBookmarkMenu::slotBookmarkSelected()
760 {
761  slotBookmarkSelected(KAction::PopupMenuActivation, Qt::NoButton);
762 }
763 
764 KExtendedBookmarkOwner* KBookmarkMenu::extOwner()
765 {
766  return dynamic_cast<KExtendedBookmarkOwner*>(m_pOwner);
767 }
768 
769 void KBookmarkMenu::slotNSLoad()
770 {
771  // only fill menu once
772  m_parentMenu->disconnect(TQT_SIGNAL(aboutToShow()));
773 
774  // not NSImporter, but kept old name for BC reasons
775  KBookmarkMenuNSImporter importer( m_pManager, this, m_actionCollection );
776  importer.openBookmarks(s_highlightedImportLocation, s_highlightedImportType);
777 }
778 
779 /********************************************************************/
780 /********************************************************************/
781 /********************************************************************/
782 
783 KBookmarkEditFields::KBookmarkEditFields(TQWidget *main, TQBoxLayout *vbox, FieldsSet fieldsSet)
784 {
785  bool isF = (fieldsSet != FolderFieldsSet);
786 
787  TQGridLayout *grid = new TQGridLayout( vbox, 2, isF ? 2 : 1 );
788 
789  m_title = new KLineEdit( main );
790  grid->addWidget( m_title, 0, 1 );
791  grid->addWidget( new TQLabel( m_title, i18n( "Name:" ), main ), 0, 0 );
792  m_title->setFocus();
793  if (isF)
794  {
795  m_url = new KLineEdit( main );
796  grid->addWidget( m_url, 1, 1 );
797  grid->addWidget( new TQLabel( m_url, i18n( "Location:" ), main ), 1, 0 );
798  }
799  else
800  {
801  m_url = 0;
802  }
803 
804  main->setMinimumSize( 300, 0 );
805 }
806 
807 void KBookmarkEditFields::setName(const TQString &str)
808 {
809  m_title->setText(str);
810 }
811 
812 void KBookmarkEditFields::setLocation(const TQString &str)
813 {
814  m_url->setText(str);
815 }
816 
817 /********************************************************************/
818 /********************************************************************/
819 /********************************************************************/
820 
821 // TODO - make the dialog use Properties as a title when in Modify mode... (dirk noticed the bug...)
822 KBookmarkEditDialog::KBookmarkEditDialog(const TQString& title, const TQString& url, KBookmarkManager * mgr, BookmarkEditType editType, const TQString& address,
823  TQWidget * parent, const char * name, const TQString& caption )
824  : KDialogBase(parent, name, true, caption,
825  (editType == InsertionMode) ? (User1|Ok|Cancel) : (Ok|Cancel),
826  Ok, false, KGuiItem()),
827  m_folderTree(0), m_mgr(mgr), m_editType(editType), m_address(address)
828 {
829  setButtonOK( (editType == InsertionMode) ? KGuiItem( i18n( "&Add" ), "bookmark_add") : i18n( "&Update" ) );
830  if (editType == InsertionMode) {
831  setButtonGuiItem( User1, KGuiItem( i18n( "&New Folder..." ), "folder_new") );
832  }
833 
834  bool folder = url.isNull();
835 
836  m_main = new TQWidget( this );
837  setMainWidget( m_main );
838 
839  TQBoxLayout *vbox = new TQVBoxLayout( m_main, 0, spacingHint() );
840  KBookmarkEditFields::FieldsSet fs =
841  folder ? KBookmarkEditFields::FolderFieldsSet
842  : KBookmarkEditFields::BookmarkFieldsSet;
843  m_fields = new KBookmarkEditFields(m_main, vbox, fs);
844  m_fields->setName(title);
845  if ( !folder )
846  m_fields->setLocation(url);
847 
848  if ( editType == InsertionMode )
849  {
850  m_folderTree = KBookmarkFolderTree::createTree( m_mgr, m_main, name, m_address );
851  connect( m_folderTree, TQT_SIGNAL( doubleClicked(TQListViewItem*) ),
852  this, TQT_SLOT( slotDoubleClicked(TQListViewItem*) ) );
853  vbox->addWidget( m_folderTree );
854  connect( this, TQT_SIGNAL( user1Clicked() ), TQT_SLOT( slotUser1() ) );
855  }
856 }
857 
858 void KBookmarkEditDialog::slotDoubleClicked( TQListViewItem* item )
859 {
860  Q_ASSERT( m_folderTree );
861  m_folderTree->setCurrentItem( item );
862  accept();
863 }
864 
865 void KBookmarkEditDialog::slotOk()
866 {
867  accept();
868 }
869 
870 void KBookmarkEditDialog::slotCancel()
871 {
872  reject();
873 }
874 
875 TQString KBookmarkEditDialog::finalAddress() const
876 {
877  Q_ASSERT( m_folderTree );
878  return KBookmarkFolderTree::selectedAddress( m_folderTree );
879 }
880 
881 TQString KBookmarkEditDialog::finalUrl() const
882 {
883  return m_fields->m_url ? m_fields->m_url->text() : TQString::null;
884 }
885 
886 TQString KBookmarkEditDialog::finalTitle() const
887 {
888  return m_fields->m_title ? m_fields->m_title->text() : TQString::null;
889 }
890 
891 void KBookmarkEditDialog::slotUser1()
892 {
893  // kdDebug(7043) << "KBookmarkEditDialog::slotUser1" << endl;
894  Q_ASSERT( m_folderTree );
895 
896  TQString address = KBookmarkFolderTree::selectedAddress( m_folderTree );
897  if ( address.isNull() ) return;
898  KBookmarkGroup bm = m_mgr->findByAddress( address ).toGroup();
899  Q_ASSERT(!bm.isNull());
900  Q_ASSERT(m_editType == InsertionMode);
901 
902  KBookmarkGroup group = bm.createNewFolder( m_mgr );
903  if ( !group.isNull() )
904  {
905  KBookmarkGroup parentGroup = group.parentGroup();
906  m_mgr->emitChanged( parentGroup );
907  }
908  KBookmarkFolderTree::fillTree( m_folderTree, m_mgr );
909 }
910 
911 /********************************************************************/
912 /********************************************************************/
913 /********************************************************************/
914 
915 static void fillGroup( TQListView* listview, KBookmarkFolderTreeItem * parentItem, KBookmarkGroup group, bool expandOpenGroups = true, const TQString& address = TQString::null )
916 {
917  bool noSubGroups = true;
918  KBookmarkFolderTreeItem * lastItem = 0L;
919  KBookmarkFolderTreeItem * item = 0L;
920  for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk) )
921  {
922  if ( bk.isGroup() )
923  {
924  KBookmarkGroup grp = bk.toGroup();
925  item = new KBookmarkFolderTreeItem( parentItem, lastItem, grp );
926  fillGroup( listview, item, grp, expandOpenGroups, address );
927  if ( expandOpenGroups && grp.isOpen() )
928  item->setOpen( true );
929  lastItem = item;
930  noSubGroups = false;
931  }
932  if (bk.address() == address) {
933  listview->setCurrentItem( lastItem );
934  listview->ensureItemVisible( item );
935  }
936  }
937  if ( noSubGroups ) {
938  parentItem->setOpen( true );
939  }
940 }
941 
942 TQListView* KBookmarkFolderTree::createTree( KBookmarkManager* mgr, TQWidget* parent, const char* name, const TQString& address )
943 {
944  TQListView *listview = new TQListView( parent, name );
945 
946  listview->setRootIsDecorated( false );
947  listview->header()->hide();
948  listview->addColumn( i18n("Bookmark"), 200 );
949  listview->setSorting( -1, false );
950  listview->setSelectionMode( TQListView::Single );
951  listview->setAllColumnsShowFocus( true );
952  listview->setResizeMode( TQListView::AllColumns );
953  listview->setMinimumSize( 60, 100 );
954 
955  fillTree( listview, mgr, address );
956 
957  return listview;
958 }
959 
960 void KBookmarkFolderTree::fillTree( TQListView *listview, KBookmarkManager* mgr, const TQString& address )
961 {
962  listview->clear();
963 
964  KBookmarkGroup root = mgr->root();
965  KBookmarkFolderTreeItem * rootItem = new KBookmarkFolderTreeItem( listview, root );
966  listview->setCurrentItem( rootItem );
967  rootItem->setSelected( true );
968  fillGroup( listview, rootItem, root, (address == root.groupAddress() || address.isNull()) ? true : false, address );
969  rootItem->setOpen( true );
970 }
971 
972 static KBookmarkFolderTreeItem* ft_cast( TQListViewItem *i )
973 {
974  return static_cast<KBookmarkFolderTreeItem*>( i );
975 }
976 
977 TQString KBookmarkFolderTree::selectedAddress( TQListView *listview )
978 {
979  if ( !listview)
980  return TQString::null;
981  KBookmarkFolderTreeItem *item = ft_cast( listview->currentItem() );
982  return item ? item->m_bookmark.address() : TQString::null;
983 }
984 
985 void KBookmarkFolderTree::setAddress( TQListView *listview, const TQString & address )
986 {
987  KBookmarkFolderTreeItem* it = ft_cast( listview->firstChild() );
988  while ( true ) {
989  kdDebug(7043) << it->m_bookmark.address() << endl;
990  it = ft_cast( it->itemBelow() );
991  if ( !it )
992  return;
993  if ( it->m_bookmark.address() == address )
994  break;
995  }
996  it->setSelected( true );
997  listview->setCurrentItem( it );
998 }
999 
1000 /********************************************************************/
1001 /********************************************************************/
1002 /********************************************************************/
1003 
1004 // toplevel item
1005 KBookmarkFolderTreeItem::KBookmarkFolderTreeItem( TQListView *parent, const KBookmark & gp )
1006  : TQListViewItem(parent, i18n("Bookmarks")), m_bookmark(gp)
1007 {
1008  setPixmap(0, SmallIcon("bookmark"));
1009  setExpandable(true);
1010 }
1011 
1012 // group
1013 KBookmarkFolderTreeItem::KBookmarkFolderTreeItem( KBookmarkFolderTreeItem *parent, TQListViewItem *after, const KBookmarkGroup & gp )
1014  : TQListViewItem(parent, after, gp.fullText()), m_bookmark(gp)
1015 {
1016  setPixmap(0, SmallIcon( gp.icon() ) );
1017  setExpandable(true);
1018 }
1019 
1020 /********************************************************************/
1021 /********************************************************************/
1022 /********************************************************************/
1023 
1024 // NOTE - KBookmarkMenuNSImporter is really === KBookmarkMenuImporter
1025 // i.e, it is _not_ ns specific. and in KDE4 it should be renamed.
1026 
1027 void KBookmarkMenuNSImporter::openNSBookmarks()
1028 {
1029  openBookmarks( KNSBookmarkImporter::netscapeBookmarksFile(), "netscape" );
1030 }
1031 
1032 void KBookmarkMenuNSImporter::openBookmarks( const TQString &location, const TQString &type )
1033 {
1034  mstack.push(m_menu);
1035 
1036  KBookmarkImporterBase *importer = KBookmarkImporterBase::factory(type);
1037  if (!importer)
1038  return;
1039  importer->setFilename(location);
1040  connectToImporter(*importer);
1041  importer->parse();
1042 
1043  delete importer;
1044 }
1045 
1046 void KBookmarkMenuNSImporter::connectToImporter(const TQObject &importer)
1047 {
1048  connect( &importer, TQT_SIGNAL( newBookmark( const TQString &, const TQCString &, const TQString & ) ),
1049  TQT_SLOT( newBookmark( const TQString &, const TQCString &, const TQString & ) ) );
1050  connect( &importer, TQT_SIGNAL( newFolder( const TQString &, bool, const TQString & ) ),
1051  TQT_SLOT( newFolder( const TQString &, bool, const TQString & ) ) );
1052  connect( &importer, TQT_SIGNAL( newSeparator() ), TQT_SLOT( newSeparator() ) );
1053  connect( &importer, TQT_SIGNAL( endFolder() ), TQT_SLOT( endFolder() ) );
1054 }
1055 
1056 void KBookmarkMenuNSImporter::newBookmark( const TQString & text, const TQCString & url, const TQString & )
1057 {
1058  TQString _text = KStringHandler::csqueeze(text);
1059  _text.replace( '&', "&&" );
1060  KAction * action = new KBookmarkAction(_text, "html", 0, 0, "", m_actionCollection, 0);
1061  connect(action, TQT_SIGNAL( activated ( KAction::ActivationReason, TQt::ButtonState )),
1062  m_menu, TQT_SLOT( slotBookmarkSelected( KAction::ActivationReason, TQt::ButtonState ) ));
1063  action->setProperty( "url", url );
1064  action->setToolTip( url );
1065  action->plug( mstack.top()->m_parentMenu );
1066  mstack.top()->m_actions.append( action );
1067 }
1068 
1069 void KBookmarkMenuNSImporter::newFolder( const TQString & text, bool, const TQString & )
1070 {
1071  TQString _text = KStringHandler::csqueeze(text);
1072  _text.replace( '&', "&&" );
1073  KActionMenu * actionMenu = new KActionMenu( _text, "folder", m_actionCollection, 0L );
1074  actionMenu->plug( mstack.top()->m_parentMenu );
1075  mstack.top()->m_actions.append( actionMenu );
1076  KBookmarkMenu *subMenu = new KBookmarkMenu( m_pManager, m_menu->m_pOwner, actionMenu->popupMenu(),
1077  m_actionCollection, false,
1078  m_menu->m_bAddBookmark, TQString::null );
1079  connect( subMenu, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ),
1080  m_menu, TQT_SIGNAL( openBookmark( const TQString &, TQt::ButtonState ) ));
1081  mstack.top()->m_lstSubMenus.append( subMenu );
1082 
1083  mstack.push(subMenu);
1084 }
1085 
1086 void KBookmarkMenuNSImporter::newSeparator()
1087 {
1088  mstack.top()->m_parentMenu->insertSeparator();
1089 }
1090 
1091 void KBookmarkMenuNSImporter::endFolder()
1092 {
1093  mstack.pop();
1094 }
1095 
1096 /********************************************************************/
1097 /********************************************************************/
1098 /********************************************************************/
1099 
1100 KBookmarkMenu::DynMenuInfo KBookmarkMenu::showDynamicBookmarks( const TQString &id )
1101 {
1102  KConfig config("kbookmarkrc", false, false);
1103  config.setGroup("Bookmarks");
1104 
1105  DynMenuInfo info;
1106  info.show = false;
1107 
1108  if (!config.hasKey("DynamicMenus")) {
1109  // upgrade path
1110  if (id == "netscape") {
1111  KBookmarkManager *manager = KBookmarkManager::userBookmarksManager();
1112  info.show = manager->root().internalElement().attribute("hide_nsbk") != "yes";
1113  info.location = KNSBookmarkImporter::netscapeBookmarksFile();
1114  info.type = "netscape";
1115  info.name = i18n("Netscape Bookmarks");
1116  } // else, no show
1117 
1118  } else {
1119  // have new version config
1120  if (config.hasGroup("DynamicMenu-" + id)) {
1121  config.setGroup("DynamicMenu-" + id);
1122  info.show = config.readBoolEntry("Show");
1123  info.location = config.readPathEntry("Location");
1124  info.type = config.readEntry("Type");
1125  info.name = config.readEntry("Name");
1126  } // else, no show
1127  }
1128 
1129  return info;
1130 }
1131 
1132 TQStringList KBookmarkMenu::dynamicBookmarksList()
1133 {
1134  KConfig config("kbookmarkrc", false, false);
1135  config.setGroup("Bookmarks");
1136 
1137  TQStringList mlist;
1138  if (config.hasKey("DynamicMenus"))
1139  mlist = config.readListEntry("DynamicMenus");
1140  else
1141  mlist << "netscape";
1142 
1143  return mlist;
1144 }
1145 
1146 void KBookmarkMenu::setDynamicBookmarks(const TQString &id, const DynMenuInfo &newMenu)
1147 {
1148  KConfig config("kbookmarkrc", false, false);
1149 
1150  // add group unconditionally
1151  config.setGroup("DynamicMenu-" + id);
1152  config.writeEntry("Show", newMenu.show);
1153  config.writePathEntry("Location", newMenu.location);
1154  config.writeEntry("Type", newMenu.type);
1155  config.writeEntry("Name", newMenu.name);
1156 
1157  TQStringList elist;
1158 
1159  config.setGroup("Bookmarks");
1160  if (!config.hasKey("DynamicMenus")) {
1161  if (newMenu.type != "netscape") {
1162  // update from old xbel method to new rc method
1163  // though only if not writing the netscape setting
1164  config.setGroup("DynamicMenu-" "netscape");
1165  DynMenuInfo xbelSetting;
1166  xbelSetting = showDynamicBookmarks("netscape");
1167  config.writeEntry("Show", xbelSetting.show);
1168  config.writePathEntry("Location", xbelSetting.location);
1169  config.writeEntry("Type", xbelSetting.type);
1170  config.writeEntry("Name", xbelSetting.name);
1171  }
1172  } else {
1173  elist = config.readListEntry("DynamicMenus");
1174  }
1175 
1176  // make sure list includes type
1177  config.setGroup("Bookmarks");
1178  if (elist.contains(id) < 1) {
1179  elist << id;
1180  config.writeEntry("DynamicMenus", elist);
1181  }
1182 
1183  config.sync();
1184 }
1185 
1186 #include "kbookmarkmenu.moc"
1187 #include "kbookmarkmenu_p.moc"

kio/bookmarks

Skip menu "kio/bookmarks"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kio/bookmarks

Skip menu "kio/bookmarks"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kio/bookmarks by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |