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
41 #include "config.h" // Needed for ASIO_SOCKETS
51 class CSharedFileList
;
59 class CCanceledFileList
;
61 class CClientCreditsList
;
63 class CClientUDPSocket
;
65 class UploadBandwidthThrottler
;
72 class CUPnPControlPoint
;
73 class CUPnPPortMapping
;
78 class wxFFileOutputStream
;
81 class wxSingleInstanceChecker
;
83 class CMuleInternalEvent
;
84 class CCompletionEvent
;
85 class CAllocFinishedEvent
;
90 namespace MuleNotify
{
94 using MuleNotify::CMuleGUIEvent
;
103 #define AMULE_APP_BASE wxAppConsole
104 #define CORE_TIMER_PERIOD 300
106 #define AMULE_APP_BASE wxApp
107 #define CORE_TIMER_PERIOD 100
110 #define CONNECTED_ED2K (1<<0)
111 #define CONNECTED_KAD_NOT (1<<1)
112 #define CONNECTED_KAD_OK (1<<2)
113 #define CONNECTED_KAD_FIREWALLED (1<<3)
116 void OnShutdownSignal( int /* sig */ );
119 // Base class common to amule, aamuled and amulegui
120 class CamuleAppCommon
123 // Used to detect a previous running instance of aMule
124 wxSingleInstanceChecker
* m_singleInstance
;
126 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
128 wxString FullMuleVersion
;
129 wxString OSDescription
;
131 bool enable_daemon_fork
;
133 bool m_skipConnectionDialog
;
134 bool m_geometryEnabled
;
135 wxString m_geometryString
;
140 bool InitCommon(int argc
, wxChar
** argv
);
141 void RefreshSingleInstanceChecker();
142 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
144 wxString m_configFile
;
148 void AddLinksFromFile();
150 wxString
CreateMagnetLink(const CAbstractFile
*f
);
151 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
154 bool IsDaemon() const { return true; }
156 bool IsDaemon() const { return false; }
160 bool IsRemoteGui() const { return true; }
162 bool IsRemoteGui() const { return false; }
165 const wxString
& GetMuleAppName() const { return m_appName
; }
166 const wxString
GetFullMuleVersion() const;
169 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
173 APP_STATE_RUNNING
= 0,
174 APP_STATE_SHUTTINGDOWN
,
180 virtual ~CamuleApp();
182 virtual bool OnInit();
184 #if wxUSE_ON_FATAL_EXCEPTION
185 void OnFatalException();
187 bool ReinitializeNetwork(wxString
*msg
);
189 // derived classes may override those
190 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
194 void ListenSocketHandler(wxSocketEvent
& event
);
195 void UDPSocketHandler(wxSocketEvent
& event
);
198 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
200 // Barry - To find out if app is running or shutting/shut down
201 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
202 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
204 // Check ED2K and Kademlia state
205 bool IsFirewalled() const;
206 // Are we connected to at least one network?
207 bool IsConnected() const;
208 // Connection to ED2K
209 bool IsConnectedED2K() const;
211 // What about Kad? Is it running?
212 bool IsKadRunning() const;
214 bool IsConnectedKad() const;
215 // Check Kad state (TCP)
216 bool IsFirewalledKad() const;
217 // Check Kad state (UDP)
218 bool IsFirewalledKadUDP() const;
219 // Check Kad state (LAN mode)
220 bool IsKadRunningInLanMode() const;
222 uint32
GetKadUsers() const;
223 uint32
GetKadFiles() const;
224 uint32
GetKadIndexedSources() const;
225 uint32
GetKadIndexedKeywords() const;
226 uint32
GetKadIndexedNotes() const;
227 uint32
GetKadIndexedLoad() const;
228 // True IP of machine
229 uint32
GetKadIPAdress() const;
231 uint8
GetBuddyStatus() const;
232 uint32
GetBuddyIP() const;
233 uint32
GetBuddyPort() const;
235 const Kademlia::CUInt128
& GetKadID() const;
237 // Check if we should callback this client
238 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
241 void OnlineSig(bool zero
= false);
242 void Localize_mule();
243 void Trigger_New_version(wxString newMule
);
245 // shakraw - new EC code using wxSocketBase
246 ExternalConn
* ECServerHandler
;
248 // return current (valid) public IP or 0 if unknown
249 // If ignorelocal is true, don't use m_localip
250 uint32
GetPublicIP(bool ignorelocal
= false) const;
251 void SetPublicIP(const uint32 dwIP
);
253 uint32
GetED2KID() const;
254 uint32
GetID() const;
256 // Other parts of the interface and such
257 CPreferences
* glob_prefs
;
258 CDownloadQueue
* downloadqueue
;
259 CUploadQueue
* uploadqueue
;
260 CServerConnect
* serverconnect
;
261 CSharedFileList
* sharedfiles
;
262 CServerList
* serverlist
;
263 CListenSocket
* listensocket
;
264 CClientList
* clientlist
;
265 CKnownFileList
* knownfiles
;
266 CCanceledFileList
* canceledfiles
;
267 CSearchList
* searchlist
;
268 CClientCreditsList
* clientcredits
;
269 CFriendList
* friendlist
;
270 CClientUDPSocket
* clientudp
;
271 CStatistics
* m_statistics
;
273 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
275 CAsioService
* m_AsioService
;
278 CUPnPControlPoint
* m_upnp
;
279 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
285 wxString
GetLog(bool reset
= false);
286 wxString
GetServerLog(bool reset
= false);
287 wxString
GetDebugLog(bool reset
= false);
289 bool AddServer(CServer
*srv
, bool fromUser
= false);
290 void AddServerMessageLine(wxString
&msg
);
292 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
294 void SetOSFiles(const wxString
& new_path
);
296 const wxString
& GetOSType() const { return OSType
; }
298 void ShowUserCount();
300 void ShowConnectionState(bool forceUpdate
= false);
305 /** Bootstraps kad from the specified IP (must be in hostorder). */
306 void BootstrapKad(uint32 ip
, uint16 port
);
307 /** Updates the nodes.dat file from the specified url. */
308 void UpdateNotesDat(const wxString
& str
);
311 void DisconnectED2K();
313 bool CryptoAvailable() const;
319 * Handles asserts in a thread-safe manner.
321 virtual void OnAssertFailure(const wxChar
* file
, int line
,
322 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
325 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
326 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
327 void OnServerDnsDone(CMuleInternalEvent
& evt
);
329 void OnTCPTimer(CTimerEvent
& evt
);
330 void OnCoreTimer(CTimerEvent
& evt
);
332 void OnFinishedHashing(CHashingEvent
& evt
);
333 void OnFinishedAICHHashing(CHashingEvent
& evt
);
334 void OnFinishedCompletion(CCompletionEvent
& evt
);
335 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
336 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
337 void OnHashingShutdown(CMuleInternalEvent
&);
338 void OnNotifyEvent(CMuleGUIEvent
& evt
);
340 void SetTimeOnTransfer();
342 APPState m_app_state
;
344 wxString m_emulesig_path
;
345 wxString m_amulesig_path
;
356 virtual void OnUnhandledException();
358 void CheckNewVersion(uint32 result
);
367 class CamuleGuiBase
{
370 virtual ~CamuleGuiBase();
372 wxString m_FrameTitle
;
375 bool CopyTextToClipboard( wxString strText
);
378 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
379 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
381 void AddGuiLogLine(const wxString
& line
);
384 * This list is used to contain log messages that are to be displayed
385 * on the GUI, when it is currently impossible to do so. This is in order
386 * to allows us to queue messages till after the dialog has been created.
388 std::list
<wxString
> m_logLines
;
395 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
398 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
405 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
407 void ShutDown(wxCloseEvent
&evt
);
409 wxString
GetLog(bool reset
= false);
410 wxString
GetServerLog(bool reset
= false);
411 void AddServerMessageLine(wxString
&msg
);
412 DECLARE_EVENT_TABLE()
416 DECLARE_APP(CamuleGuiApp
)
417 extern CamuleGuiApp
*theApp
;
420 #else /* !CLIENT_GUI */
423 #include "amule-remote-gui.h"
429 #define CALL_APP_DATA_LOCK
432 #else /* ! AMULE_DAEMON */
434 // wxWidgets 2.8 requires special code for event handling and sockets.
435 // 2.9 doesn't, so standard event loop and sockets can be used
437 // Windows: aMuled compiles with 2.8 (without the special code),
438 // but works only with 2.9
440 #if !wxCHECK_VERSION(2, 9, 0)
441 // wx 2.8 needs a hand-made event loop in any case
442 #define AMULED28_EVENTLOOP
445 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
447 #define AMULED28_SOCKETS
452 #ifdef AMULED28_SOCKETS
453 #include <wx/socket.h>
458 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
461 CSocketSet
*m_in_set
, *m_out_set
;
465 CAmuledGSocketFuncTable();
467 void AddSocket(GSocket
*socket
, GSocketEvent event
);
468 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
471 virtual bool OnInit();
472 virtual void OnExit();
473 virtual bool CanUseEventLoop();
474 virtual bool Init_Socket(GSocket
*socket
);
475 virtual void Destroy_Socket(GSocket
*socket
);
476 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
477 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
478 virtual void Enable_Events(GSocket
*socket
);
479 virtual void Disable_Events(GSocket
*socket
);
483 #endif // AMULED28_SOCKETS
485 // AppTrait functionality is required for 2.8 wx sockets
486 // Otherwise it's used to prevent zombie child processes,
487 // which stops working with wx 2.9.5.
488 // So disable it there (no idea if this has a noticeable impact).
490 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
491 #define AMULED_APPTRAITS
494 #ifdef AMULED_APPTRAITS
496 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
498 #include <wx/apptrait.h>
500 class CDaemonAppTraits
: public wxConsoleAppTraits
503 struct sigaction m_oldSignalChildAction
;
504 struct sigaction m_newSignalChildAction
;
506 #ifdef AMULED28_SOCKETS
507 CAmuledGSocketFuncTable
*m_table
;
509 std::list
<wxObject
*> m_sched_delete
;
511 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
512 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
513 virtual void ScheduleForDestroy(wxObject
*object
);
514 virtual void RemoveFromPendingDelete(wxObject
*object
);
516 void DeletePending();
517 #else // AMULED28_SOCKETS
520 #endif // !AMULED28_SOCKETS
522 virtual int WaitForChild(wxExecuteData
& execData
);
524 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
525 virtual wxStandardPathsBase
& GetStandardPaths();
529 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
530 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
532 #endif // AMULED_APPTRAITS
535 class CamuleDaemonApp
: public CamuleApp
538 #ifdef AMULED28_EVENTLOOP
541 #ifdef AMULED28_SOCKETS
542 CAmuledGSocketFuncTable
*m_table
;
548 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
549 // The GTK wxApps sets its file name conversion properly
550 // in wxApp::Initialize(), while wxAppConsole::Initialize()
551 // does not, leaving wxConvFile being set to wxConvLibc. File
552 // name conversion should be set otherwise amuled will abort to
553 // handle non-ASCII file names which monolithic amule can handle.
554 // This function are overrided to perform this.
555 virtual bool Initialize(int& argc_
, wxChar
**argv_
);
557 #ifdef AMULED_APPTRAITS
558 struct sigaction m_oldSignalChildAction
;
559 struct sigaction m_newSignalChildAction
;
561 wxAppTraits
*CreateTraits();
562 #endif // AMULED_APPTRAITS
566 #ifdef AMULED28_EVENTLOOP
569 void ExitMainLoop() { m_Exit
= true; }
572 bool CopyTextToClipboard(wxString strText
);
574 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
576 DECLARE_EVENT_TABLE()
579 DECLARE_APP(CamuleDaemonApp
)
580 extern CamuleDaemonApp
*theApp
;
582 #endif /* ! AMULE_DAEMON */
585 // File_checked_for_headers