]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
Port Dolphin to Baloo
[dolphin.git] / src / views / dolphinview.cpp
index 5b3d074c98a400de31962f0483c44e9d52190064..63b53f2e263b68324e0de4c73370bb5b682c8c59 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "dolphinview.h"
 
-#include <config-nepomuk.h>
+#include <config-baloo.h>
 
 #include <QAbstractItemView>
 #include <QApplication>
@@ -33,6 +33,8 @@
 #include <QTimer>
 #include <QScrollBar>
 
+#include <KDesktopFile>
+#include <KProtocolManager>
 #include <KActionCollection>
 #include <KColorScheme>
 #include <KDirModel>
@@ -72,8 +74,8 @@
 #include "views/tooltips/tooltipmanager.h"
 #include "zoomlevelinfo.h"
 
-#ifdef HAVE_NEPOMUK
-    #include <Nepomuk2/ResourceManager>
+#ifdef HAVE_BALOO
+    #include <baloo/indexerconfig.h>
 #endif
 
 namespace {
@@ -101,6 +103,8 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     m_scrollToCurrentItem(false),
     m_restoredContentsPosition(),
     m_selectedUrls(),
+    m_clearSelectionBeforeSelectingNewItems(false),
+    m_markFirstNewlySelectedItemAsCurrent(false),
     m_versionControlObserver(0)
 {
     m_topLayout = new QVBoxLayout(this);
@@ -141,7 +145,7 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
 
     controller->setSelectionBehavior(KItemListController::MultiSelection);
     connect(controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
-    connect(controller, SIGNAL(itemsActivated(QSet<int>)), this, SLOT(slotItemsActivated(QSet<int>)));
+    connect(controller, SIGNAL(itemsActivated(KItemSet)), this, SLOT(slotItemsActivated(KItemSet)));
     connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
     connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
     connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
@@ -150,6 +154,7 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
     connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
     connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
     connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
+    connect(controller, SIGNAL(escapePressed()), this, SLOT(stopLoading()));
     connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
 
     connect(m_model, SIGNAL(directoryLoadingStarted()),       this, SLOT(slotDirectoryLoadingStarted()));
@@ -173,14 +178,14 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
             this, SLOT(slotSortRoleChangedByHeader(QByteArray,QByteArray)));
     connect(m_view, SIGNAL(visibleRolesChanged(QList<QByteArray>,QList<QByteArray>)),
             this, SLOT(slotVisibleRolesChangedByHeader(QList<QByteArray>,QList<QByteArray>)));
-    connect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
-            this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
+    connect(m_view, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)),
+            this, SLOT(slotRoleEditingCanceled()));
     connect(m_view->header(), SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
             this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
 
     KItemListSelectionManager* selectionManager = controller->selectionManager();
-    connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
-            this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
+    connect(selectionManager, SIGNAL(selectionChanged(KItemSet,KItemSet)),
+            this, SLOT(slotSelectionChanged(KItemSet,KItemSet)));
 
     m_toolTipManager = new ToolTipManager(this);
 
@@ -244,9 +249,12 @@ void DolphinView::setMode(Mode mode)
     if (mode != m_mode) {
         ViewProperties props(viewPropertiesUrl());
         props.setViewMode(mode);
-        props.save();
 
-        applyViewProperties();
+        // We pass the new ViewProperties to applyViewProperties, rather than
+        // storing them on disk and letting applyViewProperties() read them
+        // from there, to prevent that changing the view mode fails if the
+        // .directory file is not writable (see bug 318534).
+        applyViewProperties(props);
     }
 }
 
@@ -342,12 +350,9 @@ int DolphinView::itemsCount() const
 KFileItemList DolphinView::selectedItems() const
 {
     const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
-    const QSet<int> selectedIndexes = selectionManager->selectedItems();
 
     KFileItemList selectedItems;
-    QSetIterator<int> it(selectedIndexes);
-    while (it.hasNext()) {
-        const int index = it.next();
+    foreach (int index, selectionManager->selectedItems()) {
         selectedItems.append(m_model->fileItem(index));
     }
     return selectedItems;
@@ -380,9 +385,9 @@ void DolphinView::selectItems(const QRegExp& pattern, bool enabled)
     for (int index = 0; index < m_model->count(); index++) {
         const KFileItem item = m_model->fileItem(index);
         if (pattern.exactMatch(item.text())) {
-            // An alternative approach would be to store the matching items in a QSet<int> and
+            // An alternative approach would be to store the matching items in a KItemSet and
             // select them in one go after the loop, but we'd need a new function
-            // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
+            // KItemListSelectionManager::setSelected(KItemSet, SelectionMode mode)
             // for that.
             selectionManager->setSelected(index, 1, mode);
         }
@@ -476,11 +481,6 @@ void DolphinView::reload()
     restoreState(restoreStream);
 }
 
-void DolphinView::stopLoading()
-{
-    m_model->cancelDirectoryLoading();
-}
-
 void DolphinView::readSettings()
 {
     const int oldZoomLevel = m_view->zoomLevel();
@@ -547,8 +547,8 @@ QString DolphinView::statusBarText() const
         }
 
         if (folderCount + fileCount == 1) {
-            // If only one item is selected, show the filename
-            filesText = i18nc("@info:status", "<filename>%1</filename> selected", list.first().text());
+            // If only one item is selected, show info about it
+            return list.first().getStatusBarInfo();
         } else {
             // At least 2 items are selected
             foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
@@ -599,11 +599,16 @@ void DolphinView::setUrl(const KUrl& url)
         return;
     }
 
+    clearSelection();
+
     emit urlAboutToBeChanged(url);
     m_url = url;
 
     hideToolTip();
 
+    disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
+               this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
+
     // It is important to clear the items from the model before
     // applying the view properties, otherwise expensive operations
     // might be done on the existing items although they get cleared
@@ -629,6 +634,7 @@ void DolphinView::invertSelection()
 
 void DolphinView::clearSelection()
 {
+    m_selectedUrls.clear();
     m_container->controller()->selectionManager()->clearSelection();
 }
 
@@ -642,6 +648,11 @@ void DolphinView::renameSelectedItems()
     if (items.count() == 1 && GeneralSettings::renameInline()) {
         const int index = m_model->index(items.first());
         m_view->editRole(index, "text");
+
+        hideToolTip();
+
+        connect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
+                this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
     } else {
         RenameDialog* dialog = new RenameDialog(this, items);
         dialog->setAttribute(Qt::WA_DeleteOnClose);
@@ -706,6 +717,11 @@ void DolphinView::pasteIntoFolder()
     }
 }
 
+void DolphinView::stopLoading()
+{
+    m_model->cancelDirectoryLoading();
+}
+
 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
 {
     switch (event->type()) {
@@ -784,45 +800,58 @@ void DolphinView::slotItemActivated(int index)
     }
 }
 
-void DolphinView::slotItemsActivated(const QSet<int>& indexes)
+void DolphinView::slotItemsActivated(const KItemSet& indexes)
 {
     Q_ASSERT(indexes.count() >= 2);
 
-    KFileItemList items;
-
-    QSetIterator<int> it(indexes);
-    while (it.hasNext()) {
-        const int index = it.next();
-        items.append(m_model->fileItem(index));
-    }
-
-    if (items.count() > 5) {
-        QString question = QString("Are you sure you want to open %1 items?").arg(items.count());
+    if (indexes.count() > 5) {
+        QString question = i18np("Are you sure you want to open 1 item?", "Are you sure you want to open %1 items?", indexes.count());
         const int answer = KMessageBox::warningYesNo(this, question);
         if (answer != KMessageBox::Yes) {
             return;
         }
     }
 
-    foreach (const KFileItem& item, items) {
-        if (item.isDir()) {
-            emit tabRequested(item.url());
+    KFileItemList items;
+    items.reserve(indexes.count());
+
+    foreach (int index, indexes) {
+        KFileItem item = m_model->fileItem(index);
+        const KUrl& url = openItemAsFolderUrl(item);
+
+        if (!url.isEmpty()) { // Open folders in new tabs
+            emit tabRequested(url);
         } else {
-            emit itemActivated(item);
+            items.append(item);
         }
     }
+
+    if (items.count() == 1) {
+        emit itemActivated(items.first());
+    } else if (items.count() > 1) {
+        emit itemsActivated(items);
+    }
 }
 
 void DolphinView::slotItemMiddleClicked(int index)
 {
-    const KFileItem item = m_model->fileItem(index);
-    if (item.isDir() || isTabsForFilesEnabled()) {
+    const KFileItem& item = m_model->fileItem(index);
+    const KUrl& url = openItemAsFolderUrl(item);
+    if (!url.isEmpty()) {
+        emit tabRequested(url);
+    } else if (isTabsForFilesEnabled()) {
         emit tabRequested(item.url());
     }
 }
 
 void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
 {
+    // Force emit of a selection changed signal before we request the
+    // context menu, to update the edit-actions first. (See Bug 294013)
+    if (m_selectionChangedTimer->isActive()) {
+        emitSelectionChangedSignal();
+    }
+
     const KFileItem item = m_model->fileItem(index);
     emit requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
 }
@@ -841,14 +870,10 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
     KItemListView* view = m_container->controller()->view();
     const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
 
-    bool nepomukRunning = false;
     bool indexingEnabled = false;
-#ifdef HAVE_NEPOMUK
-    nepomukRunning = (Nepomuk2::ResourceManager::instance()->initialized());
-    if (nepomukRunning) {
-        KConfig config("nepomukserverrc");
-        indexingEnabled = config.group("Service-nepomukfileindexer").readEntry("autostart", false);
-    }
+#ifdef HAVE_BALOO
+    Baloo::IndexerConfig config;
+    indexingEnabled = config.fileIndexingEnabled();
 #endif
 
     QString groupName;
@@ -879,8 +904,8 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
         action->setChecked(visibleRolesSet.contains(info.role));
         action->setData(info.role);
 
-        const bool enable = (!info.requiresNepomuk && !info.requiresIndexer) ||
-                            (info.requiresNepomuk && nepomukRunning) ||
+        const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
+                            (info.requiresBaloo) ||
                             (info.requiresIndexer && indexingEnabled);
         action->setEnabled(enable);
     }
@@ -1012,15 +1037,19 @@ void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* even
                          event->buttons(),
                          event->modifiers());
 
-    const QString error = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent);
+    QString error;
+    KonqOperations* op = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent, error);
     if (!error.isEmpty()) {
-        emit errorMessage(error);
+        emit infoMessage(error);
     }
 
-    if (destUrl == url()) {
+    if (op && destUrl == url()) {
         // Mark the dropped urls as selected.
-        markPastedUrlsAsSelected(event->mimeData());
+        m_clearSelectionBeforeSelectingNewItems = true;
+        connect(op, SIGNAL(aboutToCreate(KUrl::List)), this, SLOT(slotAboutToCreate(KUrl::List)));
     }
+
+    setActive(true);
 }
 
 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
@@ -1055,7 +1084,18 @@ void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons
     }
 }
 
-void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
+void DolphinView::slotAboutToCreate(const KUrl::List& urls)
+{
+    if (!urls.isEmpty()) {
+        if (m_markFirstNewlySelectedItemAsCurrent) {
+            markUrlAsCurrent(urls.first());
+            m_markFirstNewlySelectedItemAsCurrent = false;
+        }
+        m_selectedUrls << KDirModel::simplifiedUrlList(urls);
+    }
+}
+
+void DolphinView::slotSelectionChanged(const KItemSet& current, const KItemSet& previous)
 {
     const int currentCount = current.count();
     const int previousCount = previous.count();
@@ -1177,10 +1217,56 @@ QString DolphinView::viewPropertiesContext() const
     return m_viewPropertiesContext;
 }
 
+KUrl DolphinView::openItemAsFolderUrl(const KFileItem& item, const bool browseThroughArchives)
+{
+    if (item.isNull()) {
+        return KUrl();
+    }
+
+    KUrl url = item.targetUrl();
+
+    if (item.isDir()) {
+        return url;
+    }
+
+    if (item.isMimeTypeKnown()) {
+        const QString& mimetype = item.mimetype();
+
+        if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
+            // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
+            // zip:/<path>/ when clicking on a zip file, etc.
+            // The .protocol file specifies the mimetype that the kioslave handles.
+            // Note that we don't use mimetype inheritance since we don't want to
+            // open OpenDocument files as zip folders...
+            const QString& protocol = KProtocolManager::protocolForArchiveMimetype(mimetype);
+            if (!protocol.isEmpty()) {
+                url.setProtocol(protocol);
+                return url;
+            }
+        }
+
+        if (mimetype == QLatin1String("application/x-desktop")) {
+            // Redirect to the URL in Type=Link desktop files, unless it is a http(s) URL.
+            KDesktopFile desktopFile(url.toLocalFile());
+            if (desktopFile.hasLinkType()) {
+                const QString linkUrl = desktopFile.readUrl();
+                if (!linkUrl.startsWith(QLatin1String("http"))) {
+                    return linkUrl;
+                }
+            }
+        }
+    }
+
+    return KUrl();
+}
+
 void DolphinView::observeCreatedItem(const KUrl& url)
 {
-    markUrlAsCurrent(url);
-    markUrlsAsSelected(QList<KUrl>() << url);
+    if (m_active) {
+        clearSelection();
+        markUrlAsCurrent(url);
+        markUrlsAsSelected(QList<KUrl>() << url);
+    }
 }
 
 void DolphinView::slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl)
@@ -1204,10 +1290,11 @@ void DolphinView::updateViewState()
                 m_view->scrollToItem(currentIndex);
                 m_scrollToCurrentItem = false;
             }
+
+            m_currentItemUrl = KUrl();
         } else {
             selectionManager->setCurrentItem(0);
         }
-        m_currentItemUrl = KUrl();
     }
 
     if (!m_restoredContentsPosition.isNull()) {
@@ -1220,20 +1307,27 @@ void DolphinView::updateViewState()
     }
 
     if (!m_selectedUrls.isEmpty()) {
-        clearSelection();
-
         KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
-        QSet<int> selectedItems = selectionManager->selectedItems();
 
-        foreach (const KUrl& url, m_selectedUrls) {
-            const int index = m_model->index(url);
+        if (m_clearSelectionBeforeSelectingNewItems) {
+            selectionManager->clearSelection();
+            m_clearSelectionBeforeSelectingNewItems = false;
+        }
+
+        KItemSet selectedItems = selectionManager->selectedItems();
+
+        QList<KUrl>::iterator it = m_selectedUrls.begin();
+        while (it != m_selectedUrls.end()) {
+            const int index = m_model->index(*it);
             if (index >= 0) {
                 selectedItems.insert(index);
+                it = m_selectedUrls.erase(it);
+            } else {
+                 ++it;
             }
         }
 
         selectionManager->setSelectedItems(selectedItems);
-        m_selectedUrls.clear();
     }
 }
 
@@ -1279,6 +1373,16 @@ void DolphinView::slotDeleteFileFinished(KJob* job)
     }
 }
 
+void DolphinView::slotRenamingFailed(const KUrl& oldUrl, const KUrl& newUrl)
+{
+    const int index = m_model->index(newUrl);
+    if (index >= 0) {
+        QHash<QByteArray, QVariant> data;
+        data.insert("text", oldUrl.fileName());
+        m_model->setData(index, data);
+    }
+}
+
 void DolphinView::slotDirectoryLoadingStarted()
 {
     // Disable the writestate temporary until it can be determined in a fast way
@@ -1345,8 +1449,17 @@ void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray>& curre
     emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
 }
 
+void DolphinView::slotRoleEditingCanceled()
+{
+    disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
+               this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
+}
+
 void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
 {
+    disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
+               this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
+
     if (index < 0 || index >= m_model->count()) {
         return;
     }
@@ -1357,11 +1470,25 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, con
         if (!newName.isEmpty() && newName != oldItem.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
             const KUrl oldUrl = oldItem.url();
 
-            QHash<QByteArray, QVariant> data;
-            data.insert(role, value);
-            m_model->setData(index, data);
+            const KUrl newUrl(url().path(KUrl::AddTrailingSlash) + newName);
+            const bool newNameExistsAlready = (m_model->index(newUrl) >= 0);
+            if (!newNameExistsAlready) {
+                // Only change the data in the model if no item with the new name
+                // is in the model yet. If there is an item with the new name
+                // already, calling KonqOperations::rename() will open a dialog
+                // asking for a new name, and KFileItemModel will update the
+                // data when the dir lister signals that the file name has changed.
+                QHash<QByteArray, QVariant> data;
+                data.insert(role, value);
+                m_model->setData(index, data);
+            }
 
-            KonqOperations::rename(this, oldUrl, newName);
+            KonqOperations* op = KonqOperations::renameV2(this, oldUrl, newName);
+            if (op && !newNameExistsAlready) {
+                // Only connect the renamingFailed signal if there is no item with the new name
+                // in the model yet, see bug 328262.
+                connect(op, SIGNAL(renamingFailed(KUrl,KUrl)), SLOT(slotRenamingFailed(KUrl,KUrl)));
+            }
         }
     }
 }
@@ -1387,9 +1514,13 @@ void DolphinView::loadDirectory(const KUrl& url, bool reload)
 
 void DolphinView::applyViewProperties()
 {
-    m_view->beginTransaction();
-
     const ViewProperties props(viewPropertiesUrl());
+    applyViewProperties(props);
+}
+
+void DolphinView::applyViewProperties(const ViewProperties& props)
+{
+    m_view->beginTransaction();
 
     const Mode mode = props.viewMode();
     if (m_mode != mode) {
@@ -1493,8 +1624,12 @@ void DolphinView::applyModeToView()
 
 void DolphinView::pasteToUrl(const KUrl& url)
 {
-    markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
-    KonqOperations::doPaste(this, url);
+    KonqOperations* op = KonqOperations::doPasteV2(this, url);
+    if (op) {
+        m_clearSelectionBeforeSelectingNewItems = true;
+        m_markFirstNewlySelectedItemAsCurrent = true;
+        connect(op, SIGNAL(aboutToCreate(KUrl::List)), this, SLOT(slotAboutToCreate(KUrl::List)));
+    }
 }
 
 KUrl::List DolphinView::simplifiedSelectedUrls() const
@@ -1517,26 +1652,15 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const
 QMimeData* DolphinView::selectionMimeData() const
 {
     const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
-    const QSet<int> selectedIndexes = selectionManager->selectedItems();
+    const KItemSet selectedIndexes = selectionManager->selectedItems();
 
     return m_model->createMimeData(selectedIndexes);
 }
 
-void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
-{
-    const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
-    KUrl::List destUrls;
-    foreach (const KUrl& source, sourceUrls) {
-        KUrl destination(url().url() + '/' + source.fileName());
-        destUrls << destination;
-    }
-    markUrlsAsSelected(destUrls);
-}
-
 void DolphinView::updateWritableState()
 {
     const bool wasFolderWritable = m_isFolderWritable;
-    m_isFolderWritable = true;
+    m_isFolderWritable = false;
 
     const KFileItem item = m_model->rootItem();
     if (!item.isNull()) {