Fixed problem in the CFile write_safe concept
[amule.git] / src / UserEvents.cpp
blob70ddd8f3dc44498dc8624a8e8d5d864555825911
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.
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
26 #include "UserEvents.h"
29 #include <common/Format.h>
30 #include "Logger.h"
31 #include "Preferences.h"
32 #include "PartFile.h"
33 #include "TerminationProcess.h" // Needed for CTerminationProcess
36 #include <wx/process.h>
39 #define USEREVENTS_EVENT(ID, NAME, VARS) { wxT(#ID), NAME, false, wxEmptyString, false, wxEmptyString },
40 static struct {
41 const wxString key;
42 const wxString name;
43 bool core_enabled;
44 wxString core_command;
45 bool gui_enabled;
46 wxString gui_command;
47 } s_EventList[] = {
48 USEREVENTS_EVENTLIST()
49 /* This macro expands to initialise the list of user event types. Example:
50 { wxT("NewChatSession"), wxTRANSLATE("New chat session started"), false, wxEmptyString, false, wxEmptyString }, */
52 #undef USEREVENTS_EVENT
55 #ifdef __WXDEBUG__
56 inline bool CheckIndex(const unsigned int idx)
58 return (idx < sizeof(s_EventList) / sizeof(s_EventList[0]));
60 #endif
62 unsigned int CUserEvents::GetCount()
64 return sizeof(s_EventList) / sizeof(s_EventList[0]);
67 const wxString& CUserEvents::GetDisplayName(enum EventType event)
69 wxASSERT(CheckIndex(event));
70 return s_EventList[event].name;
73 bool CUserEvents::IsCoreCommandEnabled(enum EventType event)
75 wxASSERT(CheckIndex(event));
76 return s_EventList[event].core_enabled;
79 bool CUserEvents::IsGUICommandEnabled(enum EventType event)
81 wxASSERT(CheckIndex(event));
82 return s_EventList[event].gui_enabled;
85 const wxString& CUserEvents::GetKey(const unsigned int event)
87 wxASSERT(CheckIndex(event));
88 return s_EventList[event].key;
91 bool& CUserEvents::GetCoreEnableVar(const unsigned int event)
93 wxASSERT(CheckIndex(event));
94 return s_EventList[event].core_enabled;
97 wxString& CUserEvents::GetCoreCommandVar(const unsigned int event)
99 wxASSERT(CheckIndex(event));
100 return s_EventList[event].core_command;
103 bool& CUserEvents::GetGUIEnableVar(const unsigned int event)
105 wxASSERT(CheckIndex(event));
106 return s_EventList[event].gui_enabled;
109 wxString& CUserEvents::GetGUICommandVar(const unsigned int event)
111 wxASSERT(CheckIndex(event));
112 return s_EventList[event].gui_command;
115 #define USEREVENTS_EVENT(ID, NAME, VARS) case CUserEvents::ID: { VARS break; }
116 #define USEREVENTS_REPLACE_VAR(VAR, DESC, CODE) command.Replace(wxT("%") VAR, CODE);
117 static void ExecuteCommand(
118 enum CUserEvents::EventType event,
119 const void* object,
120 const wxString& cmd)
122 // This variable is needed by the USEREVENTS_EVENTLIST macro.
123 wxString command = cmd;
124 switch (event) {
125 USEREVENTS_EVENTLIST()
126 /* This macro expands to handle all user event types. Example:
127 case CUserEvents::NewChatSession: {
128 command.Replace( wxT("%SENDER"), *((wxString*)object) );
129 break;
130 } */
132 if (!command.empty()) {
133 CTerminationProcess *p = new CTerminationProcess(cmd);
134 if (!wxExecute(command, wxEXEC_ASYNC, p)) {
135 // If wxExecute fails, we need to delete the CTerminationProcess
136 // otherwise it will leak.
137 delete p;
138 AddLogLineC(CFormat(_("Failed to execute command `%s' on `%s' event.")) %
139 command % s_EventList[event].name);
144 void CUserEvents::ProcessEvent(enum EventType event, const void* object)
146 wxASSERT(CheckIndex(event));
147 wxASSERT(object != NULL);
149 #ifndef CLIENT_GUI
150 if (s_EventList[event].core_enabled) {
151 ExecuteCommand(event, object, s_EventList[event].core_command);
153 #endif
154 #ifndef AMULE_DAEMON
155 if (s_EventList[event].gui_enabled) {
156 ExecuteCommand(event, object, s_EventList[event].gui_command);
158 #endif