Relicense all GPLv2 only code to GPLv2+.
[kdenetwork.git] / kget / core / transferhandler.h
blob430e2bdccb69fb3c649ec789407df0714fdfe538
1 /* This file is part of the KDE project
3 Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9 */
12 #ifndef TRANSFERHANDLER_H
13 #define TRANSFERHANDLER_H
15 #include <QVariant>
17 #include "transfer.h"
18 #include "transfergroup.h"
19 #include "kget_export.h"
21 class KMenu;
24 class TransferObserver;
26 /**
27 * Class TransferHandler:
29 * --- Overview ---
30 * This class is the rapresentation of a Transfer object from the views'
31 * perspective (proxy pattern). In fact the views never handle directly the
32 * Transfer objects themselves (because this would break the model/view policy).
33 * As a general rule, all the code strictly related to the views should placed
34 * here (and not in the transfer implementation).
35 * Here we provide the same api available in the transfer class, but we change
36 * the implementation of some methods.
37 * Let's give an example about this:
38 * The start() function of a specific Transfer (let's say TransferKio) is a
39 * low level command that really makes the transfer start and should therefore
40 * be executed only by the scheduler.
41 * The start() function in this class is implemented in another way, since
42 * it requests the scheduler to execute the start command to this specific transfer.
43 * At this point the scheduler will evaluate this request and execute, if possible,
44 * the start() function directly in the TransferKio.
46 * --- Notifies about the transfer changes ---
47 * When a view is interested in receiving notifies about the specific transfer
48 * rapresented by this TransferHandler object, it should add itself to the list
49 * of observers calling the addObserver(TransferObserver *) function. On the
50 * contrary call the delObserver(TransferObserver *) function to remove it.
52 * --- Interrogation about what has changed in the transfer ---
53 * When a TransferObserver receives a notify about a change in the Transfer, it
54 * can ask to the TransferHandler for the ChangesFlags.
57 class KGET_EXPORT TransferHandler
59 friend class KGet;
60 friend class TransferTreeModel;
61 friend class Transfer;
62 friend class TransferFactory;
63 friend class TransferGroupHandler;
65 public:
67 typedef Transfer::ChangesFlags ChangesFlags;
69 TransferHandler(Transfer * transfer, Scheduler * scheduler);
71 virtual ~TransferHandler();
73 /**
74 * Adds an observer to this Transfer
76 * @param observer Tthe new observer that should be added
78 void addObserver(TransferObserver * observer);
80 /**
81 * Removes an observer from this Transfer
83 * @param observer The observer that should be removed
85 void delObserver(TransferObserver * observer);
87 /**
88 * These are all Job-related functions
90 void start();
91 void stop();
92 void setDelay(int seconds);
93 Job::Status status() const {return m_transfer->status();}
94 int elapsedTime() const;
95 int remainingTime() const;
96 bool isResumable() const;
98 /**
99 * @return the transfer's group handler
101 TransferGroupHandler * group() const {return m_transfer->group()->handler();}
104 * @return the source url
106 const KUrl & source() const {return m_transfer->source();}
109 * @return the dest url
111 const KUrl & dest() const {return m_transfer->dest();}
114 * @return the total size of the transfer in bytes
116 unsigned long totalSize() const;
119 * @return the downloaded size of the transfer in bytes
121 unsigned long processedSize() const;
124 * @return the progress percentage of the transfer
126 int percent() const;
129 * @return the download speed of the transfer in bytes/sec
131 int speed() const;
134 * @return a string describing the current transfer status
136 QString statusText() const {return m_transfer->statusText();}
139 * @return a pixmap associated with the current transfer status
141 QPixmap statusPixmap() const {return m_transfer->statusPixmap();}
144 * @returns the data associated to this Transfer item. This is
145 * necessary to make the interview model/view work
147 QVariant data(int column);
150 * @returns the number of columns associated to the transfer's data
152 int columnCount() const {return 5;}
155 * Returns a KMenu for the given list of transfers, populated with
156 * the actions that can be executed on each transfer in the list.
157 * If the list is null, it returns the KMenu associated with the
158 * this transfer.
160 * @param transfers the transfer list
162 * @return a KMenu for the given transfers
164 KMenu * popupMenu(QList<TransferHandler *> transfers);
167 * Selects the current transfer. Selecting transfers means that all
168 * the actions executed from the gui will apply also to the current
169 * transfer.
171 * @param select if true the current transfer is selected
172 * if false the current transfer is deselected
174 void setSelected( bool select );
177 * @returns true if the current transfer is selected
178 * @returns false otherwise
180 bool isSelected() const;
183 * Returns the changes flags
185 * @param observer The observer that makes this request
187 ChangesFlags changesFlags(TransferObserver * observer) const;
190 * Resets the changes flags for a given TransferObserver
192 * @param observer The observer that makes this request
194 void resetChangesFlags(TransferObserver * observer);
196 private:
198 * Sets a change flag in the ChangesFlags variable.
200 * @param change The TransferChange flag to be set
201 * @param postEvent if false the handler will not post an event to the observers,
202 * if true the handler will post an event to the observers
204 void setTransferChange(ChangesFlags change, bool postEvent=false);
207 * Posts a TransferChangedEvent to all the observers.
209 void postTransferChangedEvent();
212 * Posts a deleteEvent to all the observers
214 void postDeleteEvent();
216 Transfer * m_transfer;
217 Scheduler * m_scheduler;
219 QList<TransferObserver *> m_observers;
220 QMap<TransferObserver *, ChangesFlags> m_changesFlags;
223 #endif