From 56d0411b0a8bce305220adbf12f6b859abf9f431 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 17 Nov 2016 12:04:41 +0100 Subject: AutocompleteLineEdit --- framework/domain/CMakeLists.txt | 1 + framework/domain/composercontroller.cpp | 106 ++++++++++++++++----- framework/domain/composercontroller.h | 8 ++ framework/domain/recepientautocompletionmodel.cpp | 110 ++++++++++++++++++++++ framework/domain/recepientautocompletionmodel.h | 55 +++++++++++ 5 files changed, 259 insertions(+), 21 deletions(-) create mode 100644 framework/domain/recepientautocompletionmodel.cpp create mode 100644 framework/domain/recepientautocompletionmodel.h (limited to 'framework/domain') diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index 094eac04..24fdb49f 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -18,6 +18,7 @@ set(mailplugin_SRCS accountsmodel.cpp outboxmodel.cpp identitiesmodel.cpp + recepientautocompletionmodel.cpp settings/accountsettings.cpp ) find_package(KF5 REQUIRED COMPONENTS Package) diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index b2ad7ecc..7fd2593d 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp @@ -25,14 +25,20 @@ #include #include #include +#include +#include #include #include #include +#include #include "accountsmodel.h" #include "identitiesmodel.h" +#include "recepientautocompletionmodel.h" #include "mailtemplates.h" +SINK_DEBUG_AREA("composercontroller"); + ComposerController::ComposerController(QObject *parent) : QObject(parent) { } @@ -102,6 +108,18 @@ void ComposerController::setBody(const QString &body) } } +QString ComposerController::recepientSearchString() const +{ + return QString(); +} + +void ComposerController::setRecepientSearchString(const QString &s) +{ + if (auto model = static_cast(recepientAutocompletionModel())) { + model->setFilter(s); + } +} + QAbstractItemModel *ComposerController::identityModel() const { static auto model = new IdentitiesModel(); @@ -109,6 +127,13 @@ QAbstractItemModel *ComposerController::identityModel() const return model; } +QAbstractItemModel *ComposerController::recepientAutocompletionModel() const +{ + static auto model = new RecipientAutocompletionModel(); + QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); + return model; +} + QStringList ComposerController::attachemts() const { return m_attachments; @@ -153,43 +178,82 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) }).exec(); } -KMime::Message::Ptr ComposerController::assembleMessage() +void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName) { - auto mail = m_msg.value(); - if (!mail) { - mail = KMime::Message::Ptr::create(); + if (auto model = static_cast(recepientAutocompletionModel())) { + model->addEntry(addrSpec, displayName); } - for (const auto &to : KEmailAddress::splitAddressList(m_to)) { +} + +void applyAddresses(const QString &list, std::function callback) +{ + for (const auto &to : KEmailAddress::splitAddressList(list)) { QByteArray displayName; QByteArray addrSpec; QByteArray comment; KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); + callback(addrSpec, displayName); + } +} + +bool ComposerController::identityIsSet() const +{ + return (identityModel()->rowCount() > 0) && (m_currentAccountIndex >= 0); +} + +KMime::Message::Ptr ComposerController::assembleMessage() +{ + auto mail = m_msg.value(); + if (!mail) { + mail = KMime::Message::Ptr::create(); + } + applyAddresses(m_to, [&](const QByteArray &addrSpec, const QByteArray &displayName) { mail->to(true)->addAddress(addrSpec, displayName); + recordForAutocompletion(addrSpec, displayName); + }); + applyAddresses(m_cc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { + mail->cc(true)->addAddress(addrSpec, displayName); + recordForAutocompletion(addrSpec, displayName); + }); + applyAddresses(m_bcc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { + mail->bcc(true)->addAddress(addrSpec, displayName); + recordForAutocompletion(addrSpec, displayName); + }); + if (!identityIsSet()) { + SinkWarning() << "We don't have an identity to send the mail with."; + } else { + auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); + KMime::Types::Mailbox mb; + mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); + mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); + mail->from(true)->addAddress(mb); + mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); + mail->setBody(m_body.toUtf8()); + mail->assemble(); + return mail; } - auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); - KMime::Types::Mailbox mb; - mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); - mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); - mail->from(true)->addAddress(mb); - mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); - mail->setBody(m_body.toUtf8()); - mail->assemble(); - return mail; + return KMime::Message::Ptr(); } void ComposerController::send() { auto mail = assembleMessage(); - auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); - Kube::Context context; - context.setProperty("message", QVariant::fromValue(mail)); - context.setProperty("accountId", QVariant::fromValue(currentAccountId)); + //TODO deactivate action if we don't have the identiy set + if (!identityIsSet()) { + SinkWarning() << "We don't have an identity to send the mail with."; + } else { + auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); - qDebug() << "Current account " << currentAccountId; + Kube::Context context; + context.setProperty("message", QVariant::fromValue(mail)); + context.setProperty("accountId", QVariant::fromValue(currentAccountId)); - Kube::Action("org.kde.kube.actions.sendmail", context).execute(); - clear(); + qDebug() << "Current account " << currentAccountId; + + Kube::Action("org.kde.kube.actions.sendmail", context).execute(); + clear(); + } } void ComposerController::saveAsDraft() diff --git a/framework/domain/composercontroller.h b/framework/domain/composercontroller.h index 8390c639..aa2ae0d7 100644 --- a/framework/domain/composercontroller.h +++ b/framework/domain/composercontroller.h @@ -38,6 +38,8 @@ class ComposerController : public QObject Q_PROPERTY (QString bcc READ bcc WRITE setBcc NOTIFY bccChanged) Q_PROPERTY (QString subject READ subject WRITE setSubject NOTIFY subjectChanged) Q_PROPERTY (QString body READ body WRITE setBody NOTIFY bodyChanged) + Q_PROPERTY (QString recepientSearchString READ recepientSearchString WRITE setRecepientSearchString) + Q_PROPERTY (QAbstractItemModel* recepientAutocompletionModel READ recepientAutocompletionModel CONSTANT) Q_PROPERTY (QAbstractItemModel* identityModel READ identityModel CONSTANT) Q_PROPERTY (int currentIdentityIndex MEMBER m_currentAccountIndex) Q_PROPERTY (QStringList attachments READ attachemts NOTIFY attachmentsChanged) @@ -60,7 +62,11 @@ public: QString body() const; void setBody(const QString &body); + QString recepientSearchString() const; + void setRecepientSearchString(const QString &body); + QAbstractItemModel *identityModel() const; + QAbstractItemModel *recepientAutocompletionModel() const; QStringList attachemts() const; Q_INVOKABLE void loadMessage(const QVariant &draft, bool loadAsDraft); @@ -81,6 +87,8 @@ public slots: void addAttachment(const QUrl &fileUrl); private: + bool identityIsSet() const; + void recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName); void setMessage(const QSharedPointer &msg); QSharedPointer assembleMessage(); QString m_to; 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 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "recepientautocompletionmodel.h" + +#include +#include +#include +#include +#include +#include + +RecipientAutocompletionModel::RecipientAutocompletionModel(QObject *parent) + : QSortFilterProxyModel(), + mSourceModel(new QStandardItemModel), + mTimer(new QTimer) +{ + setSourceModel(mSourceModel.data()); + setDynamicSortFilter(true); + setFilterCaseSensitivity(Qt::CaseInsensitive); + mTimer->setSingleShot(true); + QObject::connect(mTimer.data(), &QTimer::timeout, this, &RecipientAutocompletionModel::save); + + load(); +} + +RecipientAutocompletionModel::~RecipientAutocompletionModel() +{ + save(); +} + +static QString getPath() +{ + return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kube/recepientautocompletion.ini"; +} + +void RecipientAutocompletionModel::save() +{ + QSet list; + for (int row = 0; row < mSourceModel->rowCount(); row++) { + list << mSourceModel->item(row)->data(Text).toString(); + } + + qWarning() << "Path " << getPath(); + QSettings settings(getPath(), QSettings::IniFormat); + settings.setValue("list", QStringList{list.toList()}); +} + +void RecipientAutocompletionModel::load() +{ + qWarning() << "Path " << getPath(); + QSettings settings(getPath(), QSettings::IniFormat); + auto list = settings.value("list").toStringList(); + auto add = [] (const QString &n) { + auto item = new QStandardItem{n}; + item->setData(n, Text); + return item; + }; + for (const auto &entry : list) { + mSourceModel->appendRow(add(entry)); + } +} + +QHash< int, QByteArray > RecipientAutocompletionModel::roleNames() const +{ + QHash roles; + roles[Text] = "text"; + roles[Color] = "color"; + return roles; +} + +void RecipientAutocompletionModel::addEntry(const QByteArray &address, const QByteArray &name) +{ + auto add = [] (const QString &n) { + auto item = new QStandardItem{n}; + item->setData(n, Text); + return item; + }; + auto formattedName = [&] () { + if (name.isEmpty()) { + return QString(address); + } + return QString("%1 <%2>").arg(QString(address), QString(name)); + }(); + auto matches = mSourceModel->findItems(formattedName); + if (matches.isEmpty()) { + mSourceModel->appendRow(add(formattedName)); + mTimer->start(100); + } +} + +void RecipientAutocompletionModel::setFilter(const QString &filter) +{ + setFilterWildcard("*" + filter +"*"); +} diff --git a/framework/domain/recepientautocompletionmodel.h b/framework/domain/recepientautocompletionmodel.h new file mode 100644 index 00000000..7e89e513 --- /dev/null +++ b/framework/domain/recepientautocompletionmodel.h @@ -0,0 +1,55 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#pragma once + +#include +#include + +class QStandardItemModel; +class QTimer; + +class RecipientAutocompletionModel : public QSortFilterProxyModel +{ + Q_OBJECT + +public: + RecipientAutocompletionModel(QObject *parent = Q_NULLPTR); + ~RecipientAutocompletionModel(); + + enum Roles { + Text = Qt::UserRole + 1, + Color + }; + Q_ENUMS(Roles) + + QHash roleNames() const Q_DECL_OVERRIDE; + + void addEntry(const QByteArray &address, const QByteArray &name); + void setFilter(const QString &); + +private slots: + void save(); + +private: + void load(); + + QScopedPointer mSourceModel; + QScopedPointer mTimer; +}; -- cgit v1.2.3