File indexing completed on 2024-11-24 04:53:08

0001 /* Copyright (C) 2013  Ahmed Ibrahim Khalil <ahmedibrahimkhali@gmail.com>
0002    Copyright (C) 2006 - 2016 Jan Kundrát <jkt@kde.org>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 #include "FullMessageCombiner.h"
0025 #include "Imap/Model/ItemRoles.h"
0026 #include "Imap/Model/MailboxTree.h"
0027 
0028 namespace Imap
0029 {
0030 namespace Mailbox
0031 {
0032 
0033 
0034 FullMessageCombiner::FullMessageCombiner(const QModelIndex &messageIndex, QObject *parent) :
0035     QObject(parent), m_messageIndex(messageIndex)
0036 {
0037     Q_ASSERT(m_messageIndex.isValid());
0038     m_headerPartIndex = m_messageIndex.model()->index(0, Imap::Mailbox::TreeItem::OFFSET_HEADER, m_messageIndex);
0039     Q_ASSERT(m_headerPartIndex.isValid());
0040     m_bodyPartIndex = m_messageIndex.model()->index(0, Imap::Mailbox::TreeItem::OFFSET_TEXT, m_messageIndex);
0041     Q_ASSERT(m_bodyPartIndex.isValid());
0042     m_dataChanged = connect(m_messageIndex.model(), &QAbstractItemModel::dataChanged, this, &FullMessageCombiner::slotDataChanged);
0043 }
0044 
0045 QByteArray FullMessageCombiner::data() const
0046 {
0047     if (loaded())
0048         return m_headerPartIndex.data(Imap::Mailbox::RolePartData).toByteArray() +
0049                 m_bodyPartIndex.data(Imap::Mailbox::RolePartData).toByteArray();
0050 
0051     return QByteArray();
0052 }
0053 
0054 bool FullMessageCombiner::loaded() const
0055 {
0056     if (!indexesValid())
0057         return false;
0058 
0059     return m_headerPartIndex.data(Imap::Mailbox::RoleIsFetched).toBool() && m_bodyPartIndex.data(Imap::Mailbox::RoleIsFetched).toBool();
0060 }
0061 
0062 void FullMessageCombiner::load()
0063 {
0064     if (!indexesValid())
0065         return;
0066 
0067     m_headerPartIndex.data(Imap::Mailbox::RolePartData);
0068     m_bodyPartIndex.data(Imap::Mailbox::RolePartData);
0069 
0070     slotDataChanged(QModelIndex(), QModelIndex());
0071 }
0072 
0073 void FullMessageCombiner::slotDataChanged(const QModelIndex &left, const QModelIndex &right)
0074 {
0075     Q_UNUSED(left);
0076     Q_UNUSED(right);
0077 
0078     if (!indexesValid()) {
0079         emit failed(tr("Message is gone"));
0080         return;
0081     }
0082 
0083     if (m_headerPartIndex.data(Imap::Mailbox::RoleIsFetched).toBool() && m_bodyPartIndex.data(Imap::Mailbox::RoleIsFetched).toBool()) {
0084         emit completed();
0085         disconnect(m_dataChanged);
0086     }
0087 
0088     bool headerOffline = m_headerPartIndex.data(Imap::Mailbox::RoleIsUnavailable).toBool();
0089     bool bodyOffline = m_bodyPartIndex.data(Imap::Mailbox::RoleIsUnavailable).toBool();
0090     if (headerOffline && bodyOffline) {
0091         emit failed(tr("Offline mode: uncached message data not available"));
0092     } else if (headerOffline) {
0093         emit failed(tr("Offline mode: uncached header data not available"));
0094     } else if (bodyOffline) {
0095         emit failed(tr("Offline mode: uncached body data not available"));
0096     }
0097 }
0098 
0099 bool FullMessageCombiner::indexesValid() const
0100 {
0101     return m_messageIndex.isValid() && m_headerPartIndex.isValid() && m_bodyPartIndex.isValid();
0102 }
0103 
0104 }
0105 }