File: command_line_parser.cpp

package info (click to toggle)
easyeffects 8.0.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,752 kB
  • sloc: cpp: 22,118; sh: 691; python: 448; javascript: 64; makefile: 8
file content (237 lines) | stat: -rw-r--r-- 6,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
 * Copyright © 2017-2025 Wellington Wallace
 *
 * This file is part of Easy Effects.
 *
 * Easy Effects is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Easy Effects is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Easy Effects. If not, see <https://www.gnu.org/licenses/>.
 */

#include "command_line_parser.hpp"
#include <qcommandlineparser.h>
#include <qobject.h>
#include <qtmetamacros.h>
#include <KAboutData>
#include <KLocalizedString>
#include <QApplication>
#include <QLoggingCategory>
#include <QString>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include "easyeffects_db.h"
#include "pipeline_type.hpp"
#include "presets_manager.hpp"
#include "util.hpp"

CommandLineParser::CommandLineParser(KAboutData& about, QObject* parent)
    : QObject(parent), parser(std::make_unique<QCommandLineParser>()) {
  parser->setApplicationDescription("Easy Effects");

  about.setupCommandLine(parser.get());
  parser->addOptions(
      {{{"q", "quit"}, i18n("Quit Easy Effects. Useful when running in service mode.")},
       {{"r", "reset"}, i18n("Reset Easy Effects.")},
       {{"w", "hide-window"}, i18n("Hide the window.")},
       {{"b", "bypass"}, i18n("Global bypass. 1 to enable and 2 to disable."), i18n("bypass-state")},
       {{"l", "load-preset"}, i18n("Load a preset. Example: easyeffects -l music"), i18n("preset-name")},
       {{"p", "presets"}, i18n("Show available presets.")},
       {{"a", "last-loaded-preset"}, i18n("Get the last loaded input/output preset."), i18n("preset-type")},
       {{"s", "last-loaded-presets"}, i18n("Get the last loaded input and output presets.")},
       {"gapplication-service", i18n("Deprecated. Use --service-mode instead.")},
       {"service-mode", i18n("Start the application with service mode turned on.")},
       {"debug", i18n("Enable debug messages.")}});
}

void CommandLineParser::set_is_primary(const bool& state) {
  is_primary = state;
}

void CommandLineParser::process(KAboutData& about, QApplication* app) {
  parser->process(*app);

  about.processCommandLine(parser.get());
}

void CommandLineParser::process_debug_option() {
  if (parser->isSet("debug")) {
    QLoggingCategory::setFilterRules("easyeffects.debug=true");
  }
}

void CommandLineParser::process_hide_window(bool& show_window) {
  if (parser->isSet("hide-window")) {
    show_window = false;
  }
}

void CommandLineParser::process_events() {
  auto* pm = &presets::Manager::self();

  if (parser->isSet("quit")) {
    Q_EMIT onQuit();
  }

  if (parser->isSet("reset")) {
    Q_EMIT onReset();
  }

  if (parser->isSet("hide-window")) {
    Q_EMIT onHideWindow();
  }

  if (parser->isSet("service-mode")) {
    DbMain::setEnableServiceMode(true);
  }

  if (parser->isSet("gapplication-service")) {
    DbMain::setEnableServiceMode(true);

    Q_EMIT onHideWindow();
  }

  if (parser->isSet("last-loaded-preset")) {
    bool ok = true;

    const auto value = parser->value("last-loaded-preset");

    if (value == "input" || value == "output") {
      if (is_primary) {  // no daemon running
        auto preset = (value == "input") ? DbMain::lastLoadedInputPreset() : DbMain::lastLoadedOutputPreset();

        if (preset.length() < 1) {
          preset = QString("None");
        }

        std::cout << preset.toStdString() << '\n';
      } else {
        Q_EMIT onGetLastLoadedPreset((value == "input") ? PipelineType::input : PipelineType::output);
      }
    } else {
      ok = false;

      std::cout << i18n("Must specify preset type: input/output.").toStdString() << '\n';
    }

    Q_EMIT onHideWindow();

    if (ok) {
      QCoreApplication::exit(EXIT_SUCCESS);
    } else {
      QCoreApplication::exit(EXIT_FAILURE);
    }
  }

  if (parser->isSet("last-loaded-presets")) {
    if (is_primary) {
      auto input = DbMain::lastLoadedInputPreset();
      auto output = DbMain::lastLoadedOutputPreset();

      std::cout << "Input: " << input.toStdString() << '\n';
      std::cout << "Output: " << output.toStdString() << '\n';
    } else {
      Q_EMIT onGetLastLoadedInputOutputPreset();
    }

    QCoreApplication::exit(EXIT_SUCCESS);
  }

  if (parser->isSet("presets")) {
    std::string list;
    int i = 0;

    for (const auto& p : pm->get_local_presets_paths(PipelineType::output)) {
      list += util::to_string(++i) + '\t' + p.stem().string() + '\n';
    }

    if (i > 0) {
      std::cout << i18n("Output presets").toStdString() + ":\n" + list << '\n';
    } else {
      std::cout << i18n("No output presets.").toStdString() << '\n';
    }

    list = "";
    i = 0;

    for (const auto& p : pm->get_local_presets_paths(PipelineType::input)) {
      list += util::to_string(++i) + '\t' + p.stem().string() + '\n';
    }

    if (i > 0) {
      std::cout << i18n("Input presets").toStdString() + ":\n" + list << '\n';
    } else {
      std::cout << i18n("No input presets.").toStdString() << '\n';
    }

    Q_EMIT onHideWindow();

    QCoreApplication::exit(EXIT_SUCCESS);
  }

  if (parser->isSet("load-preset")) {
    bool ok = false;

    const auto name = parser->value("load-preset");

    if (pm->preset_file_exists(PipelineType::input, name.toStdString())) {
      Q_EMIT onLoadPreset(PipelineType::input, name);

      ok = true;
    }

    if (pm->preset_file_exists(PipelineType::output, name.toStdString())) {
      Q_EMIT onLoadPreset(PipelineType::output, name);

      ok = true;
    }

    Q_EMIT onHideWindow();

    if (ok) {
      QCoreApplication::exit(EXIT_SUCCESS);
    } else {
      std::cout << i18n("Specified preset does not exist.").toStdString() << '\n';

      QCoreApplication::exit(EXIT_FAILURE);
    }
  }

  if (parser->isSet("bypass")) {
    bool ok = true;

    const auto value = parser->value("bypass");

    if (value == "1") {
      Q_EMIT onSetGlobalBypass(true);
    } else if (value == "2") {
      Q_EMIT onSetGlobalBypass(false);
    } else {
      ok = false;

      std::cout << i18n("Provided an invalid bypass state.").toStdString() << '\n';
    }

    Q_EMIT onHideWindow();

    if (ok) {
      QCoreApplication::exit(EXIT_SUCCESS);
    } else {
      QCoreApplication::exit(EXIT_FAILURE);
    }
  }

  if (is_primary) {
    Q_EMIT onInitQML();
  }
}