File indexing completed on 2023-10-01 08:39:30

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