~ubuntu-branches/ubuntu/trusty/kget/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* This file is part of the KDE project

   Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
   Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
   Coypright (C) 2010 Matthias Fuchs <mat69@gmx.net>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
*/
#ifndef GROUP_H
#define GROUP_H

#include <QRegExp>

#include <kio/netaccess.h>
#include <KIcon>
#include <KDebug>

#include "jobqueue.h"
#include "../kget_export.h"
#include "transfer.h"

#ifdef HAVE_NEPOMUK
    #include <Nepomuk2/Tag>
#endif

class QDomElement;

class TransferGroupHandler;
class TransferTreeModel;

/**
 * class TransferGroup:
 *
 * This class abstracts the concept of transfer group by means of which
 * the user can sort his transfers into categories.
 * By definition, we want each TransferGroup (transfer group) to be a JobQueue.
 * Moreover this class calculates information such as:
 * - the size obtained by the sum of all the transfer's size
 * - the size obtained by the sum of all the transfer's processed size
 * - the global progress percentage within the group
 * - the global speed within the group
 */
class KGET_EXPORT TransferGroup : public JobQueue
{
    Q_OBJECT
    public:
        enum GroupChange
        {
            Gc_None                = 0x00000000,
            // These flags respect the Model columns order
            Gc_GroupName           = 0x00000001,
            Gc_Status              = 0x00000002,
            Gc_TotalSize           = 0x00000004,
            Gc_Percent             = 0x00000008,
            Gc_UploadSpeed         = 0x00000010,
            Gc_DownloadSpeed       = 0x00000020,
            // Misc
            Gc_ProcessedSize       = 0x00010000
        };

        typedef int ChangesFlags;

        TransferGroup(TransferTreeModel * model, Scheduler * parent, const QString & name=QString());

        virtual ~TransferGroup();

        /**
         * This function is reimplemented by JobQueue::setStatus
         *
         * @param queueStatus the new JobQueue status
         */
        void setStatus(Status queueStatus);

        /**
         * Appends a new transfer to the list of the transfers
         *
         * @param transfer the transfer to append
         */
        void append(Transfer * transfer);

        /**
         * Appends new transfers to the list of the transfers
         *
         * @param transfers to append
         */
        void append(const QList<Transfer*> &transfer);

        /**
         * Prepends a new transfer to the list of the transfers
         *
         * @param transfer the transfer to prepend
         */
        void prepend(Transfer * transfer);

        /**
         * inserts a transfer to the current group after the given transfer
         *
         * @param transfer The transfer to add in the current Group
         * @param after The transfer after which to add the transfer
         */
        void insert(Transfer * transfer, Transfer * after);

        /**
         * Removes the given transfer from the list of the transfers
         *
         * @param transfer the transfer to remove
         */
        void remove(Transfer * transfer);

        /**
         * Removes the given transfers from the list of the transfers
         *
         * @param transfers the transfers to remove
         */
        void remove(const QList<Transfer*> &transfers);

        /**
         * Moves a transfer in the list
         *
         * @param transfer The transfer to move. Note that this transfer can
         *                  belong to other groups. In this situation this
         *                  transfer is deleted from the previous group and
         *                  moved inside this one.
         * @param after The transfer after which we have to move the given one
         */
        void move(Transfer * transfer, Transfer * after);

        /**
         * Finds the first transfer with source src
         *
         * @param src the url of the source location
         *
         * @return the transfer pointer if the transfer has been found. Otherwise
         * it returns 0
         */
        Transfer * findTransfer(const KUrl &src);

        /**
         * Finds the first transfer with destination dest
         *
         * @param dest the url of the destination location
         *
         * @return the transfer pointer if the transfer has been found, else return 0
         */
         Transfer *findTransferByDestination(const KUrl &dest);

        /**
         * @returns the Job in the queue at the given index i
         */
        Transfer * operator[] (int i) const {return (Transfer *)((* (JobQueue *)this)[i]);}

        /**Set the group name
         * @param name group name
         */
        void  setName(const QString &name)    {m_name=name;}

        /**
         * @return the group name
         */
        const QString & name()    {return m_name;}

        /**
         * @return the sum of the sizes of the transfers belonging to 
         * this group
         */
        int totalSize() const     {return m_totalSize;}

        /**
         * @return the sum of the downloaded sizes of the transfers
         * belonging to this group
         */
        int downloadedSize() const {return m_downloadedSize;}

        /**
         * @return the sum of the uploaded sizes of the transfers
         * belonging to this group
         */
        int uploadedSize() const {return m_uploadedSize;}

        /**
         * @return the progress percentage
         */
        int percent() const       {return m_percent;}

        /**
         * @return the sum of the download speeds of the running transfers 
         * belonging this group
         */
        int downloadSpeed();

        /**
         * @return the sum of the download speeds of the running transfers 
         * belonging this group
         */
        int uploadSpeed();

        /**
         * Set a default Folder for the group
         * @param folder the new default folder
         */
        void setDefaultFolder(QString folder) {m_defaultFolder = folder;}

        /**
         * @return the groups default folder
         */
        QString defaultFolder() {return m_defaultFolder;}

        /**
         * Sets the regular expression of the group
         * @param regexp the regular expression
         */
        void setRegExp(const QRegExp &regExp) {m_regExp = regExp;}

        /**
         * @returns the regular expression of the group
         */
        QRegExp regExp() {return m_regExp;}

#ifdef HAVE_NEPOMUK
        /**
         * Sets the Nepomuk tags of the group
         * @param tags the Nepomuk tags
         */
        void setTags(const QList<Nepomuk2::Tag> &tags);

        /**
         * @returns the Nepomuk tags of the group
         */
        QList<Nepomuk2::Tag> tags() const { return m_tags.values(); }
#endif //HAVE_NEPOMUK

        /**
         * @return true if the group supports SpeedLimits
         */
        bool supportsSpeedLimits();

        /**
         * Set a Download-Limit for the group
         * @param limit the new download-limit
         * @note if limit is 0, no download-limit is set
         */
        void setDownloadLimit(int dlLimit, Transfer::SpeedLimit limit);

        /**
         * @return the group's Download-Limit
         */
        int downloadLimit(Transfer::SpeedLimit limit) const;

        /**
         * Set a Upload-Limit for the group
         * @param limit the new upload-limit
         * @note if limit is 0, no upload-limit is set
         */
        void setUploadLimit(int ulLimit, Transfer::SpeedLimit limit);

        /**
         * @return the group's Upload-Limit
         */
        int uploadLimit(Transfer::SpeedLimit limit) const;

        /**
         * Set the group's icon
         * @param name the icon's name
         */
        void setIconName(const QString &name) {m_iconName = name;}

        /**
         * @returns the group's icon's name
         */
        QString iconName() const {return m_iconName;}

        /**
         * @return the group's icon
         */
        QPixmap pixmap() {return KIcon(m_iconName).pixmap(32);}

        /**
         * @return the handler associated with this group
         */
        TransferGroupHandler * handler() const {return m_handler;}

        /**
         * @returns the TransferTreeModel that owns this group
         */
        TransferTreeModel * model()     {return m_model;}

        /**
         * Calculates the whole SpeedLimits
         */
        void calculateSpeedLimits();

        /**
         * Calculates the DownloadLimits
         */
        void calculateDownloadLimit();

        /**
         * Calculates the DownloadLimits
         */
        void calculateUploadLimit();

        /**
         * Saves this group object to the given QDomNode 
         *
         * @param n The QDomNode where the group will be saved
         */
        void save(QDomElement e);

        /**
         * Adds all the groups in the given QDomNode * to the group
         *
         * @param n The QDomNode where the group will look for the transfers to add
         */
        void load(const QDomElement & e);

    private:
        TransferTreeModel * m_model;
        TransferGroupHandler * m_handler;

        //TransferGroup info
        QString m_name;
        int m_totalSize;
        int m_downloadedSize;
        int m_uploadedSize;
        int m_percent;
        int m_downloadSpeed;
        int m_uploadSpeed;
        int m_downloadLimit;
        int m_uploadLimit;
        int m_visibleDownloadLimit;
        int m_visibleUploadLimit;
        QString m_iconName;
        QString m_defaultFolder;
        QRegExp m_regExp;
#ifdef HAVE_NEPOMUK
        QMap<QUrl, Nepomuk2::Tag> m_tags;
#endif //HAVE_NEPOMUK
};

#endif