00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024
00025 #include <assert.h>
00026 #include <dirent.h>
00027 #include <errno.h>
00028 #include <fcntl.h>
00029 #include <stddef.h>
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032
00033 #include "kcelbookmarkmenu.h"
00034 #include <kbookmarkmenu.h>
00035 #include "kbookmarkimporter.h"
00036 #include "kcelbookmarkowner.h"
00037
00038 #include <qfile.h>
00039 #include <qregexp.h>
00040
00041 #include <kaction.h>
00042 #include <kdebug.h>
00043 #include <klocale.h>
00044 #include <kmessagebox.h>
00045 #include <kpopupmenu.h>
00046 #include <kstdaccel.h>
00047 #include <kstdaction.h>
00048
00049 template class QPtrList<KCelBookmarkMenu>;
00050
00051
00052
00053
00054
00055
00056
00057 KCelBookmarkMenu::KCelBookmarkMenu( KBookmarkManager* mgr,
00058 KCelBookmarkOwner * _owner, KPopupMenu * _parentMenu,
00059 KActionCollection *collec, bool _isRoot, bool _add,
00060 const QString & parentAddress )
00061 : m_bIsRoot(_isRoot), m_bAddBookmark(_add),
00062 m_pManager(mgr), m_pOwner(_owner),
00063 m_parentMenu( _parentMenu ),
00064 m_actionCollection( collec ),
00065 m_parentAddress( parentAddress )
00066 {
00067 m_lstSubMenus.setAutoDelete( true );
00068 m_actions.setAutoDelete( true );
00069
00070 m_bNSBookmark = m_parentAddress.isNull();
00071 if ( !m_bNSBookmark )
00072 {
00073
00074
00075 connect( _parentMenu, SIGNAL( aboutToShow() ),
00076 SLOT( slotAboutToShow() ) );
00077
00078 if ( m_bIsRoot )
00079 {
00080 connect( m_pManager, SIGNAL( changed(const QString &, const QString &) ),
00081 SLOT( slotBookmarksChanged(const QString &) ) );
00082 }
00083 }
00084
00085
00086 if ( m_bIsRoot )
00087 {
00088 if ( m_bAddBookmark ) {
00089 addAddBookmark();
00090 }
00091
00092 addEditBookmarks();
00093 }
00094
00095 m_bDirty = true;
00096 }
00097
00098 KCelBookmarkMenu::~KCelBookmarkMenu()
00099 {
00100
00101 QPtrListIterator<KAction> it( m_actions );
00102 for (; it.current(); ++it )
00103 it.current()->unplugAll();
00104
00105 m_lstSubMenus.clear();
00106 m_actions.clear();
00107 }
00108
00109 void KCelBookmarkMenu::ensureUpToDate()
00110 {
00111 slotAboutToShow();
00112 }
00113
00114
00115 void KCelBookmarkMenu::slotAboutToShow()
00116 {
00117
00118 if ( m_bDirty )
00119 {
00120 m_bDirty = false;
00121 refill();
00122 }
00123 }
00124
00125 void KCelBookmarkMenu::slotBookmarksChanged( const QString & groupAddress )
00126 {
00127 if (m_bNSBookmark)
00128 return;
00129
00130 if ( groupAddress == m_parentAddress )
00131 {
00132
00133 m_bDirty = true;
00134 }
00135 else
00136 {
00137
00138 QPtrListIterator<KCelBookmarkMenu> it( m_lstSubMenus );
00139 for (; it.current(); ++it )
00140 {
00141 it.current()->slotBookmarksChanged( groupAddress );
00142 }
00143 }
00144 }
00145
00146 void KCelBookmarkMenu::refill()
00147 {
00148
00149 m_lstSubMenus.clear();
00150
00151 QPtrListIterator<KAction> it( m_actions );
00152 for (; it.current(); ++it )
00153 it.current()->unplug( m_parentMenu );
00154
00155 m_parentMenu->clear();
00156 m_actions.clear();
00157
00158 fillBookmarkMenu();
00159 m_parentMenu->adjustSize();
00160 }
00161
00162 void KCelBookmarkMenu::addAddBookmark()
00163 {
00164 KAction * paAddBookmarks = new KAction( i18n( "&Add Bookmark" ),
00165 "bookmark_add",
00166 m_bIsRoot ? ALT + Key_B : 0,
00167 this,
00168 SLOT( slotAddBookmark() ),
00169 m_actionCollection, m_bIsRoot ? "add_bookmark" : 0 );
00170
00171 paAddBookmarks->setStatusText( i18n( "Add a bookmark for the current document" ) );
00172
00173 paAddBookmarks->plug( m_parentMenu );
00174 m_actions.append( paAddBookmarks );
00175 addAddRelativeBookmark();
00176 addAddSettingsBookmark();
00177 }
00178
00179 void KCelBookmarkMenu::addAddRelativeBookmark()
00180 {
00181 KAction * paAddBookmarks = new KAction( i18n( "Add &Relative Bookmark" ),
00182 "bookmark_add",
00183 m_bIsRoot ? ALT + Key_R : 0,
00184 this,
00185 SLOT( slotAddRelativeBookmark() ),
00186 m_actionCollection, m_bIsRoot ? "add_relative_bookmark" : 0 );
00187
00188 paAddBookmarks->setStatusText( i18n( "Add a relative bookmark for the current document" ) );
00189
00190 paAddBookmarks->plug( m_parentMenu );
00191 m_actions.append( paAddBookmarks );
00192 }
00193
00194 void KCelBookmarkMenu::addAddSettingsBookmark()
00195 {
00196 KAction * paAddBookmarks = new KAction( i18n( "Add &Settings Bookmark" ),
00197 "bookmark_add",
00198 m_bIsRoot ? CTRL + ALT + Key_S : 0,
00199 this,
00200 SLOT( slotAddSettingsBookmark() ),
00201 m_actionCollection, m_bIsRoot ? "add_settings_bookmark" : 0 );
00202
00203 paAddBookmarks->setStatusText( i18n( "Add a settings bookmark for the current document" ) );
00204
00205 paAddBookmarks->plug( m_parentMenu );
00206 m_actions.append( paAddBookmarks );
00207 }
00208
00209 void KCelBookmarkMenu::addEditBookmarks()
00210 {
00211 KAction * m_paEditBookmarks = KStdAction::editBookmarks( m_pManager, SLOT( slotEditBookmarks() ),
00212 m_actionCollection, "edit_bookmarks" );
00213 m_paEditBookmarks->plug( m_parentMenu );
00214 m_paEditBookmarks->setStatusText( i18n( "Edit your bookmark collection in a separate window" ) );
00215 m_actions.append( m_paEditBookmarks );
00216 }
00217
00218 void KCelBookmarkMenu::addNewFolder()
00219 {
00220 KAction * paNewFolder = new KAction( i18n( "&New Folder..." ),
00221 "folder_new",
00222 0,
00223 this,
00224 SLOT( slotNewFolder() ),
00225 m_actionCollection );
00226
00227 paNewFolder->setStatusText( i18n( "Create a new bookmark folder in this menu" ) );
00228
00229 paNewFolder->plug( m_parentMenu );
00230 m_actions.append( paNewFolder );
00231 }
00232
00233 void KCelBookmarkMenu::fillBookmarkMenu()
00234 {
00235 if ( m_bIsRoot )
00236 {
00237 if ( m_bAddBookmark )
00238 addAddBookmark();
00239
00240 addEditBookmarks();
00241
00242 if ( m_bAddBookmark )
00243 addNewFolder();
00244
00245 }
00246
00247 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00248 Q_ASSERT(!parentBookmark.isNull());
00249 bool separatorInserted = false;
00250 for ( KBookmark bm = parentBookmark.first(); !bm.isNull(); bm = parentBookmark.next(bm) )
00251 {
00252 QString text = bm.text();
00253 text.replace( QRegExp( "&" ), "&&" );
00254 if ( !separatorInserted && m_bIsRoot) {
00255 m_parentMenu->insertSeparator();
00256 separatorInserted = true;
00257 }
00258 if ( !bm.isGroup() )
00259 {
00260 if ( bm.isSeparator() )
00261 {
00262 m_parentMenu->insertSeparator();
00263 }
00264 else
00265 {
00266
00267
00268 KAction * action = new KAction( text, bm.icon(), 0,
00269 this, SLOT( slotBookmarkSelected() ),
00270 m_actionCollection, bm.url().url().utf8() );
00271
00272 action->setStatusText( bm.url().prettyURL() );
00273
00274 action->plug( m_parentMenu );
00275 m_actions.append( action );
00276 }
00277 }
00278 else
00279 {
00280
00281 KActionMenu * actionMenu = new KActionMenu( text, bm.icon(),
00282 m_actionCollection, 0L );
00283 actionMenu->plug( m_parentMenu );
00284 m_actions.append( actionMenu );
00285 KCelBookmarkMenu *subMenu = new KCelBookmarkMenu( m_pManager, m_pOwner, actionMenu->popupMenu(),
00286 m_actionCollection, false,
00287 m_bAddBookmark,
00288 bm.address() );
00289 m_lstSubMenus.append( subMenu );
00290 }
00291 }
00292
00293 if ( !m_bIsRoot && m_bAddBookmark )
00294 {
00295 m_parentMenu->insertSeparator();
00296 addAddBookmark();
00297 addNewFolder();
00298 }
00299 }
00300
00301 void KCelBookmarkMenu::slotAddRelativeBookmark()
00302 {
00303 Url Url = m_pOwner->currentUrl(Url::Relative);
00304 QString url = QString(Url.getAsString().c_str());;
00305 if (url.isEmpty())
00306 {
00307 KMessageBox::error( 0L, i18n("Can't add bookmark with empty URL"));
00308 return;
00309 }
00310 QString title = QString(Url.getName().c_str());
00311 if (title.isEmpty())
00312 title = url;
00313
00314 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00315 Q_ASSERT(!parentBookmark.isNull());
00316
00317 KBookmark ch = parentBookmark.first();
00318 int count = 1;
00319 QString uniqueTitle = title;
00320 do
00321 {
00322 while ( !ch.isNull() )
00323 {
00324 if ( uniqueTitle == ch.text() )
00325 {
00326
00327 if ( url != ch.url().url() )
00328 {
00329 uniqueTitle = title + QString(" (%1)").arg(++count);
00330
00331 ch = parentBookmark.first();
00332 break;
00333 }
00334 else
00335 {
00336
00337 return;
00338 }
00339 }
00340 ch = parentBookmark.next( ch );
00341 }
00342 } while ( !ch.isNull() );
00343
00344 parentBookmark.addBookmark( m_pManager, uniqueTitle, url, m_pOwner->currentIcon() );
00345 m_pManager->emitChanged( parentBookmark );
00346 }
00347
00348 void KCelBookmarkMenu::slotAddSettingsBookmark()
00349 {
00350 Url Url = m_pOwner->currentUrl(Url::Settings);
00351 QString url = QString(Url.getAsString().c_str());;
00352 if (url.isEmpty())
00353 {
00354 KMessageBox::error( 0L, i18n("Can't add bookmark with empty URL"));
00355 return;
00356 }
00357 QString title = QString(Url.getName().c_str());
00358 if (title.isEmpty())
00359 title = url;
00360
00361 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00362 Q_ASSERT(!parentBookmark.isNull());
00363
00364 KBookmark ch = parentBookmark.first();
00365 int count = 1;
00366 QString uniqueTitle = title;
00367 do
00368 {
00369 while ( !ch.isNull() )
00370 {
00371 if ( uniqueTitle == ch.text() )
00372 {
00373
00374 if ( url != ch.url().url() )
00375 {
00376 uniqueTitle = title + QString(" (%1)").arg(++count);
00377
00378 ch = parentBookmark.first();
00379 break;
00380 }
00381 else
00382 {
00383
00384 return;
00385 }
00386 }
00387 ch = parentBookmark.next( ch );
00388 }
00389 } while ( !ch.isNull() );
00390
00391 parentBookmark.addBookmark( m_pManager, uniqueTitle, url, m_pOwner->currentIcon() );
00392 m_pManager->emitChanged( parentBookmark );
00393 }
00394
00395 void KCelBookmarkMenu::slotAddBookmark()
00396 {
00397 Url Url = m_pOwner->currentUrl(Url::Absolute);
00398 QString url = QString(Url.getAsString().c_str());;
00399 if (url.isEmpty())
00400 {
00401 KMessageBox::error( 0L, i18n("Can't add bookmark with empty URL"));
00402 return;
00403 }
00404 QString title = QString(Url.getName().c_str());
00405 if (title.isEmpty())
00406 title = url;
00407
00408 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00409 Q_ASSERT(!parentBookmark.isNull());
00410
00411 KBookmark ch = parentBookmark.first();
00412 int count = 1;
00413 QString uniqueTitle = title;
00414 do
00415 {
00416 while ( !ch.isNull() )
00417 {
00418 if ( uniqueTitle == ch.text() )
00419 {
00420
00421 if ( url != ch.url().url() )
00422 {
00423 uniqueTitle = title + QString(" (%1)").arg(++count);
00424
00425 ch = parentBookmark.first();
00426 break;
00427 }
00428 else
00429 {
00430
00431 return;
00432 }
00433 }
00434 ch = parentBookmark.next( ch );
00435 }
00436 } while ( !ch.isNull() );
00437
00438 parentBookmark.addBookmark( m_pManager, uniqueTitle, url, m_pOwner->currentIcon() );
00439 m_pManager->emitChanged( parentBookmark );
00440 }
00441
00442 void KCelBookmarkMenu::slotNewFolder()
00443 {
00444 if ( !m_pOwner ) return;
00445 KBookmarkGroup parentBookmark = m_pManager->findByAddress( m_parentAddress ).toGroup();
00446 Q_ASSERT(!parentBookmark.isNull());
00447 KBookmarkGroup group = parentBookmark.createNewFolder( m_pManager );
00448 if ( !group.isNull() )
00449 {
00450 KBookmarkGroup parentGroup = group.parentGroup();
00451 m_pManager->emitChanged( parentGroup );
00452 }
00453 }
00454
00455 void KCelBookmarkMenu::slotBookmarkSelected()
00456 {
00457
00458 if ( !m_pOwner ) return;
00459
00460
00461
00462 m_pOwner->openBookmarkURL( QString::fromUtf8(sender()->name()) );
00463 }
00464
00465
00466
00467