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() { return true; }
148 bool IsDaemon() { return false; }
152 bool IsRemoteGui() { return true; }
154 bool IsRemoteGui() { return false; }
157 const wxString
& GetMuleAppName() { return m_appName
; }
162 * Returns a description of the version of aMule being used.
164 * @return A detailed description of the aMule version, including application
165 * name and wx information.
167 * This should become a CamuleAppCommon member once certain files get unlocked.
169 #define GetFullMuleVersion() (theApp->GetMuleAppName() + wxT(" ") + GetMuleVersion())
171 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
175 APP_STATE_RUNNING
= 0,
176 APP_STATE_SHUTTINGDOWN
,
182 virtual ~CamuleApp();
184 virtual bool OnInit();
186 void OnFatalException();
187 bool ReinitializeNetwork(wxString
*msg
);
189 // derived classes may override those
190 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
193 void ListenSocketHandler(wxSocketEvent
& event
);
194 void ServerSocketHandler(wxSocketEvent
& event
);
195 void UDPSocketHandler(wxSocketEvent
& event
);
197 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
199 // Barry - To find out if app is running or shutting/shut down
200 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
201 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
203 // Check ED2K and Kademlia state
204 bool IsFirewalled() const;
205 // Are we connected to at least one network?
206 bool IsConnected() const;
207 // Connection to ED2K
208 bool IsConnectedED2K() const;
210 // What about Kad? Is it running?
211 bool IsKadRunning() const;
213 bool IsConnectedKad() const;
214 // Check Kad state (TCP)
215 bool IsFirewalledKad() const;
216 // Check Kad state (UDP)
217 bool IsFirewalledKadUDP() const;
219 uint32
GetKadUsers() const;
220 uint32
GetKadFiles() const;
221 uint32
GetKadIndexedSources() const;
222 uint32
GetKadIndexedKeywords() const;
223 uint32
GetKadIndexedNotes() const;
224 uint32
GetKadIndexedLoad() const;
225 // True IP of machine
226 uint32
GetKadIPAdress() const;
228 uint8
GetBuddyStatus() const;
229 uint32
GetBuddyIP() const;
230 uint32
GetBuddyPort() const;
232 // Check if we should callback this client
233 bool CanDoCallback(CUpDownClient
*client
);
236 void OnlineSig(bool zero
= false);
237 void Localize_mule();
238 void Trigger_New_version(wxString newMule
);
240 // shakraw - new EC code using wxSocketBase
241 ExternalConn
* ECServerHandler
;
243 // return current (valid) public IP or 0 if unknown
244 // If ignorelocal is true, don't use m_localip
245 uint32
GetPublicIP(bool ignorelocal
= false) const;
246 void SetPublicIP(const uint32 dwIP
);
248 uint32
GetED2KID() const;
249 uint32
GetID() const;
251 // Other parts of the interface and such
252 CPreferences
* glob_prefs
;
253 CDownloadQueue
* downloadqueue
;
254 CUploadQueue
* uploadqueue
;
255 CServerConnect
* serverconnect
;
256 CSharedFileList
* sharedfiles
;
257 CServerList
* serverlist
;
258 CListenSocket
* listensocket
;
259 CClientList
* clientlist
;
260 CKnownFileList
* knownfiles
;
261 CCanceledFileList
* canceledfiles
;
262 CSearchList
* searchlist
;
263 CClientCreditsList
* clientcredits
;
264 CFriendList
* friendlist
;
265 CClientUDPSocket
* clientudp
;
266 CStatistics
* m_statistics
;
268 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
270 CUPnPControlPoint
* m_upnp
;
271 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
277 wxString
GetLog(bool reset
= false);
278 wxString
GetServerLog(bool reset
= false);
279 wxString
GetDebugLog(bool reset
= false);
281 bool AddServer(CServer
*srv
, bool fromUser
= false);
282 void AddServerMessageLine(wxString
&msg
);
284 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
286 void SetOSFiles(const wxString new_path
);
288 const wxString
& GetOSType() const { return OSType
; }
290 void ShowUserCount();
292 void ShowConnectionState();
297 /** Bootstraps kad from the specified IP (must be in hostorder). */
298 void BootstrapKad(uint32 ip
, uint16 port
);
299 /** Updates the nodes.dat file from the specified url. */
300 void UpdateNotesDat(const wxString
& str
);
303 void DisconnectED2K();
305 bool CryptoAvailable() const;
311 * Handles asserts in a thread-safe manner.
313 virtual void OnAssertFailure(const wxChar
* file
, int line
,
314 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
317 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
318 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
319 void OnServerDnsDone(CMuleInternalEvent
& evt
);
321 void OnTCPTimer(CTimerEvent
& evt
);
322 void OnCoreTimer(CTimerEvent
& evt
);
324 void OnFinishedHashing(CHashingEvent
& evt
);
325 void OnFinishedAICHHashing(CHashingEvent
& evt
);
326 void OnFinishedCompletion(CCompletionEvent
& evt
);
327 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
328 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
329 void OnHashingShutdown(CMuleInternalEvent
&);
330 void OnNotifyEvent(CMuleGUIEvent
& evt
);
332 void SetTimeOnTransfer();
334 APPState m_app_state
;
336 wxString m_emulesig_path
;
337 wxString m_amulesig_path
;
348 virtual void OnUnhandledException();
350 void CheckNewVersion(uint32 result
);
359 class CamuleGuiBase
{
362 virtual ~CamuleGuiBase();
364 wxString m_FrameTitle
;
366 int m_FileDetailDialogActive
;
368 bool CopyTextToClipboard( wxString strText
);
370 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
371 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
373 void AddGuiLogLine(const wxString
& line
);
376 * This list is used to contain log messages that are to be displayed
377 * on the GUI, when it is currently impossible to do so. This is in order
378 * to allows us to queue messages till after the dialog has been created.
380 std::list
<wxString
> m_logLines
;
387 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
390 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
397 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
399 void ShutDown(wxCloseEvent
&evt
);
401 wxString
GetLog(bool reset
= false);
402 wxString
GetServerLog(bool reset
= false);
403 void AddServerMessageLine(wxString
&msg
);
404 DECLARE_EVENT_TABLE()
408 DECLARE_APP(CamuleGuiApp
)
409 extern CamuleGuiApp
*theApp
;
412 #else /* !CLIENT_GUI */
415 #include "amule-remote-gui.h"
421 #define CALL_APP_DATA_LOCK
424 #else /* ! AMULE_DAEMON */
426 // wxWidgets 2.8 requires special code for event handling and sockets.
427 // 2.9 doesn't, so standard event loop and sockets can be used
429 // Windows: aMuled compiles with 2.8 (without the special code),
430 // but works only with 2.9
432 #if !wxCHECK_VERSION(2, 9, 0)
434 // MSW: can't run amuled with 2.8 anyway, just get it compiled
442 #include <wx/socket.h>
447 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
450 CSocketSet
*m_in_set
, *m_out_set
;
454 CAmuledGSocketFuncTable();
456 void AddSocket(GSocket
*socket
, GSocketEvent event
);
457 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
460 virtual bool OnInit();
461 virtual void OnExit();
462 virtual bool CanUseEventLoop();
463 virtual bool Init_Socket(GSocket
*socket
);
464 virtual void Destroy_Socket(GSocket
*socket
);
465 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
466 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
467 virtual void Enable_Events(GSocket
*socket
);
468 virtual void Disable_Events(GSocket
*socket
);
474 // no AppTraits used on Windows
477 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
479 #include <wx/apptrait.h>
481 class CDaemonAppTraits
: public wxConsoleAppTraits
484 struct sigaction m_oldSignalChildAction
;
485 struct sigaction m_newSignalChildAction
;
488 CAmuledGSocketFuncTable
*m_table
;
490 std::list
<wxObject
*> m_sched_delete
;
492 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
493 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
494 virtual void ScheduleForDestroy(wxObject
*object
);
495 virtual void RemoveFromPendingDelete(wxObject
*object
);
497 void DeletePending();
503 virtual int WaitForChild(wxExecuteData
& execData
);
505 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
506 virtual wxStandardPathsBase
& GetStandardPaths();
510 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
511 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
515 class CamuleDaemonApp
: public CamuleApp
520 CAmuledGSocketFuncTable
*m_table
;
526 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
529 struct sigaction m_oldSignalChildAction
;
530 struct sigaction m_newSignalChildAction
;
532 wxAppTraits
*CreateTraits();
540 void ExitMainLoop() { m_Exit
= true; }
544 void ExitMainLoop() {}
547 bool CopyTextToClipboard(wxString strText
);
549 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
551 DECLARE_EVENT_TABLE()
554 DECLARE_APP(CamuleDaemonApp
)
555 extern CamuleDaemonApp
*theApp
;
557 #endif /* ! AMULE_DAEMON */
560 // File_checked_for_headers