1 /* This file is part of the KDE project
2 
3    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4    Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
5    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 */
12 #ifndef GROUP_H
13 #define GROUP_H
14 
15 #include <QRegExp>
16 #include <QIcon>
17 
18 #include <QDebug>
19 
20 #include "jobqueue.h"
21 #include "kget_export.h"
22 #include "transfer.h"
23 
24 class QDomElement;
25 
26 class TransferGroupHandler;
27 class TransferTreeModel;
28 
29 /**
30  * class TransferGroup:
31  *
32  * This class abstracts the concept of transfer group by means of which
33  * the user can sort his transfers into categories.
34  * By definition, we want each TransferGroup (transfer group) to be a JobQueue.
35  * Moreover this class calculates information such as:
36  * - the size obtained by the sum of all the transfer's size
37  * - the size obtained by the sum of all the transfer's processed size
38  * - the global progress percentage within the group
39  * - the global speed within the group
40  */
41 class KGET_EXPORT TransferGroup : public JobQueue
42 {
43     Q_OBJECT
44     public:
45         enum GroupChange
46         {
47             Gc_None                = 0x00000000,
48             // These flags respect the Model columns order
49             Gc_GroupName           = 0x00000001,
50             Gc_Status              = 0x00000002,
51             Gc_TotalSize           = 0x00000004,
52             Gc_Percent             = 0x00000008,
53             Gc_UploadSpeed         = 0x00000010,
54             Gc_DownloadSpeed       = 0x00000020,
55             // Misc
56             Gc_ProcessedSize       = 0x00010000
57         };
58 
59         typedef int ChangesFlags;
60 
61         TransferGroup(TransferTreeModel * model, Scheduler * parent, const QString & name=QString());
62 
63         ~TransferGroup() override;
64 
65         /**
66          * This function is reimplemented by JobQueue::setStatus
67          *
68          * @param queueStatus the new JobQueue status
69          */
70         void setStatus(Status queueStatus) override;
71 
72         /**
73          * Appends a new transfer to the list of the transfers
74          *
75          * @param transfer the transfer to append
76          */
77         void append(Transfer * transfer);
78 
79         /**
80          * Appends new transfers to the list of the transfers
81          *
82          * @param transfer the transfers to append
83          */
84         void append(const QList<Transfer*> &transfer);
85 
86         /**
87          * Prepends a new transfer to the list of the transfers
88          *
89          * @param transfer the transfer to prepend
90          */
91         void prepend(Transfer * transfer);
92 
93         /**
94          * inserts a transfer to the current group after the given transfer
95          *
96          * @param transfer The transfer to add in the current Group
97          * @param after The transfer after which to add the transfer
98          */
99         void insert(Transfer * transfer, Transfer * after);
100 
101         /**
102          * Removes the given transfer from the list of the transfers
103          *
104          * @param transfer the transfer to remove
105          */
106         void remove(Transfer * transfer);
107 
108         /**
109          * Removes the given transfers from the list of the transfers
110          *
111          * @param transfers the transfers to remove
112          */
113         void remove(const QList<Transfer*> &transfers);
114 
115         /**
116          * Moves a transfer in the list
117          *
118          * @param transfer The transfer to move. Note that this transfer can
119          *                  belong to other groups. In this situation this
120          *                  transfer is deleted from the previous group and
121          *                  moved inside this one.
122          * @param after The transfer after which we have to move the given one
123          */
124         void move(Transfer * transfer, Transfer * after);
125 
126         /**
127          * Finds the first transfer with source src
128          *
129          * @param src the url of the source location
130          *
131          * @return the transfer pointer if the transfer has been found. Otherwise
132          * it returns nullptr
133          */
134         Transfer * findTransfer(const QUrl &src);
135 
136         /**
137          * Finds the first transfer with destination dest
138          *
139          * @param dest the url of the destination location
140          *
141          * @return the transfer pointer if the transfer has been found, else return nullptr
142          */
143          Transfer *findTransferByDestination(const QUrl &dest);
144 
145         /**
146          * @returns the Job in the queue at the given index i
147          */
148         Transfer * operator[] (int i) const {return (Transfer *)((* (JobQueue *)this)[i]);}
149 
150         /**Set the group name
151          * @param name group name
152          */
setName(const QString & name)153         void  setName(const QString &name)    {m_name=name;}
154 
155         /**
156          * @return the group name
157          */
name()158         const QString & name()    {return m_name;}
159 
160         /**
161          * @return the sum of the sizes of the transfers belonging to
162          * this group
163          */
totalSize()164         int totalSize() const     {return m_totalSize;}
165 
166         /**
167          * @return the sum of the downloaded sizes of the transfers
168          * belonging to this group
169          */
downloadedSize()170         int downloadedSize() const {return m_downloadedSize;}
171 
172         /**
173          * @return the sum of the uploaded sizes of the transfers
174          * belonging to this group
175          */
uploadedSize()176         int uploadedSize() const {return m_uploadedSize;}
177 
178         /**
179          * @return the progress percentage
180          */
percent()181         int percent() const       {return m_percent;}
182 
183         /**
184          * @return the sum of the download speeds of the running transfers
185          * belonging this group
186          */
187         int downloadSpeed();
188 
189         /**
190          * @return the sum of the download speeds of the running transfers
191          * belonging this group
192          */
193         int uploadSpeed();
194 
195         /**
196          * Set a default Folder for the group
197          * @param folder the new default folder
198          */
setDefaultFolder(QString folder)199         void setDefaultFolder(QString folder) {m_defaultFolder = folder;}
200 
201         /**
202          * @return the groups default folder
203          */
defaultFolder()204         QString defaultFolder() {return m_defaultFolder;}
205 
206         /**
207          * Sets the regular expression of the group
208          * @param regExp the regular expression
209          */
setRegExp(const QRegExp & regExp)210         void setRegExp(const QRegExp &regExp) {m_regExp = regExp;}
211 
212         /**
213          * @returns the regular expression of the group
214          */
regExp()215         QRegExp regExp() {return m_regExp;}
216 
217         /**
218          * @return true if the group supports SpeedLimits
219          */
220         bool supportsSpeedLimits();
221 
222         /**
223          * Set a Download-Limit for the group
224          * @param dlLimit the new download limit
225          * @param limit the type of the new download limit
226          * @note if limit is 0, no download-limit is set
227          */
228         void setDownloadLimit(int dlLimit, Transfer::SpeedLimit limit);
229 
230         /**
231          * @return the group's Download-Limit
232          */
233         int downloadLimit(Transfer::SpeedLimit limit) const;
234 
235         /**
236          * Set a Upload-Limit for the group
237          * @param ulLimit the new upload limit
238          * @param limit the type of the new upload-limit
239          * @note if limit is 0, no upload-limit is set
240          */
241         void setUploadLimit(int ulLimit, Transfer::SpeedLimit limit);
242 
243         /**
244          * @return the group's Upload-Limit
245          */
246         int uploadLimit(Transfer::SpeedLimit limit) const;
247 
248         /**
249          * Set the group's icon
250          * @param name the icon's name
251          */
setIconName(const QString & name)252         void setIconName(const QString &name) {m_iconName = name;}
253 
254         /**
255          * @returns the group's icon's name
256          */
iconName()257         QString iconName() const {return m_iconName;}
258 
259         /**
260          * @return the group's icon
261          */
pixmap()262         QPixmap pixmap() {return QIcon::fromTheme(m_iconName).pixmap(32);}
263 
264         /**
265          * @return the handler associated with this group
266          */
handler()267         TransferGroupHandler * handler() const {return m_handler;}
268 
269         /**
270          * @returns the TransferTreeModel that owns this group
271          */
model()272         TransferTreeModel * model()     {return m_model;}
273 
274         /**
275          * Calculates the whole SpeedLimits
276          */
277         void calculateSpeedLimits();
278 
279         /**
280          * Calculates the DownloadLimits
281          */
282         void calculateDownloadLimit();
283 
284         /**
285          * Calculates the DownloadLimits
286          */
287         void calculateUploadLimit();
288 
289         /**
290          * Saves this group object to the given QDomNode
291          *
292          * @param e The QDomNode where the group will be saved
293          */
294         void save(QDomElement e);
295 
296         /**
297          * Adds all the groups in the given QDomNode * to the group
298          *
299          * @param e The QDomNode where the group will look for the transfers to add
300          */
301         void load(const QDomElement & e);
302 
303     private:
304         TransferTreeModel * m_model;
305         TransferGroupHandler * m_handler;
306 
307         //TransferGroup info
308         QString m_name;
309         int m_totalSize;
310         int m_downloadedSize;
311         int m_uploadedSize;
312         int m_percent;
313         int m_downloadSpeed;
314         int m_uploadSpeed;
315         int m_downloadLimit;
316         int m_uploadLimit;
317         int m_visibleDownloadLimit;
318         int m_visibleUploadLimit;
319         QString m_iconName;
320         QString m_defaultFolder;
321         QRegExp m_regExp;
322 };
323 
324 #endif
325