File indexing completed on 2024-04-28 15:40:25

0001 /* SPDX-FileCopyrightText: 2012-2020 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "DeleteFiles.h"
0007 
0008 #include "ShowBusyCursor.h"
0009 
0010 #include <DB/ImageDB.h>
0011 #include <MainWindow/DirtyIndicator.h>
0012 #include <MainWindow/Window.h>
0013 #include <kpathumbnails/ThumbnailCache.h>
0014 
0015 #include <KIO/CopyJob>
0016 #include <KIO/DeleteJob>
0017 #include <KJob>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <QUrl>
0021 
0022 namespace Utilities
0023 {
0024 
0025 DeleteFiles *DeleteFiles::s_instance;
0026 
0027 bool DeleteFiles::deleteFiles(const DB::FileNameList &files, DeleteMethod method)
0028 {
0029     if (!s_instance)
0030         s_instance = new DeleteFiles;
0031     return s_instance->deleteFilesPrivate(files, method);
0032 }
0033 
0034 void DeleteFiles::slotKIOJobCompleted(KJob *job)
0035 {
0036     if (job->error())
0037         KMessageBox::error(MainWindow::Window::theMainWindow(), job->errorString(), i18n("Error Deleting Files"));
0038 }
0039 
0040 bool DeleteFiles::deleteFilesPrivate(const DB::FileNameList &files, DeleteMethod method)
0041 {
0042     Utilities::ShowBusyCursor dummy;
0043 
0044     DB::FileNameList filenamesToRemove;
0045     QList<QUrl> filesToDelete;
0046 
0047     for (const DB::FileName &fileName : files) {
0048 
0049         if (DB::ImageInfo::imageOnDisk(fileName)) {
0050             if (method == DeleteFromDisk || method == MoveToTrash) {
0051                 filesToDelete.append(QUrl::fromLocalFile(fileName.absolute()));
0052                 filenamesToRemove.append(fileName);
0053             } else {
0054                 filenamesToRemove.append(fileName);
0055             }
0056         } else
0057             filenamesToRemove.append(fileName);
0058     }
0059 
0060     MainWindow::Window::theMainWindow()->thumbnailCache()->removeThumbnails(files);
0061 
0062     if (method == DeleteFromDisk || method == MoveToTrash) {
0063         KJob *job;
0064         if (method == MoveToTrash)
0065             job = KIO::trash(filesToDelete);
0066         else
0067             job = KIO::del(filesToDelete);
0068         connect(job, &KJob::result, this, &DeleteFiles::slotKIOJobCompleted);
0069     }
0070 
0071     if (!filenamesToRemove.isEmpty()) {
0072         if (method == MoveToTrash || method == DeleteFromDisk)
0073             DB::ImageDB::instance()->deleteList(filenamesToRemove);
0074         else
0075             DB::ImageDB::instance()->addToBlockList(filenamesToRemove);
0076         MainWindow::DirtyIndicator::markDirty();
0077         return true;
0078     } else
0079         return false;
0080 }
0081 
0082 } // namespace Utilities
0083 // vi:expandtab:tabstop=4 shiftwidth=4:
0084 
0085 #include "moc_DeleteFiles.cpp"