22 #include <kfileitemlistproperties.h>
26 #include "kfileitemactions.h"
27 #include "kfileitemactionplugin.h"
28 #include "kabstractfileitemactionplugin.h"
29 #include "kpropertiesdialog.h"
34 #include <kbookmarkmanager.h>
35 #include <kbookmarkdialog.h>
38 #include <kprotocolmanager.h>
40 #include <kiconloader.h>
41 #include <kinputdialog.h>
42 #include <kglobalsettings.h>
43 #include <kmimetypetrader.h>
44 #include <kstandarddirs.h>
45 #include <kconfiggroup.h>
46 #include <kdesktopfile.h>
47 #include <kfileshare.h>
48 #include <kauthorized.h>
50 #include <kacceleratormanager.h>
76 class KonqPopupMenuPrivate
81 m_parentWidget(parentWidget),
82 m_itemFlags(KParts::BrowserExtension::DefaultPopupItems),
84 m_copyToMenu(parentWidget),
87 m_ownActionCollection(static_cast<
QWidget *>(0))
91 ~KonqPopupMenuPrivate()
93 qDeleteAll(m_ownActions);
96 void addNamedAction(
const QString& name);
97 void addGroup(
const QString& name);
101 void slotPopupNewDir();
102 void slotPopupNewView();
103 void slotPopupEmptyTrashBin();
104 void slotConfigTrashBin();
105 void slotPopupRestoreTrashedItems();
106 void slotPopupAddToBookmark();
107 void slotPopupMimeType();
108 void slotPopupProperties();
109 void slotOpenShareFileDialog();
110 void slotShowOriginalFile();
115 KParts::BrowserExtension::PopupFlags m_itemFlags;
118 KFileItemListProperties m_popupItemProperties;
120 KFileItemActions m_menuActions;
122 KBookmarkManager* m_bookmarkManager;
123 KActionCollection &m_actions;
124 KActionCollection m_ownActionCollection;
126 KParts::BrowserExtension::ActionGroupMap m_actionGroups;
133 KActionCollection & actions,
136 KParts::BrowserExtension::PopupFlags flags,
138 KBookmarkManager *mgr,
139 const KParts::BrowserExtension::ActionGroupMap& actionGroups)
140 :
QMenu(parentWidget),
141 d(new KonqPopupMenuPrivate(this, actions, parentWidget))
143 d->m_actionGroups = actionGroups;
144 d->m_pMenuNew = newMenu;
145 d->m_sViewURL = viewURL;
146 d->m_bookmarkManager = mgr;
147 d->m_popupItemProperties.setItems(items);
148 d->m_menuActions.setParentWidget(parentWidget);
151 KAcceleratorManager::manage(
this);
154 void KonqPopupMenuPrivate::addNamedAction(
const QString& name)
156 QAction* act = m_actions.action(name);
161 void KonqPopupMenuPrivate::init(
KonqPopupMenu::Flags kpf, KParts::BrowserExtension::PopupFlags flags)
164 q->setFont(KGlobalSettings::menuFont());
166 Q_ASSERT(m_popupItemProperties.items().count() >= 1);
168 bool bTrashIncluded =
false;
170 const KFileItemList lstItems = m_popupItemProperties.items();
171 KFileItemList::const_iterator it = lstItems.constBegin();
172 const KFileItemList::const_iterator kend = lstItems.constEnd();
173 for ( ; it != kend; ++it )
175 const KUrl url = (*it).url();
176 if ( !bTrashIncluded && (
177 ( url.protocol() ==
"trash" && url.path().length() <= 1 ) ) ) {
178 bTrashIncluded =
true;
182 const bool isDirectory = m_popupItemProperties.isDirectory();
183 const bool sReading = m_popupItemProperties.supportsReading();
184 bool sDeleting = (m_itemFlags & KParts::BrowserExtension::NoDeletion) == 0
185 && m_popupItemProperties.supportsDeleting();
186 const bool sWriting = m_popupItemProperties.supportsWriting();
187 const bool sMoving = sDeleting && m_popupItemProperties.supportsMoving();
188 const bool isLocal = m_popupItemProperties.isLocal();
190 KUrl url = m_sViewURL;
193 bool isTrashLink =
false;
194 bool isCurrentTrash =
false;
195 bool currentDir =
false;
196 bool isSymLink =
false;
197 bool isSymLinkInSameDir =
false;
200 if ( lstItems.count() == 1 )
202 KFileItem firstPopupItem( lstItems.first() );
203 if (firstPopupItem.isLink()) {
205 isSymLinkInSameDir = !firstPopupItem.linkDest().contains(
'/');
207 KUrl firstPopupURL( firstPopupItem.url() );
208 firstPopupURL.cleanPath();
211 currentDir = firstPopupURL.equals( url, KUrl::CompareWithoutTrailingSlash );
212 if ( firstPopupItem.isDesktopFile() ) {
213 KDesktopFile desktopFile( firstPopupItem.localPath() );
214 const KConfigGroup cfg = desktopFile.desktopGroup();
215 isTrashLink = ( cfg.readEntry(
"Type") ==
"Link" && cfg.readEntry(
"URL") ==
"trash:/" );
223 isCurrentTrash = (firstPopupURL.protocol() ==
"trash" && firstPopupURL.path().length() <= 1)
227 const bool isIntoTrash = (url.protocol() ==
"trash") && !isCurrentTrash;
229 const bool bIsLink = (m_itemFlags & KParts::BrowserExtension::IsLink);
235 addGroup(
"topactions" );
239 KAction *actNewWindow = 0;
241 #if 0 // TODO in the desktop code itself.
242 if (( flags & KParts::BrowserExtension::ShowProperties ) && isOnDesktop &&
243 !KAuthorized::authorizeKAction(
"editable_desktop_icons"))
245 flags &= ~KParts::BrowserExtension::ShowProperties;
254 const QString openStr = i18n(
"&Open");
255 actNewWindow =
new KAction(m_parentWidget );
256 m_ownActions.append(actNewWindow);
257 actNewWindow->setIcon( KIcon(
"window-new") );
258 actNewWindow->setText( openStr );
259 QObject::connect(actNewWindow, SIGNAL(triggered()), q, SLOT(slotPopupNewView()));
262 if ( isDirectory && sWriting && !isCurrentTrash )
264 const bool mkdirRequested = m_itemFlags & KParts::BrowserExtension::ShowCreateDirectory;
265 if ( (currentDir || mkdirRequested) && m_pMenuNew )
268 m_pMenuNew->checkUpToDate();
269 m_pMenuNew->setPopupFiles(m_popupItemProperties.urlList());
271 q->addAction( m_pMenuNew );
274 else if (mkdirRequested)
276 KAction *actNewDir =
new KAction(m_parentWidget);
277 m_ownActions.append(actNewDir);
278 actNewDir->setIcon( KIcon(
"folder-new") );
279 actNewDir->setText( i18n(
"Create &Folder..." ) );
280 QObject::connect(actNewDir, SIGNAL(triggered()), q, SLOT(slotPopupNewDir()));
281 q->addAction( actNewDir );
284 }
else if ( isIntoTrash ) {
286 act =
new KAction(m_parentWidget );
287 m_ownActions.append(act);
288 act->setText( i18n(
"&Restore" ) );
289 act->setHelpText(i18n(
"Restores this file or directory, back to the location where it was deleted from initially"));
290 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotPopupRestoreTrashedItems()));
294 if (m_itemFlags & KParts::BrowserExtension::ShowNavigationItems)
296 if (m_itemFlags & KParts::BrowserExtension::ShowUp)
297 addNamedAction(
"go_up" );
298 addNamedAction(
"go_back" );
299 addNamedAction(
"go_forward" );
300 if (m_itemFlags & KParts::BrowserExtension::ShowReload)
301 addNamedAction(
"reload" );
305 if (!currentDir && isSymLink && !isSymLinkInSameDir) {
307 act =
new KAction(m_parentWidget);
308 m_ownActions.append(act);
309 act->setText(isDirectory ? i18n(
"Show Original Directory") : i18n(
"Show Original File"));
310 act->setHelpText(i18n(
"Opens a new file manager window showing the target of this link, in its parent directory."));
311 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotShowOriginalFile()));
317 q->addAction(actNewWindow);
320 addGroup(
"tabhandling" );
322 if (m_itemFlags & KParts::BrowserExtension::ShowUrlOperations) {
323 if ( !currentDir && sReading ) {
325 addNamedAction(
"cut" );
327 addNamedAction(
"copy" );
330 if ( isDirectory && sWriting ) {
332 addNamedAction(
"paste" );
334 addNamedAction(
"pasteto" );
337 if ( isCurrentTrash )
339 act =
new KAction(m_parentWidget);
340 m_ownActions.append(act);
341 act->setIcon( KIcon(
"trash-empty") );
342 act->setText( i18n(
"&Empty Trash Bin" ) );
343 KConfig trashConfig(
"trashrc", KConfig::SimpleConfig);
344 act->setEnabled( !trashConfig.group(
"Status").readEntry(
"Empty",
true ) );
345 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotPopupEmptyTrashBin()));
348 if ( isCurrentTrash )
350 act =
new KAction(m_parentWidget);
351 m_ownActions.append(act);
352 act->setIcon( KIcon(
"trash-empty") );
353 act->setText( i18n(
"&Configure Trash Bin" ) );
354 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotConfigTrashBin()));
360 addGroup(
"editactions" );
362 if (m_itemFlags & KParts::BrowserExtension::ShowTextSelectionItems) {
366 addGroup(
"partactions" );
370 if ( !isCurrentTrash && !isIntoTrash && (m_itemFlags & KParts::BrowserExtension::ShowBookmark))
375 const bool httpPage = m_sViewURL.protocol().startsWith(
"http", Qt::CaseInsensitive);
377 caption = i18n(
"&Bookmark This Page");
379 caption = i18n(
"&Bookmark This Location");
381 else if (isDirectory)
382 caption = i18n(
"&Bookmark This Folder");
384 caption = i18n(
"&Bookmark This Link");
386 caption = i18n(
"&Bookmark This File");
388 act =
new KAction(m_parentWidget);
389 m_ownActions.append(act);
390 act->setObjectName( QLatin1String(
"bookmark_add" ));
391 act->setIcon( KIcon(
"bookmark-new") );
392 act->setText( caption );
393 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotPopupAddToBookmark()));
394 if (lstItems.count() > 1)
395 act->setEnabled(
false);
396 if (KAuthorized::authorizeKAction(
"bookmarks"))
399 addGroup(
"linkactions" );
404 m_menuActions.setItemListProperties(m_popupItemProperties);
407 m_menuActions.addOpenWithActionsTo(q,
"DesktopEntryName != 'kfmclient' and DesktopEntryName != 'kfmclient_dir' and DesktopEntryName != 'kfmclient_html'");
410 if (!previewActions.isEmpty()) {
411 if (previewActions.count() == 1) {
412 q->addAction(previewActions.first());
414 QMenu* subMenu =
new QMenu(i18n(
"Preview In"), q);
415 subMenu->menuAction()->setObjectName( QLatin1String(
"preview_submenu" ));
417 subMenu->addActions(previewActions);
423 m_menuActions.addServiceActionsTo(q);
428 KSharedConfig::Ptr dolphin = KSharedConfig::openConfig(
"dolphinrc");
431 if (m_itemFlags & KParts::BrowserExtension::ShowUrlOperations &&
432 KConfigGroup(dolphin,
"General").readEntry(
"ShowCopyMoveMenu",
false)) {
434 m_copyToMenu.setItems(lstItems);
435 m_copyToMenu.setReadOnly(sMoving ==
false);
436 m_copyToMenu.addActionsTo(q);
440 if (!isCurrentTrash && !isIntoTrash && sReading &&
445 if ( (m_itemFlags & KParts::BrowserExtension::ShowProperties) && KPropertiesDialog::canDisplay( lstItems ) ) {
446 act =
new KAction(m_parentWidget);
447 m_ownActions.append(act);
448 act->setObjectName( QLatin1String(
"properties" ));
449 act->setText( i18n(
"&Properties" ) );
450 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotPopupProperties()));
454 while ( !q->actions().isEmpty() &&
455 q->actions().last()->isSeparator() )
456 delete q->actions().last();
458 if ( isDirectory && isLocal ) {
459 if ( KFileShare::authorization() == KFileShare::Authorized ) {
461 act =
new KAction(m_parentWidget);
462 m_ownActions.append(act);
463 act->setText( i18n(
"Share") );
464 QObject::connect(act, SIGNAL(triggered()), q, SLOT(slotOpenShareFileDialog()));
470 addGroup(
"partactions" );
473 void KonqPopupMenuPrivate::slotOpenShareFileDialog()
475 KPropertiesDialog* dlg =
new KPropertiesDialog( m_popupItemProperties.items(), m_parentWidget );
476 dlg->showFileSharingPage();
488 d->m_urlTitle = urlTitle;
493 return &(d->m_menuActions);
496 void KonqPopupMenuPrivate::slotPopupNewView()
498 Q_FOREACH(
const KUrl& url, m_popupItemProperties.urlList()) {
499 (void)
new KRun(url, m_parentWidget);
503 void KonqPopupMenuPrivate::slotPopupNewDir()
505 if (m_popupItemProperties.urlList().empty())
511 void KonqPopupMenuPrivate::slotPopupEmptyTrashBin()
516 void KonqPopupMenuPrivate::slotConfigTrashBin()
518 KRun::run(
"kcmshell4 kcmtrash", KUrl::List(), m_parentWidget);
521 void KonqPopupMenuPrivate::slotPopupRestoreTrashedItems()
526 void KonqPopupMenuPrivate::slotPopupAddToBookmark()
529 if (m_popupItemProperties.urlList().count() == 1) {
530 const KUrl url = m_popupItemProperties.urlList().first();
531 const QString title = m_urlTitle.isEmpty() ? url.prettyUrl() : m_urlTitle;
532 KBookmarkDialog dlg(m_bookmarkManager, m_parentWidget);
533 dlg.addBookmark(title, url.url());
537 root = m_bookmarkManager->root();
538 Q_FOREACH(
const KUrl& url, m_popupItemProperties.urlList()) {
539 root.addBookmark(url.prettyUrl(), url);
541 m_bookmarkManager->emitChanged(root);
545 void KonqPopupMenuPrivate::slotPopupMimeType()
550 void KonqPopupMenuPrivate::slotPopupProperties()
552 KPropertiesDialog::showDialog(m_popupItemProperties.items(), m_parentWidget,
false);
555 void KonqPopupMenuPrivate::addGroup(
const QString& name)
558 q->addActions(actions);
561 void KonqPopupMenuPrivate::addPlugins()
563 QString commonMimeType = m_popupItemProperties.mimeType();
564 if (commonMimeType.isEmpty()) {
565 commonMimeType = QLatin1String(
"application/octet-stream");
567 const KService::List konqPlugins = KMimeTypeTrader::self()->query(commonMimeType,
"KonqPopupMenu/Plugin",
"exist Library");
569 if (!konqPlugins.isEmpty()) {
570 m_popupMenuInfo.setItemListProperties(m_popupItemProperties);
571 m_popupMenuInfo.setParentWidget(m_parentWidget);
572 KService::List::ConstIterator iterator = konqPlugins.begin();
573 const KService::List::ConstIterator end = konqPlugins.end();
574 for(; iterator != end; ++iterator) {
579 plugin->setParent(q);
580 plugin->
setup(&m_ownActionCollection, m_popupMenuInfo, q);
584 const KService::List fileItemPlugins = KMimeTypeTrader::self()->query(commonMimeType,
"KFileItemAction/Plugin",
"exist Library");
585 if (!fileItemPlugins.isEmpty()) {
586 const KConfig config(
"kservicemenurc", KConfig::NoGlobals);
587 const KConfigGroup showGroup = config.group(
"Show");
589 foreach (
const KSharedPtr<KService>& service, fileItemPlugins) {
590 if (!showGroup.readEntry(service->desktopEntryName(),
true)) {
596 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
598 plugin->setParent(q);
599 q->addActions(plugin->actions(m_popupItemProperties, m_parentWidget));
602 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
603 if (abstractPlugin) {
604 abstractPlugin->setParent(q);
605 q->addActions(abstractPlugin->actions(m_popupItemProperties, m_parentWidget));
611 void KonqPopupMenuPrivate::slotShowOriginalFile()
613 const KFileItem item = m_popupItemProperties.items().first();
614 const QString dest = item.linkDest();
615 KUrl destUrl = m_sViewURL;
616 if (dest.startsWith(
'/')) {
617 destUrl.setPath(dest);
619 destUrl.addPath(dest);
622 destUrl.setPath(destUrl.directory());
623 KRun::runUrl(destUrl,
"inode/directory", m_parentWidget);
626 #include "konq_popupmenu.moc"
static KIO::SimpleJob * newDir(QWidget *parent, const KUrl &baseUrl)
Ask for the name of a new directory and create it.
static void emptyTrash(QWidget *parent)
Empty the trash.
static void editMimeType(const QString &mimeType, QWidget *parent)
Pop up properties dialog for mimetype mimeType.
static void restoreTrashedItems(const KUrl::List &urls, QWidget *parent)
Restore trashed items.