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-2008 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 int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
133 // Barry - To find out if app is running or shutting/shut down
134 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
135 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
137 // Check ED2K and Kademlia state
138 bool IsFirewalled() const;
139 // Are we connected to at least one network?
140 bool IsConnected() const;
141 // Connection to ED2K
142 bool IsConnectedED2K() const;
144 // What about Kad? Is it running?
145 bool IsKadRunning() const;
147 bool IsConnectedKad() const;
148 // Check Kad state (TCP)
149 bool IsFirewalledKad() const;
150 // Check Kad state (UDP)
151 bool IsFirewalledKadUDP() const;
153 uint32
GetKadUsers() const;
154 uint32
GetKadFiles() const;
155 uint32
GetKadIndexedSources() const;
156 uint32
GetKadIndexedKeywords() const;
157 uint32
GetKadIndexedNotes() const;
158 uint32
GetKadIndexedLoad() const;
159 // True IP of machine
160 uint32
GetKadIPAdress() const;
162 uint8
GetBuddyStatus() const;
163 uint32
GetBuddyIP() const;
164 uint32
GetBuddyPort() const;
166 // Check if we should callback this client
167 bool DoCallback( CUpDownClient
*client
);
170 wxString
CreateMagnetLink(const CAbstractFile
*f
);
171 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool addcryptoptions
= false);
172 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
175 void OnlineSig(bool zero
= false);
176 void Localize_mule();
177 void Trigger_New_version(wxString newMule
);
179 // shakraw - new EC code using wxSocketBase
180 ExternalConn
* ECServerHandler
;
182 // return current (valid) public IP or 0 if unknown
183 // If ignorelocal is true, don't use m_localip
184 uint32
GetPublicIP(bool ignorelocal
= false) const;
185 void SetPublicIP(const uint32 dwIP
);
187 uint32
GetED2KID() const;
188 uint32
GetID() const;
190 // Other parts of the interface and such
191 CPreferences
* glob_prefs
;
192 CDownloadQueue
* downloadqueue
;
193 CUploadQueue
* uploadqueue
;
194 CServerConnect
* serverconnect
;
195 CSharedFileList
* sharedfiles
;
196 CServerList
* serverlist
;
197 CListenSocket
* listensocket
;
198 CClientList
* clientlist
;
199 CKnownFileList
* knownfiles
;
200 CSearchList
* searchlist
;
201 CClientCreditsList
* clientcredits
;
202 CFriendList
* friendlist
;
203 CClientUDPSocket
* clientudp
;
204 CStatistics
* m_statistics
;
206 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
208 CUPnPControlPoint
* m_upnp
;
209 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
215 wxString
GetLog(bool reset
= false);
216 wxString
GetServerLog(bool reset
= false);
217 wxString
GetDebugLog(bool reset
= false);
219 bool AddServer(CServer
*srv
, bool fromUser
= false);
220 void AddServerMessageLine(wxString
&msg
);
222 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
224 void SetOSFiles(const wxString new_path
);
228 const wxString
& GetOSType() const { return OSType
; }
230 void ShowUserCount();
232 void ShowConnectionState();
237 /** Bootstraps kad from the specified IP (must be in hostorder). */
238 void BootstrapKad(uint32 ip
, uint16 port
);
239 /** Updates the nodes.dat file from the specified url. */
240 void UpdateNotesDat(const wxString
& str
);
243 void DisconnectED2K();
245 bool CryptoAvailable() const;
248 // Used to detect a previous running instance of aMule
249 wxSingleInstanceChecker
* m_singleInstance
;
253 * Handles asserts in a thread-safe manner.
255 virtual void OnAssertFailure(const wxChar
* file
, int line
,
256 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
259 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
260 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
261 void OnServerDnsDone(CMuleInternalEvent
& evt
);
263 void OnTCPTimer(CTimerEvent
& evt
);
264 void OnCoreTimer(CTimerEvent
& evt
);
266 void OnFinishedHashing(CHashingEvent
& evt
);
267 void OnFinishedAICHHashing(CHashingEvent
& evt
);
268 void OnFinishedCompletion(CCompletionEvent
& evt
);
269 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
270 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
271 void OnHashingShutdown(CMuleInternalEvent
&);
272 void OnNotifyEvent(CMuleGUIEvent
& evt
);
274 void SetTimeOnTransfer();
276 APPState m_app_state
;
278 wxString m_emulesig_path
;
279 wxString m_amulesig_path
;
281 char *strFullMuleVersion
;
282 char *strOSDescription
;
289 bool enable_daemon_fork
;
297 virtual void OnUnhandledException();
299 void CheckNewVersion(uint32 result
);
308 class CamuleGuiBase
{
311 virtual ~CamuleGuiBase();
313 wxString m_FrameTitle
;
315 int m_FileDetailDialogActive
;
317 bool CopyTextToClipboard( wxString strText
);
319 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
320 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
322 void AddGuiLogLine(const wxString
& line
);
325 * This list is used to contain log messages that are to be displayed
326 * on the GUI, when it is currently impossible to do so. This is in order
327 * to allows us to queue messages till after the dialog has been created.
329 std::list
<wxString
> m_logLines
;
336 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
339 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
346 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
348 void ShutDown(wxCloseEvent
&evt
);
350 wxString
GetLog(bool reset
= false);
351 wxString
GetServerLog(bool reset
= false);
352 void AddServerMessageLine(wxString
&msg
);
353 DECLARE_EVENT_TABLE()
357 DECLARE_APP(CamuleGuiApp
)
359 CamuleGuiApp
*theApp
;
361 extern CamuleGuiApp
*theApp
;
365 #else /* !CLIENT_GUI */
368 #include "amule-remote-gui.h"
374 #define CALL_APP_DATA_LOCK
377 #else /* ! AMULE_DAEMON */
379 // wxWidgets 2.8 requires special code for event handling and sockets.
380 // 2.9 doesn't, so standard event loop and sockets can be used
382 // Windows: aMuled compiles with 2.8 (without the special code),
383 // but works only with 2.9
385 #if !wxCHECK_VERSION(2, 9, 0)
387 // MSW: can't run amuled with 2.8 anyway, just get it compiled
395 #include <wx/socket.h>
400 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
403 CSocketSet
*m_in_set
, *m_out_set
;
407 CAmuledGSocketFuncTable();
409 void AddSocket(GSocket
*socket
, GSocketEvent event
);
410 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
413 virtual bool OnInit();
414 virtual void OnExit();
415 virtual bool CanUseEventLoop();
416 virtual bool Init_Socket(GSocket
*socket
);
417 virtual void Destroy_Socket(GSocket
*socket
);
418 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
419 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
420 virtual void Enable_Events(GSocket
*socket
);
421 virtual void Disable_Events(GSocket
*socket
);
427 // no AppTraits used on Windows
430 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
432 #include <wx/apptrait.h>
434 class CDaemonAppTraits
: public wxConsoleAppTraits
437 struct sigaction m_oldSignalChildAction
;
438 struct sigaction m_newSignalChildAction
;
441 CAmuledGSocketFuncTable
*m_table
;
443 std::list
<wxObject
*> m_sched_delete
;
445 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
446 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
447 virtual void ScheduleForDestroy(wxObject
*object
);
448 virtual void RemoveFromPendingDelete(wxObject
*object
);
450 void DeletePending();
456 virtual int WaitForChild(wxExecuteData
& execData
);
459 virtual wxStandardPathsBase
& GetStandardPaths();
463 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
464 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
468 class CamuleDaemonApp
: public CamuleApp
473 CAmuledGSocketFuncTable
*m_table
;
479 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
482 struct sigaction m_oldSignalChildAction
;
483 struct sigaction m_newSignalChildAction
;
485 wxAppTraits
*CreateTraits();
493 void ExitMainLoop() { m_Exit
= true; }
497 void ExitMainLoop() {}
500 bool CopyTextToClipboard(wxString strText
);
502 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
504 DECLARE_EVENT_TABLE()
507 DECLARE_APP(CamuleDaemonApp
)
509 CamuleDaemonApp
*theApp
;
511 extern CamuleDaemonApp
*theApp
;
514 #endif /* ! AMULE_DAEMON */
517 // File_checked_for_headers