]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
BUG: 175658
[dolphin.git] / src / dolphinmainwindow.cpp
index 78e48991557aeff3c9581abc421b59ac570f6f26..c2939e59ae074de1587132dc6c97e8a4ffd21900 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "dolphinmainwindow.h"
 #include "dolphinviewactionhandler.h"
+#include "dolphinremoteencoding.h"
 
 #include <config-nepomuk.h>
 
@@ -78,6 +79,7 @@
 #include <kurlnavigator.h>
 #include <kurl.h>
 #include <kurlcombobox.h>
+#include <ktoolinvocation.h>
 
 #include <QDBusMessage>
 #include <QKeyEvent>
 #include <QLineEdit>
 #include <QSplitter>
 #include <QDockWidget>
+#include <kacceleratormanager.h>
+
+/*
+ * Remembers the tab configuration if a tab has been closed.
+ * Each closed tab can be restored by the menu
+ * "Go -> Recently Closed Tabs".
+ */
+struct ClosedTab
+{
+    KUrl primaryUrl;
+    KUrl secondaryUrl;
+    bool isSplit;
+};
+Q_DECLARE_METATYPE(ClosedTab)
 
 DolphinMainWindow::DolphinMainWindow(int id) :
     KXmlGuiWindow(0),
@@ -98,6 +114,7 @@ DolphinMainWindow::DolphinMainWindow(int id) :
     m_tabIndex(0),
     m_viewTab(),
     m_actionHandler(0),
+    m_remoteEncoding(0),
     m_settingsDialog(0)
 {
     setObjectName("Dolphin#");
@@ -223,6 +240,8 @@ void DolphinMainWindow::changeUrl(const KUrl& url)
         if (m_viewTab.count() > 1) {
             m_tabBar->setTabText(m_tabIndex, tabName(url));
         }
+        const QString iconName = KMimeType::iconNameForUrl(url);
+        m_tabBar->setTabIcon(m_tabIndex, KIcon(iconName));
         emit urlChanged(url);
     }
 }
@@ -256,11 +275,6 @@ void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
         compareFilesAction->setEnabled(false);
     }
 
-#if defined(QUICK_VIEW)
-    const bool activeViewHasSelection = (activeViewContainer()->view()->selectedItemsCount() > 0);
-    actionCollection()->action("quick_view")->setEnabled(activeViewHasSelection);
-#endif
-
     m_activeViewContainer->updateStatusBar();
 
     emit selectionChanged(selection);
@@ -322,14 +336,15 @@ void DolphinMainWindow::openNewTab()
 
 void DolphinMainWindow::openNewTab(const KUrl& url)
 {
+    const KIcon icon = KIcon(KMimeType::iconNameForUrl(m_activeViewContainer->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"), tabName(m_activeViewContainer->url()));
+        m_tabBar->addTab(icon, tabName(m_activeViewContainer->url()));
         m_tabBar->blockSignals(false);
     }
 
-    m_tabBar->addTab(KIcon("folder"), tabName(url));
+    m_tabBar->addTab(icon, tabName(url));
 
     ViewTab viewTab;
     viewTab.splitter = new QSplitter(this);
@@ -354,7 +369,7 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
 
 void DolphinMainWindow::activateNextTab()
 {
-    if (m_viewTab.count() == 1 || m_tabBar->count() < 2) {
+    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
         return;
     }
 
@@ -364,7 +379,7 @@ void DolphinMainWindow::activateNextTab()
 
 void DolphinMainWindow::activatePrevTab()
 {
-    if (m_viewTab.count() == 1 || m_tabBar->count() < 2) {
+    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
         return;
     }
 
@@ -412,6 +427,50 @@ void DolphinMainWindow::closeEvent(QCloseEvent* event)
 {
     DolphinSettings& settings = DolphinSettings::instance();
     GeneralSettings* generalSettings = settings.generalSettings();
+
+    if ((m_viewTab.count() > 1) && generalSettings->confirmClosingMultipleTabs()) {
+        // Ask the user if he really wants to quit and close all tabs.
+        // Open a confirmation dialog with 3 buttons:
+        // KDialog::Yes    -> Quit
+        // KDialog::No     -> Close only the current tab
+        // KDialog::Cancel -> do nothing
+        KDialog *dialog = new KDialog(this, Qt::Dialog);
+        dialog->setCaption(i18nc("@title:window", "Confirmation"));
+        dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Cancel);
+        dialog->setModal(true);
+        dialog->showButtonSeparator(true);
+        dialog->setButtonGuiItem(KDialog::Yes, KStandardGuiItem::quit());
+        dialog->setButtonGuiItem(KDialog::No, KGuiItem(i18n("C&lose Current Tab"), KIcon("tab-close")));
+        dialog->setButtonGuiItem(KDialog::Cancel, KStandardGuiItem::cancel());
+        dialog->setDefaultButton(KDialog::Yes);
+
+        bool doNotAskAgainCheckboxResult = false;
+
+        const int result = KMessageBox::createKMessageBox(dialog,
+            QMessageBox::Warning,
+            i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
+            QStringList(),
+            i18n("Do not ask again"),
+            &doNotAskAgainCheckboxResult,
+            KMessageBox::Notify);
+
+        if (doNotAskAgainCheckboxResult) {
+            generalSettings->setConfirmClosingMultipleTabs(false);
+        }
+
+        switch (result) {
+            case KDialog::Yes:
+                // Quit
+                break;
+            case KDialog::No:
+                // Close only the current tab
+             closeTab();
+            default:
+                event->ignore();
+                return;
+        }
+    }
+
     generalSettings->setFirstRun(false);
 
     settings.save();
@@ -421,46 +480,61 @@ void DolphinMainWindow::closeEvent(QCloseEvent* event)
 
 void DolphinMainWindow::saveProperties(KConfigGroup& group)
 {
-    // TODO: remember tabs
-    DolphinViewContainer* cont = m_viewTab[m_tabIndex].primaryView;
-    group.writeEntry("Primary Url", cont->url().url());
-    group.writeEntry("Primary Editable Url", cont->isUrlEditable());
+    const int tabCount = m_viewTab.count();
+    group.writeEntry("Tab Count", tabCount);
+    group.writeEntry("Active Tab Index", m_tabBar->currentIndex());
 
-    cont = m_viewTab[m_tabIndex].secondaryView;
-    if (cont != 0) {
-        group.writeEntry("Secondary Url", cont->url().url());
-        group.writeEntry("Secondary Editable Url", cont->isUrlEditable());
+    for (int i = 0; i < tabCount; ++i) {
+        const DolphinViewContainer* cont = m_viewTab[i].primaryView;
+        group.writeEntry(tabProperty("Primary URL", i), cont->url().url());
+        group.writeEntry(tabProperty("Primary Editable", i), cont->isUrlEditable());
+
+        cont = m_viewTab[i].secondaryView;
+        if (cont != 0) {
+            group.writeEntry(tabProperty("Secondary URL", i), cont->url().url());
+            group.writeEntry(tabProperty("Secondary Editable", i), cont->isUrlEditable());
+        }
     }
 }
 
 void DolphinMainWindow::readProperties(const KConfigGroup& group)
 {
-    // TODO: read tabs
-    DolphinViewContainer* cont = m_viewTab[m_tabIndex].primaryView;
+    const int tabCount = group.readEntry("Tab Count", 1);
+    for (int i = 0; i < tabCount; ++i) {
+        DolphinViewContainer* cont = m_viewTab[i].primaryView;
 
-    cont->setUrl(group.readEntry("Primary Url"));
-    bool editable = group.readEntry("Primary Editable Url", false);
-    cont->urlNavigator()->setUrlEditable(editable);
+        cont->setUrl(group.readEntry(tabProperty("Primary URL", i)));
+        const bool editable = group.readEntry(tabProperty("Primary Editable", i), false);
+        cont->urlNavigator()->setUrlEditable(editable);
 
-    cont = m_viewTab[m_tabIndex].secondaryView;
-    const QString secondaryUrl = group.readEntry("Secondary Url");
-    if (!secondaryUrl.isEmpty()) {
-        if (cont == 0) {
-            // a secondary view should be shown, but no one is available
-            // currently -> create a new view
+        cont = m_viewTab[i].secondaryView;
+        const QString secondaryUrl = group.readEntry(tabProperty("Secondary URL", i));
+        if (!secondaryUrl.isEmpty()) {
+            if (cont == 0) {
+                // a secondary view should be shown, but no one is available
+                // currently -> create a new view
+                toggleSplitView();
+                cont = m_viewTab[i].secondaryView;
+                Q_ASSERT(cont != 0);
+            }
+
+            cont->setUrl(secondaryUrl);
+            const bool editable = group.readEntry(tabProperty("Secondary Editable", i), false);
+            cont->urlNavigator()->setUrlEditable(editable);
+        } else if (cont != 0) {
+            // no secondary view should be shown, but the default setting shows
+            // one already -> close the view
             toggleSplitView();
-            cont = m_viewTab[m_tabIndex].secondaryView;
-            Q_ASSERT(cont != 0);
         }
 
-        cont->setUrl(secondaryUrl);
-        bool editable = group.readEntry("Secondary Editable Url", false);
-        cont->urlNavigator()->setUrlEditable(editable);
-    } else if (cont != 0) {
-        // no secondary view should be shown, but the default setting shows
-        // one already -> close the view
-        toggleSplitView();
+        // openNewTab() needs to be called only tabCount - 1 times
+        if (i != tabCount - 1) {
+             openNewTab();
+        }
     }
+
+    const int index = group.readEntry("Active Tab Index", 0);
+    m_tabBar->setCurrentIndex(index);
 }
 
 void DolphinMainWindow::updateNewMenu()
@@ -492,21 +566,21 @@ void DolphinMainWindow::slotUndoAvailable(bool available)
 
 void DolphinMainWindow::restoreClosedTab(QAction* action)
 {
-    //The Clear Recently Closed Tabs List QAction, has it's data set to true, so we can detect it in here, as it's an exception.
-    if (action->data().toBool() == true) {
-        // Lets preserve the separator, and the clear action within this menu.
-        QList<QAction*> actionlist = m_recentTabsMenu->menu()->actions();
-        for (int i = 2; i < actionlist.size(); i++) {
-        m_recentTabsMenu->menu()->removeAction(actionlist.at(i));
+    if (action->data().toBool()) {
+        // clear all actions except the "Empty Recently Closed Tabs"
+        // action and the separator
+        QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
+        const int count = actions.size();
+        for (int i = 2; i < count; ++i) {
+            m_recentTabsMenu->menu()->removeAction(actions.at(i));
         }
     } else {
         const ClosedTab closedTab = action->data().value<ClosedTab>();
-
         openNewTab(closedTab.primaryUrl);
         m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
 
         if (closedTab.isSplit) {
-            //Create secondary view.
+            // create secondary view
             toggleSplitView();
             m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
         }
@@ -724,24 +798,17 @@ void DolphinMainWindow::compareFiles()
     KRun::runCommand(command, "Kompare", "kompare", this);
 }
 
-void DolphinMainWindow::quickView()
-{
-    const KUrl::List urls = activeViewContainer()->view()->selectedUrls();
-    Q_ASSERT(urls.count() > 0);
-
-    QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.plasma", "/Previewer", "", "openFile");
-    foreach (const KUrl& url, urls) {
-        msg.setArguments(QList<QVariant>() << url.prettyUrl());
-        QDBusConnection::sessionBus().send(msg);
-    }
-}
-
 void DolphinMainWindow::toggleShowMenuBar()
 {
     const bool visible = menuBar()->isVisible();
     menuBar()->setVisible(!visible);
 }
 
+void DolphinMainWindow::openTerminal()
+{
+    KToolInvocation::invokeTerminal(QString(), m_activeViewContainer->url().path());
+}
+
 void DolphinMainWindow::editSettings()
 {
     if (m_settingsDialog == 0) {
@@ -798,7 +865,7 @@ void DolphinMainWindow::closeTab(int index)
     Q_ASSERT(index >= 0);
     Q_ASSERT(index < m_viewTab.count());
     if (m_viewTab.count() == 1) {
-          // the last tab may never get closed
+        // the last tab may never get closed
         return;
     }
 
@@ -808,7 +875,8 @@ void DolphinMainWindow::closeTab(int index)
         m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
     }
     rememberClosedTab(index);
-    //Delete this tab.
+
+    // delete tab
     m_viewTab[index].primaryView->deleteLater();
     if (m_viewTab[index].secondaryView != 0) {
         m_viewTab[index].secondaryView->deleteLater();
@@ -845,7 +913,6 @@ void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
 
     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];
@@ -887,7 +954,6 @@ void DolphinMainWindow::searchItems(const KUrl& url)
     m_activeViewContainer->setUrl(url);
 }
 
-
 void DolphinMainWindow::init()
 {
     DolphinSettings& settings = DolphinSettings::instance();
@@ -921,13 +987,17 @@ void DolphinMainWindow::init()
     view->reload();
     m_activeViewContainer->show();
     m_actionHandler->setCurrentView(view);
+    
+    m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
+    connect(this, SIGNAL(urlChanged(const KUrl&)), 
+            m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
 
     m_tabBar = new KTabBar(this);
     m_tabBar->setMovable(true);
     m_tabBar->setTabsClosable(true);
     connect(m_tabBar, SIGNAL(currentChanged(int)),
             this, SLOT(setActiveTab(int)));
-    connect(m_tabBar, SIGNAL(closeRequest(int)),
+    connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
             this, SLOT(closeTab(int)));
     connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
             this, SLOT(openTabContextMenu(int, const QPoint&)));
@@ -1010,8 +1080,9 @@ void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContain
 
     const KUrl& url = m_activeViewContainer->url();
     setCaption(url.fileName());
-    if (m_viewTab.count() > 1) {
+    if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
         m_tabBar->setTabText(m_tabIndex, tabName(url));
+        m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
     }
 
     emit urlChanged(url);
@@ -1107,13 +1178,13 @@ void DolphinMainWindow::setupActions()
     backShortcut.setAlternate(Qt::Key_Backspace);
     backAction->setShortcut(backShortcut);
 
-    m_recentTabsMenu = new KActionMenu(i18n("&Recently Closed Tabs"), this);
+    m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
     m_recentTabsMenu->setIcon(KIcon("edit-undo"));
     actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
     connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
             this, SLOT(restoreClosedTab(QAction *)));
 
-    QAction* action = new QAction("&Empty Recently Closed Tabs", m_recentTabsMenu);
+    QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
     action->setIcon(KIcon("edit-clear-list"));
     action->setData(QVariant::fromValue(true));
     m_recentTabsMenu->addAction(action);
@@ -1141,15 +1212,11 @@ void DolphinMainWindow::setupActions()
     compareFiles->setEnabled(false);
     connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
 
-    // disabled Quick View
-#if defined(QUICK_VIEW)
-    KAction* quickView = actionCollection()->addAction("quick_view");
-    quickView->setText(i18nc("@action:inmenu Tools", "Quick View"));
-    quickView->setIcon(KIcon("view-preview"));
-    quickView->setShortcut(Qt::CTRL + Qt::Key_Return);
-    quickView->setEnabled(false);
-    connect(quickView, SIGNAL(triggered()), this, SLOT(quickView()));
-#endif
+    KAction* openTerminal = actionCollection()->addAction("open_terminal");
+    openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
+    openTerminal->setIcon(KIcon("terminal"));
+    openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
+    connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
 
     // setup 'Settings' menu
     m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
@@ -1340,7 +1407,10 @@ void DolphinMainWindow::rememberClosedTab(int index)
     const QString primaryPath = m_viewTab[index].primaryView->url().path();
     const QString iconName = KMimeType::iconNameForUrl(primaryPath);
 
-    QAction* action = new QAction(primaryPath, tabsMenu);
+    const QFontMetrics fm = fontMetrics();
+    const QString actionText = fm.elidedText(primaryPath, Qt::ElideMiddle, fm.maxWidth() * 20);
+
+    QAction* action = new QAction(actionText, tabsMenu);
 
     ClosedTab closedTab;
     closedTab.primaryUrl = m_viewTab[index].primaryView->url();
@@ -1355,13 +1425,20 @@ void DolphinMainWindow::rememberClosedTab(int index)
     action->setData(QVariant::fromValue(closedTab));
     action->setIcon(KIcon(iconName));
 
-    //Add our action at the first element, but only do that if it isn't empty, else just append
-    if (tabsMenu->actions().isEmpty()) {
+    // add the closed tab menu entry after the separator and
+    // "Empty Recently Closed Tabs" entry
+    if (tabsMenu->actions().size() == 2) {
         tabsMenu->addAction(action);
     } else {
-        tabsMenu->insertAction(tabsMenu->actions().first() + 2, action);
+        tabsMenu->insertAction(tabsMenu->actions().at(2), action);
+    }
+
+    // assure that only up to 8 closed tabs are shown in the menu
+    if (tabsMenu->actions().size() > 8) {
+        tabsMenu->removeAction(tabsMenu->actions().last());
     }
     actionCollection()->action("closed_tabs")->setEnabled(true);
+    KAcceleratorManager::manage(tabsMenu);
 }
 
 void DolphinMainWindow::clearStatusBar()
@@ -1456,6 +1533,11 @@ void DolphinMainWindow::createSecondaryView(int tabIndex)
     m_viewTab[tabIndex].secondaryView->show();
 }
 
+QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
+{
+    return "Tab " + QString::number(tabIndex) + ' ' + property;
+}
+
 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
     KIO::FileUndoManager::UiInterface()
 {