Fix confusing checkbox text due to class reuse
[kdepim.git] / kmail / attachmentcontroller.cpp
blobb6224dbb09f117453b8f1ac796641e024d5906c3
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( const QModelIndex & ) ),
67 this, SLOT( doubleClicked( const 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()
80 // delete d;
83 void AttachmentController::identityChanged()
85 const KPIMIdentities::Identity &identity = mComposer->identity();
87 // "Attach public key" is only possible if OpenPGP support is available:
88 enableAttachPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() );
90 // "Attach my public key" is only possible if OpenPGP support is
91 // available and the user specified his key for the current identity:
92 enableAttachMyPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() && !identity.pgpEncryptionKey().isEmpty() );
95 void AttachmentController::attachMyPublicKey()
97 const KPIMIdentities::Identity &identity = mComposer->identity();
98 kDebug() << identity.identityName();
99 exportPublicKey( mComposer->identity().pgpEncryptionKey() );
102 void AttachmentController::actionsCreated()
104 // Disable public key actions if appropriate.
105 identityChanged();
107 // Disable actions like 'Remove', since nothing is currently selected.
108 selectionChanged();
111 void AttachmentController::addAttachmentItems( const Akonadi::Item::List &items )
113 Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this );
114 itemFetchJob->fetchScope().fetchFullPayload( true );
115 itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
116 connect( itemFetchJob, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchJob( KJob* ) ) );
119 void AttachmentController::slotFetchJob( KJob *job )
121 if ( job->error() ) {
122 MailCommon::Util::showJobErrorMessage( job );
123 return;
125 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
126 if ( !fjob )
127 return;
128 Akonadi::Item::List items = fjob->items();
130 if ( items.isEmpty() )
131 return;
133 if ( items.first().mimeType() == KMime::Message::mimeType() ) {
134 uint identity = 0;
135 if ( items.at( 0 ).isValid() && items.at( 0 ).parentCollection().isValid() ) {
136 QSharedPointer<FolderCollection> fd( FolderCollection::forCollection( items.at( 0 ).parentCollection() ) );
137 identity = fd->identity();
139 KMCommand *command = new KMForwardAttachedCommand( mComposer, items,identity, mComposer );
140 command->start();
141 } else {
142 foreach ( const Akonadi::Item &item, items ) {
143 QString attachmentName = QLatin1String( "attachment" );
144 if ( item.hasPayload<KABC::Addressee>() ) {
145 const KABC::Addressee contact = item.payload<KABC::Addressee>();
146 attachmentName = contact.realName() + QLatin1String( ".vcf" );
149 mComposer->addAttachment( attachmentName, KMime::Headers::CEbase64, QString(), item.payloadData(), item.mimeType().toLatin1() );
154 void AttachmentController::selectionChanged()
156 const QModelIndexList selectedRows = mView->selectionModel()->selectedRows();
157 AttachmentPart::List selectedParts;
158 foreach( const QModelIndex &index, selectedRows ) {
159 AttachmentPart::Ptr part = mView->model()->data(
160 index, Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
161 selectedParts.append( part );
163 setSelectedParts( selectedParts );
166 void AttachmentController::onShowAttachment( KMime::Content *content, const QByteArray &charset )
168 KMReaderMainWin *win =
169 new KMReaderMainWin( content, false, charset );
170 win->show();
173 void AttachmentController::doubleClicked( const QModelIndex &itemClicked )
175 if ( !itemClicked.isValid() ) {
176 kDebug() << "Received an invalid item clicked index";
177 return;
179 // The itemClicked index will contain the column information. But we want to retrieve
180 // the AttachmentPart, so we must recreate the QModelIndex without the column information
181 const QModelIndex &properItemClickedIndex = mView->model()->index( itemClicked.row(), 0 );
182 AttachmentPart::Ptr part = mView->model()->data(
183 properItemClickedIndex,
184 Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
186 // We can't edit encapsulated messages, but we can view them.
187 if ( part->isMessageOrMessageCollection() ) {
188 viewAttachment( part );
190 else {
191 editAttachment( part );
195 #include "attachmentcontroller.moc"