2 // This file is part of the aMule Project.
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 )
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.
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
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"
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
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
)
76 /****************************************************/
77 /************ Constructor / Destructor **************/
78 /****************************************************/
80 static long GetSpeedFromString(wxString label
){
82 label
.Replace(_("kB/s"),wxT(""),TRUE
);
89 void CMuleTrayIcon::SetUploadSpeed(wxCommandEvent
& event
){
91 wxObject
* obj
=event
.GetEventObject();
93 wxMenu
*menu
= dynamic_cast<wxMenu
*>(obj
);
95 wxMenuItem
* item
=menu
->FindItem(event
.GetId());
98 if (item
->GetItemLabelText()==(_("Unlimited"))) {
102 temp
=GetSpeedFromString(item
->GetItemLabelText());
104 thePrefs::SetMaxUpload(temp
);
107 // Send preferences to core.
108 theApp
->glob_prefs
->SendToRemote();
115 void CMuleTrayIcon::SetDownloadSpeed(wxCommandEvent
& event
){
117 wxObject
* obj
=event
.GetEventObject();
119 wxMenu
*menu
= dynamic_cast<wxMenu
*>(obj
);
121 wxMenuItem
* item
=menu
->FindItem(event
.GetId());
124 if (item
->GetItemLabelText()==(_("Unlimited"))) {
128 temp
=GetSpeedFromString(item
->GetItemLabelText());
130 thePrefs::SetMaxDownload(temp
);
133 // Send preferences to core.
134 theApp
->glob_prefs
->SendToRemote();
142 void CMuleTrayIcon::ServerConnection(wxCommandEvent
& WXUNUSED(event
))
145 theApp
->amuledlg
->OnBnConnect(evt
);
149 void CMuleTrayIcon::ShowHide(wxCommandEvent
& WXUNUSED(event
))
151 theApp
->amuledlg
->Iconize(theApp
->amuledlg
->IsShown());
152 theApp
->amuledlg
->Show(!theApp
->amuledlg
->IsShown());
156 void CMuleTrayIcon::Close(wxCommandEvent
& WXUNUSED(event
))
158 if (theApp
->amuledlg
->IsEnabled()) {
159 theApp
->amuledlg
->Close();
164 CMuleTrayIcon::CMuleTrayIcon()
167 Old_SpeedSize
= 0xFFFF; // must be > any possible one.
169 // Create the background icons (speed improvement)
170 HighId_Icon_size
= wxIcon(mule_TrayIcon_big_ico_xpm
).GetHeight();
171 LowId_Icon_size
= wxIcon(mule_Tr_yellow_big_ico_xpm
).GetHeight();
172 Disconnected_Icon_size
= wxIcon(mule_Tr_grey_big_ico_xpm
).GetHeight();
175 CMuleTrayIcon::~CMuleTrayIcon()
179 /****************************************************/
180 /***************** Public Functions *****************/
181 /****************************************************/
183 void CMuleTrayIcon::SetTrayIcon(int Icon
, uint32 percent
)
188 case TRAY_ICON_HIGHID
:
189 // Most likely case, test first
190 Bar_ySize
= HighId_Icon_size
;
192 case TRAY_ICON_LOWID
:
193 Bar_ySize
= LowId_Icon_size
;
195 case TRAY_ICON_DISCONNECTED
:
196 Bar_ySize
= Disconnected_Icon_size
;
202 // Lookup this values for speed improvement: don't draw if not needed
203 int NewSize
= (Bar_ySize
* percent
) / 100;
205 if ((Old_Icon
!= Icon
) || (Old_SpeedSize
!= NewSize
)) {
207 if ((Old_SpeedSize
> NewSize
) || (Old_Icon
!= Icon
)) {
208 // We have to rebuild the icon, because bar is lower now.
210 case TRAY_ICON_HIGHID
:
211 // Most likely case, test first
212 CurrentIcon
= wxIcon(mule_TrayIcon_big_ico_xpm
);
214 case TRAY_ICON_LOWID
:
215 CurrentIcon
= wxIcon(mule_Tr_yellow_big_ico_xpm
);
217 case TRAY_ICON_DISCONNECTED
:
218 CurrentIcon
= wxIcon(mule_Tr_grey_big_ico_xpm
);
226 Old_SpeedSize
= NewSize
;
228 // Do whatever to the icon before drawing it (percent)
231 TempBMP
.CopyFromIcon(CurrentIcon
);
233 TempBMP
.SetMask(NULL
);
235 IconWithSpeed
.SelectObject(TempBMP
);
238 // Speed bar is: centered, taking 80% of the icon heigh, and
239 // right-justified taking a 10% of the icon width.
243 int Bar_xPos
= CurrentIcon
.GetWidth() - 5;
245 IconWithSpeed
.SetBrush(*(wxTheBrushList
->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
246 IconWithSpeed
.SetPen(*wxTRANSPARENT_PEN
);
248 IconWithSpeed
.DrawRectangle(Bar_xPos
+ 1, Bar_ySize
- NewSize
, Bar_xSize
-2 , NewSize
);
250 // Unselect the icon.
251 IconWithSpeed
.SelectObject(wxNullBitmap
);
255 // Set a new mask with transparency set to red.
256 wxMask
* new_mask
= new wxMask(TempBMP
, wxColour(0xFF, 0x00, 0x00));
258 TempBMP
.SetMask(new_mask
);
259 CurrentIcon
.CopyFromBitmap(TempBMP
);
265 void CMuleTrayIcon::SetTrayToolTip(const wxString
& Tip
)
271 /****************************************************/
272 /**************** Private Functions *****************/
273 /****************************************************/
275 void CMuleTrayIcon::UpdateTray()
277 // Icon update and Tip update
282 SetIcon(CurrentIcon
, CurrentTip
);
287 wxMenu
* CMuleTrayIcon::CreatePopupMenu()
289 float kBpsUp
= theStats::GetUploadRate() / 1024.0;
290 float kBpsDown
= theStats::GetDownloadRate() / 1024.0;
291 float MBpsUp
= kBpsUp
/ 1024.0;
292 float MBpsDown
= kBpsDown
/ 1024.0;
293 bool showMBpsUp
= (MBpsUp
>= 1);
294 bool showMBpsDown
= (MBpsDown
>= 1);
296 // Creates dinamically the menu to show the user.
297 wxMenu
*traymenu
= new wxMenu();
298 traymenu
->SetTitle(_("aMule Tray Menu"));
300 // Build the Top string name
301 wxString label
= MOD_VERSION_LONG
;
302 traymenu
->Append(TRAY_MENU_INFO
, label
);
303 traymenu
->AppendSeparator();
304 label
= wxString(_("Speed limits:")) + wxT(" ");
306 // Check for upload limits
307 unsigned int max_upload
= thePrefs::GetMaxUpload();
308 if ( max_upload
== UNLIMITED
) {
309 label
+= _("UL: None");
312 label
+= CFormat(_("UL: %u")) % max_upload
;
316 // Check for download limits
317 unsigned int max_download
= thePrefs::GetMaxDownload();
318 if ( max_download
== UNLIMITED
) {
319 label
+= _("DL: None");
322 label
+= CFormat(_("DL: %u")) % max_download
;
325 traymenu
->Append(TRAY_MENU_INFO
, label
);
326 label
= CFormat(_("Download speed: %.1f%s"))
327 % (showMBpsDown
? MBpsDown
: kBpsDown
) % (showMBpsDown
? _(" MB/s") : ((kBpsDown
> 0) ? _(" kB/s") : wxT("")));
328 traymenu
->Append(TRAY_MENU_INFO
, label
);
329 label
= CFormat(_("Upload speed: %.1f%s"))
330 % (showMBpsUp
? MBpsUp
: kBpsUp
) % (showMBpsUp
? _(" MB/s") : ((kBpsUp
> 0) ? _(" kB/s") : wxT("")));
331 traymenu
->Append(TRAY_MENU_INFO
, label
);
332 traymenu
->AppendSeparator();
335 wxMenu
* ClientInfoMenu
= new wxMenu();
336 ClientInfoMenu
->SetTitle(_("Client Information"));
340 wxString temp
= CFormat(_("Nickname: %s")) % ( thePrefs::GetUserNick().IsEmpty() ? wxString(_("No Nickname Selected!")) : thePrefs::GetUserNick() );
342 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
347 wxString temp
= _("ClientID: ");
349 if (theApp
->IsConnectedED2K()) {
350 temp
+= CFormat(wxT("%u")) % theApp
->GetED2KID();
352 temp
+= _("Not connected");
354 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
357 // Current Server and Server IP
359 wxString temp_name
= _("ServerName: ");
360 wxString temp_ip
= _("ServerIP: ");
362 if ( theApp
->serverconnect
->GetCurrentServer() ) {
363 temp_name
+= theApp
->serverconnect
->GetCurrentServer()->GetListName();
364 temp_ip
+= theApp
->serverconnect
->GetCurrentServer()->GetFullIP();
366 temp_name
+= _("Not connected");
367 temp_ip
+= _("Not Connected");
369 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp_name
);
370 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp_ip
);
375 wxString temp
= CFormat(_("IP: %s")) % ( (theApp
->GetPublicIP()) ? Uint32toStringIP(theApp
->GetPublicIP()) : wxString(_("Unknown")) );
377 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
383 if (thePrefs::GetPort()) {
384 temp
= CFormat(_("TCP port: %d")) % thePrefs::GetPort();
386 temp
=_("TCP port: Not ready");
388 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
394 if (thePrefs::GetEffectiveUDPPort()) {
395 temp
= CFormat(_("UDP port: %d")) % thePrefs::GetEffectiveUDPPort();
397 temp
=_("UDP port: Not ready");
399 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
405 if (thePrefs::IsOnlineSignatureEnabled()) {
406 temp
=_("Online Signature: Enabled");
409 temp
=_("Online Signature: Disabled");
411 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
416 wxString temp
= CFormat(_("Uptime: %s")) % CastSecondsToHM(theStats::GetUptimeSeconds());
417 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
420 // Number of shared files
422 wxString temp
= CFormat(_("Shared files: %d")) % theStats::GetSharedFileCount();
423 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
426 // Number of queued clients
428 wxString temp
= CFormat(_("Queued clients: %d")) % theStats::GetWaitingUserCount();
429 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
434 wxString temp
= CastItoXBytes(theStats::GetTotalReceivedBytes());
435 temp
= CFormat(_("Total DL: %s")) % temp
;
436 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
441 wxString temp
= CastItoXBytes(theStats::GetTotalSentBytes());
442 temp
= CFormat(_("Total UL: %s")) % temp
;
443 ClientInfoMenu
->Append(TRAY_MENU_CLIENTINFO_ITEM
,temp
);
446 traymenu
->Append(TRAY_MENU_CLIENTINFO
,ClientInfoMenu
->GetTitle(),ClientInfoMenu
);
449 traymenu
->AppendSeparator();
451 // Upload Speed sub-menu
452 wxMenu
* UploadSpeedMenu
= new wxMenu();
453 UploadSpeedMenu
->SetTitle(_("Upload limit"));
455 // Download Speed sub-menu
456 wxMenu
* DownloadSpeedMenu
= new wxMenu();
457 DownloadSpeedMenu
->SetTitle(_("Download limit"));
459 // Upload Speed sub-menu
461 UploadSpeedMenu
->Append(UPLOAD_ITEM1
, _("Unlimited"));
463 uint32 max_ul_speed
= thePrefs::GetMaxGraphUploadRate();
465 if ( max_ul_speed
== UNLIMITED
) {
468 else if ( max_ul_speed
< 10 ) {
472 for ( int i
= 0; i
< 5; i
++ ) {
473 unsigned int tempspeed
= (unsigned int)((double)max_ul_speed
/ 5) * (5 - i
);
474 wxString temp
= CFormat(wxT("%u %s")) % tempspeed
% _("kB/s");
475 UploadSpeedMenu
->Append((int)UPLOAD_ITEM1
+i
+1,temp
);
478 traymenu
->Append(0,UploadSpeedMenu
->GetTitle(),UploadSpeedMenu
);
480 // Download Speed sub-menu
482 DownloadSpeedMenu
->Append(DOWNLOAD_ITEM1
, _("Unlimited"));
484 uint32 max_dl_speed
= thePrefs::GetMaxGraphDownloadRate();
486 if ( max_dl_speed
== UNLIMITED
) {
489 else if ( max_dl_speed
< 10 ) {
493 for ( int i
= 0; i
< 5; i
++ ) {
494 unsigned int tempspeed
= (unsigned int)((double)max_dl_speed
/ 5) * (5 - i
);
495 wxString temp
= CFormat(wxT("%d %s")) % tempspeed
% _("kB/s");
496 DownloadSpeedMenu
->Append((int)DOWNLOAD_ITEM1
+i
+1,temp
);
500 traymenu
->Append(0,DownloadSpeedMenu
->GetTitle(),DownloadSpeedMenu
);
502 traymenu
->AppendSeparator();
504 if (theApp
->IsConnected()) {
505 //Disconnection Speed item
506 traymenu
->Append(TRAY_MENU_DISCONNECT
, _("Disconnect"));
509 traymenu
->Append(TRAY_MENU_CONNECT
, _("Connect"));
513 traymenu
->AppendSeparator();
515 if (theApp
->amuledlg
->IsShown()) {
517 traymenu
->Append(TRAY_MENU_HIDE
, _("Hide aMule"));
520 traymenu
->Append(TRAY_MENU_SHOW
, _("Show aMule"));
524 traymenu
->AppendSeparator();
527 traymenu
->Append(TRAY_MENU_EXIT
, _("Exit"));
532 void CMuleTrayIcon::SwitchShow(wxTaskBarIconEvent
&)
534 theApp
->amuledlg
->Iconize(theApp
->amuledlg
->IsShown());
535 theApp
->amuledlg
->Show(!theApp
->amuledlg
->IsShown());
537 // File_checked_for_headers