libkonq Library API Documentation

konq_popupmenu.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 David Faure <faure@kde.org>
00003    Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>
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 #include <qdir.h>
00022 
00023 #include <klocale.h>
00024 #include <kapplication.h>
00025 #include <kbookmarkmanager.h>
00026 #include <kdebug.h>
00027 #include <krun.h>
00028 #include <kprotocolinfo.h>
00029 #include <kiconloader.h>
00030 #include <kinputdialog.h>
00031 #include <kglobalsettings.h>
00032 #include <kstandarddirs.h>
00033 #include <kxmlguifactory.h>
00034 #include <kxmlguibuilder.h>
00035 #include <kparts/componentfactory.h>
00036 
00037 #include <assert.h>
00038 
00039 #include <kfileshare.h>
00040 #include <kprocess.h>
00041 
00042 #include "kpropertiesdialog.h"
00043 #include "knewmenu.h"
00044 #include "konq_popupmenu.h"
00045 #include "konq_operations.h"
00046 #include <dcopclient.h>
00047 
00048 
00049 class KonqPopupMenuGUIBuilder : public KXMLGUIBuilder
00050 {
00051 public:
00052   KonqPopupMenuGUIBuilder( QPopupMenu *menu )
00053   : KXMLGUIBuilder( 0 )
00054   {
00055     m_menu = menu;
00056   }
00057   virtual ~KonqPopupMenuGUIBuilder()
00058   {
00059   }
00060 
00061   virtual QWidget *createContainer( QWidget *parent, int index,
00062           const QDomElement &element,
00063           int &id )
00064   {
00065     if ( !parent && element.attribute( "name" ) == "popupmenu" )
00066       return m_menu;
00067 
00068     return KXMLGUIBuilder::createContainer( parent, index, element, id );
00069   }
00070 
00071   QPopupMenu *m_menu;
00072 };
00073 
00074 class KonqPopupMenu::KonqPopupMenuPrivate
00075 {
00076 public:
00077   KonqPopupMenuPrivate() : m_parentWidget(0)
00078   {
00079     m_itemFlags=KParts::BrowserExtension::DefaultPopupItems;
00080   }
00081   QString m_urlTitle;
00082   QWidget *m_parentWidget;
00083   KParts::BrowserExtension::PopupFlags m_itemFlags;
00084 };
00085 
00086 KonqPopupMenu::ProtocolInfo::ProtocolInfo( )
00087 {
00088   m_Reading = false;
00089   m_Writing = false;
00090   m_Deleting = false;
00091   m_Moving = false;
00092   m_TrashIncluded = false;
00093 }
00094 bool KonqPopupMenu::ProtocolInfo::supportsReading() const
00095 {
00096   return m_Reading;
00097 }
00098 bool KonqPopupMenu::ProtocolInfo::supportsWriting() const
00099 {
00100   return m_Writing;
00101 }
00102 bool KonqPopupMenu::ProtocolInfo::supportsDeleting() const
00103 {
00104   return m_Deleting;
00105 }
00106 bool KonqPopupMenu::ProtocolInfo::supportsMoving() const
00107 {
00108   return m_Moving;
00109 }
00110 bool KonqPopupMenu::ProtocolInfo::trashIncluded() const
00111 {
00112   return m_TrashIncluded;
00113 }
00114 
00115 KonqPopupMenu::KonqPopupMenu( KBookmarkManager *mgr, const KFileItemList &items,
00116                               KURL viewURL,
00117                               KActionCollection & actions,
00118                               KNewMenu * newMenu,
00119                               bool showProperties )
00120   : QPopupMenu( 0L, "konq_popupmenu" ), m_actions( actions ), m_ownActions( static_cast<QObject *>( 0 ), "KonqPopupMenu::m_ownActions" ),
00121     m_pMenuNew( newMenu ), m_sViewURL(viewURL), m_lstItems(items), m_pManager(mgr)
00122 
00123 {
00124   KonqPopupFlags kpf = ( showProperties ? ShowProperties : IsLink ) | ShowNewWindow;
00125   init(0, kpf, KParts::BrowserExtension::DefaultPopupItems);
00126 }
00127 
00128 KonqPopupMenu::KonqPopupMenu( KBookmarkManager *mgr, const KFileItemList &items,
00129                               KURL viewURL,
00130                               KActionCollection & actions,
00131                               KNewMenu * newMenu,
00132                   QWidget * parentWidget,
00133                               bool showProperties )
00134   : QPopupMenu( parentWidget, "konq_popupmenu" ), m_actions( actions ), m_ownActions( static_cast<QObject *>( 0 ), "KonqPopupMenu::m_ownActions" ), m_pMenuNew( newMenu ), m_sViewURL(viewURL), m_lstItems(items), m_pManager(mgr)
00135 {
00136   KonqPopupFlags kpf = ( showProperties ? ShowProperties : IsLink ) | ShowNewWindow;
00137   init(parentWidget, kpf, KParts::BrowserExtension::DefaultPopupItems);
00138 }
00139 
00140 KonqPopupMenu::KonqPopupMenu( KBookmarkManager *mgr, const KFileItemList &items,
00141                               const KURL& viewURL,
00142                               KActionCollection & actions,
00143                               KNewMenu * newMenu,
00144                               QWidget * parentWidget,
00145                               KonqPopupFlags kpf,
00146                               KParts::BrowserExtension::PopupFlags flags)
00147   : QPopupMenu( parentWidget, "konq_popupmenu" ), m_actions( actions ), m_ownActions( static_cast<QObject *>( 0 ), "KonqPopupMenu::m_ownActions" ), m_pMenuNew( newMenu ), m_sViewURL(viewURL), m_lstItems(items), m_pManager(mgr)
00148 {
00149   init(parentWidget, kpf, flags);
00150 }
00151 
00152 void KonqPopupMenu::init (QWidget * parentWidget, KonqPopupFlags kpf, KParts::BrowserExtension::PopupFlags flags)
00153 {
00154   d = new KonqPopupMenuPrivate;
00155   d->m_parentWidget = parentWidget;
00156   d->m_itemFlags = flags;
00157   setup(kpf);
00158 }
00159 
00160 
00161 int KonqPopupMenu::insertServicesSubmenus(const QMap<QString, ServiceList>& submenus,
00162                                           QDomElement& menu,
00163                                           bool isBuiltin)
00164 {
00165     int count = 0;
00166     QMap<QString, ServiceList>::ConstIterator it;
00167 
00168     for (it = submenus.begin(); it != submenus.end(); ++it)
00169     {
00170         if (it.data().isEmpty())
00171         {
00172             //avoid empty sub-menus
00173             continue;
00174         }
00175 
00176         QDomElement actionSubmenu = m_doc.createElement( "menu" );
00177         actionSubmenu.setAttribute( "name", "actions " + it.key() );
00178         menu.appendChild( actionSubmenu );
00179         QDomElement subtext = m_doc.createElement( "text" );
00180         actionSubmenu.appendChild( subtext );
00181         subtext.appendChild( m_doc.createTextNode( it.key() ) );
00182         count += insertServices(it.data(), actionSubmenu, isBuiltin);
00183     }
00184 
00185     return count;
00186 }
00187 
00188 int KonqPopupMenu::insertServices(const ServiceList& list,
00189                                   QDomElement& menu,
00190                                   bool isBuiltin)
00191 {
00192     static int id = 1000;
00193     int count = 0;
00194 
00195     ServiceList::const_iterator it = list.begin();
00196     for( ; it != list.end(); ++it )
00197     {
00198         if ((*it).isEmpty())
00199         {
00200             if (!menu.firstChild().isNull() &&
00201                 menu.lastChild().toElement().tagName().lower() != "separator")
00202             {
00203                 QDomElement separator = m_doc.createElement( "separator" );
00204                 menu.appendChild(separator);
00205             }
00206             continue;
00207         }
00208 
00209         if (isBuiltin || (*it).m_display == true)
00210         {
00211             QCString name;
00212             name.setNum( id );
00213             name.prepend( isBuiltin ? "builtinservice_" : "userservice_" );
00214             KAction * act = new KAction( (*it).m_strName, 0,
00215                                          this, SLOT( slotRunService() ),
00216                                          &m_ownActions, name );
00217 
00218             if ( !(*it).m_strIcon.isEmpty() )
00219             {
00220                 QPixmap pix = SmallIcon( (*it).m_strIcon );
00221                 act->setIconSet( pix );
00222             }
00223 
00224             addAction( act, menu ); // Add to toplevel menu
00225 
00226             m_mapPopupServices[ id++ ] = *it;
00227             ++count;
00228         }
00229     }
00230 
00231     return count;
00232 }
00233 
00234 bool KonqPopupMenu::KIOSKAuthorizedAction(KConfig& cfg)
00235 {
00236     if ( !cfg.hasKey( "X-KDE-AuthorizeAction") )
00237     {
00238         return true;
00239     }
00240 
00241     QStringList list = cfg.readListEntry("X-KDE-AuthorizeAction");
00242     if (kapp && !list.isEmpty())
00243     {
00244         for(QStringList::ConstIterator it = list.begin();
00245             it != list.end();
00246             ++it)
00247         {
00248             if (!kapp->authorize((*it).stripWhiteSpace()))
00249             {
00250                 return false;
00251             }
00252         }
00253     }
00254 
00255     return true;
00256 }
00257 
00258 
00259 void KonqPopupMenu::setup(KonqPopupFlags kpf)
00260 {
00261     assert( m_lstItems.count() >= 1 );
00262 
00263     m_ownActions.setWidget( this );
00264 
00265     bool bIsLink        = (kpf & IsLink);
00266     bool currentDir     = false;
00267     bool sReading       = true;
00268     bool sWriting       = true;
00269     bool sDeleting      = true;
00270     bool sMoving        = true;
00271     m_sMimeType         = m_lstItems.first()->mimetype();
00272     mode_t mode         = m_lstItems.first()->mode();
00273     bool isDirectory    = m_sMimeType == "inode/directory";
00274     bool bTrashIncluded = false;
00275     bool bCanChangeSharing = false;
00276     m_lstPopupURLs.clear();
00277     int id = 0;
00278     if( isDirectory && m_lstItems.first()->isLocalFile())
00279         bCanChangeSharing=true;
00280     setFont(KGlobalSettings::menuFont());
00281     m_pluginList.setAutoDelete( true );
00282     m_ownActions.setHighlightingEnabled( true );
00283 
00284     attrName = QString::fromLatin1( "name" );
00285 
00286     prepareXMLGUIStuff();
00287     m_builder = new KonqPopupMenuGUIBuilder( this );
00288     m_factory = new KXMLGUIFactory( m_builder );
00289 
00290     KURL url;
00291     KFileItemListIterator it ( m_lstItems );
00292     // Check whether all URLs are correct
00293     bool devicesFile = false;
00294     for ( ; it.current(); ++it )
00295     {
00296         url = (*it)->url();
00297 
00298         // Build the list of URLs
00299         m_lstPopupURLs.append( url );
00300 
00301         // Determine if common mode among all URLs
00302         if ( mode != (*it)->mode() )
00303             mode = 0; // modes are different => reset to 0
00304 
00305         // Determine if common mimetype among all URLs
00306         if ( m_sMimeType != (*it)->mimetype() )
00307             m_sMimeType = QString::null; // mimetypes are different => null
00308 
00309         if ( !bTrashIncluded &&
00310              (*it)->url().isLocalFile() &&
00311              (*it)->url().path( 1 ) == KGlobalSettings::trashPath() )
00312             bTrashIncluded = true;
00313 
00314         if ( sReading )
00315             sReading = KProtocolInfo::supportsReading( url );
00316 
00317         if ( sWriting )
00318             sWriting = KProtocolInfo::supportsWriting( url );
00319 
00320         if ( sDeleting )
00321             sDeleting = KProtocolInfo::supportsDeleting( url );
00322 
00323         if ( sMoving )
00324             sMoving = KProtocolInfo::supportsMoving( url );
00325         if ( url.protocol().find("device", 0, false)==0)
00326             devicesFile = true;
00327     }
00328     // Be on the safe side when including the trash
00329     if ( bTrashIncluded )
00330     {
00331         sMoving = false;
00332         sDeleting = false;
00333     }
00334     //check if current url is trash
00335     url = m_sViewURL;
00336     url.cleanPath();
00337 
00338     m_info.m_Reading = sReading;
00339     m_info.m_Writing = sWriting;
00340     m_info.m_Deleting = sDeleting;
00341     m_info.m_Moving = sMoving;
00342     m_info.m_TrashIncluded = bTrashIncluded;
00343 
00344     //check if url is current directory
00345     if ( m_lstItems.count() == 1 )
00346     {
00347         KURL firstPopupURL ( m_lstItems.first()->url() );
00348         firstPopupURL.cleanPath();
00349         //kdDebug(1203) << "View path is " << url.url() << endl;
00350         //kdDebug(1203) << "First popup path is " << firstPopupURL.url() << endl;
00351         currentDir = firstPopupURL.equals( url, true /* ignore_trailing */ );
00352     }
00353 
00354     bool isCurrentTrash = ( url.isLocalFile() &&
00355                             url.path(1) == KGlobalSettings::trashPath() &&
00356                             currentDir) ||
00357                           ( m_lstItems.count() == 1 && bTrashIncluded );
00358     bool isIntoTrash =  url.isLocalFile() && url.path(1).startsWith(KGlobalSettings::trashPath());
00359     clear();
00360 
00362 
00363     KAction * act;
00364 
00365     if (!isCurrentTrash)
00366         addMerge( "konqueror" );
00367 
00368     bool isKDesktop = QCString(  kapp->name() ) == "kdesktop";
00369     KAction *actNewWindow = 0;
00370 
00371     if (( kpf & ShowProperties ) && isKDesktop &&
00372         !kapp->authorize("editable_desktop_icons"))
00373     {
00374         kpf &= ~ShowProperties; // remove flag
00375     }
00376 
00377     // Either 'newview' is in the actions we're given (probably in the tabhandling group)
00378     // or we need to insert it ourselves (e.g. for kdesktop). In the first case, actNewWindow must remain 0.
00379     if ( kpf & ShowNewWindow )
00380     {
00381         QString openStr = isKDesktop ? i18n( "&Open" ) : i18n( "Open in New &Window" );
00382         actNewWindow = new KAction( openStr, "window_new", 0, this, SLOT( slotPopupNewView() ), &m_ownActions, "newview" );
00383     }
00384 
00385     if ( actNewWindow && !isKDesktop )
00386     {
00387         if (isCurrentTrash)
00388             actNewWindow->setStatusText( i18n( "Open the trash in a new window" ) );
00389         else
00390             actNewWindow->setStatusText( i18n( "Open the document in a new window" ) );
00391     }
00392 
00393     if ( isCurrentTrash )
00394     {
00395         if (actNewWindow)
00396         {
00397             addAction( actNewWindow );
00398             addSeparator();
00399         }
00400         addGroup( "tabhandling" ); // includes a separator
00401 
00402         act = new KAction( i18n( "&Empty Trash Bin" ), 0, this, SLOT( slotPopupEmptyTrashBin() ), &m_ownActions, "empytrash" );
00403         addAction( act );
00404         if ( KPropertiesDialog::canDisplay( m_lstItems ) && (kpf & ShowProperties) )
00405         {
00406             act = new KAction( i18n( "&Properties" ), 0, this, SLOT( slotPopupProperties() ),
00407                                &m_ownActions, "properties" );
00408             addAction( act );
00409         }
00410         m_factory->addClient( this );
00411         return;
00412     }
00413     else
00414     {
00415         if ( S_ISDIR(mode) && sWriting && !isIntoTrash ) // A dir, and we can create things into it
00416         {
00417             if ( currentDir && m_pMenuNew ) // Current dir -> add the "new" menu
00418             {
00419                 // As requested by KNewMenu :
00420                 m_pMenuNew->slotCheckUpToDate();
00421                 m_pMenuNew->setPopupFiles( m_lstPopupURLs );
00422 
00423                 addAction( m_pMenuNew );
00424 
00425                 addSeparator();
00426             }
00427             else
00428             {
00429                 if (d->m_itemFlags & KParts::BrowserExtension::ShowCreateDirectory)
00430                 {
00431                     KAction *actNewDir = new KAction( i18n( "Create &Folder..." ), "folder_new", 0, this, SLOT( slotPopupNewDir() ), &m_ownActions, "newdir" );
00432                     addAction( actNewDir );
00433                     addSeparator();
00434                 }
00435             }
00436         }
00437 
00438         if (d->m_itemFlags & KParts::BrowserExtension::ShowNavigationItems)
00439         {
00440             if (d->m_itemFlags & KParts::BrowserExtension::ShowUp)
00441                 addAction( "up" );
00442             addAction( "back" );
00443             addAction( "forward" );
00444             if (d->m_itemFlags & KParts::BrowserExtension::ShowReload)
00445                 addAction( "reload" );
00446             addSeparator();
00447         }
00448 
00449         // "open in new window" is either provided by us, or by the tabhandling group
00450         if (actNewWindow)
00451         {
00452             addAction( actNewWindow );
00453             addSeparator();
00454         }
00455         addGroup( "tabhandling" ); // includes a separator
00456 
00457         if ( !bIsLink )
00458         {
00459             if ( !currentDir && sReading && !isIntoTrash &&!devicesFile ) {
00460                 if ( sDeleting ) {
00461                     addAction( "cut" );
00462                 }
00463                 addAction( "copy" );
00464             }
00465 
00466             if ( S_ISDIR(mode) && sWriting && !isIntoTrash) {
00467                 if ( currentDir )
00468                     addAction( "paste" );
00469                 else
00470                     addAction( "pasteto" );
00471             }
00472             if ( !isIntoTrash )
00473             {
00474                 if (!currentDir )
00475                 {
00476                     if ( m_lstItems.count() == 1 && sWriting )
00477                         addAction("rename");
00478 
00479                     if ( sMoving )
00480                         addAction( "trash" );
00481 
00482                     if ( sDeleting ) {
00483                         addAction( "del" );
00484                     }
00485                 }
00486             }
00487         }
00488         addGroup( "editactions" );
00489     }
00490     if ( !isCurrentTrash && !isIntoTrash && (d->m_itemFlags & KParts::BrowserExtension::ShowBookmark))
00491     {
00492         addSeparator();
00493         QString caption;
00494         if (currentDir)
00495         {
00496            bool httpPage = (m_sViewURL.protocol().find("http", 0, false) == 0);
00497            if (httpPage)
00498               caption = i18n("&Bookmark This Page");
00499            else
00500               caption = i18n("&Bookmark This Location");
00501         }
00502         else if (S_ISDIR(mode))
00503            caption = i18n("&Bookmark This Folder");
00504         else if (bIsLink)
00505            caption = i18n("&Bookmark This Link");
00506         else
00507            caption = i18n("&Bookmark This File");
00508 
00509         act = new KAction( caption, "bookmark_add", 0, this, SLOT( slotPopupAddToBookmark() ), &m_ownActions, "bookmark_add" );
00510         if (m_lstItems.count() > 1)
00511             act->setEnabled(false);
00512         if (kapp->authorizeKAction("bookmarks"))
00513             addAction( act );
00514         if (bIsLink)
00515             addGroup( "linkactions" );
00516     }
00517 
00519 
00520     ServiceList builtin;
00521     ServiceList user, userToplevel, userPriority;
00522     QMap<QString, ServiceList> userSubmenus, userToplevelSubmenus, userPrioritySubmenus;
00523 
00524     bool isSingleLocal = (m_lstItems.count() == 1 && m_lstItems.first()->url().isLocalFile());
00525     // 1 - Look for builtin and user-defined services
00526     if ( m_sMimeType == "application/x-desktop" && isSingleLocal ) // .desktop file
00527     {
00528         // get builtin services, like mount/unmount
00529         builtin = KDEDesktopMimeType::builtinServices( m_lstItems.first()->url() );
00530         user = KDEDesktopMimeType::userDefinedServices( m_lstItems.first()->url().path(), url.isLocalFile() );
00531     }
00532 
00533     if ( !isCurrentTrash && !isIntoTrash)
00534     {
00535 
00536         // 2 - Look for "servicesmenus" bindings (konqueror-specific user-defined services)
00537 
00538         // first check the .directory if this is a directory
00539         if (isDirectory && isSingleLocal)
00540         {
00541             QString dotDirectoryFile = m_lstItems.first()->url().path(1).append(".directory");
00542             KSimpleConfig cfg( dotDirectoryFile, true );
00543             cfg.setDesktopGroup();
00544 
00545             if (KIOSKAuthorizedAction(cfg))
00546             {
00547                 QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
00548                 if (submenuName.isEmpty())
00549                 {
00550                     user += KDEDesktopMimeType::userDefinedServices( dotDirectoryFile, true );
00551                 }
00552                 else
00553                 {
00554                     userSubmenus[submenuName] += KDEDesktopMimeType::userDefinedServices( dotDirectoryFile, true );
00555                 }
00556             }
00557         }
00558 
00559         QStringList dirs = KGlobal::dirs()->findDirs( "data", "konqueror/servicemenus/" );
00560         QStringList::ConstIterator dIt = dirs.begin();
00561         QStringList::ConstIterator dEnd = dirs.end();
00562 
00563         for (; dIt != dEnd; ++dIt )
00564         {
00565             QDir dir( *dIt );
00566 
00567             QStringList entries = dir.entryList( "*.desktop", QDir::Files );
00568             QStringList::ConstIterator eIt = entries.begin();
00569             QStringList::ConstIterator eEnd = entries.end();
00570 
00571             for (; eIt != eEnd; ++eIt )
00572             {
00573                 KSimpleConfig cfg( *dIt + *eIt, true );
00574                 cfg.setDesktopGroup();
00575 
00576                 if (!KIOSKAuthorizedAction(cfg))
00577                 {
00578                     continue;
00579                 }
00580 
00581                 if ( cfg.hasKey( "X-KDE-ShowIfRunning" ) )
00582                 {
00583                     QString app = cfg.readEntry( "X-KDE-ShowIfRunning" );
00584                     if ( !kapp->dcopClient()->isApplicationRegistered( app.utf8() ) )
00585                         continue;
00586                 }
00587 
00588                 if ( cfg.hasKey( "Actions" ) && cfg.hasKey( "ServiceTypes" ) )
00589                 {
00590                     QStringList types = cfg.readListEntry( "ServiceTypes" );
00591                     QStringList excludeTypes = cfg.readListEntry( "ExcludeServiceTypes" );
00592                     bool ok = false;
00593                     QString mimeGroup = m_sMimeType.left(m_sMimeType.find('/'));
00594 
00595                     // check for exact matches or a typeglob'd mimetype if we have a mimetype
00596                     for (QStringList::iterator it = types.begin(); it != types.end(); ++it)
00597                     {
00598                         // we could cram the following three if statements into
00599                         // one gigantic boolean statement but that would be a
00600                         // hororr show for readability
00601 
00602                         // first check if we have an all mimetype
00603                         if (*it == "all/all" ||
00604                             *it == "allfiles" /*compat with KDE up to 3.0.3*/)
00605                         {
00606               ok = true;
00607               for (QStringList::iterator itex = excludeTypes.begin(); itex != excludeTypes.end(); ++itex)
00608                 {
00609                   if( (*itex).right(1) == "*" &&(*itex).left((*itex).find('/'))== mimeGroup )
00610                 {
00611                   ok = false;
00612                   break;
00613                 }
00614                   else if ( (*itex).contains( m_sMimeType))
00615                 {
00616                   ok = false;
00617                   break;
00618                 }
00619                 }
00620                         }
00621                         // next, do we match all files?
00622                         if (*it == "all/allfiles" &&
00623                             !isDirectory) // ## or inherits from it
00624                         {
00625               ok = true;
00626               for (QStringList::iterator itex = excludeTypes.begin(); itex != excludeTypes.end(); ++itex)
00627                 {
00628                   if( (*itex).right(1) == "*" &&(*itex).left((*itex).find('/'))== mimeGroup )
00629                 {
00630                   ok = false;
00631                   break;
00632                 }
00633                   else if ( (*itex).contains( m_sMimeType))
00634                 {
00635                   ok = false;
00636                   break;
00637                 }
00638                 }
00639                         }
00640 
00641                         // if we have a mimetype, see if we have an exact or type
00642                         // globbed match
00643                         if (!m_sMimeType.isNull() &&
00644                             (*it == m_sMimeType ||
00645                              ((*it).right(1) == "*" &&
00646                               (*it).left((*it).find('/')) == mimeGroup)))
00647                         {
00648               ok = true;
00649               for (QStringList::iterator itex = excludeTypes.begin(); itex != excludeTypes.end(); ++itex)
00650                 {
00651                   if( (*itex).right(1) == "*" &&(*itex).left((*itex).find('/'))== mimeGroup )
00652                 {
00653                   ok = false;
00654                   break;
00655                 }
00656                   else if ( (*itex).contains( m_sMimeType))
00657                 {
00658                   ok = false;
00659                   break;
00660                 }
00661                 }
00662             }
00663                     }
00664 
00665                     if ( ok )
00666                     {
00667                         // we use the categories .desktop entry to define submenus
00668                         // if none is defined, we just pop it in the main menu
00669                         QString priority = cfg.readEntry("X-KDE-Priority");
00670                         QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
00671                         ServiceList* list = &user;
00672 
00673                         if (submenuName.isEmpty())
00674                         {
00675                             if (priority == "TopLevel")
00676                             {
00677                                 list = &userToplevel;
00678                             }
00679                             else if (priority == "Important")
00680                             {
00681                                 list = &userPriority;
00682                             }
00683                         }
00684                         else if (priority == "TopLevel")
00685                         {
00686                             list = &(userToplevelSubmenus[submenuName]);
00687                         }
00688                         else if (priority == "Important")
00689                         {
00690                             list = &(userPrioritySubmenus[submenuName]);
00691                         }
00692                         else
00693                         {
00694                             list = &(userSubmenus[submenuName]);
00695                         }
00696 
00697                         (*list) += KDEDesktopMimeType::userDefinedServices( *dIt + *eIt, url.isLocalFile() );
00698                     }
00699                 }
00700             }
00701         }
00702 
00703         KTrader::OfferList offers;
00704 
00705         if (kapp->authorizeKAction("openwith"))
00706         {
00707             // if check m_sMimeType.isNull (no commom mime type) set it to all/all
00708             // 3 - Query for applications
00709             offers = KTrader::self()->query( m_sMimeType.isNull( ) ? QString::fromLatin1( "all/all" ) : m_sMimeType ,
00710                                              "Type == 'Application' and DesktopEntryName != 'kfmclient' and DesktopEntryName != 'kfmclient_dir' and DesktopEntryName != 'kfmclient_html'" );
00711         }
00712 
00714 
00715         m_mapPopup.clear();
00716         m_mapPopupServices.clear();
00717         if ( !devicesFile)
00718         {
00719             if ( !offers.isEmpty() )
00720             {
00721                 // First block, app and preview offers
00722                 addSeparator();
00723 
00724                 id = 1;
00725 
00726                 QDomElement menu = m_menuElement;
00727 
00728                 if ( offers.count() > 1 ) // submenu 'open with'
00729                 {
00730                     menu = m_doc.createElement( "menu" );
00731                     menu.setAttribute( "name", "openwith submenu" );
00732                     m_menuElement.appendChild( menu );
00733                     QDomElement text = m_doc.createElement( "text" );
00734                     menu.appendChild( text );
00735                     text.appendChild( m_doc.createTextNode( i18n("&Open With") ) );
00736                 }
00737 
00738                 if ( menu == m_menuElement ) // no submenu -> open with... above the single offer
00739                 {
00740                     KAction *openWithAct = new KAction( i18n( "&Open With..." ), 0, this, SLOT( slotPopupOpenWith() ), &m_ownActions, "openwith" );
00741                     addAction( openWithAct, menu );
00742                 }
00743 
00744                 KTrader::OfferList::ConstIterator it = offers.begin();
00745                 for( ; it != offers.end(); it++ )
00746                 {
00747                     QCString nam;
00748                     nam.setNum( id );
00749 
00750                     act = new KAction( (*it)->name(), (*it)->pixmap( KIcon::Small ), 0,
00751                                        this, SLOT( slotRunService() ),
00752                                        &m_ownActions, nam.prepend( "appservice_" ) );
00753                     addAction( act, menu );
00754 
00755                     m_mapPopup[ id++ ] = *it;
00756                 }
00757 
00758                 if ( menu != m_menuElement ) // submenu
00759                 {
00760                     addSeparator( menu );
00761                     KAction *openWithAct = new KAction( i18n( "&Other..." ), 0, this, SLOT( slotPopupOpenWith() ), &m_ownActions, "openwith" );
00762                     addAction( openWithAct, menu ); // Other...
00763                 }
00764             }
00765             else // no app offers -> Open With...
00766             {
00767                 addSeparator();
00768                 act = new KAction( i18n( "&Open With..." ), 0, this, SLOT( slotPopupOpenWith() ), &m_ownActions, "openwith" );
00769                 addAction( act );
00770             }
00771 
00772             addGroup( "preview" );
00773         }
00774     }
00775 
00776     // Second block, builtin + user
00777     QDomElement actionMenu = m_menuElement;
00778     int userItemCount = 0;
00779     if (user.count() + userSubmenus.count() +
00780         userPriority.count() + userPrioritySubmenus.count() > 1)
00781     {
00782         // we have more than one item, so let's make a submenu
00783         actionMenu = m_doc.createElement( "menu" );
00784         actionMenu.setAttribute( "name", "actions submenu" );
00785         m_menuElement.appendChild( actionMenu );
00786         QDomElement text = m_doc.createElement( "text" );
00787         actionMenu.appendChild( text );
00788         text.appendChild( m_doc.createTextNode( i18n("Ac&tions") ) );
00789     }
00790 
00791     userItemCount += insertServicesSubmenus(userPrioritySubmenus, actionMenu, false);
00792     userItemCount += insertServices(userPriority, actionMenu, false);
00793 
00794     // see if we need to put a separator between our priority items and our regular items
00795     if (userItemCount > 0 &&
00796         (user.count() > 0 ||
00797          userSubmenus.count() > 0 ||
00798          builtin.count() > 0) &&
00799          actionMenu.lastChild().toElement().tagName().lower() != "separator")
00800     {
00801         QDomElement separator = m_doc.createElement( "separator" );
00802         actionMenu.appendChild(separator);
00803     }
00804 
00805     userItemCount += insertServicesSubmenus(userSubmenus, actionMenu, false);
00806     userItemCount += insertServices(user, actionMenu, false);
00807     userItemCount += insertServices(builtin, m_menuElement, true);
00808 
00809     userItemCount += insertServicesSubmenus(userToplevelSubmenus, m_menuElement, false);
00810     userItemCount += insertServices(userToplevel, m_menuElement, false);
00811 
00812     if (userItemCount > 0)
00813     {
00814         addSeparator();
00815     }
00816 
00817     if ( !isCurrentTrash && !isIntoTrash && !devicesFile)
00818         addPlugins( ); // now it's time to add plugins
00819 
00820     if ( KPropertiesDialog::canDisplay( m_lstItems ) && (kpf & ShowProperties) )
00821     {
00822         act = new KAction( i18n( "&Properties" ), 0, this, SLOT( slotPopupProperties() ),
00823                            &m_ownActions, "properties" );
00824         addAction( act );
00825     }
00826 
00827     while ( !m_menuElement.lastChild().isNull() &&
00828             m_menuElement.lastChild().toElement().tagName().lower() == "separator" )
00829         m_menuElement.removeChild( m_menuElement.lastChild() );
00830 
00831     if( bCanChangeSharing && !isCurrentTrash && !isIntoTrash)
00832     {
00833         if(KFileShare::authorization()==KFileShare::Authorized)
00834         {
00835             addSeparator();
00836             QString label;
00837             label=i18n("Share");
00838 
00839             act = new KAction( label, 0, this, SLOT( slotOpenShareFileDialog() ),
00840                                &m_ownActions, "sharefile" );
00841             addAction( act );
00842         }
00843     }
00844 
00845 
00846     addMerge( 0 );
00847 
00848     m_factory->addClient( this );
00849 }
00850 
00851 void KonqPopupMenu::slotOpenShareFileDialog()
00852 {
00853     //kdDebug()<<"KonqPopupMenu::slotOpenShareFileDialog()\n";
00854     // It may be that the kfileitem was created by hand
00855     // (see KonqKfmIconView::slotMouseButtonPressed)
00856     // In that case, we can get more precise info in the properties
00857     // (like permissions) if we stat the URL.
00858     if ( m_lstItems.count() == 1 )
00859     {
00860         KFileItem * item = m_lstItems.first();
00861         if (item->entry().count() == 0) // this item wasn't listed by a slave
00862         {
00863             // KPropertiesDialog will use stat to get more info on the file
00864             KPropertiesDialog*dlg= new KPropertiesDialog( item->url(), d->m_parentWidget );
00865             dlg->showFileSharingPage();
00866 
00867             return;
00868         }
00869     }
00870     KPropertiesDialog*dlg=new KPropertiesDialog( m_lstItems, d->m_parentWidget );
00871     dlg->showFileSharingPage();
00872 }
00873 
00874 KonqPopupMenu::~KonqPopupMenu()
00875 {
00876   m_pluginList.clear();
00877   delete m_factory;
00878   delete m_builder;
00879   delete d;
00880   kdDebug(1203) << "~KonqPopupMenu leave" << endl;
00881 }
00882 
00883 void KonqPopupMenu::setURLTitle( const QString& urlTitle )
00884 {
00885     d->m_urlTitle = urlTitle;
00886 }
00887 
00888 void KonqPopupMenu::slotPopupNewView()
00889 {
00890   KURL::List::ConstIterator it = m_lstPopupURLs.begin();
00891   for ( ; it != m_lstPopupURLs.end(); it++ )
00892     (void) new KRun(*it);
00893 }
00894 
00895 void KonqPopupMenu::slotPopupNewDir()
00896 {
00897   if (m_lstPopupURLs.empty())
00898     return;
00899 
00900   KonqOperations::newDir(d->m_parentWidget, m_lstPopupURLs.first());
00901 }
00902 
00903 void KonqPopupMenu::slotPopupEmptyTrashBin()
00904 {
00905   KonqOperations::emptyTrash();
00906 }
00907 
00908 void KonqPopupMenu::slotPopupOpenWith()
00909 {
00910   KRun::displayOpenWithDialog( m_lstPopupURLs );
00911 }
00912 
00913 void KonqPopupMenu::slotPopupAddToBookmark()
00914 {
00915   KBookmarkGroup root = m_pManager->root();
00916   if ( m_lstPopupURLs.count() == 1 ) {
00917     KURL url = m_lstPopupURLs.first();
00918     QString title = d->m_urlTitle.isEmpty() ? url.prettyURL() : d->m_urlTitle;
00919     root.addBookmark( m_pManager, title, url.url() );
00920   }
00921   else
00922   {
00923     KURL::List::ConstIterator it = m_lstPopupURLs.begin();
00924     for ( ; it != m_lstPopupURLs.end(); it++ )
00925       root.addBookmark( m_pManager, (*it).prettyURL(), (*it).url() );
00926   }
00927   m_pManager->emitChanged( root );
00928 }
00929 
00930 void KonqPopupMenu::slotRunService()
00931 {
00932   QCString senderName = sender()->name();
00933   int id = senderName.mid( senderName.find( '_' ) + 1 ).toInt();
00934 
00935   // Is it a usual service (application)
00936   QMap<int,KService::Ptr>::Iterator it = m_mapPopup.find( id );
00937   if ( it != m_mapPopup.end() )
00938   {
00939     KRun::run( **it, m_lstPopupURLs );
00940     return;
00941   }
00942 
00943   // Is it a service specific to desktop entry files ?
00944   QMap<int,KDEDesktopMimeType::Service>::Iterator it2 = m_mapPopupServices.find( id );
00945   if ( it2 != m_mapPopupServices.end() )
00946   {
00947       KDEDesktopMimeType::executeService( m_lstPopupURLs, it2.data() );
00948   }
00949 
00950   return;
00951 }
00952 
00953 void KonqPopupMenu::slotPopupMimeType()
00954 {
00955   KonqOperations::editMimeType( m_sMimeType );
00956 }
00957 
00958 void KonqPopupMenu::slotPopupProperties()
00959 {
00960     // It may be that the kfileitem was created by hand
00961     // (see KonqKfmIconView::slotMouseButtonPressed)
00962     // In that case, we can get more precise info in the properties
00963     // (like permissions) if we stat the URL.
00964     if ( m_lstItems.count() == 1 )
00965     {
00966         KFileItem * item = m_lstItems.first();
00967         if (item->entry().count() == 0) // this item wasn't listed by a slave
00968         {
00969             // KPropertiesDialog will use stat to get more info on the file
00970             (void) new KPropertiesDialog( item->url(), d->m_parentWidget );
00971             return;
00972         }
00973     }
00974     (void) new KPropertiesDialog( m_lstItems, d->m_parentWidget );
00975 }
00976 
00977 KAction *KonqPopupMenu::action( const QDomElement &element ) const
00978 {
00979   QCString name = element.attribute( attrName ).ascii();
00980   KAction *res = m_ownActions.action( name );
00981 
00982   if ( !res )
00983     res = m_actions.action( name );
00984 
00985   if ( !res && m_pMenuNew && strcmp( name, m_pMenuNew->name() ) == 0 )
00986     return m_pMenuNew;
00987 
00988   return res;
00989 }
00990 KActionCollection *KonqPopupMenu::actionCollection() const
00991 {
00992   return const_cast<KActionCollection *>( &m_ownActions );
00993 }
00994 
00995 QString KonqPopupMenu::mimeType( ) const {
00996     return m_sMimeType;
00997 }
00998 KonqPopupMenu::ProtocolInfo KonqPopupMenu::protocolInfo() const
00999 {
01000   return m_info;
01001 }
01002 void KonqPopupMenu::addPlugins( ){
01003     // search for Konq_PopupMenuPlugins inspired by simons kpropsdlg
01004     //search for a plugin with the right protocol
01005     KTrader::OfferList plugin_offers;
01006         unsigned int pluginCount = 0;
01007     plugin_offers = KTrader::self()->query( m_sMimeType.isNull() ? QString::fromLatin1( "all/all" ) : m_sMimeType , "'KonqPopupMenu/Plugin' in ServiceTypes");
01008         if ( plugin_offers.isEmpty() )
01009       return; // no plugins installed do not bother about it
01010 
01011     KTrader::OfferList::ConstIterator iterator = plugin_offers.begin( );
01012     KTrader::OfferList::ConstIterator end = plugin_offers.end( );
01013 
01014     addGroup( "plugins" );
01015     // travers the offerlist
01016     for(; iterator != end; ++iterator, ++pluginCount ){
01017         KonqPopupMenuPlugin *plugin =
01018             KParts::ComponentFactory::
01019             createInstanceFromLibrary<KonqPopupMenuPlugin>( (*iterator)->library().local8Bit(),
01020                                     this,
01021                                     (*iterator)->name().latin1() );
01022         if ( !plugin )
01023             continue;
01024                 QString pluginClientName = QString::fromLatin1( "Plugin%1" ).arg( pluginCount );
01025                 addMerge( pluginClientName );
01026                 plugin->domDocument().documentElement().setAttribute( "name", pluginClientName );
01027         m_pluginList.append( plugin );
01028         insertChildClient( plugin );
01029     }
01030 
01031     addMerge( "plugins" );
01032     addSeparator();
01033 }
01034 KURL KonqPopupMenu::url( ) const {
01035   return m_sViewURL;
01036 }
01037 KFileItemList KonqPopupMenu::fileItemList( ) const {
01038   return m_lstItems;
01039 }
01040 KURL::List KonqPopupMenu::popupURLList( ) const {
01041   return m_lstPopupURLs;
01042 }
01047 KonqPopupMenuPlugin::KonqPopupMenuPlugin( KonqPopupMenu *parent, const char *name )
01048     : QObject( parent, name ) {
01049 }
01050 KonqPopupMenuPlugin::~KonqPopupMenuPlugin( ){
01051 
01052 }
01053 #include "konq_popupmenu.moc"
KDE Logo
This file is part of the documentation for libkonq Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 29 21:20:29 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003