1 // 2 // This file is part of the aMule Project. 3 // 4 // Copyright (c) 2006-2011 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. 14 // 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 23 // 24 25 #ifndef USEREVENTS_H 26 #define USEREVENTS_H 27 28 #include <wx/intl.h> // Needed for wxTRANSLATE 29 30 31 #ifdef _MSC_VER 32 #define ATTR(x) 33 #else 34 #define ATTR(x) __attribute__((x)) 35 #endif 36 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 43 */ 44 #define USEREVENTS_IDS_PER_EVENT 5 45 46 const int USEREVENTS_FIRST_ID = 11500; /* Some safe GUI ID to start from */ 47 48 /** 49 * Macro listing all the events. 50 * 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::ExecuteCommand()) 58 */ 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 static_cast<const CPartFile*>(object)->GetFullName().GetRaw() ) \ 65 USEREVENTS_REPLACE_VAR( \ 66 wxT("NAME"), \ 67 wxTRANSLATE("The name of the file without path component."), \ 68 static_cast<const CPartFile*>(object)->GetFileName().GetRaw() ) \ 69 USEREVENTS_REPLACE_VAR( \ 70 wxT("HASH"), \ 71 wxTRANSLATE("The eD2k hash of the file."), \ 72 static_cast<const CPartFile*>(object)->GetFileHash().Encode() ) \ 73 USEREVENTS_REPLACE_VAR( \ 74 wxT("SIZE"), \ 75 wxTRANSLATE("The size of the file in bytes."), \ 76 (CFormat(wxT("%llu")) % static_cast<const CPartFile*>(object)->GetFileSize()).GetString() ) \ 77 USEREVENTS_REPLACE_VAR( \ 78 wxT("DLACTIVETIME"), \ 79 wxTRANSLATE("Cumulative download activity time."), \ 80 CastSecondsToHM(static_cast<const CPartFile*>(object)->GetDlActiveTime()) ) \ 81 ) \ 82 USEREVENTS_EVENT(NewChatSession, wxTRANSLATE("New chat session started"), \ 83 USEREVENTS_REPLACE_VAR( \ 84 wxT("SENDER"), \ 85 wxTRANSLATE("Message sender."), \ 86 *static_cast<const wxString*>(object) ) \ 87 ) \ 88 USEREVENTS_EVENT(OutOfDiskSpace, wxTRANSLATE("Out of space"), \ 89 USEREVENTS_REPLACE_VAR( \ 90 wxT("PARTITION"), \ 91 wxTRANSLATE("Disk partition."), \ 92 wxString(static_cast<const wxChar*>(object)) ) \ 93 ) \ 94 USEREVENTS_EVENT(ErrorOnCompletion, wxTRANSLATE("Error on completion"), \ 95 USEREVENTS_REPLACE_VAR( \ 96 wxT("FILE"), \ 97 wxTRANSLATE("The full path to the file."), \ 98 static_cast<const CPartFile*>(object)->GetFullName().GetRaw() ) \ 99 ) 100 101 102 #define USEREVENTS_EVENT(ID, NAME, VARS) ID, 103 104 /** 105 * Class to handle userspace events. 106 * 107 * These events that we publish to the user and let him 108 * specify a command to be run when one of these events occur. 109 */ 110 class CUserEvents { 111 friend class CPreferences; 112 public: 113 //! Event list 114 enum EventType { 115 USEREVENTS_EVENTLIST() 116 /* This macro expands to the following list of user event types: 117 DownloadCompleted, NewChatSession, OutOfDiskSpace, ErrorOnCompletion */ 118 }; 119 120 /** 121 * Process a user event. 122 * 123 * Notes on the 'object' argument: this should be a pointer to 124 * an object instance, from which all of the replacement texts 125 * can be generated. 126 * 127 * Unfortunately this approach does not provide any type-safety, 128 * a list of string pairs (key, replacement) would be the best. 129 * However, this would need either expanding the macro at all of 130 * the places where CUserEvents::ProcessEvent is called from, or 131 * creating lists of parameters for each event, etc = more lists 132 * to keep in sync manually. 133 */ 134 static void ProcessEvent(enum EventType event, const void* object); 135 136 /** 137 * Returns the number of defined user events. 138 */ 139 static unsigned int GetCount() ATTR(__const__); 140 141 /** 142 * Returs the human-readable name of the event. 143 */ 144 static const wxString& GetDisplayName(enum EventType event) ATTR(__pure__); 145 146 /** 147 * Checks whether the core command is enabled. 148 */ 149 static bool IsCoreCommandEnabled(enum EventType event) ATTR(__pure__); 150 151 /** 152 * Checks whether the GUI command is enabled. 153 */ 154 static bool IsGUICommandEnabled(enum EventType event) ATTR(__pure__); 155 156 private: 157 // functions for CPreferences 158 static const wxString& GetKey(const unsigned int event) ATTR(__pure__); 159 static bool& GetCoreEnableVar(const unsigned int event) ATTR(__pure__); 160 static wxString& GetCoreCommandVar(const unsigned int event) ATTR(__pure__); 161 static bool& GetGUIEnableVar(const unsigned int event) ATTR(__pure__); 162 static wxString& GetGUICommandVar(const unsigned int event) ATTR(__pure__); 163 }; 164 165 #undef USEREVENTS_EVENT 166 167 #endif /* USEREVENTS_H */ 168