00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kbookmarkmenu.h"
00023 #include "kbookmarkmenu_p.h"
00024 #include "kbookmarkimporter.h"
00025 #include "kbookmarkimporter_opera.h"
00026 #include "kbookmarkimporter_ie.h"
00027 #include "kbookmarkdrag.h"
00028
00029 #include <kapplication.h>
00030 #include <kconfig.h>
00031 #include <kdebug.h>
00032 #include <kdialogbase.h>
00033 #include <kiconloader.h>
00034 #include <klineedit.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 #include <kpopupmenu.h>
00038 #include <kstdaccel.h>
00039 #include <kstdaction.h>
00040 #include <kstringhandler.h>
00041
00042 #include <qclipboard.h>
00043 #include <qfile.h>
00044 #include <qheader.h>
00045 #include <qlabel.h>
00046 #include <qlayout.h>
00047 #include <qlineedit.h>
00048 #include <qlistview.h>
00049 #include <qpushbutton.h>
00050
00051 #include <dptrtemplate.h>
00052
00053 template class QPtrList<KBookmarkMenu>;
00054
00055 static QString makeTextNodeMod(KBookmark bk, const QString &m_nodename, const QString &m_newText) {
00056 QDomNode subnode = bk.internalElement().namedItem(m_nodename);
00057 if (subnode.isNull()) {
00058 subnode = bk.internalElement().ownerDocument().createElement(m_nodename);
00059 bk.internalElement().appendChild(subnode);
00060 }
00061
00062 if (subnode.firstChild().isNull()) {
00063 QDomText domtext = subnode.ownerDocument().createTextNode("");
00064 subnode.appendChild(domtext);
00065 }
00066
00067 QDomText domtext = subnode.firstChild().toText();
00068
00069 QString m_oldText = domtext.data();
00070 domtext.setData(m_newText);
00071
00072 return m_oldText;
00073 }
00074
00075
00076
00077
00078
00079 KBookmarkMenu::KBookmarkMenu( KBookmarkManager* mgr,
00080 KBookmarkOwner * _owner, KPopupMenu * _parentMenu,
00081 KActionCollection *collec, bool _isRoot, bool _add,
00082 const QString & parentAddress )
00083 : QObject(),
00084 m_bIsRoot(_isRoot), m_bAddBookmark(_add),
00085 m_bAddShortcuts(true),
00086 m_pManager(mgr), m_pOwner(_owner),
00087 m_parentMenu( _parentMenu ),
00088 m_actionCollection( collec ),
00089 m_parentAddress( parentAddress )
00090 {
00091 m_parentMenu->setKeyboardShortcutsEnabled( true );
00092
00093 m_lstSubMenus.setAutoDelete( true );
00094 m_actions.setAutoDelete( true );
00095
00096 if (m_actionCollection)
00097 {
00098 m_actionCollection->setHighlightingEnabled(true);
00099 disconnect( m_actionCollection, SIGNAL( actionHighlighted( KAction * ) ), 0, 0 );
00100 connect( m_actionCollection, SIGNAL( actionHighlighted( KAction * ) ),
00101 this, SLOT( slotActionHighlighted( KAction * ) ) );
00102 }
00103
00104 m_bNSBookmark = m_parentAddress.isNull();
00105 if ( !m_bNSBookmark )
00106 {
00107
00108
00109 connect( _parentMenu, SIGNAL( aboutToShow() ),
00110 SLOT( slotAboutToShow() ) );
00111
00112 if ( KBookmarkSettings::self()->m_contextmenu )
00113 {
00114 (void) _parentMenu->contextMenu();
00115 connect( _parentMenu, SIGNAL( aboutToShowContextMenu(KPopupMenu*, int, QPopupMenu*) ),
00116 this, SLOT( slotAboutToShowContextMenu(KPopupMenu*, int, QPopupMenu*) ));
00117 }
00118
00119 if ( m_bIsRoot )
00120 {
00121 connect( m_pManager, SIGNAL( changed(const QString &, const QString &) ),
00122 SLOT( slotBookmarksChanged(const QString &) ) );
00123 }
00124 }
00125
00126
00127 if ( m_bIsRoot )
00128 {
00129 if ( m_bAddBookmark )
00130 {
00131 addAddBookmark();
00132 if ( extOwner() )
00133 addAddBookmarksList();
00134 }
00135
00136 addEditBookmarks();
00137 }
00138
00139 m_bDirty = true;
00140 }
00141
00142 KBookmarkMenu::~KBookmarkMenu()
00143 {
00144
00145 QPtrListIterator<KAction> it( m_actions );
00146 for (; it.current(); ++it )
00147 it.current()->unplugAll();
00148
00149 m_lstSubMenus.clear();
00150 m_actions.clear();
00151 }
00152
00153 void KBookmarkMenu::ensureUpToDate()
00154 {
00155 slotAboutToShow();
00156 }
00157
00158 void KBookmarkMenu::slotAboutToShow()
00159 {
00160
00161 if ( m_bDirty )
00162 {
00163 m_bDirty = false;
00164 refill();
00165 }
00166 }
00167
00168 QString KBookmarkMenu::s_highlightedAddress;
00169 QString KBookmarkMenu::s_highlightedImportType;
00170 QString KBookmarkMenu::s_highlightedImportLocation;
00171
00172 void KBookmarkMenu::slotActionHighlighted( KAction* action )
00173 {
00174 if (action->isA("KBookmarkActionMenu") || action->isA("KBookmarkAction"))
00175 {
00176 s_highlightedAddress = action->property("address").toString();
00177
00178 }
00179 else if (action->isA("KImportedBookmarksActionMenu"))
00180 {
00181 s_highlightedImportType = action->property("type").toString();
00182 s_highlightedImportLocation = action->property("location").toString();
00183 }
00184 else
00185 {
00186 s_highlightedAddress = QString::null;
00187 s_highlightedImportType = QString::null;
00188 s_highlightedImportLocation = QString::null;
00189 }
00190 }
00191
00192
00193
00194
00195
00196 class KBookmarkMenuRMBAssoc : public dPtrTemplate<KBookmarkMenu, RMB> { };
00197 template<> QPtrDict<RMB>* dPtrTemplate<KBookmarkMenu, RMB>::d_ptr = 0;
00198
00199 static RMB* rmbSelf(KBookmarkMenu *m) { return KBookmarkMenuRMBAssoc::d(m); }
00200
00201
00202
00203 void RMB::begin_rmb_action(KBookmarkMenu *self)
00204 {
00205 RMB *s = rmbSelf(self);
00206 s->recv = self;
00207 s->m_parentAddress = self->m_parentAddress;
00208 s->s_highlightedAddress = KBookmarkMenu::s_highlightedAddress;
00209 s->m_pManager = self->m_pManager;
00210 s->m_pOwner = self->m_pOwner;
00211 s->m_parentMenu = self->m_parentMenu;
00212 }
00213
00214 bool RMB::invalid( int val )
00215 {
00216 bool valid = true;
00217
00218 if (val == 1)
00219 s_highlightedAddress = m_parentAddress;
00220
00221 if (s_highlightedAddress.isNull())
00222 valid = false;
00223
00224 return !valid;
00225 }
00226
00227 KBookmark RMB::atAddress(const QString & address)
00228 {
00229 KBookmark bookmark = m_pManager->findByAddress( address );
00230 Q_ASSERT(!bookmark.isNull());
00231 return bookmark;
00232 }
00233
00234 void KBookmarkMenu::slotAboutToShowContextMenu( KPopupMenu*, int, QPopupMenu* contextMenu )
00235 {
00236
00237 if (s_highlightedAddress.isNull())
00238 {
00239 KPopupMenu::contextMenuFocus()->hideContextMenu();
00240 return;
00241 }
00242 contextMenu->clear();
00243 fillContextMenu( contextMenu, s_highlightedAddress, 0 );
00244 }
00245
00246 void RMB::fillContextMenu( QPopupMenu* contextMenu, const QString & address, int val )
00247 {
00248 KBookmark bookmark = atAddress(address);
00249
00250 int id;
00251
00252
00253
00254
00255
00256
00257
00258
00259 id = contextMenu->insertItem( SmallIcon("bookmark_add"), i18n( "Add Bookmark Here" ), recv, SLOT(slotRMBActionInsert(int)) );
00260 contextMenu->setItemParameter( id, val );
00261
00262
00263
00264
00265
00266
00267 }
00268
00269 void RMB::fillContextMenu2( QPopupMenu* contextMenu, const QString & address, int val )
00270 {
00271 KBookmark bookmark = atAddress(address);
00272
00273 int id;
00274
00275 if (bookmark.isGroup()) {
00276 id = contextMenu->insertItem( i18n( "Open Folder in Bookmark Editor" ), recv, SLOT(slotRMBActionEditAt(int)) );
00277 contextMenu->setItemParameter( id, val );
00278 contextMenu->insertSeparator();
00279 id = contextMenu->insertItem( SmallIcon("editdelete"), i18n( "Delete Folder" ), recv, SLOT(slotRMBActionRemove(int)) );
00280 contextMenu->setItemParameter( id, val );
00281 contextMenu->insertSeparator();
00282 id = contextMenu->insertItem( i18n( "Properties" ), recv, SLOT(slotRMBActionProperties(int)) );
00283 contextMenu->setItemParameter( id, val );
00284 }
00285 else
00286 {
00287 id = contextMenu->insertItem( i18n( "Copy Link Address" ), recv, SLOT(slotRMBActionCopyLocation(int)) );
00288 contextMenu->setItemParameter( id, val );
00289 contextMenu->insertSeparator();
00290 id = contextMenu->insertItem( SmallIcon("editdelete"), i18n( "Delete Bookmark" ), recv, SLOT(slotRMBActionRemove(int)) );
00291 contextMenu->setItemParameter( id, val );
00292 contextMenu->insertSeparator();
00293 id = contextMenu->insertItem( i18n( "Properties" ), recv, SLOT(slotRMBActionProperties(int)) );
00294 contextMenu->setItemParameter( id, val );
00295 }
00296 }
00297
00298 void RMB::slotRMBActionEditAt( int val )
00299 {
00300 kdDebug(7043) << "KBookmarkMenu::slotRMBActionEditAt" << s_highlightedAddress << endl;
00301 if (invalid(val)) { hidePopup(); return; }
00302
00303 KBookmark bookmark = atAddress(s_highlightedAddress);
00304
00305 m_pManager->slotEditBookmarksAtAddress( s_highlightedAddress );
00306 }
00307
00308 void RMB::slotRMBActionProperties( int val )
00309 {
00310 kdDebug(7043) << "KBookmarkMenu::slotRMBActionProperties" << s_highlightedAddress << endl;
00311 if (invalid(val)) { hidePopup(); return; }
00312
00313 KBookmark bookmark = atAddress(s_highlightedAddress);
00314
00315 QString folder = bookmark.isGroup() ? QString::null : bookmark.url().pathOrURL();
00316 KBookmarkEditDialog dlg( bookmark.fullText(), folder,
00317 m_pManager, KBookmarkEditDialog::ModifyMode, 0,
00318 0, 0, i18n("Bookmark Properties") );
00319 if ( dlg.exec() != KDialogBase::Accepted )
00320 return;
00321
00322 makeTextNodeMod(bookmark, "title", dlg.finalTitle());
00323 if ( !dlg.finalUrl().isNull() )
00324 {
00325 KURL u = KURL::fromPathOrURL(dlg.finalUrl());
00326 bookmark.internalElement().setAttribute("href", u.url(0, 106));
00327 }
00328
00329 kdDebug(7043) << "Requested move to " << dlg.finalAddress() << "!" << endl;
00330
00331 KBookmarkGroup parentBookmark = atAddress(m_parentAddress).toGroup();
00332 m_pManager->emitChanged( parentBookmark );
00333 }
00334
00335 void RMB::slotRMBActionInsert( int val )
00336 {
00337 kdDebug(7043) << "KBookmarkMenu::slotRMBActionInsert" << s_highlightedAddress << endl;
00338 if (invalid(val)) { hidePopup(); return; }
00339
00340 QString url = m_pOwner->currentURL();
00341 if (url.isEmpty())
00342 {
00343 KMessageBox::error( 0L, i18n("Cannot add bookmark with empty URL."));
00344 return;
00345 }
00346 QString title = m_pOwner->currentTitle();
00347 if (title.isEmpty())
00348 title = url;
00349
00350 KBookmark bookmark = atAddress( s_highlightedAddress );
00351
00352
00353
00354 if (bookmark.isGroup())
00355 {
00356 KBookmarkGroup parentBookmark = bookmark.toGroup();
00357 Q_ASSERT(!parentBookmark.isNull());
00358 parentBookmark.addBookmark( m_pManager, title, KURL( url ) );
00359 m_pManager->emitChanged( parentBookmark );
00360 }
00361 else
00362 {
00363 KBookmarkGroup parentBookmark = bookmark.parentGroup();
00364 Q_ASSERT(!parentBookmark.isNull());
00365 KBookmark newBookmark = parentBookmark.addBookmark( m_pManager, title, KURL( url ) );
00366 parentBookmark.moveItem( newBookmark, parentBookmark.previous(bookmark) );
00367 m_pManager->emitChanged( parentBookmark );
00368 }
00369 }
00370
00371 void RMB::slotRMBActionRemove( int val )
00372 {
00373
00374 if (invalid(val)) { hidePopup(); return; }
00375
00376 KBookmark bookmark = atAddress( s_highlightedAddress );
00377 bool folder = bookmark.isGroup();
00378
00379 if (KMessageBox::warningContinueCancel(
00380 m_parentMenu,
00381 folder ? i18n("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bookmark.text())
00382 : i18n("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bookmark.text()),
00383 folder ? i18n("Bookmark Folder Deletion")
00384 : i18n("Bookmark Deletion"),
00385 KStdGuiItem::del())
00386 != KMessageBox::Continue
00387 )
00388 return;
00389
00390 KBookmarkGroup parentBookmark = atAddress( m_parentAddress ).toGroup();
00391 parentBookmark.deleteBookmark( bookmark );
00392 m_pManager->emitChanged( parentBookmark );
00393 if (m_parentMenu)
00394 m_parentMenu->hide();
00395 }
00396
00397 void RMB::slotRMBActionCopyLocation( int val )
00398 {
00399
00400 if (invalid(val)) { hidePopup(); return; }
00401
00402 KBookmark bookmark = atAddress( s_highlightedAddress );
00403
00404 if ( !bookmark.isGroup() )
00405 {
00406 kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
00407 QClipboard::Selection );
00408 kapp->clipboard()->setData( KBookmarkDrag::newDrag(bookmark, 0),
00409 QClipboard::Clipboard );
00410 }
00411 }
00412
00413 void RMB::hidePopup() {
00414 KPopupMenu::contextMenuFocus()->hideContextMenu();
00415 }
00416
00417
00418
00419
00420
00421 void KBookmarkMenu::fillContextMenu( QPopupMenu* contextMenu, const QString & address, int val )
00422 {
00423 RMB::begin_rmb_action(this);
00424 rmbSelf(this)->fillContextMenu(contextMenu, address, val);
00425 emit aboutToShowContextMenu( rmbSelf(this)->atAddress(address), contextMenu);
00426 rmbSelf(this)->fillContextMenu2(contextMenu, address, val);
00427 }
00428
00429 void KBookmarkMenu::slotRMBActionEditAt( int val )
00430 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionEditAt( val ); }
00431
00432 void KBookmarkMenu::slotRMBActionProperties( int val )
00433 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionProperties( val ); }
00434
00435 void KBookmarkMenu::slotRMBActionInsert( int val )
00436 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionInsert( val ); }
00437
00438 void KBookmarkMenu::slotRMBActionRemove( int val )
00439 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionRemove( val ); }
00440
00441 void KBookmarkMenu::slotRMBActionCopyLocation( int val )
00442 { RMB::begin_rmb_action(this); rmbSelf(this)->slotRMBActionCopyLocation( val ); }
00443
00444 void KBookmarkMenu::slotBookmarksChanged( const QString & groupAddress )
00445 {
00446 if (m_bNSBookmark)
00447 return;
00448
00449 if ( groupAddress == m_parentAddress )
00450 {
00451
00452 m_bDirty = true;
00453 }
00454 else
00455 {
00456
00457 QPtrListIterator<KBookmarkMenu> it( m_lstSubMenus );
00458 for (; it.current(); ++it )
00459 {
00460 it.current()->slotBookmarksChanged( groupAddress );
00461 }
00462 }
00463 }
00464
00465 void KBookmarkMenu::refill()
00466 {
00467
00468 m_lstSubMenus.clear();
00469
00470 QPtrListIterator<KAction> it( m_actions );
00471 for (; it.current(); ++it )
00472 it.current()->unplug( m_parentMenu );
00473
00474 m_parentMenu->clear();
00475 m_actions.clear();
00476
00477 fillBookmarkMenu();
00478 m_parentMenu->adjustSize();
00479 }
00480
00481 void KBookmarkMenu::addAddBookmarksList()
00482 {
00483 if (!kapp->authorizeKAction("bookmarks"))
00484 return;
00485
00486 QString title = i18n( "Bookmark Tabs as Folder..." );
00487
00488 KAction * paAddBookmarksList = new KAction( title,
00489 "bookmarks_list_add",
00490 0,
00491 this,
00492 SLOT( slotAddBookmarksList() ),
00493 m_actionCollection, m_bIsRoot ? "add_bookmarks_list" : 0 );
00494
00495 paAddBookmarksList->setToolTip( i18n( "Add a folder of bookmarks for all open tabs." ) );
00496
00497 paAddBookmarksList->plug( m_parentMenu );
00498 m_actions.append( paAddBookmarksList );
00499 }
00500
00501 void KBookmarkMenu::addAddBookmark()
00502 {
00503 if (!kapp->authorizeKAction("bookmarks"))
00504 return;
00505
00506 QString title = i18n( "Add Bookmark" );
00507
00508 KAction * paAddBookmarks = new KAction( title,
00509 "bookmark_add",
00510 m_bIsRoot && m_bAddShortcuts ? KStdAccel::addBookmark() : KShortcut(),
00511 this,
00512 SLOT( slotAddBookmark() ),
00513 m_actionCollection, m_bIsRoot ? "add_bookmark" : 0 );
00514
00515 paAddBookmarks->setToolTip( i18n( "Add a bookmark for the current document" ) );
00516
00517 paAddBookmarks->plug( m_parentMenu );
00518 m_actions.append( paAddBookmarks );
00519 }
00520
00521 void KBookmarkMenu::addEditBookmarks()
00522 {
00523 if (!kapp->authorizeKAction("bookmarks"))
00524 return;
00525
00526 KAction * m_paEditBookmarks = KStdAction::editBookmarks( m_pManager, SLOT( slotEditBookmarks() ),
00527 m_actionCollection, "edit_bookmarks" );
00528 m_paEditBookmarks->plug( m_parentMenu );
00529 m_paEditBookmarks->setToolTip( i18n( "Edit your bookmark collection in a separate window" ) );
00530 m_actions.append( m_paEditBookmarks );
00531 }
00532
00533 void KBookmarkMenu::addNewFolder()
00534 {
00535 if (!kapp->authorizeKAction("bookmarks"))
00536 return;
00537
00538 QString title = i18n( "&New Bookmark Folder..." );
00539 int p;
00540 while ( ( p = title.find( '&' ) ) >= 0 )
00541 title.remove( p, 1 );
00542
00543 KAction * paNewFolder = new KAction( title,
00544 "folder_new",
00545 0,
00546 this,
00547 SLOT( slotNewFolder() ),
00548 m_actionCollection );
00549
00550 paNewFolder->setToolTip( i18n( "Create a new bookmark folder in this menu" ) );
00551
00552 paNewFolder->plug( m_parentMenu );
00553 m_actions.append( paNewFolder );
00554 }
00555
00556 void KBookmarkMenu::fillBookmarkMenu()
00557 {
00558 if (!kapp->authorizeKAction("bookmarks"))
00559 return;
00560
00561 if ( m_bIsRoot )
00562 {
00563 if ( m_bAddBookmark )
00564 {
00565 addAddBookmark();
00566 if ( extOwner() )
00567 addAddBookmarksList();
00568 }
00569
00570 addEditBookmarks();
00571
00572 if ( m_bAddBookmark && !KBookmarkSettings::self()->m_advancedaddbookmark )
00573 addNewFolder();
00574 }
00575
00576 if ( m_bIsRoot
00577 && KBookmarkManager::userBookmarksFile() == m_pManager->path() )
00578 {
00579 bool haveSep = false;
00580
00581 QValueList<QString> keys = KBookmarkMenu::dynamicBookmarksList();
00582 QValueList<QString>::const_iterator it;
00583 for ( it = keys.begin(); it != keys.end(); ++it )
00584 {
00585 DynMenuInfo info;
00586 info = showDynamicBookmarks((*it));
00587
00588 if ( !info.show || !QFile::exists( info.location ) )
00589 continue;
00590
00591 if (!haveSep)
00592 {
00593 m_parentMenu->insertSeparator();
00594 haveSep = true;
00595 }
00596
00597 KActionMenu * actionMenu;
00598 actionMenu = new KImportedBookmarksActionMenu(
00599 info.name, info.type,
00600 m_actionCollection, "kbookmarkmenu" );
00601
00602 actionMenu->setProperty( "type", info.type );
00603 actionMenu->setProperty( "location", info.location );
00604
00605 actionMenu->plug( m_parentMenu );
00606 m_actions.append( actionMenu );
00607
00608 KBookmarkMenu *subMenu =
00609 new KBookmarkMenu( m_pManager, m_pOwner, actionMenu->popupMenu(),
00610 m_actionCollection, false,
00611 m_bAddBookmark, QString::null );
00612 connect( subMenu, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ),
00613 this, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ));
00614 m_lstSubMenus.append(subMenu);
00615
00616 connect(actionMenu->popupMenu(), SIGNAL(aboutToShow()), subMenu, SLOT(slotNSLoad()));
00617 }
00618 }
00619
00620 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00621 Q_ASSERT(!parentBookmark.isNull());
00622 bool separatorInserted = false;
00623 for ( KBookmark bm = parentBookmark.first(); !bm.isNull(); bm = parentBookmark.next(bm) )
00624 {
00625 QString text = KStringHandler::csqueeze(bm.fullText(), 60);
00626 text.replace( '&', "&&" );
00627 if ( !separatorInserted && m_bIsRoot) {
00628
00629 m_parentMenu->insertSeparator();
00630 separatorInserted = true;
00631 }
00632 if ( !bm.isGroup() )
00633 {
00634 if ( bm.isSeparator() )
00635 {
00636 m_parentMenu->insertSeparator();
00637 }
00638 else
00639 {
00640
00641 KAction * action = new KBookmarkAction( text, bm.icon(), 0, m_actionCollection, 0 );
00642 connect(action, SIGNAL( activated ( KAction::ActivationReason, Qt::ButtonState )),
00643 this, SLOT( slotBookmarkSelected( KAction::ActivationReason, Qt::ButtonState ) ));
00644
00645 action->setProperty( "url", bm.url().url() );
00646 action->setProperty( "address", bm.address() );
00647
00648 action->setToolTip( bm.url().pathOrURL() );
00649
00650 action->plug( m_parentMenu );
00651 m_actions.append( action );
00652 }
00653 }
00654 else
00655 {
00656
00657 KActionMenu * actionMenu = new KBookmarkActionMenu( text, bm.icon(),
00658 m_actionCollection,
00659 "kbookmarkmenu" );
00660 actionMenu->setProperty( "address", bm.address() );
00661 actionMenu->plug( m_parentMenu );
00662 m_actions.append( actionMenu );
00663
00664 KBookmarkMenu *subMenu = new KBookmarkMenu( m_pManager, m_pOwner, actionMenu->popupMenu(),
00665 m_actionCollection, false,
00666 m_bAddBookmark,
00667 bm.address() );
00668
00669 connect(subMenu, SIGNAL( aboutToShowContextMenu( const KBookmark &, QPopupMenu * ) ),
00670 this, SIGNAL( aboutToShowContextMenu( const KBookmark &, QPopupMenu * ) ));
00671 connect(subMenu, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ),
00672 this, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ));
00673 m_lstSubMenus.append( subMenu );
00674 }
00675 }
00676
00677 if ( !m_bIsRoot && m_bAddBookmark )
00678 {
00679 if ( m_parentMenu->count() > 0 )
00680 m_parentMenu->insertSeparator();
00681
00682 if ( KBookmarkSettings::self()->m_quickactions )
00683 {
00684 KActionMenu * actionMenu = new KActionMenu( i18n("Quick Actions"), m_actionCollection, 0L );
00685 fillContextMenu( actionMenu->popupMenu(), m_parentAddress, 1 );
00686 actionMenu->plug( m_parentMenu );
00687 m_actions.append( actionMenu );
00688 }
00689 else
00690 {
00691 addAddBookmark();
00692 if ( extOwner() )
00693 addAddBookmarksList();
00694 addNewFolder();
00695 }
00696 }
00697 }
00698
00699 void KBookmarkMenu::slotAddBookmarksList()
00700 {
00701 KExtendedBookmarkOwner *extOwner = dynamic_cast<KExtendedBookmarkOwner*>(m_pOwner);
00702 if (!extOwner)
00703 {
00704 kdWarning() << "erm, sorry ;-)" << endl;
00705 return;
00706 }
00707
00708 KExtendedBookmarkOwner::QStringPairList list;
00709 extOwner->fillBookmarksList( list );
00710
00711 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00712 Q_ASSERT(!parentBookmark.isNull());
00713 KBookmarkGroup group = parentBookmark.createNewFolder( m_pManager );
00714 if ( group.isNull() )
00715 return;
00716
00717 KExtendedBookmarkOwner::QStringPairList::const_iterator it;
00718 for ( it = list.begin(); it != list.end(); ++it )
00719 group.addBookmark( m_pManager, (*it).first, KURL((*it).second) );
00720
00721 m_pManager->emitChanged( parentBookmark );
00722 }
00723
00724
00725 void KBookmarkMenu::slotAddBookmark()
00726 {
00727 KBookmarkGroup parentBookmark;
00728 parentBookmark = m_pManager->addBookmarkDialog(m_pOwner->currentURL(), m_pOwner->currentTitle(), m_parentAddress);
00729 if (!parentBookmark.isNull())
00730 m_pManager->emitChanged( parentBookmark );
00731 }
00732
00733 void KBookmarkMenu::slotNewFolder()
00734 {
00735 if ( !m_pOwner ) return;
00736 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00737 Q_ASSERT(!parentBookmark.isNull());
00738 KBookmarkGroup group = parentBookmark.createNewFolder( m_pManager );
00739 if ( !group.isNull() )
00740 {
00741 KBookmarkGroup parentGroup = group.parentGroup();
00742 m_pManager->emitChanged( parentGroup );
00743 }
00744 }
00745
00746 void KBookmarkMenu::slotBookmarkSelected( KAction::ActivationReason , Qt::ButtonState state )
00747 {
00748 kdDebug(7043) << "KBookmarkMenu::slotBookmarkSelected()" << endl;
00749 if ( !m_pOwner ) return;
00750 const KAction* action = dynamic_cast<const KAction *>(sender());
00751 if(action)
00752 {
00753 const QString& url = sender()->property("url").toString();
00754 m_pOwner->openBookmarkURL( url );
00755 emit openBookmark( url, state );
00756 }
00757 }
00758
00759 void KBookmarkMenu::slotBookmarkSelected()
00760 {
00761 slotBookmarkSelected(KAction::PopupMenuActivation, Qt::NoButton);
00762 }
00763
00764 KExtendedBookmarkOwner* KBookmarkMenu::extOwner()
00765 {
00766 return dynamic_cast<KExtendedBookmarkOwner*>(m_pOwner);
00767 }
00768
00769 void KBookmarkMenu::slotNSLoad()
00770 {
00771
00772 m_parentMenu->disconnect(SIGNAL(aboutToShow()));
00773
00774
00775 KBookmarkMenuNSImporter importer( m_pManager, this, m_actionCollection );
00776 importer.openBookmarks(s_highlightedImportLocation, s_highlightedImportType);
00777 }
00778
00779
00780
00781
00782
00783 KBookmarkEditFields::KBookmarkEditFields(QWidget *main, QBoxLayout *vbox, FieldsSet fieldsSet)
00784 {
00785 bool isF = (fieldsSet != FolderFieldsSet);
00786
00787 QGridLayout *grid = new QGridLayout( vbox, 2, isF ? 2 : 1 );
00788
00789 m_title = new KLineEdit( main );
00790 grid->addWidget( m_title, 0, 1 );
00791 grid->addWidget( new QLabel( m_title, i18n( "Name:" ), main ), 0, 0 );
00792 m_title->setFocus();
00793 if (isF)
00794 {
00795 m_url = new KLineEdit( main );
00796 grid->addWidget( m_url, 1, 1 );
00797 grid->addWidget( new QLabel( m_url, i18n( "Location:" ), main ), 1, 0 );
00798 }
00799 else
00800 {
00801 m_url = 0;
00802 }
00803
00804 main->setMinimumSize( 300, 0 );
00805 }
00806
00807 void KBookmarkEditFields::setName(const QString &str)
00808 {
00809 m_title->setText(str);
00810 }
00811
00812 void KBookmarkEditFields::setLocation(const QString &str)
00813 {
00814 m_url->setText(str);
00815 }
00816
00817
00818
00819
00820
00821
00822 KBookmarkEditDialog::KBookmarkEditDialog(const QString& title, const QString& url, KBookmarkManager * mgr, BookmarkEditType editType, const QString& address,
00823 QWidget * parent, const char * name, const QString& caption )
00824 : KDialogBase(parent, name, true, caption,
00825 (editType == InsertionMode) ? (User1|Ok|Cancel) : (Ok|Cancel),
00826 Ok, false, KGuiItem()),
00827 m_folderTree(0), m_mgr(mgr), m_editType(editType), m_address(address)
00828 {
00829 setButtonOK( (editType == InsertionMode) ? KGuiItem( i18n( "&Add" ), "bookmark_add") : i18n( "&Update" ) );
00830 if (editType == InsertionMode) {
00831 setButtonGuiItem( User1, KGuiItem( i18n( "&New Folder..." ), "folder_new") );
00832 }
00833
00834 bool folder = url.isNull();
00835
00836 m_main = new QWidget( this );
00837 setMainWidget( m_main );
00838
00839 QBoxLayout *vbox = new QVBoxLayout( m_main, 0, spacingHint() );
00840 KBookmarkEditFields::FieldsSet fs =
00841 folder ? KBookmarkEditFields::FolderFieldsSet
00842 : KBookmarkEditFields::BookmarkFieldsSet;
00843 m_fields = new KBookmarkEditFields(m_main, vbox, fs);
00844 m_fields->setName(title);
00845 if ( !folder )
00846 m_fields->setLocation(url);
00847
00848 if ( editType == InsertionMode )
00849 {
00850 m_folderTree = KBookmarkFolderTree::createTree( m_mgr, m_main, name, m_address );
00851 connect( m_folderTree, SIGNAL( doubleClicked(QListViewItem*) ),
00852 this, SLOT( slotDoubleClicked(QListViewItem*) ) );
00853 vbox->addWidget( m_folderTree );
00854 connect( this, SIGNAL( user1Clicked() ), SLOT( slotUser1() ) );
00855 }
00856 }
00857
00858 void KBookmarkEditDialog::slotDoubleClicked( QListViewItem* item )
00859 {
00860 Q_ASSERT( m_folderTree );
00861 m_folderTree->setCurrentItem( item );
00862 accept();
00863 }
00864
00865 void KBookmarkEditDialog::slotOk()
00866 {
00867 accept();
00868 }
00869
00870 void KBookmarkEditDialog::slotCancel()
00871 {
00872 reject();
00873 }
00874
00875 QString KBookmarkEditDialog::finalAddress() const
00876 {
00877 Q_ASSERT( m_folderTree );
00878 return KBookmarkFolderTree::selectedAddress( m_folderTree );
00879 }
00880
00881 QString KBookmarkEditDialog::finalUrl() const
00882 {
00883 return m_fields->m_url ? m_fields->m_url->text() : QString::null;
00884 }
00885
00886 QString KBookmarkEditDialog::finalTitle() const
00887 {
00888 return m_fields->m_title ? m_fields->m_title->text() : QString::null;
00889 }
00890
00891 void KBookmarkEditDialog::slotUser1()
00892 {
00893
00894 Q_ASSERT( m_folderTree );
00895
00896 QString address = KBookmarkFolderTree::selectedAddress( m_folderTree );
00897 if ( address.isNull() ) return;
00898 KBookmarkGroup bm = m_mgr->findByAddress( address ).toGroup();
00899 Q_ASSERT(!bm.isNull());
00900 Q_ASSERT(m_editType == InsertionMode);
00901
00902 KBookmarkGroup group = bm.createNewFolder( m_mgr );
00903 if ( !group.isNull() )
00904 {
00905 KBookmarkGroup parentGroup = group.parentGroup();
00906 m_mgr->emitChanged( parentGroup );
00907 }
00908 KBookmarkFolderTree::fillTree( m_folderTree, m_mgr );
00909 }
00910
00911
00912
00913
00914
00915 static void fillGroup( QListView* listview, KBookmarkFolderTreeItem * parentItem, KBookmarkGroup group, bool expandOpenGroups = true, const QString& address = QString::null )
00916 {
00917 bool noSubGroups = true;
00918 KBookmarkFolderTreeItem * lastItem = 0L;
00919 KBookmarkFolderTreeItem * item = 0L;
00920 for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk) )
00921 {
00922 if ( bk.isGroup() )
00923 {
00924 KBookmarkGroup grp = bk.toGroup();
00925 item = new KBookmarkFolderTreeItem( parentItem, lastItem, grp );
00926 fillGroup( listview, item, grp, expandOpenGroups, address );
00927 if ( expandOpenGroups && grp.isOpen() )
00928 item->setOpen( true );
00929 lastItem = item;
00930 noSubGroups = false;
00931 }
00932 if (bk.address() == address) {
00933 listview->setCurrentItem( lastItem );
00934 listview->ensureItemVisible( item );
00935 }
00936 }
00937 if ( noSubGroups ) {
00938 parentItem->setOpen( true );
00939 }
00940 }
00941
00942 QListView* KBookmarkFolderTree::createTree( KBookmarkManager* mgr, QWidget* parent, const char* name, const QString& address )
00943 {
00944 QListView *listview = new QListView( parent, name );
00945
00946 listview->setRootIsDecorated( false );
00947 listview->header()->hide();
00948 listview->addColumn( i18n("Bookmark"), 200 );
00949 listview->setSorting( -1, false );
00950 listview->setSelectionMode( QListView::Single );
00951 listview->setAllColumnsShowFocus( true );
00952 listview->setResizeMode( QListView::AllColumns );
00953 listview->setMinimumSize( 60, 100 );
00954
00955 fillTree( listview, mgr, address );
00956
00957 return listview;
00958 }
00959
00960 void KBookmarkFolderTree::fillTree( QListView *listview, KBookmarkManager* mgr, const QString& address )
00961 {
00962 listview->clear();
00963
00964 KBookmarkGroup root = mgr->root();
00965 KBookmarkFolderTreeItem * rootItem = new KBookmarkFolderTreeItem( listview, root );
00966 listview->setCurrentItem( rootItem );
00967 rootItem->setSelected( true );
00968 fillGroup( listview, rootItem, root, (address == root.groupAddress() || address.isNull()) ? true : false, address );
00969 rootItem->setOpen( true );
00970 }
00971
00972 static KBookmarkFolderTreeItem* ft_cast( QListViewItem *i )
00973 {
00974 return static_cast<KBookmarkFolderTreeItem*>( i );
00975 }
00976
00977 QString KBookmarkFolderTree::selectedAddress( QListView *listview )
00978 {
00979 if ( !listview)
00980 return QString::null;
00981 KBookmarkFolderTreeItem *item = ft_cast( listview->currentItem() );
00982 return item ? item->m_bookmark.address() : QString::null;
00983 }
00984
00985 void KBookmarkFolderTree::setAddress( QListView *listview, const QString & address )
00986 {
00987 KBookmarkFolderTreeItem* it = ft_cast( listview->firstChild() );
00988 while ( true ) {
00989 kdDebug(7043) << it->m_bookmark.address() << endl;
00990 it = ft_cast( it->itemBelow() );
00991 if ( !it )
00992 return;
00993 if ( it->m_bookmark.address() == address )
00994 break;
00995 }
00996 it->setSelected( true );
00997 listview->setCurrentItem( it );
00998 }
00999
01000
01001
01002
01003
01004
01005 KBookmarkFolderTreeItem::KBookmarkFolderTreeItem( QListView *parent, const KBookmark & gp )
01006 : QListViewItem(parent, i18n("Bookmarks")), m_bookmark(gp)
01007 {
01008 setPixmap(0, SmallIcon("bookmark"));
01009 setExpandable(true);
01010 }
01011
01012
01013 KBookmarkFolderTreeItem::KBookmarkFolderTreeItem( KBookmarkFolderTreeItem *parent, QListViewItem *after, const KBookmarkGroup & gp )
01014 : QListViewItem(parent, after, gp.fullText()), m_bookmark(gp)
01015 {
01016 setPixmap(0, SmallIcon( gp.icon() ) );
01017 setExpandable(true);
01018 }
01019
01020
01021
01022
01023
01024
01025
01026
01027 void KBookmarkMenuNSImporter::openNSBookmarks()
01028 {
01029 openBookmarks( KNSBookmarkImporter::netscapeBookmarksFile(), "netscape" );
01030 }
01031
01032 void KBookmarkMenuNSImporter::openBookmarks( const QString &location, const QString &type )
01033 {
01034 mstack.push(m_menu);
01035
01036 KBookmarkImporterBase *importer = KBookmarkImporterBase::factory(type);
01037 if (!importer)
01038 return;
01039 importer->setFilename(location);
01040 connectToImporter(*importer);
01041 importer->parse();
01042
01043 delete importer;
01044 }
01045
01046 void KBookmarkMenuNSImporter::connectToImporter(const QObject &importer)
01047 {
01048 connect( &importer, SIGNAL( newBookmark( const QString &, const QCString &, const QString & ) ),
01049 SLOT( newBookmark( const QString &, const QCString &, const QString & ) ) );
01050 connect( &importer, SIGNAL( newFolder( const QString &, bool, const QString & ) ),
01051 SLOT( newFolder( const QString &, bool, const QString & ) ) );
01052 connect( &importer, SIGNAL( newSeparator() ), SLOT( newSeparator() ) );
01053 connect( &importer, SIGNAL( endFolder() ), SLOT( endFolder() ) );
01054 }
01055
01056 void KBookmarkMenuNSImporter::newBookmark( const QString & text, const QCString & url, const QString & )
01057 {
01058 QString _text = KStringHandler::csqueeze(text);
01059 _text.replace( '&', "&&" );
01060 KAction * action = new KBookmarkAction(_text, "html", 0, 0, "", m_actionCollection, 0);
01061 connect(action, SIGNAL( activated ( KAction::ActivationReason, Qt::ButtonState )),
01062 m_menu, SLOT( slotBookmarkSelected( KAction::ActivationReason, Qt::ButtonState ) ));
01063 action->setProperty( "url", url );
01064 action->setToolTip( url );
01065 action->plug( mstack.top()->m_parentMenu );
01066 mstack.top()->m_actions.append( action );
01067 }
01068
01069 void KBookmarkMenuNSImporter::newFolder( const QString & text, bool, const QString & )
01070 {
01071 QString _text = KStringHandler::csqueeze(text);
01072 _text.replace( '&', "&&" );
01073 KActionMenu * actionMenu = new KActionMenu( _text, "folder", m_actionCollection, 0L );
01074 actionMenu->plug( mstack.top()->m_parentMenu );
01075 mstack.top()->m_actions.append( actionMenu );
01076 KBookmarkMenu *subMenu = new KBookmarkMenu( m_pManager, m_menu->m_pOwner, actionMenu->popupMenu(),
01077 m_actionCollection, false,
01078 m_menu->m_bAddBookmark, QString::null );
01079 connect( subMenu, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ),
01080 m_menu, SIGNAL( openBookmark( const QString &, Qt::ButtonState ) ));
01081 mstack.top()->m_lstSubMenus.append( subMenu );
01082
01083 mstack.push(subMenu);
01084 }
01085
01086 void KBookmarkMenuNSImporter::newSeparator()
01087 {
01088 mstack.top()->m_parentMenu->insertSeparator();
01089 }
01090
01091 void KBookmarkMenuNSImporter::endFolder()
01092 {
01093 mstack.pop();
01094 }
01095
01096
01097
01098
01099
01100 KBookmarkMenu::DynMenuInfo KBookmarkMenu::showDynamicBookmarks( const QString &id )
01101 {
01102 KConfig config("kbookmarkrc", false, false);
01103 config.setGroup("Bookmarks");
01104
01105 DynMenuInfo info;
01106 info.show = false;
01107
01108 if (!config.hasKey("DynamicMenus")) {
01109
01110 if (id == "netscape") {
01111 KBookmarkManager *manager = KBookmarkManager::userBookmarksManager();
01112 info.show = manager->root().internalElement().attribute("hide_nsbk") != "yes";
01113 info.location = KNSBookmarkImporter::netscapeBookmarksFile();
01114 info.type = "netscape";
01115 info.name = i18n("Netscape Bookmarks");
01116 }
01117
01118 } else {
01119
01120 if (config.hasGroup("DynamicMenu-" + id)) {
01121 config.setGroup("DynamicMenu-" + id);
01122 info.show = config.readBoolEntry("Show");
01123 info.location = config.readPathEntry("Location");
01124 info.type = config.readEntry("Type");
01125 info.name = config.readEntry("Name");
01126 }
01127 }
01128
01129 return info;
01130 }
01131
01132 QStringList KBookmarkMenu::dynamicBookmarksList()
01133 {
01134 KConfig config("kbookmarkrc", false, false);
01135 config.setGroup("Bookmarks");
01136
01137 QStringList mlist;
01138 if (config.hasKey("DynamicMenus"))
01139 mlist = config.readListEntry("DynamicMenus");
01140 else
01141 mlist << "netscape";
01142
01143 return mlist;
01144 }
01145
01146 void KBookmarkMenu::setDynamicBookmarks(const QString &id, const DynMenuInfo &newMenu)
01147 {
01148 KConfig config("kbookmarkrc", false, false);
01149
01150
01151 config.setGroup("DynamicMenu-" + id);
01152 config.writeEntry("Show", newMenu.show);
01153 config.writePathEntry("Location", newMenu.location);
01154 config.writeEntry("Type", newMenu.type);
01155 config.writeEntry("Name", newMenu.name);
01156
01157 QStringList elist;
01158
01159 config.setGroup("Bookmarks");
01160 if (!config.hasKey("DynamicMenus")) {
01161 if (newMenu.type != "netscape") {
01162
01163
01164 config.setGroup("DynamicMenu-" "netscape");
01165 DynMenuInfo xbelSetting;
01166 xbelSetting = showDynamicBookmarks("netscape");
01167 config.writeEntry("Show", xbelSetting.show);
01168 config.writePathEntry("Location", xbelSetting.location);
01169 config.writeEntry("Type", xbelSetting.type);
01170 config.writeEntry("Name", xbelSetting.name);
01171 }
01172 } else {
01173 elist = config.readListEntry("DynamicMenus");
01174 }
01175
01176
01177 config.setGroup("Bookmarks");
01178 if (elist.contains(id) < 1) {
01179 elist << id;
01180 config.writeEntry("DynamicMenus", elist);
01181 }
01182
01183 config.sync();
01184 }
01185
01186 #include "kbookmarkmenu.moc"
01187 #include "kbookmarkmenu_p.moc"