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

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 CHECKSUMSEARCHTRANSFERDATASOURCE_H
0021 #define CHECKSUMSEARCHTRANSFERDATASOURCE_H
0022 
0023 #include "core/transferdatasource.h"
0024 
0025 #include <KIO/Job>
0026 #include <KIO/UDSEntry>
0027 
0028 class ChecksumSearch;
0029 class ChecksumSearchTransferDataSource;
0030 
0031 /**
0032  * ChecksumSearchController downloads the baseUrl of files one want to get checksums for
0033  * e.g. www.example.com/directory/file.zip the baseUrl would be www.example.com/directory/
0034  * the result is cached so not downloaded multiple times
0035  */
0036 class ChecksumSearchController : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040     ChecksumSearchController(QObject *parent = nullptr);
0041     ~ChecksumSearchController() override;
0042 
0043     /**
0044      * Registers a search, downloads baseUrl if that has not be downloaded and then
0045      * calls gotBaseUrl for any ChecksumSearchTransferDataSource that was registered
0046      * for this baseUrl
0047      * @param search ChecksumSearchTransferDataSource to register to baseUrl
0048      * @param baseUrl that is being downloaded
0049      */
0050     void registerSearch(ChecksumSearchTransferDataSource *search, const QUrl &baseUrl);
0051 
0052     /**
0053      * Unregisters a search, do that e.g. if the search gets destroyed
0054      * @param search ChecksumSearchTransferDataSource to unregister to baseUrl
0055      * @param baseUrl can be empty, in that case search is unregistered for any url
0056      */
0057     void unregisterSearch(ChecksumSearchTransferDataSource *search, const QUrl &baseUrl = QUrl());
0058 
0059 private Q_SLOTS:
0060     void slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries);
0061     void slotResult(KJob *job);
0062 
0063 private:
0064     QMultiHash<QUrl, ChecksumSearchTransferDataSource *> m_searches;
0065     QHash<QUrl, QUrl> m_finished;
0066     QHash<KJob *, QPair<QUrl, QUrl>> m_jobs;
0067 };
0068 
0069 class ChecksumSearchTransferDataSource : public TransferDataSource
0070 {
0071     Q_OBJECT
0072 public:
0073     ChecksumSearchTransferDataSource(const QUrl &srcUrl, QObject *parent);
0074     ~ChecksumSearchTransferDataSource() override;
0075 
0076     void start() override;
0077     void stop() override;
0078     void addSegments(const QPair<KIO::fileoffset_t, KIO::fileoffset_t> &segmentSize, const QPair<int, int> &segmentRange) override;
0079 
0080 private:
0081     void gotBaseUrl(const QUrl &urlToFile);
0082 
0083 private:
0084     QUrl m_src;
0085     static ChecksumSearchController s_controller;
0086 
0087     friend class ChecksumSearchController;
0088 };
0089 
0090 #endif