File indexing completed on 2024-04-21 04:57:04

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2004 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2008 - 2011 Lukas Appelhans <l.appelhans@gmx.de>
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 TRANSFER_H
0013 #define TRANSFER_H
0014 
0015 #include "job.h"
0016 #include "kget_export.h"
0017 
0018 #include <QElapsedTimer>
0019 #include <QUrl>
0020 
0021 #include <KIO/Global>
0022 
0023 class QDomElement;
0024 
0025 class Signature;
0026 class TransferHandler;
0027 class TransferFactory;
0028 class TransferGroup;
0029 class Scheduler;
0030 class TransferTreeModel;
0031 class FileModel;
0032 class Verifier;
0033 
0034 class KGET_EXPORT Transfer : public Job
0035 {
0036     Q_OBJECT
0037     friend class TransferHandler;
0038     friend class TransferTreeModel;
0039 
0040 public:
0041     /**
0042      * Here we define the flags that should be shared by all the transfers.
0043      * A transfer should also be able to define additional flags, in the future.
0044      */
0045     enum TransferChange {
0046         Tc_None = 0x00000000,
0047         // These flags respect the Model columns order NOTE: The model only checks the last 8 bits, so all values which need to be updated by the model should
0048         // look like: 0x000000xx
0049         Tc_Source = 0x00000001,
0050         Tc_FileName = 0x00000002,
0051         Tc_Status = 0x00000004,
0052         Tc_TotalSize = 0x00000008,
0053         Tc_Percent = 0x00000010,
0054         Tc_DownloadSpeed = 0x00000020,
0055         Tc_RemainingTime = 0x00000040,
0056         // Misc
0057         Tc_UploadSpeed = 0x00000100,
0058         Tc_UploadLimit = 0x00000200,
0059         Tc_DownloadLimit = 0x00000400,
0060         Tc_CanResume = 0x00000800,
0061         Tc_DownloadedSize = 0x00001000,
0062         Tc_UploadedSize = 0x00002000,
0063         Tc_Log = 0x00004000,
0064         Tc_Group = 0x00008000,
0065         Tc_Selection = 0x00010000,
0066     };
0067 
0068     enum Capability {
0069         Cap_SpeedLimit = 0x00000001,
0070         Cap_MultipleMirrors = 0x00000002,
0071         Cap_Resuming = 0x00000004,
0072         Cap_Renaming = 0x00000008,
0073         Cap_Moving = 0x00000010,
0074         Cap_FindFilesize = 0x00000020,
0075     };
0076     Q_DECLARE_FLAGS(Capabilities, Capability)
0077 
0078     enum LogLevel {
0079         Log_Info,
0080         Log_Warning,
0081         Log_Error,
0082     };
0083 
0084     enum SpeedLimit {
0085         VisibleSpeedLimit = 0x01,
0086         InvisibleSpeedLimit = 0x02,
0087     };
0088 
0089     enum DeleteOption {
0090         DeleteTemporaryFiles = 0x00000001,
0091         DeleteFiles = 0x00000002,
0092     };
0093     Q_DECLARE_FLAGS(DeleteOptions, DeleteOption)
0094     typedef int ChangesFlags;
0095 
0096     Transfer(TransferGroup *parent, TransferFactory *factory, Scheduler *scheduler, const QUrl &src, const QUrl &dest, const QDomElement *e = nullptr);
0097 
0098     ~Transfer() override;
0099 
0100     /**
0101      * Returns the capabilities this Transfer supports
0102      */
0103     Capabilities capabilities() const
0104     {
0105         return m_capabilities;
0106     }
0107 
0108     /**
0109      * This functions gets called whenever a Transfer gets created. As opposed
0110      * to init(), this isn't a virtual function and is not meant to be used in
0111      * transfer plugins
0112      */
0113     void create();
0114 
0115     /**
0116      * This functions gets called whenever a Transfer is going to be deleted. As opposed
0117      * to deinit(), this isn't a virtual function and is not meant to be used in
0118      * transfer plugins
0119      */
0120     void destroy(DeleteOptions options);
0121 
0122     /**
0123      * This function is called after the creation of a Transfer
0124      * In transfer plugins you can put here whatever needs to be initialized
0125      * @note this function creates a NepomukHandler
0126      */
0127     virtual void init();
0128 
0129     /**
0130      * This function is called before the deletion of a Transfer
0131      * In transfer plugins you can put here whatever needs to be deinitialized
0132      */
0133     virtual void deinit(DeleteOptions options)
0134     {
0135         Q_UNUSED(options);
0136     }
0137 
0138     /**
0139      * Tries to repair file
0140      * @param file the file of a download that should be repaired,
0141      * if not defined all files of a download are going to be repaired
0142      * @return true if a repair started, false if it was not necessary
0143      */
0144     virtual bool repair(const QUrl &file = QUrl())
0145     {
0146         Q_UNUSED(file)
0147         return false;
0148     }
0149 
0150     const QUrl &source() const
0151     {
0152         return m_source;
0153     }
0154     const QUrl &dest() const
0155     {
0156         return m_dest;
0157     }
0158 
0159     /**
0160      * @returns all files of this transfer
0161      */
0162     virtual QList<QUrl> files() const
0163     {
0164         return QList<QUrl>() << m_dest;
0165     }
0166 
0167     /**
0168      * @returns the directory the Transfer will be stored to
0169      */
0170     virtual QUrl directory() const
0171     {
0172         return KIO::upUrl(m_dest);
0173     }
0174 
0175     /**
0176      * Move the download to the new destination
0177      * @param newDirectory is a directory where the download should be stored
0178      * @returns true if newDestination can be used
0179      */
0180     virtual bool setDirectory(const QUrl &newDirectory);
0181 
0182     // Transfer status
0183     KIO::filesize_t totalSize() const
0184     {
0185         return m_totalSize;
0186     }
0187     KIO::filesize_t downloadedSize() const
0188     {
0189         return m_downloadedSize;
0190     }
0191     KIO::filesize_t uploadedSize() const
0192     {
0193         return m_uploadedSize;
0194     }
0195     QString statusText() const
0196     {
0197         return m_statusText;
0198     }
0199     QString statusIconName() const
0200     {
0201         return (error().iconName.isEmpty() ? m_statusIconName : error().iconName);
0202     }
0203 
0204     static QString statusText(Job::Status status);
0205     static QString statusIconName(Job::Status status);
0206 
0207     int percent() const
0208     {
0209         return m_percent;
0210     }
0211     int downloadSpeed() const
0212     {
0213         return m_downloadSpeed;
0214     }
0215     int averageDownloadSpeed() const;
0216     int uploadSpeed() const
0217     {
0218         return m_uploadSpeed;
0219     }
0220     int remainingTime() const override
0221     {
0222         return KIO::calculateRemainingSeconds(totalSize(), downloadedSize(), downloadSpeed());
0223     }
0224     int elapsedTime() const override;
0225     bool isStalled() const override
0226     {
0227         return (status() == Job::Running && downloadSpeed() == 0);
0228     }
0229     bool isWorking() const override
0230     {
0231         return downloadSpeed() > 0;
0232     }
0233 
0234     /**
0235      * The mirrors that are available
0236      * bool if it is used, int how many parallel connections are allowed
0237      * to the mirror
0238      * @param file the file for which the availableMirrors should be get
0239      */
0240     virtual QHash<QUrl, QPair<bool, int>> availableMirrors(const QUrl &file) const;
0241 
0242     /**
0243      * Set the mirrors, int the number of parallel connections to the mirror
0244      * bool if the mirror should be used
0245      * @param file the file for which the availableMirrors should be set
0246      * @param mirrors the mirrors
0247      */
0248     virtual void setAvailableMirrors(const QUrl &file, const QHash<QUrl, QPair<bool, int>> &mirrors)
0249     {
0250         Q_UNUSED(file)
0251         Q_UNUSED(mirrors)
0252     }
0253 
0254     /**
0255      * Set the Transfer's UploadLimit
0256      * @note this is not displayed in any GUI, use setVisibleUploadLimit(int) instead
0257      * @param ulLimit upload Limit
0258      * @param limit speed limit
0259      */
0260     void setUploadLimit(int ulLimit, SpeedLimit limit);
0261 
0262     /**
0263      * Set the Transfer's UploadLimit, which are displayed in the GUI
0264      * @note this is not displayed in any GUI, use setVisibleDownloadLimit(int) instead
0265      * @param dlLimit upload Limit
0266      * @param limit speed limit
0267      */
0268     void setDownloadLimit(int dlLimit, SpeedLimit limit);
0269 
0270     /**
0271      * @return the UploadLimit, which is invisible in the GUI
0272      */
0273     int uploadLimit(SpeedLimit limit) const;
0274 
0275     /**
0276      * @return the DownloadLimit, which is invisible in the GUI
0277      */
0278     int downloadLimit(SpeedLimit limit) const;
0279 
0280     /**
0281      * Set the maximum share-ratio
0282      * @param ratio the new maximum share-ratio
0283      */
0284     void setMaximumShareRatio(double ratio);
0285 
0286     /**
0287      * @return the maximum share-ratio
0288      */
0289     double maximumShareRatio()
0290     {
0291         return m_ratio;
0292     }
0293 
0294     /**
0295      * Recalculate the share ratio
0296      */
0297     void checkShareRatio();
0298 
0299     bool isSelected() const
0300     {
0301         return m_isSelected;
0302     }
0303 
0304     /**
0305      * Transfer history
0306      */
0307     const QStringList log() const;
0308 
0309     /**
0310      * The owner group
0311      */
0312     TransferGroup *group() const
0313     {
0314         return (TransferGroup *)m_jobQueue;
0315     }
0316 
0317     /**
0318      * @return the associated TransferHandler
0319      */
0320     TransferHandler *handler();
0321 
0322     /**
0323      * @returns the TransferTreeModel that owns this group
0324      */
0325     TransferTreeModel *model();
0326 
0327     /**
0328      * @returns a pointer to the TransferFactory object
0329      */
0330     TransferFactory *factory() const
0331     {
0332         return m_factory;
0333     }
0334 
0335     /**
0336      * @returns a pointer to the FileModel containing all files of this download
0337      */
0338     virtual FileModel *fileModel()
0339     {
0340         return nullptr;
0341     }
0342 
0343     /**
0344      * @param file for which to get the verifier
0345      * @return Verifier that allows you to add checksums manually verify a file etc.
0346      */
0347     virtual Verifier *verifier(const QUrl &file)
0348     {
0349         Q_UNUSED(file)
0350         return nullptr;
0351     }
0352 
0353     /**
0354      * @param file for which to get the signature
0355      * @return Signature that allows you to add signatures and verify them
0356      */
0357     virtual Signature *signature(const QUrl &file)
0358     {
0359         Q_UNUSED(file)
0360         return nullptr;
0361     }
0362 
0363     /**
0364      * Saves this transfer to the given QDomNode
0365      *
0366      * @param element The pointer to the QDomNode where the transfer will be saved
0367      */
0368     virtual void save(const QDomElement &element);
0369 
0370     /**
0371      * Loads the transfer's info from the QDomElement
0372      *
0373      * @param element The pointer to the QDomNode where info will be loaded from
0374      */
0375     virtual void load(const QDomElement *element);
0376 
0377 Q_SIGNALS:
0378     /**
0379      * Emitted when the capabilities of the Transfer change
0380      */
0381     void capabilitiesChanged();
0382 
0383 public Q_SLOTS:
0384     /**
0385      * Set Transfer history
0386      */
0387     void setLog(const QString &message, Transfer::LogLevel level = Log_Info);
0388 
0389 protected:
0390     /**
0391      * Sets the Job status to jobStatus, the status text to text and
0392      * the status icon to iconName.
0393      */
0394     void setStatus(Job::Status jobStatus, const QString &text = QString(), const QString &iconName = QString());
0395 
0396     /**
0397      * Sets the capabilities and automatically emits capabilitiesChanged
0398      */
0399     void setCapabilities(Capabilities capabilities);
0400 
0401     /**
0402      * Makes the TransferHandler associated with this transfer know that
0403      * a change in this transfer has occurred.
0404      *
0405      * @param change the TransferChange flags to be set
0406      * @param postEvent whether the post event is taken into account
0407      */
0408     virtual void setTransferChange(ChangesFlags change, bool postEvent = false);
0409 
0410     /**
0411      * Function used to set the SpeedLimits to the transfer
0412      */
0413     virtual void setSpeedLimits(int uploadLimit, int downloadLimit)
0414     {
0415         Q_UNUSED(uploadLimit)
0416         Q_UNUSED(downloadLimit)
0417     }
0418 
0419     // --- Transfer information ---
0420     QUrl m_source;
0421     QUrl m_dest;
0422 
0423     QStringList m_log;
0424     KIO::filesize_t m_totalSize;
0425     KIO::filesize_t m_downloadedSize;
0426     KIO::filesize_t m_uploadedSize;
0427     int m_percent;
0428     int m_downloadSpeed;
0429     int m_uploadSpeed;
0430 
0431     int m_uploadLimit;
0432     int m_downloadLimit;
0433 
0434     bool m_isSelected;
0435 
0436 private:
0437     Capabilities m_capabilities;
0438     int m_visibleUploadLimit;
0439     int m_visibleDownloadLimit;
0440     int m_runningSeconds;
0441     double m_ratio;
0442 
0443     QString m_statusText;
0444     QString m_statusIconName;
0445     QElapsedTimer m_runningTime;
0446 
0447     TransferHandler *m_handler;
0448     TransferFactory *m_factory;
0449 };
0450 
0451 Q_DECLARE_OPERATORS_FOR_FLAGS(Transfer::Capabilities)
0452 Q_DECLARE_OPERATORS_FOR_FLAGS(Transfer::DeleteOptions)
0453 
0454 #endif