2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 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>
51 class CSharedFileList
;
60 class CClientCreditsList
;
62 class CClientUDPSocket
;
64 class UploadBandwidthThrottler
;
66 class CUPnPControlPoint
;
67 class CUPnPPortMapping
;
72 class wxFFileOutputStream
;
76 class wxSingleInstanceChecker
;
78 class CMuleInternalEvent
;
79 class CCompletionEvent
;
84 namespace MuleNotify
{
89 using MuleNotify::CMuleGUIEvent
;
93 #define AMULE_APP_BASE wxAppConsole
95 #define AMULE_APP_BASE wxApp
98 #define CONNECTED_ED2K (1<<0)
99 #define CONNECTED_KAD_NOT (1<<1)
100 #define CONNECTED_KAD_OK (1<<2)
101 #define CONNECTED_KAD_FIREWALLED (1<<3)
104 class CamuleApp
: public AMULE_APP_BASE
108 APP_STATE_RUNNING
= 0,
109 APP_STATE_SHUTTINGDOWN
,
115 virtual ~CamuleApp();
117 virtual bool OnInit();
119 void OnFatalException();
120 bool ReinitializeNetwork(wxString
*msg
);
122 // derived classes may override those
123 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
126 void ListenSocketHandler(wxSocketEvent
& event
);
127 void ServerSocketHandler(wxSocketEvent
& event
);
128 void UDPSocketHandler(wxSocketEvent
& event
);
130 virtual void ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
132 // Barry - To find out if app is running or shutting/shut down
133 const bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
134 const bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
136 // Check ED2K and Kademlia state
139 bool IsFirewalledKad();
140 // Check if we should callback this client
141 bool DoCallback( CUpDownClient
*client
);
143 // Connection to ED2K
144 bool IsConnectedED2K();
146 bool IsConnectedKad();
147 // Are we connected to at least one network?
150 // What about Kad? Is it running?
154 wxString
CreateMagnetLink(const CAbstractFile
*f
);
155 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool addcryptoptions
= false);
156 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
159 void OnlineSig(bool zero
= false);
160 void Localize_mule();
161 void Trigger_New_version(wxString newMule
);
163 // shakraw - new EC code using wxSocketBase
164 ExternalConn
* ECServerHandler
;
166 // return current (valid) public IP or 0 if unknown
167 // If ignorelocal is true, don't use m_localip
168 uint32
GetPublicIP(bool ignorelocal
= false) const;
169 void SetPublicIP(const uint32 dwIP
);
171 uint32
GetED2KID() const;
172 uint32
GetID() const;
174 // Other parts of the interface and such
175 CPreferences
* glob_prefs
;
176 CDownloadQueue
* downloadqueue
;
177 CUploadQueue
* uploadqueue
;
178 CServerConnect
* serverconnect
;
179 CSharedFileList
* sharedfiles
;
180 CServerList
* serverlist
;
181 CListenSocket
* listensocket
;
182 CClientList
* clientlist
;
183 CKnownFileList
* knownfiles
;
184 CSearchList
* searchlist
;
185 CClientCreditsList
* clientcredits
;
186 CFriendList
* friendlist
;
187 CClientUDPSocket
* clientudp
;
188 CStatistics
* m_statistics
;
190 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
192 CUPnPControlPoint
* m_upnp
;
193 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
199 wxString
GetLog(bool reset
= false);
200 wxString
GetServerLog(bool reset
= false);
201 wxString
GetDebugLog(bool reset
= false);
203 bool AddServer(CServer
*srv
, bool fromUser
= false);
204 void AddServerMessageLine(wxString
&msg
);
206 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
208 void SetOSFiles(const wxString new_path
);
212 void AddLogLine(const wxString
&msg
);
214 const wxString
& GetOSType() const { return OSType
; }
216 void ShowUserCount();
218 void ShowConnectionState();
223 /** Bootstraps kad from the specified IP (must be in hostorder). */
224 void BootstrapKad(uint32 ip
, uint16 port
);
225 /** Updates the nodes.dat file from the specified url. */
226 void UpdateNotesDat(const wxString
& str
);
229 void DisconnectED2K();
231 bool CryptoAvailable() const;
233 //! TODO: Move to CLogger
234 wxFFileOutputStream
* applog
;
236 // Used to detect a previous running instance of aMule
237 wxSingleInstanceChecker
* m_singleInstance
;
241 * Handles asserts in a thread-safe manner.
243 virtual void OnAssertFailure(const wxChar
* file
, int line
,
244 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
248 * This class is used to contain log messages that are to be displayed
249 * on the GUI, when it is currently impossible to do so. This is in order
250 * to allows us to queue messages till after the dialog has been created.
254 //! The text line to be displayed
256 //! True if the line should be shown on the status bar, false otherwise.
260 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
261 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
262 void OnServerDnsDone(CMuleInternalEvent
& evt
);
264 void OnTCPTimer(CTimerEvent
& evt
);
265 void OnCoreTimer(CTimerEvent
& evt
);
267 void OnFinishedHashing(CHashingEvent
& evt
);
268 void OnFinishedAICHHashing(CHashingEvent
& evt
);
269 void OnFinishedCompletion(CCompletionEvent
& evt
);
270 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
271 void OnHashingShutdown(CMuleInternalEvent
&);
272 void OnNotifyEvent(CMuleGUIEvent
& evt
);
274 void SetTimeOnTransfer();
276 std::list
<QueuedLogLine
> m_logLines
;
278 APPState m_app_state
;
280 wxString m_emulesig_path
;
281 wxString m_amulesig_path
;
283 char *strFullMuleVersion
;
284 char *strOSDescription
;
291 bool enable_stdout_log
;
292 bool enable_daemon_fork
;
298 virtual void OnUnhandledException();
300 void CheckNewVersion(uint32 result
);
309 class CamuleGuiBase
{
312 virtual ~CamuleGuiBase();
314 wxString m_FrameTitle
;
316 int m_FileDetailDialogActive
;
318 bool CopyTextToClipboard( wxString strText
);
320 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
321 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
328 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
331 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
338 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
340 void ShutDown(wxCloseEvent
&evt
);
341 void OnLoggingEvent(CLoggingEvent
& evt
);
343 wxString
GetLog(bool reset
= false);
344 wxString
GetServerLog(bool reset
= false);
345 void AddServerMessageLine(wxString
&msg
);
346 DECLARE_EVENT_TABLE()
350 DECLARE_APP(CamuleGuiApp
)
352 CamuleGuiApp
*theApp
;
354 extern CamuleGuiApp
*theApp
;
358 #else /* !CLIENT_GUI */
361 #include "amule-remote-gui.h"
367 #define CALL_APP_DATA_LOCK
370 #else /* ! AMULE_DAEMON */
373 #include <wx/apptrait.h>
374 #include <wx/socket.h>
380 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
383 CSocketSet
*m_in_set
, *m_out_set
;
387 CAmuledGSocketFuncTable();
389 void AddSocket(GSocket
*socket
, GSocketEvent event
);
390 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
393 virtual bool OnInit();
394 virtual void OnExit();
395 virtual bool CanUseEventLoop();
396 virtual bool Init_Socket(GSocket
*socket
);
397 virtual void Destroy_Socket(GSocket
*socket
);
398 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
399 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
400 virtual void Enable_Events(GSocket
*socket
);
401 virtual void Disable_Events(GSocket
*socket
);
405 typedef std::map
<int, wxEndProcessData
*> EndProcessDataMap
;
408 class CDaemonAppTraits
: public wxConsoleAppTraits
411 CAmuledGSocketFuncTable
*m_table
;
413 std::list
<wxObject
*> m_sched_delete
;
415 struct sigaction m_oldSignalChildAction
;
416 struct sigaction m_newSignalChildAction
;
420 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
421 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
422 virtual void ScheduleForDestroy(wxObject
*object
);
423 virtual void RemoveFromPendingDelete(wxObject
*object
);
425 void DeletePending();
428 virtual int WaitForChild(wxExecuteData
& execData
);
431 virtual wxStandardPathsBase
& GetStandardPaths();
437 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
438 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
442 class CamuleDaemonApp
: public CamuleApp
446 CAmuledGSocketFuncTable
*m_table
;
448 struct sigaction m_oldSignalChildAction
;
449 struct sigaction m_newSignalChildAction
;
456 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
461 void ExitMainLoop() { m_Exit
= true; }
463 bool CopyTextToClipboard(wxString strText
);
465 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
467 void OnLoggingEvent(CLoggingEvent
& evt
);
469 DECLARE_EVENT_TABLE()
471 wxAppTraits
*CreateTraits();
474 DECLARE_APP(CamuleDaemonApp
)
476 CamuleDaemonApp
*theApp
;
478 extern CamuleDaemonApp
*theApp
;
481 #endif /* ! AMULE_DAEMON */
484 // File_checked_for_headers