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
;
60 class CClientCreditsList
;
62 class CClientUDPSocket
;
64 class UploadBandwidthThrottler
;
66 class CUPnPControlPoint
;
67 class CUPnPPortMapping
;
73 class wxFFileOutputStream
;
77 class wxSingleInstanceChecker
;
79 class CMuleInternalEvent
;
80 class CCompletionEvent
;
81 class CAllocFinishedEvent
;
86 namespace MuleNotify
{
91 using MuleNotify::CMuleGUIEvent
;
95 #define AMULE_APP_BASE wxAppConsole
96 #define CORE_TIMER_PERIOD 300
98 #define AMULE_APP_BASE wxApp
99 #define CORE_TIMER_PERIOD 100
102 #define CONNECTED_ED2K (1<<0)
103 #define CONNECTED_KAD_NOT (1<<1)
104 #define CONNECTED_KAD_OK (1<<2)
105 #define CONNECTED_KAD_FIREWALLED (1<<3)
108 // Base class common to amule, aamuled and amulegui
109 class CamuleAppCommon
112 // Used to detect a previous running instance of aMule
113 wxSingleInstanceChecker
* m_singleInstance
;
115 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
117 char *strFullMuleVersion
;
118 char *strOSDescription
;
120 bool enable_daemon_fork
;
122 bool m_skipConnectionDialog
;
123 bool m_geometryEnabled
;
124 wxString m_geometryString
;
129 bool InitCommon(int argc
, wxChar
** argv
);
130 void RefreshSingleInstanceChecker();
131 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
134 wxString m_configFile
;
138 void AddLinksFromFile();
140 wxString
CreateMagnetLink(const CAbstractFile
*f
);
141 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool addcryptoptions
= false);
142 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
145 bool IsDaemon() { return true; }
147 bool IsDaemon() { return false; }
151 bool IsRemoteGui() { return true; }
153 bool IsRemoteGui() { return false; }
156 const wxString
& GetMuleAppName() { return m_appName
; }
161 * Returns a description of the version of aMule being used.
163 * @return A detailed description of the aMule version, including application
164 * name and wx information.
166 * This should become a CamuleAppCommon member once certain files get unlocked.
168 #define GetFullMuleVersion() (theApp->GetMuleAppName() + wxT(" ") + GetMuleVersion())
170 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
174 APP_STATE_RUNNING
= 0,
175 APP_STATE_SHUTTINGDOWN
,
181 virtual ~CamuleApp();
183 virtual bool OnInit();
185 void OnFatalException();
186 bool ReinitializeNetwork(wxString
*msg
);
188 // derived classes may override those
189 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
192 void ListenSocketHandler(wxSocketEvent
& event
);
193 void ServerSocketHandler(wxSocketEvent
& event
);
194 void UDPSocketHandler(wxSocketEvent
& event
);
196 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
198 // Barry - To find out if app is running or shutting/shut down
199 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
200 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
202 // Check ED2K and Kademlia state
203 bool IsFirewalled() const;
204 // Are we connected to at least one network?
205 bool IsConnected() const;
206 // Connection to ED2K
207 bool IsConnectedED2K() const;
209 // What about Kad? Is it running?
210 bool IsKadRunning() const;
212 bool IsConnectedKad() const;
213 // Check Kad state (TCP)
214 bool IsFirewalledKad() const;
215 // Check Kad state (UDP)
216 bool IsFirewalledKadUDP() const;
218 uint32
GetKadUsers() const;
219 uint32
GetKadFiles() const;
220 uint32
GetKadIndexedSources() const;
221 uint32
GetKadIndexedKeywords() const;
222 uint32
GetKadIndexedNotes() const;
223 uint32
GetKadIndexedLoad() const;
224 // True IP of machine
225 uint32
GetKadIPAdress() const;
227 uint8
GetBuddyStatus() const;
228 uint32
GetBuddyIP() const;
229 uint32
GetBuddyPort() const;
231 // Check if we should callback this client
232 bool CanDoCallback(CUpDownClient
*client
);
235 void OnlineSig(bool zero
= false);
236 void Localize_mule();
237 void Trigger_New_version(wxString newMule
);
239 // shakraw - new EC code using wxSocketBase
240 ExternalConn
* ECServerHandler
;
242 // return current (valid) public IP or 0 if unknown
243 // If ignorelocal is true, don't use m_localip
244 uint32
GetPublicIP(bool ignorelocal
= false) const;
245 void SetPublicIP(const uint32 dwIP
);
247 uint32
GetED2KID() const;
248 uint32
GetID() const;
250 // Other parts of the interface and such
251 CPreferences
* glob_prefs
;
252 CDownloadQueue
* downloadqueue
;
253 CUploadQueue
* uploadqueue
;
254 CServerConnect
* serverconnect
;
255 CSharedFileList
* sharedfiles
;
256 CServerList
* serverlist
;
257 CListenSocket
* listensocket
;
258 CClientList
* clientlist
;
259 CKnownFileList
* knownfiles
;
260 CSearchList
* searchlist
;
261 CClientCreditsList
* clientcredits
;
262 CFriendList
* friendlist
;
263 CClientUDPSocket
* clientudp
;
264 CStatistics
* m_statistics
;
266 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
268 CUPnPControlPoint
* m_upnp
;
269 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
275 wxString
GetLog(bool reset
= false);
276 wxString
GetServerLog(bool reset
= false);
277 wxString
GetDebugLog(bool reset
= false);
279 bool AddServer(CServer
*srv
, bool fromUser
= false);
280 void AddServerMessageLine(wxString
&msg
);
282 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
284 void SetOSFiles(const wxString new_path
);
286 const wxString
& GetOSType() const { return OSType
; }
288 void ShowUserCount();
290 void ShowConnectionState();
295 /** Bootstraps kad from the specified IP (must be in hostorder). */
296 void BootstrapKad(uint32 ip
, uint16 port
);
297 /** Updates the nodes.dat file from the specified url. */
298 void UpdateNotesDat(const wxString
& str
);
301 void DisconnectED2K();
303 bool CryptoAvailable() const;
309 * Handles asserts in a thread-safe manner.
311 virtual void OnAssertFailure(const wxChar
* file
, int line
,
312 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
315 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
316 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
317 void OnServerDnsDone(CMuleInternalEvent
& evt
);
319 void OnTCPTimer(CTimerEvent
& evt
);
320 void OnCoreTimer(CTimerEvent
& evt
);
322 void OnFinishedHashing(CHashingEvent
& evt
);
323 void OnFinishedAICHHashing(CHashingEvent
& evt
);
324 void OnFinishedCompletion(CCompletionEvent
& evt
);
325 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
326 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
327 void OnHashingShutdown(CMuleInternalEvent
&);
328 void OnNotifyEvent(CMuleGUIEvent
& evt
);
330 void SetTimeOnTransfer();
332 APPState m_app_state
;
334 wxString m_emulesig_path
;
335 wxString m_amulesig_path
;
346 virtual void OnUnhandledException();
348 void CheckNewVersion(uint32 result
);
357 class CamuleGuiBase
{
360 virtual ~CamuleGuiBase();
362 wxString m_FrameTitle
;
364 int m_FileDetailDialogActive
;
366 bool CopyTextToClipboard( wxString strText
);
368 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
369 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
371 void AddGuiLogLine(const wxString
& line
);
374 * This list is used to contain log messages that are to be displayed
375 * on the GUI, when it is currently impossible to do so. This is in order
376 * to allows us to queue messages till after the dialog has been created.
378 std::list
<wxString
> m_logLines
;
385 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
388 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
395 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
397 void ShutDown(wxCloseEvent
&evt
);
399 wxString
GetLog(bool reset
= false);
400 wxString
GetServerLog(bool reset
= false);
401 void AddServerMessageLine(wxString
&msg
);
402 DECLARE_EVENT_TABLE()
406 DECLARE_APP(CamuleGuiApp
)
408 CamuleGuiApp
*theApp
;
410 extern CamuleGuiApp
*theApp
;
414 #else /* !CLIENT_GUI */
417 #include "amule-remote-gui.h"
423 #define CALL_APP_DATA_LOCK
426 #else /* ! AMULE_DAEMON */
428 // wxWidgets 2.8 requires special code for event handling and sockets.
429 // 2.9 doesn't, so standard event loop and sockets can be used
431 // Windows: aMuled compiles with 2.8 (without the special code),
432 // but works only with 2.9
434 #if !wxCHECK_VERSION(2, 9, 0)
436 // MSW: can't run amuled with 2.8 anyway, just get it compiled
444 #include <wx/socket.h>
449 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
452 CSocketSet
*m_in_set
, *m_out_set
;
456 CAmuledGSocketFuncTable();
458 void AddSocket(GSocket
*socket
, GSocketEvent event
);
459 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
462 virtual bool OnInit();
463 virtual void OnExit();
464 virtual bool CanUseEventLoop();
465 virtual bool Init_Socket(GSocket
*socket
);
466 virtual void Destroy_Socket(GSocket
*socket
);
467 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
468 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
469 virtual void Enable_Events(GSocket
*socket
);
470 virtual void Disable_Events(GSocket
*socket
);
476 // no AppTraits used on Windows
479 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
481 #include <wx/apptrait.h>
483 class CDaemonAppTraits
: public wxConsoleAppTraits
486 struct sigaction m_oldSignalChildAction
;
487 struct sigaction m_newSignalChildAction
;
490 CAmuledGSocketFuncTable
*m_table
;
492 std::list
<wxObject
*> m_sched_delete
;
494 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
495 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
496 virtual void ScheduleForDestroy(wxObject
*object
);
497 virtual void RemoveFromPendingDelete(wxObject
*object
);
499 void DeletePending();
505 virtual int WaitForChild(wxExecuteData
& execData
);
507 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
508 virtual wxStandardPathsBase
& GetStandardPaths();
512 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
513 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
517 class CamuleDaemonApp
: public CamuleApp
522 CAmuledGSocketFuncTable
*m_table
;
528 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
531 struct sigaction m_oldSignalChildAction
;
532 struct sigaction m_newSignalChildAction
;
534 wxAppTraits
*CreateTraits();
542 void ExitMainLoop() { m_Exit
= true; }
546 void ExitMainLoop() {}
549 bool CopyTextToClipboard(wxString strText
);
551 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
553 DECLARE_EVENT_TABLE()
556 DECLARE_APP(CamuleDaemonApp
)
558 CamuleDaemonApp
*theApp
;
560 extern CamuleDaemonApp
*theApp
;
563 #endif /* ! AMULE_DAEMON */
566 // File_checked_for_headers