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
);
118 void ShowConnState(long state
);
119 void ShowUserCount(wxString str
);
120 void ShowQueueCount(uint32 count
);
121 void ShowUpdateCatTabTitles();
124 void CategoryAdded();
125 void CategoryUpdate(uint32 cat
);
126 void CategoryDelete(uint32 cat
);
128 void NodesURLChanged(wxString url
);
129 void ServersURLChanged(wxString url
);
133 // GUI -> core notification
136 void PartFile_Swap_A4AF(CPartFile
* file
);
137 void PartFile_Swap_A4AF_Auto(CPartFile
* file
);
138 void PartFile_Swap_A4AF_Others(CPartFile
* file
);
139 void PartFile_Pause(CPartFile
* file
);
140 void PartFile_Resume(CPartFile
* file
);
141 void PartFile_Stop(CPartFile
* file
);
142 void PartFile_PrioAuto(CPartFile
* file
, bool val
);
143 void PartFile_PrioSet(CPartFile
* file
, uint8 newDownPriority
, bool bSave
);
144 void PartFile_Delete(CPartFile
* file
);
145 void PartFile_SetCat(CPartFile
* file
, uint32 val
);
147 void KnownFile_Up_Prio_Set(CKnownFile
* file
, uint8 val
);
148 void KnownFile_Up_Prio_Auto(CKnownFile
* file
);
149 void KnownFile_Comment_Set(CKnownFile
* file
, wxString comment
);
151 void Search_Add_Download(CSearchFile
* result
, uint8 category
);
152 void Search_Update_Progress(uint32 value
);
154 void Download_Set_Cat_Prio(uint8 cat
, uint8 newprio
);
155 void Download_Set_Cat_Status(uint8 cat
, int newstatus
);
158 ////////////////////////////////////////////////////////////
159 // Notification utilities
162 * The base class of the functions.
164 * This class allows the the notification call to be executed
165 * without knowing the exact specifics of a given functor.
167 class CMuleNotiferBase
170 /** The constructor does nothing. */
171 CMuleNotiferBase() {};
172 /** The destructor is virtual since we will be deleting pointers to this type. */
173 virtual ~CMuleNotiferBase() {};
175 /** Executes the actual notification call. */
176 virtual void Notify() const = 0;
177 /** Returns a copy of the functor (function + arguments). */
178 virtual CMuleNotiferBase
* Clone() const = 0;
182 /** Notification functor for functions taking no arguments. */
183 class CMuleNotifier0
: public CMuleNotiferBase
186 typedef void (*FuncType
)();
188 /** Creates a functor from the given function. */
189 CMuleNotifier0(FuncType func
)
192 /** @see CMuleNotifierBase::Notify */
193 virtual void Notify() const {
197 /** @see CMuleNotifierBase::Clone */
198 virtual CMuleNotiferBase
* Clone() const {
199 return new CMuleNotifier0(m_func
);
207 /** Notification functor for functions taking 1 arguments. */
208 template <typename ARG
>
209 class CMuleNotifier1
: public CMuleNotiferBase
212 typedef void (*FuncType
)(ARG
);
214 /** Creates a functor from the given function and arguments. */
215 CMuleNotifier1(FuncType func
, ARG arg
)
220 /** @see CMuleNotifierBase::Notify */
221 virtual void Notify() const {
225 /** @see CMuleNotifierBase::Clone */
226 virtual CMuleNotiferBase
* Clone() const {
227 return new CMuleNotifier1
<ARG
>(m_func
, m_arg
);
236 /** Notification functor for functions taking 2 arguments. */
237 template <typename ARG_1
, typename ARG_2
>
238 class CMuleNotifier2
: public CMuleNotiferBase
241 typedef void (*FuncType
)(ARG_1
, ARG_2
);
243 /** Creates a functor from the given function and arguments. */
244 CMuleNotifier2(FuncType func
, ARG_1 arg1
, ARG_2 arg2
)
246 m_arg1(DeepCopy(arg1
)),
247 m_arg2(DeepCopy(arg2
))
250 /** @see CMuleNotifierBase:: Notify */
251 virtual void Notify() const {
252 m_func(m_arg1
, m_arg2
);
255 /** @see CMuleNotifierBase::Clone */
256 virtual CMuleNotiferBase
* Clone() const {
257 return new CMuleNotifier2
<ARG_1
, ARG_2
>(m_func
, m_arg1
, m_arg2
);
267 /** Notification functor for functions taking 3 arguments. */
268 template <typename ARG_1
, typename ARG_2
, typename ARG_3
>
269 class CMuleNotifier3
: public CMuleNotiferBase
272 typedef void (*FuncType
)(ARG_1
, ARG_2
, ARG_3
);
274 /** Creates a functor from the given function and arguments. */
275 CMuleNotifier3(FuncType func
, ARG_1 arg1
, ARG_2 arg2
, ARG_3 arg3
)
277 m_arg1(DeepCopy(arg1
)),
278 m_arg2(DeepCopy(arg2
)),
279 m_arg3(DeepCopy(arg3
))
282 /** @see CMuleNotifierBase:: Notify */
283 virtual void Notify() const {
284 m_func(m_arg1
, m_arg2
, m_arg3
);
287 /** @see CMuleNotifierBase::Clone */
288 virtual CMuleNotiferBase
* Clone() const {
289 return new CMuleNotifier3
<ARG_1
, ARG_2
, ARG_3
>(m_func
, m_arg1
, m_arg2
, m_arg3
);
301 * This event is sent when a worker-thread makes use of a notify-macro.
303 * This insures that all notifications are executed on the main thread,
304 * thereby improving overall threadsafety. The events are currently
307 class CMuleGUIEvent
: public wxEvent
310 /** Takes ownership a notifier functor. */
311 CMuleGUIEvent(CMuleNotiferBase
* ntf
)
312 : wxEvent(-1, MULE_EVT_NOTIFY
)
318 /** Destructor, frees the functor object. */
319 virtual ~CMuleGUIEvent() {
323 /** Executes the notification. */
324 void Notify() const {
328 /** @see wxEvent::Clone */
329 virtual wxEvent
* Clone() const {
330 return new CMuleGUIEvent(m_functor
->Clone());
335 CMuleGUIEvent(const CMuleGUIEvent
&);
336 /** Not assignable. */
337 CMuleGUIEvent
& operator=(const CMuleGUIEvent
&);
339 //! The actual functor object,
340 CMuleNotiferBase
* m_functor
;
345 * This function will execute or queue a given notification functor.
347 * If the caller is the main thread, the functor is executed immediatly,
348 * thus acting like a regular function call. OTOH, if the caller is a
349 * worker thread, the functor is cloned and sent via an event to
352 void HandleNotification(const CMuleNotiferBase
& ntf
);
355 * These functions take a function pointer and a set of arguments,
356 * matching those of the function-pointer. A functor is created
357 * from these and either executed immediatly, or sent as an event
358 * in the case of non-main threads calling the functions.
360 * Note that the return-value of the function must be void.
362 * IMPORTANT: Note that the functions passed to DoNotify must not
363 * take arguments via references, since this causes the functors
364 * to store references to the arguments, rather than a copy and
365 * thus ends up with dangling references.
368 inline void DoNotify(void (*func
)()) {
369 HandleNotification(CMuleNotifier0(func
));
371 template <typename A1A
, typename A1B
>
372 inline void DoNotify(void (*func
)(A1A
), A1B arg1
) {
373 HandleNotification(CMuleNotifier1
<A1A
>(func
, arg1
));
375 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
>
376 inline void DoNotify(void (*func
)(A1A
, A2A
), A1B arg1
, A2B arg2
) {
377 HandleNotification(CMuleNotifier2
<A1A
, A2A
>(func
, arg1
, arg2
));
379 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
, typename A3A
, typename A3B
>
380 inline void DoNotify(void (*func
)(A1A
, A2A
, A3A
), A1B arg1
, A2B arg2
, A3B arg3
) {
381 HandleNotification(CMuleNotifier3
<A1A
, A2A
, A3A
>(func
, arg1
, arg2
, arg3
));
387 //! Placing CMuleGUIEvent in the global namespace.
388 using MuleNotify::CMuleGUIEvent
;
390 //! The event-handler type that takes a CMuleGUIEvent.
391 typedef void (wxEvtHandler::*MuleNotifyEventFunction
)(CMuleGUIEvent
&);
393 //! Event-handler for completed hashings of new shared files and partfiles.
394 #define EVT_MULE_NOTIFY(func) \
395 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_NOTIFY, -1, -1, \
396 (wxObjectEventFunction) (wxEventFunction) \
397 wxStaticCastEvent(MuleNotifyEventFunction, &func), (wxObject*) NULL),
403 #define Notify_SharedFilesShowFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFile, file)
404 #define Notify_SharedFilesRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveFile, file)
405 #define Notify_SharedFilesRemoveAllItems() MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveAllFiles)
406 #define Notify_SharedFilesShowFileList() MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFileList)
407 #define Notify_SharedFilesSort() MuleNotify::DoNotify(&MuleNotify::SharedFilesSort)
408 #define Notify_SharedFilesUpdateItem(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesUpdateItem, file)
411 #define Notify_DownloadCtrlUpdateItem(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlUpdateItem, ptr)
412 #define Notify_DownloadCtrlAddFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddFile, file)
413 #define Notify_DownloadCtrlAddSource(p0, p1, val) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddSource, p0, p1, val)
414 #define Notify_DownloadCtrlRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveFile, file)
415 #define Notify_DownloadCtrlRemoveSource(ptr0, ptr1) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveSource, ptr0, ptr1)
416 #define Notify_DownloadCtrlHideSource(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlHideSource, ptr)
417 #define Notify_DownloadCtrlSort() MuleNotify::DoNotify(&MuleNotify::DownloadCtrlSort)
420 #define Notify_UploadCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtUploading)
421 #define Notify_UploadCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtUploading)
422 #define Notify_UploadCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtUploading)
425 #define Notify_ClientCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtClients)
426 #define Notify_ClientCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtClients)
427 #define Notify_ClientCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtClients)
430 #define Notify_QlistAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtQueued)
431 #define Notify_QlistRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtQueued)
432 #define Notify_QlistRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtQueued)
435 #define Notify_ServerAdd(ptr) MuleNotify::DoNotify(&MuleNotify::ServerAdd, ptr)
436 #define Notify_ServerRemove(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRemove, ptr)
437 #define Notify_ServerRemoveDead() MuleNotify::DoNotify(&MuleNotify::ServerRemoveDead)
438 #define Notify_ServerRemoveAll() MuleNotify::DoNotify(&MuleNotify::ServerRemoveAll)
439 #define Notify_ServerHighlight(ptr, val) MuleNotify::DoNotify(&MuleNotify::ServerHighlight, ptr, val)
440 #define Notify_ServerRefresh(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRefresh, ptr)
441 #define Notify_ServerFreeze() MuleNotify::DoNotify(&MuleNotify::ServerFreeze)
442 #define Notify_ServerThaw() MuleNotify::DoNotify(&MuleNotify::ServerThaw)
443 #define Notify_ServerUpdateED2KInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateED2KInfo)
444 #define Notify_ServerUpdateKadKInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateKadKInfo)
447 #define Notify_SearchCancel() MuleNotify::DoNotify(&MuleNotify::SearchCancel)
448 #define Notify_SearchLocalEnd() MuleNotify::DoNotify(&MuleNotify::SearchLocalEnd)
449 #define Notify_KadSearchEnd(val) MuleNotify::DoNotify(&MuleNotify::KadSearchEnd, val)
450 #define Notify_Search_Update_Sources(ptr) MuleNotify::DoNotify(&MuleNotify::Search_Update_Sources, ptr)
451 #define Notify_Search_Add_Result(s) MuleNotify::DoNotify(&MuleNotify::Search_Add_Result, s)
454 #define Notify_ChatRefreshFriend(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatRefreshFriend, val0, val1, s)
455 #define Notify_ChatConnResult(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatConnResult, val0, val1, s)
456 #define Notify_ChatProcessMsg(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatProcessMsg, val0, s)
459 #define Notify_ShowConnState(val) MuleNotify::DoNotify(&MuleNotify::ShowConnState, val)
460 #define Notify_ShowUserCount(str) MuleNotify::DoNotify(&MuleNotify::ShowUserCount, str)
461 #define Notify_ShowQueueCount(val) MuleNotify::DoNotify(&MuleNotify::ShowQueueCount, val)
462 #define Notify_ShowUpdateCatTabTitles() MuleNotify::DoNotify(&MuleNotify::ShowUpdateCatTabTitles)
463 #define Notify_ShowGUI() MuleNotify::DoNotify(&MuleNotify::ShowGUI)
466 #define Notify_CategoryAdded() MuleNotify::DoNotify(&MuleNotify::CategoryAdded)
467 #define Notify_CategoryUpdate(cat) MuleNotify::DoNotify(&MuleNotify::CategoryUpdate, cat)
468 #define Notify_CategoryDelete(cat) MuleNotify::DoNotify(&MuleNotify::CategoryDelete, cat)
470 // server.met/nodes.dat default urls
471 #define Notify_NodesURLChanged(url) MuleNotify::DoNotify(&MuleNotify::NodesURLChanged, url)
472 #define Notify_ServersURLChanged(url) MuleNotify::DoNotify(&MuleNotify::ServersURLChanged, url)
476 // GUI -> core notification
480 #define CoreNotify_PartFile_Swap_A4AF(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF, ptr)
481 #define CoreNotify_PartFile_Swap_A4AF_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Auto, ptr)
482 #define CoreNotify_PartFile_Swap_A4AF_Others(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Others, ptr)
483 #define CoreNotify_PartFile_Pause(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Pause, ptr)
484 #define CoreNotify_PartFile_Resume(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Resume, ptr)
485 #define CoreNotify_PartFile_Stop(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Stop, ptr)
486 #define CoreNotify_PartFile_PrioAuto(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioAuto, ptr, val)
487 #define CoreNotify_PartFile_PrioSet(p, v0, v1) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioSet, p, v0, v1)
488 #define CoreNotify_PartFile_Delete(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Delete, ptr)
489 #define CoreNotify_PartFile_SetCat(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_SetCat, ptr, val)
492 #define CoreNotify_KnownFile_Up_Prio_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Set, ptr, val)
493 #define CoreNotify_KnownFile_Up_Prio_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Auto, ptr)
494 #define CoreNotify_KnownFile_Comment_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Comment_Set, ptr, val)
497 #define CoreNotify_Search_Add_Download(ptr, val) MuleNotify::DoNotify(&MuleNotify::Search_Add_Download, ptr, val)
498 #define CoreNotify_Search_Update_Progress(val) MuleNotify::DoNotify(&MuleNotify::Search_Update_Progress, val)
501 #define CoreNotify_Download_Set_Cat_Prio(cat, pri) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Prio, cat, pri)
502 #define CoreNotify_Download_Set_Cat_Status(cat, st) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Status, cat, st)
504 #endif // __GUIEVENTS_H__
506 // File_checked_for_headers