Upstream tarball 20080324
[amule.git] / src / amule-gui.cpp
blob71bb20745d7409854bc6248d821eccfb728530f6
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
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
8 // respective authors.
9 //
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.
19 //
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
36 #include "updownclient.h" // Needed for CUpDownClient
38 #include "muuli_wdr.h" // Needed for IDs
39 #include "amuleDlg.h" // Needed for CamuleDlg
40 #include "PartFileConvert.h"
41 #include "ThreadTasks.h"
42 #include "Logger.h" // Needed for EVT_MULE_LOGGING
43 #include "GuiEvents.h" // Needed for EVT_MULE_NOTIFY
45 #ifdef __WXMAC__
46 #include <CoreFoundation/CFBundle.h> // Do_not_auto_remove
47 #include <ApplicationServices/ApplicationServices.h> // For LSRegisterURL // Do_not_auto_remove
48 #endif
50 #ifndef CLIENT_GUI
51 #include "InternalEvents.h" // Needed for wxEVT_*
53 BEGIN_EVENT_TABLE(CamuleGuiApp, wxApp)
55 // Socket handlers
56 // Listen Socket
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)
64 // Socket timers (TCP + UDP)
65 EVT_MULE_TIMER(ID_SERVER_RETRY_TIMER_EVENT, CamuleGuiApp::OnTCPTimer)
67 // Core timer
68 EVT_MULE_TIMER(ID_CORE_TIMER_EVENT, CamuleGuiApp::OnCoreTimer)
70 EVT_MULE_NOTIFY(CamuleGuiApp::OnNotifyEvent)
71 EVT_MULE_LOGGING(CamuleGuiApp::OnLoggingEvent)
73 // Async dns handling
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)
89 END_EVENT_TABLE()
92 IMPLEMENT_APP(CamuleGuiApp)
94 #endif // CLIENT_GUI
96 CamuleGuiBase::CamuleGuiBase()
98 amuledlg = NULL;
99 m_FileDetailDialogActive = 0;
103 CamuleGuiBase::~CamuleGuiBase()
105 #ifndef CLIENT_GUI
106 CPartFileConvert::StopThread();
107 #endif
111 void CamuleGuiBase::ShowAlert(wxString msg, wxString title, int flags)
113 wxMessageBox(msg, title, flags);
117 int CamuleGuiBase::InitGui(bool geometry_enabled, wxString &geom_string)
119 // Standard size is 800x600 at position (0,0)
120 int geometry_x = 0;
121 int geometry_y = 0;
122 unsigned int geometry_width = 800;
123 unsigned int geometry_height = 600;
125 if ( geometry_enabled ) {
126 // I plan on moving this to a separate function, as it just clutters up OnInit()
128 This implementation might work with mac, provided that the
129 SetSize() function works as expected.
132 // Remove possible prefix
133 if ( geom_string.GetChar(0) == '=' ) {
134 geom_string.Remove( 0, 1 );
137 // Stupid ToLong functions forces me to use longs =(
138 long width = geometry_width;
139 long height = geometry_height;
141 // Get the avilable display area
142 wxRect display = wxGetClientDisplayRect();
144 // We want to place aMule inside the client area by default
145 long x = display.x;
146 long y = display.y;
148 // Tokenize the string
149 wxStringTokenizer tokens(geom_string, wxT("xX+-"));
151 // First part: Program width
152 if ( tokens.GetNextToken().ToLong( &width ) ) {
153 wxString prefix = geom_string[ tokens.GetPosition() - 1 ];
154 if ( prefix == wxT("x") || prefix == wxT("X") ) {
155 // Second part: Program height
156 if ( tokens.GetNextToken().ToLong( &height ) ) {
157 prefix = geom_string[ tokens.GetPosition() - 1 ];
158 if ( prefix == wxT("+") || prefix == wxT("-") ) {
159 // Third part: X-Offset
160 if ( tokens.GetNextToken().ToLong( &x ) ) {
161 if ( prefix == wxT("-") )
162 x = display.GetRight() - ( width + x );
163 prefix = geom_string[ tokens.GetPosition() - 1 ];
164 if ( prefix == wxT("+") || prefix == wxT("-") ) {
165 // Fourth part: Y-Offset
166 if ( tokens.GetNextToken().ToLong( &y ) ) {
167 if ( prefix == wxT("-") )
168 y = display.GetBottom() - ( height + y );
173 // We need at least height and width to override default geometry
174 geometry_enabled = true;
175 geometry_x = x;
176 geometry_y = y;
177 geometry_width = width;
178 geometry_height = height;
184 // Should default/last-used position be overridden?
185 #ifdef SVNDATE
186 #ifdef CLIENT_GUI
187 m_FrameTitle = wxString::Format(wxT("aMule remote control %s %s"), wxT( VERSION ), wxT( SVNDATE ));
188 #else
189 m_FrameTitle = wxString::Format(wxT("aMule %s %s"), wxT( VERSION ), wxT( SVNDATE ));
190 #endif
191 #else
192 #ifdef CLIENT_GUI
193 m_FrameTitle = wxString::Format(wxT("aMule remote control %s"), wxT( VERSION ));
194 #else
195 m_FrameTitle = wxString::Format(wxT("aMule %s"), wxT( VERSION ));
196 #endif
197 #endif
198 if ( geometry_enabled ) {
199 amuledlg = new CamuleDlg(NULL, m_FrameTitle,
200 wxPoint(geometry_x,geometry_y),
201 wxSize( geometry_width, geometry_height - 58 ));
202 } else {
203 amuledlg = new CamuleDlg(NULL, m_FrameTitle);
206 return 0;
210 // Sets the contents of the clipboard. Prior content erased.
211 bool CamuleGuiBase::CopyTextToClipboard(wxString strText)
213 bool ClipBoardOpen = wxTheClipboard->Open();
214 if (ClipBoardOpen) {
215 wxTheClipboard->UsePrimarySelection(false);
216 wxTheClipboard->SetData(new wxTextDataObject(strText));
217 wxTheClipboard->Close();
220 return ClipBoardOpen;
224 #ifndef CLIENT_GUI
226 int CamuleGuiApp::InitGui(bool geometry_enable, wxString &geometry_string)
228 CamuleGuiBase::InitGui(geometry_enable, geometry_string);
229 SetTopWindow(amuledlg);
230 return 0;
234 void CamuleGuiApp::ShowAlert(wxString msg, wxString title, int flags)
236 CamuleGuiBase::ShowAlert(msg, title, flags);
240 int CamuleGuiApp::OnExit()
242 delete core_timer;
244 return CamuleApp::OnExit();
248 void CamuleGuiApp::ShutDown(wxCloseEvent &WXUNUSED(evt))
250 amuledlg->DlgShutDown();
251 amuledlg->Destroy();
252 CamuleApp::ShutDown();
256 bool CamuleGuiApp::OnInit()
258 amuledlg = NULL;
260 if ( !CamuleApp::OnInit() ) {
261 return false;
264 // Create the Core timer
265 core_timer = new CTimer(this,ID_CORE_TIMER_EVENT);
266 if (!core_timer) {
267 printf("Fatal Error: Failed to create Core Timer");
268 OnExit();
271 // Start the Core and Gui timers
273 // Note: wxTimer can be off by more than 10% !!!
274 // In addition to the systematic error introduced by wxTimer, we are losing
275 // timer cycles due to high CPU load. I've observed about 0.5% random loss of cycles under
276 // low load, and more than 6% lost cycles with heavy download traffic and/or other tasks
277 // in the system, such as a video player or a VMware virtual machine.
278 // The upload queue process loop has now been rewritten to compensate for timer errors.
279 // When adding functionality, assume that the timer is only approximately correct;
280 // for measurements, always use the system clock [::GetTickCount()].
281 core_timer->Start(100);
282 amuledlg->StartGuiTimer();
284 #ifdef __WXMAC__
285 // This tells the OS to notice the ed2kHelperScript.app inside aMule.app.
286 // ed2kHelperScript.app describes itself (Info.plist) as handling ed2k URLs.
287 // So, from then on the OS will know to pass ed2k URLs to the helper app.
288 CFURLRef ed2kHelperUrl = CFBundleCopyAuxiliaryExecutableURL(
289 CFBundleGetMainBundle(), CFSTR("ed2kHelperScript.app"));
290 if (ed2kHelperUrl) {
291 LSRegisterURL(ed2kHelperUrl, true);
292 CFRelease(ed2kHelperUrl);
294 #endif
296 return true;
300 wxString CamuleGuiApp::GetLog(bool reset)
302 if ( reset ) {
303 amuledlg->ResetLog(ID_LOGVIEW);
305 return CamuleApp::GetLog(reset);
309 wxString CamuleGuiApp::GetServerLog(bool reset)
311 if ( reset ) {
312 amuledlg->ResetLog(ID_SERVERINFO);
314 return CamuleApp::GetServerLog(reset);
318 void CamuleGuiApp::AddServerMessageLine(wxString &msg)
320 amuledlg->AddServerMessageLine(msg);
321 CamuleApp::AddServerMessageLine(msg);
325 void CamuleGuiApp::OnLoggingEvent(CLoggingEvent& evt)
327 if (amuledlg) {
328 while ( !m_logLines.empty() ) {
329 QueuedLogLine entry = m_logLines.front();
330 amuledlg->AddLogLine( entry.show, entry.line );
331 m_logLines.pop_front();
334 amuledlg->AddLogLine(evt.IsCritical(), evt.Message());
335 } else {
336 QueuedLogLine entry = { evt.Message(), evt.IsCritical() };
337 m_logLines.push_back( entry );
340 CamuleApp::AddLogLine( evt.Message() );
343 #endif /* CLIENT_GUI */
344 // File_checked_for_headers