1 /**********************************************************************
2 
3    Audacity - A Digital Audio Editor
4    Copyright 1999-2009 Audacity Team
5    File License: wxWidgets
6 
7    Dan Horgan
8 
9 ******************************************************************//**
10 
11 \file BatchEvalCommand.cpp
12 \brief Contains definitions for the BatchEvalCommand class
13 
14 *//*******************************************************************/
15 
16 
17 #include "BatchEvalCommand.h"
18 
19 #include "CommandContext.h"
20 #include "CommandDirectory.h"
21 #include "Project.h"
22 
23 static CommandDirectory::RegisterType sRegisterType{
24    std::make_unique<BatchEvalCommandType>()
25 };
26 
BuildName()27 ComponentInterfaceSymbol BatchEvalCommandType::BuildName()
28 {
29    return { wxT("BatchCommand"), XO("Batch Command") };
30 }
31 
BuildSignature(CommandSignature & signature)32 void BatchEvalCommandType::BuildSignature(CommandSignature &signature)
33 {
34    auto commandNameValidator = std::make_unique<DefaultValidator>();
35    signature.AddParameter(wxT("CommandName"), wxT(""), std::move(commandNameValidator));
36    auto paramValidator = std::make_unique<DefaultValidator>();
37    signature.AddParameter(wxT("ParamString"), wxT(""), std::move(paramValidator));
38    auto macroValidator = std::make_unique<DefaultValidator>();
39    signature.AddParameter(wxT("MacroName"), wxT(""), std::move(macroValidator));
40 }
41 
Create(AudacityProject & project,std::unique_ptr<CommandOutputTargets> && WXUNUSED (target))42 OldStyleCommandPointer BatchEvalCommandType::Create( AudacityProject &project,
43    std::unique_ptr<CommandOutputTargets> && WXUNUSED(target))
44 {
45    return std::make_shared<BatchEvalCommand>(project, *this);
46 }
47 
Apply(const CommandContext & context)48 bool BatchEvalCommand::Apply(const CommandContext & context)
49 {
50    // Uh oh, I need to build a catalog, expensively
51    // Maybe it can be built in one long-lived place and shared among command
52    // objects instead?
53    // The catalog though may change during a session, as it includes the
54    // names of macro commands - so the long-lived copy will need to
55    // be refreshed after macros are added/deleted.
56    MacroCommandsCatalog catalog(&context.project);
57 
58    wxString macroName = GetString(wxT("MacroName"));
59    if (!macroName.empty())
60    {
61       MacroCommands batch{ context.project };
62       batch.ReadMacro(macroName);
63       return batch.ApplyMacro(catalog);
64    }
65 
66    auto cmdName = GetString(wxT("CommandName"));
67    wxString cmdParams = GetString(wxT("ParamString"));
68    auto iter = catalog.ByCommandId(cmdName);
69    const auto friendly = (iter == catalog.end())
70       ? Verbatim( cmdName ) // Expose internal name to user, in default of a better one!
71       : iter->name.Msgid().Stripped();
72 
73    // Create a Batch that will have just one command in it...
74    MacroCommands Batch{ context.project };
75    bool bResult = Batch.ApplyCommandInBatchMode(friendly, cmdName, cmdParams, &context);
76    // Relay messages, if any.
77    wxString Message = Batch.GetMessage();
78    if( !Message.empty() )
79       context.Status( Message );
80    return bResult;
81 }
82 
~BatchEvalCommand()83 BatchEvalCommand::~BatchEvalCommand()
84 { }
85