summaryrefslogtreecommitdiffstats
path: root/apps/ktorrent/ktorrentcore.h
blob: 8e256535fedc019865911f8b3d7746ccb56a0d39 (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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   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 WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  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 Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef KTORRENTCORE_H
#define KTORRENTCORE_H

#include <tqmap.h>
#include <tqtimer.h>
#include <tqcstring.h>
#include <util/constants.h>
#include <interfaces/coreinterface.h>

typedef TQValueList<TQCString> QCStringList;

namespace bt
{
	class Server;
	class QueueManager;
	class TorrentControl;
}

namespace TDEIO
{
	class Job;
}

namespace kt
{
	class Plugin;
	class PluginManager;
	class GUIInterface;
	class TorrentInterface;
	class GroupManager;
}




class KProgress;

/**
 * @author Joris Guisson
 * @brief Keeps track of all TorrentInterface objects
 *
 * This class keeps track of all TorrentInterface objects.
 */
class KTorrentCore : public kt::CoreInterface
{
	Q_OBJECT
  
public:
	KTorrentCore(kt::GUIInterface* gui);
	virtual ~KTorrentCore();
	

	kt::PluginManager & getPluginManager() {return *pman;}
	const kt::PluginManager & getPluginManager() const {return *pman;}
	
	/**
	 * Load all torrents from the data dir.
	 */
	void loadTorrents();
	
	/**
	 * Load an existing torrent, which has already a properly set up torX dir.
	 * @param tor_dir The torX dir
	 */
	void loadExistingTorrent(const TQString & tor_dir);
	
	/**
	 * Set the maximum number of simultanious downloads.
	 * @param max The max num (0 == no limit)
	 */
	void setMaxDownloads(int max);
	
	/**
	 * Set the maximum number of simultaneous seeds.
	 * @param max The max num (0 == no limit)
	 */
	void setMaxSeeds(int max);
	
	/**
	 * Set wether or not we should keep seeding after
	 * a download has finished.
	 * @param ks Keep seeding yes or no
	 */
	void setKeepSeeding(bool ks);
	
	/**
	 * Change the data dir. This involves copying
	 * all data from the old dir to the new.
	 * This can offcourse go horribly wrong, therefore
	 * if it doesn't succeed it returns false
	 * and leaves everything where it supposed to be.
	 * @param new_dir The new directory
	 */
	bool changeDataDir(const TQString & new_dir);
	
	/**
	 * Save active torrents on exit.
	 */
	void onExit();

	/**
	 * Start all, takes into account the maximum number of downloads.
	 * @param type Wether to start all downloads or seeds. 1=Downloads, 2=Seeds, 3=All
	 */
	void startAll(int type);

	/**
	 * Stop all torrents.
	 * @param type Wether to start all downloads or seeds. 1=Downloads, 2=Seeds, 3=All
	 */
	void stopAll(int type);

	/**
	 * Make a torrent file
	 * @param file The file or dir to make a torrent of
	 * @param trackers A list of trackers
	 * @param chunk_size The size of each chunk (in KB)
	 * @param name The torrents name (usually filename)
	 * @param comments The comments
	 * @param seed Wether or not to start seeding or not
	 * @param output_file File to store the torrent file
	 * @param priv_tor Is this a private torrent
	 * @param prog Progress bar to update
	 */
	void makeTorrent(const TQString & file,const TQStringList & trackers,
					 int chunk_size,const TQString & name,const TQString & comments,
					 bool seed,const TQString & output_file,bool priv_tor,KProgress* prog, bool decentralized);

	CurrentStats getStats();

	/**
	 * Switch the port when no torrents are running.
	 * @param port The new port
	 * @return true if we can, false if there are torrents running
	 */
	bool changePort(bt::Uint16 port);

	///  Get the number of torrents running (including seeding torrents).
	bt::Uint32 getNumTorrentsRunning() const;

	///  Get the number of torrents not running.
	bt::Uint32 getNumTorrentsNotRunning() const;
	
	///Inserts blocked IP range into IPBlocklist
	void addBlockedIP(TQString& ip);
	
	///Removes blocked IP range from IPBlocklist
	void removeBlockedIP(TQString& ip);
	
	/**
	 * Find the next free torX dir.
	 * @return Path to the dir (including the torX part)
	 */
	TQString findNewTorrentDir() const;
	
	/**
	 * Load plugins. 
	 */
	void loadPlugins();
	
	virtual void load(const KURL& url);
	virtual void loadSilently(const KURL& url);
	virtual void loadSilentlyDir(const KURL& url, const KURL& savedir);
	virtual float getGlobalMaxShareRatio() const;
	
	
	bt::QueueManager* getQueueManager();
	
	kt::GroupManager* getGroupManager() const { return gman; }
	void setGroupManager(kt::GroupManager* g) { gman = g; }
	
	///Gets the number of torrents running
	int getNumRunning(bool onlyDownloads = true, bool onlySeeds = false) const;
	///Gets the number of torrents that are in state 'download' - total
	int countDownloads() const;
	///Gets the number of torrents that are in state 'seed' - total
	int countSeeds() const;
	
	int getMaxDownloadSpeed();
	int getMaxUploadSpeed();
	void setMaxDownloadSpeed(int v);
	void setMaxUploadSpeed(int v);

	void setPausedState(bool pause);
	
	kt::TorrentInterface* getTorFromNumber(int tornumber);
	TQValueList<int> getTorrentNumbers(int type);
	unsigned int getFileCount(int tornumber);
	QCStringList getFileNames(int tornumber);
	TQValueList<int> getFilePriorities(int tornumber);
	void setFilePriority(kt::TorrentInterface* tc, bt::Uint32 index, int priority);
	void announceByTorNum(int tornumber);


public slots:
	/**
	 * Load a torrent file. Pops up an error dialog
	 * if something goes wrong.
	 * @param file The torrent file (always a local file)
	 * @param dir Directory to save the data
	 * @param silently Wether or not to do this silently
	 */
	bool load(const TQString & file,const TQString & dir,bool silently);
	
	/**
	 * Load a torrent file. Pops up an error dialog
	 * if something goes wrong.
	 * @param data Byte array of the torrent file
	 * @param dir Directory to save the data
	 * @param silently Wether or not to do this silently
	 */
	bool load(const TQByteArray & data,const TQString & dir,bool silently, const KURL& url);
	
	/**
	 * Remove a download.This will delete all temp
	 * data from this TorrentInterface And delete the
	 * TorrentInterface itself. It can also potentially
	 * start a new download (when one is waiting to be downloaded).
	 * @param tc
	 * @param data_to 
	 */
	void remove(kt::TorrentInterface* tc,bool data_to);

	/**
	 * Update all torrents.
	 */
	void update();
	
		/**
	 * Start a torrent, takes into account the maximum number of downloads.
	 * @param tc The TorrentInterface
		 */
	void start(kt::TorrentInterface* tc);

	/**
	 * Stop a torrent, may start another download if it hasn't been started.
	 * @param tc The TorrentInterface
	 */
	void stop(kt::TorrentInterface* tc, bool user = false);
	
	/**
	 * Enqueue/Dequeue function. Places a torrent in queue. 
	 * If the torrent is already in queue this will remove it from queue.
	 * @param tc TorrentControl pointer.
	 */
	void queue(kt::TorrentInterface* tc);
	
	/**
	 * A torrent is about to be started. We will do some file checks upon this signal.
	 * @param tc The TorrentInterface
	*/
	void aboutToBeStarted(kt::TorrentInterface* tc,bool & ret);
	
	/**
	 * User tried to enqueue a torrent that has reached max share ratio.
	 * Emits appropriate signal.
	 */
	void enqueueTorrentOverMaxRatio(kt::TorrentInterface* tc);
	
	/**
	 * Do a data check on a torrent
	 * @param tc 
	 */
	void doDataCheck(kt::TorrentInterface* tc);
	
	///Fires when disk space is running low
	void onLowDiskSpace(kt::TorrentInterface* tc, bool stopped);
	
signals:
	/**
	* TorrentCore torrents have beed updated. Stats are changed.
	**/
	void statsUpdated();
	
	/**
	 * Emitted when a torrent has reached it's max share ratio.
	 * @param tc The torrent
	 */
	void maxShareRatioReached(kt::TorrentInterface* tc);
	
	/**
	 * Emitted when a torrent has reached it's max seed time
	 * @param tc The torrent
	 */
	void maxSeedTimeReached(kt::TorrentInterface* tc);
	
	/**
	 * Corrupted data has been detected.
	 * @param tc The torrent with the corrupted data
	 */
	void corruptedData(kt::TorrentInterface* tc);
	
	/**
	 * User tried to enqueue a torrent that has reached max share ratio. It's not possible.
	 * Signal should be connected to SysTray slot which shows appropriate KPassivePopup info.
	 * @param tc The torrent in question.
	 */
	void queuingNotPossible(kt::TorrentInterface* tc);
	
	/**
	 * Emitted when a torrent cannot be started
	 * @param tc The torrent
	 * @param reason The reason
	 */
	void canNotStart(kt::TorrentInterface* tc,kt::TorrentStartResponse reason);
	
	/**
	 * Diskspace is running low.
	 * Signal should be connected to SysTray slot which shows appropriate KPassivePopup info. 
	 * @param tc The torrent in question.
	 */
	void lowDiskSpace(kt::TorrentInterface* tc, bool stopped);

private:
	void rollback(const TQPtrList<kt::TorrentInterface> & success);
	void connectSignals(kt::TorrentInterface* tc);
	bool init(bt::TorrentControl* tc,bool silently);
	
private slots:
	void torrentFinished(kt::TorrentInterface* tc);
	void slotStoppedByError(kt::TorrentInterface* tc, TQString msg);
	void torrentSeedAutoStopped(kt::TorrentInterface* tc,kt::AutoStopReason reason);
	void downloadFinished(TDEIO::Job *job);
	void downloadFinishedSilently(TDEIO::Job *job);
	void emitCorruptedData(kt::TorrentInterface* tc);
	
private:
	TQString data_dir;
	int max_downloads;
	bool keep_seeding;
	TQTimer update_timer;
	bt::Uint64 removed_bytes_up,removed_bytes_down;
	kt::PluginManager* pman;
	bt::QueueManager* qman;
	kt::GroupManager* gman;
	TQMap<TDEIO::Job*,KURL> custom_save_locations; // map to store save locations
};

#endif