Forwardport from 2.2.3: Fix autosort happening when it shouldn't
[amule.git] / src / amule.h
blobc5933a4e6854b1cf8a63a1fa579697574bc2a5c7
1 //
2 // This file is part of the aMule Project.
3 //
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 )
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.
20 //
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 CUpDownClient;
76 class CTimer;
77 class CTimerEvent;
78 class wxSingleInstanceChecker;
79 class CHashingEvent;
80 class CMuleInternalEvent;
81 class CCompletionEvent;
82 class CAllocFinishedEvent;
83 class wxExecuteData;
84 class CLoggingEvent;
87 namespace MuleNotify {
88 class CMuleGUIEvent;
92 using MuleNotify::CMuleGUIEvent;
95 #ifdef AMULE_DAEMON
96 #define AMULE_APP_BASE wxAppConsole
97 #define CORE_TIMER_PERIOD 300
98 #else
99 #define AMULE_APP_BASE wxApp
100 #define CORE_TIMER_PERIOD 100
101 #endif
103 #define CONNECTED_ED2K (1<<0)
104 #define CONNECTED_KAD_NOT (1<<1)
105 #define CONNECTED_KAD_OK (1<<2)
106 #define CONNECTED_KAD_FIREWALLED (1<<3)
109 // Base class common to amule, aamuled and amulegui
110 class CamuleAppCommon
112 private:
113 // Used to detect a previous running instance of aMule
114 wxSingleInstanceChecker* m_singleInstance;
116 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
117 protected:
118 char *strFullMuleVersion;
119 char *strOSDescription;
120 wxString OSType;
121 bool enable_daemon_fork;
122 bool ec_config;
123 bool m_skipConnectionDialog;
124 bool m_geometryEnabled;
125 wxString m_geometryString;
126 wxString m_logFile;
127 wxString m_appName;
128 wxString m_PidFile;
130 bool InitCommon(int argc, wxChar ** argv);
131 void RefreshSingleInstanceChecker();
132 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
133 public:
134 wxString ConfigDir;
135 wxString m_configFile;
137 CamuleAppCommon();
138 ~CamuleAppCommon();
139 void AddLinksFromFile();
140 // URL functions
141 wxString CreateMagnetLink(const CAbstractFile *f);
142 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool addcryptoptions = false);
143 wxString CreateED2kAICHLink(const CKnownFile* f);
144 // Who am I ?
145 #ifdef AMULE_DAEMON
146 bool IsDaemon() const { return true; }
147 #else
148 bool IsDaemon() const { return false; }
149 #endif
151 #ifdef CLIENT_GUI
152 bool IsRemoteGui() const { return true; }
153 #else
154 bool IsRemoteGui() const { return false; }
155 #endif
157 const wxString& GetMuleAppName() const { return m_appName; }
158 const wxString GetFullMuleVersion() const;
161 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
163 private:
164 enum APPState {
165 APP_STATE_RUNNING = 0,
166 APP_STATE_SHUTTINGDOWN,
167 APP_STATE_STARTING
170 public:
171 CamuleApp();
172 virtual ~CamuleApp();
174 virtual bool OnInit();
175 int OnExit();
176 void OnFatalException();
177 bool ReinitializeNetwork(wxString *msg);
179 // derived classes may override those
180 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
182 // Socket handlers
183 void ListenSocketHandler(wxSocketEvent& event);
184 void ServerSocketHandler(wxSocketEvent& event);
185 void UDPSocketHandler(wxSocketEvent& event);
187 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
189 // Barry - To find out if app is running or shutting/shut down
190 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
191 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
193 // Check ED2K and Kademlia state
194 bool IsFirewalled() const;
195 // Are we connected to at least one network?
196 bool IsConnected() const;
197 // Connection to ED2K
198 bool IsConnectedED2K() const;
200 // What about Kad? Is it running?
201 bool IsKadRunning() const;
202 // Connection to Kad
203 bool IsConnectedKad() const;
204 // Check Kad state (TCP)
205 bool IsFirewalledKad() const;
206 // Check Kad state (UDP)
207 bool IsFirewalledKadUDP() const;
208 // Kad stats
209 uint32 GetKadUsers() const;
210 uint32 GetKadFiles() const;
211 uint32 GetKadIndexedSources() const;
212 uint32 GetKadIndexedKeywords() const;
213 uint32 GetKadIndexedNotes() const;
214 uint32 GetKadIndexedLoad() const;
215 // True IP of machine
216 uint32 GetKadIPAdress() const;
217 // Buddy status
218 uint8 GetBuddyStatus() const;
219 uint32 GetBuddyIP() const;
220 uint32 GetBuddyPort() const;
222 // Check if we should callback this client
223 bool CanDoCallback(CUpDownClient *client);
225 // Misc functions
226 void OnlineSig(bool zero = false);
227 void Localize_mule();
228 void Trigger_New_version(wxString newMule);
230 // shakraw - new EC code using wxSocketBase
231 ExternalConn* ECServerHandler;
233 // return current (valid) public IP or 0 if unknown
234 // If ignorelocal is true, don't use m_localip
235 uint32 GetPublicIP(bool ignorelocal = false) const;
236 void SetPublicIP(const uint32 dwIP);
238 uint32 GetED2KID() const;
239 uint32 GetID() const;
241 // Other parts of the interface and such
242 CPreferences* glob_prefs;
243 CDownloadQueue* downloadqueue;
244 CUploadQueue* uploadqueue;
245 CServerConnect* serverconnect;
246 CSharedFileList* sharedfiles;
247 CServerList* serverlist;
248 CListenSocket* listensocket;
249 CClientList* clientlist;
250 CKnownFileList* knownfiles;
251 CCanceledFileList* canceledfiles;
252 CSearchList* searchlist;
253 CClientCreditsList* clientcredits;
254 CFriendList* friendlist;
255 CClientUDPSocket* clientudp;
256 CStatistics* m_statistics;
257 CIPFilter* ipfilter;
258 UploadBandwidthThrottler* uploadBandwidthThrottler;
259 #ifdef ENABLE_UPNP
260 CUPnPControlPoint* m_upnp;
261 std::vector<CUPnPPortMapping> m_upnpMappings;
262 #endif
263 wxLocale m_locale;
265 void ShutDown();
267 wxString GetLog(bool reset = false);
268 wxString GetServerLog(bool reset = false);
269 wxString GetDebugLog(bool reset = false);
271 bool AddServer(CServer *srv, bool fromUser = false);
272 void AddServerMessageLine(wxString &msg);
273 #ifdef __DEBUG__
274 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
275 #endif
276 void SetOSFiles(const wxString new_path);
278 const wxString& GetOSType() const { return OSType; }
280 void ShowUserCount();
282 void ShowConnectionState();
284 void StartKad();
285 void StopKad();
287 /** Bootstraps kad from the specified IP (must be in hostorder). */
288 void BootstrapKad(uint32 ip, uint16 port);
289 /** Updates the nodes.dat file from the specified url. */
290 void UpdateNotesDat(const wxString& str);
293 void DisconnectED2K();
295 bool CryptoAvailable() const;
297 protected:
299 #ifdef __WXDEBUG__
301 * Handles asserts in a thread-safe manner.
303 virtual void OnAssertFailure(const wxChar* file, int line,
304 const wxChar* func, const wxChar* cond, const wxChar* msg);
305 #endif
307 void OnUDPDnsDone(CMuleInternalEvent& evt);
308 void OnSourceDnsDone(CMuleInternalEvent& evt);
309 void OnServerDnsDone(CMuleInternalEvent& evt);
311 void OnTCPTimer(CTimerEvent& evt);
312 void OnCoreTimer(CTimerEvent& evt);
314 void OnFinishedHashing(CHashingEvent& evt);
315 void OnFinishedAICHHashing(CHashingEvent& evt);
316 void OnFinishedCompletion(CCompletionEvent& evt);
317 void OnFinishedAllocation(CAllocFinishedEvent& evt);
318 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
319 void OnHashingShutdown(CMuleInternalEvent&);
320 void OnNotifyEvent(CMuleGUIEvent& evt);
322 void SetTimeOnTransfer();
324 APPState m_app_state;
326 wxString m_emulesig_path;
327 wxString m_amulesig_path;
329 uint32 m_dwPublicIP;
331 long webserver_pid;
333 wxString server_msg;
335 CTimer* core_timer;
337 private:
338 virtual void OnUnhandledException();
340 void CheckNewVersion(uint32 result);
342 uint32 m_localip;
346 #ifndef AMULE_DAEMON
349 class CamuleGuiBase {
350 public:
351 CamuleGuiBase();
352 virtual ~CamuleGuiBase();
354 wxString m_FrameTitle;
355 CamuleDlg* amuledlg;
356 int m_FileDetailDialogActive;
358 bool CopyTextToClipboard( wxString strText );
360 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
361 virtual int ShowAlert(wxString msg, wxString title, int flags);
363 void AddGuiLogLine(const wxString& line);
364 protected:
366 * This list is used to contain log messages that are to be displayed
367 * on the GUI, when it is currently impossible to do so. This is in order
368 * to allows us to queue messages till after the dialog has been created.
370 std::list<wxString> m_logLines;
374 #ifndef CLIENT_GUI
377 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
380 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
382 int OnExit();
383 bool OnInit();
385 public:
387 virtual int ShowAlert(wxString msg, wxString title, int flags);
389 void ShutDown(wxCloseEvent &evt);
391 wxString GetLog(bool reset = false);
392 wxString GetServerLog(bool reset = false);
393 void AddServerMessageLine(wxString &msg);
394 DECLARE_EVENT_TABLE()
398 DECLARE_APP(CamuleGuiApp)
399 extern CamuleGuiApp *theApp;
402 #else /* !CLIENT_GUI */
405 #include "amule-remote-gui.h"
408 #endif // CLIENT_GUI
411 #define CALL_APP_DATA_LOCK
414 #else /* ! AMULE_DAEMON */
416 // wxWidgets 2.8 requires special code for event handling and sockets.
417 // 2.9 doesn't, so standard event loop and sockets can be used
419 // Windows: aMuled compiles with 2.8 (without the special code),
420 // but works only with 2.9
422 #if !wxCHECK_VERSION(2, 9, 0)
423 #ifdef __WXMSW__
424 // MSW: can't run amuled with 2.8 anyway, just get it compiled
425 #define AMULED_DUMMY
426 #else
427 #define AMULED28
428 #endif
429 #endif
431 #ifdef AMULED28
432 #include <wx/socket.h>
434 class CSocketSet;
437 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
439 private:
440 CSocketSet *m_in_set, *m_out_set;
442 wxMutex m_lock;
443 public:
444 CAmuledGSocketFuncTable();
446 void AddSocket(GSocket *socket, GSocketEvent event);
447 void RemoveSocket(GSocket *socket, GSocketEvent event);
448 void RunSelect();
450 virtual bool OnInit();
451 virtual void OnExit();
452 virtual bool CanUseEventLoop();
453 virtual bool Init_Socket(GSocket *socket);
454 virtual void Destroy_Socket(GSocket *socket);
455 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
456 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
457 virtual void Enable_Events(GSocket *socket);
458 virtual void Disable_Events(GSocket *socket);
462 #endif // AMULED28
464 // no AppTraits used on Windows
465 #ifndef __WXMSW__
467 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
469 #include <wx/apptrait.h>
471 class CDaemonAppTraits : public wxConsoleAppTraits
473 private:
474 struct sigaction m_oldSignalChildAction;
475 struct sigaction m_newSignalChildAction;
477 #ifdef AMULED28
478 CAmuledGSocketFuncTable *m_table;
479 wxMutex m_lock;
480 std::list<wxObject *> m_sched_delete;
481 public:
482 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
483 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
484 virtual void ScheduleForDestroy(wxObject *object);
485 virtual void RemoveFromPendingDelete(wxObject *object);
487 void DeletePending();
488 #else // AMULED28
489 public:
490 CDaemonAppTraits();
491 #endif // !AMULED28
493 virtual int WaitForChild(wxExecuteData& execData);
495 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
496 virtual wxStandardPathsBase& GetStandardPaths();
497 #endif
500 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
501 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
502 #endif // __WXMSW__
505 class CamuleDaemonApp : public CamuleApp
507 private:
508 #ifdef AMULED28
509 bool m_Exit;
510 CAmuledGSocketFuncTable *m_table;
511 #endif
512 bool OnInit();
513 int OnRun();
514 int OnExit();
516 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
518 #ifndef __WXMSW__
519 struct sigaction m_oldSignalChildAction;
520 struct sigaction m_newSignalChildAction;
521 public:
522 wxAppTraits *CreateTraits();
523 #endif // __WXMSW__
525 public:
527 #ifdef AMULED28
528 CamuleDaemonApp();
530 void ExitMainLoop() { m_Exit = true; }
531 #endif
533 #ifdef AMULED_DUMMY
534 void ExitMainLoop() {}
535 #endif
537 bool CopyTextToClipboard(wxString strText);
539 virtual int ShowAlert(wxString msg, wxString title, int flags);
541 DECLARE_EVENT_TABLE()
544 DECLARE_APP(CamuleDaemonApp)
545 extern CamuleDaemonApp *theApp;
547 #endif /* ! AMULE_DAEMON */
549 #endif // AMULE_H
550 // File_checked_for_headers