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"
34 #define __need_convertinfo // We need only the ConvertInfo struct from PartFileConvert.h
35 #include "PartFileConvert.h"
44 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_NOTIFY
, -1)
48 * This namespaces contains a number of functions and classes
49 * related to defered function calls, allowing a notification
50 * call to be delayed till it can be initiated from the main
56 * Creates a deep copy of the object passed.
58 * Note that this function should be overwritten as
59 * needed. See the wxString version below.
61 template <class ValueType
>
62 inline ValueType
DeepCopy(const ValueType
& value
)
64 return ValueType(value
);
67 /** Special DeepCopy for wxString, which uses reference counting. */
68 inline wxString
DeepCopy(const wxString
& value
)
70 return wxString(value
.c_str(), value
.Length());
74 ////////////////////////////////////////////////////////////
75 // Notification handlers
77 // These functions should not be called directly, but
78 // through the Notify_*, etc. macros.
81 void SharedFilesShowFile(CKnownFile
* file
);
82 void SharedFilesRemoveFile(CKnownFile
* file
);
83 void SharedFilesRemoveAllFiles();
84 void SharedFilesShowFileList();
85 void SharedFilesUpdateItem(CKnownFile
* file
);
87 void DownloadCtrlUpdateItem(const void* item
);
88 void SourceCtrlUpdateSource(CUpDownClient
* source
, SourceItemType type
);
89 void DownloadCtrlAddFile(CPartFile
* file
);
90 void SourceCtrlAddSource(CPartFile
* owner
, CUpDownClient
* source
, SourceItemType type
);
91 void DownloadCtrlRemoveFile(CPartFile
* file
);
92 void SourceCtrlRemoveSource(const CUpDownClient
* source
, const CPartFile
* owner
);
93 void DownloadCtrlHideSource(CPartFile
* file
);
94 void DownloadCtrlSort();
96 void SharedCtrlAddClient(CKnownFile
* owner
, CUpDownClient
* client
, SourceItemType type
);
97 void SharedCtrlRefreshClient(CUpDownClient
* client
, SourceItemType type
);
98 void SharedCtrlRemoveClient(const CKnownFile
* owner
, const CUpDownClient
* client
);
100 void ServerAdd(CServer
* server
);
101 void ServerRemove(CServer
* server
);
102 void ServerRemoveDead();
103 void ServerRemoveAll();
104 void ServerHighlight(CServer
* server
, bool highlight
);
105 void ServerRefresh(CServer
* server
);
108 void ServerUpdateED2KInfo();
109 void ServerUpdateKadKInfo();
112 void SearchLocalEnd();
113 void KadSearchEnd(uint32 id
);
114 void Search_Update_Sources(CSearchFile
* result
);
115 void Search_Add_Result(CSearchFile
* result
);
117 void ChatRefreshFriend(CFriend
* Friend
, bool connected
);
118 void ChatConnResult(bool success
, uint64 id
, wxString message
);
119 void ChatProcessMsg(uint64 sender
, wxString message
);
120 void ChatSendCaptcha(wxString captcha
, uint64 to_id
);
122 void ShowConnState(long state
);
123 void ShowUserCount(wxString str
);
124 void ShowQueueCount(uint32 count
);
125 void ShowUpdateCatTabTitles();
128 void CategoryAdded();
129 void CategoryUpdate(uint32 cat
);
130 void CategoryDelete(uint32 cat
);
132 void NodesURLChanged(wxString url
);
133 void ServersURLChanged(wxString url
);
135 // Partfile conversion: Core -> GUI
136 void ConvertUpdateProgress(float percent
, wxString label
, wxString header
);
137 void ConvertUpdateJobInfo(ConvertInfo info
);
138 void ConvertRemoveJobInfo(unsigned id
);
139 void ConvertClearInfos();
140 // Partfile conversion: GUI -> Core
141 void ConvertRemoveJob(unsigned id
);
142 void ConvertRetryJob(unsigned id
);
143 void ConvertReaddAllJobs();
146 // GUI -> core notification
149 void PartFile_Swap_A4AF(CPartFile
* file
);
150 void PartFile_Swap_A4AF_Auto(CPartFile
* file
);
151 void PartFile_Swap_A4AF_Others(CPartFile
* file
);
152 void PartFile_Pause(CPartFile
* file
);
153 void PartFile_Resume(CPartFile
* file
);
154 void PartFile_Stop(CPartFile
* file
);
155 void PartFile_PrioAuto(CPartFile
* file
, bool val
);
156 void PartFile_PrioSet(CPartFile
* file
, uint8 newDownPriority
, bool bSave
);
157 void PartFile_Delete(CPartFile
* file
);
158 void PartFile_SetCat(CPartFile
* file
, uint32 val
);
160 void KnownFile_Up_Prio_Set(CKnownFile
* file
, uint8 val
);
161 void KnownFile_Up_Prio_Auto(CKnownFile
* file
);
162 void KnownFile_Comment_Set(CKnownFile
* file
, wxString comment
);
164 void Search_Add_Download(CSearchFile
* result
, uint8 category
);
165 void Search_Update_Progress(uint32 value
);
167 void Download_Set_Cat_Prio(uint8 cat
, uint8 newprio
);
168 void Download_Set_Cat_Status(uint8 cat
, int newstatus
);
171 ////////////////////////////////////////////////////////////
172 // Notification utilities
175 * The base class of the functions.
177 * This class allows the the notification call to be executed
178 * without knowing the exact specifics of a given functor.
180 class CMuleNotiferBase
183 /** The constructor does nothing. */
184 CMuleNotiferBase() {};
185 /** The destructor is virtual since we will be deleting pointers to this type. */
186 virtual ~CMuleNotiferBase() {};
188 /** Executes the actual notification call. */
189 virtual void Notify() const = 0;
190 /** Returns a copy of the functor (function + arguments). */
191 virtual CMuleNotiferBase
* Clone() const = 0;
195 /** Notification functor for functions taking no arguments. */
196 class CMuleNotifier0
: public CMuleNotiferBase
199 typedef void (*FuncType
)();
201 /** Creates a functor from the given function. */
202 CMuleNotifier0(FuncType func
)
205 /** @see CMuleNotifierBase::Notify */
206 virtual void Notify() const {
210 /** @see CMuleNotifierBase::Clone */
211 virtual CMuleNotiferBase
* Clone() const {
212 return new CMuleNotifier0(m_func
);
220 /** Notification functor for functions taking 1 arguments. */
221 template <typename ARG
>
222 class CMuleNotifier1
: public CMuleNotiferBase
225 typedef void (*FuncType
)(ARG
);
227 /** Creates a functor from the given function and arguments. */
228 CMuleNotifier1(FuncType func
, ARG arg
)
233 /** @see CMuleNotifierBase::Notify */
234 virtual void Notify() const {
238 /** @see CMuleNotifierBase::Clone */
239 virtual CMuleNotiferBase
* Clone() const {
240 return new CMuleNotifier1
<ARG
>(m_func
, m_arg
);
249 /** Notification functor for functions taking 2 arguments. */
250 template <typename ARG_1
, typename ARG_2
>
251 class CMuleNotifier2
: public CMuleNotiferBase
254 typedef void (*FuncType
)(ARG_1
, ARG_2
);
256 /** Creates a functor from the given function and arguments. */
257 CMuleNotifier2(FuncType func
, ARG_1 arg1
, ARG_2 arg2
)
259 m_arg1(DeepCopy(arg1
)),
260 m_arg2(DeepCopy(arg2
))
263 /** @see CMuleNotifierBase:: Notify */
264 virtual void Notify() const {
265 m_func(m_arg1
, m_arg2
);
268 /** @see CMuleNotifierBase::Clone */
269 virtual CMuleNotiferBase
* Clone() const {
270 return new CMuleNotifier2
<ARG_1
, ARG_2
>(m_func
, m_arg1
, m_arg2
);
280 /** Notification functor for functions taking 3 arguments. */
281 template <typename ARG_1
, typename ARG_2
, typename ARG_3
>
282 class CMuleNotifier3
: public CMuleNotiferBase
285 typedef void (*FuncType
)(ARG_1
, ARG_2
, ARG_3
);
287 /** Creates a functor from the given function and arguments. */
288 CMuleNotifier3(FuncType func
, ARG_1 arg1
, ARG_2 arg2
, ARG_3 arg3
)
290 m_arg1(DeepCopy(arg1
)),
291 m_arg2(DeepCopy(arg2
)),
292 m_arg3(DeepCopy(arg3
))
295 /** @see CMuleNotifierBase:: Notify */
296 virtual void Notify() const {
297 m_func(m_arg1
, m_arg2
, m_arg3
);
300 /** @see CMuleNotifierBase::Clone */
301 virtual CMuleNotiferBase
* Clone() const {
302 return new CMuleNotifier3
<ARG_1
, ARG_2
, ARG_3
>(m_func
, m_arg1
, m_arg2
, m_arg3
);
314 * This event is sent when a worker-thread makes use of a notify-macro.
316 * This insures that all notifications are executed on the main thread,
317 * thereby improving overall threadsafety. The events are currently
320 class CMuleGUIEvent
: public wxEvent
323 /** Takes ownership a notifier functor. */
324 CMuleGUIEvent(CMuleNotiferBase
* ntf
)
325 : wxEvent(-1, MULE_EVT_NOTIFY
)
331 /** Destructor, frees the functor object. */
332 virtual ~CMuleGUIEvent() {
336 /** Executes the notification. */
337 void Notify() const {
341 /** @see wxEvent::Clone */
342 virtual wxEvent
* Clone() const {
343 return new CMuleGUIEvent(m_functor
->Clone());
348 CMuleGUIEvent(const CMuleGUIEvent
&);
349 /** Not assignable. */
350 CMuleGUIEvent
& operator=(const CMuleGUIEvent
&);
352 //! The actual functor object,
353 CMuleNotiferBase
* m_functor
;
358 * This function will execute or queue a given notification functor.
360 * If the caller is the main thread, the functor is executed immediatly,
361 * thus acting like a regular function call. OTOH, if the caller is a
362 * worker thread, the functor is cloned and sent via an event to
365 void HandleNotification(const CMuleNotiferBase
& ntf
);
368 * These functions take a function pointer and a set of arguments,
369 * matching those of the function-pointer. A functor is created
370 * from these and either executed immediatly, or sent as an event
371 * in the case of non-main threads calling the functions.
373 * Note that the return-value of the function must be void.
375 * IMPORTANT: Note that the functions passed to DoNotify must not
376 * take arguments via references, since this causes the functors
377 * to store references to the arguments, rather than a copy and
378 * thus ends up with dangling references.
381 inline void DoNotify(void (*func
)()) {
382 HandleNotification(CMuleNotifier0(func
));
384 template <typename A1A
, typename A1B
>
385 inline void DoNotify(void (*func
)(A1A
), A1B arg1
) {
386 HandleNotification(CMuleNotifier1
<A1A
>(func
, arg1
));
388 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
>
389 inline void DoNotify(void (*func
)(A1A
, A2A
), A1B arg1
, A2B arg2
) {
390 HandleNotification(CMuleNotifier2
<A1A
, A2A
>(func
, arg1
, arg2
));
392 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
, typename A3A
, typename A3B
>
393 inline void DoNotify(void (*func
)(A1A
, A2A
, A3A
), A1B arg1
, A2B arg2
, A3B arg3
) {
394 HandleNotification(CMuleNotifier3
<A1A
, A2A
, A3A
>(func
, arg1
, arg2
, arg3
));
400 //! Placing CMuleGUIEvent in the global namespace.
401 using MuleNotify::CMuleGUIEvent
;
403 //! The event-handler type that takes a CMuleGUIEvent.
404 typedef void (wxEvtHandler::*MuleNotifyEventFunction
)(CMuleGUIEvent
&);
406 //! Event-handler for completed hashings of new shared files and partfiles.
407 #define EVT_MULE_NOTIFY(func) \
408 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_NOTIFY, -1, -1, \
409 (wxObjectEventFunction) (wxEventFunction) \
410 wxStaticCastEvent(MuleNotifyEventFunction, &func), (wxObject*) NULL),
416 #define Notify_SharedFilesShowFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFile, file)
417 #define Notify_SharedFilesRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveFile, file)
418 #define Notify_SharedFilesRemoveAllItems() MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveAllFiles)
419 #define Notify_SharedFilesShowFileList() MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFileList)
420 #define Notify_SharedFilesSort() MuleNotify::DoNotify(&MuleNotify::SharedFilesSort)
421 #define Notify_SharedFilesUpdateItem(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesUpdateItem, file)
424 #define Notify_DownloadCtrlUpdateItem(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlUpdateItem, ptr)
425 #define Notify_DownloadCtrlAddFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddFile, file)
426 #define Notify_DownloadCtrlRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveFile, file)
427 #define Notify_DownloadCtrlSort() MuleNotify::DoNotify(&MuleNotify::DownloadCtrlSort)
430 #define Notify_SourceCtrlUpdateSource(ptr, val) MuleNotify::DoNotify(&MuleNotify::SourceCtrlUpdateSource, ptr, val)
431 #define Notify_SourceCtrlAddSource(p0, p1, val) MuleNotify::DoNotify(&MuleNotify::SourceCtrlAddSource, p0, p1, val)
432 #define Notify_SourceCtrlRemoveSource(ptr0, ptr1) MuleNotify::DoNotify(&MuleNotify::SourceCtrlRemoveSource, ptr0, ptr1)
435 #define Notify_SharedCtrlAddClient(p0, p1, val) MuleNotify::DoNotify(&MuleNotify::SharedCtrlAddClient, p0, p1, val)
436 #define Notify_SharedCtrlRefreshClient(ptr, val) MuleNotify::DoNotify(&MuleNotify::SharedCtrlRefreshClient, ptr, val)
437 #define Notify_SharedCtrlRemoveClient(p0, p1) MuleNotify::DoNotify(&MuleNotify::SharedCtrlRemoveClient, p0, p1)
440 #define Notify_ServerAdd(ptr) MuleNotify::DoNotify(&MuleNotify::ServerAdd, ptr)
441 #define Notify_ServerRemove(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRemove, ptr)
442 #define Notify_ServerRemoveDead() MuleNotify::DoNotify(&MuleNotify::ServerRemoveDead)
443 #define Notify_ServerRemoveAll() MuleNotify::DoNotify(&MuleNotify::ServerRemoveAll)
444 #define Notify_ServerHighlight(ptr, val) MuleNotify::DoNotify(&MuleNotify::ServerHighlight, ptr, val)
445 #define Notify_ServerRefresh(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRefresh, ptr)
446 #define Notify_ServerFreeze() MuleNotify::DoNotify(&MuleNotify::ServerFreeze)
447 #define Notify_ServerThaw() MuleNotify::DoNotify(&MuleNotify::ServerThaw)
448 #define Notify_ServerUpdateED2KInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateED2KInfo)
449 #define Notify_ServerUpdateKadKInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateKadKInfo)
452 #define Notify_SearchCancel() MuleNotify::DoNotify(&MuleNotify::SearchCancel)
453 #define Notify_SearchLocalEnd() MuleNotify::DoNotify(&MuleNotify::SearchLocalEnd)
454 #define Notify_KadSearchEnd(val) MuleNotify::DoNotify(&MuleNotify::KadSearchEnd, val)
455 #define Notify_Search_Update_Sources(ptr) MuleNotify::DoNotify(&MuleNotify::Search_Update_Sources, ptr)
456 #define Notify_Search_Add_Result(s) MuleNotify::DoNotify(&MuleNotify::Search_Add_Result, s)
459 #define Notify_ChatRefreshFriend(ptr, val) MuleNotify::DoNotify(&MuleNotify::ChatRefreshFriend, ptr, val)
460 #define Notify_ChatConnResult(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatConnResult, val0, val1, s)
461 #define Notify_ChatProcessMsg(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatProcessMsg, val0, s)
462 #define Notify_ChatSendCaptcha(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatSendCaptcha, val0, s)
465 #define Notify_ShowConnState(val) MuleNotify::DoNotify(&MuleNotify::ShowConnState, val)
466 #define Notify_ShowUserCount(str) MuleNotify::DoNotify(&MuleNotify::ShowUserCount, str)
467 #define Notify_ShowQueueCount(val) MuleNotify::DoNotify(&MuleNotify::ShowQueueCount, val)
468 #define Notify_ShowUpdateCatTabTitles() MuleNotify::DoNotify(&MuleNotify::ShowUpdateCatTabTitles)
469 #define Notify_ShowGUI() MuleNotify::DoNotify(&MuleNotify::ShowGUI)
472 #define Notify_CategoryAdded() MuleNotify::DoNotify(&MuleNotify::CategoryAdded)
473 #define Notify_CategoryUpdate(cat) MuleNotify::DoNotify(&MuleNotify::CategoryUpdate, cat)
474 #define Notify_CategoryDelete(cat) MuleNotify::DoNotify(&MuleNotify::CategoryDelete, cat)
476 // server.met/nodes.dat default urls
477 #define Notify_NodesURLChanged(url) MuleNotify::DoNotify(&MuleNotify::NodesURLChanged, url)
478 #define Notify_ServersURLChanged(url) MuleNotify::DoNotify(&MuleNotify::ServersURLChanged, url)
480 // Partfile conversion: Core -> GUI
481 #define Notify_ConvertUpdateProgress(val, text) Notify_ConvertUpdateProgressFull(val, text, wxEmptyString)
482 #define Notify_ConvertUpdateProgressFull(val, text, hdr) MuleNotify::DoNotify(&MuleNotify::ConvertUpdateProgress, val, text, hdr)
483 #define Notify_ConvertUpdateJobInfo(info) MuleNotify::DoNotify(&MuleNotify::ConvertUpdateJobInfo, info)
484 #define Notify_ConvertRemoveJobInfo(id) MuleNotify::DoNotify(&MuleNotify::ConvertRemoveJobInfo, id)
485 #define Notify_ConvertClearInfos() MuleNotify::DoNotify(&MuleNotify::ConvertClearInfos)
486 // Partfile conversion: GUI -> Core
487 #define Notify_ConvertRemoveJob(id) MuleNotify::DoNotify(&MuleNotify::ConvertRemoveJob, id)
488 #define Notify_ConvertRetryJob(id) MuleNotify::DoNotify(&MuleNotify::ConvertRetryJob, id)
489 #define Notify_ConvertReaddAllJobs() MuleNotify::DoNotify(&MuleNotify::ConvertReaddAllJobs)
492 // GUI -> core notification
496 #define CoreNotify_PartFile_Swap_A4AF(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF, ptr)
497 #define CoreNotify_PartFile_Swap_A4AF_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Auto, ptr)
498 #define CoreNotify_PartFile_Swap_A4AF_Others(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Others, ptr)
499 #define CoreNotify_PartFile_Pause(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Pause, ptr)
500 #define CoreNotify_PartFile_Resume(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Resume, ptr)
501 #define CoreNotify_PartFile_Stop(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Stop, ptr)
502 #define CoreNotify_PartFile_PrioAuto(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioAuto, ptr, val)
503 #define CoreNotify_PartFile_PrioSet(p, v0, v1) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioSet, p, v0, v1)
504 #define CoreNotify_PartFile_Delete(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Delete, ptr)
505 #define CoreNotify_PartFile_SetCat(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_SetCat, ptr, val)
508 #define CoreNotify_KnownFile_Up_Prio_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Set, ptr, val)
509 #define CoreNotify_KnownFile_Up_Prio_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Auto, ptr)
510 #define CoreNotify_KnownFile_Comment_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Comment_Set, ptr, val)
513 #define CoreNotify_Search_Add_Download(ptr, val) MuleNotify::DoNotify(&MuleNotify::Search_Add_Download, ptr, val)
514 #define CoreNotify_Search_Update_Progress(val) MuleNotify::DoNotify(&MuleNotify::Search_Update_Progress, val)
517 #define CoreNotify_Download_Set_Cat_Prio(cat, pri) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Prio, cat, pri)
518 #define CoreNotify_Download_Set_Cat_Status(cat, st) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Status, cat, st)
520 #endif // __GUIEVENTS_H__
522 // File_checked_for_headers