Audacity 3.2.0
Command.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity - A Digital Audio Editor
4 Copyright 1999-2009 Audacity Team
5 License: wxwidgets
6
7 Dan Horgan
8
9******************************************************************//*******************************************************************/
77
78
79#include "Command.h"
80
81#include <map>
82#include <wx/log.h>
83#include <wx/variant.h>
84#include <wx/arrstr.h>
85
86#include "CommandTargets.h"
87#include "CommandDirectory.h"
88
89#include "CommandContext.h"
90
91#include "AudacityException.h"
92
93
94
95bool OldStyleCommand::SetParameter(const wxString & WXUNUSED(paramName),
96 const wxVariant & WXUNUSED(paramValue))
97{
98 wxASSERT_MSG(false, wxT("Tried to set parameter for command which doesn't support parameters!"));
99 return false;
100}
101
103{
104}
105
107{
108 return mCommand->GetSymbol();
109}
110
112{
113 return mCommand->GetSignature();
114}
115
116bool DecoratedCommand::SetParameter(const wxString &paramName,
117 const wxVariant &paramValue)
118{
119 return mCommand->SetParameter(paramName, paramValue);
120}
121
123 const OldStyleCommandPointer &cmd, std::unique_ptr<CommandOutputTargets> &target)
124 : DecoratedCommand(cmd),
125 mCtx( std::make_unique<CommandContext>( cmd->mProject, std::move(target) ) )
126{
127}
128
129
130bool ApplyAndSendResponse::Apply(const CommandContext &WXUNUSED(context))
131{
132 wxLogMessage( "Context was passed in, but was ignored. ApplyAndSendResponse has its own one");
133 return Apply();
134}
135
136
138{
139 // ApplyAndSendResponse IS a command.
140 // It also HOLDS a command.
141
142 // Mostly its functions forward to the recipient.
143 // However it uses its OWN context, not the one of
144 // the command it holds.
145 auto result = GuardedCall<bool>(
146 [&] {
147 bool bResult = mCommand->Apply(*( mCtx.get()));
148 return bResult; }
149 );
150 wxString response = wxT( "\n" );
151
152 // PRL: it's all right to send untranslated strings to this channel
153 // I don't see _("") used with literal strings.
154 response += GetSymbol().Internal();
155
156 // These three strings are deliberately not localised.
157 // They are used in script responses and always happen in English.
158 response += wxT(" finished: ");
159 if (result)
160 {
161 response += wxT("OK");
162 }
163 else
164 {
165 response += wxT("Failed!");
166 }
167 mCtx->Status(response, true);
168 return result;
169}
170
174 mType(type),
175 mParams(type.GetSignature().GetDefaults()),
176 mSetParams()
177{
178}
179
181{
182}
183
184void CommandImplementation::TypeCheck(const wxString &typeName,
185 const wxString &paramName,
186 const wxVariant &param)
187{
188 // this macro is empty if wxWidgets is not compiled in debug mode
189 wxASSERT_MSG(param.IsType(typeName),
191 + wxT("command tried to get '")
192 + paramName
193 + wxT("' parameter as a ")
194 + typeName
195 + wxT(", but that wasn't enforced by the command signature."));
196}
197
198void CommandImplementation::CheckParam(const wxString &paramName)
199{
200 // this macro is empty if wxWidgets is not compiled in debug mode
201 wxASSERT_MSG(mParams.find(paramName) != mParams.end(),
203 + wxT("command tried to get '")
204 + paramName
205 + wxT("' parameter, but that parameter doesn't exist in the command signature!"));
206}
207
208bool CommandImplementation::HasParam( const wxString &paramName)
209{
210 // Test for not even in map...
211 if( mParams.count(paramName) < 1)
212 return false;
213 return mSetParams[paramName];
214}
215
216bool CommandImplementation::GetBool(const wxString &paramName)
217{
218 CheckParam(paramName);
219 const wxVariant &v = mParams[paramName];
220 TypeCheck(wxT("bool"), paramName, v);
221 return v.GetBool();
222}
223
224long CommandImplementation::GetLong(const wxString &paramName)
225{
226 CheckParam(paramName);
227 const wxVariant &v = mParams[paramName];
228 TypeCheck(wxT("double"), paramName, v);
229 return (long)v.GetDouble();
230}
231
232double CommandImplementation::GetDouble(const wxString &paramName)
233{
234 CheckParam(paramName);
235 const wxVariant &v = mParams[paramName];
236 TypeCheck(wxT("double"), paramName, v);
237 return v.GetDouble();
238}
239
240wxString CommandImplementation::GetString(const wxString &paramName)
241{
242 CheckParam(paramName);
243 const wxVariant &v = mParams[paramName];
244 TypeCheck(wxT("string"), paramName, v);
245 return v.GetString();
246}
247
250{
251 return mType.GetSymbol();
252}
253
256{
257 return mType.GetSignature();
258}
259
260bool CommandImplementation::SetParameter(const wxString &paramName, const wxVariant &paramValue)
261{
262 wxASSERT(!paramValue.IsType(wxT("null")));
263 CommandContext context( mProject );
264 ParamValueMap::iterator iter = mParams.find(paramName);
265 if (iter == mParams.end())
266 {
267 // Translated format, but untranslated command name substituted into it?
268 // Perhaps these formats that don't need translating.
269 context.Error( wxString::Format(
270 _("%s is not a parameter accepted by %s"),
271 paramName, GetSymbol().Internal() ) );
272 // neglect translation for scripting ??
273 return false;
274 }
275
276 Validator &validator = mType.GetSignature().GetValidator(iter->first);
277 if (!validator.Validate(paramValue))
278 {
279 context.Error( wxString::Format(
280 _("Invalid value for parameter '%s': should be %s"),
281 paramName, validator.GetDescription() ) );
282 return false;
283 }
284 mParams[paramName] = validator.GetConverted();
285 mSetParams[ paramName ] = true;
286
287 // (debug)
288 // context.Status(wxT("Set parameter ") + paramName + wxT(" to type ") + mParams[paramName].GetType() + wxT(", value ") + mParams[paramName].MakeString());
289
290 return true;
291}
292
293bool CommandImplementation::Apply(const CommandContext & WXUNUSED(context))
294{
295 return true;
296}
297
298
wxT("CloseDown"))
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
@ Internal
Indicates internal failure from Audacity.
Contains declaration of Command base class.
Contains declarations for CommandDirectory class.
#define _(s)
Definition: Internat.h:73
const auto project
ApplyAndSendResponse(const OldStyleCommandPointer &cmd, std::unique_ptr< CommandOutputTargets > &target)
Definition: Command.cpp:122
bool Apply() override
Definition: Command.cpp:137
std::unique_ptr< const CommandContext > mCtx
Definition: Command.h:72
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
virtual void Error(const wxString &message) const
bool GetBool(const wxString &paramName)
Definition: Command.cpp:216
CommandSignature & GetSignature() override
Get the signature of the command.
Definition: Command.cpp:255
bool Apply() override
Definition: Command.h:122
bool HasParam(const wxString &paramName)
Definition: Command.cpp:208
ComponentInterfaceSymbol GetSymbol() override
An instance method for getting the command name (for consistency)
Definition: Command.cpp:249
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:260
void CheckParam(const wxString &paramName)
Definition: Command.cpp:198
ParamValueMap mParams
Definition: Command.h:81
virtual ~CommandImplementation()
Definition: Command.cpp:180
OldStyleCommandType & mType
Definition: Command.h:80
void TypeCheck(const wxString &typeName, const wxString &paramName, const wxVariant &param)
Definition: Command.cpp:184
ParamBoolMap mSetParams
Definition: Command.h:82
long GetLong(const wxString &paramName)
Definition: Command.cpp:224
CommandImplementation(AudacityProject &project, OldStyleCommandType &type)
Definition: Command.cpp:171
wxString GetString(const wxString &paramName)
Definition: Command.cpp:240
double GetDouble(const wxString &paramName)
Definition: Command.cpp:232
Class that maps parameter names to default values and validators.
Validator & GetValidator(const wxString &paramName)
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
const wxString & Internal() const
DecoratedCommand is a decorator for command. It forwards functions to the mCommand it holds.
Definition: Command.h:48
CommandSignature & GetSignature() override
Definition: Command.cpp:111
OldStyleCommandPointer mCommand
Definition: Command.h:50
virtual ~DecoratedCommand()
Definition: Command.cpp:102
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:116
ComponentInterfaceSymbol GetSymbol() override
Definition: Command.cpp:106
Abstract base class for command interface. This is the version created by Dan Horgan....
Definition: Command.h:30
virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:95
AudacityProject & mProject
Definition: Command.h:32
OldStyleCommandPointer is a unique_ptr to an OldStyleCommand.
Base class for containing data common to all commands of a given type. Also acts as a factory.
Definition: CommandType.h:45
CommandSignature & GetSignature()
Definition: CommandType.cpp:37
ComponentInterfaceSymbol GetSymbol() const override
Definition: CommandType.cpp:32
A Validator is an object which checks whether a wxVariant satisfies a certain criterion....
Definition: Validators.h:54
virtual wxString GetDescription() const
Definition: Validators.h:79
const wxVariant & GetConverted()
Definition: Validators.h:65
virtual bool Validate(const wxVariant &v)
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:71
STL namespace.