File indexing completed on 2024-11-24 05:00:27

0001 /*
0002     SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "trash.h"
0008 
0009 #include <QApplication>
0010 #include <QFileInfo>
0011 
0012 #include <KIO/DeleteOrTrashJob>
0013 
0014 Trash::Trash(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 void Trash::trashUrls(const QList<QUrl> &urls)
0020 {
0021     using Iface = KIO::AskUserActionInterface;
0022     auto *job = new KIO::DeleteOrTrashJob(urls, Iface::Trash, Iface::DefaultConfirmation, this);
0023     job->start();
0024 }
0025 
0026 void Trash::emptyTrash()
0027 {
0028     using Iface = KIO::AskUserActionInterface;
0029     auto *job = new KIO::DeleteOrTrashJob({}, Iface::EmptyTrash, Iface::DefaultConfirmation, this);
0030     job->start();
0031 }
0032 
0033 bool Trash::canBeTrashed(const QUrl &url) const
0034 {
0035     return url.isValid() && url.isLocalFile() && QFileInfo(url.toLocalFile()).isWritable();
0036 }
0037 
0038 QList<QUrl> Trash::trashableUrls(const QList<QUrl> &urls) const
0039 {
0040     QList<QUrl> validUrls = urls;
0041 
0042     QMutableListIterator<QUrl> it(validUrls);
0043 
0044     while (it.hasNext()) {
0045         if (!canBeTrashed(it.next())) {
0046             it.remove();
0047         }
0048     }
0049 
0050     return validUrls;
0051 }