]> cloud.milkyroute.net Git - dolphin.git/commitdiff
let the context menu be aware about the trash
authorPeter Penz <peter.penz19@gmail.com>
Thu, 15 Mar 2007 21:24:41 +0000 (21:24 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 15 Mar 2007 21:24:41 +0000 (21:24 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=642935

src/dolphincontextmenu.cpp
src/dolphincontextmenu.h
src/dolphinmainwindow.cpp

index 14760658128d3477ab2361eb7b95e6086a28e502..f6ee7c78f168edc04286920a178e94c57df3c788 100644 (file)
@@ -25,8 +25,6 @@
 #include "dolphinview.h"
 #include "editbookmarkdialog.h"
 
-#include <assert.h>
-
 #include <kactioncollection.h>
 #include <kbookmarkmanager.h>
 #include <kbookmark.h>
 #include <kiconloader.h>
 #include <kio/netaccess.h>
 #include <kmenu.h>
+#include <kmessagebox.h>
 #include <kmimetypetrader.h>
 #include <knewmenu.h>
+#include <konq_operations.h>
 #include <klocale.h>
 #include <kpropertiesdialog.h>
 #include <krun.h>
 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<KService::Ptr> openWithVector;
+    const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
+
+    // Insert 'Actions' sub menu
+    QVector<KDEDesktopMimeType::Service> actionsVector;
+    const QList<QAction*> 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<KService::Ptr> openWithVector;
-    const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
-
-    // Insert 'Actions' sub menu
-    QVector<KDEDesktopMimeType::Service> actionsVector;
-    const QList<QAction*> 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<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
@@ -411,7 +507,7 @@ QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
         if (menu == actionsMenu) {
             // The item is an action, hence show the action in the root menu.
             const QList<QAction*> 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<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
 bool DolphinContextMenu::containsEntry(const KMenu* menu,
                                        const QString& entryName) const
 {
-    assert(menu != 0);
+    Q_ASSERT(menu != 0);
 
     const QList<QAction*> list = menu->actions();
     const uint count = list.count();
index 82bc9f24d9ed61ba843793e9d8735482ba5072bb..ac9a19d7c6533766d9efd2ff18ee3f1b3c4f70d1 100644 (file)
@@ -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
index 5183f993d31971e0c895df9343a4ca2549ad0c4c..f305468c84bb6e138523408fbe92f92e69506de8 100644 (file)
@@ -204,6 +204,7 @@ void DolphinMainWindow::dropUrls(const KUrl::List& urls,
 \r
 void DolphinMainWindow::rename(const KUrl& oldUrl, const KUrl& newUrl)\r
 {\r
+    clearStatusBar();\r
     KonqOperations::rename(this, oldUrl, newUrl);\r
     m_undoCommandTypes.append(KonqUndoManager::RENAME);\r
 }\r
@@ -456,17 +457,29 @@ void DolphinMainWindow::deleteItems()
 {\r
     clearStatusBar();\r
 \r
+    // TODO: if KonqOperations::askDeleteConfirmation() would indicate when\r
+    // the operation has been finished, this method should be used.\r
+\r
     KUrl::List list = m_activeView->selectedUrls();\r
     const uint itemCount = list.count();\r
     Q_ASSERT(itemCount >= 1);\r
 \r
     QString text;\r
     if (itemCount > 1) {\r
-        text = i18n("Do you really want to delete the %1 selected items?",itemCount);\r
+        text = i18n("Do you really want to delete the %1 selected items?", itemCount);\r
     }\r
     else {\r
         const KUrl& url = list.first();\r
-        text = i18n("Do you really want to delete '%1'?",url.fileName());\r
+        QString itemName;\r
+        if (url.protocol() == "trash" ) {\r
+            itemName = url.path();\r
+            // TODO: check comment in konq_undo.cc in the method askDeleteConfirmation()\r
+            itemName.remove(QRegExp("^/[0-9]*-"));\r
+        }\r
+        else {\r
+           itemName = url.pathOrUrl();\r
+        }\r
+        text = i18n("Do you really want to delete '%1'?", itemName);\r
     }\r
 \r
     const bool del = KMessageBox::warningContinueCancel(this,\r
@@ -544,7 +557,7 @@ void DolphinMainWindow::slotUndoAvailable(bool available)
                                       DolphinStatusBar::OperationCompleted);\r
                 break;\r
 \r
-           case KonqUndoManager::MKDIR:\r
+            case KonqUndoManager::MKDIR:\r
                 statusBar->setMessage(i18n("Created directory."),\r
                                       DolphinStatusBar::OperationCompleted);\r
                 break;\r