X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/41105103b063c2e538bf0071e54fd429a841238b..67ebd66f94356b4e66005b1072919cb7b5e858bb:/src/views/dolphinview.cpp diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 3437db7a7..f102e9acb 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -1,22 +1,9 @@ -/*************************************************************************** - * Copyright (C) 2006-2009 by Peter Penz * - * Copyright (C) 2006 by Gregor KaliÅ¡nik * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2006-2009 Peter Penz + * SPDX-FileCopyrightText: 2006 Gregor KaliÅ¡nik + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "dolphinview.h" @@ -31,7 +18,6 @@ #include "kitemviews/kitemlistcontroller.h" #include "kitemviews/kitemlistheader.h" #include "kitemviews/kitemlistselectionmanager.h" -#include "renamedialog.h" #include "versioncontrol/versioncontrolobserver.h" #include "viewproperties.h" #include "views/tooltips/tooltipmanager.h" @@ -52,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -363,7 +351,7 @@ void DolphinView::markUrlAsCurrent(const QUrl &url) m_scrollToCurrentItem = true; } -void DolphinView::selectItems(const QRegExp& pattern, bool enabled) +void DolphinView::selectItems(const QRegularExpression ®exp, bool enabled) { const KItemListSelectionManager::SelectionMode mode = enabled ? KItemListSelectionManager::Select @@ -372,7 +360,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) @@ -637,8 +625,9 @@ void DolphinView::renameSelectedItems() connect(m_view, &DolphinItemListView::roleEditingFinished, this, &DolphinView::slotRoleEditingFinished); } else { - RenameDialog* dialog = new RenameDialog(this, items); - connect(dialog, &RenameDialog::renamingFinished, this, &DolphinView::slotRenameDialogRenamingFinished); + KIO::RenameFileDialog* dialog = new KIO::RenameFileDialog(items, this); + connect(dialog, &KIO::RenameFileDialog::renamingFinished, + this, &DolphinView::slotRenameDialogRenamingFinished); dialog->open(); } @@ -677,19 +666,40 @@ void DolphinView::deleteSelectedItems() } } -void DolphinView::cutSelectedItems() +void DolphinView::cutSelectedItemsToClipboard() { QMimeData* mimeData = selectionMimeData(); KIO::setClipboardDataCut(mimeData, true); QApplication::clipboard()->setMimeData(mimeData); } -void DolphinView::copySelectedItems() +void DolphinView::copySelectedItemsToClipboard() { QMimeData* mimeData = selectionMimeData(); QApplication::clipboard()->setMimeData(mimeData); } +void DolphinView::copySelectedItems(const KFileItemList &selection, const QUrl &destinationUrl) +{ + KIO::CopyJob* job = KIO::copy(selection.urlList(), destinationUrl, KIO::DefaultFlags); + KJobWidgets::setWindow(job, this); + + connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult); + connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotCopyingDone); + KIO::FileUndoManager::self()->recordCopyJob(job); +} + +void DolphinView::moveSelectedItems(const KFileItemList &selection, const QUrl &destinationUrl) +{ + KIO::CopyJob* job = KIO::move(selection.urlList(), destinationUrl, KIO::DefaultFlags); + KJobWidgets::setWindow(job, this); + + connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult); + connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotCopyingDone); + KIO::FileUndoManager::self()->recordCopyJob(job); + +} + void DolphinView::paste() { pasteToUrl(url()); @@ -703,6 +713,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 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 " copy" + if (extension.isEmpty()) { + duplicateURL.setPath(originalDirectoryPath + i18nc(" copy", "%1 copy", originalFileName)); + // There's an extension; new filename is " copy." + } 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(" copy", "%1 copy", originalFilenameWithoutExtension) + originalExtension); + } + + KIO::CopyJob* job = KIO::copyAs(originalURL, duplicateURL); + KJobWidgets::setWindow(job, this); + + if (job) { + newSelection << duplicateURL; + KIO::FileUndoManager::self()->recordCopyJob(job); + } + } + + forceUrlsSelection(newSelection.first(), newSelection); +} + void DolphinView::stopLoading() { m_model->cancelDirectoryLoading(); @@ -1083,7 +1140,7 @@ void DolphinView::dropUrls(const QUrl &destUrl, QDropEvent *dropEvent, QWidget * KIO::DropJob* job = DragAndDropHelper::dropUrls(destUrl, dropEvent, dropWidget); if (job) { - connect(job, &KIO::DropJob::result, this, &DolphinView::slotPasteJobResult); + connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult); if (destUrl == url()) { // Mark the dropped urls as selected. @@ -1136,6 +1193,11 @@ void DolphinView::slotSelectedItemTextPressed(int index) } } +void DolphinView::slotCopyingDone(KIO::Job *, const QUrl &, const QUrl &to) +{ + slotItemCreated(to); +} + void DolphinView::slotItemCreated(const QUrl& url) { if (m_markFirstNewlySelectedItemAsCurrent) { @@ -1145,13 +1207,13 @@ void DolphinView::slotItemCreated(const QUrl& url) m_selectedUrls << url; } -void DolphinView::slotPasteJobResult(KJob *job) +void DolphinView::slotJobResult(KJob *job) { if (job->error()) { emit errorMessage(job->errorString()); } if (!m_selectedUrls.isEmpty()) { - m_selectedUrls << KDirModel::simplifiedUrlList(m_selectedUrls); + m_selectedUrls = KDirModel::simplifiedUrlList(m_selectedUrls); } } @@ -1444,13 +1506,31 @@ void DolphinView::calculateItemCount(int& fileCount, KIO::filesize_t& totalFileSize) const { const int itemCount = m_model->count(); + + bool countFileSize = true; + + if (!m_model->rootItem().url().isValid()) { + return; + } + + // In case we have a precomputed value + const auto job = KIO::statDetails(m_model->rootItem().url(), KIO::StatJob::SourceSide, KIO::StatRecursiveSize, KIO::HideProgressInfo); + job->exec(); + const auto entry = job->statResult(); + if (entry.contains(KIO::UDSEntry::UDS_RECURSIVE_SIZE)) { + totalFileSize = static_cast(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(); + } } } } @@ -1782,7 +1862,7 @@ void DolphinView::pasteToUrl(const QUrl& url) m_clearSelectionBeforeSelectingNewItems = true; m_markFirstNewlySelectedItemAsCurrent = true; connect(job, &KIO::PasteJob::itemCreated, this, &DolphinView::slotItemCreated); - connect(job, &KIO::PasteJob::result, this, &DolphinView::slotPasteJobResult); + connect(job, &KIO::PasteJob::result, this, &DolphinView::slotJobResult); } QList DolphinView::simplifiedSelectedUrls() const @@ -1856,3 +1936,21 @@ void DolphinView::forceUrlsSelection(const QUrl& current, const QList& sel markUrlAsCurrent(current); markUrlsAsSelected(selected); } + +void DolphinView::copyPathToClipboard() +{ + const KFileItemList list = selectedItems(); + if (list.isEmpty()) { + return; + } + const KFileItem& item = list.at(0); + QString path = item.localPath(); + if (path.isEmpty()) { + path = item.url().toDisplayString(); + } + QClipboard* clipboard = QApplication::clipboard(); + if (clipboard == nullptr) { + return; + } + clipboard->setText(path); +}