Threw in some tabs for better readabillity and added the forgotten CMakeLists.txt...
[amule.git] / src / amule.h
blobf8792f8646cd526d5a6bb5ce94365908d13be57a
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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
27 #ifndef AMULE_H
28 #define AMULE_H
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
36 #include <map>
37 #ifndef __WINDOWS__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WINDOWS__
42 #include "amule-config.h" // Needed for ASIO_SOCKETS
44 class CAbstractFile;
45 class CKnownFile;
46 class ExternalConn;
47 class CamuleDlg;
48 class CPreferences;
49 class CDownloadQueue;
50 class CUploadQueue;
51 class CServerConnect;
52 class CSharedFileList;
53 class CServer;
54 class CFriend;
55 class CMD4Hash;
56 class CServerList;
57 class CListenSocket;
58 class CClientList;
59 class CKnownFileList;
60 class CCanceledFileList;
61 class CSearchList;
62 class CClientCreditsList;
63 class CFriendList;
64 class CClientUDPSocket;
65 class CIPFilter;
66 class UploadBandwidthThrottler;
67 #ifdef ASIO_SOCKETS
68 class CAsioService;
69 #else
70 class wxSocketEvent;
71 #endif
72 #ifdef ENABLE_UPNP
73 class CUPnPControlPoint;
74 class CUPnPPortMapping;
75 #endif
76 class CStatistics;
77 class wxCommandEvent;
78 class wxCloseEvent;
79 class wxFFileOutputStream;
80 class CTimer;
81 class CTimerEvent;
82 class wxSingleInstanceChecker;
83 class CHashingEvent;
84 class CMuleInternalEvent;
85 class CCompletionEvent;
86 class CAllocFinishedEvent;
87 class wxExecuteData;
88 class CLoggingEvent;
91 namespace MuleNotify {
92 class CMuleGUIEvent;
95 using MuleNotify::CMuleGUIEvent;
98 namespace Kademlia {
99 class CUInt128;
103 #ifdef AMULE_DAEMON
104 #define AMULE_APP_BASE wxAppConsole
105 #define CORE_TIMER_PERIOD 300
106 #else
107 #define AMULE_APP_BASE wxApp
108 #define CORE_TIMER_PERIOD 100
109 #endif
111 #define CONNECTED_ED2K (1<<0)
112 #define CONNECTED_KAD_NOT (1<<1)
113 #define CONNECTED_KAD_OK (1<<2)
114 #define CONNECTED_KAD_FIREWALLED (1<<3)
117 void OnShutdownSignal( int /* sig */ );
120 // Base class common to amule, aamuled and amulegui
121 class CamuleAppCommon
123 private:
124 // Used to detect a previous running instance of aMule
125 wxSingleInstanceChecker* m_singleInstance;
127 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
128 protected:
129 wxString FullMuleVersion;
130 wxString OSDescription;
131 wxString OSType;
132 bool enable_daemon_fork;
133 bool ec_config;
134 bool m_skipConnectionDialog;
135 bool m_geometryEnabled;
136 wxString m_geometryString;
137 wxString m_logFile;
138 wxString m_appName;
139 wxString m_PidFile;
141 bool InitCommon(int argc, wxChar ** argv);
142 void RefreshSingleInstanceChecker();
143 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
144 public:
145 wxString m_configFile;
147 CamuleAppCommon();
148 ~CamuleAppCommon();
149 void AddLinksFromFile();
150 // URL functions
151 wxString CreateMagnetLink(const CAbstractFile *f);
152 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
153 // Who am I ?
154 #ifdef AMULE_DAEMON
155 bool IsDaemon() const { return true; }
156 #else
157 bool IsDaemon() const { return false; }
158 #endif
160 #ifdef CLIENT_GUI
161 bool IsRemoteGui() const { return true; }
162 #else
163 bool IsRemoteGui() const { return false; }
164 #endif
166 const wxString& GetMuleAppName() const { return m_appName; }
167 const wxString GetFullMuleVersion() const;
170 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
172 private:
173 enum APPState {
174 APP_STATE_RUNNING = 0,
175 APP_STATE_SHUTTINGDOWN,
176 APP_STATE_STARTING
179 public:
180 CamuleApp();
181 virtual ~CamuleApp();
183 virtual bool OnInit();
184 int OnExit();
185 #if wxUSE_ON_FATAL_EXCEPTION
186 void OnFatalException();
187 #endif
188 bool ReinitializeNetwork(wxString *msg);
190 // derived classes may override those
191 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
193 #ifndef ASIO_SOCKETS
194 // Socket handlers
195 void ListenSocketHandler(wxSocketEvent& event);
196 void UDPSocketHandler(wxSocketEvent& event);
197 #endif
199 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
201 // Barry - To find out if app is running or shutting/shut down
202 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
203 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
205 // Check ED2K and Kademlia state
206 bool IsFirewalled() const;
207 // Are we connected to at least one network?
208 bool IsConnected() const;
209 // Connection to ED2K
210 bool IsConnectedED2K() const;
212 // What about Kad? Is it running?
213 bool IsKadRunning() const;
214 // Connection to Kad
215 bool IsConnectedKad() const;
216 // Check Kad state (TCP)
217 bool IsFirewalledKad() const;
218 // Check Kad state (UDP)
219 bool IsFirewalledKadUDP() const;
220 // Check Kad state (LAN mode)
221 bool IsKadRunningInLanMode() const;
222 // Kad stats
223 uint32 GetKadUsers() const;
224 uint32 GetKadFiles() const;
225 uint32 GetKadIndexedSources() const;
226 uint32 GetKadIndexedKeywords() const;
227 uint32 GetKadIndexedNotes() const;
228 uint32 GetKadIndexedLoad() const;
229 // True IP of machine
230 uint32 GetKadIPAdress() const;
231 // Buddy status
232 uint8 GetBuddyStatus() const;
233 uint32 GetBuddyIP() const;
234 uint32 GetBuddyPort() const;
235 // Kad ID
236 const Kademlia::CUInt128& GetKadID() const;
238 // Check if we should callback this client
239 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
241 // Misc functions
242 void OnlineSig(bool zero = false);
243 void Localize_mule();
244 void Trigger_New_version(wxString newMule);
246 // shakraw - new EC code using wxSocketBase
247 ExternalConn* ECServerHandler;
249 // return current (valid) public IP or 0 if unknown
250 // If ignorelocal is true, don't use m_localip
251 uint32 GetPublicIP(bool ignorelocal = false) const;
252 void SetPublicIP(const uint32 dwIP);
254 uint32 GetED2KID() const;
255 uint32 GetID() const;
257 // Other parts of the interface and such
258 CPreferences* glob_prefs;
259 CDownloadQueue* downloadqueue;
260 CUploadQueue* uploadqueue;
261 CServerConnect* serverconnect;
262 CSharedFileList* sharedfiles;
263 CServerList* serverlist;
264 CListenSocket* listensocket;
265 CClientList* clientlist;
266 CKnownFileList* knownfiles;
267 CCanceledFileList* canceledfiles;
268 CSearchList* searchlist;
269 CClientCreditsList* clientcredits;
270 CFriendList* friendlist;
271 CClientUDPSocket* clientudp;
272 CStatistics* m_statistics;
273 CIPFilter* ipfilter;
274 UploadBandwidthThrottler* uploadBandwidthThrottler;
275 #ifdef ASIO_SOCKETS
276 CAsioService* m_AsioService;
277 #endif
278 #ifdef ENABLE_UPNP
279 CUPnPControlPoint* m_upnp;
280 std::vector<CUPnPPortMapping> m_upnpMappings;
281 #endif
282 wxLocale m_locale;
284 void ShutDown();
286 wxString GetLog(bool reset = false);
287 wxString GetServerLog(bool reset = false);
288 wxString GetDebugLog(bool reset = false);
290 bool AddServer(CServer *srv, bool fromUser = false);
291 void AddServerMessageLine(wxString &msg);
292 #ifdef __DEBUG__
293 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
294 #endif
295 void SetOSFiles(const wxString& new_path);
297 const wxString& GetOSType() const { return OSType; }
299 void ShowUserCount();
301 void ShowConnectionState(bool forceUpdate = false);
303 void StartKad();
304 void StopKad();
306 /** Bootstraps kad from the specified IP (must be in hostorder). */
307 void BootstrapKad(uint32 ip, uint16 port);
308 /** Updates the nodes.dat file from the specified url. */
309 void UpdateNotesDat(const wxString& str);
312 void DisconnectED2K();
314 bool CryptoAvailable() const;
316 protected:
318 #ifdef __WXDEBUG__
320 * Handles asserts in a thread-safe manner.
322 virtual void OnAssertFailure(const wxChar* file, int line,
323 const wxChar* func, const wxChar* cond, const wxChar* msg);
324 #endif
326 void OnUDPDnsDone(CMuleInternalEvent& evt);
327 void OnSourceDnsDone(CMuleInternalEvent& evt);
328 void OnServerDnsDone(CMuleInternalEvent& evt);
330 void OnTCPTimer(CTimerEvent& evt);
331 void OnCoreTimer(CTimerEvent& evt);
333 void OnFinishedHashing(CHashingEvent& evt);
334 void OnFinishedAICHHashing(CHashingEvent& evt);
335 void OnFinishedCompletion(CCompletionEvent& evt);
336 void OnFinishedAllocation(CAllocFinishedEvent& evt);
337 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
338 void OnHashingShutdown(CMuleInternalEvent&);
339 void OnNotifyEvent(CMuleGUIEvent& evt);
341 void SetTimeOnTransfer();
343 APPState m_app_state;
345 wxString m_emulesig_path;
346 wxString m_amulesig_path;
348 uint32 m_dwPublicIP;
350 long webserver_pid;
352 wxString server_msg;
354 CTimer* core_timer;
356 private:
357 virtual void OnUnhandledException();
359 void CheckNewVersion(uint32 result);
361 uint32 m_localip;
365 #ifndef AMULE_DAEMON
368 class CamuleGuiBase {
369 public:
370 CamuleGuiBase();
371 virtual ~CamuleGuiBase();
373 wxString m_FrameTitle;
374 CamuleDlg* amuledlg;
376 bool CopyTextToClipboard( wxString strText );
377 void ResetTitle();
379 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
380 virtual int ShowAlert(wxString msg, wxString title, int flags);
382 void AddGuiLogLine(const wxString& line);
383 protected:
385 * This list is used to contain log messages that are to be displayed
386 * on the GUI, when it is currently impossible to do so. This is in order
387 * to allows us to queue messages till after the dialog has been created.
389 std::list<wxString> m_logLines;
393 #ifndef CLIENT_GUI
396 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
399 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
401 int OnExit();
402 bool OnInit();
404 public:
406 virtual int ShowAlert(wxString msg, wxString title, int flags);
408 void ShutDown(wxCloseEvent &evt);
410 wxString GetLog(bool reset = false);
411 wxString GetServerLog(bool reset = false);
412 void AddServerMessageLine(wxString &msg);
413 DECLARE_EVENT_TABLE()
417 DECLARE_APP(CamuleGuiApp)
418 extern CamuleGuiApp *theApp;
421 #else /* !CLIENT_GUI */
424 #include "amule-remote-gui.h"
427 #endif // CLIENT_GUI
430 #define CALL_APP_DATA_LOCK
433 #else /* ! AMULE_DAEMON */
435 // wxWidgets 2.8 requires special code for event handling and sockets.
436 // 2.9 doesn't, so standard event loop and sockets can be used
438 // Windows: aMuled compiles with 2.8 (without the special code),
439 // but works only with 2.9
441 #if !wxCHECK_VERSION(2, 9, 0)
442 // wx 2.8 needs a hand-made event loop in any case
443 #define AMULED28_EVENTLOOP
445 #ifndef ASIO_SOCKETS
446 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
447 #ifndef __WINDOWS__
448 #define AMULED28_SOCKETS
449 #endif
450 #endif
451 #endif
453 #ifdef AMULED28_SOCKETS
454 #include <wx/socket.h>
456 class CSocketSet;
459 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
461 private:
462 CSocketSet *m_in_set, *m_out_set;
464 wxMutex m_lock;
465 public:
466 CAmuledGSocketFuncTable();
468 void AddSocket(GSocket *socket, GSocketEvent event);
469 void RemoveSocket(GSocket *socket, GSocketEvent event);
470 void RunSelect();
472 virtual bool OnInit();
473 virtual void OnExit();
474 virtual bool CanUseEventLoop();
475 virtual bool Init_Socket(GSocket *socket);
476 virtual void Destroy_Socket(GSocket *socket);
477 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
478 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
479 virtual void Enable_Events(GSocket *socket);
480 virtual void Disable_Events(GSocket *socket);
484 #endif // AMULED28_SOCKETS
486 // AppTrait functionality is required for 2.8 wx sockets
487 // Otherwise it's used to prevent zombie child processes,
488 // which stops working with wx 2.9.5.
489 // So disable it there (no idea if this has a noticeable impact).
491 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
492 #define AMULED_APPTRAITS
493 #endif
495 #ifdef AMULED_APPTRAITS
497 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
499 #include <wx/apptrait.h>
501 class CDaemonAppTraits : public wxConsoleAppTraits
503 private:
504 struct sigaction m_oldSignalChildAction;
505 struct sigaction m_newSignalChildAction;
507 #ifdef AMULED28_SOCKETS
508 CAmuledGSocketFuncTable *m_table;
509 wxMutex m_lock;
510 std::list<wxObject *> m_sched_delete;
511 public:
512 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
513 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
514 virtual void ScheduleForDestroy(wxObject *object);
515 virtual void RemoveFromPendingDelete(wxObject *object);
517 void DeletePending();
518 #else // AMULED28_SOCKETS
519 public:
520 CDaemonAppTraits();
521 #endif // !AMULED28_SOCKETS
523 virtual int WaitForChild(wxExecuteData& execData);
525 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
526 virtual wxStandardPathsBase& GetStandardPaths();
527 #endif
530 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
531 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
533 #endif // AMULED_APPTRAITS
536 class CamuleDaemonApp : public CamuleApp
538 private:
539 #ifdef AMULED28_EVENTLOOP
540 bool m_Exit;
541 #endif
542 #ifdef AMULED28_SOCKETS
543 CAmuledGSocketFuncTable *m_table;
544 #endif
545 bool OnInit();
546 int OnRun();
547 int OnExit();
549 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
550 // The GTK wxApps sets its file name conversion properly
551 // in wxApp::Initialize(), while wxAppConsole::Initialize()
552 // does not, leaving wxConvFile being set to wxConvLibc. File
553 // name conversion should be set otherwise amuled will abort to
554 // handle non-ASCII file names which monolithic amule can handle.
555 // This function are overrided to perform this.
556 virtual bool Initialize(int& argc_, wxChar **argv_);
558 #ifdef AMULED_APPTRAITS
559 struct sigaction m_oldSignalChildAction;
560 struct sigaction m_newSignalChildAction;
561 public:
562 wxAppTraits *CreateTraits();
563 #endif // AMULED_APPTRAITS
565 public:
567 #ifdef AMULED28_EVENTLOOP
568 CamuleDaemonApp();
570 void ExitMainLoop() { m_Exit = true; }
571 #endif
573 bool CopyTextToClipboard(wxString strText);
575 virtual int ShowAlert(wxString msg, wxString title, int flags);
577 DECLARE_EVENT_TABLE()
580 DECLARE_APP(CamuleDaemonApp)
581 extern CamuleDaemonApp *theApp;
583 #endif /* ! AMULE_DAEMON */
585 #endif // AMULE_H
586 // File_checked_for_headers