File indexing completed on 2024-04-14 04:52:00

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 
0011 #ifndef TRANSFERHANDLER_H
0012 #define TRANSFERHANDLER_H
0013 
0014 #include <QVariant>
0015 
0016 #include "handler.h"
0017 #include "kget_export.h"
0018 #include "transfer.h"
0019 #include "transfergroup.h"
0020 
0021 class QAction;
0022 
0023 class KGetKJobAdapter;
0024 
0025 /**
0026  * Class TransferHandler:
0027  *
0028  * --- Overview ---
0029  * This class is the representation of a Transfer object from the views'
0030  * perspective (proxy pattern). In fact the views never handle directly the
0031  * Transfer objects themselves (because this would break the model/view policy).
0032  * As a general rule, all the code strictly related to the views should placed
0033  * here (and not in the transfer implementation).
0034  * Here we provide the same api available in the transfer class, but we change
0035  * the implementation of some methods.
0036  * Let's give an example about this:
0037  * The start() function of a specific Transfer (let's say TransferKio) is a
0038  * low level command that really makes the transfer start and should therefore
0039  * be executed only by the scheduler.
0040  * The start() function in this class is implemented in another way, since
0041  * it requests the scheduler to execute the start command to this specific transfer.
0042  * At this point the scheduler will evaluate this request and execute, if possible,
0043  * the start() function directly in the TransferKio.
0044  */
0045 
0046 class KGET_EXPORT TransferHandler : public Handler
0047 {
0048     Q_OBJECT
0049     friend class KGet;
0050     friend class TransferTreeModel;
0051     friend class Transfer;
0052     friend class TransferFactory;
0053     friend class TransferGroupHandler;
0054 
0055 public:
0056     typedef Transfer::ChangesFlags ChangesFlags;
0057 
0058     TransferHandler(Transfer *parent, Scheduler *scheduler);
0059 
0060     ~TransferHandler() override;
0061 
0062     Job::Status status() const
0063     {
0064         return m_transfer->status();
0065     }
0066     Job::Error error() const
0067     {
0068         return m_transfer->error();
0069     }
0070     Job::Status startStatus() const
0071     {
0072         return m_transfer->startStatus();
0073     }
0074     int elapsedTime() const;
0075     int remainingTime() const;
0076 
0077     void resolveError(int errorId)
0078     {
0079         m_transfer->resolveError(errorId);
0080     }
0081 
0082     /**
0083      * Returns the capabilities the Transfer supports
0084      */
0085     Transfer::Capabilities capabilities() const;
0086 
0087     /**
0088      * Tries to repair file
0089      * @param file the file of a download that should be repaired,
0090      * if not defined all files of a download are going to be repaired
0091      * @return true if a repair started, false if it was not nescessary
0092      */
0093     bool repair(const QUrl &file = QUrl())
0094     {
0095         return m_transfer->repair(file);
0096     }
0097 
0098     /**
0099      * @return the transfer's group handler
0100      */
0101     TransferGroupHandler *group() const
0102     {
0103         return m_transfer->group()->handler();
0104     }
0105 
0106     /**
0107      * @return the source url
0108      */
0109     const QUrl &source() const
0110     {
0111         return m_transfer->source();
0112     }
0113 
0114     /**
0115      * @return the dest url
0116      */
0117     const QUrl &dest() const
0118     {
0119         return m_transfer->dest();
0120     }
0121 
0122     /**
0123      * @returns all files of this transfer
0124      */
0125     QList<QUrl> files() const
0126     {
0127         return m_transfer->files();
0128     }
0129 
0130     /**
0131      * @returns the directory the Transfer will be stored to
0132      */
0133     QUrl directory() const
0134     {
0135         return m_transfer->directory();
0136     }
0137 
0138     /**
0139      * Move the download to the new destination
0140      * @param newDirectory is a directory where the download should be stored
0141      * @returns true if newDestination can be used
0142      */
0143     bool setDirectory(const QUrl &newDirectory)
0144     {
0145         return m_transfer->setDirectory(newDirectory);
0146     }
0147 
0148     /**
0149      * The mirrors that are available
0150      * bool if it is used, int how many parallel connections are allowed
0151      * to the mirror
0152      * @param file the file for which the availableMirrors should be get
0153      */
0154     QHash<QUrl, QPair<bool, int>> availableMirrors(const QUrl &file) const
0155     {
0156         return m_transfer->availableMirrors(file);
0157     }
0158 
0159     /**
0160      * Set the mirrors, int the number of parallel connections to the mirror
0161      * bool if the mirror should be used
0162      * @param file the file for which the availableMirrors should be set
0163      * @param mirrors the mirror list
0164      */
0165     void setAvailableMirrors(const QUrl &file, const QHash<QUrl, QPair<bool, int>> &mirrors)
0166     {
0167         m_transfer->setAvailableMirrors(file, mirrors);
0168     }
0169 
0170     /**
0171      * @return the total size of the transfer in bytes
0172      */
0173     KIO::filesize_t totalSize() const;
0174 
0175     /**
0176      * @return the downloaded size of the transfer in bytes
0177      */
0178     KIO::filesize_t downloadedSize() const;
0179 
0180     /**
0181      * @return the uploaded size of the transfer in bytes
0182      */
0183     KIO::filesize_t uploadedSize() const;
0184 
0185     /**
0186      * @return the progress percentage of the transfer
0187      */
0188     int percent() const;
0189 
0190     /**
0191      * @return the download speed of the transfer in bytes/sec
0192      */
0193     int downloadSpeed() const;
0194 
0195     /**
0196      * @return the average download speed of the transfer in bytes/sec
0197      */
0198     int averageDownloadSped() const;
0199 
0200     /**
0201      * @return the upload speed of the transfer in bytes/sec
0202      */
0203     int uploadSpeed() const;
0204 
0205     /**
0206      * Set an UploadLimit for the transfer
0207      * @note this UploadLimit is not visible in the GUI
0208      * @param ulLimit the upload limit
0209      * @param limit the type of the upload Limit
0210      */
0211     void setUploadLimit(int ulLimit, Transfer::SpeedLimit limit)
0212     {
0213         m_transfer->setUploadLimit(ulLimit, limit);
0214     }
0215 
0216     /**
0217      * Set a DownloadLimit for the transfer
0218      * @note this DownloadLimit is not visible in the GUI
0219      * @param dlLimit download limit
0220      * @param limit the type of the download Limit
0221      */
0222     void setDownloadLimit(int dlLimit, Transfer::SpeedLimit limit)
0223     {
0224         m_transfer->setDownloadLimit(dlLimit, limit);
0225     }
0226 
0227     /**
0228      * @return the upload Limit of the transfer in KiB
0229      */
0230     int uploadLimit(Transfer::SpeedLimit limit) const
0231     {
0232         return m_transfer->uploadLimit(limit);
0233     }
0234 
0235     /**
0236      * @return the download Limit of the transfer in KiB
0237      */
0238     int downloadLimit(Transfer::SpeedLimit limit) const
0239     {
0240         return m_transfer->downloadLimit(limit);
0241     }
0242 
0243     /**
0244      * Set the maximum share-ratio
0245      * @param ratio the new maximum share-ratio
0246      */
0247     void setMaximumShareRatio(double ratio)
0248     {
0249         m_transfer->setMaximumShareRatio(ratio);
0250     }
0251 
0252     /**
0253      * @return the maximum share-ratio
0254      */
0255     double maximumShareRatio()
0256     {
0257         return m_transfer->maximumShareRatio();
0258     }
0259 
0260     /**
0261      * Recalculate the share ratio
0262      */
0263     void checkShareRatio()
0264     {
0265         m_transfer->checkShareRatio();
0266     }
0267 
0268     /**
0269      * @return a string describing the current transfer status
0270      */
0271     QString statusText() const
0272     {
0273         return m_transfer->statusText();
0274     }
0275 
0276     /**
0277      * @return an icon name associated with the current transfer status
0278      */
0279     QString statusIconName() const
0280     {
0281         return m_transfer->statusIconName();
0282     }
0283 
0284     /**
0285      * @returns the data associated to this Transfer item. This is
0286      * necessary to make the interview model/view work
0287      */
0288     QVariant data(int column) override;
0289 
0290     /**
0291      * @returns the number of columns associated to the transfer's data
0292      */
0293     int columnCount() const
0294     {
0295         return 6;
0296     }
0297 
0298     /**
0299      * Selects the current transfer. Selecting transfers means that all
0300      * the actions executed from the gui will apply also to the current
0301      * transfer.
0302      *
0303      * @param select if true the current transfer is selected
0304      *               if false the current transfer is deselected
0305      */
0306     void setSelected(bool select);
0307 
0308     /**
0309      * @returns true if the current transfer is selected
0310      * @returns false otherwise
0311      */
0312     bool isSelected() const;
0313 
0314     /**
0315      * Returns the changes flags
0316      */
0317     ChangesFlags changesFlags() const;
0318 
0319     /**
0320      * @returns a list of a actions, which are associated with this TransferHandler
0321      */
0322     QList<QAction *> contextActions();
0323 
0324     /**
0325      * @returns a list of the transfer's factory's actions
0326      */
0327     QList<QAction *> factoryActions();
0328 
0329     /**
0330      * @returns the object path that will be shown in the DBUS interface
0331      */
0332     QString dBusObjectPath()
0333     {
0334         return m_dBusObjectPath;
0335     }
0336 
0337     /**
0338      * @returns the kJobAdapter object
0339      */
0340     KGetKJobAdapter *kJobAdapter()
0341     {
0342         return m_kjobAdapter;
0343     }
0344 
0345     /**
0346      * @returns a pointer to the FileModel containing all files of this download
0347      */
0348     virtual FileModel *fileModel()
0349     {
0350         return m_transfer->fileModel();
0351     }
0352 
0353     /**
0354      * @param file for which to get the verifier
0355      * @return Verifier that allows you to add checksums manually verify a file etc.
0356      */
0357     virtual Verifier *verifier(const QUrl &file)
0358     {
0359         return m_transfer->verifier(file);
0360     }
0361 
0362     /**
0363      * @param file for which to get the signature
0364      * @return Signature that allows you to add signatures and verify them
0365      */
0366     virtual Signature *signature(const QUrl &file)
0367     {
0368         return m_transfer->signature(file);
0369     }
0370 
0371 public Q_SLOTS:
0372     /**
0373      * These are all Job-related functions
0374      */
0375     void start() override;
0376     void stop() override;
0377 
0378 Q_SIGNALS:
0379     /**
0380      * Emitted when the capabilities of the Transfer change
0381      */
0382     void capabilitiesChanged();
0383     void transferChangedEvent(TransferHandler *transfer, TransferHandler::ChangesFlags flags);
0384 
0385 private:
0386     /**
0387      * This functions gets called just before the handler is deleted
0388      */
0389     void destroy();
0390 
0391     /**
0392      * Sets a change flag in the ChangesFlags variable.
0393      *
0394      * @param change The TransferChange flag to be set
0395      * @param notifyModel if false the handler will not post an event to the model,
0396      * if true the handler will post an event to the model
0397      */
0398     void setTransferChange(ChangesFlags change, bool notifyModel = false);
0399 
0400     /**
0401      * Resets the changes flags
0402      */
0403     void resetChangesFlags();
0404 
0405     Transfer *m_transfer;
0406 
0407     KGetKJobAdapter *m_kjobAdapter;
0408 
0409     QString m_dBusObjectPath;
0410 
0411     ChangesFlags m_changesFlags;
0412 };
0413 
0414 #endif