File indexing completed on 2024-11-24 04:53:01
0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net> 0002 0003 This file is part of the Trojita Qt IMAP e-mail client, 0004 http://trojita.flaska.net/ 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License as 0008 published by the Free Software Foundation; either version 2 of 0009 the License or (at your option) version 3 or any later version 0010 accepted by the membership of KDE e.V. (or its successor approved 0011 by the membership of KDE e.V.), which shall act as a proxy 0012 defined in Section 14 of version 3 of the license. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 #include "LoadablePartWidget.h" 0023 #include "Gui/MessageView.h" // so that the compiler knows that it's a QObject 0024 #include "Imap/Model/ItemRoles.h" 0025 #include "UiUtils/Formatting.h" 0026 0027 #include <QPushButton> 0028 0029 namespace Gui 0030 { 0031 0032 LoadablePartWidget::LoadablePartWidget(QWidget *parent, Imap::Network::MsgPartNetAccessManager *manager, const QModelIndex &part, 0033 PartWidgetFactory *factory, int recursionDepth, 0034 const UiUtils::PartLoadingOptions loadingMode): 0035 QStackedWidget(parent), manager(manager), partIndex(part), m_factory(factory), 0036 m_recursionDepth(recursionDepth), m_loadingMode(loadingMode), realPart(0), loadButton(0), m_loadOnShow(false) 0037 { 0038 Q_ASSERT(partIndex.isValid()); 0039 0040 QString mimeType = partIndex.data(Imap::Mailbox::RolePartMimeType).toString().toLower(); 0041 0042 if ((loadingMode & UiUtils::PART_IS_HIDDEN) || 0043 (loadingMode & UiUtils::PART_IGNORE_CLICKTHROUGH) || 0044 partIndex.data(Imap::Mailbox::RoleIsFetched).toBool()) { 0045 m_loadOnShow = true; 0046 } else { 0047 loadButton = new QPushButton(tr("Load %1 (%2)").arg(partIndex.data(Imap::Mailbox::RolePartMimeType).toString(), 0048 UiUtils::Formatting::prettySize(partIndex.data(Imap::Mailbox::RolePartOctets).toULongLong())), 0049 this); 0050 connect(loadButton, &QAbstractButton::clicked, this, &LoadablePartWidget::loadClicked); 0051 addWidget(loadButton); 0052 } 0053 } 0054 0055 void LoadablePartWidget::loadClicked() 0056 { 0057 if (!partIndex.isValid()) { 0058 if (loadButton) { 0059 loadButton->setEnabled(false); 0060 } 0061 return; 0062 } 0063 if (loadButton) { 0064 loadButton->deleteLater(); 0065 loadButton = 0; 0066 } 0067 0068 // We have to disable any flags which might cause recursion here 0069 realPart = m_factory->walk(partIndex, m_recursionDepth + 1, 0070 (m_loadingMode | UiUtils::PART_IGNORE_CLICKTHROUGH | 0071 UiUtils::PART_IGNORE_LOAD_ON_SHOW) 0072 ^ UiUtils::PART_IS_HIDDEN); 0073 addWidget(realPart); 0074 setCurrentIndex(1); 0075 } 0076 0077 QString LoadablePartWidget::quoteMe() const 0078 { 0079 AbstractPartWidget *part = dynamic_cast<AbstractPartWidget*>(realPart); 0080 return part ? part->quoteMe() : QString(); 0081 } 0082 0083 bool LoadablePartWidget::searchDialogRequested() 0084 { 0085 if (AbstractPartWidget *part = dynamic_cast<AbstractPartWidget*>(realPart)) 0086 return part->searchDialogRequested(); 0087 return false; 0088 } 0089 0090 void LoadablePartWidget::showEvent(QShowEvent *event) 0091 { 0092 QStackedWidget::showEvent(event); 0093 if (m_loadOnShow) { 0094 m_loadOnShow = false; 0095 loadClicked(); 0096 } 0097 } 0098 0099 #define IMPL_PART_FORWARD_ONE_METHOD(METHOD) \ 0100 void LoadablePartWidget::METHOD() \ 0101 {\ 0102 if (AbstractPartWidget *w = dynamic_cast<AbstractPartWidget*>(realPart)) \ 0103 w->METHOD(); \ 0104 } 0105 0106 IMPL_PART_FORWARD_ONE_METHOD(reloadContents) 0107 IMPL_PART_FORWARD_ONE_METHOD(zoomIn) 0108 IMPL_PART_FORWARD_ONE_METHOD(zoomOut) 0109 IMPL_PART_FORWARD_ONE_METHOD(zoomOriginal) 0110 0111 }