File indexing completed on 2025-01-19 04:52:00

0001 /*
0002     Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net>
0003     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0004 
0005     This library is free software; you can redistribute it and/or modify it
0006     under the terms of the GNU Library General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or (at your
0008     option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful, but WITHOUT
0011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0013     License for more details.
0014 
0015     You should have received a copy of the GNU Library General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to the
0017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018     02110-1301, USA.
0019 */
0020 
0021 #include "outboxmodel.h"
0022 
0023 #include <QFile>
0024 #include <QDateTime>
0025 #include <QString>
0026 
0027 #include <sink/standardqueries.h>
0028 #include <sink/notifier.h>
0029 #include <sink/notification.h>
0030 
0031 
0032 OutboxModel::OutboxModel(QObject *parent)
0033     : QSortFilterProxyModel(parent),
0034     mNotifier(new Sink::Notifier{Sink::Query{}.containsFilter<Sink::ApplicationDomain::SinkResource::Capabilities>(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)}),
0035     mStatus(NoStatus)
0036 {
0037     setDynamicSortFilter(true);
0038     sort(0, Qt::DescendingOrder);
0039 
0040     using namespace Sink::ApplicationDomain;
0041     auto query = Sink::StandardQueries::outboxMails();
0042     query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus);
0043     query.request<Mail::Subject>();
0044     query.request<Mail::Date>();
0045     query.request<Mail::Folder>();
0046     runQuery(query);
0047     connect(this, &QAbstractItemModel::rowsInserted, this, &OutboxModel::countChanged);
0048     connect(this, &QAbstractItemModel::rowsRemoved, this, &OutboxModel::countChanged);
0049 
0050     mNotifier->registerHandler([this] (const Sink::Notification &n) {
0051         //TODO aggregate status from multiple resources
0052         if (n.type == Sink::Notification::Status) {
0053             switch (n.code) { 
0054                 case Sink::ApplicationDomain::Status::ErrorStatus:
0055                     mStatus = ErrorStatus;
0056                     break;
0057                 case Sink::ApplicationDomain::Status::BusyStatus:
0058                     mStatus = InProgressStatus;
0059                     break;
0060                 default:
0061                     mStatus = NoStatus;
0062                     break;
0063             }
0064             emit statusChanged();
0065         }
0066 
0067     });
0068 
0069 }
0070 
0071 OutboxModel::~OutboxModel()
0072 {
0073 
0074 }
0075 
0076 QHash< int, QByteArray > OutboxModel::roleNames() const
0077 {
0078     QHash<int, QByteArray> roles;
0079 
0080     roles[Subject] = "subject";
0081     roles[Date] = "date";
0082     roles[Status] = "status";
0083     roles[Id] = "id";
0084     roles[MimeMessage] = "mimeMessage";
0085     roles[DomainObject] = "domainObject";
0086 
0087     return roles;
0088 }
0089 
0090 QVariant OutboxModel::data(const QModelIndex &idx, int role) const
0091 {
0092     auto srcIdx = mapToSource(idx);
0093     auto mail = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>();
0094     switch (role) {
0095         case Subject:
0096             return mail->getSubject();
0097         case Date:
0098             return mail->getDate();
0099         case Status: {
0100             const auto status = srcIdx.data(Sink::Store::StatusRole).toInt();
0101             if (status == Sink::ApplicationDomain::SyncStatus::SyncInProgress) {
0102                 return InProgressStatus;
0103             }
0104             if (status == Sink::ApplicationDomain::SyncStatus::SyncError) {
0105                 return ErrorStatus;
0106             }
0107             return PendingStatus;
0108         }
0109         case Id:
0110             return mail->identifier();
0111         case DomainObject:
0112             return QVariant::fromValue(mail);
0113         case MimeMessage: {
0114             return mail->getMimeMessage();
0115         }
0116     }
0117     return QSortFilterProxyModel::data(idx, role);
0118 }
0119 
0120 bool OutboxModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0121 {
0122     const auto leftDate = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate();
0123     const auto rightDate = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate();
0124     return leftDate < rightDate;
0125 }
0126 
0127 void OutboxModel::runQuery(const Sink::Query &query)
0128 {
0129     mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query);
0130     setSourceModel(mModel.data());
0131 }
0132 
0133 int OutboxModel::count() const
0134 {
0135     return rowCount();
0136 }
0137 
0138 int OutboxModel::status() const
0139 {
0140     return mStatus;
0141 }