Make the boss happy.
[kdepim.git] / kmail / attachmentcontroller.cpp
bloba88a889ecda7742d89c084884da91fba02c7b333
1 /*
2 * This file is part of KMail.
3 * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
5 * Parts based on KMail code by:
6 * Various authors.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "attachmentcontroller.h"
25 #include "attachmentview.h"
26 #include "attachmentfrompublickeyjob.h"
27 #include "foldercollection.h"
28 #include "globalsettings.h"
29 #include "kmcommands.h"
30 #include "kmcomposewin.h"
31 #include "kmkernel.h"
32 #include "kmreadermainwin.h"
33 #include "mailutil.h"
35 #include <akonadi/itemfetchjob.h>
36 #include <kabc/addressee.h>
37 #include <kdebug.h>
38 #include <libkleo/kleo/cryptobackendfactory.h>
39 #include <libkleo/ui/keyselectiondialog.h>
41 #include <messagecomposer/attachmentmodel.h>
42 #include <messagecore/attachmentcompressjob.h>
43 #include <messagecore/attachmentfrommimecontentjob.h>
44 #include <messagecore/attachmentfromurljob.h>
45 #include <messagecore/attachmentpropertiesdialog.h>
46 #include <messagecore/attachmentpart.h>
47 #include <messageviewer/editorwatcher.h>
49 using namespace KMail;
50 using namespace KPIM;
51 using namespace MailCommon;
52 using namespace MessageCore;
54 AttachmentController::AttachmentController( Message::AttachmentModel *model, AttachmentView *view, KMComposeWin *composer )
55 : AttachmentControllerBase( model, composer, composer->actionCollection() )
57 mComposer = composer;
59 connect( composer, SIGNAL(identityChanged(KPIMIdentities::Identity)),
60 this, SLOT(identityChanged()) );
62 mView = view;
63 connect( view, SIGNAL(contextMenuRequested()), this, SLOT(showContextMenu()) );
64 connect( view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
65 this, SLOT(selectionChanged()) );
66 connect( view, SIGNAL(doubleClicked(QModelIndex)),
67 this, SLOT(doubleClicked(QModelIndex)) );
69 connect( this, SIGNAL(refreshSelection()), SLOT(selectionChanged()));
71 connect( this, SIGNAL(showAttachment(KMime::Content*,QByteArray)),
72 SLOT(onShowAttachment(KMime::Content*,QByteArray)));
74 connect( model, SIGNAL(attachItemsRequester(Akonadi::Item::List)), this, SLOT(addAttachmentItems(Akonadi::Item::List)) );
78 AttachmentController::~AttachmentController()
82 void AttachmentController::identityChanged()
84 const KPIMIdentities::Identity &identity = mComposer->identity();
86 // "Attach public key" is only possible if OpenPGP support is available:
87 enableAttachPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() );
89 // "Attach my public key" is only possible if OpenPGP support is
90 // available and the user specified his key for the current identity:
91 enableAttachMyPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() && !identity.pgpEncryptionKey().isEmpty() );
94 void AttachmentController::attachMyPublicKey()
96 const KPIMIdentities::Identity &identity = mComposer->identity();
97 kDebug() << identity.identityName();
98 exportPublicKey( mComposer->identity().pgpEncryptionKey() );
101 void AttachmentController::actionsCreated()
103 // Disable public key actions if appropriate.
104 identityChanged();
106 // Disable actions like 'Remove', since nothing is currently selected.
107 selectionChanged();
110 void AttachmentController::addAttachmentItems( const Akonadi::Item::List &items )
112 Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this );
113 itemFetchJob->fetchScope().fetchFullPayload( true );
114 itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
115 connect( itemFetchJob, SIGNAL(result(KJob*)), this, SLOT(slotFetchJob(KJob*)) );
118 void AttachmentController::slotFetchJob( KJob *job )
120 if ( job->error() ) {
121 MailCommon::Util::showJobErrorMessage( job );
122 return;
124 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
125 if ( !fjob )
126 return;
127 Akonadi::Item::List items = fjob->items();
129 if ( items.isEmpty() )
130 return;
132 if ( items.first().mimeType() == KMime::Message::mimeType() ) {
133 uint identity = 0;
134 if ( items.at( 0 ).isValid() && items.at( 0 ).parentCollection().isValid() ) {
135 QSharedPointer<FolderCollection> fd( FolderCollection::forCollection( items.at( 0 ).parentCollection(), false ) );
136 identity = fd->identity();
138 KMCommand *command = new KMForwardAttachedCommand( mComposer, items,identity, mComposer );
139 command->start();
140 } else {
141 foreach ( const Akonadi::Item &item, items ) {
142 QString attachmentName = QLatin1String( "attachment" );
143 if ( item.hasPayload<KABC::Addressee>() ) {
144 const KABC::Addressee contact = item.payload<KABC::Addressee>();
145 attachmentName = contact.realName() + QLatin1String( ".vcf" );
148 mComposer->addAttachment( attachmentName, KMime::Headers::CEbase64, QString(), item.payloadData(), item.mimeType().toLatin1() );
153 void AttachmentController::selectionChanged()
155 const QModelIndexList selectedRows = mView->selectionModel()->selectedRows();
156 AttachmentPart::List selectedParts;
157 foreach( const QModelIndex &index, selectedRows ) {
158 AttachmentPart::Ptr part = mView->model()->data(
159 index, Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
160 selectedParts.append( part );
162 setSelectedParts( selectedParts );
165 void AttachmentController::onShowAttachment( KMime::Content *content, const QByteArray &charset )
167 KMReaderMainWin *win =
168 new KMReaderMainWin( content, false, charset );
169 win->show();
172 void AttachmentController::doubleClicked( const QModelIndex &itemClicked )
174 if ( !itemClicked.isValid() ) {
175 kDebug() << "Received an invalid item clicked index";
176 return;
178 // The itemClicked index will contain the column information. But we want to retrieve
179 // the AttachmentPart, so we must recreate the QModelIndex without the column information
180 const QModelIndex &properItemClickedIndex = mView->model()->index( itemClicked.row(), 0 );
181 AttachmentPart::Ptr part = mView->model()->data(
182 properItemClickedIndex,
183 Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
185 // We can't edit encapsulated messages, but we can view them.
186 if ( part->isMessageOrMessageCollection() ) {
187 viewAttachment( part );
189 else {
190 editAttachment( part );
194 #include "attachmentcontroller.moc"