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>
43 # include "config.h" // Needed for ASIO_SOCKETS
55 class CSharedFileList
;
63 class CCanceledFileList
;
65 class CClientCreditsList
;
67 class CClientUDPSocket
;
69 class UploadBandwidthThrottler
;
76 class CUPnPControlPoint
;
77 class CUPnPPortMapping
;
82 class wxFFileOutputStream
;
85 class wxSingleInstanceChecker
;
87 class CMuleInternalEvent
;
88 class CCompletionEvent
;
89 class CAllocFinishedEvent
;
94 namespace MuleNotify
{
98 using MuleNotify::CMuleGUIEvent
;
107 #define AMULE_APP_BASE wxAppConsole
108 #define CORE_TIMER_PERIOD 300
110 #define AMULE_APP_BASE wxApp
111 #define CORE_TIMER_PERIOD 100
114 #define CONNECTED_ED2K (1<<0)
115 #define CONNECTED_KAD_NOT (1<<1)
116 #define CONNECTED_KAD_OK (1<<2)
117 #define CONNECTED_KAD_FIREWALLED (1<<3)
120 void OnShutdownSignal( int /* sig */ );
123 // Base class common to amule, aamuled and amulegui
124 class CamuleAppCommon
127 // Used to detect a previous running instance of aMule
128 wxSingleInstanceChecker
* m_singleInstance
;
130 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
132 wxString FullMuleVersion
;
133 wxString OSDescription
;
135 bool enable_daemon_fork
;
137 bool m_skipConnectionDialog
;
138 bool m_geometryEnabled
;
139 wxString m_geometryString
;
144 bool InitCommon(int argc
, wxChar
** argv
);
145 void RefreshSingleInstanceChecker();
146 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
148 wxString m_configFile
;
152 void AddLinksFromFile();
154 wxString
CreateMagnetLink(const CAbstractFile
*f
);
155 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
158 bool IsDaemon() const { return true; }
160 bool IsDaemon() const { return false; }
164 bool IsRemoteGui() const { return true; }
166 bool IsRemoteGui() const { return false; }
169 const wxString
& GetMuleAppName() const { return m_appName
; }
170 const wxString
GetFullMuleVersion() const;
173 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
177 APP_STATE_RUNNING
= 0,
178 APP_STATE_SHUTTINGDOWN
,
184 virtual ~CamuleApp();
186 virtual bool OnInit();
188 #if wxUSE_ON_FATAL_EXCEPTION
189 void OnFatalException();
191 bool ReinitializeNetwork(wxString
*msg
);
193 // derived classes may override those
194 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
198 void ListenSocketHandler(wxSocketEvent
& event
);
199 void UDPSocketHandler(wxSocketEvent
& event
);
202 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
204 // Barry - To find out if app is running or shutting/shut down
205 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
206 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
208 // Check ED2K and Kademlia state
209 bool IsFirewalled() const;
210 // Are we connected to at least one network?
211 bool IsConnected() const;
212 // Connection to ED2K
213 bool IsConnectedED2K() const;
215 // What about Kad? Is it running?
216 bool IsKadRunning() const;
218 bool IsConnectedKad() const;
219 // Check Kad state (TCP)
220 bool IsFirewalledKad() const;
221 // Check Kad state (UDP)
222 bool IsFirewalledKadUDP() const;
223 // Check Kad state (LAN mode)
224 bool IsKadRunningInLanMode() const;
226 uint32
GetKadUsers() const;
227 uint32
GetKadFiles() const;
228 uint32
GetKadIndexedSources() const;
229 uint32
GetKadIndexedKeywords() const;
230 uint32
GetKadIndexedNotes() const;
231 uint32
GetKadIndexedLoad() const;
232 // True IP of machine
233 uint32
GetKadIPAdress() const;
235 uint8
GetBuddyStatus() const;
236 uint32
GetBuddyIP() const;
237 uint32
GetBuddyPort() const;
239 const Kademlia::CUInt128
& GetKadID() const;
241 // Check if we should callback this client
242 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
245 void OnlineSig(bool zero
= false);
246 void Localize_mule();
247 void Trigger_New_version(wxString newMule
);
249 // shakraw - new EC code using wxSocketBase
250 ExternalConn
* ECServerHandler
;
252 // return current (valid) public IP or 0 if unknown
253 // If ignorelocal is true, don't use m_localip
254 uint32
GetPublicIP(bool ignorelocal
= false) const;
255 void SetPublicIP(const uint32 dwIP
);
257 uint32
GetED2KID() const;
258 uint32
GetID() const;
260 // Other parts of the interface and such
261 CPreferences
* glob_prefs
;
262 CDownloadQueue
* downloadqueue
;
263 CUploadQueue
* uploadqueue
;
264 CServerConnect
* serverconnect
;
265 CSharedFileList
* sharedfiles
;
266 CServerList
* serverlist
;
267 CListenSocket
* listensocket
;
268 CClientList
* clientlist
;
269 CKnownFileList
* knownfiles
;
270 CCanceledFileList
* canceledfiles
;
271 CSearchList
* searchlist
;
272 CClientCreditsList
* clientcredits
;
273 CFriendList
* friendlist
;
274 CClientUDPSocket
* clientudp
;
275 CStatistics
* m_statistics
;
277 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
279 CAsioService
* m_AsioService
;
282 CUPnPControlPoint
* m_upnp
;
283 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
289 wxString
GetLog(bool reset
= false);
290 wxString
GetServerLog(bool reset
= false);
291 wxString
GetDebugLog(bool reset
= false);
293 bool AddServer(CServer
*srv
, bool fromUser
= false);
294 void AddServerMessageLine(wxString
&msg
);
296 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
298 void SetOSFiles(const wxString
& new_path
);
300 const wxString
& GetOSType() const { return OSType
; }
302 void ShowUserCount();
304 void ShowConnectionState(bool forceUpdate
= false);
309 /** Bootstraps kad from the specified IP (must be in hostorder). */
310 void BootstrapKad(uint32 ip
, uint16 port
);
311 /** Updates the nodes.dat file from the specified url. */
312 void UpdateNotesDat(const wxString
& str
);
315 void DisconnectED2K();
317 bool CryptoAvailable() const;
323 * Handles asserts in a thread-safe manner.
325 virtual void OnAssertFailure(const wxChar
* file
, int line
,
326 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
329 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
330 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
331 void OnServerDnsDone(CMuleInternalEvent
& evt
);
333 void OnTCPTimer(CTimerEvent
& evt
);
334 void OnCoreTimer(CTimerEvent
& evt
);
336 void OnFinishedHashing(CHashingEvent
& evt
);
337 void OnFinishedAICHHashing(CHashingEvent
& evt
);
338 void OnFinishedCompletion(CCompletionEvent
& evt
);
339 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
340 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
341 void OnHashingShutdown(CMuleInternalEvent
&);
342 void OnNotifyEvent(CMuleGUIEvent
& evt
);
344 void SetTimeOnTransfer();
346 APPState m_app_state
;
348 wxString m_emulesig_path
;
349 wxString m_amulesig_path
;
360 virtual void OnUnhandledException();
362 void CheckNewVersion(uint32 result
);
371 class CamuleGuiBase
{
374 virtual ~CamuleGuiBase();
376 wxString m_FrameTitle
;
379 bool CopyTextToClipboard( wxString strText
);
382 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
383 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
385 void AddGuiLogLine(const wxString
& line
);
388 * This list is used to contain log messages that are to be displayed
389 * on the GUI, when it is currently impossible to do so. This is in order
390 * to allows us to queue messages till after the dialog has been created.
392 std::list
<wxString
> m_logLines
;
399 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
402 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
409 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
411 void ShutDown(wxCloseEvent
&evt
);
413 wxString
GetLog(bool reset
= false);
414 wxString
GetServerLog(bool reset
= false);
415 void AddServerMessageLine(wxString
&msg
);
416 DECLARE_EVENT_TABLE()
420 DECLARE_APP(CamuleGuiApp
)
421 extern CamuleGuiApp
*theApp
;
424 #else /* !CLIENT_GUI */
427 #include "amule-remote-gui.h"
433 #define CALL_APP_DATA_LOCK
436 #else /* ! AMULE_DAEMON */
438 // wxWidgets 2.8 requires special code for event handling and sockets.
439 // 2.9 doesn't, so standard event loop and sockets can be used
441 // Windows: aMuled compiles with 2.8 (without the special code),
442 // but works only with 2.9
444 #if !wxCHECK_VERSION(2, 9, 0)
445 // wx 2.8 needs a hand-made event loop in any case
446 #define AMULED28_EVENTLOOP
448 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
451 # include "config.h" // defines ASIO_SOCKETS
455 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
457 #define AMULED28_SOCKETS
462 #ifdef AMULED28_SOCKETS
463 #include <wx/socket.h>
468 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
471 CSocketSet
*m_in_set
, *m_out_set
;
475 CAmuledGSocketFuncTable();
477 void AddSocket(GSocket
*socket
, GSocketEvent event
);
478 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
481 virtual bool OnInit();
482 virtual void OnExit();
483 virtual bool CanUseEventLoop();
484 virtual bool Init_Socket(GSocket
*socket
);
485 virtual void Destroy_Socket(GSocket
*socket
);
486 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
487 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
488 virtual void Enable_Events(GSocket
*socket
);
489 virtual void Disable_Events(GSocket
*socket
);
493 #endif // AMULED28_SOCKETS
495 // AppTrait functionality is required for 2.8 wx sockets
496 // Otherwise it's used to prevent zombie child processes,
497 // which stops working with wx 2.9.5.
498 // So disable it there (no idea if this has a noticeable impact).
500 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
501 #define AMULED_APPTRAITS
504 #ifdef AMULED_APPTRAITS
506 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
508 #include <wx/apptrait.h>
510 class CDaemonAppTraits
: public wxConsoleAppTraits
513 struct sigaction m_oldSignalChildAction
;
514 struct sigaction m_newSignalChildAction
;
516 #ifdef AMULED28_SOCKETS
517 CAmuledGSocketFuncTable
*m_table
;
519 std::list
<wxObject
*> m_sched_delete
;
521 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
522 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
523 virtual void ScheduleForDestroy(wxObject
*object
);
524 virtual void RemoveFromPendingDelete(wxObject
*object
);
526 void DeletePending();
527 #else // AMULED28_SOCKETS
530 #endif // !AMULED28_SOCKETS
532 virtual int WaitForChild(wxExecuteData
& execData
);
534 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
535 virtual wxStandardPathsBase
& GetStandardPaths();
539 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
540 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
542 #endif // AMULED_APPTRAITS
545 class CamuleDaemonApp
: public CamuleApp
548 #ifdef AMULED28_EVENTLOOP
551 #ifdef AMULED28_SOCKETS
552 CAmuledGSocketFuncTable
*m_table
;
558 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
559 // The GTK wxApps sets its file name conversion properly
560 // in wxApp::Initialize(), while wxAppConsole::Initialize()
561 // does not, leaving wxConvFile being set to wxConvLibc. File
562 // name conversion should be set otherwise amuled will abort to
563 // handle non-ASCII file names which monolithic amule can handle.
564 // This function are overrided to perform this.
565 virtual bool Initialize(int& argc_
, wxChar
**argv_
);
567 #ifdef AMULED_APPTRAITS
568 struct sigaction m_oldSignalChildAction
;
569 struct sigaction m_newSignalChildAction
;
571 wxAppTraits
*CreateTraits();
572 #endif // AMULED_APPTRAITS
576 #ifdef AMULED28_EVENTLOOP
579 void ExitMainLoop() { m_Exit
= true; }
582 bool CopyTextToClipboard(wxString strText
);
584 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
586 DECLARE_EVENT_TABLE()
589 DECLARE_APP(CamuleDaemonApp
)
590 extern CamuleDaemonApp
*theApp
;
592 #endif /* ! AMULE_DAEMON */
595 // File_checked_for_headers