Upstream tarball 20080603
[amule.git] / src / UserEvents.cpp
blobf01b433a32d3e76aea12441f971a505696d58e42
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
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()
50 #undef USEREVENTS_EVENT
53 #ifdef __WXDEBUG__
54 inline bool CheckIndex(const unsigned int idx)
56 return (idx < sizeof(s_EventList) / sizeof(s_EventList[0]));
58 #endif
60 unsigned int CUserEvents::GetCount()
62 return sizeof(s_EventList) / sizeof(s_EventList[0]);
65 const wxString& CUserEvents::GetDisplayName(enum EventType event)
67 wxASSERT(CheckIndex(event));
68 return s_EventList[event].name;
71 bool CUserEvents::IsCoreCommandEnabled(enum EventType event)
73 wxASSERT(CheckIndex(event));
74 return s_EventList[event].core_enabled;
77 bool CUserEvents::IsGUICommandEnabled(enum EventType event)
79 wxASSERT(CheckIndex(event));
80 return s_EventList[event].gui_enabled;
83 const wxString& CUserEvents::GetKey(const unsigned int event)
85 wxASSERT(CheckIndex(event));
86 return s_EventList[event].key;
89 bool& CUserEvents::GetCoreEnableVar(const unsigned int event)
91 wxASSERT(CheckIndex(event));
92 return s_EventList[event].core_enabled;
95 wxString& CUserEvents::GetCoreCommandVar(const unsigned int event)
97 wxASSERT(CheckIndex(event));
98 return s_EventList[event].core_command;
101 bool& CUserEvents::GetGUIEnableVar(const unsigned int event)
103 wxASSERT(CheckIndex(event));
104 return s_EventList[event].gui_enabled;
107 wxString& CUserEvents::GetGUICommandVar(const unsigned int event)
109 wxASSERT(CheckIndex(event));
110 return s_EventList[event].gui_command;
113 #define USEREVENTS_EVENT(ID, NAME, VARS) case CUserEvents::ID: { VARS break; }
114 #define USEREVENTS_REPLACE_VAR(VAR, DESC, CODE) command.Replace(wxT("%") VAR, CODE);
115 static void ExecuteCommand(
116 enum CUserEvents::EventType event,
117 const void* object,
118 const wxString& cmd)
120 // This variable is needed by the USEREVENTS_EVENTLIST macro.
121 wxString command = cmd;
122 switch (event) {
123 USEREVENTS_EVENTLIST()
125 if (!command.empty()) {
126 CTerminationProcess *p = new CTerminationProcess(cmd);
127 if (!wxExecute(command, wxEXEC_ASYNC, p)) {
128 // If wxExecute fails, we need to delete the CTerminationProcess
129 // otherwise it will leak.
130 delete p;
131 AddLogLineM(true,
132 CFormat(_("Failed to execute command `%s' on `%s' event.")) %
133 command % s_EventList[event].name);
138 void CUserEvents::ProcessEvent(enum EventType event, const void* object)
140 wxASSERT(CheckIndex(event));
141 wxASSERT(object != NULL);
143 #ifndef CLIENT_GUI
144 if (s_EventList[event].core_enabled) {
145 ExecuteCommand(event, object, s_EventList[event].core_command);
147 #endif
148 #ifndef AMULE_DAEMON
149 if (s_EventList[event].gui_enabled) {
150 ExecuteCommand(event, object, s_EventList[event].gui_command);
152 #endif