File indexing completed on 2024-04-28 04:57:36

0001 /***************************************************************************
0002  *   Copyright (C) 2009 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 #ifndef FILEHANDLER
0021 #define FILEHANDLER
0022 
0023 #include <QMutex>
0024 #include <QThread>
0025 
0026 #include <KIO/UDSEntry>
0027 
0028 #include "metalinker.h"
0029 
0030 struct FileData {
0031     QUrl url;
0032     KGetMetalink::File file;
0033 };
0034 
0035 /**
0036  * Calculates the checksum and partial checksums for files and adds KGetMetalink::Resources and
0037  * KGetMetalink::CommonData to the resulting KGetMetalink::File
0038  */
0039 class FileHandlerThread : public QThread
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     FileHandlerThread(QObject *parent = nullptr);
0045     ~FileHandlerThread() override;
0046 
0047     void setData(const QList<FileData> &files,
0048                  const QStringList &types,
0049                  bool createPartial,
0050                  const KGetMetalink::Resources &tempResources,
0051                  const KGetMetalink::CommonData &tempCommonData);
0052 
0053 Q_SIGNALS:
0054     void fileResult(const KGetMetalink::File file);
0055 
0056 protected:
0057     void run() override;
0058 
0059 private:
0060     QMutex mutex;
0061     bool abort;
0062     QList<QList<FileData>> m_files;
0063     QList<QStringList> m_types;
0064     QList<bool> m_createPartial;
0065     QList<KGetMetalink::Resources> m_tempResources;
0066     QList<KGetMetalink::CommonData> m_tempCommonDatas;
0067 };
0068 
0069 /**
0070  * Gets the name and the size of the files of a list of urls, also enters
0071  * directories recursively if needed
0072  */
0073 class DirectoryHandler : public QObject
0074 {
0075     Q_OBJECT
0076 
0077 public:
0078     DirectoryHandler(QObject *parent);
0079     ~DirectoryHandler() override;
0080 
0081     /**
0082      * Returns the handled files and clears the member, call this after finished is emitted
0083      */
0084     QList<FileData> takeFiles();
0085 
0086 public Q_SLOTS:
0087     /**
0088      * The files the FileHandler should handle, the urls can also be urls to directories
0089      * then the files of these directories will be got recursively
0090      */
0091     void slotFiles(const QList<QUrl> &files);
0092 
0093 Q_SIGNALS:
0094     void finished();
0095 
0096 private Q_SLOTS:
0097     void slotDirEntries(KIO::Job *job, const KIO::UDSEntryList &entries);
0098 
0099     /**
0100      * Finished one of the jobs to get all files of a directory recursively
0101      */
0102     void slotFinished(KJob *job);
0103 
0104 private:
0105     /**
0106      * Checks if all urls have been parsed already and emits finished in that case
0107      */
0108     void evaluateFileProcess();
0109 
0110 private:
0111     bool m_allJobsStarted;
0112     QHash<KJob *, QUrl> m_jobs;
0113     QList<FileData> m_files;
0114 };
0115 
0116 #endif