2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "amule.h" // Interface declarations.
27 #include <common/EventIDs.h>
28 #include <common/ClientVersion.h>
30 #include <wx/clipbrd.h> // Needed for wxClipBoard
31 #include <wx/tokenzr.h> // Needed for wxStringTokenizer
33 #include "SharedFilesWnd.h" // Needed for CSharedFilesWnd
34 #include "Timer.h" // Needed for CTimer
35 #include "PartFile.h" // Needed for CPartFile
37 #include "muuli_wdr.h" // Needed for IDs
38 #include "amuleDlg.h" // Needed for CamuleDlg
39 #include "PartFileConvert.h"
40 #include "ThreadTasks.h"
41 #include "Logger.h" // Needed for EVT_MULE_LOGGING
42 #include "GuiEvents.h" // Needed for EVT_MULE_NOTIFY
45 #include <CoreFoundation/CFBundle.h> // Do_not_auto_remove
46 #include <ApplicationServices/ApplicationServices.h> // For LSRegisterURL // Do_not_auto_remove
50 #include "InternalEvents.h" // Needed for wxEVT_*
52 BEGIN_EVENT_TABLE(CamuleGuiApp
, wxApp
)
57 EVT_SOCKET(ID_LISTENSOCKET_EVENT
, CamuleGuiApp::ListenSocketHandler
)
59 // UDP Socket (servers)
60 EVT_SOCKET(ID_SERVERUDPSOCKET_EVENT
, CamuleGuiApp::UDPSocketHandler
)
61 // UDP Socket (clients)
62 EVT_SOCKET(ID_CLIENTUDPSOCKET_EVENT
, CamuleGuiApp::UDPSocketHandler
)
65 // Socket timers (TCP + UDP)
66 EVT_MULE_TIMER(ID_SERVER_RETRY_TIMER_EVENT
, CamuleGuiApp::OnTCPTimer
)
69 EVT_MULE_TIMER(ID_CORE_TIMER_EVENT
, CamuleGuiApp::OnCoreTimer
)
71 EVT_MULE_NOTIFY(CamuleGuiApp::OnNotifyEvent
)
74 EVT_MULE_INTERNAL(wxEVT_CORE_UDP_DNS_DONE
, -1, CamuleGuiApp::OnUDPDnsDone
)
76 EVT_MULE_INTERNAL(wxEVT_CORE_SOURCE_DNS_DONE
, -1, CamuleGuiApp::OnSourceDnsDone
)
78 EVT_MULE_INTERNAL(wxEVT_CORE_SERVER_DNS_DONE
, -1, CamuleGuiApp::OnServerDnsDone
)
80 // Hash ended notifier
81 EVT_MULE_HASHING(CamuleGuiApp::OnFinishedHashing
)
82 EVT_MULE_AICH_HASHING(CamuleGuiApp::OnFinishedAICHHashing
)
84 // File completion ended notifier
85 EVT_MULE_FILE_COMPLETED(CamuleGuiApp::OnFinishedCompletion
)
87 // HTTPDownload finished
88 EVT_MULE_INTERNAL(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD
, -1, CamuleGuiApp::OnFinishedHTTPDownload
)
90 // Disk space preallocation finished
91 EVT_MULE_ALLOC_FINISHED(CamuleGuiApp::OnFinishedAllocation
)
95 IMPLEMENT_APP(CamuleGuiApp
)
99 CamuleGuiBase::CamuleGuiBase()
105 CamuleGuiBase::~CamuleGuiBase()
108 CPartFileConvert::StopThread();
113 int CamuleGuiBase::ShowAlert(wxString msg
, wxString title
, int flags
)
115 return wxMessageBox(msg
, title
, flags
);
119 int CamuleGuiBase::InitGui(bool geometry_enabled
, wxString
&geom_string
)
121 // Standard size is 800x600 at position (0,0)
124 unsigned int geometry_width
= 800;
125 unsigned int geometry_height
= 600;
127 if ( geometry_enabled
) {
128 // I plan on moving this to a separate function, as it just clutters up OnInit()
130 This implementation might work with mac, provided that the
131 SetSize() function works as expected.
134 // Remove possible prefix
135 if ( geom_string
.GetChar(0) == '=' ) {
136 geom_string
.Remove( 0, 1 );
139 // Stupid ToLong functions forces me to use longs =(
140 long width
= geometry_width
;
141 long height
= geometry_height
;
143 // Get the avilable display area
144 wxRect display
= wxGetClientDisplayRect();
146 // We want to place aMule inside the client area by default
150 // Tokenize the string
151 wxStringTokenizer
tokens(geom_string
, wxT("xX+-"));
153 // First part: Program width
154 if ( tokens
.GetNextToken().ToLong( &width
) ) {
155 wxString prefix
= geom_string
[ tokens
.GetPosition() - 1 ];
156 if ( prefix
== wxT("x") || prefix
== wxT("X") ) {
157 // Second part: Program height
158 if ( tokens
.GetNextToken().ToLong( &height
) ) {
159 prefix
= geom_string
[ tokens
.GetPosition() - 1 ];
160 if ( prefix
== wxT("+") || prefix
== wxT("-") ) {
161 // Third part: X-Offset
162 if ( tokens
.GetNextToken().ToLong( &x
) ) {
163 if ( prefix
== wxT("-") )
164 x
= display
.GetRight() - ( width
+ x
);
165 prefix
= geom_string
[ tokens
.GetPosition() - 1 ];
166 if ( prefix
== wxT("+") || prefix
== wxT("-") ) {
167 // Fourth part: Y-Offset
168 if ( tokens
.GetNextToken().ToLong( &y
) ) {
169 if ( prefix
== wxT("-") )
170 y
= display
.GetBottom() - ( height
+ y
);
175 // We need at least height and width to override default geometry
176 geometry_enabled
= true;
179 geometry_width
= width
;
180 geometry_height
= height
;
188 // Should default/last-used position be overridden?
189 if ( geometry_enabled
) {
190 amuledlg
= new CamuleDlg(NULL
, m_FrameTitle
,
191 wxPoint(geometry_x
,geometry_y
),
192 wxSize( geometry_width
, geometry_height
- 58 ));
194 amuledlg
= new CamuleDlg(NULL
, m_FrameTitle
);
201 void CamuleGuiBase::ResetTitle()
205 m_FrameTitle
= CFormat(wxT("aMule remote control %s %s")) % wxT( VERSION
) % wxT( SVNDATE
);
207 m_FrameTitle
= CFormat(wxT("aMule %s %s")) % wxT( VERSION
) % wxT( SVNDATE
);
211 m_FrameTitle
= _("aMule remote control");
213 m_FrameTitle
= _("aMule");
216 if (thePrefs::ShowVersionOnTitle()) {
217 m_FrameTitle
+= wxT(' ');
218 m_FrameTitle
+= wxT( VERSION
);
224 // Sets the contents of the clipboard. Prior content erased.
225 bool CamuleGuiBase::CopyTextToClipboard(wxString strText
)
227 bool ClipBoardOpen
= wxTheClipboard
->Open();
229 wxTheClipboard
->UsePrimarySelection(false);
230 wxTheClipboard
->SetData(new wxTextDataObject(strText
));
231 wxTheClipboard
->Close();
234 return ClipBoardOpen
;
238 void CamuleGuiBase::AddGuiLogLine(const wxString
& line
)
241 while ( !m_logLines
.empty() ) {
242 amuledlg
->AddLogLine(m_logLines
.front());
243 m_logLines
.pop_front();
245 amuledlg
->AddLogLine(line
);
247 m_logLines
.push_back(line
);
254 int CamuleGuiApp::InitGui(bool geometry_enable
, wxString
&geometry_string
)
256 CamuleGuiBase::InitGui(geometry_enable
, geometry_string
);
257 SetTopWindow(amuledlg
);
262 int CamuleGuiApp::ShowAlert(wxString msg
, wxString title
, int flags
)
264 return CamuleGuiBase::ShowAlert(msg
, title
, flags
);
268 int CamuleGuiApp::OnExit()
272 return CamuleApp::OnExit();
276 void CamuleGuiApp::ShutDown(wxCloseEvent
&WXUNUSED(evt
))
278 amuledlg
->DlgShutDown();
280 CamuleApp::ShutDown();
284 bool CamuleGuiApp::OnInit()
288 if ( !CamuleApp::OnInit() ) {
292 // Create the Core timer
293 core_timer
= new CTimer(this,ID_CORE_TIMER_EVENT
);
295 AddLogLineCS(_("Fatal Error: Failed to create Core Timer"));
299 // Start the Core and Gui timers
301 // Note: wxTimer can be off by more than 10% !!!
302 // In addition to the systematic error introduced by wxTimer, we are losing
303 // timer cycles due to high CPU load. I've observed about 0.5% random loss of cycles under
304 // low load, and more than 6% lost cycles with heavy download traffic and/or other tasks
305 // in the system, such as a video player or a VMware virtual machine.
306 // The upload queue process loop has now been rewritten to compensate for timer errors.
307 // When adding functionality, assume that the timer is only approximately correct;
308 // for measurements, always use the system clock [::GetTickCount()].
309 core_timer
->Start(CORE_TIMER_PERIOD
);
310 amuledlg
->StartGuiTimer();
313 // This tells the OS to notice the ed2kHelperScript.app inside aMule.app.
314 // ed2kHelperScript.app describes itself (Info.plist) as handling ed2k URLs.
315 // So, from then on the OS will know to pass ed2k URLs to the helper app.
316 CFURLRef ed2kHelperUrl
= CFBundleCopyAuxiliaryExecutableURL(
317 CFBundleGetMainBundle(), CFSTR("ed2kHelperScript.app"));
319 LSRegisterURL(ed2kHelperUrl
, true);
320 CFRelease(ed2kHelperUrl
);
328 wxString
CamuleGuiApp::GetLog(bool reset
)
331 amuledlg
->ResetLog(ID_LOGVIEW
);
333 return CamuleApp::GetLog(reset
);
337 wxString
CamuleGuiApp::GetServerLog(bool reset
)
340 amuledlg
->ResetLog(ID_SERVERINFO
);
342 return CamuleApp::GetServerLog(reset
);
346 void CamuleGuiApp::AddServerMessageLine(wxString
&msg
)
348 amuledlg
->AddServerMessageLine(msg
);
349 CamuleApp::AddServerMessageLine(msg
);
352 #endif /* CLIENT_GUI */
353 // File_checked_for_headers