Gracefully handle the case when wxWidgets doesn't have fatal exception handling
[amule.git] / src / amule.h
blob85ced6e8e2ed126201809534ce0260bd552d99f2
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 #if wxUSE_ON_FATAL_EXCEPTION
177 void OnFatalException();
178 #endif
179 bool ReinitializeNetwork(wxString *msg);
181 // derived classes may override those
182 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
184 // Socket handlers
185 void ListenSocketHandler(wxSocketEvent& event);
186 void ServerSocketHandler(wxSocketEvent& event);
187 void UDPSocketHandler(wxSocketEvent& event);
189 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
191 // Barry - To find out if app is running or shutting/shut down
192 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
193 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
195 // Check ED2K and Kademlia state
196 bool IsFirewalled() const;
197 // Are we connected to at least one network?
198 bool IsConnected() const;
199 // Connection to ED2K
200 bool IsConnectedED2K() const;
202 // What about Kad? Is it running?
203 bool IsKadRunning() const;
204 // Connection to Kad
205 bool IsConnectedKad() const;
206 // Check Kad state (TCP)
207 bool IsFirewalledKad() const;
208 // Check Kad state (UDP)
209 bool IsFirewalledKadUDP() const;
210 // Check Kad state (LAN mode)
211 bool IsKadRunningInLanMode() const;
212 // Kad stats
213 uint32 GetKadUsers() const;
214 uint32 GetKadFiles() const;
215 uint32 GetKadIndexedSources() const;
216 uint32 GetKadIndexedKeywords() const;
217 uint32 GetKadIndexedNotes() const;
218 uint32 GetKadIndexedLoad() const;
219 // True IP of machine
220 uint32 GetKadIPAdress() const;
221 // Buddy status
222 uint8 GetBuddyStatus() const;
223 uint32 GetBuddyIP() const;
224 uint32 GetBuddyPort() const;
226 // Check if we should callback this client
227 bool CanDoCallback(CUpDownClient *client);
229 // Misc functions
230 void OnlineSig(bool zero = false);
231 void Localize_mule();
232 void Trigger_New_version(wxString newMule);
234 // shakraw - new EC code using wxSocketBase
235 ExternalConn* ECServerHandler;
237 // return current (valid) public IP or 0 if unknown
238 // If ignorelocal is true, don't use m_localip
239 uint32 GetPublicIP(bool ignorelocal = false) const;
240 void SetPublicIP(const uint32 dwIP);
242 uint32 GetED2KID() const;
243 uint32 GetID() const;
245 // Other parts of the interface and such
246 CPreferences* glob_prefs;
247 CDownloadQueue* downloadqueue;
248 CUploadQueue* uploadqueue;
249 CServerConnect* serverconnect;
250 CSharedFileList* sharedfiles;
251 CServerList* serverlist;
252 CListenSocket* listensocket;
253 CClientList* clientlist;
254 CKnownFileList* knownfiles;
255 CCanceledFileList* canceledfiles;
256 CSearchList* searchlist;
257 CClientCreditsList* clientcredits;
258 CFriendList* friendlist;
259 CClientUDPSocket* clientudp;
260 CStatistics* m_statistics;
261 CIPFilter* ipfilter;
262 UploadBandwidthThrottler* uploadBandwidthThrottler;
263 #ifdef ENABLE_UPNP
264 CUPnPControlPoint* m_upnp;
265 std::vector<CUPnPPortMapping> m_upnpMappings;
266 #endif
267 wxLocale m_locale;
269 void ShutDown();
271 wxString GetLog(bool reset = false);
272 wxString GetServerLog(bool reset = false);
273 wxString GetDebugLog(bool reset = false);
275 bool AddServer(CServer *srv, bool fromUser = false);
276 void AddServerMessageLine(wxString &msg);
277 #ifdef __DEBUG__
278 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
279 #endif
280 void SetOSFiles(const wxString new_path);
282 const wxString& GetOSType() const { return OSType; }
284 void ShowUserCount();
286 void ShowConnectionState(bool forceUpdate = false);
288 void StartKad();
289 void StopKad();
291 /** Bootstraps kad from the specified IP (must be in hostorder). */
292 void BootstrapKad(uint32 ip, uint16 port);
293 /** Updates the nodes.dat file from the specified url. */
294 void UpdateNotesDat(const wxString& str);
297 void DisconnectED2K();
299 bool CryptoAvailable() const;
301 protected:
303 #ifdef __WXDEBUG__
305 * Handles asserts in a thread-safe manner.
307 virtual void OnAssertFailure(const wxChar* file, int line,
308 const wxChar* func, const wxChar* cond, const wxChar* msg);
309 #endif
311 void OnUDPDnsDone(CMuleInternalEvent& evt);
312 void OnSourceDnsDone(CMuleInternalEvent& evt);
313 void OnServerDnsDone(CMuleInternalEvent& evt);
315 void OnTCPTimer(CTimerEvent& evt);
316 void OnCoreTimer(CTimerEvent& evt);
318 void OnFinishedHashing(CHashingEvent& evt);
319 void OnFinishedAICHHashing(CHashingEvent& evt);
320 void OnFinishedCompletion(CCompletionEvent& evt);
321 void OnFinishedAllocation(CAllocFinishedEvent& evt);
322 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
323 void OnHashingShutdown(CMuleInternalEvent&);
324 void OnNotifyEvent(CMuleGUIEvent& evt);
326 void SetTimeOnTransfer();
328 APPState m_app_state;
330 wxString m_emulesig_path;
331 wxString m_amulesig_path;
333 uint32 m_dwPublicIP;
335 long webserver_pid;
337 wxString server_msg;
339 CTimer* core_timer;
341 private:
342 virtual void OnUnhandledException();
344 void CheckNewVersion(uint32 result);
346 uint32 m_localip;
350 #ifndef AMULE_DAEMON
353 class CamuleGuiBase {
354 public:
355 CamuleGuiBase();
356 virtual ~CamuleGuiBase();
358 wxString m_FrameTitle;
359 CamuleDlg* amuledlg;
360 int m_FileDetailDialogActive;
362 bool CopyTextToClipboard( wxString strText );
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