Disable clipboardsharing in view only mode.
[kdenetwork.git] / kget / core / transferhandler.h
blobea6ecb7516d01d2c099457e041bb3b16a434b25b
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 "handler.h"
18 #include "transfer.h"
19 #include "transfergroup.h"
20 #include "kget_export.h"
21 #include "observer.h"
23 class QAction;
24 class KPassivePopup;
26 class TransferObserver;
27 class GenericTransferGroupObserver;
29 /**
30 * Class TransferHandler:
32 * --- Overview ---
33 * This class is the rapresentation of a Transfer object from the views'
34 * perspective (proxy pattern). In fact the views never handle directly the
35 * Transfer objects themselves (because this would break the model/view policy).
36 * As a general rule, all the code strictly related to the views should placed
37 * here (and not in the transfer implementation).
38 * Here we provide the same api available in the transfer class, but we change
39 * the implementation of some methods.
40 * Let's give an example about this:
41 * The start() function of a specific Transfer (let's say TransferKio) is a
42 * low level command that really makes the transfer start and should therefore
43 * be executed only by the scheduler.
44 * The start() function in this class is implemented in another way, since
45 * it requests the scheduler to execute the start command to this specific transfer.
46 * At this point the scheduler will evaluate this request and execute, if possible,
47 * the start() function directly in the TransferKio.
49 * --- Notifies about the transfer changes ---
50 * When a view is interested in receiving notifies about the specific transfer
51 * rapresented by this TransferHandler object, it should add itself to the list
52 * of observers calling the addObserver(TransferObserver *) function. On the
53 * contrary call the delObserver(TransferObserver *) function to remove it.
55 * --- Interrogation about what has changed in the transfer ---
56 * When a TransferObserver receives a notify about a change in the Transfer, it
57 * can ask to the TransferHandler for the ChangesFlags.
60 class KGET_EXPORT TransferHandler : public Handler
62 friend class KGet;
63 friend class TransferTreeModel;
64 friend class Transfer;
65 friend class TransferFactory;
66 friend class TransferGroupHandler;
68 public:
70 typedef Transfer::ChangesFlags ChangesFlags;
72 TransferHandler(Transfer * transfer, Scheduler * scheduler);
74 virtual ~TransferHandler();
76 bool supportsSpeedLimits() {return m_transfer->supportsSpeedLimits();}
78 /**
79 * Adds an observer to this Transfer
80 * Note that the observer with pointer == 0 is added by default, and
81 * it's used by the TransferTreeModel class
83 * @param observer The new observer that should be added
85 void addObserver(TransferObserver * observer);
87 /**
88 * Removes an observer from this Transfer
90 * @param observer The observer that should be removed
92 void delObserver(TransferObserver * observer);
94 /**
95 * These are all Job-related functions
97 void start();
98 void stop();
99 void setDelay(int seconds);
100 Job::Status status() const {return m_transfer->status();}
101 int elapsedTime() const;
102 int remainingTime() const;
103 bool isResumable() const;
106 * @return the transfer's group handler
108 TransferGroupHandler * group() const {return m_transfer->group()->handler();}
111 * @return the source url
113 const KUrl & source() const {return m_transfer->source();}
116 * @return the dest url
118 const KUrl & dest() const {return m_transfer->dest();}
121 * @return the total size of the transfer in bytes
123 KIO::filesize_t totalSize() const;
126 * @return the downloaded size of the transfer in bytes
128 KIO::filesize_t downloadedSize() const;
131 * @return the uploaded size of the transfer in bytes
133 KIO::filesize_t uploadedSize() const;
136 * @return the progress percentage of the transfer
138 int percent() const;
141 * @return the download speed of the transfer in bytes/sec
143 int downloadSpeed() const;
146 * @return the upload speed of the transfer in bytes/sec
148 int uploadSpeed() const;
151 * Set an UploadLimit for the transfer
152 * @note this UploadLimit is not visible in the GUI
153 * @param ulLimit upload Limit
155 void setUploadLimit(int ulLimit, Transfer::SpeedLimit limit) {m_transfer->setUploadLimit(ulLimit, limit);}
158 * Set a DownloadLimit for the transfer
159 * @note this DownloadLimit is not visible in the GUI
160 * @param dlLimit download Limit
162 void setDownloadLimit(int dlLimit, Transfer::SpeedLimit limit) {m_transfer->setDownloadLimit(dlLimit, limit);}
165 * @return the upload Limit of the transfer in KiB
167 int uploadLimit(Transfer::SpeedLimit limit) const {return m_transfer->uploadLimit(limit);}
170 * @return the download Limit of the transfer in KiB
172 int downloadLimit(Transfer::SpeedLimit limit) const {return m_transfer->downloadLimit(limit);}
175 * Set the maximum share-ratio
176 * @param ratio the new maximum share-ratio
178 void setMaximumShareRatio(double ratio) {m_transfer->setMaximumShareRatio(ratio);}
181 * @return the maximum share-ratio
183 double maximumShareRatio() {return m_transfer->maximumShareRatio();}
186 * Recalculate the share ratio
188 void checkShareRatio() {m_transfer->checkShareRatio();}
191 * @return a string describing the current transfer status
193 QString statusText() const {return m_transfer->statusText();}
196 * @return a pixmap associated with the current transfer status
198 QPixmap statusPixmap() const {return m_transfer->statusPixmap();}
201 * @returns the data associated to this Transfer item. This is
202 * necessary to make the interview model/view work
204 QVariant data(int column);
207 * @returns the number of columns associated to the transfer's data
209 int columnCount() const {return 6;}
212 * Selects the current transfer. Selecting transfers means that all
213 * the actions executed from the gui will apply also to the current
214 * transfer.
216 * @param select if true the current transfer is selected
217 * if false the current transfer is deselected
219 void setSelected( bool select );
222 * @returns true if the current transfer is selected
223 * @returns false otherwise
225 bool isSelected() const;
228 * Returns the changes flags
230 * @param observer The observer that makes this request
232 ChangesFlags changesFlags(TransferObserver * observer) const;
235 * Resets the changes flags for a given TransferObserver
237 * @param observer The observer that makes this request
239 void resetChangesFlags(TransferObserver * observer);
242 * @returns a list of a actions, which are associated with this TransferHandler
244 QList<QAction*> contextActions();
247 * @returns a list of the transfer's factory's actions
249 QList<QAction*> factoryActions();
251 #ifdef HAVE_NEPOMUK
253 * Sets the NepomukHandler for the transfer
254 * @param handler the new NepomukHandler
256 void setNepomukHandler(NepomukHandler *handler) {m_transfer->setNepomukHandler(handler);}
259 * @returns the NepomukHandler of the transfer
261 NepomukHandler * nepomukHandler() {return m_transfer->nepomukHandler();}
262 #endif
264 private:
266 * Sets a change flag in the ChangesFlags variable.
268 * @param change The TransferChange flag to be set
269 * @param postEvent if false the handler will not post an event to the observers,
270 * if true the handler will post an event to the observers
272 void setTransferChange(ChangesFlags change, bool postEvent=false);
275 * Posts a TransferChangedEvent to all the observers.
277 void postTransferChangedEvent();
280 * Posts a deleteEvent to all the observers
282 void postDeleteEvent();
284 Transfer * m_transfer;
286 QList<TransferObserver *> m_observers;
287 QMap<TransferObserver *, ChangesFlags> m_changesFlags;
291 class GenericTransferObserver : public QObject, public TransferObserver
293 Q_OBJECT
294 public:
295 GenericTransferObserver(GenericTransferGroupObserver *groupObserver);
297 void transferChangedEvent(TransferHandler * transfer);
299 #ifdef HAVE_KWORKSPACE
300 private slots:
301 void slotShutdown();
302 #endif
304 private:
305 bool allTransfersFinished();
306 KPassivePopup* popupMessage(const QString &title, const QString &message);
308 void checkAndFinish();
310 #ifdef HAVE_KWORKSPACE
311 void checkAndShutdown();
312 #endif
313 void checkAndUpdateSystemTray();
316 QString prevStatus;
317 GenericTransferGroupObserver *m_groupObserver;
320 #endif