File indexing completed on 2024-06-16 05:01:12

0001 /*
0002  * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include <QCoreApplication>
0021 #include <QDebug>
0022 #include <QObject> // tr()
0023 #include <QModelIndex>
0024 #include <QTime>
0025 
0026 #include "common/resource.h"
0027 #include "common/storage.h"
0028 #include "common/resourceconfig.h"
0029 #include "common/log.h"
0030 #include "common/storage.h"
0031 #include "common/definitions.h"
0032 
0033 #include "sinksh_utils.h"
0034 #include "state.h"
0035 #include "syntaxtree.h"
0036 
0037 namespace SinkCount
0038 {
0039 
0040 Syntax::List syntax();
0041 
0042 bool count(const QStringList &args, State &state)
0043 {
0044     Sink::Query query;
0045     query.setId("count");
0046     if (!SinkshUtils::applyFilter(query, SyntaxTree::parseOptions(args))) {
0047         state.printError(syntax()[0].usage());
0048         return false;
0049     }
0050 
0051     auto model = SinkshUtils::loadModel(query.type(), query);
0052     QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
0053         if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
0054             state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex())));
0055             state.commandFinished();
0056         }
0057     });
0058 
0059     if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
0060         return true;
0061     }
0062 
0063     return true;
0064 }
0065 
0066 Syntax::List syntax()
0067 {
0068     Syntax count("count", QObject::tr("Returns the number of items of a given type in a resource"), &SinkCount::count, Syntax::EventDriven);
0069     count.addPositionalArgument({"type", "The entity type to count"});
0070     count.addPositionalArgument({"resource", "A resource id where to count", false});
0071     count.completer = &SinkshUtils::typeCompleter;
0072     return Syntax::List() << count;
0073 }
0074 
0075 REGISTER_SYNTAX(SinkCount)
0076 
0077 }