Fix for libraries being compiled unconditionally
[amule.git] / src / MuleTrayIcon.cpp
blobc9e62541d8c0caaa5c21cfdfbb8515c6a20dfbcf
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->GetItemLabelText()==(_("Unlimited"))) {
123 temp=UNLIMITED;
125 else {
126 temp=GetSpeedFromString(item->GetItemLabelText());
128 thePrefs::SetMaxUpload(temp);
130 #ifdef CLIENT_GUI
131 // Send preferences to core.
132 theApp->glob_prefs->SendToRemote();
133 #endif
139 void CMuleTrayIcon::SetDownloadSpeed(wxCommandEvent& event){
141 wxObject* obj=event.GetEventObject();
142 if (obj!=NULL) {
143 wxMenu *menu = dynamic_cast<wxMenu *>(obj);
144 if (menu) {
145 wxMenuItem* item=menu->FindItem(event.GetId());
146 if (item!=NULL) {
147 long temp;
148 if (item->GetItemLabelText()==(_("Unlimited"))) {
149 temp=UNLIMITED;
151 else {
152 temp=GetSpeedFromString(item->GetItemLabelText());
154 thePrefs::SetMaxDownload(temp);
156 #ifdef CLIENT_GUI
157 // Send preferences to core.
158 theApp->glob_prefs->SendToRemote();
159 #endif
166 void CMuleTrayIcon::ServerConnection(wxCommandEvent& WXUNUSED(event))
168 wxCommandEvent evt;
169 theApp->amuledlg->OnBnConnect(evt);
173 void CMuleTrayIcon::ShowHide(wxCommandEvent& WXUNUSED(event))
175 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
179 void CMuleTrayIcon::Close(wxCommandEvent& WXUNUSED(event))
181 if (theApp->amuledlg->IsEnabled()) {
182 theApp->amuledlg->Close();
187 CMuleTrayIcon::CMuleTrayIcon()
189 Old_Icon = -1;
190 Old_SpeedSize = 0xFFFF; // must be > any possible one.
192 // Create the background icons (speed improvement)
193 HighId_Icon_size = wxIcon(mule_TrayIcon_big_ico_xpm).GetHeight();
194 LowId_Icon_size = wxIcon(mule_Tr_yellow_big_ico_xpm).GetHeight();
195 Disconnected_Icon_size = wxIcon(mule_Tr_grey_big_ico_xpm).GetHeight();
198 CMuleTrayIcon::~CMuleTrayIcon()
202 /****************************************************/
203 /***************** Public Functions *****************/
204 /****************************************************/
206 void CMuleTrayIcon::SetTrayIcon(int Icon, uint32 percent)
208 int Bar_ySize = 0;
210 switch (Icon) {
211 case TRAY_ICON_HIGHID:
212 // Most likely case, test first
213 Bar_ySize = HighId_Icon_size;
214 break;
215 case TRAY_ICON_LOWID:
216 Bar_ySize = LowId_Icon_size;
217 break;
218 case TRAY_ICON_DISCONNECTED:
219 Bar_ySize = Disconnected_Icon_size;
220 break;
221 default:
222 wxFAIL;
225 // Lookup this values for speed improvement: don't draw if not needed
226 int NewSize = (Bar_ySize * percent) / 100;
228 if ((Old_Icon != Icon) || (Old_SpeedSize != NewSize)) {
230 if ((Old_SpeedSize > NewSize) || (Old_Icon != Icon)) {
231 // We have to rebuild the icon, because bar is lower now.
232 switch (Icon) {
233 case TRAY_ICON_HIGHID:
234 // Most likely case, test first
235 CurrentIcon = wxIcon(mule_TrayIcon_big_ico_xpm);
236 break;
237 case TRAY_ICON_LOWID:
238 CurrentIcon = wxIcon(mule_Tr_yellow_big_ico_xpm);
239 break;
240 case TRAY_ICON_DISCONNECTED:
241 CurrentIcon = wxIcon(mule_Tr_grey_big_ico_xpm);
242 break;
243 default:
244 wxFAIL;
248 Old_Icon = Icon;
249 Old_SpeedSize = NewSize;
251 // Do whatever to the icon before drawing it (percent)
253 wxBitmap TempBMP;
254 TempBMP.CopyFromIcon(CurrentIcon);
256 TempBMP.SetMask(NULL);
258 IconWithSpeed.SelectObject(TempBMP);
261 // Speed bar is: centered, taking 80% of the icon heigh, and
262 // right-justified taking a 10% of the icon width.
264 // X
265 int Bar_xSize = 4;
266 int Bar_xPos = CurrentIcon.GetWidth() - 5;
268 IconWithSpeed.SetBrush(*(wxTheBrushList->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
269 IconWithSpeed.SetPen(*wxTRANSPARENT_PEN);
271 IconWithSpeed.DrawRectangle(Bar_xPos + 1, Bar_ySize - NewSize, Bar_xSize -2 , NewSize);
273 // Unselect the icon.
274 IconWithSpeed.SelectObject(wxNullBitmap);
276 // Do transparency
278 // Set a new mask with transparency set to red.
279 wxMask* new_mask = new wxMask(TempBMP, wxColour(0xFF, 0x00, 0x00));
281 TempBMP.SetMask(new_mask);
282 CurrentIcon.CopyFromBitmap(TempBMP);
284 UpdateTray();
288 void CMuleTrayIcon::SetTrayToolTip(const wxString& Tip)
290 CurrentTip = Tip;
291 UpdateTray();
294 /****************************************************/
295 /**************** Private Functions *****************/
296 /****************************************************/
298 void CMuleTrayIcon::UpdateTray()
300 // Icon update and Tip update
301 #ifndef __WXCOCOA__
302 if (IsOk())
303 #endif
305 SetIcon(CurrentIcon, CurrentTip);
310 wxMenu* CMuleTrayIcon::CreatePopupMenu()
312 // Creates dinamically the menu to show the user.
313 wxMenu *traymenu = new wxMenu();
314 traymenu->SetTitle(_("aMule Tray Menu"));
316 // Build the Top string name
317 wxString label = MOD_VERSION_LONG;
318 traymenu->Append(TRAY_MENU_INFO, label);
319 traymenu->AppendSeparator();
320 label = wxString(_("Speed limits:")) + wxT(" ");
322 // Check for upload limits
323 unsigned int max_upload = thePrefs::GetMaxUpload();
324 if ( max_upload == UNLIMITED ) {
325 label += _("UL: None");
327 else {
328 label += wxString::Format(_("UL: %u"), max_upload);
330 label += wxT(", ");
332 // Check for download limits
333 unsigned int max_download = thePrefs::GetMaxDownload();
334 if ( max_download == UNLIMITED ) {
335 label += _("DL: None");
337 else {
338 label += wxString::Format(_("DL: %u"), max_download);
341 traymenu->Append(TRAY_MENU_INFO, label);
342 label = wxString::Format(_("Download speed: %.1f"), theStats::GetDownloadRate() / 1024.0);
343 traymenu->Append(TRAY_MENU_INFO, label);
344 label = wxString::Format(_("Upload speed: %.1f"), theStats::GetUploadRate() / 1024.0);
345 traymenu->Append(TRAY_MENU_INFO, label);
346 traymenu->AppendSeparator();
348 // Client Info
349 wxMenu* ClientInfoMenu = new wxMenu();
350 ClientInfoMenu->SetTitle(_("Client Information"));
352 // User nick-name
354 wxString temp = CFormat(_("Nickname: %s")) % ( thePrefs::GetUserNick().IsEmpty() ? wxString(_("No Nickname Selected!")) : thePrefs::GetUserNick() );
356 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
359 // Client ID
361 wxString temp = _("ClientID: ");
363 if (theApp->IsConnectedED2K()) {
364 unsigned long id = theApp->GetED2KID();
365 temp += wxString::Format(wxT("%lu"), id);
366 } else {
367 temp += _("Not connected");
369 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
372 // Current Server and Server IP
374 wxString temp_name = _("ServerName: ");
375 wxString temp_ip = _("ServerIP: ");
377 if ( theApp->serverconnect->GetCurrentServer() ) {
378 temp_name += theApp->serverconnect->GetCurrentServer()->GetListName();
379 temp_ip += theApp->serverconnect->GetCurrentServer()->GetFullIP();
380 } else {
381 temp_name += _("Not connected");
382 temp_ip += _("Not Connected");
384 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_name);
385 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_ip);
388 // IP Address
390 wxString temp = CFormat(_("IP: %s")) % ( (theApp->GetPublicIP()) ? Uint32toStringIP(theApp->GetPublicIP()) : wxString(_("Unknown")) );
392 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
395 // TCP PORT
397 wxString temp;
398 if (thePrefs::GetPort()) {
399 temp = CFormat(_("TCP port: %d")) % thePrefs::GetPort();
400 } else {
401 temp=_("TCP port: Not ready");
403 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
406 // UDP PORT
408 wxString temp;
409 if (thePrefs::GetEffectiveUDPPort()) {
410 temp = CFormat(_("UDP port: %d")) % thePrefs::GetEffectiveUDPPort();
411 } else {
412 temp=_("UDP port: Not ready");
414 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
417 // Online Signature
419 wxString temp;
420 if (thePrefs::IsOnlineSignatureEnabled()) {
421 temp=_("Online Signature: Enabled");
423 else {
424 temp=_("Online Signature: Disabled");
426 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
429 // Uptime
431 wxString temp = CFormat(_("Uptime: %s")) % CastSecondsToHM(theStats::GetUptimeSeconds());
432 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
435 // Number of shared files
437 wxString temp = CFormat(_("Shared files: %d")) % theStats::GetSharedFileCount();
438 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
441 // Number of queued clients
443 wxString temp = CFormat(_("Queued clients: %d")) % theStats::GetWaitingUserCount();
444 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
447 // Total Downloaded
449 wxString temp = CastItoXBytes( theStats::GetSessionReceivedBytes() + thePrefs::GetTotalDownloaded() );
450 temp = CFormat(_("Total DL: %s")) % temp;
451 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
454 // Total Uploaded
456 wxString temp = CastItoXBytes( theStats::GetSessionSentBytes() + thePrefs::GetTotalUploaded() );
457 temp = CFormat(_("Total UL: %s")) % temp;
458 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
461 traymenu->Append(TRAY_MENU_CLIENTINFO,ClientInfoMenu->GetTitle(),ClientInfoMenu);
463 // Separator
464 traymenu->AppendSeparator();
466 // Upload Speed sub-menu
467 wxMenu* UploadSpeedMenu = new wxMenu();
468 UploadSpeedMenu->SetTitle(_("Upload limit"));
470 // Download Speed sub-menu
471 wxMenu* DownloadSpeedMenu = new wxMenu();
472 DownloadSpeedMenu->SetTitle(_("Download limit"));
474 // Upload Speed sub-menu
476 UploadSpeedMenu->Append(UPLOAD_ITEM1, _("Unlimited"));
478 uint32 max_ul_speed = thePrefs::GetMaxGraphUploadRate();
480 if ( max_ul_speed == UNLIMITED ) {
481 max_ul_speed = 100;
483 else if ( max_ul_speed < 10 ) {
484 max_ul_speed = 10;
487 for ( int i = 0; i < 5; i++ ) {
488 unsigned int tempspeed = (unsigned int)((double)max_ul_speed / 5) * (5 - i);
489 wxString temp = wxString::Format(wxT("%u kB/s"), tempspeed);
490 UploadSpeedMenu->Append((int)UPLOAD_ITEM1+i+1,temp);
493 traymenu->Append(0,UploadSpeedMenu->GetTitle(),UploadSpeedMenu);
495 // Download Speed sub-menu
497 DownloadSpeedMenu->Append(DOWNLOAD_ITEM1, _("Unlimited"));
499 uint32 max_dl_speed = thePrefs::GetMaxGraphDownloadRate();
501 if ( max_dl_speed == UNLIMITED ) {
502 max_dl_speed = 100;
504 else if ( max_dl_speed < 10 ) {
505 max_dl_speed = 10;
508 for ( int i = 0; i < 5; i++ ) {
509 unsigned int tempspeed = (unsigned int)((double)max_dl_speed / 5) * (5 - i);
510 wxString temp = wxString::Format(wxT("%d kB/s"), tempspeed);
511 DownloadSpeedMenu->Append((int)DOWNLOAD_ITEM1+i+1,temp);
515 traymenu->Append(0,DownloadSpeedMenu->GetTitle(),DownloadSpeedMenu);
516 // Separator
517 traymenu->AppendSeparator();
519 if (theApp->IsConnected()) {
520 //Disconnection Speed item
521 traymenu->Append(TRAY_MENU_DISCONNECT, _("Disconnect"));
522 } else {
523 //Connect item
524 traymenu->Append(TRAY_MENU_CONNECT, _("Connect"));
527 // Separator
528 traymenu->AppendSeparator();
530 if (theApp->amuledlg->IsShown()) {
531 //hide item
532 traymenu->Append(TRAY_MENU_HIDE, _("Hide aMule"));
533 } else {
534 //show item
535 traymenu->Append(TRAY_MENU_SHOW, _("Show aMule"));
538 // Separator
539 traymenu->AppendSeparator();
541 // Exit item
542 traymenu->Append(TRAY_MENU_EXIT, _("Exit"));
544 return traymenu;
547 void CMuleTrayIcon::SwitchShow(wxTaskBarIconEvent&)
549 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
551 // File_checked_for_headers