File indexing completed on 2024-02-18 04:58:18

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
0005    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 */
0012 #ifndef GROUP_H
0013 #define GROUP_H
0014 
0015 #include <QDebug>
0016 #include <QIcon>
0017 #include <QRegularExpression>
0018 
0019 #include "jobqueue.h"
0020 #include "kget_export.h"
0021 #include "transfer.h"
0022 
0023 class QDomElement;
0024 
0025 class TransferGroupHandler;
0026 class TransferTreeModel;
0027 
0028 /**
0029  * class TransferGroup:
0030  *
0031  * This class abstracts the concept of transfer group by means of which
0032  * the user can sort his transfers into categories.
0033  * By definition, we want each TransferGroup (transfer group) to be a JobQueue.
0034  * Moreover this class calculates information such as:
0035  * - the size obtained by the sum of all the transfer's size
0036  * - the size obtained by the sum of all the transfer's processed size
0037  * - the global progress percentage within the group
0038  * - the global speed within the group
0039  */
0040 class KGET_EXPORT TransferGroup : public JobQueue
0041 {
0042     Q_OBJECT
0043 public:
0044     enum GroupChange {
0045         Gc_None = 0x00000000,
0046         // These flags respect the Model columns order
0047         Gc_GroupName = 0x00000001,
0048         Gc_Status = 0x00000002,
0049         Gc_TotalSize = 0x00000004,
0050         Gc_Percent = 0x00000008,
0051         Gc_UploadSpeed = 0x00000010,
0052         Gc_DownloadSpeed = 0x00000020,
0053         // Misc
0054         Gc_ProcessedSize = 0x00010000
0055     };
0056 
0057     typedef int ChangesFlags;
0058 
0059     TransferGroup(TransferTreeModel *model, Scheduler *parent, const QString &name = QString());
0060 
0061     ~TransferGroup() override;
0062 
0063     /**
0064      * This function is reimplemented by JobQueue::setStatus
0065      *
0066      * @param queueStatus the new JobQueue status
0067      */
0068     void setStatus(Status queueStatus) override;
0069 
0070     /**
0071      * Appends a new transfer to the list of the transfers
0072      *
0073      * @param transfer the transfer to append
0074      */
0075     void append(Transfer *transfer);
0076 
0077     /**
0078      * Appends new transfers to the list of the transfers
0079      *
0080      * @param transfer the transfers to append
0081      */
0082     void append(const QList<Transfer *> &transfer);
0083 
0084     /**
0085      * Prepends a new transfer to the list of the transfers
0086      *
0087      * @param transfer the transfer to prepend
0088      */
0089     void prepend(Transfer *transfer);
0090 
0091     /**
0092      * inserts a transfer to the current group after the given transfer
0093      *
0094      * @param transfer The transfer to add in the current Group
0095      * @param after The transfer after which to add the transfer
0096      */
0097     void insert(Transfer *transfer, Transfer *after);
0098 
0099     /**
0100      * Removes the given transfer from the list of the transfers
0101      *
0102      * @param transfer the transfer to remove
0103      */
0104     void remove(Transfer *transfer);
0105 
0106     /**
0107      * Removes the given transfers from the list of the transfers
0108      *
0109      * @param transfers the transfers to remove
0110      */
0111     void remove(const QList<Transfer *> &transfers);
0112 
0113     /**
0114      * Moves a transfer in the list
0115      *
0116      * @param transfer The transfer to move. Note that this transfer can
0117      *                  belong to other groups. In this situation this
0118      *                  transfer is deleted from the previous group and
0119      *                  moved inside this one.
0120      * @param after The transfer after which we have to move the given one
0121      */
0122     void move(Transfer *transfer, Transfer *after);
0123 
0124     /**
0125      * Finds the first transfer with source src
0126      *
0127      * @param src the url of the source location
0128      *
0129      * @return the transfer pointer if the transfer has been found. Otherwise
0130      * it returns nullptr
0131      */
0132     Transfer *findTransfer(const QUrl &src);
0133 
0134     /**
0135      * Finds the first transfer with destination dest
0136      *
0137      * @param dest the url of the destination location
0138      *
0139      * @return the transfer pointer if the transfer has been found, else return nullptr
0140      */
0141     Transfer *findTransferByDestination(const QUrl &dest);
0142 
0143     /**
0144      * @returns the Job in the queue at the given index i
0145      */
0146     Transfer *operator[](int i) const
0147     {
0148         return (Transfer *)((*(JobQueue *)this)[i]);
0149     }
0150 
0151     /**Set the group name
0152      * @param name group name
0153      */
0154     void setName(const QString &name)
0155     {
0156         m_name = name;
0157     }
0158 
0159     /**
0160      * @return the group name
0161      */
0162     const QString &name()
0163     {
0164         return m_name;
0165     }
0166 
0167     /**
0168      * @return the sum of the sizes of the transfers belonging to
0169      * this group
0170      */
0171     int totalSize() const
0172     {
0173         return m_totalSize;
0174     }
0175 
0176     /**
0177      * @return the sum of the downloaded sizes of the transfers
0178      * belonging to this group
0179      */
0180     int downloadedSize() const
0181     {
0182         return m_downloadedSize;
0183     }
0184 
0185     /**
0186      * @return the sum of the uploaded sizes of the transfers
0187      * belonging to this group
0188      */
0189     int uploadedSize() const
0190     {
0191         return m_uploadedSize;
0192     }
0193 
0194     /**
0195      * @return the progress percentage
0196      */
0197     int percent() const
0198     {
0199         return m_percent;
0200     }
0201 
0202     /**
0203      * @return the sum of the download speeds of the running transfers
0204      * belonging this group
0205      */
0206     int downloadSpeed();
0207 
0208     /**
0209      * @return the sum of the download speeds of the running transfers
0210      * belonging this group
0211      */
0212     int uploadSpeed();
0213 
0214     /**
0215      * Set a default Folder for the group
0216      * @param folder the new default folder
0217      */
0218     void setDefaultFolder(QString folder)
0219     {
0220         m_defaultFolder = folder;
0221     }
0222 
0223     /**
0224      * @return the groups default folder
0225      */
0226     QString defaultFolder()
0227     {
0228         return m_defaultFolder;
0229     }
0230 
0231     /**
0232      * Sets the regular expression of the group
0233      * @param regExp the regular expression
0234      */
0235     void setRegExp(const QRegularExpression &regExp)
0236     {
0237         m_regExp = regExp;
0238     }
0239 
0240     /**
0241      * @returns the regular expression of the group
0242      */
0243     QRegularExpression regExp()
0244     {
0245         return m_regExp;
0246     }
0247 
0248     /**
0249      * @return true if the group supports SpeedLimits
0250      */
0251     bool supportsSpeedLimits();
0252 
0253     /**
0254      * Set a Download-Limit for the group
0255      * @param dlLimit the new download limit
0256      * @param limit the type of the new download limit
0257      * @note if limit is 0, no download-limit is set
0258      */
0259     void setDownloadLimit(int dlLimit, Transfer::SpeedLimit limit);
0260 
0261     /**
0262      * @return the group's Download-Limit
0263      */
0264     int downloadLimit(Transfer::SpeedLimit limit) const;
0265 
0266     /**
0267      * Set a Upload-Limit for the group
0268      * @param ulLimit the new upload limit
0269      * @param limit the type of the new upload-limit
0270      * @note if limit is 0, no upload-limit is set
0271      */
0272     void setUploadLimit(int ulLimit, Transfer::SpeedLimit limit);
0273 
0274     /**
0275      * @return the group's Upload-Limit
0276      */
0277     int uploadLimit(Transfer::SpeedLimit limit) const;
0278 
0279     /**
0280      * Set the group's icon
0281      * @param name the icon's name
0282      */
0283     void setIconName(const QString &name)
0284     {
0285         m_iconName = name;
0286     }
0287 
0288     /**
0289      * @returns the group's icon's name
0290      */
0291     QString iconName() const
0292     {
0293         return m_iconName;
0294     }
0295 
0296     /**
0297      * @return the group's icon
0298      */
0299     QPixmap pixmap()
0300     {
0301         return QIcon::fromTheme(m_iconName).pixmap(32);
0302     }
0303 
0304     /**
0305      * @return the handler associated with this group
0306      */
0307     TransferGroupHandler *handler() const
0308     {
0309         return m_handler;
0310     }
0311 
0312     /**
0313      * @returns the TransferTreeModel that owns this group
0314      */
0315     TransferTreeModel *model()
0316     {
0317         return m_model;
0318     }
0319 
0320     /**
0321      * Calculates the whole SpeedLimits
0322      */
0323     void calculateSpeedLimits();
0324 
0325     /**
0326      * Calculates the DownloadLimits
0327      */
0328     void calculateDownloadLimit();
0329 
0330     /**
0331      * Calculates the DownloadLimits
0332      */
0333     void calculateUploadLimit();
0334 
0335     /**
0336      * Saves this group object to the given QDomNode
0337      *
0338      * @param e The QDomNode where the group will be saved
0339      */
0340     void save(QDomElement e);
0341 
0342     /**
0343      * Adds all the groups in the given QDomNode * to the group
0344      *
0345      * @param e The QDomNode where the group will look for the transfers to add
0346      */
0347     void load(const QDomElement &e);
0348 
0349 private:
0350     TransferTreeModel *m_model;
0351     TransferGroupHandler *m_handler;
0352 
0353     // TransferGroup info
0354     QString m_name;
0355     int m_totalSize;
0356     int m_downloadedSize;
0357     int m_uploadedSize;
0358     int m_percent;
0359     int m_downloadSpeed;
0360     int m_uploadSpeed;
0361     int m_downloadLimit;
0362     int m_uploadLimit;
0363     int m_visibleDownloadLimit;
0364     int m_visibleUploadLimit;
0365     QString m_iconName;
0366     QString m_defaultFolder;
0367     QRegularExpression m_regExp;
0368 };
0369 
0370 #endif