File indexing completed on 2023-12-03 08:26:47

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