summaryrefslogtreecommitdiffstats
path: root/framework/domain/recepientautocompletionmodel.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-17 12:04:41 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-17 17:57:36 +0100
commit56d0411b0a8bce305220adbf12f6b859abf9f431 (patch)
treeec1e6aeb0bd1b73b6c59d48eb3fa9d623a69a0b4 /framework/domain/recepientautocompletionmodel.cpp
parentc633f7c5fce50db60035792526b6c7e84b51149f (diff)
downloadkube-56d0411b0a8bce305220adbf12f6b859abf9f431.tar.gz
kube-56d0411b0a8bce305220adbf12f6b859abf9f431.zip
AutocompleteLineEdit
Diffstat (limited to 'framework/domain/recepientautocompletionmodel.cpp')
-rw-r--r--framework/domain/recepientautocompletionmodel.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/framework/domain/recepientautocompletionmodel.cpp b/framework/domain/recepientautocompletionmodel.cpp
new file mode 100644
index 00000000..4e5fed95
--- /dev/null
+++ b/framework/domain/recepientautocompletionmodel.cpp
@@ -0,0 +1,110 @@
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
28RecipientAutocompletionModel::RecipientAutocompletionModel(QObject *parent)
29 : QSortFilterProxyModel(),
30 mSourceModel(new QStandardItemModel),
31 mTimer(new QTimer)
32{
33 setSourceModel(mSourceModel.data());
34 setDynamicSortFilter(true);
35 setFilterCaseSensitivity(Qt::CaseInsensitive);
36 mTimer->setSingleShot(true);
37 QObject::connect(mTimer.data(), &QTimer::timeout, this, &RecipientAutocompletionModel::save);
38
39 load();
40}
41
42RecipientAutocompletionModel::~RecipientAutocompletionModel()
43{
44 save();
45}
46
47static QString getPath()
48{
49 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kube/recepientautocompletion.ini";
50}
51
52void RecipientAutocompletionModel::save()
53{
54 QSet<QString> list;
55 for (int row = 0; row < mSourceModel->rowCount(); row++) {
56 list << mSourceModel->item(row)->data(Text).toString();
57 }
58
59 qWarning() << "Path " << getPath();
60 QSettings settings(getPath(), QSettings::IniFormat);
61 settings.setValue("list", QStringList{list.toList()});
62}
63
64void RecipientAutocompletionModel::load()
65{
66 qWarning() << "Path " << getPath();
67 QSettings settings(getPath(), QSettings::IniFormat);
68 auto list = settings.value("list").toStringList();
69 auto add = [] (const QString &n) {
70 auto item = new QStandardItem{n};
71 item->setData(n, Text);
72 return item;
73 };
74 for (const auto &entry : list) {
75 mSourceModel->appendRow(add(entry));
76 }
77}
78
79QHash< int, QByteArray > RecipientAutocompletionModel::roleNames() const
80{
81 QHash<int, QByteArray> roles;
82 roles[Text] = "text";
83 roles[Color] = "color";
84 return roles;
85}
86
87void RecipientAutocompletionModel::addEntry(const QByteArray &address, const QByteArray &name)
88{
89 auto add = [] (const QString &n) {
90 auto item = new QStandardItem{n};
91 item->setData(n, Text);
92 return item;
93 };
94 auto formattedName = [&] () {
95 if (name.isEmpty()) {
96 return QString(address);
97 }
98 return QString("%1 <%2>").arg(QString(address), QString(name));
99 }();
100 auto matches = mSourceModel->findItems(formattedName);
101 if (matches.isEmpty()) {
102 mSourceModel->appendRow(add(formattedName));
103 mTimer->start(100);
104 }
105}
106
107void RecipientAutocompletionModel::setFilter(const QString &filter)
108{
109 setFilterWildcard("*" + filter +"*");
110}