Commit 0721a22b authored by Ronan Abhamon's avatar Ronan Abhamon

feat(Cli): Cli is now a static object + clean code

parent e43fb27a
......@@ -69,12 +69,12 @@
<translation>show app version</translation>
</message>
<message>
<source>commandLineCliHelp</source>
<translation>displays the help menu to use Linphone with the CLI.</translation>
<source>commandLineOptionCliHelp</source>
<translation>displays the help menu to use Linphone with the CLI</translation>
</message>
<message>
<source>commandLineDescription</source>
<translation>send aan order to the application towards a command line.</translation>
<translation>send an order to the application towards a command line</translation>
</message>
</context>
<context>
......
......@@ -69,12 +69,12 @@
<translation>affiche la version de l&apos;application</translation>
</message>
<message>
<source>commandLineCliHelp</source>
<translation>affiche le menu d&apos;aide pour l&apos;utilisation de Linphone en CLI.</translation>
<source>commandLineOptionCliHelp</source>
<translation>affiche le menu d&apos;aide pour l&apos;utilisation de Linphone en CLI</translation>
</message>
<message>
<source>commandLineDescription</source>
<translation>envoie un ordre à l&apos;application Linphone, voir --cli-help pour plus de détails.</translation>
<translation>envoie un ordre à l&apos;application Linphone, voir --cli-help pour plus de détails</translation>
</message>
</context>
<context>
......
......@@ -100,9 +100,8 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U
mParser->showHelp();
}
if(mParser->isSet("cli-help")){
mCli = new Cli(this);
mCli->showHelp();
if (mParser->isSet("cli-help")) {
Cli::showHelp();
::exit(EXIT_SUCCESS);
}
......@@ -168,11 +167,10 @@ void App::initContentApp () {
setQuitOnLastWindowClosed(false);
// Deal with received messages and CLI.
mCli = new Cli(this);
QObject::connect(this, &App::receivedMessage, this, [this](int, const QByteArray &byteArray) {
QString command(byteArray);
qInfo() << QStringLiteral("Received command from other application: `%1`.").arg(command);
mCli->executeCommand(command);
Cli::executeCommand(command);
});
// Add plugins directory.
......@@ -190,7 +188,7 @@ void App::initContentApp () {
const QString commandArgument = getCommandArgument();
if (!commandArgument.isEmpty()) {
Cli::CommandFormat format;
mCli->executeCommand(commandArgument, &format);
Cli::executeCommand(commandArgument, &format);
if (format == Cli::UriFormat)
mustBeIconified = true;
}
......@@ -254,8 +252,7 @@ QString App::getCommandArgument () {
// -----------------------------------------------------------------------------
void App::executeCommand (const QString &command) {
Q_CHECK_PTR(mCli);
mCli->executeCommand(command);
Cli::executeCommand(command);
}
// -----------------------------------------------------------------------------
......@@ -333,10 +330,10 @@ void App::createParser () {
mParser = new QCommandLineParser();
mParser->setApplicationDescription(tr("applicationDescription"));
mParser->addPositionalArgument("command",tr("commandLineDescription"),"[command]");
mParser->addPositionalArgument("command", tr("commandLineDescription"), "[command]");
mParser->addOptions({
{ { "h", "help" }, tr("commandLineOptionHelp") },
{ "cli-help", tr("commandLineCliHelp") },
{ "cli-help", tr("commandLineOptionCliHelp") },
{ { "v", "version" }, tr("commandLineOptionVersion") },
{ "config", tr("commandLineOptionConfig"), tr("commandLineOptionConfigArg") },
#ifndef Q_OS_MACOS
......
......@@ -37,7 +37,6 @@
class QCommandLineParser;
class QSystemTrayIcon;
class Cli;
class DefaultTranslator;
class App : public SingleApplication {
......@@ -143,8 +142,6 @@ private:
Colors *mColors = nullptr;
QSystemTrayIcon *mSystemTrayIcon = nullptr;
Cli *mCli = nullptr;
};
#endif // APP_H_
This diff is collapsed.
......@@ -26,6 +26,7 @@
#include <memory>
#include <QHash>
#include <QMap>
#include <QObject>
// =============================================================================
......@@ -58,8 +59,8 @@ class Cli : public QObject {
Command () = default;
Command (
const QString &functionName,
const QString &functionDescription,
const QString &cliDescription,
const char *functionDescription,
const char *cliDescription,
Function function,
const QHash<QString, Argument> &argsScheme
);
......@@ -67,25 +68,23 @@ class Cli : public QObject {
void execute (QHash<QString, QString> &args) const;
void executeUri (const std::shared_ptr<linphone::Address> &address) const;
QString getFunctionDescription() {
const char *getFunctionDescription () const {
return mFunctionDescription;
}
QString getCliDescription() {
const char *getCliDescription () const {
return mCliDescription;
}
private:
QString mFunctionDescription;
QString mCliDescription;
QString mFunctionName;
const char *mFunctionDescription;
const char *mCliDescription;
Function mFunction = nullptr;
QHash<QString, Argument> mArgsScheme;
};
public:
Cli (QObject *parent = Q_NULLPTR);
~Cli () = default;
enum CommandFormat {
......@@ -94,23 +93,25 @@ public:
UriFormat
};
void executeCommand (const QString &command, CommandFormat *format = nullptr) const;
static void executeCommand (const QString &command, CommandFormat *format = nullptr);
void showHelp();
static void showHelp ();
private:
void addCommand (
Cli ();
static std::pair<QString, Command> createCommand (
const QString &functionName,
const QString &functionDescription,
const QString &cliDescription,
const char *functionDescription,
const char *cliDescription,
Function function,
const QHash<QString, Argument> &argsScheme = QHash<QString, Argument>()
);
QString parseFunctionName (const QString &command) const;
QHash<QString, QString> parseArgs (const QString &command) const;
static QString parseFunctionName (const QString &command);
static QHash<QString, QString> parseArgs (const QString &command);
QHash<QString, Command> mCommands;
static QMap<QString, Command> mCommands;
static QRegExp mRegExpArgs;
static QRegExp mRegExpFunctionName;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment