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>
51 class CSharedFileList
;
59 class CCanceledFileList
;
61 class CClientCreditsList
;
63 class CClientUDPSocket
;
65 class UploadBandwidthThrottler
;
68 class CUPnPControlPoint
;
69 class CUPnPPortMapping
;
75 class wxFFileOutputStream
;
78 class wxSingleInstanceChecker
;
80 class CMuleInternalEvent
;
81 class CCompletionEvent
;
82 class CAllocFinishedEvent
;
87 namespace MuleNotify
{
92 using MuleNotify::CMuleGUIEvent
;
96 #define AMULE_APP_BASE wxAppConsole
97 #define CORE_TIMER_PERIOD 300
99 #define AMULE_APP_BASE wxApp
100 #define CORE_TIMER_PERIOD 100
103 #define CONNECTED_ED2K (1<<0)
104 #define CONNECTED_KAD_NOT (1<<1)
105 #define CONNECTED_KAD_OK (1<<2)
106 #define CONNECTED_KAD_FIREWALLED (1<<3)
109 // Base class common to amule, aamuled and amulegui
110 class CamuleAppCommon
113 // Used to detect a previous running instance of aMule
114 wxSingleInstanceChecker
* m_singleInstance
;
116 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
118 wxString FullMuleVersion
;
119 wxString OSDescription
;
121 bool enable_daemon_fork
;
123 bool m_skipConnectionDialog
;
124 bool m_geometryEnabled
;
125 wxString m_geometryString
;
130 bool InitCommon(int argc
, wxChar
** argv
);
131 void RefreshSingleInstanceChecker();
132 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
135 wxString m_configFile
;
139 void AddLinksFromFile();
141 wxString
CreateMagnetLink(const CAbstractFile
*f
);
142 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
145 bool IsDaemon() const { return true; }
147 bool IsDaemon() const { return false; }
151 bool IsRemoteGui() const { return true; }
153 bool IsRemoteGui() const { return false; }
156 const wxString
& GetMuleAppName() const { return m_appName
; }
157 const wxString
GetFullMuleVersion() const;
160 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
164 APP_STATE_RUNNING
= 0,
165 APP_STATE_SHUTTINGDOWN
,
171 virtual ~CamuleApp();
173 virtual bool OnInit();
175 #if wxUSE_ON_FATAL_EXCEPTION
176 void OnFatalException();
178 bool ReinitializeNetwork(wxString
*msg
);
180 // derived classes may override those
181 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
184 void ListenSocketHandler(wxSocketEvent
& event
);
185 void ServerSocketHandler(wxSocketEvent
& event
);
186 void UDPSocketHandler(wxSocketEvent
& event
);
188 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
190 // Barry - To find out if app is running or shutting/shut down
191 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
192 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
194 // Check ED2K and Kademlia state
195 bool IsFirewalled() const;
196 // Are we connected to at least one network?
197 bool IsConnected() const;
198 // Connection to ED2K
199 bool IsConnectedED2K() const;
201 // What about Kad? Is it running?
202 bool IsKadRunning() const;
204 bool IsConnectedKad() const;
205 // Check Kad state (TCP)
206 bool IsFirewalledKad() const;
207 // Check Kad state (UDP)
208 bool IsFirewalledKadUDP() const;
209 // Check Kad state (LAN mode)
210 bool IsKadRunningInLanMode() const;
212 uint32
GetKadUsers() const;
213 uint32
GetKadFiles() const;
214 uint32
GetKadIndexedSources() const;
215 uint32
GetKadIndexedKeywords() const;
216 uint32
GetKadIndexedNotes() const;
217 uint32
GetKadIndexedLoad() const;
218 // True IP of machine
219 uint32
GetKadIPAdress() const;
221 uint8
GetBuddyStatus() const;
222 uint32
GetBuddyIP() const;
223 uint32
GetBuddyPort() const;
225 // Check if we should callback this client
226 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
229 void OnlineSig(bool zero
= false);
230 void Localize_mule();
231 void Trigger_New_version(wxString newMule
);
233 // shakraw - new EC code using wxSocketBase
234 ExternalConn
* ECServerHandler
;
236 // return current (valid) public IP or 0 if unknown
237 // If ignorelocal is true, don't use m_localip
238 uint32
GetPublicIP(bool ignorelocal
= false) const;
239 void SetPublicIP(const uint32 dwIP
);
241 uint32
GetED2KID() const;
242 uint32
GetID() const;
244 // Other parts of the interface and such
245 CPreferences
* glob_prefs
;
246 CDownloadQueue
* downloadqueue
;
247 CUploadQueue
* uploadqueue
;
248 CServerConnect
* serverconnect
;
249 CSharedFileList
* sharedfiles
;
250 CServerList
* serverlist
;
251 CListenSocket
* listensocket
;
252 CClientList
* clientlist
;
253 CKnownFileList
* knownfiles
;
254 CCanceledFileList
* canceledfiles
;
255 CSearchList
* searchlist
;
256 CClientCreditsList
* clientcredits
;
257 CFriendList
* friendlist
;
258 CClientUDPSocket
* clientudp
;
259 CStatistics
* m_statistics
;
261 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
262 CAsioService
* m_AsioService
;
264 CUPnPControlPoint
* m_upnp
;
265 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
271 wxString
GetLog(bool reset
= false);
272 wxString
GetServerLog(bool reset
= false);
273 wxString
GetDebugLog(bool reset
= false);
275 bool AddServer(CServer
*srv
, bool fromUser
= false);
276 void AddServerMessageLine(wxString
&msg
);
278 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
280 void SetOSFiles(const wxString
& new_path
);
282 const wxString
& GetOSType() const { return OSType
; }
284 void ShowUserCount();
286 void ShowConnectionState(bool forceUpdate
= false);
291 /** Bootstraps kad from the specified IP (must be in hostorder). */
292 void BootstrapKad(uint32 ip
, uint16 port
);
293 /** Updates the nodes.dat file from the specified url. */
294 void UpdateNotesDat(const wxString
& str
);
297 void DisconnectED2K();
299 bool CryptoAvailable() const;
305 * Handles asserts in a thread-safe manner.
307 virtual void OnAssertFailure(const wxChar
* file
, int line
,
308 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
311 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
312 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
313 void OnServerDnsDone(CMuleInternalEvent
& evt
);
315 void OnTCPTimer(CTimerEvent
& evt
);
316 void OnCoreTimer(CTimerEvent
& evt
);
318 void OnFinishedHashing(CHashingEvent
& evt
);
319 void OnFinishedAICHHashing(CHashingEvent
& evt
);
320 void OnFinishedCompletion(CCompletionEvent
& evt
);
321 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
322 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
323 void OnHashingShutdown(CMuleInternalEvent
&);
324 void OnNotifyEvent(CMuleGUIEvent
& evt
);
326 void SetTimeOnTransfer();
328 APPState m_app_state
;
330 wxString m_emulesig_path
;
331 wxString m_amulesig_path
;
342 virtual void OnUnhandledException();
344 void CheckNewVersion(uint32 result
);
353 class CamuleGuiBase
{
356 virtual ~CamuleGuiBase();
358 wxString m_FrameTitle
;
360 int m_FileDetailDialogActive
;
362 bool CopyTextToClipboard( wxString strText
);
365 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
366 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
368 void AddGuiLogLine(const wxString
& line
);
371 * This list is used to contain log messages that are to be displayed
372 * on the GUI, when it is currently impossible to do so. This is in order
373 * to allows us to queue messages till after the dialog has been created.
375 std::list
<wxString
> m_logLines
;
382 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
385 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
392 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
394 void ShutDown(wxCloseEvent
&evt
);
396 wxString
GetLog(bool reset
= false);
397 wxString
GetServerLog(bool reset
= false);
398 void AddServerMessageLine(wxString
&msg
);
399 DECLARE_EVENT_TABLE()
403 DECLARE_APP(CamuleGuiApp
)
404 extern CamuleGuiApp
*theApp
;
407 #else /* !CLIENT_GUI */
410 #include "amule-remote-gui.h"
416 #define CALL_APP_DATA_LOCK
419 #else /* ! AMULE_DAEMON */
421 // wxWidgets 2.8 requires special code for event handling and sockets.
422 // 2.9 doesn't, so standard event loop and sockets can be used
424 // Windows: aMuled compiles with 2.8 (without the special code),
425 // but works only with 2.9
427 #if !wxCHECK_VERSION(2, 9, 0)
428 // wx 2.8 needs a hand-made event loop in any case
429 #define AMULED28_EVENTLOOP
431 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
434 # include "config.h" // defines ASIO_SOCKETS
438 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
440 #define AMULED28_SOCKETS
445 #ifdef AMULED28_SOCKETS
446 #include <wx/socket.h>
451 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
454 CSocketSet
*m_in_set
, *m_out_set
;
458 CAmuledGSocketFuncTable();
460 void AddSocket(GSocket
*socket
, GSocketEvent event
);
461 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
464 virtual bool OnInit();
465 virtual void OnExit();
466 virtual bool CanUseEventLoop();
467 virtual bool Init_Socket(GSocket
*socket
);
468 virtual void Destroy_Socket(GSocket
*socket
);
469 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
470 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
471 virtual void Enable_Events(GSocket
*socket
);
472 virtual void Disable_Events(GSocket
*socket
);
476 #endif // AMULED28_SOCKETS
478 // no AppTraits used on Windows
481 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
483 #include <wx/apptrait.h>
485 class CDaemonAppTraits
: public wxConsoleAppTraits
488 struct sigaction m_oldSignalChildAction
;
489 struct sigaction m_newSignalChildAction
;
491 #ifdef AMULED28_SOCKETS
492 CAmuledGSocketFuncTable
*m_table
;
494 std::list
<wxObject
*> m_sched_delete
;
496 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
497 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
498 virtual void ScheduleForDestroy(wxObject
*object
);
499 virtual void RemoveFromPendingDelete(wxObject
*object
);
501 void DeletePending();
502 #else // AMULED28_SOCKETS
505 #endif // !AMULED28_SOCKETS
507 virtual int WaitForChild(wxExecuteData
& execData
);
509 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
510 virtual wxStandardPathsBase
& GetStandardPaths();
514 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
515 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
519 class CamuleDaemonApp
: public CamuleApp
522 #ifdef AMULED28_EVENTLOOP
525 #ifdef AMULED28_SOCKETS
526 CAmuledGSocketFuncTable
*m_table
;
532 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
535 struct sigaction m_oldSignalChildAction
;
536 struct sigaction m_newSignalChildAction
;
538 wxAppTraits
*CreateTraits();
543 #ifdef AMULED28_EVENTLOOP
546 void ExitMainLoop() { m_Exit
= true; }
549 bool CopyTextToClipboard(wxString strText
);
551 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
553 DECLARE_EVENT_TABLE()
556 DECLARE_APP(CamuleDaemonApp
)
557 extern CamuleDaemonApp
*theApp
;
559 #endif /* ! AMULE_DAEMON */
562 // File_checked_for_headers