From a638c410ad83f8d8fe236c1500beaf63d69cbac6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:10:03 +0100 Subject: add count command --- akonadi2_cli/syntax_modules/akonadi_count.cpp | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 akonadi2_cli/syntax_modules/akonadi_count.cpp (limited to 'akonadi2_cli/syntax_modules/akonadi_count.cpp') diff --git a/akonadi2_cli/syntax_modules/akonadi_count.cpp b/akonadi2_cli/syntax_modules/akonadi_count.cpp new file mode 100644 index 0000000..40ad693 --- /dev/null +++ b/akonadi2_cli/syntax_modules/akonadi_count.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "akonadi_count.h" + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "akonadish_utils.h" + +namespace AkonadiCount +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); + + return syntax; +} + +bool count(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Akonadi2::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + + auto model = AkonadishUtils::loadModel(type, query); + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { + state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); + state.commandFinished(); + } + }); + + return true; +} + +} -- cgit v1.2.3