Upstream tarball 9613
[amule.git] / src / MuleTrayIcon.cpp
blob0d19f44f2d05b553df6318ba2692a61bdd5d7e28
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);
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->GetLabel()==(_("Unlimited"))) {
149 temp=UNLIMITED;
151 else {
152 temp=GetSpeedFromString(item->GetLabel());
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.
191 // Create the background icons (speed improvement)
192 HighId_Icon_size = wxIcon(mule_TrayIcon_big_ico_xpm).GetHeight();
193 LowId_Icon_size = wxIcon(mule_Tr_yellow_big_ico_xpm).GetHeight();
194 Disconnected_Icon_size = wxIcon(mule_Tr_grey_big_ico_xpm).GetHeight();
197 CMuleTrayIcon::~CMuleTrayIcon()
199 #ifdef __WXGTK__
200 // FIXME: EVIL HACK: We need to ensure that the superclass doesn't
201 // try to destroy a dangling pointer. See also CMuleTrayIcon::UpdateTray
202 // for comments on this issue.
203 if (m_iconWnd) {
204 if (wxTopLevelWindows.IndexOf((wxWindow*)m_iconWnd) == wxNOT_FOUND) {
205 m_iconWnd = NULL;
208 #endif
211 /****************************************************/
212 /***************** Public Functions *****************/
213 /****************************************************/
215 void CMuleTrayIcon::SetTrayIcon(int Icon, uint32 percent)
217 int Bar_ySize = 0;
219 switch (Icon) {
220 case TRAY_ICON_HIGHID:
221 // Most likely case, test first
222 Bar_ySize = HighId_Icon_size;
223 break;
224 case TRAY_ICON_LOWID:
225 Bar_ySize = LowId_Icon_size;
226 break;
227 case TRAY_ICON_DISCONNECTED:
228 Bar_ySize = Disconnected_Icon_size;
229 break;
230 default:
231 wxFAIL;
234 // Lookup this values for speed improvement: don't draw if not needed
235 int NewSize = (Bar_ySize * percent) / 100;
237 if ((Old_Icon != Icon) || (Old_SpeedSize != NewSize)) {
239 if ((Old_SpeedSize > NewSize) || (Old_Icon != Icon)) {
240 // We have to rebuild the icon, because bar is lower now.
241 switch (Icon) {
242 case TRAY_ICON_HIGHID:
243 // Most likely case, test first
244 CurrentIcon = wxIcon(mule_TrayIcon_big_ico_xpm);
245 break;
246 case TRAY_ICON_LOWID:
247 CurrentIcon = wxIcon(mule_Tr_yellow_big_ico_xpm);
248 break;
249 case TRAY_ICON_DISCONNECTED:
250 CurrentIcon = wxIcon(mule_Tr_grey_big_ico_xpm);
251 break;
252 default:
253 wxFAIL;
257 Old_Icon = Icon;
258 Old_SpeedSize = NewSize;
260 // Do whatever to the icon before drawing it (percent)
262 wxBitmap TempBMP;
263 TempBMP.CopyFromIcon(CurrentIcon);
265 TempBMP.SetMask(NULL);
267 IconWithSpeed.SelectObject(TempBMP);
270 // Speed bar is: centered, taking 80% of the icon heigh, and
271 // right-justified taking a 10% of the icon width.
273 // X
274 int Bar_xSize = 4;
275 int Bar_xPos = CurrentIcon.GetWidth() - 5;
277 IconWithSpeed.SetBrush(*(wxTheBrushList->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
278 IconWithSpeed.SetPen(*wxTRANSPARENT_PEN);
280 IconWithSpeed.DrawRectangle(Bar_xPos + 1, Bar_ySize - NewSize, Bar_xSize -2 , NewSize);
282 // Unselect the icon.
283 IconWithSpeed.SelectObject(wxNullBitmap);
285 // Do transparency
287 // Set a new mask with transparency set to red.
288 wxMask* new_mask = new wxMask(TempBMP, wxColour(0xFF, 0x00, 0x00));
290 TempBMP.SetMask(new_mask);
291 CurrentIcon.CopyFromBitmap(TempBMP);
293 UpdateTray();
297 void CMuleTrayIcon::SetTrayToolTip(const wxString& Tip)
299 CurrentTip = Tip;
300 UpdateTray();
303 /****************************************************/
304 /**************** Private Functions *****************/
305 /****************************************************/
307 void CMuleTrayIcon::UpdateTray()
309 #ifdef __WXGTK__
310 // FIXME: EVIL HACK: As of wxGTK-2.8.7, closing of the trayicon
311 // window (caused for instance by a crashing kicker) is not
312 // handled, with the result that the pointer to the trayicon
313 // window becomes a dangling pointer. Since we have access to
314 // the pointer, and it's created as a top-level window, it's
315 // relatively easy to force the recreation of a valid window.
316 // Ugly as hell though ....
318 // This has been repported as bug #1872724:
319 // http://sourceforge.net/tracker/index.php?func=detail&aid=1872724&group_id=9863&atid=109863
320 if (m_iconWnd) {
321 if (wxTopLevelWindows.IndexOf((wxWindow*)m_iconWnd) == wxNOT_FOUND) {
322 AddLogLineCS(_("Traybar-icon lost, trying to recreate ..."));
323 m_iconWnd = NULL;
326 #endif
328 // Icon update and Tip update
329 #ifndef __WXCOCOA__
330 if (IsOk())
331 #endif
333 SetIcon(CurrentIcon, CurrentTip);
338 wxMenu* CMuleTrayIcon::CreatePopupMenu()
340 // Creates dinamically the menu to show the user.
341 wxMenu *traymenu = new wxMenu();
342 traymenu->SetTitle(_("aMule Tray Menu"));
344 // Build the Top string name
345 wxString label = MOD_VERSION_LONG;
346 traymenu->Append(TRAY_MENU_INFO, label);
347 traymenu->AppendSeparator();
348 label = wxString(_("Speed limits:")) + wxT(" ");
350 // Check for upload limits
351 unsigned int max_upload = thePrefs::GetMaxUpload();
352 if ( max_upload == UNLIMITED ) {
353 label += _("UL: None");
355 else {
356 label += wxString::Format(_("UL: %u"), max_upload);
358 label += wxT(", ");
360 // Check for download limits
361 unsigned int max_download = thePrefs::GetMaxDownload();
362 if ( max_download == UNLIMITED ) {
363 label += _("DL: None");
365 else {
366 label += wxString::Format(_("DL: %u"), max_download);
369 traymenu->Append(TRAY_MENU_INFO, label);
370 label = wxString::Format(_("Download speed: %.1f"), theStats::GetDownloadRate() / 1024.0);
371 traymenu->Append(TRAY_MENU_INFO, label);
372 label = wxString::Format(_("Upload speed: %.1f"), theStats::GetUploadRate() / 1024.0);
373 traymenu->Append(TRAY_MENU_INFO, label);
374 traymenu->AppendSeparator();
376 // Client Info
377 wxMenu* ClientInfoMenu = new wxMenu();
378 ClientInfoMenu->SetTitle(_("Client Information"));
380 // User nick-name
382 wxString temp = CFormat(_("Nickname: %s")) % ( thePrefs::GetUserNick().IsEmpty() ? wxString(_("No Nickname Selected!")) : thePrefs::GetUserNick() );
384 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
387 // Client ID
389 wxString temp = _("ClientID: ");
391 if (theApp->IsConnectedED2K()) {
392 unsigned long id = theApp->GetED2KID();
393 temp += wxString::Format(wxT("%lu"), id);
394 } else {
395 temp += _("Not connected");
397 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
400 // Current Server and Server IP
402 wxString temp_name = _("ServerName: ");
403 wxString temp_ip = _("ServerIP: ");
405 if ( theApp->serverconnect->GetCurrentServer() ) {
406 temp_name += theApp->serverconnect->GetCurrentServer()->GetListName();
407 temp_ip += theApp->serverconnect->GetCurrentServer()->GetFullIP();
408 } else {
409 temp_name += _("Not connected");
410 temp_ip += _("Not Connected");
412 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_name);
413 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp_ip);
416 // IP Address
418 wxString temp = CFormat(_("IP: %s")) % ( (theApp->GetPublicIP()) ? Uint32toStringIP(theApp->GetPublicIP()) : wxString(_("Unknown")) );
420 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
423 // TCP PORT
425 wxString temp;
426 if (thePrefs::GetPort()) {
427 temp = CFormat(_("TCP port: %d")) % thePrefs::GetPort();
428 } else {
429 temp=_("TCP port: Not ready");
431 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
434 // UDP PORT
436 wxString temp;
437 if (thePrefs::GetEffectiveUDPPort()) {
438 temp = CFormat(_("UDP port: %d")) % thePrefs::GetEffectiveUDPPort();
439 } else {
440 temp=_("UDP port: Not ready");
442 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
445 // Online Signature
447 wxString temp;
448 if (thePrefs::IsOnlineSignatureEnabled()) {
449 temp=_("Online Signature: Enabled");
451 else {
452 temp=_("Online Signature: Disabled");
454 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
457 // Uptime
459 wxString temp = CFormat(_("Uptime: %s")) % CastSecondsToHM(theStats::GetUptimeSeconds());
460 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
463 // Number of shared files
465 wxString temp = CFormat(_("Shared files: %d")) % theStats::GetSharedFileCount();
466 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
469 // Number of queued clients
471 wxString temp = CFormat(_("Queued clients: %d")) % theStats::GetWaitingUserCount();
472 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
475 // Total Downloaded
477 wxString temp = CastItoXBytes( theStats::GetSessionReceivedBytes() + thePrefs::GetTotalDownloaded() );
478 temp = CFormat(_("Total DL: %s")) % temp;
479 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
482 // Total Uploaded
484 wxString temp = CastItoXBytes( theStats::GetSessionSentBytes() + thePrefs::GetTotalUploaded() );
485 temp = CFormat(_("Total UL: %s")) % temp;
486 ClientInfoMenu->Append(TRAY_MENU_CLIENTINFO_ITEM,temp);
489 traymenu->Append(TRAY_MENU_CLIENTINFO,ClientInfoMenu->GetTitle(),ClientInfoMenu);
491 // Separator
492 traymenu->AppendSeparator();
494 // Upload Speed sub-menu
495 wxMenu* UploadSpeedMenu = new wxMenu();
496 UploadSpeedMenu->SetTitle(_("Upload limit"));
498 // Download Speed sub-menu
499 wxMenu* DownloadSpeedMenu = new wxMenu();
500 DownloadSpeedMenu->SetTitle(_("Download limit"));
502 // Upload Speed sub-menu
504 UploadSpeedMenu->Append(UPLOAD_ITEM1, _("Unlimited"));
506 uint32 max_ul_speed = thePrefs::GetMaxGraphUploadRate();
508 if ( max_ul_speed == UNLIMITED ) {
509 max_ul_speed = 100;
511 else if ( max_ul_speed < 10 ) {
512 max_ul_speed = 10;
515 for ( int i = 0; i < 5; i++ ) {
516 unsigned int tempspeed = (unsigned int)((double)max_ul_speed / 5) * (5 - i);
517 wxString temp = wxString::Format(wxT("%u kB/s"), tempspeed);
518 UploadSpeedMenu->Append((int)UPLOAD_ITEM1+i+1,temp);
521 traymenu->Append(0,UploadSpeedMenu->GetTitle(),UploadSpeedMenu);
523 // Download Speed sub-menu
525 DownloadSpeedMenu->Append(DOWNLOAD_ITEM1, _("Unlimited"));
527 uint32 max_dl_speed = thePrefs::GetMaxGraphDownloadRate();
529 if ( max_dl_speed == UNLIMITED ) {
530 max_dl_speed = 100;
532 else if ( max_dl_speed < 10 ) {
533 max_dl_speed = 10;
536 for ( int i = 0; i < 5; i++ ) {
537 unsigned int tempspeed = (unsigned int)((double)max_dl_speed / 5) * (5 - i);
538 wxString temp = wxString::Format(wxT("%d kB/s"), tempspeed);
539 DownloadSpeedMenu->Append((int)DOWNLOAD_ITEM1+i+1,temp);
543 traymenu->Append(0,DownloadSpeedMenu->GetTitle(),DownloadSpeedMenu);
544 // Separator
545 traymenu->AppendSeparator();
547 if (theApp->IsConnected()) {
548 //Disconnection Speed item
549 traymenu->Append(TRAY_MENU_DISCONNECT, _("Disconnect"));
550 } else {
551 //Connect item
552 traymenu->Append(TRAY_MENU_CONNECT, _("Connect"));
555 // Separator
556 traymenu->AppendSeparator();
558 if (theApp->amuledlg->IsShown()) {
559 //hide item
560 traymenu->Append(TRAY_MENU_HIDE, _("Hide aMule"));
561 } else {
562 //show item
563 traymenu->Append(TRAY_MENU_SHOW, _("Show aMule"));
566 // Separator
567 traymenu->AppendSeparator();
569 // Exit item
570 traymenu->Append(TRAY_MENU_EXIT, _("Exit"));
572 return traymenu;
575 void CMuleTrayIcon::SwitchShow(wxTaskBarIconEvent&)
577 theApp->amuledlg->DoIconize(theApp->amuledlg->IsShown());
579 // File_checked_for_headers