Upstream tarball 9964
[amule.git] / src / UserEvents.h
blob0c3c36dd9d7ca1c22a62fc11b48425aee7d5233d
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2006-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifndef USEREVENTS_H
26 #define USEREVENTS_H
28 #include <wx/intl.h> // Needed for wxTRANSLATE
31 #ifdef _MSC_VER
32 #define ATTR(x)
33 #else
34 #define ATTR(x) __attribute__((x))
35 #endif
37 /* Each event will use 5 IDs:
38 - the panel that shows the prefs for this event
39 - the 'Core command enabled' checkbox
40 - the 'Core command' textctrl
41 - the 'GUI command enabled' checkbox
42 - the 'GUI command' textctrl
44 #define USEREVENTS_IDS_PER_EVENT 5
46 const int USEREVENTS_FIRST_ID = 11500; /* Some safe GUI ID to start from */
48 /**
49 * Macro listing all the events.
51 * This huge macro is expanded 5 times in the sources, each time producing
52 * different code. If we decide to get rid of the macro either because of coding style
53 * decision, or someone finds a compiler that doesn't support this big macro, we
54 * have to maintain these five places in sync. They are:
55 * - one in PrefsUnifiedDlg.cpp (EVENT_LIST, PrefsUnifiedDlg::PrefsUnifiedDlg())
56 * - one in this header (CUserEvents::EventType)
57 * - two in UserEvents.cpp (static struct EventList[]; CUserEvent::ProcessEvent())
59 #define USEREVENTS_EVENTLIST() \
60 USEREVENTS_EVENT(DownloadCompleted, wxTRANSLATE("Download completed"), \
61 USEREVENTS_REPLACE_VAR( \
62 wxT("FILE"), \
63 wxTRANSLATE("The full path to the file."), \
64 ((CPartFile*)object)->GetFullName().GetRaw() ) \
65 USEREVENTS_REPLACE_VAR( \
66 wxT("NAME"), \
67 wxTRANSLATE("The name of the file without path component."), \
68 ((CPartFile*)object)->GetFileName().GetRaw() ) \
69 USEREVENTS_REPLACE_VAR( \
70 wxT("HASH"), \
71 wxTRANSLATE("The eD2k hash of the file."), \
72 ((CPartFile*)object)->GetFileHash().Encode() ) \
73 USEREVENTS_REPLACE_VAR( \
74 wxT("SIZE"), \
75 wxTRANSLATE("The size of the file in bytes."), \
76 (wxString)(CFormat(wxT("%llu")) % ((CPartFile*)object)->GetFileSize()) ) \
77 USEREVENTS_REPLACE_VAR( \
78 wxT("DLACTIVETIME"), \
79 wxTRANSLATE("Cumulative download activity time."), \
80 CastSecondsToHM(((CPartFile*)object)->GetDlActiveTime()) ) \
81 ) \
82 USEREVENTS_EVENT( \
83 NewChatSession, \
84 wxTRANSLATE("New chat session started"), \
85 USEREVENTS_REPLACE_VAR( \
86 wxT("SENDER"), \
87 wxTRANSLATE("Message sender."), \
88 *((wxString*)object) ) \
89 ) \
90 USEREVENTS_EVENT( \
91 OutOfDiskSpace, \
92 wxTRANSLATE("Out of space"), \
93 USEREVENTS_REPLACE_VAR( \
94 wxT("PARTITION"), \
95 wxTRANSLATE("Disk partition."), \
96 wxString((wxChar*)object) ) \
97 ) \
98 USEREVENTS_EVENT( \
99 ErrorOnCompletion, \
100 wxTRANSLATE("Error on completion"), \
101 USEREVENTS_REPLACE_VAR( \
102 wxT("FILE"), \
103 wxTRANSLATE("The full path to the file."), \
104 ((CPartFile*)object)->GetFullName().GetRaw() ) \
108 #define USEREVENTS_EVENT(ID, NAME, VARS) ID,
111 * Class to handle userspace events.
113 * These events that we publish to the user and let him
114 * specify a command to be run when one of these events occur.
116 class CUserEvents {
117 friend class CPreferences;
118 public:
119 //! Event list
120 enum EventType {
121 USEREVENTS_EVENTLIST()
125 * Process a user event.
127 * Notes on the 'object' argument: this should be a pointer to
128 * an object instance, from which all of the replacement texts
129 * can be generated.
131 * Unfortunately this approach does not provide any type-safety,
132 * a list of string pairs (key, replacement) would be the best.
133 * However, this would need either expanding the macro at all of
134 * the places where CUserEvents::ProcessEvent is called from, or
135 * creating lists of parameters for each event, etc = more lists
136 * to keep in sync manually.
138 static void ProcessEvent(enum EventType event, const void* object);
141 * Returns the number of defined user events.
143 static unsigned int GetCount() ATTR(__const__);
146 * Returs the human-readable name of the event.
148 static const wxString& GetDisplayName(enum EventType event) ATTR(__pure__);
151 * Checks whether the core command is enabled.
153 static bool IsCoreCommandEnabled(enum EventType event) ATTR(__pure__);
156 * Checks whether the GUI command is enabled.
158 static bool IsGUICommandEnabled(enum EventType event) ATTR(__pure__);
160 private:
161 // functions for CPreferences
162 static const wxString& GetKey(const unsigned int event) ATTR(__pure__);
163 static bool& GetCoreEnableVar(const unsigned int event) ATTR(__pure__);
164 static wxString& GetCoreCommandVar(const unsigned int event) ATTR(__pure__);
165 static bool& GetGUIEnableVar(const unsigned int event) ATTR(__pure__);
166 static wxString& GetGUICommandVar(const unsigned int event) ATTR(__pure__);
169 #undef USEREVENTS_EVENT
171 #endif /* USEREVENTS_H */