diff options
Diffstat (limited to 'sinksh/sinksh_utils.cpp')
-rw-r--r-- | sinksh/sinksh_utils.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/sinksh/sinksh_utils.cpp b/sinksh/sinksh_utils.cpp new file mode 100644 index 0000000..fa06b34 --- /dev/null +++ b/sinksh/sinksh_utils.cpp | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Aaron Seigo <aseigo@kolabsystems.com> | ||
3 | * Copyright (C) 2015 Christian Mollekopf <mollekopf@kolabsystems.com> | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include "sinksh_utils.h" | ||
22 | |||
23 | #include "common/clientapi.h" | ||
24 | |||
25 | #include "utils.h" | ||
26 | |||
27 | namespace SinkshUtils | ||
28 | { | ||
29 | |||
30 | static QStringList s_types = QStringList() << "resource" << "folder" << "mail" << "event"; | ||
31 | |||
32 | bool isValidStoreType(const QString &type) | ||
33 | { | ||
34 | return s_types.contains(type); | ||
35 | } | ||
36 | |||
37 | StoreBase &getStore(const QString &type) | ||
38 | { | ||
39 | if (type == "folder") { | ||
40 | static Store<Sink::ApplicationDomain::Folder> store; | ||
41 | return store; | ||
42 | } else if (type == "mail") { | ||
43 | static Store<Sink::ApplicationDomain::Mail> store; | ||
44 | return store; | ||
45 | } else if (type == "event") { | ||
46 | static Store<Sink::ApplicationDomain::Event> store; | ||
47 | return store; | ||
48 | } else if (type == "resource") { | ||
49 | static Store<Sink::ApplicationDomain::SinkResource> store; | ||
50 | return store; | ||
51 | } | ||
52 | |||
53 | //TODO: reinstate the warning+assert | ||
54 | //Q_ASSERT(false); | ||
55 | //qWarning() << "Trying to get a store that doesn't exist, falling back to event"; | ||
56 | static Store<Sink::ApplicationDomain::Event> store; | ||
57 | return store; | ||
58 | } | ||
59 | |||
60 | QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Sink::Query query) | ||
61 | { | ||
62 | if (type == "folder") { | ||
63 | query.requestedProperties << "name" << "parent"; | ||
64 | } else if (type == "mail") { | ||
65 | query.requestedProperties << "subject" << "folder" << "date"; | ||
66 | } else if (type == "event") { | ||
67 | query.requestedProperties << "summary"; | ||
68 | } else if (type == "resource") { | ||
69 | query.requestedProperties << "type"; | ||
70 | } | ||
71 | auto model = getStore(type).loadModel(query); | ||
72 | Q_ASSERT(model); | ||
73 | return model; | ||
74 | } | ||
75 | |||
76 | QStringList resourceIds(State &state) | ||
77 | { | ||
78 | QStringList resources; | ||
79 | Sink::Query query; | ||
80 | query.liveQuery = false; | ||
81 | auto model = SinkshUtils::loadModel("resource", query); | ||
82 | |||
83 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, &resources] (const QModelIndex &index, int start, int end) mutable { | ||
84 | for (int i = start; i <= end; i++) { | ||
85 | auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); | ||
86 | resources << object->identifier(); | ||
87 | } | ||
88 | }); | ||
89 | |||
90 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | ||
91 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | ||
92 | state.commandFinished(); | ||
93 | } | ||
94 | }); | ||
95 | |||
96 | state.commandStarted(); | ||
97 | |||
98 | return resources; | ||
99 | } | ||
100 | |||
101 | QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state) | ||
102 | { | ||
103 | return Utils::filteredCompletions(resourceIds(state), fragment); | ||
104 | } | ||
105 | |||
106 | QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state) | ||
107 | { | ||
108 | static QStringList types = QStringList() << "resource" << "folder" << "mail" << "event"; | ||
109 | if (commands.count() == 1) { | ||
110 | return Utils::filteredCompletions(s_types, fragment); | ||
111 | } | ||
112 | |||
113 | return Utils::filteredCompletions(resourceIds(state), fragment); | ||
114 | } | ||
115 | |||
116 | QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state) | ||
117 | { | ||
118 | return Utils::filteredCompletions(s_types, fragment); | ||
119 | } | ||
120 | |||
121 | QMap<QString, QString> keyValueMapFromArgs(const QStringList &args) | ||
122 | { | ||
123 | //TODO: this is not the most clever of algorithms. preserved during the port of commands | ||
124 | // from sink_client ... we can probably do better, however ;) | ||
125 | QMap<QString, QString> map; | ||
126 | for (int i = 0; i + 2 <= args.size(); i += 2) { | ||
127 | map.insert(args.at(i), args.at(i + 1)); | ||
128 | } | ||
129 | |||
130 | return map; | ||
131 | } | ||
132 | |||
133 | } | ||
134 | |||