From: Peter Penz Date: Thu, 15 Mar 2007 21:24:41 +0000 (+0000) Subject: let the context menu be aware about the trash X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/242bda1eaddd863b12afe8f3da7cf11d98475ccb let the context menu be aware about the trash svn path=/trunk/KDE/kdebase/apps/; revision=642935 --- diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 147606581..f6ee7c78f 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -25,8 +25,6 @@ #include "dolphinview.h" #include "editbookmarkdialog.h" -#include - #include #include #include @@ -35,8 +33,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -48,50 +48,205 @@ DolphinContextMenu::DolphinContextMenu(DolphinView* parent, KFileItem* fileInfo) : m_dolphinView(parent), - m_fileInfo(fileInfo) + m_fileInfo(fileInfo), + m_context(NoContext) { } void DolphinContextMenu::open() { - if (m_fileInfo == 0) { - openViewportContextMenu(); + // get the context information + const KUrl& url = m_dolphinView->url(); + if (url.protocol() == "trash") { + m_context |= TrashContext; } - else { + + if (m_fileInfo != 0) { + m_context |= ItemContext; + + // TODO: handle other use cases like devices + desktop files + /* + const KUrl::List urls = m_dolphinView->selectedUrls(); + + KUrl::List::const_iterator it = urls.begin(); + KUrl::List::const_iterator end = urls.end(); + while (it != end) { + const KUrl& url = *it; + + ++it; + }*/ + } + + // open the corresponding popup for the context + if (m_context & TrashContext) { + if (m_context & ItemContext) { + openTrashItemContextMenu(); + } + else { + openTrashContextMenu(); + } + } + else if (m_context & ItemContext) { openItemContextMenu(); } + else { + Q_ASSERT(m_context == NoContext); + openViewportContextMenu(); + } } DolphinContextMenu::~DolphinContextMenu() { } +void DolphinContextMenu::openTrashContextMenu() +{ + Q_ASSERT(m_context & TrashContext); + + KMenu* popup = new KMenu(m_dolphinView); + + QAction* emptyTrashAction = new QAction(KIcon("user-trash"), i18n("Emtpy Trash"), popup); + KConfig trashConfig("trashrc", KConfig::OnlyLocal); + emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true)); + popup->addAction(emptyTrashAction); + + DolphinMainWindow* mainWindow = m_dolphinView->mainWindow(); + QAction* propertiesAction = mainWindow->actionCollection()->action("properties"); + popup->addAction(propertiesAction); + + if (popup->exec(QCursor::pos()) == emptyTrashAction) { + const QString text(i18n("Do you really want to empty the Trash? All items will get deleted.")); + const bool del = KMessageBox::warningContinueCancel(mainWindow, + text, + QString(), + KGuiItem(i18n("Empty Trash"), KIcon("user-trash")) + ) == KMessageBox::Continue; + if (del) { + KonqOperations::emptyTrash(m_dolphinView); + } + } + + popup->deleteLater(); +} + +void DolphinContextMenu::openTrashItemContextMenu() +{ + Q_ASSERT(m_context & TrashContext); + Q_ASSERT(m_context & ItemContext); + + KMenu* popup = new KMenu(m_dolphinView); + + DolphinMainWindow* mainWindow = m_dolphinView->mainWindow(); + QAction* restoreAction = new QAction(i18n("Restore"), m_dolphinView); + popup->addAction(restoreAction); + + QAction* deleteAction = mainWindow->actionCollection()->action("delete"); + popup->addAction(deleteAction); + + QAction* propertiesAction = mainWindow->actionCollection()->action("properties"); + popup->addAction(propertiesAction); + + if (popup->exec(QCursor::pos()) == restoreAction) { + const KUrl::List urls = m_dolphinView->selectedUrls(); + KonqOperations::restoreTrashedItems(urls, m_dolphinView); + } + + popup->deleteLater(); +} + +void DolphinContextMenu::openItemContextMenu() +{ + Q_ASSERT(m_fileInfo != 0); + + KMenu* popup = new KMenu(m_dolphinView); + insertDefaultItemActions(popup); + + DolphinMainWindow* mainWindow = m_dolphinView->mainWindow(); + const KUrl::List urls = m_dolphinView->selectedUrls(); + + // insert 'Bookmark this folder' entry if exactly one item is selected + QAction* bookmarkAction = 0; + if (m_fileInfo->isDir() && (urls.count() == 1)) { + bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder")); + } + + // Insert 'Open With...' sub menu + QVector openWithVector; + const QList openWithActions = insertOpenWithItems(popup, openWithVector); + + // Insert 'Actions' sub menu + QVector actionsVector; + const QList serviceActions = insertActionItems(popup, actionsVector); + popup->addSeparator(); + + // insert 'Properties...' entry + QAction* propertiesAction = mainWindow->actionCollection()->action("properties"); + popup->addAction(propertiesAction); + + QAction* activatedAction = popup->exec(QCursor::pos()); + + if ((bookmarkAction!= 0) && (activatedAction == bookmarkAction)) { + const KUrl selectedUrl(m_fileInfo->url()); + KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"), + selectedUrl.fileName(), + selectedUrl, + "bookmark"); + if (!bookmark.isNull()) { + KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager(); + KBookmarkGroup root = manager->root(); + root.addBookmark(manager, bookmark); + manager->emitChanged(root); + } + } + else if (serviceActions.contains(activatedAction)) { + // one of the 'Actions' items has been selected + int id = serviceActions.indexOf(activatedAction); + KDEDesktopMimeType::executeService(urls, actionsVector[id]); + } + 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(urls, m_dolphinView); + } + else { + int id = openWithActions.indexOf(activatedAction); + KService::Ptr servicePtr = openWithVector[id]; + KRun::run(*servicePtr, urls, m_dolphinView); + } + } + + openWithVector.clear(); + actionsVector.clear(); + popup->deleteLater(); +} + void DolphinContextMenu::openViewportContextMenu() { - assert(m_fileInfo == 0); - DolphinMainWindow* dolphin = m_dolphinView->mainWindow(); + Q_ASSERT(m_fileInfo == 0); + DolphinMainWindow* mainWindow = m_dolphinView->mainWindow(); KMenu* popup = new KMenu(m_dolphinView); // setup 'Create New' menu - KNewMenu* newMenu = dolphin->newMenu(); + KNewMenu* newMenu = mainWindow->newMenu(); newMenu->slotCheckUpToDate(); newMenu->setPopupFiles(m_dolphinView->url()); popup->addMenu(newMenu->menu()); popup->addSeparator(); - QAction* pasteAction = dolphin->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste)); + QAction* pasteAction = mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste)); popup->addAction(pasteAction); // setup 'View Mode' menu KMenu* viewModeMenu = new KMenu(i18n("View Mode")); - QAction* iconsMode = dolphin->actionCollection()->action("icons"); + QAction* iconsMode = mainWindow->actionCollection()->action("icons"); viewModeMenu->addAction(iconsMode); - QAction* detailsMode = dolphin->actionCollection()->action("details"); + QAction* detailsMode = mainWindow->actionCollection()->action("details"); viewModeMenu->addAction(detailsMode); - QAction* previewsMode = dolphin->actionCollection()->action("previews"); + QAction* previewsMode = mainWindow->actionCollection()->action("previews"); viewModeMenu->addAction(previewsMode); popup->addMenu(viewModeMenu); @@ -104,10 +259,10 @@ void DolphinContextMenu::openViewportContextMenu() QAction* activatedAction = popup->exec(QCursor::pos()); if (activatedAction == propertiesAction) { - new KPropertiesDialog(dolphin->activeView()->url()); + new KPropertiesDialog(mainWindow->activeView()->url()); } else if (activatedAction == bookmarkAction) { - const KUrl& url = dolphin->activeView()->url(); + const KUrl& url = mainWindow->activeView()->url(); KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"), url.fileName(), url, @@ -123,12 +278,11 @@ void DolphinContextMenu::openViewportContextMenu() popup->deleteLater(); } -void DolphinContextMenu::openItemContextMenu() +void DolphinContextMenu::insertDefaultItemActions(KMenu* popup) { - assert(m_fileInfo != 0); + Q_ASSERT(popup != 0); - KMenu* popup = new KMenu(m_dolphinView); - DolphinMainWindow* dolphin = m_dolphinView->mainWindow(); + DolphinMainWindow* mainWindow = m_dolphinView->mainWindow(); const KUrl::List urls = m_dolphinView->selectedUrls(); // insert 'Cut', 'Copy' and 'Paste' @@ -140,7 +294,7 @@ void DolphinContextMenu::openItemContextMenu() const int count = sizeof(actionNames) / sizeof(KStandardAction::StandardAction); for (int i = 0; i < count; ++i) { - QAction* action = dolphin->actionCollection()->action(KStandardAction::stdName(actionNames[i])); + QAction* action = mainWindow->actionCollection()->action(KStandardAction::stdName(actionNames[i])); if (action != 0) { popup->addAction(action); } @@ -148,16 +302,16 @@ void DolphinContextMenu::openItemContextMenu() popup->addSeparator(); // insert 'Rename' - QAction* renameAction = dolphin->actionCollection()->action("rename"); + QAction* renameAction = mainWindow->actionCollection()->action("rename"); popup->addAction(renameAction); // insert 'Move to Trash' and (optionally) 'Delete' const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals); const KConfigGroup kdeConfig(globalConfig, "KDE"); bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false); - const KUrl& url = dolphin->activeView()->url(); + const KUrl& url = mainWindow->activeView()->url(); if (url.isLocalFile()) { - QAction* moveToTrashAction = dolphin->actionCollection()->action("move_to_trash"); + QAction* moveToTrashAction = mainWindow->actionCollection()->action("move_to_trash"); popup->addAction(moveToTrashAction); } else { @@ -165,67 +319,9 @@ void DolphinContextMenu::openItemContextMenu() } if (showDeleteCommand) { - QAction* deleteAction = dolphin->actionCollection()->action("delete"); + QAction* deleteAction = mainWindow->actionCollection()->action("delete"); popup->addAction(deleteAction); } - - // insert 'Bookmark this folder...' entry - // urls is a list of selected items, so insert boolmark menu if - // urls contains only one item, i.e. no multiple selection made - QAction* bookmarkAction = 0; - if (m_fileInfo->isDir() && (urls.count() == 1)) { - bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder")); - } - - // Insert 'Open With...' sub menu - QVector openWithVector; - const QList openWithActions = insertOpenWithItems(popup, openWithVector); - - // Insert 'Actions' sub menu - QVector actionsVector; - const QList serviceActions = insertActionItems(popup, actionsVector); - popup->addSeparator(); - - // insert 'Properties...' entry - QAction* propertiesAction = dolphin->actionCollection()->action("properties"); - popup->addAction(propertiesAction); - - QAction* activatedAction = popup->exec(QCursor::pos()); - - if ((bookmarkAction!= 0) && (activatedAction == bookmarkAction)) { - const KUrl selectedUrl(m_fileInfo->url()); - KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"), - selectedUrl.fileName(), - selectedUrl, - "bookmark"); - if (!bookmark.isNull()) { - KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager(); - KBookmarkGroup root = manager->root(); - root.addBookmark(manager, bookmark); - manager->emitChanged(root); - } - } - else if (serviceActions.contains(activatedAction)) { - // one of the 'Actions' items has been selected - int id = serviceActions.indexOf(activatedAction); - KDEDesktopMimeType::executeService(urls, actionsVector[id]); - } - 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(urls, m_dolphinView); - } - else { - int id = openWithActions.indexOf(activatedAction); - KService::Ptr servicePtr = openWithVector[id]; - KRun::run(*servicePtr, urls, m_dolphinView); - } - } - - openWithVector.clear(); - actionsVector.clear(); - popup->deleteLater(); } QList DolphinContextMenu::insertOpenWithItems(KMenu* popup, @@ -411,7 +507,7 @@ QList DolphinContextMenu::insertActionItems(KMenu* popup, if (menu == actionsMenu) { // The item is an action, hence show the action in the root menu. const QList actions = actionsMenu->actions(); - assert(actions.count() == 1); + Q_ASSERT(actions.count() == 1); const QString text = actions[0]->text(); const QIcon icon = actions[0]->icon(); @@ -443,7 +539,7 @@ QList DolphinContextMenu::insertActionItems(KMenu* popup, bool DolphinContextMenu::containsEntry(const KMenu* menu, const QString& entryName) const { - assert(menu != 0); + Q_ASSERT(menu != 0); const QList list = menu->actions(); const uint count = list.count(); diff --git a/src/dolphincontextmenu.h b/src/dolphincontextmenu.h index 82bc9f24d..ac9a19d7c 100644 --- a/src/dolphincontextmenu.h +++ b/src/dolphincontextmenu.h @@ -67,8 +67,12 @@ public: void open(); private: - void openViewportContextMenu(); + void openTrashContextMenu(); + void openTrashItemContextMenu(); void openItemContextMenu(); + void openViewportContextMenu(); + + void insertDefaultItemActions(KMenu* popup); /** * Inserts the 'Open With...' submenu to \a popup. @@ -100,9 +104,7 @@ private: bool containsEntry(const KMenu* menu, const QString& entryName) const; - DolphinView* m_dolphinView; - KFileItem* m_fileInfo; - +private: struct Entry { int type; QString name; @@ -111,6 +113,16 @@ private: QString icon; QString comment; }; + + enum ContextType { + NoContext = 0, + ItemContext = 1, + TrashContext = 2 + }; + + DolphinView* m_dolphinView; + KFileItem* m_fileInfo; + int m_context; }; #endif diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 5183f993d..f305468c8 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -204,6 +204,7 @@ void DolphinMainWindow::dropUrls(const KUrl::List& urls, void DolphinMainWindow::rename(const KUrl& oldUrl, const KUrl& newUrl) { + clearStatusBar(); KonqOperations::rename(this, oldUrl, newUrl); m_undoCommandTypes.append(KonqUndoManager::RENAME); } @@ -456,17 +457,29 @@ void DolphinMainWindow::deleteItems() { clearStatusBar(); + // TODO: if KonqOperations::askDeleteConfirmation() would indicate when + // the operation has been finished, this method should be used. + KUrl::List list = m_activeView->selectedUrls(); const uint itemCount = list.count(); Q_ASSERT(itemCount >= 1); QString text; if (itemCount > 1) { - text = i18n("Do you really want to delete the %1 selected items?",itemCount); + text = i18n("Do you really want to delete the %1 selected items?", itemCount); } else { const KUrl& url = list.first(); - text = i18n("Do you really want to delete '%1'?",url.fileName()); + QString itemName; + if (url.protocol() == "trash" ) { + itemName = url.path(); + // TODO: check comment in konq_undo.cc in the method askDeleteConfirmation() + itemName.remove(QRegExp("^/[0-9]*-")); + } + else { + itemName = url.pathOrUrl(); + } + text = i18n("Do you really want to delete '%1'?", itemName); } const bool del = KMessageBox::warningContinueCancel(this, @@ -544,7 +557,7 @@ void DolphinMainWindow::slotUndoAvailable(bool available) DolphinStatusBar::OperationCompleted); break; - case KonqUndoManager::MKDIR: + case KonqUndoManager::MKDIR: statusBar->setMessage(i18n("Created directory."), DolphinStatusBar::OperationCompleted); break;