File indexing completed on 2024-05-26 05:28:14

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 "MessageSourceWidget.h"
0025 #include <QAction>
0026 #include <QModelIndex>
0027 #include <QVBoxLayout>
0028 #include <QWebView>
0029 #include "Gui/FindBar.h"
0030 #include "Gui/Spinner.h"
0031 #include "Imap/Model/FullMessageCombiner.h"
0032 #include "UiUtils/IconLoader.h"
0033 
0034 namespace Gui
0035 {
0036 
0037 MessageSourceWidget::MessageSourceWidget(QWidget *parent, const QModelIndex &messageIndex)
0038     : QWidget(parent)
0039     , FindBarMixin(this)
0040     , m_combiner(nullptr)
0041     , m_loadingSpinner(nullptr)
0042     , m_widget(new QWebView(this))
0043 {
0044     setWindowIcon(UiUtils::loadIcon(QStringLiteral("text-x-hex")));
0045     Q_ASSERT(messageIndex.isValid());
0046     m_widget->page()->setNetworkAccessManager(0);
0047 
0048     m_loadingSpinner = new Spinner(this);
0049     m_loadingSpinner->setText(tr("Fetching\nMessage"));
0050     m_loadingSpinner->setType(Spinner::Sun);
0051     m_loadingSpinner->start(250);
0052 
0053     m_combiner = new Imap::Mailbox::FullMessageCombiner(messageIndex, this);
0054     connect(m_combiner, &Imap::Mailbox::FullMessageCombiner::completed, this, &MessageSourceWidget::slotCompleted);
0055     connect(m_combiner, &Imap::Mailbox::FullMessageCombiner::failed, this, &MessageSourceWidget::slotError);
0056     m_combiner->load();
0057 
0058     auto find = new QAction(UiUtils::loadIcon(QStringLiteral("edit-find")), tr("Search..."), this);
0059     find->setShortcut(tr("Ctrl+F"));
0060     connect(find, &QAction::triggered, this, [this]() {
0061         searchRequestedBy(m_widget);
0062     });
0063     addAction(find);
0064 
0065     auto layout = new QVBoxLayout(this);
0066     layout->setContentsMargins(0, 0, 0, 0);
0067     layout->addWidget(m_widget, 1);
0068     layout->addWidget(m_findBar);
0069     setLayout(layout);
0070 }
0071 
0072 void MessageSourceWidget::slotCompleted()
0073 {
0074     m_loadingSpinner->stop();
0075     m_widget->setContent(m_combiner->data(), QStringLiteral("text/plain"));
0076 }
0077 
0078 void MessageSourceWidget::slotError(const QString &message)
0079 {
0080     m_loadingSpinner->stop();
0081     m_widget->setContent(message.toUtf8(), QStringLiteral("text/plain; charset=utf-8"));
0082 }
0083 
0084 }