Remove do-nothing command and add warning about it
[amule.git] / src / amule.h
blob5ded928716dccf4ad1a03ef7a9739851df2ec651
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 #endif // __WINDOWS__
41 #include "config.h" // Needed for ASIO_SOCKETS
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 ASIO_SOCKETS
67 class CAsioService;
68 #else
69 class wxSocketEvent;
70 #endif
71 #ifdef ENABLE_UPNP
72 class CUPnPControlPoint;
73 class CUPnPPortMapping;
74 #endif
75 class CStatistics;
76 class wxCommandEvent;
77 class wxCloseEvent;
78 class wxFFileOutputStream;
79 class CTimer;
80 class CTimerEvent;
81 class wxSingleInstanceChecker;
82 class CHashingEvent;
83 class CMuleInternalEvent;
84 class CCompletionEvent;
85 class CAllocFinishedEvent;
86 class wxExecuteData;
87 class CLoggingEvent;
90 namespace MuleNotify {
91 class CMuleGUIEvent;
94 using MuleNotify::CMuleGUIEvent;
97 namespace Kademlia {
98 class CUInt128;
102 #ifdef AMULE_DAEMON
103 #define AMULE_APP_BASE wxAppConsole
104 #define CORE_TIMER_PERIOD 300
105 #else
106 #define AMULE_APP_BASE wxApp
107 #define CORE_TIMER_PERIOD 100
108 #endif
110 #define CONNECTED_ED2K (1<<0)
111 #define CONNECTED_KAD_NOT (1<<1)
112 #define CONNECTED_KAD_OK (1<<2)
113 #define CONNECTED_KAD_FIREWALLED (1<<3)
116 void OnShutdownSignal( int /* sig */ );
119 // Base class common to amule, aamuled and amulegui
120 class CamuleAppCommon
122 private:
123 // Used to detect a previous running instance of aMule
124 wxSingleInstanceChecker* m_singleInstance;
126 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
127 protected:
128 wxString FullMuleVersion;
129 wxString OSDescription;
130 wxString OSType;
131 bool enable_daemon_fork;
132 bool ec_config;
133 bool m_skipConnectionDialog;
134 bool m_geometryEnabled;
135 wxString m_geometryString;
136 wxString m_logFile;
137 wxString m_appName;
138 wxString m_PidFile;
140 bool InitCommon(int argc, wxChar ** argv);
141 void RefreshSingleInstanceChecker();
142 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
143 public:
144 wxString m_configFile;
146 CamuleAppCommon();
147 ~CamuleAppCommon();
148 void AddLinksFromFile();
149 // URL functions
150 wxString CreateMagnetLink(const CAbstractFile *f);
151 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
152 // Who am I ?
153 #ifdef AMULE_DAEMON
154 bool IsDaemon() const { return true; }
155 #else
156 bool IsDaemon() const { return false; }
157 #endif
159 #ifdef CLIENT_GUI
160 bool IsRemoteGui() const { return true; }
161 #else
162 bool IsRemoteGui() const { return false; }
163 #endif
165 const wxString& GetMuleAppName() const { return m_appName; }
166 const wxString GetFullMuleVersion() const;
169 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
171 private:
172 enum APPState {
173 APP_STATE_RUNNING = 0,
174 APP_STATE_SHUTTINGDOWN,
175 APP_STATE_STARTING
178 public:
179 CamuleApp();
180 virtual ~CamuleApp();
182 virtual bool OnInit();
183 int OnExit();
184 #if wxUSE_ON_FATAL_EXCEPTION
185 void OnFatalException();
186 #endif
187 bool ReinitializeNetwork(wxString *msg);
189 // derived classes may override those
190 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
192 #ifndef ASIO_SOCKETS
193 // Socket handlers
194 void ListenSocketHandler(wxSocketEvent& event);
195 void UDPSocketHandler(wxSocketEvent& event);
196 #endif
198 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
200 // Barry - To find out if app is running or shutting/shut down
201 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
202 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
204 // Check ED2K and Kademlia state
205 bool IsFirewalled() const;
206 // Are we connected to at least one network?
207 bool IsConnected() const;
208 // Connection to ED2K
209 bool IsConnectedED2K() const;
211 // What about Kad? Is it running?
212 bool IsKadRunning() const;
213 // Connection to Kad
214 bool IsConnectedKad() const;
215 // Check Kad state (TCP)
216 bool IsFirewalledKad() const;
217 // Check Kad state (UDP)
218 bool IsFirewalledKadUDP() const;
219 // Check Kad state (LAN mode)
220 bool IsKadRunningInLanMode() const;
221 // Kad stats
222 uint32 GetKadUsers() const;
223 uint32 GetKadFiles() const;
224 uint32 GetKadIndexedSources() const;
225 uint32 GetKadIndexedKeywords() const;
226 uint32 GetKadIndexedNotes() const;
227 uint32 GetKadIndexedLoad() const;
228 // True IP of machine
229 uint32 GetKadIPAdress() const;
230 // Buddy status
231 uint8 GetBuddyStatus() const;
232 uint32 GetBuddyIP() const;
233 uint32 GetBuddyPort() const;
234 // Kad ID
235 const Kademlia::CUInt128& GetKadID() const;
237 // Check if we should callback this client
238 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
240 // Misc functions
241 void OnlineSig(bool zero = false);
242 void Localize_mule();
243 void Trigger_New_version(wxString newMule);
245 // shakraw - new EC code using wxSocketBase
246 ExternalConn* ECServerHandler;
248 // return current (valid) public IP or 0 if unknown
249 // If ignorelocal is true, don't use m_localip
250 uint32 GetPublicIP(bool ignorelocal = false) const;
251 void SetPublicIP(const uint32 dwIP);
253 uint32 GetED2KID() const;
254 uint32 GetID() const;
256 // Other parts of the interface and such
257 CPreferences* glob_prefs;
258 CDownloadQueue* downloadqueue;
259 CUploadQueue* uploadqueue;
260 CServerConnect* serverconnect;
261 CSharedFileList* sharedfiles;
262 CServerList* serverlist;
263 CListenSocket* listensocket;
264 CClientList* clientlist;
265 CKnownFileList* knownfiles;
266 CCanceledFileList* canceledfiles;
267 CSearchList* searchlist;
268 CClientCreditsList* clientcredits;
269 CFriendList* friendlist;
270 CClientUDPSocket* clientudp;
271 CStatistics* m_statistics;
272 CIPFilter* ipfilter;
273 UploadBandwidthThrottler* uploadBandwidthThrottler;
274 #ifdef ASIO_SOCKETS
275 CAsioService* m_AsioService;
276 #endif
277 #ifdef ENABLE_UPNP
278 CUPnPControlPoint* m_upnp;
279 std::vector<CUPnPPortMapping> m_upnpMappings;
280 #endif
281 wxLocale m_locale;
283 void ShutDown();
285 wxString GetLog(bool reset = false);
286 wxString GetServerLog(bool reset = false);
287 wxString GetDebugLog(bool reset = false);
289 bool AddServer(CServer *srv, bool fromUser = false);
290 void AddServerMessageLine(wxString &msg);
291 #ifdef __DEBUG__
292 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
293 #endif
294 void SetOSFiles(const wxString& new_path);
296 const wxString& GetOSType() const { return OSType; }
298 void ShowUserCount();
300 void ShowConnectionState(bool forceUpdate = false);
302 void StartKad();
303 void StopKad();
305 /** Bootstraps kad from the specified IP (must be in hostorder). */
306 void BootstrapKad(uint32 ip, uint16 port);
307 /** Updates the nodes.dat file from the specified url. */
308 void UpdateNotesDat(const wxString& str);
311 void DisconnectED2K();
313 bool CryptoAvailable() const;
315 protected:
317 #ifdef __WXDEBUG__
319 * Handles asserts in a thread-safe manner.
321 virtual void OnAssertFailure(const wxChar* file, int line,
322 const wxChar* func, const wxChar* cond, const wxChar* msg);
323 #endif
325 void OnUDPDnsDone(CMuleInternalEvent& evt);
326 void OnSourceDnsDone(CMuleInternalEvent& evt);
327 void OnServerDnsDone(CMuleInternalEvent& evt);
329 void OnTCPTimer(CTimerEvent& evt);
330 void OnCoreTimer(CTimerEvent& evt);
332 void OnFinishedHashing(CHashingEvent& evt);
333 void OnFinishedAICHHashing(CHashingEvent& evt);
334 void OnFinishedCompletion(CCompletionEvent& evt);
335 void OnFinishedAllocation(CAllocFinishedEvent& evt);
336 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
337 void OnHashingShutdown(CMuleInternalEvent&);
338 void OnNotifyEvent(CMuleGUIEvent& evt);
340 void SetTimeOnTransfer();
342 APPState m_app_state;
344 wxString m_emulesig_path;
345 wxString m_amulesig_path;
347 uint32 m_dwPublicIP;
349 long webserver_pid;
351 wxString server_msg;
353 CTimer* core_timer;
355 private:
356 virtual void OnUnhandledException();
358 void CheckNewVersion(uint32 result);
360 uint32 m_localip;
364 #ifndef AMULE_DAEMON
367 class CamuleGuiBase {
368 public:
369 CamuleGuiBase();
370 virtual ~CamuleGuiBase();
372 wxString m_FrameTitle;
373 CamuleDlg* amuledlg;
375 bool CopyTextToClipboard( wxString strText );
376 void ResetTitle();
378 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
379 virtual int ShowAlert(wxString msg, wxString title, int flags);
381 void AddGuiLogLine(const wxString& line);
382 protected:
384 * This list is used to contain log messages that are to be displayed
385 * on the GUI, when it is currently impossible to do so. This is in order
386 * to allows us to queue messages till after the dialog has been created.
388 std::list<wxString> m_logLines;
392 #ifndef CLIENT_GUI
395 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
398 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
400 int OnExit();
401 bool OnInit();
403 public:
405 virtual int ShowAlert(wxString msg, wxString title, int flags);
407 void ShutDown(wxCloseEvent &evt);
409 wxString GetLog(bool reset = false);
410 wxString GetServerLog(bool reset = false);
411 void AddServerMessageLine(wxString &msg);
412 DECLARE_EVENT_TABLE()
416 DECLARE_APP(CamuleGuiApp)
417 extern CamuleGuiApp *theApp;
420 #else /* !CLIENT_GUI */
423 #include "amule-remote-gui.h"
426 #endif // CLIENT_GUI
429 #define CALL_APP_DATA_LOCK
432 #else /* ! AMULE_DAEMON */
434 // wxWidgets 2.8 requires special code for event handling and sockets.
435 // 2.9 doesn't, so standard event loop and sockets can be used
437 // Windows: aMuled compiles with 2.8 (without the special code),
438 // but works only with 2.9
440 #if !wxCHECK_VERSION(2, 9, 0)
441 // wx 2.8 needs a hand-made event loop in any case
442 #define AMULED28_EVENTLOOP
444 #ifndef ASIO_SOCKETS
445 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
446 #ifndef __WINDOWS__
447 #define AMULED28_SOCKETS
448 #endif
449 #endif
450 #endif
452 #ifdef AMULED28_SOCKETS
453 #include <wx/socket.h>
455 class CSocketSet;
458 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
460 private:
461 CSocketSet *m_in_set, *m_out_set;
463 wxMutex m_lock;
464 public:
465 CAmuledGSocketFuncTable();
467 void AddSocket(GSocket *socket, GSocketEvent event);
468 void RemoveSocket(GSocket *socket, GSocketEvent event);
469 void RunSelect();
471 virtual bool OnInit();
472 virtual void OnExit();
473 virtual bool CanUseEventLoop();
474 virtual bool Init_Socket(GSocket *socket);
475 virtual void Destroy_Socket(GSocket *socket);
476 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
477 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
478 virtual void Enable_Events(GSocket *socket);
479 virtual void Disable_Events(GSocket *socket);
483 #endif // AMULED28_SOCKETS
485 // AppTrait functionality is required for 2.8 wx sockets
486 // Otherwise it's used to prevent zombie child processes,
487 // which stops working with wx 2.9.5.
488 // So disable it there (no idea if this has a noticeable impact).
490 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
491 #define AMULED_APPTRAITS
492 #endif
494 #ifdef AMULED_APPTRAITS
496 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
498 #include <wx/apptrait.h>
500 class CDaemonAppTraits : public wxConsoleAppTraits
502 private:
503 struct sigaction m_oldSignalChildAction;
504 struct sigaction m_newSignalChildAction;
506 #ifdef AMULED28_SOCKETS
507 CAmuledGSocketFuncTable *m_table;
508 wxMutex m_lock;
509 std::list<wxObject *> m_sched_delete;
510 public:
511 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
512 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
513 virtual void ScheduleForDestroy(wxObject *object);
514 virtual void RemoveFromPendingDelete(wxObject *object);
516 void DeletePending();
517 #else // AMULED28_SOCKETS
518 public:
519 CDaemonAppTraits();
520 #endif // !AMULED28_SOCKETS
522 virtual int WaitForChild(wxExecuteData& execData);
524 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
525 virtual wxStandardPathsBase& GetStandardPaths();
526 #endif
529 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
530 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
532 #endif // AMULED_APPTRAITS
535 class CamuleDaemonApp : public CamuleApp
537 private:
538 #ifdef AMULED28_EVENTLOOP
539 bool m_Exit;
540 #endif
541 #ifdef AMULED28_SOCKETS
542 CAmuledGSocketFuncTable *m_table;
543 #endif
544 bool OnInit();
545 int OnRun();
546 int OnExit();
548 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
549 // The GTK wxApps sets its file name conversion properly
550 // in wxApp::Initialize(), while wxAppConsole::Initialize()
551 // does not, leaving wxConvFile being set to wxConvLibc. File
552 // name conversion should be set otherwise amuled will abort to
553 // handle non-ASCII file names which monolithic amule can handle.
554 // This function are overrided to perform this.
555 virtual bool Initialize(int& argc_, wxChar **argv_);
557 #ifdef AMULED_APPTRAITS
558 struct sigaction m_oldSignalChildAction;
559 struct sigaction m_newSignalChildAction;
560 public:
561 wxAppTraits *CreateTraits();
562 #endif // AMULED_APPTRAITS
564 public:
566 #ifdef AMULED28_EVENTLOOP
567 CamuleDaemonApp();
569 void ExitMainLoop() { m_Exit = true; }
570 #endif
572 bool CopyTextToClipboard(wxString strText);
574 virtual int ShowAlert(wxString msg, wxString title, int flags);
576 DECLARE_EVENT_TABLE()
579 DECLARE_APP(CamuleDaemonApp)
580 extern CamuleDaemonApp *theApp;
582 #endif /* ! AMULE_DAEMON */
584 #endif // AMULE_H
585 // File_checked_for_headers