]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[dolphin.git] / src / dolphinmainwindow.cpp
index 3eb9b760841009a43eea08ebd0324a4b8858ac86..0804f4be9330033130a60f900fc2628fbf550070 100644 (file)
 #include "panels/information/informationpanel.h"
 #include "settings/dolphinsettingsdialog.h"
 #include "statusbar/dolphinstatusbar.h"
+#include "views/dolphinview.h"
 #include "views/dolphinviewactionhandler.h"
 #include "views/dolphinremoteencoding.h"
 #include "views/draganddrophelper.h"
 #include "views/viewproperties.h"
+#include "views/dolphinnewfilemenuobserver.h"
 
 #ifndef Q_OS_WIN
 #include "panels/terminal/terminalpanel.h"
@@ -51,7 +53,6 @@
 #include <kdeversion.h>
 #include <kdualaction.h>
 #include <KFileDialog>
-#include <KFilePlacesModel>
 #include <KGlobal>
 #include <KLineEdit>
 #include <KToolBar>
@@ -80,8 +81,6 @@
 #include <KUrlComboBox>
 #include <KToolInvocation>
 
-#include "views/dolphinplacesmodel.h"
-
 #include <QDesktopWidget>
 #include <QDBusMessage>
 #include <QKeyEvent>
@@ -123,21 +122,15 @@ DolphinMainWindow::DolphinMainWindow() :
     m_updateToolBarTimer(0),
     m_lastHandleUrlStatJob(0)
 {
-    DolphinPlacesModel::setModel(new KFilePlacesModel(this));
-    connect(DolphinPlacesModel::instance(), SIGNAL(errorMessage(QString)),
-            this, SLOT(showErrorMessage(QString)));
-
-    // Workaround for a X11-issue in combination with KModifierInfo
-    // (see DolphinContextMenu::initializeModifierKeyInfo() for
-    // more information):
-    DolphinContextMenu::initializeModifierKeyInfo();
-
     setObjectName("Dolphin#");
 
     m_viewTab.append(ViewTab());
     ViewTab& viewTab = m_viewTab[m_tabIndex];
     viewTab.wasActive = true; // The first opened tab is automatically active
 
+    connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(errorMessage(QString)),
+            this, SLOT(showErrorMessage(QString)));
+
     KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
     undoManager->setUiInterface(new UndoUiInterface());
 
@@ -255,8 +248,20 @@ void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
         return;
     }
 
-    if (dirs.count() == 1) {
-        m_activeViewContainer->setUrl(dirs.first());
+    // dirs could contain URLs that actually point to archives or other files.
+    // Replace them by URLs we can open where possible and filter the rest out.
+    QList<KUrl> urlsToOpen;
+    foreach (const KUrl& rawUrl, dirs) {
+        const KFileItem& item = KFileItem(KFileItem::Unknown, KFileItem::Unknown, rawUrl);
+        item.determineMimeType();
+        const KUrl& url = DolphinView::openItemAsFolderUrl(item);
+        if (!url.isEmpty()) {
+            urlsToOpen.append(url);
+        }
+    }
+
+    if (urlsToOpen.count() == 1) {
+        m_activeViewContainer->setUrl(urlsToOpen.first());
         return;
     }
 
@@ -266,12 +271,12 @@ void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
 
     // Open each directory inside a new tab. If the "split view" option has been enabled,
     // always show two directories within one tab.
-    QList<KUrl>::const_iterator it = dirs.begin();
-    while (it != dirs.end()) {
+    QList<KUrl>::const_iterator it = urlsToOpen.constBegin();
+    while (it != urlsToOpen.constEnd()) {
         openNewTab(*it);
         ++it;
 
-        if (hasSplitView && (it != dirs.end())) {
+        if (hasSplitView && (it != urlsToOpen.constEnd())) {
             const int tabIndex = m_viewTab.count() - 1;
             m_viewTab[tabIndex].secondaryView->setUrl(*it);
             ++it;
@@ -376,6 +381,13 @@ void DolphinMainWindow::changeUrl(const KUrl& url)
     }
 }
 
+void DolphinMainWindow::slotTerminalDirectoryChanged(const KUrl& url)
+{
+    m_activeViewContainer->setAutoGrabFocus(false);
+    changeUrl(url);
+    m_activeViewContainer->setAutoGrabFocus(true);
+}
+
 void DolphinMainWindow::slotEditableStateChanged(bool editable)
 {
     KToggleAction* editableLocationAction =
@@ -434,7 +446,7 @@ void DolphinMainWindow::updateFilterBarAction(bool show)
 
 void DolphinMainWindow::openNewMainWindow()
 {
-    KRun::run("dolphin", KUrl::List(), this);
+    KRun::run("dolphin %u", KUrl::List(), this);
 }
 
 void DolphinMainWindow::openNewTab()
@@ -525,11 +537,16 @@ void DolphinMainWindow::activatePrevTab()
 
 void DolphinMainWindow::openInNewTab()
 {
-    const KFileItemList list = m_activeViewContainer->view()->selectedItems();
+    const KFileItemList& list = m_activeViewContainer->view()->selectedItems();
     if (list.isEmpty()) {
         openNewTab(m_activeViewContainer->url());
-    } else if ((list.count() == 1) && list[0].isDir()) {
-        openNewTab(list[0].url());
+    } else {
+        foreach (const KFileItem& item, list) {
+            const KUrl& url = DolphinView::openItemAsFolderUrl(item);
+            if (!url.isEmpty()) {
+                openNewTab(url);
+            }
+        }
     }
 }
 
@@ -540,12 +557,13 @@ void DolphinMainWindow::openInNewWindow()
     const KFileItemList list = m_activeViewContainer->view()->selectedItems();
     if (list.isEmpty()) {
         newWindowUrl = m_activeViewContainer->url();
-    } else if ((list.count() == 1) && list[0].isDir()) {
-        newWindowUrl = list[0].url();
+    } else if (list.count() == 1) {
+        const KFileItem& item = list.first();
+        newWindowUrl = DolphinView::openItemAsFolderUrl(item);
     }
 
     if (!newWindowUrl.isEmpty()) {
-        KRun::run("dolphin", KUrl::List() << newWindowUrl, this);
+        KRun::run("dolphin %u", KUrl::List() << newWindowUrl, this);
     }
 }
 
@@ -617,7 +635,7 @@ void DolphinMainWindow::closeEvent(QCloseEvent* event)
                 break;
             case KDialog::No:
                 // Close only the current tab
-              closeTab();
+                closeTab();
             default:
                 event->ignore();
                 return;
@@ -683,7 +701,7 @@ void DolphinMainWindow::readProperties(const KConfigGroup& group)
 
         // openNewTab() needs to be called only tabCount - 1 times
         if (i != tabCount - 1) {
-             openNewTab();
+            openNewTab();
         }
     }
 
@@ -985,50 +1003,23 @@ void DolphinMainWindow::goHome(Qt::MouseButtons buttons)
 
 void DolphinMainWindow::compareFiles()
 {
-    // The method is only invoked if exactly 2 files have
-    // been selected. The selected files may be:
-    // - both in the primary view
-    // - both in the secondary view
-    // - one in the primary view and the other in the secondary
-    //   view
-    Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
-
-    KUrl urlA;
-    KUrl urlB;
-
-    KFileItemList items = m_viewTab[m_tabIndex].primaryView->view()->selectedItems();
-
-    switch (items.count()) {
-    case 0: {
-        Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
-        items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
-        Q_ASSERT(items.count() == 2);
-        urlA = items[0].url();
-        urlB = items[1].url();
-        break;
-    }
+    const DolphinViewContainer* primaryViewContainer = m_viewTab[m_tabIndex].primaryView;
+    Q_ASSERT(primaryViewContainer);
+    KFileItemList items = primaryViewContainer->view()->selectedItems();
 
-    case 1: {
-        urlA = items[0].url();
-        Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
-        items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
-        Q_ASSERT(items.count() == 1);
-        urlB = items[0].url();
-        break;
+    const DolphinViewContainer* secondaryViewContainer = m_viewTab[m_tabIndex].secondaryView;
+    if (secondaryViewContainer) {
+        items.append(secondaryViewContainer->view()->selectedItems());
     }
 
-    case 2: {
-        urlA = items[0].url();
-        urlB = items[1].url();
-        break;
+    if (items.count() != 2) {
+        // The action is disabled in this case, but it could have been triggered
+        // via D-Bus, see https://bugs.kde.org/show_bug.cgi?id=325517
+        return;
     }
 
-    default: {
-        // may not happen: compareFiles may only get invoked if 2
-        // files are selected
-        Q_ASSERT(false);
-    }
-    }
+    KUrl urlA = items.at(0).url();
+    KUrl urlB = items.at(1).url();
 
     QString command("kompare -c \"");
     command.append(urlA.pathOrUrl());
@@ -1276,7 +1267,8 @@ void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event)
         const ViewTab& viewTab = m_viewTab[tab];
         const DolphinView* view = viewTab.isPrimaryViewActive ? viewTab.primaryView->view()
                                                               : viewTab.secondaryView->view();
-        const QString error = DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event);
+        QString error;
+        DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error);
         if (!error.isEmpty()) {
             activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
         }
@@ -1299,7 +1291,7 @@ void DolphinMainWindow::openContextMenu(const QPoint& pos,
 
     switch (command) {
     case DolphinContextMenu::OpenParentFolderInNewWindow: {
-        KRun::run("dolphin", KUrl::List() << item.url().upUrl(), this);
+        KRun::run("dolphin %u", KUrl::List() << item.url().upUrl(), this);
         break;
     }
 
@@ -1424,6 +1416,24 @@ void DolphinMainWindow::slotControlButtonDeleted()
     m_updateToolBarTimer->start();
 }
 
+void DolphinMainWindow::slotPanelErrorMessage(const QString& error)
+{
+    activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
+}
+
+void DolphinMainWindow::slotPlaceActivated(const KUrl& url)
+{
+    DolphinViewContainer* view = activeViewContainer();
+
+    if (view->url() == url) {
+        // We can end up here if the user clicked a device in the Places Panel
+        // which had been unmounted earlier, see https://bugs.kde.org/show_bug.cgi?id=161385.
+        reloadView();
+    } else {
+        changeUrl(url);
+    }
+}
+
 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
 {
     Q_ASSERT(viewContainer);
@@ -1475,10 +1485,11 @@ DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QW
 void DolphinMainWindow::setupActions()
 {
     // setup 'File' menu
-    m_newFileMenu = new DolphinNewFileMenu(this);
+    m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
     KMenu* menu = m_newFileMenu->menu();
     menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
     menu->setIcon(KIcon("document-new"));
+    m_newFileMenu->setDelayed(false);
     connect(menu, SIGNAL(aboutToShow()),
             this, SLOT(updateNewMenu()));
 
@@ -1555,12 +1566,12 @@ void DolphinMainWindow::setupActions()
 
     KToggleAction* editableLocation = actionCollection()->add<KToggleAction>("editable_location");
     editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
-    editableLocation->setShortcut(Qt::CTRL | Qt::Key_L);
+    editableLocation->setShortcut(Qt::Key_F6);
     connect(editableLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
 
     KAction* replaceLocation = actionCollection()->addAction("replace_location");
     replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
-    replaceLocation->setShortcut(Qt::Key_F6);
+    replaceLocation->setShortcut(Qt::CTRL | Qt::Key_L);
     connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
 
     // setup 'Go' menu
@@ -1572,6 +1583,7 @@ void DolphinMainWindow::setupActions()
 
     m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
     m_recentTabsMenu->setIcon(KIcon("edit-undo"));
+    m_recentTabsMenu->setDelayed(false);
     actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
     connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction*)),
             this, SLOT(restoreClosedTab(QAction*)));
@@ -1642,6 +1654,11 @@ void DolphinMainWindow::setupActions()
     openInNewTab->setIcon(KIcon("tab-new"));
     connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
 
+    KAction* openInNewTabs = actionCollection()->addAction("open_in_new_tabs");
+    openInNewTabs->setText(i18nc("@action:inmenu", "Open in New Tabs"));
+    openInNewTabs->setIcon(KIcon("tab-new"));
+    connect(openInNewTabs, SIGNAL(triggered()), this, SLOT(openInNewTab()));
+
     KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
     openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
     openInNewWindow->setIcon(KIcon("window-new"));
@@ -1699,7 +1716,9 @@ void DolphinMainWindow::setupDockWidgets()
     connect(foldersPanel, SIGNAL(folderActivated(KUrl)),
             this, SLOT(changeUrl(KUrl)));
     connect(foldersPanel, SIGNAL(folderMiddleClicked(KUrl)),
-            this, SLOT(openNewActivatedTab(KUrl)));
+            this, SLOT(openNewTab(KUrl)));
+    connect(foldersPanel, SIGNAL(errorMessage(QString)),
+            this, SLOT(slotPanelErrorMessage(QString)));
 
     // Setup "Terminal"
 #ifndef Q_OS_WIN
@@ -1712,6 +1731,7 @@ void DolphinMainWindow::setupDockWidgets()
     terminalDock->setWidget(terminalPanel);
 
     connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
+    connect(terminalPanel, SIGNAL(changeUrl(KUrl)), this, SLOT(slotTerminalDirectoryChanged(KUrl)));
     connect(terminalDock, SIGNAL(visibilityChanged(bool)),
             terminalPanel, SLOT(dockVisibilityChanged()));
 
@@ -1738,14 +1758,7 @@ void DolphinMainWindow::setupDockWidgets()
     placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
 
     PlacesPanel* placesPanel = new PlacesPanel(placesDock);
-    QAction* separator = new QAction(placesPanel);
-    separator->setSeparator(true);
-    QList<QAction*> placesActions;
-    placesActions.append(separator);
-    placesActions.append(lockLayoutAction);
-    //placesPanel->addActions(placesActions);
-    //placesPanel->setModel(DolphinPlacesModel::instance());
-    //placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    placesPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
     placesDock->setWidget(placesPanel);
 
     QAction* placesAction = placesDock->toggleViewAction();
@@ -1753,13 +1766,17 @@ void DolphinMainWindow::setupDockWidgets()
 
     addDockWidget(Qt::LeftDockWidgetArea, placesDock);
     connect(placesPanel, SIGNAL(placeActivated(KUrl)),
-            this, SLOT(changeUrl(KUrl)));
+            this, SLOT(slotPlaceActivated(KUrl)));
     connect(placesPanel, SIGNAL(placeMiddleClicked(KUrl)),
-            this, SLOT(openNewActivatedTab(KUrl)));
+            this, SLOT(openNewTab(KUrl)));
+    connect(placesPanel, SIGNAL(errorMessage(QString)),
+            this, SLOT(slotPanelErrorMessage(QString)));
     connect(this, SIGNAL(urlChanged(KUrl)),
             placesPanel, SLOT(setUrl(KUrl)));
     connect(placesDock, SIGNAL(visibilityChanged(bool)),
             this, SLOT(slotPlacesPanelVisibilityChanged(bool)));
+    connect(this, SIGNAL(settingsChanged()),
+           placesPanel, SLOT(readSettings()));
 
     // Add actions into the "Panels" menu
     KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
@@ -1950,6 +1967,8 @@ void DolphinMainWindow::refreshViews()
             toggleSplitView();
         }
     }
+
+    emit settingsChanged();
 }
 
 void DolphinMainWindow::clearStatusBar()
@@ -2054,7 +2073,13 @@ void DolphinMainWindow::createSecondaryView(int tabIndex)
     const int newWidth = (viewTab.primaryView->width() - splitter->handleWidth()) / 2;
 
     const DolphinView* view = viewTab.primaryView->view();
-    viewTab.secondaryView = createViewContainer(view->url(), 0);
+    // The final parent of the new view container will be set by adding it
+    // to the splitter. However, we must make sure that the DolphinMainWindow
+    // is a parent of the view container already when it is constructed
+    // because this enables the container's KFileItemModel to assign its
+    // dir lister to the right main window. The dir lister can then cache
+    // authentication data.
+    viewTab.secondaryView = createViewContainer(view->url(), this);
     splitter->addWidget(viewTab.secondaryView);
     splitter->setSizes(QList<int>() << newWidth << newWidth);