]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
DolphinView: Add placeholder label for AFC kio
[dolphin.git] / src / views / dolphinview.cpp
index 5646fa9828ccabdcad33ffe746f682a58d2cb8bd..c84e642dc8a0362cb19d5acae4d4345dc5faf9e4 100644 (file)
 #include "kitemviews/kitemlistselectionmanager.h"
 #include "kitemviews/private/kitemlistroleeditor.h"
 #include "settings/viewmodes/viewmodesettings.h"
+#include "selectionmode/singleclickselectionproxystyle.h"
 #include "versioncontrol/versioncontrolobserver.h"
 #include "viewproperties.h"
 #include "views/tooltips/tooltipmanager.h"
 #include "zoomlevelinfo.h"
 
-#ifdef HAVE_BALOO
+#if HAVE_BALOO
 #include <Baloo/IndexerConfig>
 #endif
 #include <KColorScheme>
 #include <KIO/JobUiDelegate>
 #include <KIO/Paste>
 #include <KIO/PasteJob>
-#include <KIO/PreviewJob>
 #include <KIO/RenameFileDialog>
 #include <KJobWidgets>
 #include <KLocalizedString>
 #include <KMessageBox>
 #include <KProtocolManager>
+#include <KUrlMimeData>
+
+#include <kwidgetsaddons_version.h>
+
+#include <kio_version.h>
+#if KIO_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+#include <KIO/DeleteOrTrashJob>
+#endif
 
 #include <QAbstractItemView>
 #include <QActionGroup>
@@ -70,7 +78,6 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
     m_assureVisibleCurrentIndex(false),
     m_isFolderWritable(true),
     m_dragging(false),
-    m_loading(false),
     m_url(url),
     m_viewPropertiesContext(),
     m_mode(DolphinView::IconsView),
@@ -110,7 +117,7 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
 
     m_model = new KFileItemModel(this);
     m_view = new DolphinItemListView();
-    m_view->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
+    m_view->setEnabledSelectionToggles(DolphinItemListView::SelectionTogglesEnabled::FollowSetting);
     m_view->setVisibleRoles({"text"});
     applyModeToView();
 
@@ -172,6 +179,7 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
     connect(controller, &KItemListController::increaseZoom, this, &DolphinView::slotIncreaseZoom);
     connect(controller, &KItemListController::decreaseZoom, this, &DolphinView::slotDecreaseZoom);
     connect(controller, &KItemListController::swipeUp, this, &DolphinView::slotSwipeUp);
+    connect(controller, &KItemListController::selectionModeChangeRequested, this, &DolphinView::selectionModeChangeRequested);
 
     connect(m_model, &KFileItemModel::directoryLoadingStarted,       this, &DolphinView::slotDirectoryLoadingStarted);
     connect(m_model, &KFileItemModel::directoryLoadingCompleted,     this, &DolphinView::slotDirectoryLoadingCompleted);
@@ -187,6 +195,7 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
     connect(m_model, &KFileItemModel::directoryRedirection, this, &DolphinView::slotDirectoryRedirection);
     connect(m_model, &KFileItemModel::urlIsFileError,            this, &DolphinView::urlIsFileError);
     connect(m_model, &KFileItemModel::fileItemsChanged,            this, &DolphinView::fileItemsChanged);
+    connect(m_model, &KFileItemModel::currentDirectoryRemoved, this, &DolphinView::currentDirectoryRemoved);
 
     connect(this, &DolphinView::itemCountChanged,
             this, &DolphinView::updatePlaceholderLabel);
@@ -202,14 +211,14 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
             this, &DolphinView::slotRoleEditingCanceled);
     connect(m_view->header(), &KItemListHeader::columnWidthChangeFinished,
             this, &DolphinView::slotHeaderColumnWidthChangeFinished);
-    connect(m_view->header(), &KItemListHeader::leadingPaddingChanged,
-            this, &DolphinView::slotLeadingPaddingWidthChanged);
+    connect(m_view->header(), &KItemListHeader::sidePaddingChanged,
+            this, &DolphinView::slotSidePaddingWidthChanged);
 
     KItemListSelectionManager* selectionManager = controller->selectionManager();
     connect(selectionManager, &KItemListSelectionManager::selectionChanged,
             this, &DolphinView::slotSelectionChanged);
 
-#ifdef HAVE_BALOO
+#if HAVE_BALOO
     m_toolTipManager = new ToolTipManager(this);
     connect(m_toolTipManager, &ToolTipManager::urlActivated, this, &DolphinView::urlActivated);
 #endif
@@ -233,6 +242,7 @@ DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
 
 DolphinView::~DolphinView()
 {
+    disconnect(m_container->controller(), &KItemListController::modelChanged, this, &DolphinView::slotModelChanged);
 }
 
 QUrl DolphinView::url() const
@@ -262,7 +272,7 @@ bool DolphinView::isActive() const
     return m_active;
 }
 
-void DolphinView::setMode(Mode mode)
+void DolphinView::setViewMode(Mode mode)
 {
     if (mode != m_mode) {
         ViewProperties props(viewPropertiesUrl());
@@ -276,11 +286,31 @@ void DolphinView::setMode(Mode mode)
     }
 }
 
-DolphinView::Mode DolphinView::mode() const
+DolphinView::Mode DolphinView::viewMode() const
 {
     return m_mode;
 }
 
+void DolphinView::setSelectionModeEnabled(const bool enabled)
+{
+    if (enabled) {
+        m_proxyStyle = std::make_unique<SelectionMode::SingleClickSelectionProxyStyle>();
+        setStyle(m_proxyStyle.get());
+        m_view->setStyle(m_proxyStyle.get());
+        m_view->setEnabledSelectionToggles(DolphinItemListView::SelectionTogglesEnabled::False);
+    } else {
+        setStyle(QApplication::style());
+        m_view->setStyle(QApplication::style());
+        m_view->setEnabledSelectionToggles(DolphinItemListView::SelectionTogglesEnabled::FollowSetting);
+    }
+    m_container->controller()->setSelectionModeEnabled(enabled);
+}
+
+bool DolphinView::selectionMode() const
+{
+    return m_container->controller()->selectionMode();
+}
+
 void DolphinView::setPreviewsShown(bool show)
 {
     if (previewsShown() == show) {
@@ -729,6 +759,13 @@ void DolphinView::renameSelectedItems()
 void DolphinView::trashSelectedItems()
 {
     const QList<QUrl> list = simplifiedSelectedUrls();
+
+#if KIO_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+    using Iface = KIO::AskUserActionInterface;
+    auto *trashJob = new KIO::DeleteOrTrashJob(list, Iface::Trash, Iface::DefaultConfirmation, this);
+    connect(trashJob, &KJob::result, this, &DolphinView::slotTrashFileFinished);
+    trashJob->start();
+#else
     KIO::JobUiDelegate uiDelegate;
     uiDelegate.setWindow(window());
     if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) {
@@ -738,12 +775,19 @@ void DolphinView::trashSelectedItems()
         connect(job, &KIO::Job::result,
                 this, &DolphinView::slotTrashFileFinished);
     }
+#endif
 }
 
 void DolphinView::deleteSelectedItems()
 {
     const QList<QUrl> list = simplifiedSelectedUrls();
 
+#if KIO_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+    using Iface = KIO::AskUserActionInterface;
+    auto *trashJob = new KIO::DeleteOrTrashJob(list, Iface::Delete, Iface::DefaultConfirmation, this);
+    connect(trashJob, &KJob::result, this, &DolphinView::slotTrashFileFinished);
+    trashJob->start();
+#else
     KIO::JobUiDelegate uiDelegate;
     uiDelegate.setWindow(window());
     if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) {
@@ -752,18 +796,21 @@ void DolphinView::deleteSelectedItems()
         connect(job, &KIO::Job::result,
                 this, &DolphinView::slotDeleteFileFinished);
     }
+#endif
 }
 
 void DolphinView::cutSelectedItemsToClipboard()
 {
     QMimeData* mimeData = selectionMimeData();
     KIO::setClipboardDataCut(mimeData, true);
+    KUrlMimeData::exportUrlsToPortal(mimeData);
     QApplication::clipboard()->setMimeData(mimeData);
 }
 
 void DolphinView::copySelectedItemsToClipboard()
 {
-    QMimeData* mimeData = selectionMimeData();
+    QMimeData *mimeData = selectionMimeData();
+    KUrlMimeData::exportUrlsToPortal(mimeData);
     QApplication::clipboard()->setMimeData(mimeData);
 }
 
@@ -925,7 +972,7 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event)
         break;
 
     case QEvent::ToolTip:
-        tryShowNameToolTip(event);
+        tryShowNameToolTip(static_cast<QHelpEvent*>(event));
 
     default:
         break;
@@ -993,8 +1040,19 @@ void DolphinView::slotItemsActivated(const KItemSet &indexes)
 
     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 KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+        const int answer = KMessageBox::warningTwoActions(this, question, {},
+#else
+        const int answer = KMessageBox::warningYesNo(this, question, {},
+#endif
+                                                     KGuiItem(i18ncp("@action:button", "Open %1 Item", "Open %1 Items", indexes.count()),
+                                                              QStringLiteral("document-open")),
+                                                     KStandardGuiItem::cancel());
+#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+        if (answer != KMessageBox::PrimaryAction) {
+#else
         if (answer != KMessageBox::Yes) {
+#endif
             return;
         }
     }
@@ -1058,12 +1116,12 @@ void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
     }
 
     const KFileItem item = m_model->fileItem(index);
-    Q_EMIT requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
+    Q_EMIT requestContextMenu(pos.toPoint(), item, selectedItems(), url());
 }
 
 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
 {
-    Q_EMIT requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
+    Q_EMIT requestContextMenu(pos.toPoint(), KFileItem(), selectedItems(), url());
 }
 
 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
@@ -1076,7 +1134,7 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
     const QList<QByteArray> visibleRolesSet = view->visibleRoles();
 
     bool indexingEnabled = false;
-#ifdef HAVE_BALOO
+#if HAVE_BALOO
     Baloo::IndexerConfig config;
     indexingEnabled = config.fileIndexingEnabled();
 #endif
@@ -1120,9 +1178,9 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
     QActionGroup* widthsGroup = new QActionGroup(menu);
     const bool autoColumnWidths = props.headerColumnWidths().isEmpty();
 
-    QAction* toggleLeadingPaddingAction = menu->addAction(i18nc("@action:inmenu", "Leading Column Padding"));
-    toggleLeadingPaddingAction->setCheckable(true);
-    toggleLeadingPaddingAction->setChecked(view->header()->leadingPadding() > 0);
+    QAction* toggleSidePaddingAction = menu->addAction(i18nc("@action:inmenu", "Side Padding"));
+    toggleSidePaddingAction->setCheckable(true);
+    toggleSidePaddingAction->setChecked(view->header()->sidePadding() > 0);
 
     QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
     autoAdjustWidthsAction->setCheckable(true);
@@ -1154,8 +1212,8 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
             }
             props.setHeaderColumnWidths(columnWidths);
             header->setAutomaticColumnResizing(false);
-        } else if (action == toggleLeadingPaddingAction) {
-            header->setLeadingPadding(toggleLeadingPaddingAction->isChecked() ? 20 : 0);
+        } else if (action == toggleSidePaddingAction) {
+            header->setSidePadding(toggleSidePaddingAction->isChecked() ? 20 : 0);
         } else {
             // Show or hide the selected role
             const QByteArray selectedRole = action->data().toByteArray();
@@ -1208,10 +1266,10 @@ void DolphinView::slotHeaderColumnWidthChangeFinished(const QByteArray& role, qr
     props.setHeaderColumnWidths(columnWidths);
 }
 
-void DolphinView::slotLeadingPaddingWidthChanged(qreal width)
+void DolphinView::slotSidePaddingWidthChanged(qreal width)
 {
     ViewProperties props(viewPropertiesUrl());
-    DetailsModeSettings::setLeadingPadding(int(width));
+    DetailsModeSettings::setSidePadding(int(width));
     m_view->writeSettings();
 }
 
@@ -1224,8 +1282,11 @@ void DolphinView::slotItemHovered(int index)
         const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
         itemRect.moveTo(pos);
 
-#ifdef HAVE_BALOO
-        m_toolTipManager->showToolTip(item, itemRect, nativeParentWidget()->windowHandle());
+#if HAVE_BALOO
+        auto nativeParent = nativeParentWidget();
+        if (nativeParent) {
+            m_toolTipManager->showToolTip(item, itemRect, nativeParent->windowHandle());
+        }
 #endif
     }
 
@@ -1337,7 +1398,7 @@ void DolphinView::slotItemCreated(const QUrl& url)
 
 void DolphinView::slotJobResult(KJob *job)
 {
-    if (job->error()) {
+    if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
         Q_EMIT errorMessage(job->errorString());
     }
     if (!m_selectedUrls.isEmpty()) {
@@ -1460,6 +1521,16 @@ bool DolphinView::itemsExpandable() const
     return m_mode == DetailsView;
 }
 
+bool DolphinView::isExpanded(const KFileItem& item) const
+{
+    Q_ASSERT(item.isDir());
+    Q_ASSERT(items().contains(item));
+    if (!itemsExpandable()) {
+        return false;
+    }
+    return m_model->isExpanded(m_model->index(item));
+}
+
 void DolphinView::restoreState(QDataStream& stream)
 {
     // Read the version number of the view state and check if the version is supported.
@@ -1611,12 +1682,13 @@ void DolphinView::updateViewState()
                     m_view->scrollToItem(currentIndex);
                     m_scrollToCurrentItem = false;
                 }
+                m_currentItemUrl = QUrl();
             } else {
                 selectionManager->setCurrentItem(0);
             }
+        } else {
+            m_currentItemUrl = QUrl();
         }
-
-        m_currentItemUrl = QUrl();
     }
 
     if (!m_restoredContentsPosition.isNull()) {
@@ -1651,8 +1723,10 @@ void DolphinView::updateViewState()
                 }
             }
 
-            selectionManager->beginAnchoredSelection(selectionManager->currentItem());
-            selectionManager->setSelectedItems(selectedItems);
+            if (!selectedItems.isEmpty()) {
+                selectionManager->beginAnchoredSelection(selectionManager->currentItem());
+                selectionManager->setSelectedItems(selectedItems);
+            }
         }
     }
 }
@@ -1660,7 +1734,7 @@ void DolphinView::updateViewState()
 void DolphinView::hideToolTip(const ToolTipManager::HideBehavior behavior)
 {
     if (GeneralSettings::showToolTips()) {
-#ifdef HAVE_BALOO
+#if HAVE_BALOO
         m_toolTipManager->hideToolTip(behavior);
 #else
         Q_UNUSED(behavior)
@@ -1722,7 +1796,7 @@ void DolphinView::slotRenamingResult(KJob* job)
 
 void DolphinView::slotDirectoryLoadingStarted()
 {
-    m_loading = true;
+    m_loadingState = LoadingState::Loading;
     updatePlaceholderLabel();
 
     // Disable the writestate temporary until it can be determined in a fast way
@@ -1737,7 +1811,7 @@ void DolphinView::slotDirectoryLoadingStarted()
 
 void DolphinView::slotDirectoryLoadingCompleted()
 {
-    m_loading = false;
+    m_loadingState = LoadingState::Completed;
 
     // Update the view-state. This has to be done asynchronously
     // because the view might not be in its final state yet.
@@ -1754,7 +1828,7 @@ void DolphinView::slotDirectoryLoadingCompleted()
 
 void DolphinView::slotDirectoryLoadingCanceled()
 {
-    m_loading = false;
+    m_loadingState = LoadingState::Canceled;
 
     updatePlaceholderLabel();
 
@@ -1833,10 +1907,13 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, con
 #ifndef Q_OS_WIN
             //Confirm hiding file/directory by renaming inline
             if (!hiddenFilesShown() && newName.startsWith(QLatin1Char('.')) && !oldItem.name().startsWith(QLatin1Char('.'))) {
-                KGuiItem yesGuiItem(KStandardGuiItem::yes());
-                yesGuiItem.setText(i18nc("@action:button", "Rename and Hide"));
+                KGuiItem yesGuiItem(i18nc("@action:button", "Rename and Hide"), QStringLiteral("view-hidden"));
 
+#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+                const auto code = KMessageBox::questionTwoActions(this,
+#else
                 const auto code = KMessageBox::questionYesNo(this,
+#endif
                                                              oldItem.isFile() ? i18n("Adding a dot to the beginning of this file's name will hide it from view.\n"
                                                                                      "Do you still want to rename it?")
                                                                               : i18n("Adding a dot to the beginning of this folder's name will hide it from view.\n"
@@ -1847,7 +1924,11 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, con
                                                              QStringLiteral("ConfirmHide")
                                                             );
 
+#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+                if (code == KMessageBox::SecondaryAction) {
+#else
                 if (code == KMessageBox::No) {
+#endif
                    return;
                 }
             }
@@ -2008,7 +2089,7 @@ void DolphinView::applyViewProperties(const ViewProperties& props)
         } else {
             header->setAutomaticColumnResizing(true);
         }
-        header->setLeadingPadding(DetailsModeSettings::leadingPadding());
+        header->setSidePadding(DetailsModeSettings::sidePadding());
     }
 
     m_view->endTransaction();
@@ -2153,13 +2234,15 @@ void DolphinView::updatePlaceholderLabel()
         return;
     }
 
-    if (m_loading) {
+    if (m_loadingState == LoadingState::Loading) {
         m_placeholderLabel->setVisible(false);
         m_showLoadingPlaceholderTimer->start();
         return;
     }
 
-    if (!nameFilter().isEmpty()) {
+    if (m_loadingState == LoadingState::Canceled) {
+        m_placeholderLabel->setText(i18n("Loading canceled"));
+    } else if (!nameFilter().isEmpty()) {
         m_placeholderLabel->setText(i18n("No items matching the filter"));
     } else if (m_url.scheme() == QLatin1String("baloosearch") || m_url.scheme() == QLatin1String("filenamesearch")) {
         m_placeholderLabel->setText(i18n("No items matching the search"));
@@ -2181,6 +2264,8 @@ void DolphinView::updatePlaceholderLabel()
         m_placeholderLabel->setText(i18n("No relevant network resources found"));
     } else if (m_url.scheme() == QLatin1String("mtp") && m_url.path() == QLatin1String("/")) {
         m_placeholderLabel->setText(i18n("No MTP-compatible devices found"));
+    } else if (m_url.scheme() == QLatin1String("afc") && m_url.path() == QLatin1String("/")) {
+        m_placeholderLabel->setText(i18n("No Apple devices found"));
     } else if (m_url.scheme() == QLatin1String("bluetooth")) {
         m_placeholderLabel->setText(i18n("No Bluetooth devices found"));
     } else {
@@ -2190,11 +2275,10 @@ void DolphinView::updatePlaceholderLabel()
     m_placeholderLabel->setVisible(true);
 }
 
-void DolphinView::tryShowNameToolTip(QEvent* event)
+void DolphinView::tryShowNameToolTip(QHelpEvent* event)
 {
     if (!GeneralSettings::showToolTips() && m_mode == DolphinView::IconsView) {
-        QHelpEvent *hoverEvent = reinterpret_cast<QHelpEvent *>(event);
-        const std::optional<int> index = m_view->itemAt(hoverEvent->pos());
+        const std::optional<int> index = m_view->itemAt(event->pos());
 
         if (!index.has_value()) {
             return;
@@ -2206,7 +2290,7 @@ void DolphinView::tryShowNameToolTip(QEvent* event)
         if(isElided) {
             const KFileItem item = m_model->fileItem(index.value());
             const QString text = item.text();
-            const QPoint pos = mapToGlobal(hoverEvent->pos());
+            const QPoint pos = mapToGlobal(event->pos());
             QToolTip::showText(pos, text);
         }
     }