fix Messages.sh after yesterday change in extract-messages.sh
[kdepim.git] / kmail / attachmentcontroller.cpp
blobfcd97b7b798ab1be08477c87b55af4551ce88e8a
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 "messagecomposer/attachmentmodel.h"
26 #include "attachmentview.h"
27 #include "attachmentfrompublickeyjob.h"
28 #include "messageviewer/editorwatcher.h"
29 #include "globalsettings.h"
30 #include "kmkernel.h"
31 #include "kmcomposewin.h"
32 #include "kmcommands.h"
33 #include "foldercollection.h"
35 #include <akonadi/itemfetchjob.h>
36 #include <kio/jobuidelegate.h>
39 #include <KAction>
40 #include <KActionCollection>
41 #include <KDebug>
42 #include <KPushButton>
44 #include "kmreadermainwin.h"
45 #include <kpimutils/kfileio.h>
47 #include <libkleo/kleo/cryptobackendfactory.h>
48 #include <libkleo/ui/keyselectiondialog.h>
50 #include <messagecore/attachmentcompressjob.h>
51 #include <messagecore/attachmentfrommimecontentjob.h>
52 #include <messagecore/attachmentfromurljob.h>
53 #include <messagecore/attachmentpropertiesdialog.h>
55 using namespace KMail;
56 using namespace KPIM;
58 AttachmentController::AttachmentController( Message::AttachmentModel *model, AttachmentView *view, KMComposeWin *composer )
59 : AttachmentControllerBase( model, composer, composer->actionCollection() )
61 mComposer = composer;
63 connect( composer, SIGNAL(identityChanged(KPIMIdentities::Identity)),
64 this, SLOT(identityChanged()) );
66 mView = view;
67 connect( view, SIGNAL(contextMenuRequested()), this, SLOT(showContextMenu()) );
68 connect( view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
69 this, SLOT(selectionChanged()) );
70 connect( view, SIGNAL(doubleClicked(QModelIndex)),
71 this, SLOT(editSelectedAttachment()) );
73 connect( this, SIGNAL(refreshSelection()), SLOT(selectionChanged()));
75 connect( this, SIGNAL(showAttachment(KMime::Content*,QByteArray)),
76 SLOT(onShowAttachment(KMime::Content*,QByteArray)));
78 connect( model, SIGNAL(attachItemsRequester(Akonadi::Item::List ) ), this, SLOT( addAttachmentItems( Akonadi::Item::List ) ) );
82 AttachmentController::~AttachmentController()
84 // delete d;
87 void AttachmentController::identityChanged()
89 const KPIMIdentities::Identity &identity = mComposer->identity();
91 // "Attach public key" is only possible if OpenPGP support is available:
92 enableAttachPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() );
94 // "Attach my public key" is only possible if OpenPGP support is
95 // available and the user specified his key for the current identity:
96 enableAttachMyPublicKey( Kleo::CryptoBackendFactory::instance()->openpgp() && !identity.pgpEncryptionKey().isEmpty() );
99 void AttachmentController::attachMyPublicKey()
101 const KPIMIdentities::Identity &identity = mComposer->identity();
102 kDebug() << identity.identityName();
103 exportPublicKey( mComposer->identity().pgpEncryptionKey() );
106 void AttachmentController::actionsCreated()
108 // Disable public key actions if appropriate.
109 identityChanged();
111 // Disable actions like 'Remove', since nothing is currently selected.
112 selectionChanged();
115 void AttachmentController::addAttachmentItems( const Akonadi::Item::List &items )
117 Akonadi::ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( items, this );
118 itemFetchJob->fetchScope().fetchFullPayload( true );
119 itemFetchJob->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
120 connect( itemFetchJob, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchJob( KJob* ) ) );
123 void AttachmentController::slotFetchJob( KJob *job )
125 if ( job->error() ) {
126 if ( static_cast<KIO::Job*>( job )->ui() ) {
127 static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
129 return;
131 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
132 if ( !fjob )
133 return;
134 Akonadi::Item::List items = fjob->items();
136 uint identity = 0;
137 if ( items.at( 0 ).isValid() && items.at( 0 ).parentCollection().isValid() ) {
138 QSharedPointer<FolderCollection> fd( FolderCollection::forCollection( items.at( 0 ).parentCollection() ) );
139 identity = fd->identity();
141 KMCommand *command = new KMForwardAttachedCommand( mComposer, items,identity, mComposer );
142 command->start();
145 void AttachmentController::selectionChanged()
147 const QModelIndexList selectedRows = mView->selectionModel()->selectedRows();
148 AttachmentPart::List selectedParts;
149 foreach( const QModelIndex &index, selectedRows ) {
150 AttachmentPart::Ptr part = mView->model()->data(
151 index, Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
152 selectedParts.append( part );
154 setSelectedParts( selectedParts );
157 void AttachmentController::onShowAttachment( KMime::Content *content, const QByteArray &charset )
159 KMReaderMainWin *win =
160 new KMReaderMainWin( content, false, charset );
161 win->show();
164 #include "attachmentcontroller.moc"