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 46const 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 ((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() ) \ 105 ) 106 107 108#define USEREVENTS_EVENT(ID, NAME, VARS) ID, 109 110/** 111 * Class to handle userspace events. 112 * 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. 115 */ 116class CUserEvents { 117 friend class CPreferences; 118 public: 119 //! Event list 120 enum EventType { 121 USEREVENTS_EVENTLIST() 122 /* This macro expands to the following list of user event types: 123 DownloadCompleted, NewChatSession, OutOfDiskSpace, ErrorOnCompletion */ 124 }; 125 126 /** 127 * Process a user event. 128 * 129 * Notes on the 'object' argument: this should be a pointer to 130 * an object instance, from which all of the replacement texts 131 * can be generated. 132 * 133 * Unfortunately this approach does not provide any type-safety, 134 * a list of string pairs (key, replacement) would be the best. 135 * However, this would need either expanding the macro at all of 136 * the places where CUserEvents::ProcessEvent is called from, or 137 * creating lists of parameters for each event, etc = more lists 138 * to keep in sync manually. 139 */ 140 static void ProcessEvent(enum EventType event, const void* object); 141 142 /** 143 * Returns the number of defined user events. 144 */ 145 static unsigned int GetCount() ATTR(__const__); 146 147 /** 148 * Returs the human-readable name of the event. 149 */ 150 static const wxString& GetDisplayName(enum EventType event) ATTR(__pure__); 151 152 /** 153 * Checks whether the core command is enabled. 154 */ 155 static bool IsCoreCommandEnabled(enum EventType event) ATTR(__pure__); 156 157 /** 158 * Checks whether the GUI command is enabled. 159 */ 160 static bool IsGUICommandEnabled(enum EventType event) ATTR(__pure__); 161 162 private: 163 // functions for CPreferences 164 static const wxString& GetKey(const unsigned int event) ATTR(__pure__); 165 static bool& GetCoreEnableVar(const unsigned int event) ATTR(__pure__); 166 static wxString& GetCoreCommandVar(const unsigned int event) ATTR(__pure__); 167 static bool& GetGUIEnableVar(const unsigned int event) ATTR(__pure__); 168 static wxString& GetGUICommandVar(const unsigned int event) ATTR(__pure__); 169}; 170 171#undef USEREVENTS_EVENT 172 173#endif /* USEREVENTS_H */ 174