X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/c33b01efb2aed904af9ffaef27fb3b0b4bdda37b..ab39a5952001cdb3d1b9ca693da7f8e246558fb8:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index dd88fb74b..8b0ea4e92 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -21,13 +21,16 @@ #include "dolphincontextmenu.h" #include "dolphinmainwindow.h" -#include "dolphinsettings.h" +#include "dolphinnewmenu.h" +#include "settings/dolphinsettings.h" #include "dolphinview.h" #include "dolphinviewcontainer.h" +#include "dolphin_generalsettings.h" #include -#include #include +#include +#include #include #include #include @@ -53,19 +56,29 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, const KFileItem& fileInfo, const KUrl& baseUrl) : m_mainWindow(parent), + m_capabilities(0), m_fileInfo(fileInfo), m_baseUrl(baseUrl), - m_context(NoContext) + m_context(NoContext), + m_copyToMenu(parent), + m_customActions() { // The context menu either accesses the URLs of the selected items // or the items itself. To increase the performance both lists are cached. - DolphinView* view = m_mainWindow->activeViewContainer()->view(); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); m_selectedUrls = view->selectedUrls(); m_selectedItems = view->selectedItems(); } DolphinContextMenu::~DolphinContextMenu() { + delete m_capabilities; + m_capabilities = 0; +} + +void DolphinContextMenu::setCustomActions(const QList& actions) +{ + m_customActions = actions; } void DolphinContextMenu::open() @@ -75,7 +88,7 @@ void DolphinContextMenu::open() m_context |= TrashContext; } - if (!m_fileInfo.isNull() && (m_selectedItems.count() > 0)) { + if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) { m_context |= ItemContext; // TODO: handle other use cases like devices + desktop files } @@ -111,12 +124,19 @@ void DolphinContextMenu::openTrashContextMenu() QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"), i18nc("@action:inmenu Add current folder to places", "Add to Places")); + // Don't show if url is already in places + if (placeExists(m_mainWindow->activeViewContainer()->url())) { + addToPlacesAction->setVisible(false); + } + + addCustomActions(popup); + QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); popup->addAction(propertiesAction); QAction *action = popup->exec(QCursor::pos()); if (action == emptyTrashAction) { - const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted.")); + const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.")); const bool del = KMessageBox::warningContinueCancel(m_mainWindow, text, QString(), @@ -166,6 +186,26 @@ void DolphinContextMenu::openItemContextMenu() Q_ASSERT(!m_fileInfo.isNull()); KMenu* popup = new KMenu(m_mainWindow); + if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) { + // setup 'Create New' menu + DolphinNewMenu* newMenu = new DolphinNewMenu(popup, m_mainWindow); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + newMenu->setViewShowsHiddenFiles(view->showHiddenFiles()); + newMenu->slotCheckUpToDate(); + newMenu->setPopupFiles(m_fileInfo.url()); + newMenu->setEnabled(capabilities().supportsWriting()); + + KMenu* menu = newMenu->menu(); + menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); + menu->setIcon(KIcon("document-new")); + popup->addMenu(newMenu->menu()); + popup->addSeparator(); + + // insert 'Open in new window' and 'Open in new tab' entries + popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window")); + popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab")); + popup->addSeparator(); + } addShowMenubarAction(popup); insertDefaultItemActions(popup); @@ -176,17 +216,34 @@ void DolphinContextMenu::openItemContextMenu() if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) { addToPlacesAction = popup->addAction(KIcon("bookmark-new"), i18nc("@action:inmenu Add selected folder to places", "Add to Places")); + // Don't show if url is already in places + if (placeExists(m_fileInfo.url())) { + addToPlacesAction->setVisible(false); + } } - // Insert 'Open With...' sub menu - QVector openWithVector; - const QList openWithActions = insertOpenWithItems(popup, openWithVector); - - // Insert 'Actions' sub menu KonqMenuActions menuActions; - menuActions.setItems(m_selectedItems); - if (menuActions.addActionsTo(popup)) + menuActions.setParentWidget(m_mainWindow); + menuActions.setItemListProperties(m_selectedItems); + + // insert 'Open With...' action or sub menu + menuActions.addOpenWithActionsTo(popup, "DesktopEntryName != 'dolphin'"); + + // insert 'Actions' sub menu + if (menuActions.addActionsTo(popup)) { + popup->addSeparator(); + } + + // insert version control actions + addRevisionControlActions(popup); + + // insert 'Copy To' and 'Move To' sub menus + if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) { + m_copyToMenu.setItems(m_selectedItems); + m_copyToMenu.setReadOnly(!capabilities().supportsWriting()); + m_copyToMenu.addActionsTo(popup); popup->addSeparator(); + } // insert 'Properties...' entry QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); @@ -200,19 +257,8 @@ void DolphinContextMenu::openItemContextMenu() DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl), selectedUrl); } - } else if (openWithActions.contains(activatedAction)) { - // one of the 'Open With' items has been selected - if (openWithActions.last() == activatedAction) { - // the item 'Other...' has been selected - KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow); - } else { - int id = openWithActions.indexOf(activatedAction); - KService::Ptr servicePtr = openWithVector[id]; - KRun::run(*servicePtr, m_selectedUrls, m_mainWindow); - } } - openWithVector.clear(); popup->deleteLater(); } @@ -224,6 +270,8 @@ void DolphinContextMenu::openViewportContextMenu() // setup 'Create New' menu KNewMenu* newMenu = m_mainWindow->newMenu(); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + newMenu->setViewShowsHiddenFiles(view->showHiddenFiles()); newMenu->slotCheckUpToDate(); newMenu->setPopupFiles(m_baseUrl); popup->addMenu(newMenu->menu()); @@ -233,7 +281,7 @@ void DolphinContextMenu::openViewportContextMenu() popup->addAction(pasteAction); // setup 'View Mode' menu - KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode")); + KMenu* viewModeMenu = new KMenu(i18nc("@title:menu", "View Mode"), popup); QAction* iconsMode = m_mainWindow->actionCollection()->action("icons"); viewModeMenu->addAction(iconsMode); @@ -244,24 +292,31 @@ void DolphinContextMenu::openViewportContextMenu() QAction* columnsMode = m_mainWindow->actionCollection()->action("columns"); viewModeMenu->addAction(columnsMode); - QAction* previewsMode = m_mainWindow->actionCollection()->action("previews"); - viewModeMenu->addAction(previewsMode); - popup->addMenu(viewModeMenu); popup->addSeparator(); + addRevisionControlActions(popup); + QAction* addToPlacesAction = popup->addAction(KIcon("bookmark-new"), i18nc("@action:inmenu Add current folder to places", "Add to Places")); - popup->addSeparator(); - QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties")); + // Don't show if url is already in places + if (placeExists(m_mainWindow->activeViewContainer()->url())) { + addToPlacesAction->setVisible(false); + } + addCustomActions(popup); + + QAction* propertiesAction = popup->addAction(i18nc("@action:inmenu", "Properties")); + propertiesAction->setIcon(KIcon("document-properties")); QAction* action = popup->exec(QCursor::pos()); if (action == propertiesAction) { const KUrl& url = m_mainWindow->activeViewContainer()->url(); - KPropertiesDialog dialog(url, m_mainWindow); - dialog.exec(); + + KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow); + dialog->setAttribute(Qt::WA_DeleteOnClose); + dialog->show(); } else if (action == addToPlacesAction) { const KUrl& url = m_mainWindow->activeViewContainer()->url(); if (url.isValid()) { @@ -292,8 +347,10 @@ void DolphinContextMenu::insertDefaultItemActions(KMenu* popup) popup->addAction(renameAction); // insert 'Move to Trash' and (optionally) 'Delete' - KConfigGroup kdeConfig(KGlobal::config(), "KDE"); - bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false); + KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals); + KConfigGroup configGroup(globalConfig, "KDE"); + bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false); + const KUrl& url = m_mainWindow->activeViewContainer()->url(); if (url.isLocalFile()) { QAction* moveToTrashAction = collection->action("move_to_trash"); @@ -308,98 +365,10 @@ void DolphinContextMenu::insertDefaultItemActions(KMenu* popup) } } -QList DolphinContextMenu::insertOpenWithItems(KMenu* popup, - QVector& openWithVector) -{ - // Parts of the following code have been taken - // from the class KonqOperations located in - // libqonq/konq_operations.h of Konqueror. - // (Copyright (C) 2000 David Faure ) - - // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications - // are listed which are registered to open the item. As last entry "Other..." will be - // attached which allows to select a custom application. If no applications are registered - // no sub menu is created at all, only "Open With..." will be offered. - bool insertOpenWithItems = true; - const QString contextMimeType(m_fileInfo.mimetype()); - - QListIterator mimeIt(m_selectedItems); - while (insertOpenWithItems && mimeIt.hasNext()) { - KFileItem item = mimeIt.next(); - insertOpenWithItems = (contextMimeType == item.mimetype()); - } - - QList openWithActions; - if (insertOpenWithItems) { - // fill the 'Open with' sub menu with application types - const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo.url()); - KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(), - "Application", - "Type == 'Application'"); - if (offers.count() > 0) { - KService::List::Iterator it; - KMenu* openWithMenu = new KMenu(i18nc("@title:menu", "Open With")); - for (it = offers.begin(); it != offers.end(); ++it) { - // The offer list from the KTrader returns duplicate - // application entries. Although this seems to be a configuration - // problem outside the scope of Dolphin, duplicated entries just - // will be skipped here. - const QString appName((*it)->name()); - if (!containsEntry(openWithMenu, appName)) { - const KIcon icon((*it)->icon()); - QAction* action = openWithMenu->addAction(icon, appName); - openWithVector.append(*it); - openWithActions << action; - } - } - - openWithMenu->addSeparator(); - QAction* action = openWithMenu->addAction(i18nc("@action:inmenu Open With", "&Other...")); - - openWithActions << action; - popup->addMenu(openWithMenu); - } else { - // No applications are registered, hence just offer - // a "Open With..." item instead of a sub menu containing - // only one entry. - QAction* action = popup->addAction(i18nc("@title:menu", "Open With...")); - openWithActions << action; - } - } else { - // At least one of the selected items has a different MIME type. In this case - // just show a disabled "Open With..." entry. - QAction* action = popup->addAction(i18nc("@title:menu", "Open With...")); - action->setEnabled(false); - } - - return openWithActions; -} - -bool DolphinContextMenu::containsEntry(const KMenu* menu, - const QString& entryName) const -{ - Q_ASSERT(menu != 0); - - const QList list = menu->actions(); - const uint count = list.count(); - for (uint i = 0; i < count; ++i) { - const QAction* action = list.at(i); - if (action->text() == entryName) { - return true; - } - } - - return false; -} - void DolphinContextMenu::addShowMenubarAction(KMenu* menu) { KAction* showMenuBar = m_mainWindow->showMenuBarAction(); if (!m_mainWindow->menuBar()->isVisible()) { - // TODO: it should not be necessary to uncheck the menu - // bar action, but currently the action states don't get - // updated if the menu is disabled - showMenuBar->setChecked(false); menu->addAction(showMenuBar); menu->addSeparator(); } @@ -414,14 +383,30 @@ QString DolphinContextMenu::placesName(const KUrl& url) const return name; } +bool DolphinContextMenu::placeExists(const KUrl& url) const +{ + const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel(); + const int count = placesModel->rowCount(); + + for (int i = 0; i < count; ++i) { + const QModelIndex index = placesModel->index(i, 0); + + if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) { + return true; + } + } + return false; +} + QAction* DolphinContextMenu::createPasteAction() { QAction* action = 0; - if ((m_selectedItems.count() == 1) && m_fileInfo.isDir()) { + const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir(); + if (isDir && (m_selectedItems.count() == 1)) { action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this); const QMimeData* mimeData = QApplication::clipboard()->mimeData(); const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData); - action->setEnabled(!pasteData.isEmpty()); + action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting()); connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder())); } else { action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); @@ -430,4 +415,31 @@ QAction* DolphinContextMenu::createPasteAction() return action; } +KFileItemListProperties& DolphinContextMenu::capabilities() +{ + if (m_capabilities == 0) { + m_capabilities = new KFileItemListProperties(m_selectedItems); + } + return *m_capabilities; +} + +void DolphinContextMenu::addRevisionControlActions(KMenu* menu) +{ + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + const QList versionControlActions = view->versionControlActions(m_selectedItems); + if (!versionControlActions.isEmpty()) { + foreach (QAction* action, versionControlActions) { + menu->addAction(action); + } + menu->addSeparator(); + } +} + +void DolphinContextMenu::addCustomActions(KMenu* menu) +{ + foreach (QAction* action, m_customActions) { + menu->addAction(action); + } +} + #include "dolphincontextmenu.moc"