summaryrefslogtreecommitdiffstats
path: root/apps/ktorrent/trayicon.cpp
blob: 884db94685513568eeb1af6d648ac7d44168c4bf (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
373
374
375
376
377
378
379
380
/***************************************************************************
 *   Copyright (C) 2005 by                                                 *
 *   Joris Guisson <joris.guisson@gmail.com>                               *
 *   Ivan Vasic <ivasic@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.             *
 ***************************************************************************/
#include <tdepopupmenu.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include "ktorrent.h"
#include "trayicon.h"
#include <tqtooltip.h>
#include <kpassivepopup.h>
#include <interfaces/torrentinterface.h>
#include "ktorrentcore.h"
#include <interfaces/functions.h>
#include <net/socketmonitor.h>
#include <util/log.h>
#include "trayhoverpopup.h"


using namespace bt;
using namespace kt;

TrayIcon::TrayIcon( KTorrentCore* tc, TQWidget *parent, const char *name)
		: KSystemTray(parent, name)
{
	m_core = tc;
	m_kt_pix = loadIcon("ktorrent");
	setPixmap(m_kt_pix);
	paint=new TQPainter( this );
	drawContents ( paint );
	previousDownloadHeight=0;
	previousUploadHeight=0;
	
	m_hover_popup = new TrayHoverPopup(m_kt_pix,this);

	connect(this,TQ_SIGNAL(quitSelected()),kapp,TQ_SLOT(quit()));
	connect(m_core, TQ_SIGNAL(finished(kt::TorrentInterface* )),
	        this, TQ_SLOT(finished(kt::TorrentInterface* )));
	connect(m_core,TQ_SIGNAL(torrentStoppedByError(kt::TorrentInterface*, TQString )),
	        this,TQ_SLOT(torrentStoppedByError(kt::TorrentInterface*, TQString )));
	connect(m_core,TQ_SIGNAL(maxShareRatioReached( kt::TorrentInterface* )),
			this,TQ_SLOT(maxShareRatioReached( kt::TorrentInterface* )));
	connect(m_core,TQ_SIGNAL(maxSeedTimeReached(kt::TorrentInterface*)),
			this, TQ_SLOT(maxSeedTimeReached(kt::TorrentInterface*)));
	connect(m_core,TQ_SIGNAL(corruptedData( kt::TorrentInterface* )),
			this,TQ_SLOT(corruptedData( kt::TorrentInterface* )));
	connect(m_core, TQ_SIGNAL(queuingNotPossible( kt::TorrentInterface* )),
			this, TQ_SLOT(queuingNotPossible( kt::TorrentInterface* )));
	connect(m_core,TQ_SIGNAL(canNotStart(kt::TorrentInterface*, kt::TorrentStartResponse)),
			this,TQ_SLOT(canNotStart(kt::TorrentInterface*, kt::TorrentStartResponse)));
	connect(m_core, TQ_SIGNAL(lowDiskSpace(kt::TorrentInterface*, bool)),
			this, TQ_SLOT(lowDiskSpace(kt::TorrentInterface*, bool)));
	
	connect(this->contextMenu(),TQ_SIGNAL(aboutToShow()),m_hover_popup,TQ_SLOT(contextMenuAboutToShow()));
	connect(this->contextMenu(),TQ_SIGNAL(aboutToHide()),m_hover_popup,TQ_SLOT(contextMenuAboutToHide()));
}

TrayIcon::~TrayIcon()
{}

void TrayIcon::enterEvent(TQEvent* ev)
{
	KSystemTray::enterEvent(ev);
	m_hover_popup->enterEvent();
}

void TrayIcon::leaveEvent(TQEvent* )
{
	m_hover_popup->leaveEvent();
}

void TrayIcon::updateStats(const CurrentStats stats, bool showBars,int downloadBandwidth, int uploadBandwidth )
{
	TQString tip = i18n("<table cellpadding='2' cellspacing='2' align='center'><tr><td><b>Speed:</b></td><td></td></tr><tr><td>Download: <font color='#1c9a1c'>%1</font></td><td>Upload: <font color='#990000'>%2</font></td></tr><tr><td><b>Transfer:</b></td><td></td></tr><tr><td>Download: <font color='#1c9a1c'>%3</font></td><td>Upload: <font color='#990000'>%4</font></td></tr></table>")
			.arg(KBytesPerSecToString((double)stats.download_speed/1024.0))
			.arg(KBytesPerSecToString((double)stats.upload_speed/1024.0))
			.arg(BytesToString(stats.bytes_downloaded))
			.arg(BytesToString(stats.bytes_uploaded));
	m_hover_popup->updateText(tip);
	
	if(showBars)
		drawSpeedBar(stats.download_speed/1024,stats.upload_speed/1024, downloadBandwidth, uploadBandwidth);
	else if (previousDownloadHeight > 0 || previousUploadHeight > 0)
	{
		repaint(); // clear the bars if they are disabled
		previousDownloadHeight = previousUploadHeight = 0;
	}
}

void TrayIcon::drawSpeedBar(int downloadSpeed, int uploadSpeed, int downloadBandwidth, int uploadBandwidth )
{
	//check if need repaint
	if (uploadBandwidth == 0)
		 uploadBandwidth = 1;
	if (downloadBandwidth == 0)
		downloadBandwidth = 1;
		
	int DownloadHeight=((downloadSpeed*pixmap()->height())/downloadBandwidth);
	int UploadHeight=((uploadSpeed*pixmap()->height())/uploadBandwidth);
	if(previousDownloadHeight==DownloadHeight && previousUploadHeight==UploadHeight)
		return;
	
	repaint ();

	TQBrush brushD(green);
	TQBrush brushU(red);
	paint->fillRect(0,pixmap()->height(),2,-DownloadHeight,brushD);
	paint->fillRect(pixmap()->width()-2,pixmap()->height(),2,-UploadHeight,brushU);

	previousDownloadHeight=DownloadHeight;
	previousUploadHeight=UploadHeight;	
}

void TrayIcon::showPassivePopup(const TQString & msg,const TQString & title)
{
	KPassivePopup* p = KPassivePopup::message(KPassivePopup::Boxed,title,msg,m_kt_pix, this);
	p->setPalette(TQToolTip::palette());
	p->setLineWidth(1);
}


void TrayIcon::finished(TorrentInterface* tc)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	double speed_up = (double)s.bytes_uploaded / 1024.0;
	double speed_down = (double)(s.bytes_downloaded - s.imported_bytes)/ 1024.0;

	TQString msg = i18n("<b>%1</b> has completed downloading."
						"<br>Average speed: %2 DL / %3 UL.")
						.arg(s.torrent_name)
						.arg(KBytesPerSecToString(speed_down / tc->getRunningTimeDL()))
						.arg(KBytesPerSecToString(speed_up / tc->getRunningTimeUL()));

	showPassivePopup(msg,i18n("Download completed"));
}

void TrayIcon::maxShareRatioReached(kt::TorrentInterface* tc)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	TDELocale* loc = TDEGlobal::locale();
	double speed_up = (double)s.bytes_uploaded / 1024.0;
	
	TQString msg = i18n("<b>%1</b> has reached its maximum share ratio of %2 and has been stopped."
			"<br>Uploaded %3 at an average speed of %4.")
			.arg(s.torrent_name)
			.arg(loc->formatNumber(s.max_share_ratio,2))
			.arg(BytesToString(s.bytes_uploaded))
			.arg(KBytesPerSecToString(speed_up / tc->getRunningTimeUL()));
	
	showPassivePopup(msg,i18n("Seeding completed"));
}

void TrayIcon::maxSeedTimeReached(kt::TorrentInterface* tc)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	TDELocale* loc = TDEGlobal::locale();
	double speed_up = (double)s.bytes_uploaded / 1024.0;
	
	TQString msg = i18n("<b>%1</b> has reached its maximum seed time of %2 hours and has been stopped."
			"<br>Uploaded %3 at an average speed of %4.")
			.arg(s.torrent_name)
			.arg(loc->formatNumber(s.max_seed_time,2))
			.arg(BytesToString(s.bytes_uploaded))
			.arg(KBytesPerSecToString(speed_up / tc->getRunningTimeUL()));
	
	showPassivePopup(msg,i18n("Seeding completed"));
}

void TrayIcon::torrentStoppedByError(kt::TorrentInterface* tc, TQString msg)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	TQString err_msg = i18n("<b>%1</b> has been stopped with the following error: <br>%2")
				.arg(s.torrent_name).arg(msg);
	
	showPassivePopup(err_msg,i18n("Error"));
}

void TrayIcon::corruptedData(kt::TorrentInterface* tc)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	TQString err_msg = i18n("Corrupted data has been found in the torrent <b>%1</b>"
			"<br>It would be a good idea to do a data integrity check on the torrent.")
			.arg(s.torrent_name);
	showPassivePopup(err_msg,i18n("Error"));
}

void TrayIcon::queuingNotPossible(kt::TorrentInterface* tc)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
		
	TQString msg;
	TDELocale* loc = TDEGlobal::locale();
	 
	if (tc->overMaxRatio())
		msg = i18n("<b>%1</b> has reached its maximum share ratio of %2 and cannot be enqueued. Remove the limit manually if you want to continue seeding.")
				.arg(s.torrent_name).arg(loc->formatNumber(s.max_share_ratio,2));
	else
		msg = i18n("<b>%1</b> has reached its maximum seed time of %2 hours and cannot be enqueued. Remove the limit manually if you want to continue seeding.")
				.arg(s.torrent_name).arg(loc->formatNumber(s.max_seed_time,2));
	
	showPassivePopup(msg,i18n("Torrent cannot be enqueued."));
}

void TrayIcon::canNotStart(kt::TorrentInterface* tc,kt::TorrentStartResponse reason)
{
	if (!Settings::showPopups())
		return;
	
	TQString msg = i18n("Cannot start <b>%1</b> : <br>").arg(tc->getStats().torrent_name);
	switch (reason)
	{
	case kt::TQM_LIMITS_REACHED:
		if (tc->getStats().bytes_left_to_download == 0)
		{
			// is a seeder
			msg += i18n("Cannot seed more than 1 torrent. <br>",
						"Cannot seed more than %n torrents. <br>",Settings::maxSeeds());
		}
		else
		{
			msg += i18n("Cannot download more than 1 torrent. <br>",
						"Cannot download more than %n torrents. <br>",Settings::maxDownloads());
		}
		msg += i18n("Go to Settings -> Configure KTorrent, if you want to change the limits.");
		showPassivePopup(msg,i18n("Torrent cannot be started"));
		break;
	case kt::NOT_ENOUGH_DISKSPACE:
		msg += i18n("There is not enough diskspace available.");
		showPassivePopup(msg,i18n("Torrent cannot be started"));
		break;
	default:
		break;
	}
}

void TrayIcon::lowDiskSpace(kt::TorrentInterface * tc, bool stopped)
{
	if (!Settings::showPopups())
		return;
	
	const TorrentStats & s = tc->getStats();
	
	TQString msg = i18n("Your disk is running out of space.<br /><b>%1</b> is being downloaded to '%2'.").arg(s.torrent_name).arg(tc->getDataDir());
	
	if(stopped)
		msg.prepend(i18n("Torrent has been stopped.<br />"));
	
	showPassivePopup(msg,i18n("Device running out of space"));
}

SetMaxRate::SetMaxRate( KTorrentCore* tc, int t, TQWidget *parent, const char *name):TDEPopupMenu(parent, name)
{
	m_core = tc;
	type=t;
	makeMenu();
	connect(this,TQ_SIGNAL(activated (int)),this,TQ_SLOT(rateSelected(int )));
}
void SetMaxRate::makeMenu()
{

	int rate=(type==0) ? m_core->getMaxUploadSpeed() : m_core->getMaxDownloadSpeed();
	int maxBandwidth=(rate > 0) ? rate : (type==0) ? 0 : 20 ;
	int delta = 0;
	int maxBandwidthRounded;

	setCheckable(true);
	insertTitle(i18n("Speed limit in KB/s"));

	if(rate == 0)
		setItemChecked(insertItem(i18n("Unlimited")), true);
	else
		insertItem(i18n("Unlimited"));

	if((maxBandwidth%5)>=3)
		maxBandwidthRounded=maxBandwidth + 5 - (maxBandwidth%5);
	else
		maxBandwidthRounded=maxBandwidth - (maxBandwidth%5);

	for (int i = 0; i < 15; i++)
	{
		TQValueList<int> valuePair;
		if (delta == 0)
			valuePair.append(maxBandwidth);
		else
		{
			if((maxBandwidth%5)!=0)
			{
				valuePair.append(maxBandwidthRounded - delta);
				valuePair.append(maxBandwidthRounded + delta);
			}
			else
			{
				valuePair.append(maxBandwidth - delta);
				valuePair.append(maxBandwidth + delta);
			}
		}

		for (int j = 0; j < (int)valuePair.count(); j++)
		{
			if (valuePair[j] >= 1)
			{
				if(rate == valuePair[j] && j==0)
				{
					setItemChecked(insertItem(TQString("%1").arg(valuePair[j]),-1, (j == 0) ? 2 : count()), true);
				}
				else
					insertItem(TQString("%1").arg(valuePair[j]),-1, (j == 0) ? 2 : count());
			}
		}

		delta += (delta >= 50) ? 50 : (delta >= 20) ? 10 : 5;

	}
}
void SetMaxRate::update()
{
	clear();
	makeMenu();
}

void SetMaxRate::rateSelected(int id)
{
	int rate;
	TQString ratestr = text(id).remove('&');
	if (ratestr.contains(i18n("Unlimited")))
		rate = 0;
	else
		rate = ratestr.toInt();

	if(type==0)
	{
		m_core->setMaxUploadSpeed(rate);
		net::SocketMonitor::setUploadCap( Settings::maxUploadRate() * 1024);
	}
	else
	{
		m_core->setMaxDownloadSpeed(rate);
		net::SocketMonitor::setDownloadCap(Settings::maxDownloadRate()*1024);
	}
	Settings::writeConfig();

	update();
}


#include "trayicon.moc"