diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-01-20 19:07:07 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-01-20 19:07:07 +0100 |
commit | bdb01c2c068df326f5a8328ed1492ab1bea388c5 (patch) | |
tree | 25c2ee1b29bc481b6914c244ed9ca194b1415d16 /sinksh/syntax_modules/sink_count.cpp | |
parent | 17e7ee40c9185c0505883853345fd6024c675b1a (diff) | |
download | sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.tar.gz sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.zip |
Renamed Akonadi2 to Sink
(except for documentation).
Diffstat (limited to 'sinksh/syntax_modules/sink_count.cpp')
-rw-r--r-- | sinksh/syntax_modules/sink_count.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/sink_count.cpp b/sinksh/syntax_modules/sink_count.cpp new file mode 100644 index 0000000..fde7c33 --- /dev/null +++ b/sinksh/syntax_modules/sink_count.cpp | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include <QCoreApplication> | ||
21 | #include <QDebug> | ||
22 | #include <QObject> // tr() | ||
23 | #include <QModelIndex> | ||
24 | #include <QTime> | ||
25 | |||
26 | #include "common/resource.h" | ||
27 | #include "common/storage.h" | ||
28 | #include "common/domain/event.h" | ||
29 | #include "common/domain/folder.h" | ||
30 | #include "common/resourceconfig.h" | ||
31 | #include "common/log.h" | ||
32 | #include "common/storage.h" | ||
33 | #include "common/definitions.h" | ||
34 | |||
35 | #include "sinksh_utils.h" | ||
36 | #include "state.h" | ||
37 | #include "syntaxtree.h" | ||
38 | |||
39 | namespace SinkCount | ||
40 | { | ||
41 | |||
42 | bool count(const QStringList &args, State &state) | ||
43 | { | ||
44 | auto resources = args; | ||
45 | auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); | ||
46 | |||
47 | if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { | ||
48 | state.printError(QObject::tr("Unknown type: %1").arg(type)); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | Sink::Query query; | ||
53 | for (const auto &res : resources) { | ||
54 | query.resources << res.toLatin1(); | ||
55 | } | ||
56 | query.liveQuery = false; | ||
57 | |||
58 | auto model = SinkshUtils::loadModel(type, query); | ||
59 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | ||
60 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | ||
61 | state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); | ||
62 | state.commandFinished(); | ||
63 | } | ||
64 | }); | ||
65 | |||
66 | if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { | ||
67 | return true; | ||
68 | } | ||
69 | |||
70 | return true; | ||
71 | } | ||
72 | |||
73 | Syntax::List syntax() | ||
74 | { | ||
75 | Syntax count("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count <type> <resource>"), &SinkCount::count, Syntax::EventDriven); | ||
76 | count.completer = &SinkshUtils::typeCompleter; | ||
77 | |||
78 | return Syntax::List() << count; | ||
79 | } | ||
80 | |||
81 | REGISTER_SYNTAX(SinkCount) | ||
82 | |||
83 | } | ||