diff options
Diffstat (limited to 'framework/src/domain/recepientautocompletionmodel.cpp')
-rw-r--r-- | framework/src/domain/recepientautocompletionmodel.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/framework/src/domain/recepientautocompletionmodel.cpp b/framework/src/domain/recepientautocompletionmodel.cpp new file mode 100644 index 00000000..2f52db07 --- /dev/null +++ b/framework/src/domain/recepientautocompletionmodel.cpp | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "recepientautocompletionmodel.h" | ||
20 | |||
21 | #include <QStandardItemModel> | ||
22 | #include <QSettings> | ||
23 | #include <QStandardPaths> | ||
24 | #include <QSet> | ||
25 | #include <QDebug> | ||
26 | #include <QTimer> | ||
27 | #include <sink/store.h> | ||
28 | #include <sink/applicationdomaintype.h> | ||
29 | |||
30 | using namespace Sink::ApplicationDomain; | ||
31 | |||
32 | RecipientAutocompletionModel::RecipientAutocompletionModel(QObject *parent) | ||
33 | : QSortFilterProxyModel(), | ||
34 | mSourceModel(new QStandardItemModel), | ||
35 | mTimer(new QTimer) | ||
36 | { | ||
37 | setSourceModel(mSourceModel.data()); | ||
38 | setDynamicSortFilter(true); | ||
39 | setFilterCaseSensitivity(Qt::CaseInsensitive); | ||
40 | mTimer->setSingleShot(true); | ||
41 | QObject::connect(mTimer.data(), &QTimer::timeout, this, &RecipientAutocompletionModel::save); | ||
42 | |||
43 | load(); | ||
44 | } | ||
45 | |||
46 | RecipientAutocompletionModel::~RecipientAutocompletionModel() | ||
47 | { | ||
48 | save(); | ||
49 | } | ||
50 | |||
51 | static QString getPath() | ||
52 | { | ||
53 | return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kube/recepientautocompletion.ini"; | ||
54 | } | ||
55 | |||
56 | void RecipientAutocompletionModel::save() | ||
57 | { | ||
58 | QSet<QString> list; | ||
59 | for (int row = 0; row < mSourceModel->rowCount(); row++) { | ||
60 | list << mSourceModel->item(row)->data(Text).toString(); | ||
61 | } | ||
62 | |||
63 | qWarning() << "Path " << getPath(); | ||
64 | QSettings settings(getPath(), QSettings::IniFormat); | ||
65 | settings.setValue("list", QStringList{list.toList()}); | ||
66 | } | ||
67 | |||
68 | void RecipientAutocompletionModel::load() | ||
69 | { | ||
70 | qWarning() << "Path " << getPath(); | ||
71 | QSettings settings(getPath(), QSettings::IniFormat); | ||
72 | auto list = settings.value("list").toStringList(); | ||
73 | auto add = [] (const QString &n) { | ||
74 | auto item = new QStandardItem{n}; | ||
75 | item->setData(n, Text); | ||
76 | return item; | ||
77 | }; | ||
78 | for (const auto &entry : list) { | ||
79 | mSourceModel->appendRow(add(entry)); | ||
80 | } | ||
81 | Sink::Query query; | ||
82 | query.request<Contact::Fn>(); | ||
83 | query.request<Contact::Emails>(); | ||
84 | Sink::Store::fetchAll<Contact>(query) | ||
85 | .then([this] (const QList<Contact::Ptr> &list) { | ||
86 | for (const auto &c : list) { | ||
87 | for (const auto &email : c->getEmails()) { | ||
88 | addToModel(email.email, c->getFn()); | ||
89 | } | ||
90 | } | ||
91 | }).exec(); | ||
92 | } | ||
93 | |||
94 | QHash< int, QByteArray > RecipientAutocompletionModel::roleNames() const | ||
95 | { | ||
96 | QHash<int, QByteArray> roles; | ||
97 | roles[Text] = "text"; | ||
98 | roles[Color] = "color"; | ||
99 | return roles; | ||
100 | } | ||
101 | |||
102 | |||
103 | bool RecipientAutocompletionModel::addToModel(const QString &address, const QString &name) | ||
104 | { | ||
105 | auto add = [] (const QString &n) { | ||
106 | auto item = new QStandardItem{n}; | ||
107 | item->setData(n, Text); | ||
108 | return item; | ||
109 | }; | ||
110 | auto formattedName = [&] () { | ||
111 | if (name.isEmpty()) { | ||
112 | return QString(address); | ||
113 | } | ||
114 | return QString("%1 <%2>").arg(QString(address), QString(name)); | ||
115 | }(); | ||
116 | auto matches = mSourceModel->findItems(formattedName); | ||
117 | if (matches.isEmpty()) { | ||
118 | mSourceModel->appendRow(add(formattedName)); | ||
119 | return true; | ||
120 | } | ||
121 | return false; | ||
122 | } | ||
123 | |||
124 | void RecipientAutocompletionModel::addEntry(const QByteArray &address, const QByteArray &name) | ||
125 | { | ||
126 | if (addToModel(address, name)) { | ||
127 | mTimer->start(100); | ||
128 | } | ||
129 | } | ||
130 | |||
131 | void RecipientAutocompletionModel::setFilter(const QString &filter) | ||
132 | { | ||
133 | setFilterWildcard("*" + filter +"*"); | ||
134 | } | ||