Change policy about how we handle generated files
[amule.git] / src / amule.h
blobc778c34042237d603aa3a4ce86b2c8de566dc755
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 #ifdef HAVE_CONFIG_H
43 # include "config.h" // Needed for ASIO_SOCKETS
44 #endif
47 class CAbstractFile;
48 class CKnownFile;
49 class ExternalConn;
50 class CamuleDlg;
51 class CPreferences;
52 class CDownloadQueue;
53 class CUploadQueue;
54 class CServerConnect;
55 class CSharedFileList;
56 class CServer;
57 class CFriend;
58 class CMD4Hash;
59 class CServerList;
60 class CListenSocket;
61 class CClientList;
62 class CKnownFileList;
63 class CCanceledFileList;
64 class CSearchList;
65 class CClientCreditsList;
66 class CFriendList;
67 class CClientUDPSocket;
68 class CIPFilter;
69 class UploadBandwidthThrottler;
70 #ifdef ASIO_SOCKETS
71 class CAsioService;
72 #else
73 class wxSocketEvent;
74 #endif
75 #ifdef ENABLE_UPNP
76 class CUPnPControlPoint;
77 class CUPnPPortMapping;
78 #endif
79 class CStatistics;
80 class wxCommandEvent;
81 class wxCloseEvent;
82 class wxFFileOutputStream;
83 class CTimer;
84 class CTimerEvent;
85 class wxSingleInstanceChecker;
86 class CHashingEvent;
87 class CMuleInternalEvent;
88 class CCompletionEvent;
89 class CAllocFinishedEvent;
90 class wxExecuteData;
91 class CLoggingEvent;
94 namespace MuleNotify {
95 class CMuleGUIEvent;
98 using MuleNotify::CMuleGUIEvent;
101 namespace Kademlia {
102 class CUInt128;
106 #ifdef AMULE_DAEMON
107 #define AMULE_APP_BASE wxAppConsole
108 #define CORE_TIMER_PERIOD 300
109 #else
110 #define AMULE_APP_BASE wxApp
111 #define CORE_TIMER_PERIOD 100
112 #endif
114 #define CONNECTED_ED2K (1<<0)
115 #define CONNECTED_KAD_NOT (1<<1)
116 #define CONNECTED_KAD_OK (1<<2)
117 #define CONNECTED_KAD_FIREWALLED (1<<3)
120 void OnShutdownSignal( int /* sig */ );
123 // Base class common to amule, aamuled and amulegui
124 class CamuleAppCommon
126 private:
127 // Used to detect a previous running instance of aMule
128 wxSingleInstanceChecker* m_singleInstance;
130 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
131 protected:
132 wxString FullMuleVersion;
133 wxString OSDescription;
134 wxString OSType;
135 bool enable_daemon_fork;
136 bool ec_config;
137 bool m_skipConnectionDialog;
138 bool m_geometryEnabled;
139 wxString m_geometryString;
140 wxString m_logFile;
141 wxString m_appName;
142 wxString m_PidFile;
144 bool InitCommon(int argc, wxChar ** argv);
145 void RefreshSingleInstanceChecker();
146 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
147 public:
148 wxString m_configFile;
150 CamuleAppCommon();
151 ~CamuleAppCommon();
152 void AddLinksFromFile();
153 // URL functions
154 wxString CreateMagnetLink(const CAbstractFile *f);
155 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
156 // Who am I ?
157 #ifdef AMULE_DAEMON
158 bool IsDaemon() const { return true; }
159 #else
160 bool IsDaemon() const { return false; }
161 #endif
163 #ifdef CLIENT_GUI
164 bool IsRemoteGui() const { return true; }
165 #else
166 bool IsRemoteGui() const { return false; }
167 #endif
169 const wxString& GetMuleAppName() const { return m_appName; }
170 const wxString GetFullMuleVersion() const;
173 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
175 private:
176 enum APPState {
177 APP_STATE_RUNNING = 0,
178 APP_STATE_SHUTTINGDOWN,
179 APP_STATE_STARTING
182 public:
183 CamuleApp();
184 virtual ~CamuleApp();
186 virtual bool OnInit();
187 int OnExit();
188 #if wxUSE_ON_FATAL_EXCEPTION
189 void OnFatalException();
190 #endif
191 bool ReinitializeNetwork(wxString *msg);
193 // derived classes may override those
194 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
196 #ifndef ASIO_SOCKETS
197 // Socket handlers
198 void ListenSocketHandler(wxSocketEvent& event);
199 void UDPSocketHandler(wxSocketEvent& event);
200 #endif
202 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
204 // Barry - To find out if app is running or shutting/shut down
205 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
206 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
208 // Check ED2K and Kademlia state
209 bool IsFirewalled() const;
210 // Are we connected to at least one network?
211 bool IsConnected() const;
212 // Connection to ED2K
213 bool IsConnectedED2K() const;
215 // What about Kad? Is it running?
216 bool IsKadRunning() const;
217 // Connection to Kad
218 bool IsConnectedKad() const;
219 // Check Kad state (TCP)
220 bool IsFirewalledKad() const;
221 // Check Kad state (UDP)
222 bool IsFirewalledKadUDP() const;
223 // Check Kad state (LAN mode)
224 bool IsKadRunningInLanMode() const;
225 // Kad stats
226 uint32 GetKadUsers() const;
227 uint32 GetKadFiles() const;
228 uint32 GetKadIndexedSources() const;
229 uint32 GetKadIndexedKeywords() const;
230 uint32 GetKadIndexedNotes() const;
231 uint32 GetKadIndexedLoad() const;
232 // True IP of machine
233 uint32 GetKadIPAdress() const;
234 // Buddy status
235 uint8 GetBuddyStatus() const;
236 uint32 GetBuddyIP() const;
237 uint32 GetBuddyPort() const;
238 // Kad ID
239 const Kademlia::CUInt128& GetKadID() const;
241 // Check if we should callback this client
242 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
244 // Misc functions
245 void OnlineSig(bool zero = false);
246 void Localize_mule();
247 void Trigger_New_version(wxString newMule);
249 // shakraw - new EC code using wxSocketBase
250 ExternalConn* ECServerHandler;
252 // return current (valid) public IP or 0 if unknown
253 // If ignorelocal is true, don't use m_localip
254 uint32 GetPublicIP(bool ignorelocal = false) const;
255 void SetPublicIP(const uint32 dwIP);
257 uint32 GetED2KID() const;
258 uint32 GetID() const;
260 // Other parts of the interface and such
261 CPreferences* glob_prefs;
262 CDownloadQueue* downloadqueue;
263 CUploadQueue* uploadqueue;
264 CServerConnect* serverconnect;
265 CSharedFileList* sharedfiles;
266 CServerList* serverlist;
267 CListenSocket* listensocket;
268 CClientList* clientlist;
269 CKnownFileList* knownfiles;
270 CCanceledFileList* canceledfiles;
271 CSearchList* searchlist;
272 CClientCreditsList* clientcredits;
273 CFriendList* friendlist;
274 CClientUDPSocket* clientudp;
275 CStatistics* m_statistics;
276 CIPFilter* ipfilter;
277 UploadBandwidthThrottler* uploadBandwidthThrottler;
278 #ifdef ASIO_SOCKETS
279 CAsioService* m_AsioService;
280 #endif
281 #ifdef ENABLE_UPNP
282 CUPnPControlPoint* m_upnp;
283 std::vector<CUPnPPortMapping> m_upnpMappings;
284 #endif
285 wxLocale m_locale;
287 void ShutDown();
289 wxString GetLog(bool reset = false);
290 wxString GetServerLog(bool reset = false);
291 wxString GetDebugLog(bool reset = false);
293 bool AddServer(CServer *srv, bool fromUser = false);
294 void AddServerMessageLine(wxString &msg);
295 #ifdef __DEBUG__
296 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
297 #endif
298 void SetOSFiles(const wxString& new_path);
300 const wxString& GetOSType() const { return OSType; }
302 void ShowUserCount();
304 void ShowConnectionState(bool forceUpdate = false);
306 void StartKad();
307 void StopKad();
309 /** Bootstraps kad from the specified IP (must be in hostorder). */
310 void BootstrapKad(uint32 ip, uint16 port);
311 /** Updates the nodes.dat file from the specified url. */
312 void UpdateNotesDat(const wxString& str);
315 void DisconnectED2K();
317 bool CryptoAvailable() const;
319 protected:
321 #ifdef __WXDEBUG__
323 * Handles asserts in a thread-safe manner.
325 virtual void OnAssertFailure(const wxChar* file, int line,
326 const wxChar* func, const wxChar* cond, const wxChar* msg);
327 #endif
329 void OnUDPDnsDone(CMuleInternalEvent& evt);
330 void OnSourceDnsDone(CMuleInternalEvent& evt);
331 void OnServerDnsDone(CMuleInternalEvent& evt);
333 void OnTCPTimer(CTimerEvent& evt);
334 void OnCoreTimer(CTimerEvent& evt);
336 void OnFinishedHashing(CHashingEvent& evt);
337 void OnFinishedAICHHashing(CHashingEvent& evt);
338 void OnFinishedCompletion(CCompletionEvent& evt);
339 void OnFinishedAllocation(CAllocFinishedEvent& evt);
340 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
341 void OnHashingShutdown(CMuleInternalEvent&);
342 void OnNotifyEvent(CMuleGUIEvent& evt);
344 void SetTimeOnTransfer();
346 APPState m_app_state;
348 wxString m_emulesig_path;
349 wxString m_amulesig_path;
351 uint32 m_dwPublicIP;
353 long webserver_pid;
355 wxString server_msg;
357 CTimer* core_timer;
359 private:
360 virtual void OnUnhandledException();
362 void CheckNewVersion(uint32 result);
364 uint32 m_localip;
368 #ifndef AMULE_DAEMON
371 class CamuleGuiBase {
372 public:
373 CamuleGuiBase();
374 virtual ~CamuleGuiBase();
376 wxString m_FrameTitle;
377 CamuleDlg* amuledlg;
379 bool CopyTextToClipboard( wxString strText );
380 void ResetTitle();
382 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
383 virtual int ShowAlert(wxString msg, wxString title, int flags);
385 void AddGuiLogLine(const wxString& line);
386 protected:
388 * This list is used to contain log messages that are to be displayed
389 * on the GUI, when it is currently impossible to do so. This is in order
390 * to allows us to queue messages till after the dialog has been created.
392 std::list<wxString> m_logLines;
396 #ifndef CLIENT_GUI
399 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
402 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
404 int OnExit();
405 bool OnInit();
407 public:
409 virtual int ShowAlert(wxString msg, wxString title, int flags);
411 void ShutDown(wxCloseEvent &evt);
413 wxString GetLog(bool reset = false);
414 wxString GetServerLog(bool reset = false);
415 void AddServerMessageLine(wxString &msg);
416 DECLARE_EVENT_TABLE()
420 DECLARE_APP(CamuleGuiApp)
421 extern CamuleGuiApp *theApp;
424 #else /* !CLIENT_GUI */
427 #include "amule-remote-gui.h"
430 #endif // CLIENT_GUI
433 #define CALL_APP_DATA_LOCK
436 #else /* ! AMULE_DAEMON */
438 // wxWidgets 2.8 requires special code for event handling and sockets.
439 // 2.9 doesn't, so standard event loop and sockets can be used
441 // Windows: aMuled compiles with 2.8 (without the special code),
442 // but works only with 2.9
444 #if !wxCHECK_VERSION(2, 9, 0)
445 // wx 2.8 needs a hand-made event loop in any case
446 #define AMULED28_EVENTLOOP
448 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
450 #ifdef HAVE_CONFIG_H
451 # include "config.h" // defines ASIO_SOCKETS
452 #endif
454 #ifndef ASIO_SOCKETS
455 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
456 #ifndef __WINDOWS__
457 #define AMULED28_SOCKETS
458 #endif
459 #endif
460 #endif
462 #ifdef AMULED28_SOCKETS
463 #include <wx/socket.h>
465 class CSocketSet;
468 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
470 private:
471 CSocketSet *m_in_set, *m_out_set;
473 wxMutex m_lock;
474 public:
475 CAmuledGSocketFuncTable();
477 void AddSocket(GSocket *socket, GSocketEvent event);
478 void RemoveSocket(GSocket *socket, GSocketEvent event);
479 void RunSelect();
481 virtual bool OnInit();
482 virtual void OnExit();
483 virtual bool CanUseEventLoop();
484 virtual bool Init_Socket(GSocket *socket);
485 virtual void Destroy_Socket(GSocket *socket);
486 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
487 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
488 virtual void Enable_Events(GSocket *socket);
489 virtual void Disable_Events(GSocket *socket);
493 #endif // AMULED28_SOCKETS
495 // AppTrait functionality is required for 2.8 wx sockets
496 // Otherwise it's used to prevent zombie child processes,
497 // which stops working with wx 2.9.5.
498 // So disable it there (no idea if this has a noticeable impact).
500 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
501 #define AMULED_APPTRAITS
502 #endif
504 #ifdef AMULED_APPTRAITS
506 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
508 #include <wx/apptrait.h>
510 class CDaemonAppTraits : public wxConsoleAppTraits
512 private:
513 struct sigaction m_oldSignalChildAction;
514 struct sigaction m_newSignalChildAction;
516 #ifdef AMULED28_SOCKETS
517 CAmuledGSocketFuncTable *m_table;
518 wxMutex m_lock;
519 std::list<wxObject *> m_sched_delete;
520 public:
521 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
522 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
523 virtual void ScheduleForDestroy(wxObject *object);
524 virtual void RemoveFromPendingDelete(wxObject *object);
526 void DeletePending();
527 #else // AMULED28_SOCKETS
528 public:
529 CDaemonAppTraits();
530 #endif // !AMULED28_SOCKETS
532 virtual int WaitForChild(wxExecuteData& execData);
534 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
535 virtual wxStandardPathsBase& GetStandardPaths();
536 #endif
539 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
540 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
542 #endif // AMULED_APPTRAITS
545 class CamuleDaemonApp : public CamuleApp
547 private:
548 #ifdef AMULED28_EVENTLOOP
549 bool m_Exit;
550 #endif
551 #ifdef AMULED28_SOCKETS
552 CAmuledGSocketFuncTable *m_table;
553 #endif
554 bool OnInit();
555 int OnRun();
556 int OnExit();
558 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
559 // The GTK wxApps sets its file name conversion properly
560 // in wxApp::Initialize(), while wxAppConsole::Initialize()
561 // does not, leaving wxConvFile being set to wxConvLibc. File
562 // name conversion should be set otherwise amuled will abort to
563 // handle non-ASCII file names which monolithic amule can handle.
564 // This function are overrided to perform this.
565 virtual bool Initialize(int& argc_, wxChar **argv_);
567 #ifdef AMULED_APPTRAITS
568 struct sigaction m_oldSignalChildAction;
569 struct sigaction m_newSignalChildAction;
570 public:
571 wxAppTraits *CreateTraits();
572 #endif // AMULED_APPTRAITS
574 public:
576 #ifdef AMULED28_EVENTLOOP
577 CamuleDaemonApp();
579 void ExitMainLoop() { m_Exit = true; }
580 #endif
582 bool CopyTextToClipboard(wxString strText);
584 virtual int ShowAlert(wxString msg, wxString title, int flags);
586 DECLARE_EVENT_TABLE()
589 DECLARE_APP(CamuleDaemonApp)
590 extern CamuleDaemonApp *theApp;
592 #endif /* ! AMULE_DAEMON */
594 #endif // AMULE_H
595 // File_checked_for_headers