summaryrefslogtreecommitdiffstats
path: root/src/trayicon.cpp
blob: 6884564fb642cf790d5eedbaca665952c2ad1d9e (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
/***************************************************************************
 * $Id: trayicon.cpp,v 1.10 2008/07/31 19:56:28 hoganrobert Exp $
 *   Copyright (C) 2006 - 2008 Robert Hogan                                *
 *   robert@roberthogan.net                                                *
 *                                                                         *
 *   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 St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************
 * This is a modified version of trayicon.cpp from ktorrent.               *
 * Original copyright notice follows:                                      *
 ***************************************************************************
 *   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 <kurldrag.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include "trayicon.h"
#include <ntqtooltip.h>
#include <kpassivepopup.h>
#include <ntqstringlist.h>
#include "trayhoverpopup.h"
#include "torkconfig.h"


TrayIcon::TrayIcon( tork *parent, const char *name)
		: KSystemTray(parent, name)
{
	m_kt_pix = loadIcon("tork");
	setPixmap(m_kt_pix);
	paint=new TQPainter( this );
	drawContents ( paint );
	m_parent = parent;

    setAcceptDrops( TRUE );
	m_hover_popup = new TrayHoverPopup(m_kt_pix,this);

	connect(this,SIGNAL(quitSelected()),kapp,SLOT(quit()));
	
	connect(this->contextMenu(),SIGNAL(aboutToShow()),m_hover_popup,SLOT(contextMenuAboutToShow()));
	connect(this->contextMenu(),SIGNAL(aboutToHide()),m_hover_popup,SLOT(contextMenuAboutToHide()));
}

TrayIcon::~TrayIcon()
{}

void  TrayIcon::dragEnterEvent(TQDragEnterEvent *e)
{
    e->accept (KURLDrag::canDecode(e));
}

void  TrayIcon::dropEvent (TQDropEvent *o)
{
    KURL::List list;
    TQString text;
    if ( KURLDrag::decode( o, list ) )
            droppedfile(list);
}

void  TrayIcon::droppedfile (KURL::List url)
{
    m_parent->startEverything();
    if (TorkConfig::availablePrograms().contains("konqueror"))
      m_parent->enableKDEAndLaunchKonqWithUrl(url.first().url());
    else if (TorkConfig::availablePrograms().contains("firefox"))
      m_parent->anonymizedFirefox(url.first().url());
}


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

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

void TrayIcon::updateStats(const TQString & downloadBandwidthHist, 
                           const TQString & uploadBandwidthHist,
                           const TQString & downloadBandwidth, const TQString & uploadBandwidth,
                           const TQStringList & serverReport, const TQStringList & clientReport,
                           const TQString & curBandwidthRate)
{

    TQString serverStatus;
	for ( TQStringList::ConstIterator it = serverReport.begin(); it != serverReport.end(); ++it )
	{
		if ((*it).isEmpty())
			continue;
        if ((it) != serverReport.begin())
            serverStatus.append(TQString("<tr><td></td><td colspan='2'>%1</td></tr>").arg((*it)));
    }

	TQString tip = i18n("<table cellpadding='2' cellspacing='2'align='center'>"
                        "<tr>"
                          "<td><b>Client:</b></td>"
                          "<td colspan='2'>%1</td>"
                        "</tr>")
                        .arg(*clientReport.begin());
    if (!TorkConfig::clientOnly()){
        tip.append(i18n("<tr>"
                          "<td><b>Server:</b></td>"
                          "<td colspan='2'>Nickname <b>%1</b></td>"
                        "</tr>"
                        "<tr>"
                          "<td></td>"
                          "<td colspan='2'>%2</td>"
                        "</tr>"
                        "%3")
                        .arg(TorkConfig::nickName())
                        .arg(*serverReport.begin())
                        .arg(serverStatus));
    }
    tip.append(i18n(    
                        "<tr>"
                          "<td></td>"
                          "<td><b>BW Down</b></td>"
                          "<td><b>BW Up</b></td>"
                        "</tr>"
                        "<tr>"
                          "<td><b>Speed:</b></td>"
                          "<td><font color='#1c9a1c'>%1</font></td>"
                          "<td><font color='#990000'>%2</font></td>"
                        "</tr>"
                        "<tr>"
                          "<td><b>Total:</b></td>"
                          "<td><font color='#1c9a1c'>%3</font></td>"
                          "<td> <font color='#990000'>%4</font></td>"
                        "</tr>"
                        "<tr>"
                          "<td><b>Max:</b></td>"
                          "<td><font color='#1c9a1c'>%5</font></td>"
                          "<td> <font color='#990000'>%6</font></td>"
                        "</tr>"
                        "</table>")
                        .arg(downloadBandwidth)
                        .arg(uploadBandwidth)
                        .arg(downloadBandwidthHist)
                        .arg(uploadBandwidthHist)
                        .arg(curBandwidthRate)
                        .arg(curBandwidthRate));

	m_hover_popup->updateText(tip);
	
}

SetMaxRate::SetMaxRate( tork *parent, const char *name):TDEPopupMenu(parent, name)
{
    m_parent = parent;
    m_rate = TorkConfig::bandwidthRate();
	makeMenu();
	connect(this,SIGNAL(activated(int)),this,SLOT(rateSelected(int)));
}
void SetMaxRate::makeMenu()
{

	int maxBandwidth=(m_rate > 0) ? m_rate : 300 ;
	int delta = 0;
	int maxBandwidthRounded;

	setCheckable(true);
	insertTitle(i18n("Bandwidth Limit"));

	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(m_rate == valuePair[j] && j==0)
				{
					setItemChecked(insertItem(TQString("%1 KB/s").arg(valuePair[j]),-1, (j == 0) ? 2 : (int)count()), true);
				}
				else
					insertItem(TQString("%1 KB/s").arg(valuePair[j]),-1, (j == 0) ? 2 : (int)count());
			}
		}

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

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

void SetMaxRate::rateSelected(int id)
{

//     kdDebug() << id << endl;
//     kdDebug() << text(id) << endl;
//     kdDebug() << text(id).toInt() << endl;

    //QT Bug: The value referenced by the TQPopUpMenu ID often has an ampersand
    TQString textID = text(id);
    textID.replace("&","");
    textID.replace(" KB/s","");

//     kdDebug() << textID << endl;
//     kdDebug() << textID.toInt() << endl;

    m_rate=textID.toInt();
	m_parent->setBandwidthFromSysTray(m_rate);
	TorkConfig::setBandwidthRate(m_rate);
	TorkConfig::setBandwidthBurst(m_rate*2);
	TorkConfig::writeConfig();

	update();
}


#include "trayicon.moc"