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 #if wxUSE_ON_FATAL_EXCEPTION
177 void OnFatalException();
179 bool ReinitializeNetwork(wxString
*msg
);
181 // derived classes may override those
182 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
185 void ListenSocketHandler(wxSocketEvent
& event
);
186 void ServerSocketHandler(wxSocketEvent
& event
);
187 void UDPSocketHandler(wxSocketEvent
& event
);
189 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
191 // Barry - To find out if app is running or shutting/shut down
192 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
193 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
195 // Check ED2K and Kademlia state
196 bool IsFirewalled() const;
197 // Are we connected to at least one network?
198 bool IsConnected() const;
199 // Connection to ED2K
200 bool IsConnectedED2K() const;
202 // What about Kad? Is it running?
203 bool IsKadRunning() const;
205 bool IsConnectedKad() const;
206 // Check Kad state (TCP)
207 bool IsFirewalledKad() const;
208 // Check Kad state (UDP)
209 bool IsFirewalledKadUDP() const;
210 // Check Kad state (LAN mode)
211 bool IsKadRunningInLanMode() const;
213 uint32
GetKadUsers() const;
214 uint32
GetKadFiles() const;
215 uint32
GetKadIndexedSources() const;
216 uint32
GetKadIndexedKeywords() const;
217 uint32
GetKadIndexedNotes() const;
218 uint32
GetKadIndexedLoad() const;
219 // True IP of machine
220 uint32
GetKadIPAdress() const;
222 uint8
GetBuddyStatus() const;
223 uint32
GetBuddyIP() const;
224 uint32
GetBuddyPort() const;
226 // Check if we should callback this client
227 bool CanDoCallback(CUpDownClient
*client
);
230 void OnlineSig(bool zero
= false);
231 void Localize_mule();
232 void Trigger_New_version(wxString newMule
);
234 // shakraw - new EC code using wxSocketBase
235 ExternalConn
* ECServerHandler
;
237 // return current (valid) public IP or 0 if unknown
238 // If ignorelocal is true, don't use m_localip
239 uint32
GetPublicIP(bool ignorelocal
= false) const;
240 void SetPublicIP(const uint32 dwIP
);
242 uint32
GetED2KID() const;
243 uint32
GetID() const;
245 // Other parts of the interface and such
246 CPreferences
* glob_prefs
;
247 CDownloadQueue
* downloadqueue
;
248 CUploadQueue
* uploadqueue
;
249 CServerConnect
* serverconnect
;
250 CSharedFileList
* sharedfiles
;
251 CServerList
* serverlist
;
252 CListenSocket
* listensocket
;
253 CClientList
* clientlist
;
254 CKnownFileList
* knownfiles
;
255 CCanceledFileList
* canceledfiles
;
256 CSearchList
* searchlist
;
257 CClientCreditsList
* clientcredits
;
258 CFriendList
* friendlist
;
259 CClientUDPSocket
* clientudp
;
260 CStatistics
* m_statistics
;
262 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
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
);
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)
428 // MSW: can't run amuled with 2.8 anyway, just get it compiled
436 #include <wx/socket.h>
441 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
444 CSocketSet
*m_in_set
, *m_out_set
;
448 CAmuledGSocketFuncTable();
450 void AddSocket(GSocket
*socket
, GSocketEvent event
);
451 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
454 virtual bool OnInit();
455 virtual void OnExit();
456 virtual bool CanUseEventLoop();
457 virtual bool Init_Socket(GSocket
*socket
);
458 virtual void Destroy_Socket(GSocket
*socket
);
459 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
460 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
461 virtual void Enable_Events(GSocket
*socket
);
462 virtual void Disable_Events(GSocket
*socket
);
468 // no AppTraits used on Windows
471 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
473 #include <wx/apptrait.h>
475 class CDaemonAppTraits
: public wxConsoleAppTraits
478 struct sigaction m_oldSignalChildAction
;
479 struct sigaction m_newSignalChildAction
;
482 CAmuledGSocketFuncTable
*m_table
;
484 std::list
<wxObject
*> m_sched_delete
;
486 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
487 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
488 virtual void ScheduleForDestroy(wxObject
*object
);
489 virtual void RemoveFromPendingDelete(wxObject
*object
);
491 void DeletePending();
497 virtual int WaitForChild(wxExecuteData
& execData
);
499 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
500 virtual wxStandardPathsBase
& GetStandardPaths();
504 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
505 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
509 class CamuleDaemonApp
: public CamuleApp
514 CAmuledGSocketFuncTable
*m_table
;
520 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
523 struct sigaction m_oldSignalChildAction
;
524 struct sigaction m_newSignalChildAction
;
526 wxAppTraits
*CreateTraits();
534 void ExitMainLoop() { m_Exit
= true; }
538 void ExitMainLoop() {}
541 bool CopyTextToClipboard(wxString strText
);
543 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
545 DECLARE_EVENT_TABLE()
548 DECLARE_APP(CamuleDaemonApp
)
549 extern CamuleDaemonApp
*theApp
;
551 #endif /* ! AMULE_DAEMON */
554 // File_checked_for_headers