Nothing to see here. Really.
[amule.git] / src / MuleTrayIcon.cpp
blobb23ed8635bf8410dfad1b24a6a554e06a3943b48
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2011 Patrizio Bassi ( hetfield@amule.org )
6 // Copyright (c) 2003-2011 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"
49 #include <common/MenuIDs.h> // Needed to access menu item constants
51 /****************************************************/
52 /******************* Event Table ********************/
53 /****************************************************/
55 BEGIN_EVENT_TABLE(CMuleTrayIcon, wxTaskBarIcon)
56 EVT_TASKBAR_LEFT_DCLICK(CMuleTrayIcon::SwitchShow)
57 EVT_MENU( TRAY_MENU_EXIT, CMuleTrayIcon::Close)
58 EVT_MENU( TRAY_MENU_CONNECT, CMuleTrayIcon::ServerConnection)
59 EVT_MENU( TRAY_MENU_DISCONNECT, CMuleTrayIcon::ServerConnection)
60 EVT_MENU( TRAY_MENU_HIDE, CMuleTrayIcon::ShowHide)
61 EVT_MENU( TRAY_MENU_SHOW, CMuleTrayIcon::ShowHide)
62 EVT_MENU( UPLOAD_ITEM1, CMuleTrayIcon::SetUploadSpeed)
63 EVT_MENU( UPLOAD_ITEM2, CMuleTrayIcon::SetUploadSpeed)
64 EVT_MENU( UPLOAD_ITEM3, CMuleTrayIcon::SetUploadSpeed)
65 EVT_MENU( UPLOAD_ITEM4, CMuleTrayIcon::SetUploadSpeed)
66 EVT_MENU( UPLOAD_ITEM5, CMuleTrayIcon::SetUploadSpeed)
67 EVT_MENU( UPLOAD_ITEM6, CMuleTrayIcon::SetUploadSpeed)
68 EVT_MENU( DOWNLOAD_ITEM1, CMuleTrayIcon::SetDownloadSpeed)
69 EVT_MENU( DOWNLOAD_ITEM2, CMuleTrayIcon::SetDownloadSpeed)
70 EVT_MENU( DOWNLOAD_ITEM3, CMuleTrayIcon::SetDownloadSpeed)
71 EVT_MENU( DOWNLOAD_ITEM4, CMuleTrayIcon::SetDownloadSpeed)
72 EVT_MENU( DOWNLOAD_ITEM5, CMuleTrayIcon::SetDownloadSpeed)
73 EVT_MENU( DOWNLOAD_ITEM6, CMuleTrayIcon::SetDownloadSpeed)
74 END_EVENT_TABLE()
76 /****************************************************/
77 /************ Constructor / Destructor **************/
78 /****************************************************/
80 long GetSpeedFromString(wxString label){
81 long temp;
82 label.Replace(_("kB/s"),wxT(""),TRUE);
83 label.Trim(FALSE);
84 label.Trim(TRUE);
85 label.ToLong(&temp);
86 return temp;
89 void CMuleTrayIcon::SetUploadSpeed(wxCommandEvent& event){
91 wxObject* obj=event.GetEventObject();
92 if (obj!=NULL) {
93 wxMenu *menu = dynamic_cast<wxMenu *>(obj);
94 if (menu) {
95 wxMenuItem* item=menu->FindItem(event.GetId());
96 if (item!=NULL) {
97 long temp;
98 if (item->GetItemLabelText()==(_("Unlimited"))) {
99 temp=UNLIMITED;
101 else {
102 temp=GetSpeedFromString(item->GetItemLabelText());
104 thePrefs::SetMaxUpload(temp);
106 #ifdef CLIENT_GUI
107 // Send preferences to core.
108 theApp->glob_prefs->SendToRemote();
109 #endif
115 void CMuleTrayIcon::SetDownloadSpeed(wxCommandEvent& event){
117 wxObject* obj=event.GetEventObject();
118 if (obj!=NULL) {
119 wxMenu *menu = dynamic_cast<wxMenu *>(obj);
120 if (menu) {
121 wxMenuItem* item=menu->FindItem(event.GetId());
122 if (item!=NULL) {
123 long temp;
124 if (item->GetItemLabelText()==(_("Unlimited"))) {
125 temp=UNLIMITED;
127 else {
128 temp=GetSpeedFromString(item->GetItemLabelText());
130 thePrefs::SetMaxDownload(temp);
132 #ifdef CLIENT_GUI
133 // Send preferences to core.
134 theApp->glob_prefs->SendToRemote();
135 #endif
142 void CMuleTrayIcon::ServerConnection(wxCommandEvent& WXUNUSED(event))
144 wxCommandEvent evt;
145 theApp->amuledlg->OnBnConnect(evt);
149 void CMuleTrayIcon::ShowHide(wxCommandEvent& WXUNUSED(event))
151 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
155 void CMuleTrayIcon::Close(wxCommandEvent& WXUNUSED(event))
157 if (theApp->amuledlg->IsEnabled()) {
158 theApp->amuledlg->Close();
163 CMuleTrayIcon::CMuleTrayIcon()
165 Old_Icon = -1;
166 Old_SpeedSize = 0xFFFF; // must be > any possible one.
168 // Create the background icons (speed improvement)
169 HighId_Icon_size = wxIcon(mule_TrayIcon_big_ico_xpm).GetHeight();
170 LowId_Icon_size = wxIcon(mule_Tr_yellow_big_ico_xpm).GetHeight();
171 Disconnected_Icon_size = wxIcon(mule_Tr_grey_big_ico_xpm).GetHeight();
174 CMuleTrayIcon::~CMuleTrayIcon()
178 /****************************************************/
179 /***************** Public Functions *****************/
180 /****************************************************/
182 void CMuleTrayIcon::SetTrayIcon(int Icon, uint32 percent)
184 int Bar_ySize = 0;
186 switch (Icon) {
187 case TRAY_ICON_HIGHID:
188 // Most likely case, test first
189 Bar_ySize = HighId_Icon_size;
190 break;
191 case TRAY_ICON_LOWID:
192 Bar_ySize = LowId_Icon_size;
193 break;
194 case TRAY_ICON_DISCONNECTED:
195 Bar_ySize = Disconnected_Icon_size;
196 break;
197 default:
198 wxFAIL;
201 // Lookup this values for speed improvement: don't draw if not needed
202 int NewSize = (Bar_ySize * percent) / 100;
204 if ((Old_Icon != Icon) || (Old_SpeedSize != NewSize)) {
206 if ((Old_SpeedSize > NewSize) || (Old_Icon != Icon)) {
207 // We have to rebuild the icon, because bar is lower now.
208 switch (Icon) {
209 case TRAY_ICON_HIGHID:
210 // Most likely case, test first
211 CurrentIcon = wxIcon(mule_TrayIcon_big_ico_xpm);
212 break;
213 case TRAY_ICON_LOWID:
214 CurrentIcon = wxIcon(mule_Tr_yellow_big_ico_xpm);
215 break;
216 case TRAY_ICON_DISCONNECTED:
217 CurrentIcon = wxIcon(mule_Tr_grey_big_ico_xpm);
218 break;
219 default:
220 wxFAIL;
224 Old_Icon = Icon;
225 Old_SpeedSize = NewSize;
227 // Do whatever to the icon before drawing it (percent)
229 wxBitmap TempBMP;
230 TempBMP.CopyFromIcon(CurrentIcon);
232 TempBMP.SetMask(NULL);
234 IconWithSpeed.SelectObject(TempBMP);
237 // Speed bar is: centered, taking 80% of the icon heigh, and
238 // right-justified taking a 10% of the icon width.
240 // X
241 int Bar_xSize = 4;
242 int Bar_xPos = CurrentIcon.GetWidth() - 5;
244 IconWithSpeed.SetBrush(*(wxTheBrushList->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
245 IconWithSpeed.SetPen(*wxTRANSPARENT_PEN);
247 IconWithSpeed.DrawRectangle(Bar_xPos + 1, Bar_ySize - NewSize, Bar_xSize -2 , NewSize);
249 // Unselect the icon.
250 IconWithSpeed.SelectObject(wxNullBitmap);
252 // Do transparency
254 // Set a new mask with transparency set to red.
255 wxMask* new_mask = new wxMask(TempBMP, wxColour(0xFF, 0x00, 0x00));
257 TempBMP.SetMask(new_mask);
258 CurrentIcon.CopyFromBitmap(TempBMP);
260 UpdateTray();
264 void CMuleTrayIcon::SetTrayToolTip(const wxString& Tip)
266 CurrentTip = Tip;
267 UpdateTray();
270 /****************************************************/
271 /**************** Private Functions *****************/
272 /****************************************************/
274 void CMuleTrayIcon::UpdateTray()
276 // Icon update and Tip update
277 #ifndef __WXCOCOA__
278 if (IsOk())
279 #endif
281 SetIcon(CurrentIcon, CurrentTip);
286 wxMenu* CMuleTrayIcon::CreatePopupMenu()
288 // Creates dinamically the menu to show the user.
289 wxMenu *traymenu = new wxMenu();
290 traymenu->SetTitle(_("aMule Tray Menu"));
292 // Build the Top string name
293 wxString label = MOD_VERSION_LONG;
294 traymenu->Append(TRAY_MENU_INFO, label);
295 traymenu->AppendSeparator();
296 label = wxString(_("Speed limits:")) + wxT(" ");
298 // Check for upload limits
299 unsigned int max_upload = thePrefs::GetMaxUpload();
300 if ( max_upload == UNLIMITED ) {
301 label += _("UL: None");
303 else {
304 label += CFormat(_("UL: %u")) % max_upload;
306 label += wxT(", ");
308 // Check for download limits
309 unsigned int max_download = thePrefs::GetMaxDownload();
310 if ( max_download == UNLIMITED ) {
311 label += _("DL: None");
313 else {
314 label += CFormat(_("DL: %u")) % max_download;
317 traymenu->Append(TRAY_MENU_INFO, label);
318 label = CFormat(_("Download speed: %.1f")) % (theStats::GetDownloadRate() / 1024.0);
319 traymenu->Append(TRAY_MENU_INFO, label);
320 label = CFormat(_("Upload speed: %.1f")) % (theStats::GetUploadRate() / 1024.0);
321 traymenu->Append(TRAY_MENU_INFO, label);
322 traymenu->AppendSeparator();
324 // Client Info
325 wxMenu* ClientInfoMenu = new wxMenu();
326 ClientInfoMenu->SetTitle(_("Client Information"));
328 // User nick-name
330 wxString temp = CFormat(_("Nickname: %s")) % ( thePrefs::GetUserNick().IsEmpty() ? wxString(_("No Nickname Selected!")) : thePrefs::GetUserNick() );
332 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
335 // Client ID
337 wxString temp = _("ClientID: ");
339 if (theApp->IsConnectedED2K()) {
340 temp += CFormat(wxT("%u")) % theApp->GetED2KID();
341 } else {
342 temp += _("Not connected");
344 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
347 // Current Server and Server IP
349 wxString temp_name = _("ServerName: ");
350 wxString temp_ip = _("ServerIP: ");
352 if ( theApp->serverconnect->GetCurrentServer() ) {
353 temp_name += theApp->serverconnect->GetCurrentServer()->GetListName();
354 temp_ip += theApp->serverconnect->GetCurrentServer()->GetFullIP();
355 } else {
356 temp_name += _("Not connected");
357 temp_ip += _("Not Connected");
359 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_name);
360 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_ip);
363 // IP Address
365 wxString temp = CFormat(_("IP: %s")) % ( (theApp->GetPublicIP()) ? Uint32toStringIP(theApp->GetPublicIP()) : wxString(_("Unknown")) );
367 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
370 // TCP PORT
372 wxString temp;
373 if (thePrefs::GetPort()) {
374 temp = CFormat(_("TCP port: %d")) % thePrefs::GetPort();
375 } else {
376 temp=_("TCP port: Not ready");
378 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
381 // UDP PORT
383 wxString temp;
384 if (thePrefs::GetEffectiveUDPPort()) {
385 temp = CFormat(_("UDP port: %d")) % thePrefs::GetEffectiveUDPPort();
386 } else {
387 temp=_("UDP port: Not ready");
389 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
392 // Online Signature
394 wxString temp;
395 if (thePrefs::IsOnlineSignatureEnabled()) {
396 temp=_("Online Signature: Enabled");
398 else {
399 temp=_("Online Signature: Disabled");
401 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
404 // Uptime
406 wxString temp = CFormat(_("Uptime: %s")) % CastSecondsToHM(theStats::GetUptimeSeconds());
407 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
410 // Number of shared files
412 wxString temp = CFormat(_("Shared files: %d")) % theStats::GetSharedFileCount();
413 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
416 // Number of queued clients
418 wxString temp = CFormat(_("Queued clients: %d")) % theStats::GetWaitingUserCount();
419 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
422 // Total Downloaded
424 wxString temp = CastItoXBytes(theStats::GetTotalReceivedBytes());
425 temp = CFormat(_("Total DL: %s")) % temp;
426 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
429 // Total Uploaded
431 wxString temp = CastItoXBytes(theStats::GetTotalSentBytes());
432 temp = CFormat(_("Total UL: %s")) % temp;
433 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
436 traymenu->Append(TRAY_MENU_CLIENTINFO,ClientInfoMenu->GetTitle(),ClientInfoMenu);
438 // Separator
439 traymenu->AppendSeparator();
441 // Upload Speed sub-menu
442 wxMenu* UploadSpeedMenu = new wxMenu();
443 UploadSpeedMenu->SetTitle(_("Upload limit"));
445 // Download Speed sub-menu
446 wxMenu* DownloadSpeedMenu = new wxMenu();
447 DownloadSpeedMenu->SetTitle(_("Download limit"));
449 // Upload Speed sub-menu
451 UploadSpeedMenu->Append(UPLOAD_ITEM1, _("Unlimited"));
453 uint32 max_ul_speed = thePrefs::GetMaxGraphUploadRate();
455 if ( max_ul_speed == UNLIMITED ) {
456 max_ul_speed = 100;
458 else if ( max_ul_speed < 10 ) {
459 max_ul_speed = 10;
462 for ( int i = 0; i < 5; i++ ) {
463 unsigned int tempspeed = (unsigned int)((double)max_ul_speed / 5) * (5 - i);
464 wxString temp = CFormat(wxT("%u %s")) % tempspeed % _("kB/s");
465 UploadSpeedMenu->Append((int)UPLOAD_ITEM1+i+1,temp);
468 traymenu->Append(0,UploadSpeedMenu->GetTitle(),UploadSpeedMenu);
470 // Download Speed sub-menu
472 DownloadSpeedMenu->Append(DOWNLOAD_ITEM1, _("Unlimited"));
474 uint32 max_dl_speed = thePrefs::GetMaxGraphDownloadRate();
476 if ( max_dl_speed == UNLIMITED ) {
477 max_dl_speed = 100;
479 else if ( max_dl_speed < 10 ) {
480 max_dl_speed = 10;
483 for ( int i = 0; i < 5; i++ ) {
484 unsigned int tempspeed = (unsigned int)((double)max_dl_speed / 5) * (5 - i);
485 wxString temp = CFormat(wxT("%d %s")) % tempspeed % _("kB/s");
486 DownloadSpeedMenu->Append((int)DOWNLOAD_ITEM1+i+1,temp);
490 traymenu->Append(0,DownloadSpeedMenu->GetTitle(),DownloadSpeedMenu);
491 // Separator
492 traymenu->AppendSeparator();
494 if (theApp->IsConnected()) {
495 //Disconnection Speed item
496 traymenu->Append(TRAY_MENU_DISCONNECT, _("Disconnect"));
497 } else {
498 //Connect item
499 traymenu->Append(TRAY_MENU_CONNECT, _("Connect"));
502 // Separator
503 traymenu->AppendSeparator();
505 if (theApp->amuledlg->IsShown()) {
506 //hide item
507 traymenu->Append(TRAY_MENU_HIDE, _("Hide aMule"));
508 } else {
509 //show item
510 traymenu->Append(TRAY_MENU_SHOW, _("Show aMule"));
513 // Separator
514 traymenu->AppendSeparator();
516 // Exit item
517 traymenu->Append(TRAY_MENU_EXIT, _("Exit"));
519 return traymenu;
522 void CMuleTrayIcon::SwitchShow(wxTaskBarIconEvent&)
524 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
526 // File_checked_for_headers