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
;
59 class CCanceledFileList
;
61 class CClientCreditsList
;
63 class CClientUDPSocket
;
65 class UploadBandwidthThrottler
;
67 class CUPnPControlPoint
;
68 class CUPnPPortMapping
;
74 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 char *strFullMuleVersion
;
119 char *strOSDescription
;
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 addcryptoptions
= false);
143 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
146 bool IsDaemon() const { return true; }
148 bool IsDaemon() const { return false; }
152 bool IsRemoteGui() const { return true; }
154 bool IsRemoteGui() const { return false; }
157 const wxString
& GetMuleAppName() const { return m_appName
; }
158 const wxString
GetFullMuleVersion() const;
161 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
165 APP_STATE_RUNNING
= 0,
166 APP_STATE_SHUTTINGDOWN
,
172 virtual ~CamuleApp();
174 virtual bool OnInit();
176 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;
209 uint32
GetKadUsers() const;
210 uint32
GetKadFiles() const;
211 uint32
GetKadIndexedSources() const;
212 uint32
GetKadIndexedKeywords() const;
213 uint32
GetKadIndexedNotes() const;
214 uint32
GetKadIndexedLoad() const;
215 // True IP of machine
216 uint32
GetKadIPAdress() const;
218 uint8
GetBuddyStatus() const;
219 uint32
GetBuddyIP() const;
220 uint32
GetBuddyPort() const;
222 // Check if we should callback this client
223 bool CanDoCallback(CUpDownClient
*client
);
226 void OnlineSig(bool zero
= false);
227 void Localize_mule();
228 void Trigger_New_version(wxString newMule
);
230 // shakraw - new EC code using wxSocketBase
231 ExternalConn
* ECServerHandler
;
233 // return current (valid) public IP or 0 if unknown
234 // If ignorelocal is true, don't use m_localip
235 uint32
GetPublicIP(bool ignorelocal
= false) const;
236 void SetPublicIP(const uint32 dwIP
);
238 uint32
GetED2KID() const;
239 uint32
GetID() const;
241 // Other parts of the interface and such
242 CPreferences
* glob_prefs
;
243 CDownloadQueue
* downloadqueue
;
244 CUploadQueue
* uploadqueue
;
245 CServerConnect
* serverconnect
;
246 CSharedFileList
* sharedfiles
;
247 CServerList
* serverlist
;
248 CListenSocket
* listensocket
;
249 CClientList
* clientlist
;
250 CKnownFileList
* knownfiles
;
251 CCanceledFileList
* canceledfiles
;
252 CSearchList
* searchlist
;
253 CClientCreditsList
* clientcredits
;
254 CFriendList
* friendlist
;
255 CClientUDPSocket
* clientudp
;
256 CStatistics
* m_statistics
;
258 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
260 CUPnPControlPoint
* m_upnp
;
261 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
267 wxString
GetLog(bool reset
= false);
268 wxString
GetServerLog(bool reset
= false);
269 wxString
GetDebugLog(bool reset
= false);
271 bool AddServer(CServer
*srv
, bool fromUser
= false);
272 void AddServerMessageLine(wxString
&msg
);
274 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
276 void SetOSFiles(const wxString new_path
);
278 const wxString
& GetOSType() const { return OSType
; }
280 void ShowUserCount();
282 void ShowConnectionState(bool forceUpdate
= false);
287 /** Bootstraps kad from the specified IP (must be in hostorder). */
288 void BootstrapKad(uint32 ip
, uint16 port
);
289 /** Updates the nodes.dat file from the specified url. */
290 void UpdateNotesDat(const wxString
& str
);
293 void DisconnectED2K();
295 bool CryptoAvailable() const;
301 * Handles asserts in a thread-safe manner.
303 virtual void OnAssertFailure(const wxChar
* file
, int line
,
304 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
307 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
308 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
309 void OnServerDnsDone(CMuleInternalEvent
& evt
);
311 void OnTCPTimer(CTimerEvent
& evt
);
312 void OnCoreTimer(CTimerEvent
& evt
);
314 void OnFinishedHashing(CHashingEvent
& evt
);
315 void OnFinishedAICHHashing(CHashingEvent
& evt
);
316 void OnFinishedCompletion(CCompletionEvent
& evt
);
317 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
318 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
319 void OnHashingShutdown(CMuleInternalEvent
&);
320 void OnNotifyEvent(CMuleGUIEvent
& evt
);
322 void SetTimeOnTransfer();
324 APPState m_app_state
;
326 wxString m_emulesig_path
;
327 wxString m_amulesig_path
;
338 virtual void OnUnhandledException();
340 void CheckNewVersion(uint32 result
);
349 class CamuleGuiBase
{
352 virtual ~CamuleGuiBase();
354 wxString m_FrameTitle
;
356 int m_FileDetailDialogActive
;
358 bool CopyTextToClipboard( wxString strText
);
360 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
361 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
363 void AddGuiLogLine(const wxString
& line
);
366 * This list is used to contain log messages that are to be displayed
367 * on the GUI, when it is currently impossible to do so. This is in order
368 * to allows us to queue messages till after the dialog has been created.
370 std::list
<wxString
> m_logLines
;
377 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
380 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
387 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
389 void ShutDown(wxCloseEvent
&evt
);
391 wxString
GetLog(bool reset
= false);
392 wxString
GetServerLog(bool reset
= false);
393 void AddServerMessageLine(wxString
&msg
);
394 DECLARE_EVENT_TABLE()
398 DECLARE_APP(CamuleGuiApp
)
399 extern CamuleGuiApp
*theApp
;
402 #else /* !CLIENT_GUI */
405 #include "amule-remote-gui.h"
411 #define CALL_APP_DATA_LOCK
414 #else /* ! AMULE_DAEMON */
416 // wxWidgets 2.8 requires special code for event handling and sockets.
417 // 2.9 doesn't, so standard event loop and sockets can be used
419 // Windows: aMuled compiles with 2.8 (without the special code),
420 // but works only with 2.9
422 #if !wxCHECK_VERSION(2, 9, 0)
424 // MSW: can't run amuled with 2.8 anyway, just get it compiled
432 #include <wx/socket.h>
437 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
440 CSocketSet
*m_in_set
, *m_out_set
;
444 CAmuledGSocketFuncTable();
446 void AddSocket(GSocket
*socket
, GSocketEvent event
);
447 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
450 virtual bool OnInit();
451 virtual void OnExit();
452 virtual bool CanUseEventLoop();
453 virtual bool Init_Socket(GSocket
*socket
);
454 virtual void Destroy_Socket(GSocket
*socket
);
455 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
456 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
457 virtual void Enable_Events(GSocket
*socket
);
458 virtual void Disable_Events(GSocket
*socket
);
464 // no AppTraits used on Windows
467 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
469 #include <wx/apptrait.h>
471 class CDaemonAppTraits
: public wxConsoleAppTraits
474 struct sigaction m_oldSignalChildAction
;
475 struct sigaction m_newSignalChildAction
;
478 CAmuledGSocketFuncTable
*m_table
;
480 std::list
<wxObject
*> m_sched_delete
;
482 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
483 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
484 virtual void ScheduleForDestroy(wxObject
*object
);
485 virtual void RemoveFromPendingDelete(wxObject
*object
);
487 void DeletePending();
493 virtual int WaitForChild(wxExecuteData
& execData
);
495 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
496 virtual wxStandardPathsBase
& GetStandardPaths();
500 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
501 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
505 class CamuleDaemonApp
: public CamuleApp
510 CAmuledGSocketFuncTable
*m_table
;
516 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
519 struct sigaction m_oldSignalChildAction
;
520 struct sigaction m_newSignalChildAction
;
522 wxAppTraits
*CreateTraits();
530 void ExitMainLoop() { m_Exit
= true; }
534 void ExitMainLoop() {}
537 bool CopyTextToClipboard(wxString strText
);
539 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
541 DECLARE_EVENT_TABLE()
544 DECLARE_APP(CamuleDaemonApp
)
545 extern CamuleDaemonApp
*theApp
;
547 #endif /* ! AMULE_DAEMON */
550 // File_checked_for_headers