File indexing completed on 2024-10-27 04:50:58
0001 /* 0002 * This file is part of KMail. 0003 * SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com> 0004 * 0005 * Parts based on KMail code by: 0006 * Various authors. 0007 * 0008 * SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "attachmentcontroller.h" 0012 0013 #include "attachmentview.h" 0014 #include "editor/kmcomposerwin.h" 0015 #include "kmail_debug.h" 0016 #include "kmreadermainwin.h" 0017 #include <Akonadi/ItemFetchJob> 0018 #include <Akonadi/ItemFetchScope> 0019 #include <KContacts/Addressee> 0020 #include <KIdentityManagementCore/Identity> 0021 #include <MailCommon/FolderSettings> 0022 #include <MailCommon/MailUtil> 0023 0024 #include <QGpgME/Protocol> 0025 0026 #include <MessageComposer/AttachmentModel> 0027 #include <MessageCore/AttachmentPart> 0028 0029 using namespace KMail; 0030 using namespace KPIM; 0031 using namespace MailCommon; 0032 using namespace MessageCore; 0033 0034 AttachmentController::AttachmentController(MessageComposer::AttachmentModel *model, AttachmentView *view, KMComposerWin *composer) 0035 : AttachmentControllerBase(model, composer, composer->actionCollection()) 0036 , mComposer(composer) 0037 , mView(view) 0038 { 0039 connect(composer, &KMComposerWin::identityChanged, this, &AttachmentController::identityChanged); 0040 0041 connect(view, &AttachmentView::contextMenuRequested, this, &AttachmentControllerBase::showContextMenu); 0042 connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &AttachmentController::selectionChanged); 0043 connect(view, &QAbstractItemView::doubleClicked, this, &AttachmentController::doubleClicked); 0044 0045 connect(this, &AttachmentController::refreshSelection, this, &AttachmentController::selectionChanged); 0046 0047 connect(this, &AttachmentController::showAttachment, this, &AttachmentController::onShowAttachment); 0048 connect(this, &AttachmentController::selectedAllAttachment, this, &AttachmentController::slotSelectAllAttachment); 0049 connect(model, &MessageComposer::AttachmentModel::attachItemsRequester, this, &AttachmentController::addAttachmentItems); 0050 connect(this, &AttachmentController::actionsCreated, this, &AttachmentController::slotActionsCreated); 0051 } 0052 0053 AttachmentController::~AttachmentController() = default; 0054 0055 void AttachmentController::slotSelectAllAttachment() 0056 { 0057 mView->selectAll(); 0058 } 0059 0060 void AttachmentController::identityChanged() 0061 { 0062 const KIdentityManagementCore::Identity &identity = mComposer->identity(); 0063 0064 // "Attach public key" is only possible if OpenPGP support is available: 0065 enableAttachPublicKey(QGpgME::openpgp()); 0066 0067 // "Attach my public key" is only possible if OpenPGP support is 0068 // available and the user specified his key for the current identity: 0069 enableAttachMyPublicKey(QGpgME::openpgp() && !identity.pgpEncryptionKey().isEmpty()); 0070 } 0071 0072 void AttachmentController::attachMyPublicKey() 0073 { 0074 const KIdentityManagementCore::Identity &identity = mComposer->identity(); 0075 qCDebug(KMAIL_LOG) << identity.identityName(); 0076 exportPublicKey(QString::fromLatin1(identity.pgpEncryptionKey())); 0077 } 0078 0079 void AttachmentController::slotActionsCreated() 0080 { 0081 // Disable public key actions if appropriate. 0082 identityChanged(); 0083 0084 // Disable actions like 'Remove', since nothing is currently selected. 0085 selectionChanged(); 0086 } 0087 0088 void AttachmentController::addAttachmentItems(const Akonadi::Item::List &items) 0089 { 0090 auto itemFetchJob = new Akonadi::ItemFetchJob(items, this); 0091 itemFetchJob->fetchScope().fetchFullPayload(true); 0092 itemFetchJob->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent); 0093 connect(itemFetchJob, &Akonadi::ItemFetchJob::result, mComposer, &KMComposerWin::slotFetchJob); 0094 } 0095 0096 void AttachmentController::selectionChanged() 0097 { 0098 const QModelIndexList selectedRows = mView->selectionModel()->selectedRows(); 0099 AttachmentPart::List selectedParts; 0100 selectedParts.reserve(selectedRows.count()); 0101 for (const QModelIndex &index : selectedRows) { 0102 auto part = mView->model()->data(index, MessageComposer::AttachmentModel::AttachmentPartRole).value<AttachmentPart::Ptr>(); 0103 selectedParts.append(part); 0104 } 0105 setSelectedParts(selectedParts); 0106 } 0107 0108 void AttachmentController::onShowAttachment(KMime::Content *content, const QByteArray &charset) 0109 { 0110 const QString charsetStr = QString::fromLatin1(charset); 0111 if (content->bodyAsMessage()) { 0112 KMime::Message::Ptr m(new KMime::Message); 0113 m->setContent(content->bodyAsMessage()->encodedContent()); 0114 m->parse(); 0115 auto win = new KMReaderMainWin(); 0116 win->showMessage(charsetStr, m); 0117 win->show(); 0118 } else { 0119 auto win = new KMReaderMainWin(content, MessageViewer::Viewer::Text, charsetStr); 0120 win->show(); 0121 } 0122 } 0123 0124 void AttachmentController::doubleClicked(const QModelIndex &itemClicked) 0125 { 0126 if (!itemClicked.isValid()) { 0127 qCDebug(KMAIL_LOG) << "Received an invalid item clicked index"; 0128 return; 0129 } 0130 // The itemClicked index will contain the column information. But we want to retrieve 0131 // the AttachmentPart, so we must recreate the QModelIndex without the column information 0132 const QModelIndex &properItemClickedIndex = mView->model()->index(itemClicked.row(), 0); 0133 auto part = mView->model()->data(properItemClickedIndex, MessageComposer::AttachmentModel::AttachmentPartRole).value<AttachmentPart::Ptr>(); 0134 0135 // We can't edit encapsulated messages, but we can view them. 0136 if (part->isMessageOrMessageCollection()) { 0137 viewAttachment(part); 0138 } else { 0139 editAttachment(part); 0140 } 0141 } 0142 0143 #include "moc_attachmentcontroller.cpp"