File indexing completed on 2024-04-28 09:36:42

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 "listhandler.h"
0022 
0023 #include <QCoreApplication>
0024 #include <QTextStream>
0025 #include <QJsonArray>
0026 #include <QJsonDocument>
0027 #include <QJsonObject>
0028 
0029 ListHandler::ListHandler(QTextStream &stream)
0030     : m_stream(stream)
0031 {
0032 }
0033 
0034 void ListHandler::list(const QJsonDocument &doc)
0035 {
0036     const QJsonArray libs = doc.array();
0037     int longestName = 0;
0038     for (int i = 0; i < libs.count(); ++i) {
0039         const QJsonObject details = libs.at(i).toObject();
0040         const QString name = details.value("name").toString();
0041         longestName = qMax(longestName, name.length());
0042     }
0043     for (int i = 0; i < libs.count(); ++i) {
0044         const QJsonObject details = libs.at(i).toObject();
0045         const QString name = details.value("name").toString();
0046         const QString summary = details.value("summary").toString();
0047         m_stream << qPrintable(name.leftJustified(longestName + 1)) << qPrintable(summary) << '\n';
0048     }
0049     m_stream.flush();
0050     handlingCompleted();
0051 }