File indexing completed on 2024-04-28 05:48:12

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include "contextMenuContext.h"
0005 
0006 #include <QClipboard>
0007 #include <QGuiApplication>
0008 #include <QMimeData>
0009 
0010 #include <KIO/DeleteJob>
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <KTerminalLauncherJob>
0014 #include <KUrlMimeData>
0015 
0016 #include "Config.h"
0017 #include "radialMap/map.h"
0018 #include "radialMap/radialMap.h"
0019 
0020 namespace Filelight
0021 {
0022 
0023 void ContextMenuContext::openTerminal(RadialMap::Segment *segment)
0024 {
0025     openTerminal(segment->url());
0026 }
0027 
0028 void ContextMenuContext::openTerminal(const QUrl &url)
0029 {
0030     auto *job = new KTerminalLauncherJob(QString(), this);
0031     job->setWorkingDirectory(url.path());
0032     job->start();
0033 }
0034 
0035 void ContextMenuContext::doNotScan(RadialMap::Segment *segment)
0036 {
0037     doNotScan(segment->url());
0038 }
0039 
0040 void ContextMenuContext::doNotScan(const QUrl &url)
0041 {
0042     if (!Config::instance()->skipList.contains(url.toLocalFile())) {
0043         Config::instance()->skipList.append(url.toLocalFile());
0044         Config::instance()->write();
0045     }
0046 }
0047 
0048 void ContextMenuContext::copyClipboard(RadialMap::Segment *segment)
0049 {
0050     copyClipboard(segment->url());
0051 }
0052 
0053 void ContextMenuContext::copyClipboard(const QUrl &url)
0054 {
0055     auto mime = new QMimeData;
0056     mime->setUrls({url});
0057     KUrlMimeData::exportUrlsToPortal(mime);
0058     QGuiApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
0059 }
0060 
0061 static bool shouldDelete(const QUrl &url, bool isFolder)
0062 {
0063     const QString message = isFolder ? i18n("<qt>The folder at <i>'%1'</i> will be <b>recursively</b> and <b>permanently</b> deleted.</qt>", url.toString())
0064                                      : i18n("<qt><i>'%1'</i> will be <b>permanently</b> deleted.</qt>", url.toString());
0065     const auto userIntention = KMessageBox::warningContinueCancel(nullptr, message, QString(), KGuiItem(i18n("&Delete"), QStringLiteral("edit-delete")));
0066 
0067     return userIntention == KMessageBox::Continue;
0068 }
0069 
0070 void ContextMenuContext::deleteFileFromSegment(RadialMap::Segment *segment)
0071 {
0072     deleteFile(segment->file());
0073 }
0074 
0075 void ContextMenuContext::deleteFile(const std::shared_ptr<File> &file)
0076 {
0077     const auto url = file->url();
0078     const auto isFolder = file->isFolder();
0079 
0080     if (!shouldDelete(url, isFolder)) {
0081         return;
0082     }
0083 
0084     auto job = KIO::del(url);
0085     connect(job, &KJob::finished, this, [this, url, job, file] {
0086         QGuiApplication::restoreOverrideCursor();
0087         setDeleting(false);
0088         if (!job->error()) {
0089             file->parent()->remove(file);
0090             RadialMap::Map::instance()->refresh(Dirty::Layout);
0091         } else {
0092             KMessageBox::error(nullptr, job->errorString(), i18n("Error while deleting"));
0093         }
0094     });
0095     setDeleting(true);
0096     QGuiApplication::setOverrideCursor(Qt::BusyCursor);
0097 }
0098 
0099 void ContextMenuContext::setDeleting(bool status)
0100 {
0101     m_deleting = status;
0102     Q_EMIT deletingChanged();
0103 }
0104 
0105 } // namespace Filelight