actions: use unique_ptr for storing actions
[ncmpcpp.git] / src / macro_utilities.cpp
blobbf89fafd40fdeca70592e1ba099eb7b2cfcf0961
1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "bindings.h"
22 #include "global.h"
23 #include "macro_utilities.h"
24 #include "utility/string.h"
25 #include "utility/wide_string.h"
27 namespace Actions {
29 PushCharacters::PushCharacters(NC::Window **w, std::vector<NC::Key::Type> &&queue)
30 : BaseAction(Type::MacroUtility, "push_characters")
31 , m_window(w)
32 , m_queue(queue)
34 assert(w != nullptr);
35 std::vector<std::string> keys;
36 for (const auto &key : queue)
37 keys.push_back(ToString(keyToWString(key)));
38 m_name += " \"";
39 m_name += join<std::string>(keys, ", ");
40 m_name += "\"";
43 void PushCharacters::run()
45 for (auto it = m_queue.begin(); it != m_queue.end(); ++it)
46 (*m_window)->pushChar(*it);
49 RequireRunnable::RequireRunnable(BaseAction *action)
50 : BaseAction(Type::MacroUtility, "require_runnable")
51 , m_action(action)
53 assert(m_action != nullptr);
54 m_name += " \"";
55 m_name += m_action->name();
56 m_name += "\"";
59 bool RequireRunnable::canBeRun()
61 return m_action->canBeRun();
64 RequireScreen::RequireScreen(ScreenType screen_type)
65 : BaseAction(Type::MacroUtility, "require_screen")
66 , m_screen_type(screen_type)
68 m_name += " \"";
69 m_name += screenTypeToString(m_screen_type);
70 m_name += "\"";
73 bool RequireScreen::canBeRun()
75 return Global::myScreen->type() == m_screen_type;
78 RunExternalCommand::RunExternalCommand(std::string command)
79 : BaseAction(Type::MacroUtility, "run_external_command")
80 , m_command(std::move(command))
82 m_name += " \"";
83 m_name += m_command;
84 m_name += "\"";
87 void RunExternalCommand::run()
89 GNUC_UNUSED int res;
90 res = std::system(m_command.c_str());