2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include <wx/app.h> // Needed for wxApp
32 #include <wx/intl.h> // Needed for wxLocale
35 #include "Types.h" // Needed for int32, uint16 and uint64
39 // #include <wx/unix/execute.h>
43 # include "config.h" // Needed for ASIO_SOCKETS
55 class CSharedFileList
;
63 class CCanceledFileList
;
65 class CClientCreditsList
;
67 class CClientUDPSocket
;
69 class UploadBandwidthThrottler
;
76 class CUPnPControlPoint
;
77 class CUPnPPortMapping
;
82 class wxFFileOutputStream
;
85 class wxSingleInstanceChecker
;
87 class CMuleInternalEvent
;
88 class CCompletionEvent
;
89 class CAllocFinishedEvent
;
94 namespace MuleNotify
{
99 using MuleNotify::CMuleGUIEvent
;
103 #define AMULE_APP_BASE wxAppConsole
104 #define CORE_TIMER_PERIOD 300
106 #define AMULE_APP_BASE wxApp
107 #define CORE_TIMER_PERIOD 100
110 #define CONNECTED_ED2K (1<<0)
111 #define CONNECTED_KAD_NOT (1<<1)
112 #define CONNECTED_KAD_OK (1<<2)
113 #define CONNECTED_KAD_FIREWALLED (1<<3)
116 // Base class common to amule, aamuled and amulegui
117 class CamuleAppCommon
120 // Used to detect a previous running instance of aMule
121 wxSingleInstanceChecker
* m_singleInstance
;
123 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
125 wxString FullMuleVersion
;
126 wxString OSDescription
;
128 bool enable_daemon_fork
;
130 bool m_skipConnectionDialog
;
131 bool m_geometryEnabled
;
132 wxString m_geometryString
;
137 bool InitCommon(int argc
, wxChar
** argv
);
138 void RefreshSingleInstanceChecker();
139 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
141 wxString m_configFile
;
145 void AddLinksFromFile();
147 wxString
CreateMagnetLink(const CAbstractFile
*f
);
148 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
151 bool IsDaemon() const { return true; }
153 bool IsDaemon() const { return false; }
157 bool IsRemoteGui() const { return true; }
159 bool IsRemoteGui() const { return false; }
162 const wxString
& GetMuleAppName() const { return m_appName
; }
163 const wxString
GetFullMuleVersion() const;
166 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
170 APP_STATE_RUNNING
= 0,
171 APP_STATE_SHUTTINGDOWN
,
177 virtual ~CamuleApp();
179 virtual bool OnInit();
181 #if wxUSE_ON_FATAL_EXCEPTION
182 void OnFatalException();
184 bool ReinitializeNetwork(wxString
*msg
);
186 // derived classes may override those
187 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
191 void ListenSocketHandler(wxSocketEvent
& event
);
192 void UDPSocketHandler(wxSocketEvent
& event
);
195 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
197 // Barry - To find out if app is running or shutting/shut down
198 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
199 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
201 // Check ED2K and Kademlia state
202 bool IsFirewalled() const;
203 // Are we connected to at least one network?
204 bool IsConnected() const;
205 // Connection to ED2K
206 bool IsConnectedED2K() const;
208 // What about Kad? Is it running?
209 bool IsKadRunning() const;
211 bool IsConnectedKad() const;
212 // Check Kad state (TCP)
213 bool IsFirewalledKad() const;
214 // Check Kad state (UDP)
215 bool IsFirewalledKadUDP() const;
216 // Check Kad state (LAN mode)
217 bool IsKadRunningInLanMode() const;
219 uint32
GetKadUsers() const;
220 uint32
GetKadFiles() const;
221 uint32
GetKadIndexedSources() const;
222 uint32
GetKadIndexedKeywords() const;
223 uint32
GetKadIndexedNotes() const;
224 uint32
GetKadIndexedLoad() const;
225 // True IP of machine
226 uint32
GetKadIPAdress() const;
228 uint8
GetBuddyStatus() const;
229 uint32
GetBuddyIP() const;
230 uint32
GetBuddyPort() const;
232 // Check if we should callback this client
233 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
236 void OnlineSig(bool zero
= false);
237 void Localize_mule();
238 void Trigger_New_version(wxString newMule
);
240 // shakraw - new EC code using wxSocketBase
241 ExternalConn
* ECServerHandler
;
243 // return current (valid) public IP or 0 if unknown
244 // If ignorelocal is true, don't use m_localip
245 uint32
GetPublicIP(bool ignorelocal
= false) const;
246 void SetPublicIP(const uint32 dwIP
);
248 uint32
GetED2KID() const;
249 uint32
GetID() const;
251 // Other parts of the interface and such
252 CPreferences
* glob_prefs
;
253 CDownloadQueue
* downloadqueue
;
254 CUploadQueue
* uploadqueue
;
255 CServerConnect
* serverconnect
;
256 CSharedFileList
* sharedfiles
;
257 CServerList
* serverlist
;
258 CListenSocket
* listensocket
;
259 CClientList
* clientlist
;
260 CKnownFileList
* knownfiles
;
261 CCanceledFileList
* canceledfiles
;
262 CSearchList
* searchlist
;
263 CClientCreditsList
* clientcredits
;
264 CFriendList
* friendlist
;
265 CClientUDPSocket
* clientudp
;
266 CStatistics
* m_statistics
;
268 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
270 CAsioService
* m_AsioService
;
273 CUPnPControlPoint
* m_upnp
;
274 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
280 wxString
GetLog(bool reset
= false);
281 wxString
GetServerLog(bool reset
= false);
282 wxString
GetDebugLog(bool reset
= false);
284 bool AddServer(CServer
*srv
, bool fromUser
= false);
285 void AddServerMessageLine(wxString
&msg
);
287 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
289 void SetOSFiles(const wxString
& new_path
);
291 const wxString
& GetOSType() const { return OSType
; }
293 void ShowUserCount();
295 void ShowConnectionState(bool forceUpdate
= false);
300 /** Bootstraps kad from the specified IP (must be in hostorder). */
301 void BootstrapKad(uint32 ip
, uint16 port
);
302 /** Updates the nodes.dat file from the specified url. */
303 void UpdateNotesDat(const wxString
& str
);
306 void DisconnectED2K();
308 bool CryptoAvailable() const;
314 * Handles asserts in a thread-safe manner.
316 virtual void OnAssertFailure(const wxChar
* file
, int line
,
317 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
320 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
321 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
322 void OnServerDnsDone(CMuleInternalEvent
& evt
);
324 void OnTCPTimer(CTimerEvent
& evt
);
325 void OnCoreTimer(CTimerEvent
& evt
);
327 void OnFinishedHashing(CHashingEvent
& evt
);
328 void OnFinishedAICHHashing(CHashingEvent
& evt
);
329 void OnFinishedCompletion(CCompletionEvent
& evt
);
330 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
331 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
332 void OnHashingShutdown(CMuleInternalEvent
&);
333 void OnNotifyEvent(CMuleGUIEvent
& evt
);
335 void SetTimeOnTransfer();
337 APPState m_app_state
;
339 wxString m_emulesig_path
;
340 wxString m_amulesig_path
;
351 virtual void OnUnhandledException();
353 void CheckNewVersion(uint32 result
);
362 class CamuleGuiBase
{
365 virtual ~CamuleGuiBase();
367 wxString m_FrameTitle
;
369 int m_FileDetailDialogActive
;
371 bool CopyTextToClipboard( wxString strText
);
374 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
375 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
377 void AddGuiLogLine(const wxString
& line
);
380 * This list is used to contain log messages that are to be displayed
381 * on the GUI, when it is currently impossible to do so. This is in order
382 * to allows us to queue messages till after the dialog has been created.
384 std::list
<wxString
> m_logLines
;
391 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
394 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
401 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
403 void ShutDown(wxCloseEvent
&evt
);
405 wxString
GetLog(bool reset
= false);
406 wxString
GetServerLog(bool reset
= false);
407 void AddServerMessageLine(wxString
&msg
);
408 DECLARE_EVENT_TABLE()
412 DECLARE_APP(CamuleGuiApp
)
413 extern CamuleGuiApp
*theApp
;
416 #else /* !CLIENT_GUI */
419 #include "amule-remote-gui.h"
425 #define CALL_APP_DATA_LOCK
428 #else /* ! AMULE_DAEMON */
430 // wxWidgets 2.8 requires special code for event handling and sockets.
431 // 2.9 doesn't, so standard event loop and sockets can be used
433 // Windows: aMuled compiles with 2.8 (without the special code),
434 // but works only with 2.9
436 #if !wxCHECK_VERSION(2, 9, 0)
437 // wx 2.8 needs a hand-made event loop in any case
438 #define AMULED28_EVENTLOOP
440 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
443 # include "config.h" // defines ASIO_SOCKETS
447 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
449 #define AMULED28_SOCKETS
454 #ifdef AMULED28_SOCKETS
455 #include <wx/socket.h>
460 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
463 CSocketSet
*m_in_set
, *m_out_set
;
467 CAmuledGSocketFuncTable();
469 void AddSocket(GSocket
*socket
, GSocketEvent event
);
470 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
473 virtual bool OnInit();
474 virtual void OnExit();
475 virtual bool CanUseEventLoop();
476 virtual bool Init_Socket(GSocket
*socket
);
477 virtual void Destroy_Socket(GSocket
*socket
);
478 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
479 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
480 virtual void Enable_Events(GSocket
*socket
);
481 virtual void Disable_Events(GSocket
*socket
);
485 #endif // AMULED28_SOCKETS
487 // AppTrait functionality is required for 2.8 wx sockets
488 // Otherwise it's used to prevent zombie child processes,
489 // which stops working with wx 2.9.5.
490 // So disable it there (no idea if this has a noticeable impact).
492 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__ )
493 #define AMULED_APPTRAITS
496 #ifdef AMULED_APPTRAITS
498 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
500 #include <wx/apptrait.h>
502 class CDaemonAppTraits
: public wxConsoleAppTraits
505 struct sigaction m_oldSignalChildAction
;
506 struct sigaction m_newSignalChildAction
;
508 #ifdef AMULED28_SOCKETS
509 CAmuledGSocketFuncTable
*m_table
;
511 std::list
<wxObject
*> m_sched_delete
;
513 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
514 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
515 virtual void ScheduleForDestroy(wxObject
*object
);
516 virtual void RemoveFromPendingDelete(wxObject
*object
);
518 void DeletePending();
519 #else // AMULED28_SOCKETS
522 #endif // !AMULED28_SOCKETS
524 virtual int WaitForChild(wxExecuteData
& execData
);
526 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
527 virtual wxStandardPathsBase
& GetStandardPaths();
531 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
532 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
534 #endif // AMULED_APPTRAITS
537 class CamuleDaemonApp
: public CamuleApp
540 #ifdef AMULED28_EVENTLOOP
543 #ifdef AMULED28_SOCKETS
544 CAmuledGSocketFuncTable
*m_table
;
550 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
552 #ifdef AMULED_APPTRAITS
553 struct sigaction m_oldSignalChildAction
;
554 struct sigaction m_newSignalChildAction
;
556 wxAppTraits
*CreateTraits();
557 #endif // AMULED_APPTRAITS
561 #ifdef AMULED28_EVENTLOOP
564 void ExitMainLoop() { m_Exit
= true; }
567 bool CopyTextToClipboard(wxString strText
);
569 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
571 DECLARE_EVENT_TABLE()
574 DECLARE_APP(CamuleDaemonApp
)
575 extern CamuleDaemonApp
*theApp
;
577 #endif /* ! AMULE_DAEMON */
580 // File_checked_for_headers