]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
ViewProperties: Store view properties in extended file attributes
[dolphin.git] / src / views / dolphinview.cpp
index d51ed0e066ed58ddf48476e3d88ec04999bed780..0c5ebb1df72a2a82e0372751d8183cf40ed119ec 100644 (file)
@@ -119,6 +119,7 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent)
     KItemListController *controller = new KItemListController(m_model, m_view, this);
     const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
     controller->setAutoActivationDelay(delay);
+    connect(controller, &KItemListController::doubleClickViewBackground, this, &DolphinView::doubleClickViewBackground);
 
     // The EnlargeSmallPreviews setting can only be changed after the model
     // has been set in the view by KItemListController.
@@ -146,11 +147,13 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent)
     // This is made using a heavily-modified QLabel rather than a KTitleWidget
     // because KTitleWidget can't be told to turn off mouse-selectable text
     m_placeholderLabel = new QLabel(this);
+    // Don't consume mouse events
+    m_placeholderLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
+
     QFont placeholderLabelFont;
     // To match the size of a level 2 Heading/KTitleWidget
     placeholderLabelFont.setPointSize(qRound(placeholderLabelFont.pointSize() * 1.3));
     m_placeholderLabel->setFont(placeholderLabelFont);
-    m_placeholderLabel->setTextInteractionFlags(Qt::NoTextInteraction);
     m_placeholderLabel->setWordWrap(true);
     m_placeholderLabel->setAlignment(Qt::AlignCenter);
     // Match opacity of QML placeholder label component
@@ -163,10 +166,6 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent)
     auto *centeringLayout = new QVBoxLayout(m_container);
     centeringLayout->addWidget(m_placeholderLabel);
     centeringLayout->setAlignment(m_placeholderLabel, Qt::AlignCenter);
-    m_placeholderLabel->setContextMenuPolicy(Qt::CustomContextMenu);
-    connect(m_placeholderLabel, &QWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
-        slotViewContextMenuRequested(m_placeholderLabel->mapToGlobal(pos));
-    });
 
     controller->setSelectionBehavior(KItemListController::MultiSelection);
     connect(controller, &KItemListController::itemActivated, this, &DolphinView::slotItemActivated);
@@ -231,7 +230,9 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent)
     m_versionControlObserver->setView(this);
     m_versionControlObserver->setModel(m_model);
     connect(m_versionControlObserver, &VersionControlObserver::infoMessage, this, &DolphinView::infoMessage);
-    connect(m_versionControlObserver, &VersionControlObserver::errorMessage, this, &DolphinView::errorMessage);
+    connect(m_versionControlObserver, &VersionControlObserver::errorMessage, this, [this](const QString &message) {
+        Q_EMIT errorMessage(message, KIO::ERR_UNKNOWN);
+    });
     connect(m_versionControlObserver, &VersionControlObserver::operationCompletedMessage, this, &DolphinView::operationCompletedMessage);
 
     m_twoClicksRenamingTimer = new QTimer(this);
@@ -374,9 +375,8 @@ void DolphinView::setGroupedSorting(bool grouped)
 
     ViewProperties props(viewPropertiesUrl());
     props.setGroupedSorting(grouped);
-    props.save();
 
-    m_container->controller()->model()->setGroupedSorting(grouped);
+    m_model->setGroupedSorting(grouped);
 
     Q_EMIT groupedSortingChanged(grouped);
 }
@@ -740,22 +740,39 @@ void DolphinView::renameSelectedItems()
     if (items.count() == 1 && GeneralSettings::renameInline()) {
         const int index = m_model->index(items.first());
 
-        QMetaObject::Connection *const connection = new QMetaObject::Connection;
-        *connection = connect(m_view, &KItemListView::scrollingStopped, this, [=]() {
-            QObject::disconnect(*connection);
-            delete connection;
-
-            m_view->editRole(index, "text");
+        connect(
+            m_view,
+            &KItemListView::scrollingStopped,
+            this,
+            [this, index]() {
+                m_view->editRole(index, "text");
 
-            hideToolTip();
+                hideToolTip();
 
-            connect(m_view, &DolphinItemListView::roleEditingFinished, this, &DolphinView::slotRoleEditingFinished);
-        });
+                connect(m_view, &DolphinItemListView::roleEditingFinished, this, &DolphinView::slotRoleEditingFinished);
+            },
+            Qt::SingleShotConnection);
         m_view->scrollToItem(index);
 
     } else {
         KIO::RenameFileDialog *dialog = new KIO::RenameFileDialog(items, this);
-        connect(dialog, &KIO::RenameFileDialog::renamingFinished, this, &DolphinView::slotRenameDialogRenamingFinished);
+        connect(dialog, &KIO::RenameFileDialog::renamingFinished, this, [this, items](const QList<QUrl> &urls) {
+            // The model may have already been updated, so it's possible that we don't find the old items.
+            for (int i = 0; i < items.count(); ++i) {
+                const int index = m_model->index(items[i]);
+                if (index >= 0) {
+                    QHash<QByteArray, QVariant> data;
+                    data.insert("text", urls[i].fileName());
+                    m_model->setData(index, data);
+                }
+            }
+
+            forceUrlsSelection(urls.first(), urls);
+            updateSelectionState();
+        });
+        connect(dialog, &KIO::RenameFileDialog::error, this, [this](KJob *job) {
+            KMessageBox::error(this, job->errorString());
+        });
 
         dialog->open();
     }
@@ -863,9 +880,12 @@ void DolphinView::duplicateSelectedItems()
 
     const QMimeDatabase db;
 
+    m_clearSelectionBeforeSelectingNewItems = true;
+    m_markFirstNewlySelectedItemAsCurrent = true;
+    m_selectJobCreatedItems = true;
+
     // Duplicate all selected items and append "copy" to the end of the file name
     // but before the filename extension, if present
-    QList<QUrl> newSelection;
     for (const auto &item : itemList) {
         const QUrl originalURL = item.url();
         const QString originalDirectoryPath = originalURL.adjusted(QUrl::RemoveFilename).path();
@@ -890,15 +910,14 @@ void DolphinView::duplicateSelectedItems()
         }
 
         KIO::CopyJob *job = KIO::copyAs(originalURL, duplicateURL);
+        job->setAutoRename(true);
         KJobWidgets::setWindow(job, this);
 
-        if (job) {
-            newSelection << duplicateURL;
-            KIO::FileUndoManager::self()->recordCopyJob(job);
-        }
+        connect(job, &KIO::CopyJob::result, this, &DolphinView::slotJobResult);
+        connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotItemCreatedFromJob);
+        connect(job, &KIO::CopyJob::copyingLinkDone, this, &DolphinView::slotItemLinkCreatedFromJob);
+        KIO::FileUndoManager::self()->recordCopyJob(job);
     }
-
-    forceUrlsSelection(newSelection.first(), newSelection);
 }
 
 void DolphinView::stopLoading()
@@ -1115,9 +1134,8 @@ void DolphinView::slotItemsActivated(const KItemSet &indexes)
 void DolphinView::slotItemMiddleClicked(int index)
 {
     const KFileItem &item = m_model->fileItem(index);
-    const QUrl &url = openItemAsFolderUrl(item);
+    const QUrl &url = openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
     const auto modifiers = QGuiApplication::keyboardModifiers();
-    const QString &archiveProtocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
     if (!url.isEmpty()) {
         // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
         if (modifiers & Qt::ShiftModifier) {
@@ -1125,7 +1143,7 @@ void DolphinView::slotItemMiddleClicked(int index)
         } else {
             Q_EMIT tabRequested(url);
         }
-    } else if (!archiveProtocol.isEmpty() && isTabsForFilesEnabled()) {
+    } else if (isTabsForFilesEnabled()) {
         // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
         if (modifiers & Qt::ShiftModifier) {
             Q_EMIT activeTabRequested(item.url());
@@ -1422,6 +1440,11 @@ void DolphinView::slotItemCreatedFromJob(KIO::Job *, const QUrl &, const QUrl &t
     slotItemCreated(to);
 }
 
+void DolphinView::slotItemLinkCreatedFromJob(KIO::Job *, const QUrl &, const QString &, const QUrl &to)
+{
+    slotItemCreated(to);
+}
+
 void DolphinView::slotItemCreated(const QUrl &url)
 {
     if (m_markFirstNewlySelectedItemAsCurrent) {
@@ -1446,7 +1469,7 @@ void DolphinView::onDirectoryLoadingCompletedAfterJob()
 void DolphinView::slotJobResult(KJob *job)
 {
     if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
-        Q_EMIT errorMessage(job->errorString());
+        Q_EMIT errorMessage(job->errorString(), job->error());
     }
     if (!m_selectJobCreatedItems) {
         m_selectedUrls.clear();
@@ -1708,6 +1731,7 @@ void DolphinView::updateSelectionState()
     if (!m_selectedUrls.isEmpty()) {
         KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager();
 
+        const bool shouldScrollToCurrentItem = m_clearSelectionBeforeSelectingNewItems;
         // if there is a selection already, leave it that way
         // unless some drop/paste job are in the process of creating items
         if (!selectionManager->hasSelection() || m_selectJobCreatedItems) {
@@ -1732,6 +1756,10 @@ void DolphinView::updateSelectionState()
             if (!selectedItems.isEmpty()) {
                 selectionManager->beginAnchoredSelection(selectionManager->currentItem());
                 selectionManager->setSelectedItems(selectedItems);
+                selectionManager->endAnchoredSelection();
+                if (shouldScrollToCurrentItem) {
+                    m_view->scrollToItem(selectedItems.first());
+                }
             }
         }
     }
@@ -1814,7 +1842,7 @@ void DolphinView::slotTrashFileFinished(KJob *job)
         selectNextItem(); // Fixes BUG: 419914 via selecting next item
         Q_EMIT operationCompletedMessage(i18nc("@info:status", "Trash operation completed."));
     } else if (job->error() != KIO::ERR_USER_CANCELED) {
-        Q_EMIT errorMessage(job->errorString());
+        Q_EMIT errorMessage(job->errorString(), job->error());
     }
 }
 
@@ -1824,7 +1852,7 @@ void DolphinView::slotDeleteFileFinished(KJob *job)
         selectNextItem(); // Fixes BUG: 419914 via selecting next item
         Q_EMIT operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
     } else if (job->error() != KIO::ERR_USER_CANCELED) {
-        Q_EMIT errorMessage(job->errorString());
+        Q_EMIT errorMessage(job->errorString(), job->error());
     }
 }
 
@@ -1930,7 +1958,10 @@ void DolphinView::slotSortRoleChangedByHeader(const QByteArray &current, const Q
     Q_UNUSED(previous)
     Q_ASSERT(m_model->sortRole() == current);
 
-    setSortRole(current);
+    ViewProperties props(viewPropertiesUrl());
+    props.setSortRole(current);
+
+    Q_EMIT sortRoleChanged(current);
 }
 
 void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray> &current, const QList<QByteArray> &previous)
@@ -2013,6 +2044,7 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray &role, con
 
             if (!newNameExistsAlready) {
                 forceUrlsSelection(newUrl, {newUrl});
+                updateSelectionState();
 
                 // Only connect the result signal if there is no item with the new name
                 // in the model yet, see bug 328262.
@@ -2033,9 +2065,9 @@ void DolphinView::loadDirectory(const QUrl &url, bool reload)
     if (!url.isValid()) {
         const QString location(url.toDisplayString(QUrl::PreferLocalFile));
         if (location.isEmpty()) {
-            Q_EMIT errorMessage(i18nc("@info:status", "The location is empty."));
+            Q_EMIT errorMessage(i18nc("@info:status", "The location is empty."), KIO::ERR_UNKNOWN);
         } else {
-            Q_EMIT errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
+            Q_EMIT errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location), KIO::ERR_UNKNOWN);
         }
         return;
     }
@@ -2179,8 +2211,16 @@ void DolphinView::pasteToUrl(const QUrl &url)
     m_clearSelectionBeforeSelectingNewItems = true;
     m_markFirstNewlySelectedItemAsCurrent = true;
     m_selectJobCreatedItems = true;
-    // TODO KF6 use KIO::PasteJob::copyJobStarted to hook to earlier events copying/moving
     connect(job, &KIO::PasteJob::itemCreated, this, &DolphinView::slotItemCreated);
+    connect(job, &KIO::PasteJob::copyJobStarted, this, [this](const KIO::CopyJob *copyJob) {
+        connect(copyJob, &KIO::CopyJob::copying, this, &DolphinView::slotItemCreatedFromJob);
+        connect(copyJob, &KIO::CopyJob::moving, this, &DolphinView::slotItemCreatedFromJob);
+        connect(copyJob, &KIO::CopyJob::linking, this, [this](KIO::Job *job, const QString &src, const QUrl &dest) {
+            Q_UNUSED(job)
+            Q_UNUSED(src)
+            slotItemCreated(dest);
+        });
+    });
     connect(job, &KIO::PasteJob::result, this, &DolphinView::slotJobResult);
 }
 
@@ -2248,11 +2288,6 @@ QUrl DolphinView::viewPropertiesUrl() const
     return url;
 }
 
-void DolphinView::slotRenameDialogRenamingFinished(const QList<QUrl> &urls)
-{
-    forceUrlsSelection(urls.first(), urls);
-}
-
 void DolphinView::forceUrlsSelection(const QUrl &current, const QList<QUrl> &selected)
 {
     clearSelection();