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 DownloadCtrlAddFile(CPartFile
* file
);
89 void DownloadCtrlAddSource(CPartFile
* owner
, CUpDownClient
* source
, DownloadItemType type
);
90 void DownloadCtrlRemoveFile(CPartFile
* file
);
91 void DownloadCtrlRemoveSource(const CUpDownClient
* source
, const CPartFile
* owner
);
92 void DownloadCtrlHideSource(CPartFile
* file
);
93 void DownloadCtrlSort();
95 void ClientCtrlAddClient(CUpDownClient
* client
, ViewType type
);
96 void ClientCtrlRefreshClient(CUpDownClient
* client
, ViewType type
);
97 void ClientCtrlRemoveClient(CUpDownClient
* client
, ViewType type
);
99 void ServerAdd(CServer
* server
);
100 void ServerRemove(CServer
* server
);
101 void ServerRemoveDead();
102 void ServerRemoveAll();
103 void ServerHighlight(CServer
* server
, bool highlight
);
104 void ServerRefresh(CServer
* server
);
107 void ServerUpdateED2KInfo();
108 void ServerUpdateKadKInfo();
111 void SearchLocalEnd();
112 void KadSearchEnd(uint32 id
);
113 void Search_Update_Sources(CSearchFile
* result
);
114 void Search_Add_Result(CSearchFile
* result
);
116 void ChatRefreshFriend(CFriend
* Friend
, bool connected
);
117 void ChatConnResult(bool success
, uint64 id
, wxString message
);
118 void ChatProcessMsg(uint64 sender
, wxString message
);
119 void ChatSendCaptcha(wxString captcha
, uint64 to_id
);
121 void ShowConnState(long state
);
122 void ShowUserCount(wxString str
);
123 void ShowQueueCount(uint32 count
);
124 void ShowUpdateCatTabTitles();
127 void CategoryAdded();
128 void CategoryUpdate(uint32 cat
);
129 void CategoryDelete(uint32 cat
);
131 void NodesURLChanged(wxString url
);
132 void ServersURLChanged(wxString url
);
134 // Partfile conversion: Core -> GUI
135 void ConvertUpdateProgress(float percent
, wxString label
, wxString header
);
136 void ConvertUpdateJobInfo(ConvertInfo info
);
137 void ConvertRemoveJobInfo(unsigned id
);
138 void ConvertClearInfos();
139 // Partfile conversion: GUI -> Core
140 void ConvertRemoveJob(unsigned id
);
141 void ConvertRetryJob(unsigned id
);
142 void ConvertReaddAllJobs();
145 // GUI -> core notification
148 void PartFile_Swap_A4AF(CPartFile
* file
);
149 void PartFile_Swap_A4AF_Auto(CPartFile
* file
);
150 void PartFile_Swap_A4AF_Others(CPartFile
* file
);
151 void PartFile_Pause(CPartFile
* file
);
152 void PartFile_Resume(CPartFile
* file
);
153 void PartFile_Stop(CPartFile
* file
);
154 void PartFile_PrioAuto(CPartFile
* file
, bool val
);
155 void PartFile_PrioSet(CPartFile
* file
, uint8 newDownPriority
, bool bSave
);
156 void PartFile_Delete(CPartFile
* file
);
157 void PartFile_SetCat(CPartFile
* file
, uint32 val
);
159 void KnownFile_Up_Prio_Set(CKnownFile
* file
, uint8 val
);
160 void KnownFile_Up_Prio_Auto(CKnownFile
* file
);
161 void KnownFile_Comment_Set(CKnownFile
* file
, wxString comment
);
163 void Search_Add_Download(CSearchFile
* result
, uint8 category
);
164 void Search_Update_Progress(uint32 value
);
166 void Download_Set_Cat_Prio(uint8 cat
, uint8 newprio
);
167 void Download_Set_Cat_Status(uint8 cat
, int newstatus
);
170 ////////////////////////////////////////////////////////////
171 // Notification utilities
174 * The base class of the functions.
176 * This class allows the the notification call to be executed
177 * without knowing the exact specifics of a given functor.
179 class CMuleNotiferBase
182 /** The constructor does nothing. */
183 CMuleNotiferBase() {};
184 /** The destructor is virtual since we will be deleting pointers to this type. */
185 virtual ~CMuleNotiferBase() {};
187 /** Executes the actual notification call. */
188 virtual void Notify() const = 0;
189 /** Returns a copy of the functor (function + arguments). */
190 virtual CMuleNotiferBase
* Clone() const = 0;
194 /** Notification functor for functions taking no arguments. */
195 class CMuleNotifier0
: public CMuleNotiferBase
198 typedef void (*FuncType
)();
200 /** Creates a functor from the given function. */
201 CMuleNotifier0(FuncType func
)
204 /** @see CMuleNotifierBase::Notify */
205 virtual void Notify() const {
209 /** @see CMuleNotifierBase::Clone */
210 virtual CMuleNotiferBase
* Clone() const {
211 return new CMuleNotifier0(m_func
);
219 /** Notification functor for functions taking 1 arguments. */
220 template <typename ARG
>
221 class CMuleNotifier1
: public CMuleNotiferBase
224 typedef void (*FuncType
)(ARG
);
226 /** Creates a functor from the given function and arguments. */
227 CMuleNotifier1(FuncType func
, ARG arg
)
232 /** @see CMuleNotifierBase::Notify */
233 virtual void Notify() const {
237 /** @see CMuleNotifierBase::Clone */
238 virtual CMuleNotiferBase
* Clone() const {
239 return new CMuleNotifier1
<ARG
>(m_func
, m_arg
);
248 /** Notification functor for functions taking 2 arguments. */
249 template <typename ARG_1
, typename ARG_2
>
250 class CMuleNotifier2
: public CMuleNotiferBase
253 typedef void (*FuncType
)(ARG_1
, ARG_2
);
255 /** Creates a functor from the given function and arguments. */
256 CMuleNotifier2(FuncType func
, ARG_1 arg1
, ARG_2 arg2
)
258 m_arg1(DeepCopy(arg1
)),
259 m_arg2(DeepCopy(arg2
))
262 /** @see CMuleNotifierBase:: Notify */
263 virtual void Notify() const {
264 m_func(m_arg1
, m_arg2
);
267 /** @see CMuleNotifierBase::Clone */
268 virtual CMuleNotiferBase
* Clone() const {
269 return new CMuleNotifier2
<ARG_1
, ARG_2
>(m_func
, m_arg1
, m_arg2
);
279 /** Notification functor for functions taking 3 arguments. */
280 template <typename ARG_1
, typename ARG_2
, typename ARG_3
>
281 class CMuleNotifier3
: public CMuleNotiferBase
284 typedef void (*FuncType
)(ARG_1
, ARG_2
, ARG_3
);
286 /** Creates a functor from the given function and arguments. */
287 CMuleNotifier3(FuncType func
, ARG_1 arg1
, ARG_2 arg2
, ARG_3 arg3
)
289 m_arg1(DeepCopy(arg1
)),
290 m_arg2(DeepCopy(arg2
)),
291 m_arg3(DeepCopy(arg3
))
294 /** @see CMuleNotifierBase:: Notify */
295 virtual void Notify() const {
296 m_func(m_arg1
, m_arg2
, m_arg3
);
299 /** @see CMuleNotifierBase::Clone */
300 virtual CMuleNotiferBase
* Clone() const {
301 return new CMuleNotifier3
<ARG_1
, ARG_2
, ARG_3
>(m_func
, m_arg1
, m_arg2
, m_arg3
);
313 * This event is sent when a worker-thread makes use of a notify-macro.
315 * This insures that all notifications are executed on the main thread,
316 * thereby improving overall threadsafety. The events are currently
319 class CMuleGUIEvent
: public wxEvent
322 /** Takes ownership a notifier functor. */
323 CMuleGUIEvent(CMuleNotiferBase
* ntf
)
324 : wxEvent(-1, MULE_EVT_NOTIFY
)
330 /** Destructor, frees the functor object. */
331 virtual ~CMuleGUIEvent() {
335 /** Executes the notification. */
336 void Notify() const {
340 /** @see wxEvent::Clone */
341 virtual wxEvent
* Clone() const {
342 return new CMuleGUIEvent(m_functor
->Clone());
347 CMuleGUIEvent(const CMuleGUIEvent
&);
348 /** Not assignable. */
349 CMuleGUIEvent
& operator=(const CMuleGUIEvent
&);
351 //! The actual functor object,
352 CMuleNotiferBase
* m_functor
;
357 * This function will execute or queue a given notification functor.
359 * If the caller is the main thread, the functor is executed immediatly,
360 * thus acting like a regular function call. OTOH, if the caller is a
361 * worker thread, the functor is cloned and sent via an event to
364 void HandleNotification(const CMuleNotiferBase
& ntf
);
367 * These functions take a function pointer and a set of arguments,
368 * matching those of the function-pointer. A functor is created
369 * from these and either executed immediatly, or sent as an event
370 * in the case of non-main threads calling the functions.
372 * Note that the return-value of the function must be void.
374 * IMPORTANT: Note that the functions passed to DoNotify must not
375 * take arguments via references, since this causes the functors
376 * to store references to the arguments, rather than a copy and
377 * thus ends up with dangling references.
380 inline void DoNotify(void (*func
)()) {
381 HandleNotification(CMuleNotifier0(func
));
383 template <typename A1A
, typename A1B
>
384 inline void DoNotify(void (*func
)(A1A
), A1B arg1
) {
385 HandleNotification(CMuleNotifier1
<A1A
>(func
, arg1
));
387 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
>
388 inline void DoNotify(void (*func
)(A1A
, A2A
), A1B arg1
, A2B arg2
) {
389 HandleNotification(CMuleNotifier2
<A1A
, A2A
>(func
, arg1
, arg2
));
391 template <typename A1A
, typename A1B
, typename A2A
, typename A2B
, typename A3A
, typename A3B
>
392 inline void DoNotify(void (*func
)(A1A
, A2A
, A3A
), A1B arg1
, A2B arg2
, A3B arg3
) {
393 HandleNotification(CMuleNotifier3
<A1A
, A2A
, A3A
>(func
, arg1
, arg2
, arg3
));
399 //! Placing CMuleGUIEvent in the global namespace.
400 using MuleNotify::CMuleGUIEvent
;
402 //! The event-handler type that takes a CMuleGUIEvent.
403 typedef void (wxEvtHandler::*MuleNotifyEventFunction
)(CMuleGUIEvent
&);
405 //! Event-handler for completed hashings of new shared files and partfiles.
406 #define EVT_MULE_NOTIFY(func) \
407 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_NOTIFY, -1, -1, \
408 (wxObjectEventFunction) (wxEventFunction) \
409 wxStaticCastEvent(MuleNotifyEventFunction, &func), (wxObject*) NULL),
415 #define Notify_SharedFilesShowFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFile, file)
416 #define Notify_SharedFilesRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveFile, file)
417 #define Notify_SharedFilesRemoveAllItems() MuleNotify::DoNotify(&MuleNotify::SharedFilesRemoveAllFiles)
418 #define Notify_SharedFilesShowFileList() MuleNotify::DoNotify(&MuleNotify::SharedFilesShowFileList)
419 #define Notify_SharedFilesSort() MuleNotify::DoNotify(&MuleNotify::SharedFilesSort)
420 #define Notify_SharedFilesUpdateItem(file) MuleNotify::DoNotify(&MuleNotify::SharedFilesUpdateItem, file)
423 #define Notify_DownloadCtrlUpdateItem(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlUpdateItem, ptr)
424 #define Notify_DownloadCtrlAddFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddFile, file)
425 #define Notify_DownloadCtrlAddSource(p0, p1, val) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlAddSource, p0, p1, val)
426 #define Notify_DownloadCtrlRemoveFile(file) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveFile, file)
427 #define Notify_DownloadCtrlRemoveSource(ptr0, ptr1) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlRemoveSource, ptr0, ptr1)
428 #define Notify_DownloadCtrlHideSource(ptr) MuleNotify::DoNotify(&MuleNotify::DownloadCtrlHideSource, ptr)
429 #define Notify_DownloadCtrlSort() MuleNotify::DoNotify(&MuleNotify::DownloadCtrlSort)
432 #define Notify_UploadCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtUploading)
433 #define Notify_UploadCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtUploading)
434 #define Notify_UploadCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtUploading)
437 #define Notify_ClientCtrlAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtClients)
438 #define Notify_ClientCtrlRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtClients)
439 #define Notify_ClientCtrlRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtClients)
442 #define Notify_QlistAddClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlAddClient, ptr, vtQueued)
443 #define Notify_QlistRefreshClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRefreshClient, ptr, vtQueued)
444 #define Notify_QlistRemoveClient(ptr) MuleNotify::DoNotify(&MuleNotify::ClientCtrlRemoveClient, ptr, vtQueued)
447 #define Notify_ServerAdd(ptr) MuleNotify::DoNotify(&MuleNotify::ServerAdd, ptr)
448 #define Notify_ServerRemove(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRemove, ptr)
449 #define Notify_ServerRemoveDead() MuleNotify::DoNotify(&MuleNotify::ServerRemoveDead)
450 #define Notify_ServerRemoveAll() MuleNotify::DoNotify(&MuleNotify::ServerRemoveAll)
451 #define Notify_ServerHighlight(ptr, val) MuleNotify::DoNotify(&MuleNotify::ServerHighlight, ptr, val)
452 #define Notify_ServerRefresh(ptr) MuleNotify::DoNotify(&MuleNotify::ServerRefresh, ptr)
453 #define Notify_ServerFreeze() MuleNotify::DoNotify(&MuleNotify::ServerFreeze)
454 #define Notify_ServerThaw() MuleNotify::DoNotify(&MuleNotify::ServerThaw)
455 #define Notify_ServerUpdateED2KInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateED2KInfo)
456 #define Notify_ServerUpdateKadKInfo() MuleNotify::DoNotify(&MuleNotify::ServerUpdateKadKInfo)
459 #define Notify_SearchCancel() MuleNotify::DoNotify(&MuleNotify::SearchCancel)
460 #define Notify_SearchLocalEnd() MuleNotify::DoNotify(&MuleNotify::SearchLocalEnd)
461 #define Notify_KadSearchEnd(val) MuleNotify::DoNotify(&MuleNotify::KadSearchEnd, val)
462 #define Notify_Search_Update_Sources(ptr) MuleNotify::DoNotify(&MuleNotify::Search_Update_Sources, ptr)
463 #define Notify_Search_Add_Result(s) MuleNotify::DoNotify(&MuleNotify::Search_Add_Result, s)
466 #define Notify_ChatRefreshFriend(ptr, val) MuleNotify::DoNotify(&MuleNotify::ChatRefreshFriend, ptr, val)
467 #define Notify_ChatConnResult(val0, val1, s) MuleNotify::DoNotify(&MuleNotify::ChatConnResult, val0, val1, s)
468 #define Notify_ChatProcessMsg(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatProcessMsg, val0, s)
469 #define Notify_ChatSendCaptcha(val0, s) MuleNotify::DoNotify(&MuleNotify::ChatSendCaptcha, val0, s)
472 #define Notify_ShowConnState(val) MuleNotify::DoNotify(&MuleNotify::ShowConnState, val)
473 #define Notify_ShowUserCount(str) MuleNotify::DoNotify(&MuleNotify::ShowUserCount, str)
474 #define Notify_ShowQueueCount(val) MuleNotify::DoNotify(&MuleNotify::ShowQueueCount, val)
475 #define Notify_ShowUpdateCatTabTitles() MuleNotify::DoNotify(&MuleNotify::ShowUpdateCatTabTitles)
476 #define Notify_ShowGUI() MuleNotify::DoNotify(&MuleNotify::ShowGUI)
479 #define Notify_CategoryAdded() MuleNotify::DoNotify(&MuleNotify::CategoryAdded)
480 #define Notify_CategoryUpdate(cat) MuleNotify::DoNotify(&MuleNotify::CategoryUpdate, cat)
481 #define Notify_CategoryDelete(cat) MuleNotify::DoNotify(&MuleNotify::CategoryDelete, cat)
483 // server.met/nodes.dat default urls
484 #define Notify_NodesURLChanged(url) MuleNotify::DoNotify(&MuleNotify::NodesURLChanged, url)
485 #define Notify_ServersURLChanged(url) MuleNotify::DoNotify(&MuleNotify::ServersURLChanged, url)
487 // Partfile conversion: Core -> GUI
488 #define Notify_ConvertUpdateProgress(val, text) Notify_ConvertUpdateProgressFull(val, text, wxEmptyString)
489 #define Notify_ConvertUpdateProgressFull(val, text, hdr) MuleNotify::DoNotify(&MuleNotify::ConvertUpdateProgress, val, text, hdr)
490 #define Notify_ConvertUpdateJobInfo(info) MuleNotify::DoNotify(&MuleNotify::ConvertUpdateJobInfo, info)
491 #define Notify_ConvertRemoveJobInfo(id) MuleNotify::DoNotify(&MuleNotify::ConvertRemoveJobInfo, id)
492 #define Notify_ConvertClearInfos() MuleNotify::DoNotify(&MuleNotify::ConvertClearInfos)
493 // Partfile conversion: GUI -> Core
494 #define Notify_ConvertRemoveJob(id) MuleNotify::DoNotify(&MuleNotify::ConvertRemoveJob, id)
495 #define Notify_ConvertRetryJob(id) MuleNotify::DoNotify(&MuleNotify::ConvertRetryJob, id)
496 #define Notify_ConvertReaddAllJobs() MuleNotify::DoNotify(&MuleNotify::ConvertReaddAllJobs)
499 // GUI -> core notification
503 #define CoreNotify_PartFile_Swap_A4AF(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF, ptr)
504 #define CoreNotify_PartFile_Swap_A4AF_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Auto, ptr)
505 #define CoreNotify_PartFile_Swap_A4AF_Others(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Swap_A4AF_Others, ptr)
506 #define CoreNotify_PartFile_Pause(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Pause, ptr)
507 #define CoreNotify_PartFile_Resume(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Resume, ptr)
508 #define CoreNotify_PartFile_Stop(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Stop, ptr)
509 #define CoreNotify_PartFile_PrioAuto(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioAuto, ptr, val)
510 #define CoreNotify_PartFile_PrioSet(p, v0, v1) MuleNotify::DoNotify(&MuleNotify::PartFile_PrioSet, p, v0, v1)
511 #define CoreNotify_PartFile_Delete(ptr) MuleNotify::DoNotify(&MuleNotify::PartFile_Delete, ptr)
512 #define CoreNotify_PartFile_SetCat(ptr, val) MuleNotify::DoNotify(&MuleNotify::PartFile_SetCat, ptr, val)
515 #define CoreNotify_KnownFile_Up_Prio_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Set, ptr, val)
516 #define CoreNotify_KnownFile_Up_Prio_Auto(ptr) MuleNotify::DoNotify(&MuleNotify::KnownFile_Up_Prio_Auto, ptr)
517 #define CoreNotify_KnownFile_Comment_Set(ptr, val) MuleNotify::DoNotify(&MuleNotify::KnownFile_Comment_Set, ptr, val)
520 #define CoreNotify_Search_Add_Download(ptr, val) MuleNotify::DoNotify(&MuleNotify::Search_Add_Download, ptr, val)
521 #define CoreNotify_Search_Update_Progress(val) MuleNotify::DoNotify(&MuleNotify::Search_Update_Progress, val)
524 #define CoreNotify_Download_Set_Cat_Prio(cat, pri) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Prio, cat, pri)
525 #define CoreNotify_Download_Set_Cat_Status(cat, st) MuleNotify::DoNotify(&MuleNotify::Download_Set_Cat_Status, cat, st)
527 #endif // __GUIEVENTS_H__
529 // File_checked_for_headers