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
;
80 class CAllocFinishedEvent
;
85 namespace MuleNotify
{
90 using MuleNotify::CMuleGUIEvent
;
94 #define AMULE_APP_BASE wxAppConsole
96 #define AMULE_APP_BASE wxApp
99 #define CONNECTED_ED2K (1<<0)
100 #define CONNECTED_KAD_NOT (1<<1)
101 #define CONNECTED_KAD_OK (1<<2)
102 #define CONNECTED_KAD_FIREWALLED (1<<3)
105 class CamuleApp
: public AMULE_APP_BASE
109 APP_STATE_RUNNING
= 0,
110 APP_STATE_SHUTTINGDOWN
,
116 virtual ~CamuleApp();
118 virtual bool OnInit();
120 void OnFatalException();
121 bool ReinitializeNetwork(wxString
*msg
);
123 // derived classes may override those
124 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
127 void ListenSocketHandler(wxSocketEvent
& event
);
128 void ServerSocketHandler(wxSocketEvent
& event
);
129 void UDPSocketHandler(wxSocketEvent
& event
);
131 virtual void ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
133 // Barry - To find out if app is running or shutting/shut down
134 const bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
135 const bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
137 // Check ED2K and Kademlia state
140 bool IsFirewalledKad();
141 // Check if we should callback this client
142 bool DoCallback( CUpDownClient
*client
);
144 // Connection to ED2K
145 bool IsConnectedED2K();
147 bool IsConnectedKad();
148 // Are we connected to at least one network?
151 // What about Kad? Is it running?
155 wxString
CreateMagnetLink(const CAbstractFile
*f
);
156 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool addcryptoptions
= false);
157 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
160 void OnlineSig(bool zero
= false);
161 void Localize_mule();
162 void Trigger_New_version(wxString newMule
);
164 // shakraw - new EC code using wxSocketBase
165 ExternalConn
* ECServerHandler
;
167 // return current (valid) public IP or 0 if unknown
168 // If ignorelocal is true, don't use m_localip
169 uint32
GetPublicIP(bool ignorelocal
= false) const;
170 void SetPublicIP(const uint32 dwIP
);
172 uint32
GetED2KID() const;
173 uint32
GetID() const;
175 // Other parts of the interface and such
176 CPreferences
* glob_prefs
;
177 CDownloadQueue
* downloadqueue
;
178 CUploadQueue
* uploadqueue
;
179 CServerConnect
* serverconnect
;
180 CSharedFileList
* sharedfiles
;
181 CServerList
* serverlist
;
182 CListenSocket
* listensocket
;
183 CClientList
* clientlist
;
184 CKnownFileList
* knownfiles
;
185 CSearchList
* searchlist
;
186 CClientCreditsList
* clientcredits
;
187 CFriendList
* friendlist
;
188 CClientUDPSocket
* clientudp
;
189 CStatistics
* m_statistics
;
191 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
193 CUPnPControlPoint
* m_upnp
;
194 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
200 wxString
GetLog(bool reset
= false);
201 wxString
GetServerLog(bool reset
= false);
202 wxString
GetDebugLog(bool reset
= false);
204 bool AddServer(CServer
*srv
, bool fromUser
= false);
205 void AddServerMessageLine(wxString
&msg
);
207 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
209 void SetOSFiles(const wxString new_path
);
213 void AddLogLine(const wxString
&msg
);
215 const wxString
& GetOSType() const { return OSType
; }
217 void ShowUserCount();
219 void ShowConnectionState();
224 /** Bootstraps kad from the specified IP (must be in hostorder). */
225 void BootstrapKad(uint32 ip
, uint16 port
);
226 /** Updates the nodes.dat file from the specified url. */
227 void UpdateNotesDat(const wxString
& str
);
230 void DisconnectED2K();
232 bool CryptoAvailable() const;
234 //! TODO: Move to CLogger
235 wxFFileOutputStream
* applog
;
237 // Used to detect a previous running instance of aMule
238 wxSingleInstanceChecker
* m_singleInstance
;
242 * Handles asserts in a thread-safe manner.
244 virtual void OnAssertFailure(const wxChar
* file
, int line
,
245 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
249 * This class is used to contain log messages that are to be displayed
250 * on the GUI, when it is currently impossible to do so. This is in order
251 * to allows us to queue messages till after the dialog has been created.
255 //! The text line to be displayed
257 //! True if the line should be shown on the status bar, false otherwise.
261 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
262 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
263 void OnServerDnsDone(CMuleInternalEvent
& evt
);
265 void OnTCPTimer(CTimerEvent
& evt
);
266 void OnCoreTimer(CTimerEvent
& evt
);
268 void OnFinishedHashing(CHashingEvent
& evt
);
269 void OnFinishedAICHHashing(CHashingEvent
& evt
);
270 void OnFinishedCompletion(CCompletionEvent
& evt
);
271 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
272 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
273 void OnHashingShutdown(CMuleInternalEvent
&);
274 void OnNotifyEvent(CMuleGUIEvent
& evt
);
276 void SetTimeOnTransfer();
278 std::list
<QueuedLogLine
> m_logLines
;
280 APPState m_app_state
;
282 wxString m_emulesig_path
;
283 wxString m_amulesig_path
;
285 char *strFullMuleVersion
;
286 char *strOSDescription
;
293 bool enable_stdout_log
;
294 bool enable_daemon_fork
;
300 virtual void OnUnhandledException();
302 void CheckNewVersion(uint32 result
);
311 class CamuleGuiBase
{
314 virtual ~CamuleGuiBase();
316 wxString m_FrameTitle
;
318 int m_FileDetailDialogActive
;
320 bool CopyTextToClipboard( wxString strText
);
322 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
323 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
330 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
333 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
340 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
342 void ShutDown(wxCloseEvent
&evt
);
343 void OnLoggingEvent(CLoggingEvent
& evt
);
345 wxString
GetLog(bool reset
= false);
346 wxString
GetServerLog(bool reset
= false);
347 void AddServerMessageLine(wxString
&msg
);
348 DECLARE_EVENT_TABLE()
352 DECLARE_APP(CamuleGuiApp
)
354 CamuleGuiApp
*theApp
;
356 extern CamuleGuiApp
*theApp
;
360 #else /* !CLIENT_GUI */
363 #include "amule-remote-gui.h"
369 #define CALL_APP_DATA_LOCK
372 #else /* ! AMULE_DAEMON */
375 #include <wx/apptrait.h>
376 #include <wx/socket.h>
382 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
385 CSocketSet
*m_in_set
, *m_out_set
;
389 CAmuledGSocketFuncTable();
391 void AddSocket(GSocket
*socket
, GSocketEvent event
);
392 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
395 virtual bool OnInit();
396 virtual void OnExit();
397 virtual bool CanUseEventLoop();
398 virtual bool Init_Socket(GSocket
*socket
);
399 virtual void Destroy_Socket(GSocket
*socket
);
400 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
401 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
402 virtual void Enable_Events(GSocket
*socket
);
403 virtual void Disable_Events(GSocket
*socket
);
407 typedef std::map
<int, wxEndProcessData
*> EndProcessDataMap
;
410 class CDaemonAppTraits
: public wxConsoleAppTraits
413 CAmuledGSocketFuncTable
*m_table
;
415 std::list
<wxObject
*> m_sched_delete
;
417 struct sigaction m_oldSignalChildAction
;
418 struct sigaction m_newSignalChildAction
;
422 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
423 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
424 virtual void ScheduleForDestroy(wxObject
*object
);
425 virtual void RemoveFromPendingDelete(wxObject
*object
);
427 void DeletePending();
430 virtual int WaitForChild(wxExecuteData
& execData
);
433 virtual wxStandardPathsBase
& GetStandardPaths();
439 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
440 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
444 class CamuleDaemonApp
: public CamuleApp
448 CAmuledGSocketFuncTable
*m_table
;
450 struct sigaction m_oldSignalChildAction
;
451 struct sigaction m_newSignalChildAction
;
458 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
463 void ExitMainLoop() { m_Exit
= true; }
465 bool CopyTextToClipboard(wxString strText
);
467 virtual void ShowAlert(wxString msg
, wxString title
, int flags
);
469 void OnLoggingEvent(CLoggingEvent
& evt
);
471 DECLARE_EVENT_TABLE()
473 wxAppTraits
*CreateTraits();
476 DECLARE_APP(CamuleDaemonApp
)
478 CamuleDaemonApp
*theApp
;
480 extern CamuleDaemonApp
*theApp
;
483 #endif /* ! AMULE_DAEMON */
486 // File_checked_for_headers