Fix header inclusions
[amule.git] / src / amule.h
blob33cacdbf95c9c6a5c3a488b45a1693923ff62cf0
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 __WXMSW__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WXMSW__
43 class CAbstractFile;
44 class CKnownFile;
45 class ExternalConn;
46 class CamuleDlg;
47 class CPreferences;
48 class CDownloadQueue;
49 class CUploadQueue;
50 class CServerConnect;
51 class CSharedFileList;
52 class CServer;
53 class CFriend;
54 class CMD4Hash;
55 class CServerList;
56 class CListenSocket;
57 class CClientList;
58 class CKnownFileList;
59 class CCanceledFileList;
60 class CSearchList;
61 class CClientCreditsList;
62 class CFriendList;
63 class CClientUDPSocket;
64 class CIPFilter;
65 class UploadBandwidthThrottler;
66 #ifdef ENABLE_UPNP
67 class CUPnPControlPoint;
68 class CUPnPPortMapping;
69 #endif
70 class CStatistics;
71 class wxSocketEvent;
72 class wxCommandEvent;
73 class wxCloseEvent;
74 class wxFFileOutputStream;
75 class CTimer;
76 class CTimerEvent;
77 class wxSingleInstanceChecker;
78 class CHashingEvent;
79 class CMuleInternalEvent;
80 class CCompletionEvent;
81 class CAllocFinishedEvent;
82 class wxExecuteData;
83 class CLoggingEvent;
86 namespace MuleNotify {
87 class CMuleGUIEvent;
91 using MuleNotify::CMuleGUIEvent;
94 #ifdef AMULE_DAEMON
95 #define AMULE_APP_BASE wxAppConsole
96 #define CORE_TIMER_PERIOD 300
97 #else
98 #define AMULE_APP_BASE wxApp
99 #define CORE_TIMER_PERIOD 100
100 #endif
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
111 private:
112 // Used to detect a previous running instance of aMule
113 wxSingleInstanceChecker* m_singleInstance;
115 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
116 protected:
117 wxString FullMuleVersion;
118 wxString OSDescription;
119 wxString OSType;
120 bool enable_daemon_fork;
121 bool ec_config;
122 bool m_skipConnectionDialog;
123 bool m_geometryEnabled;
124 wxString m_geometryString;
125 wxString m_logFile;
126 wxString m_appName;
127 wxString m_PidFile;
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);
132 public:
133 wxString ConfigDir;
134 wxString m_configFile;
136 CamuleAppCommon();
137 ~CamuleAppCommon();
138 void AddLinksFromFile();
139 // URL functions
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);
143 // Who am I ?
144 #ifdef AMULE_DAEMON
145 bool IsDaemon() const { return true; }
146 #else
147 bool IsDaemon() const { return false; }
148 #endif
150 #ifdef CLIENT_GUI
151 bool IsRemoteGui() const { return true; }
152 #else
153 bool IsRemoteGui() const { return false; }
154 #endif
156 const wxString& GetMuleAppName() const { return m_appName; }
157 const wxString GetFullMuleVersion() const;
160 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
162 private:
163 enum APPState {
164 APP_STATE_RUNNING = 0,
165 APP_STATE_SHUTTINGDOWN,
166 APP_STATE_STARTING
169 public:
170 CamuleApp();
171 virtual ~CamuleApp();
173 virtual bool OnInit();
174 int OnExit();
175 #if wxUSE_ON_FATAL_EXCEPTION
176 void OnFatalException();
177 #endif
178 bool ReinitializeNetwork(wxString *msg);
180 // derived classes may override those
181 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
183 // Socket handlers
184 void ListenSocketHandler(wxSocketEvent& event);
185 void ServerSocketHandler(wxSocketEvent& event);
186 void UDPSocketHandler(wxSocketEvent& event);
188 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
190 // Barry - To find out if app is running or shutting/shut down
191 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
192 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
194 // Check ED2K and Kademlia state
195 bool IsFirewalled() const;
196 // Are we connected to at least one network?
197 bool IsConnected() const;
198 // Connection to ED2K
199 bool IsConnectedED2K() const;
201 // What about Kad? Is it running?
202 bool IsKadRunning() const;
203 // Connection to Kad
204 bool IsConnectedKad() const;
205 // Check Kad state (TCP)
206 bool IsFirewalledKad() const;
207 // Check Kad state (UDP)
208 bool IsFirewalledKadUDP() const;
209 // Check Kad state (LAN mode)
210 bool IsKadRunningInLanMode() const;
211 // Kad stats
212 uint32 GetKadUsers() const;
213 uint32 GetKadFiles() const;
214 uint32 GetKadIndexedSources() const;
215 uint32 GetKadIndexedKeywords() const;
216 uint32 GetKadIndexedNotes() const;
217 uint32 GetKadIndexedLoad() const;
218 // True IP of machine
219 uint32 GetKadIPAdress() const;
220 // Buddy status
221 uint8 GetBuddyStatus() const;
222 uint32 GetBuddyIP() const;
223 uint32 GetBuddyPort() const;
225 // Check if we should callback this client
226 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
228 // Misc functions
229 void OnlineSig(bool zero = false);
230 void Localize_mule();
231 void Trigger_New_version(wxString newMule);
233 // shakraw - new EC code using wxSocketBase
234 ExternalConn* ECServerHandler;
236 // return current (valid) public IP or 0 if unknown
237 // If ignorelocal is true, don't use m_localip
238 uint32 GetPublicIP(bool ignorelocal = false) const;
239 void SetPublicIP(const uint32 dwIP);
241 uint32 GetED2KID() const;
242 uint32 GetID() const;
244 // Other parts of the interface and such
245 CPreferences* glob_prefs;
246 CDownloadQueue* downloadqueue;
247 CUploadQueue* uploadqueue;
248 CServerConnect* serverconnect;
249 CSharedFileList* sharedfiles;
250 CServerList* serverlist;
251 CListenSocket* listensocket;
252 CClientList* clientlist;
253 CKnownFileList* knownfiles;
254 CCanceledFileList* canceledfiles;
255 CSearchList* searchlist;
256 CClientCreditsList* clientcredits;
257 CFriendList* friendlist;
258 CClientUDPSocket* clientudp;
259 CStatistics* m_statistics;
260 CIPFilter* ipfilter;
261 UploadBandwidthThrottler* uploadBandwidthThrottler;
262 #ifdef ENABLE_UPNP
263 CUPnPControlPoint* m_upnp;
264 std::vector<CUPnPPortMapping> m_upnpMappings;
265 #endif
266 wxLocale m_locale;
268 void ShutDown();
270 wxString GetLog(bool reset = false);
271 wxString GetServerLog(bool reset = false);
272 wxString GetDebugLog(bool reset = false);
274 bool AddServer(CServer *srv, bool fromUser = false);
275 void AddServerMessageLine(wxString &msg);
276 #ifdef __DEBUG__
277 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
278 #endif
279 void SetOSFiles(const wxString new_path);
281 const wxString& GetOSType() const { return OSType; }
283 void ShowUserCount();
285 void ShowConnectionState(bool forceUpdate = false);
287 void StartKad();
288 void StopKad();
290 /** Bootstraps kad from the specified IP (must be in hostorder). */
291 void BootstrapKad(uint32 ip, uint16 port);
292 /** Updates the nodes.dat file from the specified url. */
293 void UpdateNotesDat(const wxString& str);
296 void DisconnectED2K();
298 bool CryptoAvailable() const;
300 protected:
302 #ifdef __WXDEBUG__
304 * Handles asserts in a thread-safe manner.
306 virtual void OnAssertFailure(const wxChar* file, int line,
307 const wxChar* func, const wxChar* cond, const wxChar* msg);
308 #endif
310 void OnUDPDnsDone(CMuleInternalEvent& evt);
311 void OnSourceDnsDone(CMuleInternalEvent& evt);
312 void OnServerDnsDone(CMuleInternalEvent& evt);
314 void OnTCPTimer(CTimerEvent& evt);
315 void OnCoreTimer(CTimerEvent& evt);
317 void OnFinishedHashing(CHashingEvent& evt);
318 void OnFinishedAICHHashing(CHashingEvent& evt);
319 void OnFinishedCompletion(CCompletionEvent& evt);
320 void OnFinishedAllocation(CAllocFinishedEvent& evt);
321 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
322 void OnHashingShutdown(CMuleInternalEvent&);
323 void OnNotifyEvent(CMuleGUIEvent& evt);
325 void SetTimeOnTransfer();
327 APPState m_app_state;
329 wxString m_emulesig_path;
330 wxString m_amulesig_path;
332 uint32 m_dwPublicIP;
334 long webserver_pid;
336 wxString server_msg;
338 CTimer* core_timer;
340 private:
341 virtual void OnUnhandledException();
343 void CheckNewVersion(uint32 result);
345 uint32 m_localip;
349 #ifndef AMULE_DAEMON
352 class CamuleGuiBase {
353 public:
354 CamuleGuiBase();
355 virtual ~CamuleGuiBase();
357 wxString m_FrameTitle;
358 CamuleDlg* amuledlg;
359 int m_FileDetailDialogActive;
361 bool CopyTextToClipboard( wxString strText );
362 void ResetTitle();
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);
368 protected:
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;
378 #ifndef CLIENT_GUI
381 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
384 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
386 int OnExit();
387 bool OnInit();
389 public:
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"
412 #endif // CLIENT_GUI
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)
427 #ifdef __WXMSW__
428 // MSW: can't run amuled with 2.8 anyway, just get it compiled
429 #define AMULED_DUMMY
430 #else
431 #define AMULED28
432 #endif
433 #endif
435 #ifdef AMULED28
436 #include <wx/socket.h>
438 class CSocketSet;
441 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
443 private:
444 CSocketSet *m_in_set, *m_out_set;
446 wxMutex m_lock;
447 public:
448 CAmuledGSocketFuncTable();
450 void AddSocket(GSocket *socket, GSocketEvent event);
451 void RemoveSocket(GSocket *socket, GSocketEvent event);
452 void RunSelect();
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);
466 #endif // AMULED28
468 // no AppTraits used on Windows
469 #ifndef __WXMSW__
471 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
473 #include <wx/apptrait.h>
475 class CDaemonAppTraits : public wxConsoleAppTraits
477 private:
478 struct sigaction m_oldSignalChildAction;
479 struct sigaction m_newSignalChildAction;
481 #ifdef AMULED28
482 CAmuledGSocketFuncTable *m_table;
483 wxMutex m_lock;
484 std::list<wxObject *> m_sched_delete;
485 public:
486 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
487 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
488 virtual void ScheduleForDestroy(wxObject *object);
489 virtual void RemoveFromPendingDelete(wxObject *object);
491 void DeletePending();
492 #else // AMULED28
493 public:
494 CDaemonAppTraits();
495 #endif // !AMULED28
497 virtual int WaitForChild(wxExecuteData& execData);
499 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
500 virtual wxStandardPathsBase& GetStandardPaths();
501 #endif
504 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
505 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
506 #endif // __WXMSW__
509 class CamuleDaemonApp : public CamuleApp
511 private:
512 #ifdef AMULED28
513 bool m_Exit;
514 CAmuledGSocketFuncTable *m_table;
515 #endif
516 bool OnInit();
517 int OnRun();
518 int OnExit();
520 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
522 #ifndef __WXMSW__
523 struct sigaction m_oldSignalChildAction;
524 struct sigaction m_newSignalChildAction;
525 public:
526 wxAppTraits *CreateTraits();
527 #endif // __WXMSW__
529 public:
531 #ifdef AMULED28
532 CamuleDaemonApp();
534 void ExitMainLoop() { m_Exit = true; }
535 #endif
537 #ifdef AMULED_DUMMY
538 void ExitMainLoop() {}
539 #endif
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 */
553 #endif // AMULE_H
554 // File_checked_for_headers