Fix system-tray...
[kdenetwork.git] / kget / core / kget.h
blob46dfea0a68edeb63b02c5ba086c74419b290ccae
1 /* This file is part of the KDE project
3 Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
5 Based on:
6 kmainwidget.{h,cpp}
7 Copyright (C) 2002 by Patrick Charbonnier <pch@freeshell.org>
8 that was based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
16 #ifndef KGET_H
17 #define KGET_H
19 #include <kservice.h>
20 #include <kurl.h>
21 #include <kactioncollection.h>
22 #include <ktabwidget.h>
24 #include "scheduler.h"
25 #include "kget_export.h"
26 #include "observer.h"
28 class QDomElement;
29 class QAbstractItemView;
31 class KLibrary;
33 class Transfer;
34 class TransferGroup;
35 class TransferHandler;
36 class TransferGroupHandler;
37 class TransferFactory;
38 class TransferTreeModel;
39 class TransferTreeSelectionModel;
40 class ModelObserver;
41 class KGetPlugin;
42 class MainWindow;
43 class NewTransferDialog;
45 /**
46 * This is our KGet class. This is where the user's transfers and searches are
47 * stored and organized.
48 * Use this class from the views to add or remove transfers or searches
49 * In order to organize the transfers inside categories we have a TransferGroup
50 * class. By definition, a transfer must always belong to a TransferGroup. If we
51 * don't want it to be displayed by the gui inside a specific group, we will put
52 * it in the group named "Not grouped" (better name?).
53 **/
55 static const QString KGET_QUIT_MESSAGE_TITLE = i18n("Quit KGet");
56 static const QString KGET_QUIT_MESSAGE = i18n("KGet quits now because all downloads have been completed.");
58 class KGET_EXPORT KGet
60 friend class NewTransferDialog;
61 friend class TransferFinishedObserver;
63 public:
64 static KGet& self( MainWindow * mainWindow=0 );
66 /**
67 * Adds a new observer of the KGet. See observer.h for more info about it.
69 * @param observer The new observer
71 static void addObserver(ModelObserver * observer);
73 /**
74 * Removes an observer of the KGet. See observer.h for more info about it.
76 * @param observer The observer to remove
78 static void delObserver(ModelObserver * observer);
80 /**
81 * Adds a new group to the KGet.
83 * @param groupName The name of the new group
85 * @returns true if the group has been successully added, otherwise
86 * it returns false, probably because a group with that named
87 * already exists
89 static bool addGroup(const QString& groupName);
91 /**
92 * Removes a group from the KGet.
94 * @param groupName The name of the group to be deleted
96 static void delGroup(const QString& groupName);
98 /**
99 * Changes the name of the group
101 * @param oldName the name of the group to be changed
102 * @param newName the new name of the group
104 static void renameGroup(const QString& oldName, const QString& newName);
107 * @returns the name of the available transfers groups
109 static QStringList transferGroupNames();
112 * Adds a new transfer to the KGet
114 * @param srcUrl The url to be downloaded
115 * @param destDir The destination directory. If empty we show a dialog
116 * where the user can choose it.
117 * @param groupName The name of the group the new transfer will belong to
118 * @param start Specifies if the newly added transfers should be started.
119 * If the group queue is already in a running state, this flag does nothing
121 static void addTransfer(KUrl srcUrl, QString destDir = QString(),
122 const QString& groupName = QString(), bool start=false);
125 * Adds a new transfer to the KGet
127 * @param e The transfer's dom element
128 * @param groupName The name of the group the new transfer will belong to
130 static void addTransfer(const QDomElement& e, const QString& groupName = QString());
133 * Adds new transfers to the KGet
135 * @param srcUrls The urls to be downloaded
136 * @param destDir The destination directory. If empty we show a dialog
137 * where the user can choose it.
138 * @param groupName The name of the group the new transfer will belong to
139 * @param start Specifies if the newly added transfers should be started.
140 * If the group queue is already in a running state, this flag does nothing
142 static void addTransfer(KUrl::List srcUrls, QString destDir = QString(),
143 const QString& groupName = QString(), bool start=false);
146 * Removes a transfer from the KGet
148 * @param transfer The transfer to be removed
150 static void delTransfer(TransferHandler * transfer);
153 * Moves a transfer to a new group
155 * @param transfer The transfer to be moved
156 * @param groupName The name of the new transfer's group
158 static void moveTransfer(TransferHandler * transfer, const QString& groupName);
161 * @returns the list of selected transfers
163 static QList<TransferHandler *> selectedTransfers();
166 * @returns the list of selected groups
168 static QList<TransferGroupHandler *>
169 selectedTransferGroups();
172 * @returns a pointer to the QItemSelectionModel object
174 static TransferTreeSelectionModel * selectionModel();
177 * Imports the transfers and groups included in the provided xml file
179 * @param filename the file name to
181 static void load( QString filename=QString() );
184 * Exports all the transfers and groups to the given file
186 * @param filename the file name
188 static void save( QString filename=QString() );
191 * @returns The factory of a given transfer
193 * @param transfer the transfer about which we want to have the factory
195 static TransferFactory * factory(TransferHandler * transfer);
198 * @return a pointer to the KActionCollection objects
200 static KActionCollection * actionCollection();
203 * if running == true starts the scheduler
204 * if running == false stops the scheduler
206 static void setSchedulerRunning(bool running=true);
209 * Returns true if the scheduler has running jobs.
211 static bool schedulerRunning();
214 * Sets the given view to the TransferTreeModel object
216 static void addTransferView(QAbstractItemView * view);
219 * Adds a Settings tab for every plugins that needs one
220 * to the KTabWidget.
222 static void setPluginsSettingsWidget(KTabWidget * widget);
225 * Gets all transfers which are running
227 static QList<TransferHandler*> allTransfers();
229 static void setTrayDownloading(bool running);
231 private:
232 KGet();
233 ~KGet();
236 * Scans for all the available plugins and creates the proper
237 * transfer object for the given src url
239 * @param src the source url
240 * @param dest the destination url
241 * @param groupName the group name
242 * @param start Specifies if the newly added transfers should be started.
244 static void createTransfer(const KUrl &src, const KUrl &dest, const QString& groupName = QString(), bool start = false, const QDomElement * e = 0);
247 * Posts an addedTransferGroupEvent to all the observers
249 * @param group the group that has been added to the group
251 static void postAddedTransferGroupEvent(TransferGroup * group, ModelObserver * observer = 0);
254 * Posts an removedTransferGroupEvent to all the observers
256 * @param group the group that has been removed from the group
258 static void postRemovedTransferGroupEvent(TransferGroup * group, ModelObserver * observer = 0);
260 static KUrl urlInputDialog();
261 static QString destInputDialog();
262 static QString getSaveDirectoryFromExceptions(const KUrl &filename);
264 static bool isValidSource(const KUrl &source);
265 static bool isValidDestDirectory(const QString& destDir);
268 * if the given url is a file that already exists the function asks
269 * the user to confirm the overwriting action
271 * @param destFile the url of the destination file
272 * @return true if the destination file is ok, otherwise returns false
274 static bool isValidDestUrl(const KUrl &destUrl);
276 static KUrl getValidDestUrl(const QString& destDir, const KUrl &srcUrl);
278 static TransferGroup * findGroup(const QString& groupName);
279 static Transfer * findTransfer(KUrl url);
281 //Plugin-related functions
282 static void loadPlugins();
283 static void unloadPlugins();
284 static KGetPlugin * createPluginFromService( const KService::Ptr service );
288 * Deletes the given file, if possible.
290 * @param url The file to delete
292 * @return true if the file was successully deleted: if the given url
293 * is a directory or if it is not local it returns false and shows a
294 * warning message.
296 static bool safeDeleteFile( const KUrl& url );
298 static QList<ModelObserver *> m_observers;
300 //Interview models
301 static TransferTreeModel * m_transferTreeModel;
302 static TransferTreeSelectionModel * m_selectionModel;
304 //Lists of available plugins
305 static QList<TransferFactory *> m_transferFactories;
307 //List of KLibrary objects (used to release the plugins from memory)
308 static QList<KLibrary *> m_pluginKLibraries;
310 //pointer to the Main window
311 static MainWindow * m_mainWindow;
313 //Scheduler object
314 static Scheduler * m_scheduler;
318 class TransferFinishedObserver : public TransferObserver
320 public:
321 TransferFinishedObserver();
323 void transferChangedEvent(TransferHandler * transfer);
325 private:
326 void checkAndFinish();
327 void checkAndUpdateSystemTray();
330 #endif