diff options
Diffstat (limited to 'akonadish/akonadish_utils.cpp')
-rw-r--r-- | akonadish/akonadish_utils.cpp | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp index ffbdcb3..070d788 100644 --- a/akonadish/akonadish_utils.cpp +++ b/akonadish/akonadish_utils.cpp | |||
@@ -25,10 +25,11 @@ | |||
25 | namespace AkonadishUtils | 25 | namespace AkonadishUtils |
26 | { | 26 | { |
27 | 27 | ||
28 | static QStringList s_types = QStringList() << "resource" << "folder" << "mail" << "event"; | ||
29 | |||
28 | bool isValidStoreType(const QString &type) | 30 | bool isValidStoreType(const QString &type) |
29 | { | 31 | { |
30 | static const QSet<QString> types = QSet<QString>() << "folder" << "mail" << "event" << "resource"; | 32 | return s_types.contains(type); |
31 | return types.contains(type); | ||
32 | } | 33 | } |
33 | 34 | ||
34 | StoreBase &getStore(const QString &type) | 35 | StoreBase &getStore(const QString &type) |
@@ -70,6 +71,69 @@ QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Quer | |||
70 | return model; | 71 | return model; |
71 | } | 72 | } |
72 | 73 | ||
74 | QStringList resourceIds(State &state) | ||
75 | { | ||
76 | QStringList resources; | ||
77 | Akonadi2::Query query; | ||
78 | query.syncOnDemand = false; | ||
79 | query.processAll = false; | ||
80 | query.liveQuery = false; | ||
81 | auto model = AkonadishUtils::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), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::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(Akonadi2::Store::ChildrenFetchedRole)) { | ||
92 | state.commandFinished(); | ||
93 | } | ||
94 | }); | ||
95 | |||
96 | state.commandStarted(); | ||
97 | |||
98 | return resources; | ||
99 | } | ||
100 | |||
101 | QStringList filtered(const QStringList &list, const QString &fragment) | ||
102 | { | ||
103 | if (fragment.isEmpty()) { | ||
104 | return list; | ||
105 | } | ||
106 | |||
107 | QStringList filtered; | ||
108 | for (auto item: list) { | ||
109 | if (item.startsWith(fragment)) { | ||
110 | filtered << item; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return filtered; | ||
115 | } | ||
116 | |||
117 | QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state) | ||
118 | { | ||
119 | return filtered(resourceIds(state), fragment); | ||
120 | } | ||
121 | |||
122 | QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state) | ||
123 | { | ||
124 | static QStringList types = QStringList() << "resource" << "folder" << "mail" << "event"; | ||
125 | if (commands.count() == 1) { | ||
126 | return filtered(s_types, fragment); | ||
127 | } | ||
128 | |||
129 | return filtered(resourceIds(state), fragment); | ||
130 | } | ||
131 | |||
132 | QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state) | ||
133 | { | ||
134 | return filtered(s_types, fragment); | ||
135 | } | ||
136 | |||
73 | QMap<QString, QString> keyValueMapFromArgs(const QStringList &args) | 137 | QMap<QString, QString> keyValueMapFromArgs(const QStringList &args) |
74 | { | 138 | { |
75 | //TODO: this is not the most clever of algorithms. preserved during the port of commands | 139 | //TODO: this is not the most clever of algorithms. preserved during the port of commands |