2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
6 // Copyright (c) 2004-2008 Froenchenko Leonid (lfroen@users.sourceforge.net)
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "Constants.h"
42 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_NOTIFY
, -1)
46 * This namespaces contains a number of functions and classes
47 * related to defered function calls, allowing a notification
48 * call to be delayed till it can be initiated from the main
54 * Creates a deep copy of the object passed.
56 * Note that this function should be overwritten as
57 * needed. See the wxString version below.
59 template <class ValueType
>
60 inline ValueType
DeepCopy(const ValueType
& value
)
62 return ValueType(value
);
65 /** Special DeepCopy for wxString, which uses reference counting. */
66 inline wxString
DeepCopy(const wxString
& value
)
68 return wxString(value
.c_str(), value
.Length());
72 ////////////////////////////////////////////////////////////
73 // Notification handlers
75 // These functions should not be called directly, but
76 // through the Notify_*, etc. macros.
79 void SharedFilesShowFile(CKnownFile
* file
);
80 void SharedFilesRemoveFile(CKnownFile
* file
);
81 void SharedFilesRemoveAllFiles();
82 void SharedFilesShowFileList();
83 void SharedFilesUpdateItem(CKnownFile
* file
);
85 void DownloadCtrlUpdateItem(const void* item
);
86 void DownloadCtrlAddFile(CPartFile
* file
);
87 void DownloadCtrlAddSource(CPartFile
* owner
, CUpDownClient
* source
, DownloadItemType type
);
88 void DownloadCtrlRemoveFile(CPartFile
* file
);
89 void DownloadCtrlRemoveSource(const CUpDownClient
* source
, const CPartFile
* owner
);
90 void DownloadCtrlHideSource(CPartFile
* file
);
91 void DownloadCtrlSort();
93 void ClientCtrlAddClient(CUpDownClient
* client
, ViewType type
);
94 void ClientCtrlRefreshClient(CUpDownClient
* client
, ViewType type
);
95 void ClientCtrlRemoveClient(CUpDownClient
* client
, ViewType type
);
97 void ServerAdd(CServer
* server
);
98 void ServerRemove(CServer
* server
);
99 void ServerRemoveDead();
100 void ServerRemoveAll();
101 void ServerHighlight(CServer
* server
, bool highlight
);
102 void ServerRefresh(CServer
* server
);
105 void ServerUpdateED2KInfo();
106 void ServerUpdateKadKInfo();
109 void SearchLocalEnd();
110 void KadSearchEnd(uint32 id
);
111 void Search_Update_Sources(CSearchFile
* result
);
112 void Search_Add_Result(CSearchFile
* result
);
114 void ChatRefreshFriend(uint32 lastUsedIP
, uint32 lastUsedPort
, wxString name
);
115 void ChatConnResult(bool success
, uint64 id
, wxString message
);
116 void ChatProcessMsg(uint64 sender
, wxString message
);
117 void ChatSendCaptcha(wxString captcha
, uint64 to_id
);
119 void ShowConnState(long state
);
120 void ShowUserCount(wxString str
);
121 void ShowQueueCount(uint32 count
);
122 void ShowUpdateCatTabTitles();
125 void CategoryAdded();
126 void CategoryUpdate(uint32 cat
);
127 void CategoryDelete(uint32 cat
);
129 void NodesURLChanged(wxString url
);
130 void ServersURLChanged(wxString url
);
134 // GUI -> core notification
137 void PartFile_Swap_A4AF(CPartFile
* file
);
138 void PartFile_Swap_A4AF_Auto(CPartFile
* file
);
139 void PartFile_Swap_A4AF_Others(CPartFile
* file
);
140 void PartFile_Pause(CPartFile
* file
);
141 void PartFile_Resume(CPartFile
* file
);
142 void PartFile_Stop(CPartFile
* file
);
143 void PartFile_PrioAuto(CPartFile
* file
, bool val
);
144 void PartFile_PrioSet(CPartFile
* file
, uint8 newDownPriority
, bool bSave
);
145 void PartFile_Delete(CPartFile
* file
);
146 void PartFile_SetCat(CPartFile
* file
, uint32 val
);
148 void KnownFile_Up_Prio_Set(CKnownFile
* file
, uint8 val
);
149 void KnownFile_Up_Prio_Auto(CKnownFile
* file
);
150 void KnownFile_Comment_Set(CKnownFile
* file
, wxString comment
);
152 void Search_Add_Download(CSearchFile
* result
, uint8 category
);
153 void Search_Update_Progress(uint32 value
);
155 void Download_Set_Cat_Prio(uint8 cat
, uint8 newprio
);
156 void Download_Set_Cat_Status(uint8 cat
, int newstatus
);
159 ////////////////////////////////////////////////////////////
160 // Notification utilities
163 * The base class of the functions.
165 * This class allows the the notification call to be executed
166 * without knowing the exact specifics of a given functor.
168 class CMuleNotiferBase
171 /** The constructor does nothing. */
172 CMuleNotiferBase() {};
173 /** The destructor is virtual since we will be deleting pointers to this type. */
174 virtual ~CMuleNotiferBase() {};
176 /** Executes the actual notification call. */
177 virtual void Notify() const = 0;
178 /** Returns a copy of the functor (function + arguments). */
179 virtual CMuleNotiferBase
* Clone() const = 0;
183 /** Notification functor for functions taking no arguments. */
184 class CMuleNotifier0
: public CMuleNotiferBase
187 typedef void (*FuncType
)();
189 /** Creates a functor from the given function. */
190 CMuleNotifier0(FuncType func
)
193 /** @see CMuleNotifierBase::Notify */
194 virtual void Notify() const {
198 /** @see CMuleNotifierBase::Clone */
199 virtual CMuleNotiferBase
* Clone() const {
200 return new CMuleNotifier0(m_func
);
208 /** Notification functor for functions taking 1 arguments. */
209 template <typename ARG
>
210 class CMuleNotifier1
: public CMuleNotiferBase
213 typedef void (*FuncType
)(ARG
);
215 /** Creates a functor from the given function and arguments. */
216 CMuleNotifier1(FuncType func
, ARG arg
)
221 /** @see CMuleNotifierBase::Notify */
222 virtual void Notify() const {
226 /** @see CMuleNotifierBase::Clone */
227 virtual CMuleNotiferBase
* Clone() const {
228 return new CMuleNotifier1
<ARG
>(m_func
, m_arg
);
237 /** Notification functor for functions taking 2 arguments. */
238 template <typename ARG_1
, typename ARG_2
>
239 class CMuleNotifier2
: public CMuleNotiferBase
242 typedef void (*FuncType
)(ARG_1
, ARG_2
);
244 /** Creates a functor from the given function and arguments. */
245 CMuleNotifier2(FuncType func
, ARG_1 arg1
, ARG_2 arg2
)
247 m_arg1(DeepCopy(arg1
)),
248 m_arg2(DeepCopy(arg2
))
251 /** @see CMuleNotifierBase:: Notify */
252 virtual void Notify() const {
253 m_func(m_arg1
, m_arg2
);
256 /** @see CMuleNotifierBase::Clone */
257 virtual CMuleNotiferBase
* Clone() const {
258 return new CMuleNotifier2
<ARG_1
, ARG_2
>(m_func
, m_arg1
, m_arg2
);
268 /** Notification functor for functions taking 3 arguments. */
269 template <typename ARG_1
, typename ARG_2
, typename ARG_3
>
270 class CMuleNotifier3
: public CMuleNotiferBase
273 typedef void (*FuncType
)(ARG_1
, ARG_2
, ARG_3
);
275 /** Creates a functor from the given function and arguments. */
276 CMuleNotifier3(FuncType func
, ARG_1 arg1
, ARG_2 arg2
, ARG_3 arg3
)
278 m_arg1(DeepCopy(arg1
)),
279 m_arg2(DeepCopy(arg2
)),
280 m_arg3(DeepCopy(arg3
))
283 /** @see CMuleNotifierBase:: Notify */
284 virtual void Notify() const {
285 m_func(m_arg1
, m_arg2
, m_arg3
);
288 /** @see CMuleNotifierBase::Clone */
289 virtual CMuleNotiferBase
* Clone() const {
290 return new CMuleNotifier3
<ARG_1
, ARG_2
, ARG_3
>(m_func
, m_arg1
, m_arg2
, m_arg3
);
302 * This event is sent when a worker-thread makes use of a notify-macro.
304 * This insures that all notifications are executed on the main thread,
305 * thereby improving overall threadsafety. The events are currently
308 class CMuleGUIEvent
: public wxEvent
311 /** Takes ownership a notifier functor. */
312 CMuleGUIEvent(CMuleNotiferBase
* ntf
)
313 : wxEvent(-1, MULE_EVT_NOTIFY
)
319 /** Destructor, frees the functor object. */
320 virtual ~CMuleGUIEvent() {
324 /** Executes the notification. */
325 void Notify() const {
329 /** @see wxEvent::Clone */
330 virtual wxEvent
* Clone() const {
331 return new CMuleGUIEvent(m_functor
->Clone());
336 CMuleGUIEvent(const CMuleGUIEvent
&);
337 /** Not assignable. */
338 CMuleGUIEvent
& operator=(const CMuleGUIEvent
&);
340 //! The actual functor object,
341 CMuleNotiferBase
* m_functor
;
346 * This function will execute or queue a given notification functor.
348 * If the caller is the main thread, the functor is executed immediatly,
349 * thus acting like a regular function call. OTOH, if the caller is a
350 * worker thread, the functor is cloned and sent via an event to
353 void HandleNotification(const CMuleNotiferBase
& ntf
);
356 * These functions take a function pointer and a set of arguments,
357 * matching those of the function-pointer. A functor is created
358 * from these and either executed immediatly, or sent as an event
359 * in the case of non-main threads calling the functions.
361 * Note that the return-value of the function must be void.
363 * IMPORTANT: Note that the functions passed to DoNotify must not
364 * take arguments via references, since this causes the functors
365 * to store references to the arguments, rather than a copy and
366 * thus ends up with dangling references.
369 inline void DoNotify(void (*func
)()) {
370 HandleNotification(CMuleNotifier0(func
));
372 template <typename A1A
, typename A1B
>
373 inline void DoNotify(void (*func
)(A1A
), A1B arg1
) {
374 HandleNotification(CMuleNotifier1
<A1A
>(func
, arg1
));
376 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
>
377 inline void DoNotify(void (*func
)(A1A
, A2A
), A1B arg1
, A2B arg2
) {
378 HandleNotification(CMuleNotifier2
<A1A
, A2A
>(func
, arg1
, arg2
));
380 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
, typename A3A
, typename A3B
>
381 inline void DoNotify(void (*func
)(A1A
, A2A
, A3A
), A1B arg1
, A2B arg2
, A3B arg3
) {
382 HandleNotification(CMuleNotifier3
<A1A
, A2A
, A3A
>(func
, arg1
, arg2
, arg3
));
388 //! Placing CMuleGUIEvent in the global namespace.
389 using MuleNotify::CMuleGUIEvent
;
391 //! The event-handler type that takes a CMuleGUIEvent.
392 typedef void (wxEvtHandler::*MuleNotifyEventFunction
)(CMuleGUIEvent
&);
394 //! Event-handler for completed hashings of new shared files and partfiles.
395 #define EVT_MULE_NOTIFY(func) \
396 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_NOTIFY, -1, -1, \
397 (wxObjectEventFunction) (wxEventFunction) \
398 wxStaticCastEvent(MuleNotifyEventFunction, &func), (wxObject*) NULL),
404 #define Notify_SharedFilesShowFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFile, file)
405 #define Notify_SharedFilesRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveFile, file)
406 #define Notify_SharedFilesRemoveAllItems() MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveAllFiles)
407 #define Notify_SharedFilesShowFileList() MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFileList)
408 #define Notify_SharedFilesSort() MuleNotify::DoNotify(&MuleNotify::SharedFilesSort)
409 #define Notify_SharedFilesUpdateItem(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesUpdateItem, file)
412 #define Notify_DownloadCtrlUpdateItem(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlUpdateItem, ptr)
413 #define Notify_DownloadCtrlAddFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddFile, file)
414 #define Notify_DownloadCtrlAddSource(p0, p1, val) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddSource, p0, p1, val)
415 #define Notify_DownloadCtrlRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveFile, file)
416 #define Notify_DownloadCtrlRemoveSource(ptr0, ptr1) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveSource, ptr0, ptr1)
417 #define Notify_DownloadCtrlHideSource(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlHideSource, ptr)
418 #define Notify_DownloadCtrlSort() MuleNotify::DoNotify(&MuleNotify::DownloadCtrlSort)
421 #define Notify_UploadCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtUploading)
422 #define Notify_UploadCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtUploading)
423 #define Notify_UploadCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtUploading)
426 #define Notify_ClientCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtClients)
427 #define Notify_ClientCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtClients)
428 #define Notify_ClientCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtClients)
431 #define Notify_QlistAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtQueued)
432 #define Notify_QlistRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtQueued)
433 #define Notify_QlistRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtQueued)
436 #define Notify_ServerAdd(ptr) MuleNotify::DoNotify(&MuleNotify::ServerAdd, ptr)
437 #define Notify_ServerRemove(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRemove, ptr)
438 #define Notify_ServerRemoveDead() MuleNotify::DoNotify(&MuleNotify::ServerRemoveDead)
439 #define Notify_ServerRemoveAll() MuleNotify::DoNotify(&MuleNotify::ServerRemoveAll)
440 #define Notify_ServerHighlight(ptr, val) MuleNotify::DoNotify(&MuleNotify::ServerHighlight, ptr, val)
441 #define Notify_ServerRefresh(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRefresh, ptr)
442 #define Notify_ServerFreeze() MuleNotify::DoNotify(&MuleNotify::ServerFreeze)
443 #define Notify_ServerThaw() MuleNotify::DoNotify(&MuleNotify::ServerThaw)
444 #define Notify_ServerUpdateED2KInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateED2KInfo)
445 #define Notify_ServerUpdateKadKInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateKadKInfo)
448 #define Notify_SearchCancel() MuleNotify::DoNotify(&MuleNotify::SearchCancel)
449 #define Notify_SearchLocalEnd() MuleNotify::DoNotify(&MuleNotify::SearchLocalEnd)
450 #define Notify_KadSearchEnd(val) MuleNotify::DoNotify(&MuleNotify::KadSearchEnd, val)
451 #define Notify_Search_Update_Sources(ptr) MuleNotify::DoNotify(&MuleNotify::Search_Update_Sources, ptr)
452 #define Notify_Search_Add_Result(s) MuleNotify::DoNotify(&MuleNotify::Search_Add_Result, s)
455 #define Notify_ChatRefreshFriend(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatRefreshFriend, val0, val1, s)
456 #define Notify_ChatConnResult(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatConnResult, val0, val1, s)
457 #define Notify_ChatProcessMsg(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatProcessMsg, val0, s)
458 #define Notify_ChatSendCaptcha(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatSendCaptcha, val0, s)
461 #define Notify_ShowConnState(val) MuleNotify::DoNotify(&MuleNotify::ShowConnState, val)
462 #define Notify_ShowUserCount(str) MuleNotify::DoNotify(&MuleNotify::ShowUserCount, str)
463 #define Notify_ShowQueueCount(val) MuleNotify::DoNotify(&MuleNotify::ShowQueueCount, val)
464 #define Notify_ShowUpdateCatTabTitles() MuleNotify::DoNotify(&MuleNotify::ShowUpdateCatTabTitles)
465 #define Notify_ShowGUI() MuleNotify::DoNotify(&MuleNotify::ShowGUI)
468 #define Notify_CategoryAdded() MuleNotify::DoNotify(&MuleNotify::CategoryAdded)
469 #define Notify_CategoryUpdate(cat) MuleNotify::DoNotify(&MuleNotify::CategoryUpdate, cat)
470 #define Notify_CategoryDelete(cat) MuleNotify::DoNotify(&MuleNotify::CategoryDelete, cat)
472 // server.met/nodes.dat default urls
473 #define Notify_NodesURLChanged(url) MuleNotify::DoNotify(&MuleNotify::NodesURLChanged, url)
474 #define Notify_ServersURLChanged(url) MuleNotify::DoNotify(&MuleNotify::ServersURLChanged, url)
478 // GUI -> core notification
482 #define CoreNotify_PartFile_Swap_A4AF(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF, ptr)
483 #define CoreNotify_PartFile_Swap_A4AF_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Auto, ptr)
484 #define CoreNotify_PartFile_Swap_A4AF_Others(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Others, ptr)
485 #define CoreNotify_PartFile_Pause(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Pause, ptr)
486 #define CoreNotify_PartFile_Resume(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Resume, ptr)
487 #define CoreNotify_PartFile_Stop(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Stop, ptr)
488 #define CoreNotify_PartFile_PrioAuto(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioAuto, ptr, val)
489 #define CoreNotify_PartFile_PrioSet(p, v0, v1) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioSet, p, v0, v1)
490 #define CoreNotify_PartFile_Delete(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Delete, ptr)
491 #define CoreNotify_PartFile_SetCat(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_SetCat, ptr, val)
494 #define CoreNotify_KnownFile_Up_Prio_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Set, ptr, val)
495 #define CoreNotify_KnownFile_Up_Prio_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Auto, ptr)
496 #define CoreNotify_KnownFile_Comment_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Comment_Set, ptr, val)
499 #define CoreNotify_Search_Add_Download(ptr, val) MuleNotify::DoNotify(&MuleNotify::Search_Add_Download, ptr, val)
500 #define CoreNotify_Search_Update_Progress(val) MuleNotify::DoNotify(&MuleNotify::Search_Update_Progress, val)
503 #define CoreNotify_Download_Set_Cat_Prio(cat, pri) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Prio, cat, pri)
504 #define CoreNotify_Download_Set_Cat_Status(cat, st) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Status, cat, st)
506 #endif // __GUIEVENTS_H__
508 // File_checked_for_headers