Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

kcelbookmarkmenu.cpp

Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 2 -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
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  * KCelBookmarkMenu
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 ) // not for the netscape bookmark
00072   {
00073     //kdDebug(1203) << "KBookmarkMenu::KBookmarkMenu " << this << " address : " << m_parentAddress << endl;
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   // add entries that possibly have a shortcut, so they are available _before_ first popup
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   //kdDebug(1203) << "KBookmarkMenu::~KBookmarkMenu() " << this << endl;
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   // Did the bookmarks change since the last time we showed them ?
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     //kdDebug(1203) << "KBookmarkMenu::slotBookmarksChanged -> setting m_bDirty on " << groupAddress << endl;
00133     m_bDirty = true;
00134   }
00135   else
00136   {
00137     // Iterate recursively into child menus
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   //kdDebug(1203) << "KBookmarkMenu::refill()" << endl;
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, //m_bIsRoot ? KStdAccel::addBookmark() : KShortcut(),
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, //m_bIsRoot ? KStdAccel::addBookmark() : KShortcut(),
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", //"folder",
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) { // inserted before the first konq bookmark, to avoid the separator if no konq bookmark
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         // kdDebug(1203) << "Creating URL bookmark menu item for " << bm.text() << endl;
00267         // create a normal URL item, with ID as a name
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       // kdDebug(1203) << "Creating bookmark submenu named " << bm.text() << endl;
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   // If this title is already used, we'll try to find something unused.
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         // Title already used !
00327         if ( url != ch.url().url() )
00328         {
00329           uniqueTitle = title + QString(" (%1)").arg(++count);
00330           // New title -> restart search from the beginning
00331           ch = parentBookmark.first();
00332           break;
00333         }
00334         else
00335         {
00336           // this exact URL already exists
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   // If this title is already used, we'll try to find something unused.
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         // Title already used !
00374         if ( url != ch.url().url() )
00375         {
00376           uniqueTitle = title + QString(" (%1)").arg(++count);
00377           // New title -> restart search from the beginning
00378           ch = parentBookmark.first();
00379           break;
00380         }
00381         else
00382         {
00383           // this exact URL already exists
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   // If this title is already used, we'll try to find something unused.
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         // Title already used !
00421         if ( url != ch.url().url() )
00422         {
00423           uniqueTitle = title + QString(" (%1)").arg(++count);
00424           // New title -> restart search from the beginning
00425           ch = parentBookmark.first();
00426           break;
00427         }
00428         else
00429         {
00430           // this exact URL already exists
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; // this view doesn't handle bookmarks...
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   //kdDebug(1203) << "KBookmarkMenu::slotBookmarkSelected()" << endl;
00458   if ( !m_pOwner ) return; // this view doesn't handle bookmarks...
00459   //kdDebug(1203) << sender()->name() << endl;
00460 
00461   // The name of the action is the URL to open
00462   m_pOwner->openBookmarkURL( QString::fromUtf8(sender()->name()) );
00463 }
00464 
00465 // -----------------------------------------------------------------------------
00466 
00467 

Generated on Sat Jan 14 22:30:31 2006 for Celestia by  doxygen 1.4.1