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
);
134 wxString m_configFile
;
138 void AddLinksFromFile();
140 wxString
CreateMagnetLink(const CAbstractFile
*f
);
141 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
144 bool IsDaemon() const { return true; }
146 bool IsDaemon() const { return false; }
150 bool IsRemoteGui() const { return true; }
152 bool IsRemoteGui() const { return false; }
155 const wxString
& GetMuleAppName() const { return m_appName
; }
156 const wxString
GetFullMuleVersion() const;
159 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
163 APP_STATE_RUNNING
= 0,
164 APP_STATE_SHUTTINGDOWN
,
170 virtual ~CamuleApp();
172 virtual bool OnInit();
174 #if wxUSE_ON_FATAL_EXCEPTION
175 void OnFatalException();
177 bool ReinitializeNetwork(wxString
*msg
);
179 // derived classes may override those
180 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
183 void ListenSocketHandler(wxSocketEvent
& event
);
184 void ServerSocketHandler(wxSocketEvent
& event
);
185 void UDPSocketHandler(wxSocketEvent
& event
);
187 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
189 // Barry - To find out if app is running or shutting/shut down
190 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
191 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
193 // Check ED2K and Kademlia state
194 bool IsFirewalled() const;
195 // Are we connected to at least one network?
196 bool IsConnected() const;
197 // Connection to ED2K
198 bool IsConnectedED2K() const;
200 // What about Kad? Is it running?
201 bool IsKadRunning() const;
203 bool IsConnectedKad() const;
204 // Check Kad state (TCP)
205 bool IsFirewalledKad() const;
206 // Check Kad state (UDP)
207 bool IsFirewalledKadUDP() const;
208 // Check Kad state (LAN mode)
209 bool IsKadRunningInLanMode() const;
211 uint32
GetKadUsers() const;
212 uint32
GetKadFiles() const;
213 uint32
GetKadIndexedSources() const;
214 uint32
GetKadIndexedKeywords() const;
215 uint32
GetKadIndexedNotes() const;
216 uint32
GetKadIndexedLoad() const;
217 // True IP of machine
218 uint32
GetKadIPAdress() const;
220 uint8
GetBuddyStatus() const;
221 uint32
GetBuddyIP() const;
222 uint32
GetBuddyPort() const;
224 // Check if we should callback this client
225 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
228 void OnlineSig(bool zero
= false);
229 void Localize_mule();
230 void Trigger_New_version(wxString newMule
);
232 // shakraw - new EC code using wxSocketBase
233 ExternalConn
* ECServerHandler
;
235 // return current (valid) public IP or 0 if unknown
236 // If ignorelocal is true, don't use m_localip
237 uint32
GetPublicIP(bool ignorelocal
= false) const;
238 void SetPublicIP(const uint32 dwIP
);
240 uint32
GetED2KID() const;
241 uint32
GetID() const;
243 // Other parts of the interface and such
244 CPreferences
* glob_prefs
;
245 CDownloadQueue
* downloadqueue
;
246 CUploadQueue
* uploadqueue
;
247 CServerConnect
* serverconnect
;
248 CSharedFileList
* sharedfiles
;
249 CServerList
* serverlist
;
250 CListenSocket
* listensocket
;
251 CClientList
* clientlist
;
252 CKnownFileList
* knownfiles
;
253 CCanceledFileList
* canceledfiles
;
254 CSearchList
* searchlist
;
255 CClientCreditsList
* clientcredits
;
256 CFriendList
* friendlist
;
257 CClientUDPSocket
* clientudp
;
258 CStatistics
* m_statistics
;
260 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
261 CAsioService
* m_AsioService
;
263 CUPnPControlPoint
* m_upnp
;
264 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
270 wxString
GetLog(bool reset
= false);
271 wxString
GetServerLog(bool reset
= false);
272 wxString
GetDebugLog(bool reset
= false);
274 bool AddServer(CServer
*srv
, bool fromUser
= false);
275 void AddServerMessageLine(wxString
&msg
);
277 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
279 void SetOSFiles(const wxString
& new_path
);
281 const wxString
& GetOSType() const { return OSType
; }
283 void ShowUserCount();
285 void ShowConnectionState(bool forceUpdate
= false);
290 /** Bootstraps kad from the specified IP (must be in hostorder). */
291 void BootstrapKad(uint32 ip
, uint16 port
);
292 /** Updates the nodes.dat file from the specified url. */
293 void UpdateNotesDat(const wxString
& str
);
296 void DisconnectED2K();
298 bool CryptoAvailable() const;
304 * Handles asserts in a thread-safe manner.
306 virtual void OnAssertFailure(const wxChar
* file
, int line
,
307 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
310 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
311 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
312 void OnServerDnsDone(CMuleInternalEvent
& evt
);
314 void OnTCPTimer(CTimerEvent
& evt
);
315 void OnCoreTimer(CTimerEvent
& evt
);
317 void OnFinishedHashing(CHashingEvent
& evt
);
318 void OnFinishedAICHHashing(CHashingEvent
& evt
);
319 void OnFinishedCompletion(CCompletionEvent
& evt
);
320 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
321 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
322 void OnHashingShutdown(CMuleInternalEvent
&);
323 void OnNotifyEvent(CMuleGUIEvent
& evt
);
325 void SetTimeOnTransfer();
327 APPState m_app_state
;
329 wxString m_emulesig_path
;
330 wxString m_amulesig_path
;
341 virtual void OnUnhandledException();
343 void CheckNewVersion(uint32 result
);
352 class CamuleGuiBase
{
355 virtual ~CamuleGuiBase();
357 wxString m_FrameTitle
;
359 int m_FileDetailDialogActive
;
361 bool CopyTextToClipboard( wxString strText
);
364 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
365 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
367 void AddGuiLogLine(const wxString
& line
);
370 * This list is used to contain log messages that are to be displayed
371 * on the GUI, when it is currently impossible to do so. This is in order
372 * to allows us to queue messages till after the dialog has been created.
374 std::list
<wxString
> m_logLines
;
381 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
384 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
391 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
393 void ShutDown(wxCloseEvent
&evt
);
395 wxString
GetLog(bool reset
= false);
396 wxString
GetServerLog(bool reset
= false);
397 void AddServerMessageLine(wxString
&msg
);
398 DECLARE_EVENT_TABLE()
402 DECLARE_APP(CamuleGuiApp
)
403 extern CamuleGuiApp
*theApp
;
406 #else /* !CLIENT_GUI */
409 #include "amule-remote-gui.h"
415 #define CALL_APP_DATA_LOCK
418 #else /* ! AMULE_DAEMON */
420 // wxWidgets 2.8 requires special code for event handling and sockets.
421 // 2.9 doesn't, so standard event loop and sockets can be used
423 // Windows: aMuled compiles with 2.8 (without the special code),
424 // but works only with 2.9
426 #if !wxCHECK_VERSION(2, 9, 0)
427 // wx 2.8 needs a hand-made event loop in any case
428 #define AMULED28_EVENTLOOP
430 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
433 # include "config.h" // defines ASIO_SOCKETS
437 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
439 #define AMULED28_SOCKETS
444 #ifdef AMULED28_SOCKETS
445 #include <wx/socket.h>
450 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
453 CSocketSet
*m_in_set
, *m_out_set
;
457 CAmuledGSocketFuncTable();
459 void AddSocket(GSocket
*socket
, GSocketEvent event
);
460 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
463 virtual bool OnInit();
464 virtual void OnExit();
465 virtual bool CanUseEventLoop();
466 virtual bool Init_Socket(GSocket
*socket
);
467 virtual void Destroy_Socket(GSocket
*socket
);
468 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
469 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
470 virtual void Enable_Events(GSocket
*socket
);
471 virtual void Disable_Events(GSocket
*socket
);
475 #endif // AMULED28_SOCKETS
477 // AppTrait functionality is required for 2.8 wx sockets
478 // Otherwise it's used to prevent zombie child processes,
479 // which stops working with wx 2.9.5.
480 // So disable it there (no idea if this has a noticeable impact).
482 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__ )
483 #define AMULED_APPTRAITS
486 #ifdef AMULED_APPTRAITS
488 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
490 #include <wx/apptrait.h>
492 class CDaemonAppTraits
: public wxConsoleAppTraits
495 struct sigaction m_oldSignalChildAction
;
496 struct sigaction m_newSignalChildAction
;
498 #ifdef AMULED28_SOCKETS
499 CAmuledGSocketFuncTable
*m_table
;
501 std::list
<wxObject
*> m_sched_delete
;
503 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
504 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
505 virtual void ScheduleForDestroy(wxObject
*object
);
506 virtual void RemoveFromPendingDelete(wxObject
*object
);
508 void DeletePending();
509 #else // AMULED28_SOCKETS
512 #endif // !AMULED28_SOCKETS
514 virtual int WaitForChild(wxExecuteData
& execData
);
516 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
517 virtual wxStandardPathsBase
& GetStandardPaths();
521 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
522 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
524 #endif // AMULED_APPTRAITS
527 class CamuleDaemonApp
: public CamuleApp
530 #ifdef AMULED28_EVENTLOOP
533 #ifdef AMULED28_SOCKETS
534 CAmuledGSocketFuncTable
*m_table
;
540 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
542 #ifdef AMULED_APPTRAITS
543 struct sigaction m_oldSignalChildAction
;
544 struct sigaction m_newSignalChildAction
;
546 wxAppTraits
*CreateTraits();
547 #endif // AMULED_APPTRAITS
551 #ifdef AMULED28_EVENTLOOP
554 void ExitMainLoop() { m_Exit
= true; }
557 bool CopyTextToClipboard(wxString strText
);
559 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
561 DECLARE_EVENT_TABLE()
564 DECLARE_APP(CamuleDaemonApp
)
565 extern CamuleDaemonApp
*theApp
;
567 #endif /* ! AMULE_DAEMON */
570 // File_checked_for_headers