summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/queueobject.h
blob: 3431774ea76e771c82269033afcf38daedac14d6 (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2005 by the KFTPGrabber developers
 * Copyright (C) 2003-2005 Jernej Kos <kostko@jweb-network.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.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 *
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
#ifndef KFTPQUEUEQUEUEOBJECT_H
#define KFTPQUEUEQUEUEOBJECT_H

#include <ntqobject.h>
#include <ntqptrlist.h>
#include <ntqtimer.h>

#include "engine/directorylisting.h"

namespace KFTPQueue {

class QueueGroup;

/**
 * This class represents a basic object that can be queued.
 *
 * @author Jernej Kos
 */
class QueueObject : public TQObject
{
friend class QueueGroup;
Q_OBJECT
public:
    enum Type {
      File,
      Directory,
      Site,
      Toplevel
    };
    
    enum Status {
      Running,
      Stopped,
      Connecting,
      Locked,
      Failed,
      Waiting
    };
    
    /**
     * Class constructor.
     *
     * @param parent Parent object
     * @param type Object type
     */
    QueueObject(TQObject *parent, Type type);
    
    /**
     * Class destructor.
     */
    ~QueueObject();
    
    /**
     * Returns true if this object has a parent object.
     *
     * @return True if this object has a parent object
     */
    bool hasParentObject() const { return parent() ? parent()->inherits("KFTPQueue::QueueObject") : false; }
    
    /**
     * Returns the parent QueueObject.
     *
     * @return The parent QueueObject
     */
    QueueObject *parentObject() const { return static_cast<QueueObject*>(parent()); }
    
    /**
     * Do we have any children ?
     *
     * @return True if we have some kids
     */
    bool hasChildren() const { return m_children.count() > 0; }
    
    /**
     * Get object status.
     *
     * @return Status of this object
     */
    Status getStatus() const { return m_status; }
    
    /**
     * Is this object currently running ?
     *
     * @return true if this object's status is set to Running or Connecting
     */
    bool isRunning() const { return m_status == Running || m_status == Connecting || m_status == Waiting; }
    
    /**
     * Is the object currently locked ?
     *
     * @return true if this object's status is set to Locked
     */
    bool isLocked() const { return m_status == Locked; }
    
    /**
     * Is this object currently aborting ?
     *
     * @return true if this object is currently aborting
     */
    bool isAborting() const { return m_aborting; }
    
    /**
     * Returns the size of this queue object.
     *
     * @return Size
     */
    filesize_t getSize() const { return m_size; }
    
    /**
     * Returns the actual size - that is usefull only if this is a directory since
     * it returns the current size of all its items (if some items were removed
     * from its first scan getSize() will return the initial size and getActualSize()
     * will return current size of all items). If this is not a directory, this
     * will return the same value as getSize();
     *
     * @return Actual directory size
     */
    filesize_t getActualSize() const { return m_actualSize; }
    
    /**
     * Returns the already transfered file/dir size.
     *
     * @return Transfered file/dir size
     */
    filesize_t getCompleted() const { return m_completed; }
    
    /**
     * Returns the number of bytes that have been resumed (using REST).
     *
     * @return Resume offset
     */
    filesize_t getResumed() const { return m_resumed; }
    
    /**
     * Get current transfer speed or 0 if the transfer is stalled.
     *
     * @return Transfer speed
     */
    filesize_t getSpeed() const { return m_speed; }
    
    /**
     * Adds size bytes to the current transfer size. This will also update all
     * parent transfers (if any).
     *
     * @param size Size to add
     */
    void addSize(filesize_t size);
    
    /**
     * Adds completed bytes to the current completed size. This will also update all
     * parent transfers (if any).
     *
     * @param completed Size to add
     */
    void addCompleted(filesize_t completed);
    
    /**
     * Set the current transfer speed. This will also update all parent transfers.
     *
     * @param speed Speed to set
     */
    void setSpeed(filesize_t speed = 0);
    
    /**
     * Returns the KFTPQueue::Transfer::Type of this transfer. This can either be
     * File or Directory.
     *
     * @return Transfer type
     */
    Type getType() const { return m_type; }
    
    /**
     * Is this object a directory ?
     *
     * @return true if this object's type is set to Directory
     */
    bool isDir() const { return m_type == Directory; }
    
    /**
     * Is this object a transfer ?
     *
     * @return true if this object's type is File or Directory
     */
    bool isTransfer() const { return m_type == File || m_type == Directory; }
    
    /**
     * Delays transfer execution for msec miliseconds. If this number is greater than
     * 1000, it will be set to 1000.
     *
     * @param msec Number of miliseconds to delay execution
     */
    void delayedExecute(int msec = 100);
    
    /**
     * Set transfer's ID.
     *
     * @param id Transfer identifier (must be unique)
     */
    void setId(long id) { m_id = id; }
    
    /**
     * Get transfer's ID.
     *
     * @return Transfer's unique ID number
     */
    long getId() const { return m_id; }
    
    /**
     * Abort current transfer.
     */
    virtual void abort();
    
    /**
     * Add a child queue object to this object. The object is NOT reparented!
     *
     * @param object The child queue object
     */
    void addChildObject(QueueObject *object);
    
    /**
     * Delete a child queue object from this object. The object is NOT reparented!
     *
     * @param object The child queue object
     */
    void delChildObject(QueueObject *object);
    
    /**
     * Find a QueueObject that is child of the current object by its id. This
     * method goes trough all the objects under this one.
     *
     * @param id Object's id
     * @return A valid QueueObject or NULL if no such object can be found
     */
    QueueObject *findChildObject(long id);
    
    /**
     * Removes all transfers that have been marked for deletion.
     */
    void removeMarkedTransfers();
    
    /**
     * Move a child object up in the queue.
     *
     * @param child The object to move
     */
    void moveChildUp(QueueObject *child);
    
    /**
     * Move a child object down in the queue.
     *
     * @param child The object to move
     */
    void moveChildDown(QueueObject *child);
    
    /**
     * Move a child object to the top.
     *
     * @param child The object to move
     */
    void moveChildTop(QueueObject *child);
    
    /**
     * Move a child object to the bottom.
     *
     * @param child The object to move
     */
    void moveChildBottom(QueueObject *child);
    
    /**
     * Can a child be moved up ?
     *
     * @param child The child to be moved
     * @return True if the child can be moved up
     */
    bool canMoveChildUp(QueueObject *child);
    
    /**
     * Can a child be moved down ?
     *
     * @param child The child to be moved
     * @return True if the child can be moved down
     */
    bool canMoveChildDown(QueueObject *child);
    
    /**
     * Returns the list of this object's child QueueObjects.
     *
     * @return A QueueObject list
     */
    TQPtrList<QueueObject> getChildrenList() const { return m_children; }
public slots:
    /**
     * Execute this queue object.
     */
    virtual void execute();
protected:
    bool m_aborting;
    Status m_status;
    
    long m_id;
    Type m_type;
    TQPtrList<QueueObject> m_children;
    
    TQTimer m_delayedExecuteTimer;
    
    /* Statistical information */
    filesize_t m_size;
    filesize_t m_actualSize;
    filesize_t m_completed;
    filesize_t m_resumed;
    filesize_t m_speed;
    
    void addActualSize(filesize_t size);
    
    /**
     * This method is called every time the object's statistics must be
     * updated.
     */
    virtual void statisticsUpdated();
    
    /**
     * This method should return true if the object can be moved.
     */
    virtual bool canMove();
private slots:
    void slotChildDestroyed(TQObject *child);
signals:
    /**
     * This signal gets emitted when the object's state has changed in
     * some way.
     */
    void objectUpdated();
};

}

#endif