1 /*
2  * libqtxdg - An Qt implementation of freedesktop.org xdg specs
3  * Copyright (C) 2018  Luís Pereira <luis.artur.pereira@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  */
20 
21 #include "matcommandmanager.h"
22 #include "mimetypematcommand.h"
23 #include "defappmatcommand.h"
24 #include "openmatcommand.h"
25 #include "defwebbrowsermatcommand.h"
26 #include "defemailclientmatcommand.h"
27 #include "deffilemanagermatcommand.h"
28 #include "defterminalmatcommand.h"
29 
30 #include "xdgmacros.h"
31 
32 #include <QCoreApplication>
33 #include <QCommandLineOption>
34 #include <QCommandLineParser>
35 #include <QDebug>
36 
37 extern void Q_CORE_EXPORT qt_call_post_routines();
38 
39 [[noreturn]] void showHelp(const QString &parserHelp, const QString &commandsDescription, int exitCode = 0);
40 
showHelp(const QString & parserHelp,const QString & commandsDescription,int exitCode)41 [[noreturn]] void showHelp(const QString &parserHelp, const QString &commandsDescription, int exitCode)
42 {
43     QString text;
44     const QLatin1Char nl('\n');
45 
46     text.append(parserHelp);
47     text.append(nl);
48     text.append(QCoreApplication::tr("Available commands:\n"));
49     text.append(commandsDescription);
50     text.append(nl);
51     fputs(qPrintable(text), stdout);
52 
53     qt_call_post_routines();
54     ::exit(exitCode);
55 }
56 
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59     QCoreApplication app(argc, argv);
60     int runResult = 0;
61     app.setApplicationName(QSL("qtxdg-mat"));
62     app.setApplicationVersion(QSL(QTXDG_VERSION));
63     app.setOrganizationName(QSL("LXQt"));
64     app.setOrganizationDomain(QSL("lxqt.org"));
65 
66     QCommandLineParser parser;
67     parser.setApplicationDescription(QSL("QtXdg MimeApps Tool"));
68 
69     parser.addPositionalArgument(QSL("command"),
70                                  QSL("Command to execute."));
71 
72     QScopedPointer<MatCommandManager> manager(new MatCommandManager());
73 
74     MatCommandInterface *const mimeCmd = new DefAppMatCommand(&parser);
75     manager->add(mimeCmd);
76 
77     MatCommandInterface *const openCmd = new OpenMatCommand(&parser);
78     manager->add(openCmd);
79 
80     MatCommandInterface *const mimeTypeCmd = new MimeTypeMatCommand(&parser);
81     manager->add(mimeTypeCmd);
82 
83     MatCommandInterface *const defWebBrowserCmd = new DefWebBrowserMatCommand(&parser);
84     manager->add(defWebBrowserCmd);
85 
86     MatCommandInterface *const defEmailClientCmd = new DefEmailClientMatCommand(&parser);
87     manager->add(defEmailClientCmd);
88 
89     MatCommandInterface *const defFileManagerCmd = new DefFileManagerMatCommand(&parser);
90     manager->add(defFileManagerCmd);
91 
92     MatCommandInterface *const defTerminalCmd = new DefTerminalMatCommand(&parser);
93     manager->add(defTerminalCmd);
94 
95     // Find out the positional arguments.
96     parser.parse(QCoreApplication::arguments());
97     const QStringList args = parser.positionalArguments();
98     const QString command = args.isEmpty() ? QString() : args.first();
99     bool cmdFound = false;
100 
101     const QList <MatCommandInterface *> commands = manager->commands();
102     for (auto *const cmd : commands) {
103         if (command == cmd->name()) {
104             cmdFound = true;
105             runResult = cmd->run(args);
106         }
107         if (cmdFound)
108             break;
109     }
110 
111     if (!cmdFound) {
112         const QCommandLineOption helpOption = parser.addHelpOption();
113         const QCommandLineOption versionOption = parser.addVersionOption();
114         parser.parse(QCoreApplication::arguments());
115         if (parser.isSet(helpOption)) {
116             showHelp(parser.helpText(), manager->descriptionsHelpText(), EXIT_SUCCESS);
117             Q_UNREACHABLE();
118         }
119         if (parser.isSet(versionOption)) {
120             parser.showVersion();
121             Q_UNREACHABLE();
122         }
123         showHelp(parser.helpText(), manager->descriptionsHelpText(), EXIT_FAILURE);
124         Q_UNREACHABLE();
125     } else {
126         return runResult;
127     }
128 }
129