Chatterino
CommandController.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 #include "common/Singleton.hpp"
9 
10 #include <QMap>
11 #include <pajlada/settings.hpp>
12 
13 #include <memory>
14 #include <mutex>
15 #include <unordered_map>
16 #include <variant>
17 
18 namespace chatterino {
19 
20 class Settings;
21 class Paths;
22 class Channel;
23 
24 class CommandModel;
25 
26 class CommandController final : public Singleton
27 {
28 public:
30 
31  QString execCommand(const QString &text, std::shared_ptr<Channel> channel,
32  bool dryRun);
33  QStringList getDefaultChatterinoCommandList();
34 
35  virtual void initialize(Settings &, Paths &paths) override;
36  virtual void save() override;
37 
38  CommandModel *createModel(QObject *parent);
39 
40  QString execCustomCommand(
41  const QStringList &words, const Command &command, bool dryRun,
42  ChannelPtr channel, const Message *message = nullptr,
43  std::unordered_map<QString, QString> context = {});
44 
45 private:
46  void load(Paths &paths);
47 
48  using CommandFunction =
49  std::function<QString(QStringList /*words*/, ChannelPtr /*channel*/)>;
50 
51  using CommandFunctionWithContext = std::function<QString(CommandContext)>;
52 
53  using CommandFunctionVariants =
54  std::variant<CommandFunction, CommandFunctionWithContext>;
55 
56  void registerCommand(const QString &commandName,
57  CommandFunctionVariants commandFunction);
58 
59  // Chatterino commands
60  std::unordered_map<QString, CommandFunctionVariants> commands_;
61 
62  // User-created commands
63  QMap<QString, Command> userCommands_;
64  int maxSpaces_ = 0;
65 
66  std::shared_ptr<pajlada::Settings::SettingManager> sm_;
67  // Because the setting manager is not initialized until the initialize
68  // function is called (and not in the constructor), we have to
69  // late-initialize the setting, which is why we're storing it as a
70  // unique_ptr
71  std::unique_ptr<pajlada::Settings::Setting<std::vector<Command>>>
72  commandsSetting_;
73 
74  QStringList defaultChatterinoCommandAutoCompletions_;
75 };
76 
77 } // namespace chatterino
Definition: Command.hpp:10
Definition: Singleton.hpp:10
Definition: Application.cpp:48
Definition: CommandController.hpp:26
CommandModel * createModel(QObject *parent)
Definition: CommandController.cpp:3136
QString execCustomCommand(const QStringList &words, const Command &command, bool dryRun, ChannelPtr channel, const Message *message=nullptr, std::unordered_map< QString, QString > context={})
Definition: CommandController.cpp:3233
Settings which are availlable for reading and writing on the gui thread.
Definition: Settings.hpp:78
Definition: Paths.hpp:8
QString execCommand(const QString &text, std::shared_ptr< Channel > channel, bool dryRun)
Definition: CommandController.cpp:3144
Definition: Message.hpp:54
virtual void initialize(Settings &, Paths &paths) override
Definition: CommandController.cpp:555
Definition: CommandModel.hpp:12
std::shared_ptr< Channel > ChannelPtr
Definition: Channel.hpp:125
QStringList getDefaultChatterinoCommandList()
Definition: CommandController.cpp:3332
virtual void save() override
Definition: CommandController.cpp:3131
SignalVector< Command > items
Definition: CommandController.hpp:29