]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/dolphinview.cpp
DolphinView: rename copySelectedItems to copySelectedItemsToClipboard
[dolphin.git] / src / views / dolphinview.cpp
index cfece0fe6b7f1e0b17349269fe340a888ec36b01..c98929ead97dbfc545bb6bf783d339f7317c1604 100644 (file)
@@ -63,6 +63,7 @@
 #include <QDropEvent>
 #include <QGraphicsSceneDragDropEvent>
 #include <QMenu>
+#include <QMimeDatabase>
 #include <QPixmapCache>
 #include <QPointer>
 #include <QScrollBar>
@@ -363,7 +364,7 @@ void DolphinView::markUrlAsCurrent(const QUrl &url)
     m_scrollToCurrentItem = true;
 }
 
-void DolphinView::selectItems(const QRegExp& pattern, bool enabled)
+void DolphinView::selectItems(const QRegularExpression &regexp, bool enabled)
 {
     const KItemListSelectionManager::SelectionMode mode = enabled
                                                         ? KItemListSelectionManager::Select
@@ -372,7 +373,7 @@ 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())) {
+        if (regexp.match(item.text()).hasMatch()) {
             // 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(KItemSet, SelectionMode mode)
@@ -685,7 +686,7 @@ void DolphinView::cutSelectedItems()
     QApplication::clipboard()->setMimeData(mimeData);
 }
 
-void DolphinView::copySelectedItems()
+void DolphinView::copySelectedItemsToClipboard()
 {
     QMimeData* mimeData = selectionMimeData();
     QApplication::clipboard()->setMimeData(mimeData);
@@ -704,6 +705,53 @@ void DolphinView::pasteIntoFolder()
     }
 }
 
+void DolphinView::duplicateSelectedItems()
+{
+    const KFileItemList itemList = selectedItems();
+    if (itemList.isEmpty()) {
+        return;
+    }
+
+    const QMimeDatabase db;
+
+    // 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();
+        const QString originalFileName = item.name();
+
+        QString extension = db.suffixForFileName(originalFileName);
+
+        QUrl duplicateURL = originalURL;
+
+        // No extension; new filename is "<oldfilename> copy"
+        if (extension.isEmpty()) {
+            duplicateURL.setPath(originalDirectoryPath + i18nc("<filename> copy", "%1 copy", originalFileName));
+        // There's an extension; new filename is "<oldfilename> copy.<extension>"
+        } else {
+            // Need to add a dot since QMimeDatabase::suffixForFileName() doesn't include it
+            extension = QLatin1String(".") + extension;
+            const QString originalFilenameWithoutExtension = originalFileName.chopped(extension.size());
+            // Preserve file's original filename extension in case the casing differs
+            // from what QMimeDatabase::suffixForFileName() returned
+            const QString originalExtension = originalFileName.right(extension.size());
+            duplicateURL.setPath(originalDirectoryPath + i18nc("<filename> copy", "%1 copy", originalFilenameWithoutExtension) + originalExtension);
+        }
+
+        KIO::CopyJob* job = KIO::copyAs(originalURL, duplicateURL, KIO::HideProgressInfo);
+        KJobWidgets::setWindow(job, this);
+
+        if (job) {
+            newSelection << duplicateURL;
+            KIO::FileUndoManager::self()->recordCopyJob(job);
+        }
+    }
+
+    forceUrlsSelection(newSelection.first(), newSelection);
+}
+
 void DolphinView::stopLoading()
 {
     m_model->cancelDirectoryLoading();
@@ -1152,7 +1200,7 @@ void DolphinView::slotPasteJobResult(KJob *job)
         emit errorMessage(job->errorString());
     }
     if (!m_selectedUrls.isEmpty()) {
-        m_selectedUrls << KDirModel::simplifiedUrlList(m_selectedUrls);
+        m_selectedUrls = KDirModel::simplifiedUrlList(m_selectedUrls);
     }
 }
 
@@ -1445,13 +1493,27 @@ void DolphinView::calculateItemCount(int& fileCount,
                                      KIO::filesize_t& totalFileSize) const
 {
     const int itemCount = m_model->count();
+
+    bool countFileSize = true;
+
+    // In case we have a precomputed value
+    const auto job = KIO::statDetails(m_model->rootItem().url(), KIO::StatJob::SourceSide, KIO::StatRecursiveSize);
+    job->exec();
+    const auto entry =  job->statResult();
+    if (entry.contains(KIO::UDSEntry::UDS_RECURSIVE_SIZE)) {
+        totalFileSize = static_cast<KIO::filesize_t>(entry.numberValue(KIO::UDSEntry::UDS_RECURSIVE_SIZE));
+        countFileSize = false;
+    }
+
     for (int i = 0; i < itemCount; ++i) {
         const KFileItem item = m_model->fileItem(i);
         if (item.isDir()) {
             ++folderCount;
         } else {
             ++fileCount;
-            totalFileSize += item.size();
+            if (countFileSize) {
+                totalFileSize += item.size();
+            }
         }
     }
 }