File indexing completed on 2024-11-10 04:30:01

0001 /**************************************************************************
0002  *   Copyright (C) 2011 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "filedeleter.h"
0021 #include "filedeleter_p.h"
0022 
0023 Q_GLOBAL_STATIC(FileDeleter, fileDeleter)
0024 
0025 FileDeleter::Private::Private()
0026     : QObject(nullptr)
0027 {
0028 }
0029 
0030 FileDeleter::Private::~Private()
0031 {
0032 }
0033 
0034 bool FileDeleter::Private::isFileBeingDeleted(const QUrl &dest) const
0035 {
0036     return m_jobs.contains(dest);
0037 }
0038 
0039 KJob *FileDeleter::Private::deleteFile(const QUrl &dest, QObject *receiver, const char *method)
0040 {
0041     QHash<QUrl, KJob *>::iterator it = m_jobs.find(dest);
0042     if (it == m_jobs.end()) {
0043         KJob *job = KIO::del(dest, KIO::HideProgressInfo);
0044         it = m_jobs.insert(dest, job);
0045         connect(*it, &KJob::result, this, &FileDeleter::Private::slotResult);
0046     }
0047 
0048     if (receiver && method) {
0049         // make sure that there is just one connection
0050         disconnect(*it, SIGNAL(result(KJob *)), receiver, method);
0051         connect(*it, SIGNAL(result(KJob *)), receiver, method);
0052     }
0053 
0054     return *it;
0055 }
0056 
0057 void FileDeleter::Private::slotResult(KJob *job)
0058 {
0059     auto *deleteJob = static_cast<KIO::DeleteJob *>(job);
0060     m_jobs.remove(deleteJob->urls().first());
0061 }
0062 
0063 FileDeleter::FileDeleter()
0064     : d(new Private)
0065 {
0066 }
0067 
0068 FileDeleter::~FileDeleter()
0069 {
0070     delete d;
0071 }
0072 
0073 KJob *FileDeleter::deleteFile(const QUrl &dest, QObject *receiver, const char *method)
0074 {
0075     return fileDeleter->d->deleteFile(dest, receiver, method);
0076 }
0077 
0078 bool FileDeleter::isFileBeingDeleted(const QUrl &dest)
0079 {
0080     return fileDeleter->d->isFileBeingDeleted(dest);
0081 }
0082 
0083 #include "moc_filedeleter_p.cpp"