]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
avoid unnecessary copies by using const & in foreach
[dolphin.git] / src / dolphinmainwindow.cpp
index f64f847feb97d69705e6c581ff90e64aad6d4d68..48956e0936803a0ea127cc4834956fc6dd00640f 100644 (file)
@@ -32,6 +32,7 @@
 #include "dolphinsettingsdialog.h"
 #include "dolphinstatusbar.h"
 #include "dolphinviewcontainer.h"
+#include "fileitemcapabilities.h"
 #include "infosidebarpage.h"
 #include "metadatawidget.h"
 #include "mainwindowadaptor.h"
@@ -101,10 +102,8 @@ DolphinMainWindow::DolphinMainWindow(int id) :
     new MainWindowAdaptor(this);
     QDBusConnection::sessionBus().registerObject(QString("/dolphin/MainWindow%1").arg(m_id), this);
 
-    KonqFileUndoManager::incRef();
-
     KonqFileUndoManager* undoManager = KonqFileUndoManager::self();
-    undoManager->setUiInterface(new UndoUiInterface(this));
+    undoManager->setUiInterface(new UndoUiInterface());
 
     connect(undoManager, SIGNAL(undoAvailable(bool)),
             this, SLOT(slotUndoAvailable(bool)));
@@ -116,7 +115,6 @@ DolphinMainWindow::DolphinMainWindow(int id) :
 
 DolphinMainWindow::~DolphinMainWindow()
 {
-    KonqFileUndoManager::decRef();
     DolphinApplication::app()->removeMainWindow(this);
 }
 
@@ -180,10 +178,9 @@ void DolphinMainWindow::changeUrl(const KUrl& url)
         updateEditActions();
         updateViewActions();
         updateGoActions();
-        const QString caption = url.fileName();
-        setCaption(caption);
+        setCaption(url.fileName());
         if (m_viewTab.count() > 1) {
-            m_tabBar->setTabText(m_tabIndex, caption);
+            m_tabBar->setTabText(m_tabIndex, tabName(url));
         }
         emit urlChanged(url);
     }
@@ -267,11 +264,11 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
     if (m_viewTab.count() == 1) {
         // Only one view is open currently and hence no tab is shown at
         // all. Before creating a tab for 'url', provide a tab for the current URL.
-        m_tabBar->addTab(KIcon("folder"), m_activeViewContainer->url().fileName());
+        m_tabBar->addTab(KIcon("folder"), tabName(m_activeViewContainer->url()));
         m_tabBar->blockSignals(false);
     }
 
-    m_tabBar->addTab(KIcon("folder"), url.fileName());
+    m_tabBar->addTab(KIcon("folder"), tabName(url));
 
     ViewTab viewTab;
     viewTab.splitter = new QSplitter(this);
@@ -437,6 +434,7 @@ void DolphinMainWindow::slotUndoTextChanged(const QString& text)
 void DolphinMainWindow::undo()
 {
     clearStatusBar();
+    KonqFileUndoManager::self()->uiInterface()->setParentWidget(this);
     KonqFileUndoManager::self()->undo();
 }
 
@@ -458,10 +456,6 @@ void DolphinMainWindow::paste()
 void DolphinMainWindow::updatePasteAction()
 {
     QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
-    if (pasteAction == 0) {
-        return;
-    }
-
     QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
     pasteAction->setEnabled(pasteInfo.first);
     pasteAction->setText(pasteInfo.second);
@@ -694,6 +688,11 @@ void DolphinMainWindow::setActiveTab(int index)
                                                          viewTab.secondaryView);
 }
 
+void DolphinMainWindow::closeTab()
+{
+    closeTab(m_tabBar->currentIndex());
+}
+
 void DolphinMainWindow::closeTab(int index)
 {
     Q_ASSERT(index >= 0);
@@ -734,6 +733,39 @@ void DolphinMainWindow::closeTab(int index)
     }
 }
 
+void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
+{
+    KMenu menu(this);
+
+    QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
+    newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
+
+    QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Other Tabs"));
+
+    QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
+    closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
+
+    QAction* selectedAction = menu.exec(pos);
+    if (selectedAction == newTabAction) {
+        const ViewTab& tab = m_viewTab[index];
+        Q_ASSERT(tab.primaryView != 0);
+        const KUrl url = (tab.secondaryView != 0) && tab.secondaryView->isActive() ?
+                         tab.secondaryView->url() : tab.primaryView->url();
+        openNewTab(url);
+        m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
+    } else if (selectedAction == closeOtherTabsAction) {
+        const int count = m_tabBar->count();
+        for (int i = 0; i < index; ++i) {
+            closeTab(0);
+        }
+        for (int i = index + 1; i < count; ++i) {
+            closeTab(1);
+        }
+    } else if (selectedAction == closeTabAction) {
+        closeTab(index);
+    }
+}
+
 void DolphinMainWindow::init()
 {
     DolphinSettings& settings = DolphinSettings::instance();
@@ -774,6 +806,10 @@ void DolphinMainWindow::init()
             this, SLOT(setActiveTab(int)));
     connect(m_tabBar, SIGNAL(closeRequest(int)),
             this, SLOT(closeTab(int)));
+    connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
+            this, SLOT(openTabContextMenu(int, const QPoint&)));
+    connect(m_tabBar, SIGNAL(newTabRequest()),
+            this, SLOT(openNewTab()));
     m_tabBar->blockSignals(true);  // signals get unblocked after at least 2 tabs are open
 
     QWidget* centralWidget = new QWidget(this);
@@ -839,10 +875,9 @@ void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContain
     updateGoActions();
 
     const KUrl& url = m_activeViewContainer->url();
-    const QString caption = url.fileName();
-    setCaption(caption);
+    setCaption(url.fileName());
     if (m_viewTab.count() > 1) {
-        m_tabBar->setTabText(m_tabIndex, caption);
+        m_tabBar->setTabText(m_tabIndex, tabName(url));
     }
 
     emit urlChanged(url);
@@ -870,6 +905,11 @@ void DolphinMainWindow::setupActions()
     newTab->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_N);
     connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
 
+    QAction* closeTab = new QAction(KIcon("tab-close"), i18nc("@action:inmenu File", "Close Tab"), this);
+    closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
+    connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
+    actionCollection()->addAction("close_tab", closeTab);
+
     KAction* properties = actionCollection()->addAction("properties");
     properties->setText(i18nc("@action:inmenu File", "Properties"));
     properties->setShortcut(Qt::ALT | Qt::Key_Return);
@@ -1065,26 +1105,11 @@ void DolphinMainWindow::updateEditActions()
     } else {
         stateChanged("has_selection");
 
-        QAction* renameAction = actionCollection()->action("rename");
-        if (renameAction != 0) {
-            renameAction->setEnabled(true);
-        }
-
-        bool enableMoveToTrash = true;
-
-        KFileItemList::const_iterator it = list.begin();
-        const KFileItemList::const_iterator end = list.end();
-        while (it != end) {
-            const KUrl& url = (*it).url();
-            // only enable the 'Move to Trash' action for local files
-            if (!url.isLocalFile()) {
-                enableMoveToTrash = false;
-            }
-            ++it;
-        }
-
-        QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
-        moveToTrashAction->setEnabled(enableMoveToTrash);
+        FileItemCapabilities capabilities(list);
+        actionCollection()->action("rename")->setEnabled(capabilities.supportsMoving());
+        const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
+        actionCollection()->action("move_to_trash")->setEnabled(enableMoveToTrash);
+        actionCollection()->action("delete")->setEnabled(capabilities.supportsDeleting());
     }
     updatePasteAction();
 }
@@ -1158,11 +1183,14 @@ void DolphinMainWindow::updateSplitAction()
     }
 }
 
-DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
-    KonqFileUndoManager::UiInterface(mainWin),
-    m_mainWin(mainWin)
+QString DolphinMainWindow::tabName(const KUrl& url) const
+{
+    return url.equals(KUrl("file:///")) ? "/" : url.fileName();
+}
+
+DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
+    KonqFileUndoManager::UiInterface()
 {
-    Q_ASSERT(m_mainWin != 0);
 }
 
 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
@@ -1171,8 +1199,13 @@ DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
 
 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
 {
-    DolphinStatusBar* statusBar = m_mainWin->activeViewContainer()->statusBar();
-    statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
+    DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
+    if (mainWin) {
+        DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
+        statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
+    } else {
+        KonqFileUndoManager::UiInterface::jobError(job);
+    }
 }
 
 #include "dolphinmainwindow.moc"