File indexing completed on 2024-05-19 05:00:08

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Manolo Valdes <nolis71cu@gmail.com>
0004    Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #ifndef KGET_MULTISEGKIODATASOURCE_H
0013 #define KGET_MULTISEGKIODATASOURCE_H
0014 
0015 #include "core/transferdatasource.h"
0016 
0017 class Segment;
0018 
0019 class MultiSegKioDataSource : public TransferDataSource
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     MultiSegKioDataSource(const QUrl &srcUrl, QObject *parent);
0025     ~MultiSegKioDataSource() override;
0026 
0027     void start() override;
0028     void stop() override;
0029 
0030     void findFileSize(KIO::fileoffset_t segmentSize) override;
0031     void addSegments(const QPair<KIO::fileoffset_t, KIO::fileoffset_t> &segmentSize, const QPair<int, int> &segmentRange) override;
0032     QPair<int, int> removeConnection() override;
0033     QList<QPair<int, int>> assignedSegments() const override;
0034     int countUnfinishedSegments() const override;
0035     QPair<int, int> split() override;
0036 
0037     void setSupposedSize(KIO::filesize_t supposedSize) override;
0038     int currentSegments() const override;
0039 
0040 private Q_SLOTS:
0041     void slotSpeed(ulong speed) override;
0042     void slotFinishedSegment(Segment *segment, int segmentNum, bool connectionFinished);
0043     void slotRestartBrokenSegment();
0044 
0045     /**
0046      * There was an error while downloading segment, the number of connections this
0047      * TransferDataSource uses simultaneously gets reduced
0048      */
0049     void slotError(Segment *segment, const QString &errorText, Transfer::LogLevel logLevel);
0050 
0051     /**the following slots are there to check if the size reported by the mirror
0052      * Checks if the sizre reported by the mirror is correct
0053      */
0054     void slotTotalSize(KIO::filesize_t size, const QPair<int, int> &range = qMakePair(-1, -1));
0055 
0056     void slotCanResume();
0057 
0058     void slotFinishedDownload(KIO::filesize_t size);
0059 
0060     void slotUrlChanged(const QUrl &url);
0061 
0062 private:
0063     Segment *mostUnfinishedSegments(int *unfinished = nullptr) const;
0064     bool tryMerge(const QPair<KIO::fileoffset_t, KIO::fileoffset_t> &segmentSize, const QPair<int, int> &segmentRange);
0065 
0066 private:
0067     QList<Segment *> m_segments;
0068     KIO::filesize_t m_size;
0069     bool m_canResume;
0070     bool m_started;
0071 };
0072 
0073 #endif