File indexing completed on 2024-04-14 15:46:43

0001 /*  This file is part of inqludeclient
0002  *  Copyright 2014  David Faure  <faure@kde.org>
0003  *
0004  *  This library is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU Lesser General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License or ( at
0007  *  your option ) version 3 or, at the discretion of KDE e.V. ( which shall
0008  *  act as a proxy as in section 14 of the GPLv3 ), any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Lesser General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "inqludeclient.h"
0022 #include "dataprovider.h"
0023 #include "downloadhandler.h"
0024 #include "listhandler.h"
0025 
0026 #include <QJsonDocument>
0027 #include <QCommandLineParser>
0028 #include <QDebug>
0029 
0030 #include <iostream>
0031 
0032 InqludeClient::InqludeClient()
0033 {
0034 }
0035 
0036 InqludeClient::~InqludeClient()
0037 {}
0038 
0039 int InqludeClient::run()
0040 {
0041     DataProvider::Ptr provider = DataProvider::createProvider();
0042 
0043     QCommandLineParser parser;
0044     parser.setApplicationDescription(tr("Command-line client for the inqlude.org repository of Qt libraries"));
0045     parser.addHelpOption();
0046     parser.addVersionOption();
0047     parser.addPositionalArgument("command", tr("Command: \n"
0048                 "\tlist               List libraries\n"
0049                 "\tdownload <lib>     Download a given library\n"));
0050     // This leads to --list or -list, it would be nice for QCommandLineParser to support "list" too, but how
0051     // would it know where to stop parsing options? I guess this would require builtin support for "one-word commands" then.
0052     //QCommandLineOption optList("list", tr("List libraries"));
0053     //parser.addOption(optList);
0054 
0055     parser.process(*qApp);
0056 
0057 
0058     const QStringList args = parser.positionalArguments();
0059     const QString command = args.isEmpty() ? QString() : args.first();
0060     if (command == QLatin1String("list")) {
0061         QTextStream outStream(stdout);
0062         ListHandler handler(outStream);
0063         handler.setQuitOnCompletion(true);
0064         connect(provider.data(), &DataProvider::dataAvailable, &handler, &ListHandler::list);
0065         ensureDataAvailable(provider);
0066         return qApp->exec();
0067     } else if (command == QLatin1String("download")) {
0068         if (args.count() < 2) {
0069             parser.showHelp(1);
0070         }
0071         const QString library = args.at(1);
0072         QTextStream errorStream(stdout);
0073         DownloadHandler handler(errorStream, library);
0074         handler.setQuitOnCompletion(true);
0075         connect(provider.data(), &DataProvider::dataAvailable, &handler, &DownloadHandler::download);
0076         ensureDataAvailable(provider);
0077         return qApp->exec();
0078     }
0079 
0080     parser.showHelp(1);
0081     return 1;
0082 }
0083 
0084 void InqludeClient::error()
0085 {
0086     qApp->exit(1);
0087 }
0088 
0089 void InqludeClient::ensureDataAvailable(DataProvider::Ptr provider)
0090 {
0091     provider->ensureDataAvailable();
0092     connect(provider.data(), &DataProvider::error, this, &InqludeClient::error);
0093 }