Upstream tarball 9440
[amule.git] / src / MuleTrayIcon.cpp
blob5c8bfa28addbab7becbffe257c6fe6813739cd0e
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2008 Patrizio Bassi ( hetfield@amule.org )
6 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <wx/app.h>
29 #include "MuleTrayIcon.h"
31 #include <common/ClientVersion.h>
32 #include <common/Constants.h>
34 #include "pixmaps/mule_TrayIcon_big.ico.xpm"
35 #include "pixmaps/mule_Tr_yellow_big.ico.xpm"
36 #include "pixmaps/mule_Tr_grey_big.ico.xpm"
38 #include <wx/menu.h>
40 #include "amule.h" // Needed for theApp
41 #include "amuleDlg.h" // Needed for IsShown
42 #include "Preferences.h" // Needed for thePrefs
43 #include "ServerConnect.h" // Needed for CServerConnect
44 #include "Server.h" // Needed for CServer
45 #include "StatisticsDlg.h" // Needed for CStatisticsDlg::getColors()
46 #include "Statistics.h" // Needed for theStats
47 #include <common/Format.h> // Needed for CFormat
48 #include "Logger.h"
51 // Pop-up menu clickable entries
52 enum {
53 TRAY_MENU_INFO = 0,
54 TRAY_MENU_CLIENTINFO=0,
55 TRAY_MENU_CLIENTINFO_ITEM = 13007,
56 TRAY_MENU_DISCONNECT,
57 TRAY_MENU_CONNECT,
58 TRAY_MENU_HIDE,
59 TRAY_MENU_SHOW,
60 TRAY_MENU_EXIT,
61 UPLOAD_ITEM1=12340,
62 UPLOAD_ITEM2=12341,
63 UPLOAD_ITEM3=12342,
64 UPLOAD_ITEM4=12343,
65 UPLOAD_ITEM5=12344,
66 UPLOAD_ITEM6=12345,
67 DOWNLOAD_ITEM1=54320,
68 DOWNLOAD_ITEM2=54321,
69 DOWNLOAD_ITEM3=54322,
70 DOWNLOAD_ITEM4=54323,
71 DOWNLOAD_ITEM5=54324,
72 DOWNLOAD_ITEM6=54325
75 /****************************************************/
76 /******************* Event Table ********************/
77 /****************************************************/
79 BEGIN_EVENT_TABLE(CMuleTrayIcon, wxTaskBarIcon)
80 EVT_TASKBAR_LEFT_DCLICK(CMuleTrayIcon::SwitchShow)
81 EVT_MENU( TRAY_MENU_EXIT, CMuleTrayIcon::Close)
82 EVT_MENU( TRAY_MENU_CONNECT, CMuleTrayIcon::ServerConnection)
83 EVT_MENU( TRAY_MENU_DISCONNECT, CMuleTrayIcon::ServerConnection)
84 EVT_MENU( TRAY_MENU_HIDE, CMuleTrayIcon::ShowHide)
85 EVT_MENU( TRAY_MENU_SHOW, CMuleTrayIcon::ShowHide)
86 EVT_MENU( UPLOAD_ITEM1, CMuleTrayIcon::SetUploadSpeed)
87 EVT_MENU( UPLOAD_ITEM2, CMuleTrayIcon::SetUploadSpeed)
88 EVT_MENU( UPLOAD_ITEM3, CMuleTrayIcon::SetUploadSpeed)
89 EVT_MENU( UPLOAD_ITEM4, CMuleTrayIcon::SetUploadSpeed)
90 EVT_MENU( UPLOAD_ITEM5, CMuleTrayIcon::SetUploadSpeed)
91 EVT_MENU( UPLOAD_ITEM6, CMuleTrayIcon::SetUploadSpeed)
92 EVT_MENU( DOWNLOAD_ITEM1, CMuleTrayIcon::SetDownloadSpeed)
93 EVT_MENU( DOWNLOAD_ITEM2, CMuleTrayIcon::SetDownloadSpeed)
94 EVT_MENU( DOWNLOAD_ITEM3, CMuleTrayIcon::SetDownloadSpeed)
95 EVT_MENU( DOWNLOAD_ITEM4, CMuleTrayIcon::SetDownloadSpeed)
96 EVT_MENU( DOWNLOAD_ITEM5, CMuleTrayIcon::SetDownloadSpeed)
97 EVT_MENU( DOWNLOAD_ITEM6, CMuleTrayIcon::SetDownloadSpeed)
98 END_EVENT_TABLE()
100 /****************************************************/
101 /************ Constructor / Destructor **************/
102 /****************************************************/
104 long GetSpeedFromString(wxString label){
105 long temp;
106 label.Replace(wxT("kB/s"),wxT(""),TRUE);
107 label.Trim(FALSE);
108 label.Trim(TRUE);
109 label.ToLong(&temp);
110 return temp;
113 void CMuleTrayIcon::SetUploadSpeed(wxCommandEvent& event){
115 wxObject* obj=event.GetEventObject();
116 if (obj!=NULL) {
117 wxMenu *menu = dynamic_cast<wxMenu *>(obj);
118 if (menu) {
119 wxMenuItem* item=menu->FindItem(event.GetId());
120 if (item!=NULL) {
121 long temp;
122 if (item->GetLabel()==(_("Unlimited"))) {
123 temp=UNLIMITED;
125 else {
126 temp=GetSpeedFromString(item->GetLabel());
128 thePrefs::SetMaxUpload(temp);
134 void CMuleTrayIcon::SetDownloadSpeed(wxCommandEvent& event){
136 wxObject* obj=event.GetEventObject();
137 if (obj!=NULL) {
138 wxMenu *menu = dynamic_cast<wxMenu *>(obj);
139 if (menu) {
140 wxMenuItem* item=menu->FindItem(event.GetId());
141 if (item!=NULL) {
142 long temp;
143 if (item->GetLabel()==(_("Unlimited"))) {
144 temp=UNLIMITED;
146 else {
147 temp=GetSpeedFromString(item->GetLabel());
149 thePrefs::SetMaxDownload(temp);
156 void CMuleTrayIcon::ServerConnection(wxCommandEvent& WXUNUSED(event))
158 wxCommandEvent evt;
159 theApp->amuledlg->OnBnConnect(evt);
163 void CMuleTrayIcon::ShowHide(wxCommandEvent& WXUNUSED(event))
165 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
169 void CMuleTrayIcon::Close(wxCommandEvent& WXUNUSED(event))
171 if (theApp->amuledlg->IsEnabled()) {
172 theApp->amuledlg->Close();
177 CMuleTrayIcon::CMuleTrayIcon()
179 Old_Icon = -1;
180 Old_SpeedSize = 0xFFFF; // must be > any possible one.
181 // Create the background icons (speed improvement)
182 HighId_Icon_size = wxIcon(mule_TrayIcon_big_ico_xpm).GetHeight();
183 LowId_Icon_size = wxIcon(mule_Tr_yellow_big_ico_xpm).GetHeight();
184 Disconnected_Icon_size = wxIcon(mule_Tr_grey_big_ico_xpm).GetHeight();
187 CMuleTrayIcon::~CMuleTrayIcon()
189 #ifdef __WXGTK__
190 // FIXME: EVIL HACK: We need to ensure that the superclass doesn't
191 // try to destroy a dangling pointer. See also CMuleTrayIcon::UpdateTray
192 // for comments on this issue.
193 if (m_iconWnd) {
194 if (wxTopLevelWindows.IndexOf((wxWindow*)m_iconWnd) == wxNOT_FOUND) {
195 m_iconWnd = NULL;
198 #endif
201 /****************************************************/
202 /***************** Public Functions *****************/
203 /****************************************************/
205 void CMuleTrayIcon::SetTrayIcon(int Icon, uint32 percent)
207 int Bar_ySize = 0;
209 switch (Icon) {
210 case TRAY_ICON_HIGHID:
211 // Most likely case, test first
212 Bar_ySize = HighId_Icon_size;
213 break;
214 case TRAY_ICON_LOWID:
215 Bar_ySize = LowId_Icon_size;
216 break;
217 case TRAY_ICON_DISCONNECTED:
218 Bar_ySize = Disconnected_Icon_size;
219 break;
220 default:
221 wxFAIL;
224 // Lookup this values for speed improvement: don't draw if not needed
225 int NewSize = (Bar_ySize * percent) / 100;
227 if ((Old_Icon != Icon) || (Old_SpeedSize != NewSize)) {
229 if ((Old_SpeedSize > NewSize) || (Old_Icon != Icon)) {
230 // We have to rebuild the icon, because bar is lower now.
231 switch (Icon) {
232 case TRAY_ICON_HIGHID:
233 // Most likely case, test first
234 CurrentIcon = wxIcon(mule_TrayIcon_big_ico_xpm);
235 break;
236 case TRAY_ICON_LOWID:
237 CurrentIcon = wxIcon(mule_Tr_yellow_big_ico_xpm);
238 break;
239 case TRAY_ICON_DISCONNECTED:
240 CurrentIcon = wxIcon(mule_Tr_grey_big_ico_xpm);
241 break;
242 default:
243 wxFAIL;
247 Old_Icon = Icon;
248 Old_SpeedSize = NewSize;
250 // Do whatever to the icon before drawing it (percent)
252 wxBitmap TempBMP;
253 TempBMP.CopyFromIcon(CurrentIcon);
255 TempBMP.SetMask(NULL);
257 IconWithSpeed.SelectObject(TempBMP);
260 // Speed bar is: centered, taking 80% of the icon heigh, and
261 // right-justified taking a 10% of the icon width.
263 // X
264 int Bar_xSize = 4;
265 int Bar_xPos = CurrentIcon.GetWidth() - 5;
267 IconWithSpeed.SetBrush(*(wxTheBrushList->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
268 IconWithSpeed.SetPen(*wxTRANSPARENT_PEN);
270 IconWithSpeed.DrawRectangle(Bar_xPos + 1, Bar_ySize - NewSize, Bar_xSize -2 , NewSize);
272 // Unselect the icon.
273 IconWithSpeed.SelectObject(wxNullBitmap);
275 // Do transparency
277 // Set a new mask with transparency set to red.
278 wxMask* new_mask = new wxMask(TempBMP, wxColour(0xFF, 0x00, 0x00));
280 TempBMP.SetMask(new_mask);
281 CurrentIcon.CopyFromBitmap(TempBMP);
283 UpdateTray();
287 void CMuleTrayIcon::SetTrayToolTip(const wxString& Tip)
289 CurrentTip = Tip;
290 UpdateTray();
293 /****************************************************/
294 /**************** Private Functions *****************/
295 /****************************************************/
297 void CMuleTrayIcon::UpdateTray()
299 #ifdef __WXGTK__
300 // FIXME: EVIL HACK: As of wxGTK-2.8.7, closing of the trayicon
301 // window (caused for instance by a crashing kicker) is not
302 // handled, with the result that the pointer to the trayicon
303 // window becomes a dangling pointer. Since we have access to
304 // the pointer, and it's created as a top-level window, it's
305 // relatively easy to force the recreation of a valid window.
306 // Ugly as hell though ....
308 // This has been repported as bug #1872724:
309 // http://sourceforge.net/tracker/index.php?func=detail&aid=1872724&group_id=9863&atid=109863
310 if (m_iconWnd) {
311 if (wxTopLevelWindows.IndexOf((wxWindow*)m_iconWnd) == wxNOT_FOUND) {
312 AddLogLineCS(_("Traybar-icon lost, trying to recreate ..."));
313 m_iconWnd = NULL;
316 #endif
318 // Icon update and Tip update
319 #ifndef __WXCOCOA__
320 if (IsOk())
321 #endif
323 SetIcon(CurrentIcon, CurrentTip);
328 wxMenu* CMuleTrayIcon::CreatePopupMenu()
330 // Creates dinamically the menu to show the user.
331 wxMenu *traymenu = new wxMenu();
332 traymenu->SetTitle(_("aMule Tray Menu"));
334 // Build the Top string name
335 wxString label = MOD_VERSION_LONG;
336 traymenu->Append(TRAY_MENU_INFO, label);
337 traymenu->AppendSeparator();
338 label = wxString(_("Speed limits:")) + wxT(" ");
340 // Check for upload limits
341 unsigned int max_upload = thePrefs::GetMaxUpload();
342 if ( max_upload == UNLIMITED ) {
343 label += _("UL: None");
345 else {
346 label += wxString::Format(_("UL: %u"), max_upload);
348 label += wxT(", ");
350 // Check for download limits
351 unsigned int max_download = thePrefs::GetMaxDownload();
352 if ( max_download == UNLIMITED ) {
353 label += _("DL: None");
355 else {
356 label += wxString::Format(_("DL: %u"), max_download);
359 traymenu->Append(TRAY_MENU_INFO, label);
360 label = wxString::Format(_("Download speed: %.1f"), theStats::GetDownloadRate() / 1024.0);
361 traymenu->Append(TRAY_MENU_INFO, label);
362 label = wxString::Format(_("Upload speed: %.1f"), theStats::GetUploadRate() / 1024.0);
363 traymenu->Append(TRAY_MENU_INFO, label);
364 traymenu->AppendSeparator();
366 // Client Info
367 wxMenu* ClientInfoMenu = new wxMenu();
368 ClientInfoMenu->SetTitle(_("Client Information"));
370 // User nick-name
372 wxString temp = CFormat(_("Nickname: %s")) % ( thePrefs::GetUserNick().IsEmpty() ? wxString(_("No Nickname Selected!")) : thePrefs::GetUserNick() );
374 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
377 // Client ID
379 wxString temp = _("ClientID: ");
381 if (theApp->IsConnectedED2K()) {
382 unsigned long id = theApp->GetED2KID();
383 temp += wxString::Format(wxT("%lu"), id);
384 } else {
385 temp += _("Not connected");
387 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
390 // Current Server and Server IP
392 wxString temp_name = _("ServerName: ");
393 wxString temp_ip = _("ServerIP: ");
395 if ( theApp->serverconnect->GetCurrentServer() ) {
396 temp_name += theApp->serverconnect->GetCurrentServer()->GetListName();
397 temp_ip += theApp->serverconnect->GetCurrentServer()->GetFullIP();
398 } else {
399 temp_name += _("Not connected");
400 temp_ip += _("Not Connected");
402 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_name);
403 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_ip);
406 // IP Address
408 wxString temp = CFormat(_("IP: %s")) % ( (theApp->GetPublicIP()) ? Uint32toStringIP(theApp->GetPublicIP()) : wxString(_("Unknown")) );
410 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
413 // TCP PORT
415 wxString temp;
416 if (thePrefs::GetPort()) {
417 temp = CFormat(_("TCP port: %d")) % thePrefs::GetPort();
418 } else {
419 temp=_("TCP port: Not ready");
421 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
424 // UDP PORT
426 wxString temp;
427 if (thePrefs::GetEffectiveUDPPort()) {
428 temp = CFormat(_("UDP port: %d")) % thePrefs::GetEffectiveUDPPort();
429 } else {
430 temp=_("UDP port: Not ready");
432 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
435 // Online Signature
437 wxString temp;
438 if (thePrefs::IsOnlineSignatureEnabled()) {
439 temp=_("Online Signature: Enabled");
441 else {
442 temp=_("Online Signature: Disabled");
444 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
447 // Uptime
449 wxString temp = CFormat(_("Uptime: %s")) % CastSecondsToHM(theStats::GetUptimeSeconds());
450 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
453 // Number of shared files
455 wxString temp = CFormat(_("Shared files: %d")) % theStats::GetSharedFileCount();
456 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
459 // Number of queued clients
461 wxString temp = CFormat(_("Queued clients: %d")) % theStats::GetWaitingUserCount();
462 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
465 // Total Downloaded
467 wxString temp = CastItoXBytes( theStats::GetSessionReceivedBytes() + thePrefs::GetTotalDownloaded() );
468 temp = CFormat(_("Total DL: %s")) % temp;
469 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
472 // Total Uploaded
474 wxString temp = CastItoXBytes( theStats::GetSessionSentBytes() + thePrefs::GetTotalUploaded() );
475 temp = CFormat(_("Total UL: %s")) % temp;
476 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
479 traymenu->Append(TRAY_MENU_CLIENTINFO,ClientInfoMenu->GetTitle(),ClientInfoMenu);
481 // Separator
482 traymenu->AppendSeparator();
484 // Upload Speed sub-menu
485 wxMenu* UploadSpeedMenu = new wxMenu();
486 UploadSpeedMenu->SetTitle(_("Upload limit"));
488 // Download Speed sub-menu
489 wxMenu* DownloadSpeedMenu = new wxMenu();
490 DownloadSpeedMenu->SetTitle(_("Download limit"));
492 // Upload Speed sub-menu
494 UploadSpeedMenu->Append(UPLOAD_ITEM1, _("Unlimited"));
496 uint32 max_ul_speed = thePrefs::GetMaxGraphUploadRate();
498 if ( max_ul_speed == UNLIMITED ) {
499 max_ul_speed = 100;
501 else if ( max_ul_speed < 10 ) {
502 max_ul_speed = 10;
505 for ( int i = 0; i < 5; i++ ) {
506 unsigned int tempspeed = (unsigned int)((double)max_ul_speed / 5) * (5 - i);
507 wxString temp = wxString::Format(wxT("%u kB/s"), tempspeed);
508 UploadSpeedMenu->Append((int)UPLOAD_ITEM1+i+1,temp);
511 traymenu->Append(0,UploadSpeedMenu->GetTitle(),UploadSpeedMenu);
513 // Download Speed sub-menu
515 DownloadSpeedMenu->Append(DOWNLOAD_ITEM1, _("Unlimited"));
517 uint32 max_dl_speed = thePrefs::GetMaxGraphDownloadRate();
519 if ( max_dl_speed == UNLIMITED ) {
520 max_dl_speed = 100;
522 else if ( max_dl_speed < 10 ) {
523 max_dl_speed = 10;
526 for ( int i = 0; i < 5; i++ ) {
527 unsigned int tempspeed = (unsigned int)((double)max_dl_speed / 5) * (5 - i);
528 wxString temp = wxString::Format(wxT("%d kB/s"), tempspeed);
529 DownloadSpeedMenu->Append((int)DOWNLOAD_ITEM1+i+1,temp);
533 traymenu->Append(0,DownloadSpeedMenu->GetTitle(),DownloadSpeedMenu);
534 // Separator
535 traymenu->AppendSeparator();
537 if (theApp->IsConnected()) {
538 //Disconnection Speed item
539 traymenu->Append(TRAY_MENU_DISCONNECT, _("Disconnect"));
540 } else {
541 //Connect item
542 traymenu->Append(TRAY_MENU_CONNECT, _("Connect"));
545 // Separator
546 traymenu->AppendSeparator();
548 if (theApp->amuledlg->IsShown()) {
549 //hide item
550 traymenu->Append(TRAY_MENU_HIDE, _("Hide aMule"));
551 } else {
552 //show item
553 traymenu->Append(TRAY_MENU_SHOW, _("Show aMule"));
556 // Separator
557 traymenu->AppendSeparator();
559 // Exit item
560 traymenu->Append(TRAY_MENU_EXIT, _("Exit"));
562 return traymenu;
565 void CMuleTrayIcon::SwitchShow(wxTaskBarIconEvent&)
567 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
569 // File_checked_for_headers