From a204de1c989067a6aff95e81949405a45e8cbe3f Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 17 Apr 2016 12:28:56 +0200 Subject: Show identities instead of accounts --- framework/domain/CMakeLists.txt | 1 + framework/domain/composercontroller.cpp | 16 +++--- framework/domain/identitiesmodel.cpp | 93 +++++++++++++++++++++++++++++++++ framework/domain/identitiesmodel.h | 60 +++++++++++++++++++++ 4 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 framework/domain/identitiesmodel.cpp create mode 100644 framework/domain/identitiesmodel.h (limited to 'framework') diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index f40463be..b3d47750 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -15,6 +15,7 @@ set(mailplugin_SRCS accountfactory.cpp accountscontroller.cpp accountsmodel.cpp + identitiesmodel.cpp ) add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index a383de26..bca90d33 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp @@ -29,6 +29,7 @@ #include #include "accountsmodel.h" +#include "identitiesmodel.h" #include "mailtemplates.h" ComposerController::ComposerController(QObject *parent) : QObject(parent) @@ -102,9 +103,9 @@ void ComposerController::setBody(const QString &body) QAbstractItemModel *ComposerController::identityModel() const { - static auto accountsModel = new AccountsModel(); - QQmlEngine::setObjectOwnership(accountsModel, QQmlEngine::CppOwnership); - return accountsModel;; + static auto model = new IdentitiesModel(); + QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); + return model; } QStringList ComposerController::attachemts() const @@ -155,8 +156,11 @@ KMime::Message::Ptr ComposerController::assembleMessage() KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); mail->to(true)->addAddress(addrSpec, displayName); } - //FIXME set "from" from identity (or do that in the action directly?) - // mail->from(true)->addAddress("test@example.com", "John Doe"); + 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(); @@ -166,7 +170,7 @@ KMime::Message::Ptr ComposerController::assembleMessage() void ComposerController::send() { auto mail = assembleMessage(); - auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(AccountsModel::AccountId).toByteArray(); + auto currentAccountId = identityModel()->index(m_currentAccountIndex, 0).data(IdentitiesModel::AccountId).toByteArray(); Kube::Context context; context.setProperty("message", QVariant::fromValue(mail)); diff --git a/framework/domain/identitiesmodel.cpp b/framework/domain/identitiesmodel.cpp new file mode 100644 index 00000000..adb5d625 --- /dev/null +++ b/framework/domain/identitiesmodel.cpp @@ -0,0 +1,93 @@ +/* + 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 "identitiesmodel.h" +#include + +IdentitiesModel::IdentitiesModel(QObject *parent) : QIdentityProxyModel() +{ + Sink::Query query; + query.liveQuery = true; + query.requestedProperties << "name" << "username" << "address" << "account"; + runQuery(query); +} + +IdentitiesModel::~IdentitiesModel() +{ + +} + +QHash< int, QByteArray > IdentitiesModel::roleNames() const +{ + QHash roles; + + roles[Name] = "name"; + roles[Username] = "username"; + roles[Address] = "address"; + roles[IdentityId] = "identityId"; + roles[AccountId] = "accountId"; + roles[AccountName] = "accountName"; + roles[AccountIcon] = "accountIcon"; + roles[DisplayName] = "displayName"; + + return roles; +} + +QVariant IdentitiesModel::data(const QModelIndex &idx, int role) const +{ + auto srcIdx = mapToSource(idx); + switch (role) { + case Name: + return srcIdx.sibling(srcIdx.row(), 0).data(Qt::DisplayRole).toString(); + case Username: + return srcIdx.sibling(srcIdx.row(), 1).data(Qt::DisplayRole).toString(); + case Address: + return srcIdx.sibling(srcIdx.row(), 2).data(Qt::DisplayRole).toString(); + case IdentityId: + return srcIdx.data(Sink::Store::DomainObjectBaseRole).value()->identifier(); + case AccountId: + return srcIdx.data(Sink::Store::DomainObjectBaseRole).value()->getProperty("account").toByteArray(); + case AccountName: { + const auto accountId = srcIdx.sibling(srcIdx.row(), 3).data(Qt::DisplayRole).toByteArray(); + return mAccountNames.value(accountId); + } + case AccountIcon: { + const auto accountId = srcIdx.sibling(srcIdx.row(), 3).data(Qt::DisplayRole).toByteArray(); + return mAccountIcons.value(accountId); + } + case DisplayName: { + return data(idx, AccountName).toString() + ": " + data(idx, Username).toString() + ", " + data(idx, Address).toString(); + } + } + return QIdentityProxyModel::data(idx, role); +} + +void IdentitiesModel::runQuery(const Sink::Query &query) +{ + mModel = Sink::Store::loadModel(query); + setSourceModel(mModel.data()); + + Sink::Store::fetchAll(Sink::Query()) + .then >([this](const QList &accounts) { + for (const auto &account : accounts) { + mAccountNames.insert(account->identifier(), account->getProperty("name").toString()); + mAccountIcons.insert(account->identifier(), account->getProperty("icon").toString()); + } + emit layoutChanged(); + }).exec(); +} diff --git a/framework/domain/identitiesmodel.h b/framework/domain/identitiesmodel.h new file mode 100644 index 00000000..9b172251 --- /dev/null +++ b/framework/domain/identitiesmodel.h @@ -0,0 +1,60 @@ +/* + 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 +#include +#include + +namespace Sink { + class Query; +} + +class IdentitiesModel : public QIdentityProxyModel +{ + Q_OBJECT + +public: + IdentitiesModel(QObject *parent = Q_NULLPTR); + ~IdentitiesModel(); + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + + enum Roles { + Name = Qt::UserRole + 1, + Username, + Address, + IdentityId, + AccountId, + AccountName, + AccountIcon, + DisplayName + }; + Q_ENUMS(Roles) + + QHash roleNames() const; + +private: + void runQuery(const Sink::Query &query); + QSharedPointer mModel; + QHash mAccountNames; + QHash mAccountIcons; +}; -- cgit v1.2.3