Commit 2aefd5a7 authored by Laurent Montel's avatar Laurent Montel 😁

Extract code to edit attachment. Fix bug when we select another message

parent 2bd18d46
......@@ -159,6 +159,7 @@ set(libmessageviewer_job_SRCS
job/createtodojob.cpp
job/createeventjob.cpp
job/attachmentencryptwithchiasmusjob.cpp
job/attachmenteditjob.cpp
)
set(libmessageviewer_SRCS
......
/*
Copyright (c) 2014 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "attachmenteditjob.h"
#include "viewer/editorwatcher.h"
#include <KMessageBox>
#include <KLocalizedString>
#include <KTemporaryFile>
#include <KUrl>
#include <KDebug>
#include <Akonadi/ItemModifyJob>
#include <KMime/Content>
using namespace MessageViewer;
AttachmentEditJob::AttachmentEditJob(QObject *parent)
: QObject(parent),
mShowWarning(true),
mMainWindow(0)
{
}
AttachmentEditJob::~AttachmentEditJob()
{
qDebug()<<" AttachmentEditJob::~AttachmentEditJob()";
}
bool AttachmentEditJob::addAttachment(KMime::Content * node, bool showWarning)
{
if ( showWarning && KMessageBox::warningContinueCancel( mMainWindow,
i18n("Modifying an attachment might invalidate any digital signature on this message."),
i18n("Edit Attachment"), KGuiItem( i18n("Edit"), QLatin1String("document-properties") ), KStandardGuiItem::cancel(),
QLatin1String("EditAttachmentSignatureWarning") )
!= KMessageBox::Continue ) {
return false;
}
KTemporaryFile file;
file.setAutoRemove( false );
if ( !file.open() ) {
kWarning() << "Edit Attachment: Unable to open temp file.";
return true;
}
file.write( node->decodedContent() );
file.flush();
EditorWatcher *watcher =
new EditorWatcher( KUrl( file.fileName() ), QLatin1String(node->contentType()->mimeType()),
MessageViewer::EditorWatcher::NoOpenWithDialog, this, mMainWindow );
mEditorWatchers[ watcher ] = node;
connect( watcher, SIGNAL(editDone(MessageViewer::EditorWatcher*)), SLOT(slotAttachmentEditDone(MessageViewer::EditorWatcher*)) );
if ( !watcher->start() ) {
removeEditorWatcher(watcher, file.fileName());
}
return true;
}
void AttachmentEditJob::setMainWindow(QWidget *mainWindow)
{
mMainWindow = mainWindow;
}
void AttachmentEditJob::slotAttachmentEditDone( MessageViewer::EditorWatcher* editorWatcher )
{
const QString name = editorWatcher->url().fileName();
if ( editorWatcher->fileChanged() ) {
QFile file( name );
if ( file.open( QIODevice::ReadOnly ) ) {
QByteArray data = file.readAll();
KMime::Content *node = mEditorWatchers[editorWatcher];
node->setBody( data );
file.close();
mMessageItem.setPayloadFromData( mMessage->encodedContent() );
Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( mMessageItem );
connect( job, SIGNAL(result(KJob*)), SLOT(slotItemModifiedResult(KJob*)) );
removeEditorWatcher(editorWatcher, name);
}
} else {
removeEditorWatcher(editorWatcher, name);
canDeleteJob();
}
}
void AttachmentEditJob::setMessageItem(const Akonadi::Item &messageItem)
{
mMessageItem = messageItem;
}
void AttachmentEditJob::slotItemModifiedResult(KJob *job)
{
if ( job->error() ) {
kDebug() << "Item update failed:" << job->errorString();
} else {
Q_EMIT refreshMessage(mMessageItem);
}
canDeleteJob();
}
void AttachmentEditJob::canDeleteJob()
{
if (mEditorWatchers.isEmpty())
deleteLater();
}
void AttachmentEditJob::removeEditorWatcher(MessageViewer::EditorWatcher* editorWatcher, const QString &name)
{
mEditorWatchers.remove( editorWatcher );
QFile::remove( name );
}
void AttachmentEditJob::setMessage(const KMime::Message::Ptr &message)
{
mMessage = message;
}
/*
Copyright (c) 2014 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ATTACHMENTEDITJOB_H
#define ATTACHMENTEDITJOB_H
#include <QObject>
#include <Akonadi/Item>
#include <QHash>
#include <kmime/kmime_message.h>
namespace KMime {
class Content;
}
namespace MessageViewer {
class EditorWatcher;
class AttachmentEditJob : public QObject
{
Q_OBJECT
public:
explicit AttachmentEditJob(QObject *parent=0);
~AttachmentEditJob();
void setMainWindow(QWidget *mainWindow);
void setMessageItem(const Akonadi::Item &messageItem);
bool addAttachment(KMime::Content *node, bool showWarning);
void canDeleteJob();
void setMessage(const KMime::Message::Ptr &message);
Q_SIGNALS:
void refreshMessage(const Akonadi::Item &item);
private slots:
void slotAttachmentEditDone(MessageViewer::EditorWatcher *editorWatcher);
void slotItemModifiedResult(KJob *);
private:
void removeEditorWatcher(MessageViewer::EditorWatcher *editorWatcher, const QString &name);
QHash<EditorWatcher*, KMime::Content*> mEditorWatchers;
Akonadi::Item mMessageItem;
KMime::Message::Ptr mMessage;
bool mShowWarning;
QWidget *mMainWindow;
};
}
#endif // ATTACHMENTEDITJOB_H
......@@ -38,7 +38,7 @@
#include "widgets/openattachmentfolderwidget.h"
#include "pimcommon/widgets/slidecontainer.h"
#include "messageviewer/job/attachmentencryptwithchiasmusjob.h"
#include "messageviewer/job/attachmenteditjob.h"
#ifdef MESSAGEVIEWER_READER_HTML_DEBUG
#include "htmlwriter/filehtmlwriter.h"
#include "htmlwriter/teehtmlwriter.h"
......@@ -122,7 +122,6 @@
#include "widgets/attachmentdialog.h"
#include "viewer/attachmentstrategy.h"
#include "csshelper.h"
#include "viewer/editorwatcher.h"
#include "settings/globalsettings.h"
#include "header/headerstyle.h"
#include "header/headerstrategy.h"
......@@ -478,37 +477,15 @@ void ViewerPrivate::itemModifiedResult( KJob* job )
}
bool ViewerPrivate::editAttachment( KMime::Content * node, bool showWarning )
void ViewerPrivate::editAttachment( KMime::Content * node, bool showWarning )
{
//FIXME(Andras) just that I don't forget...handle the case when the user starts editing and switches to another message meantime
if ( showWarning && KMessageBox::warningContinueCancel( mMainWindow,
i18n("Modifying an attachment might invalidate any digital signature on this message."),
i18n("Edit Attachment"), KGuiItem( i18n("Edit"), QLatin1String("document-properties") ), KStandardGuiItem::cancel(),
QLatin1String("EditAttachmentSignatureWarning") )
!= KMessageBox::Continue ) {
return false;
}
KTemporaryFile file;
file.setAutoRemove( false );
if ( !file.open() ) {
kWarning() << "Edit Attachment: Unable to open temp file.";
return true;
}
file.write( node->decodedContent() );
file.flush();
EditorWatcher *watcher =
new EditorWatcher( KUrl( file.fileName() ), QLatin1String(node->contentType()->mimeType()),
MessageViewer::EditorWatcher::NoOpenWithDialog, this, mMainWindow );
mEditorWatchers[ watcher ] = node;
connect( watcher, SIGNAL(editDone(MessageViewer::EditorWatcher*)), SLOT(slotAttachmentEditDone(MessageViewer::EditorWatcher*)) );
if ( !watcher->start() ) {
mEditorWatchers.remove( watcher );
QFile::remove( file.fileName() );
}
return true;
MessageViewer::AttachmentEditJob *job = new MessageViewer::AttachmentEditJob(this);
connect(job, SIGNAL(refreshMessage(Akonadi::Item)), this, SLOT(slotRefreshMessage(Akonadi::Item)));
job->setMainWindow(mMainWindow);
job->setMessageItem(mMessageItem);
job->setMessage(mMessage);
job->addAttachment(node, showWarning);
job->canDeleteJob();
}
void ViewerPrivate::createOpenWithMenu( KMenu *topMenu, const QString &contentTypeStr, bool fromCurrentContent )
......@@ -2660,35 +2637,22 @@ void ViewerPrivate::slotAttachmentEdit()
if ( contents.isEmpty() )
return;
MessageViewer::AttachmentEditJob *job = new MessageViewer::AttachmentEditJob(this);
connect(job, SIGNAL(refreshMessage(Akonadi::Item)), this, SLOT(slotRefreshMessage(Akonadi::Item)));
job->setMainWindow(mMainWindow);
job->setMessageItem(mMessageItem);
job->setMessage(mMessage);
bool showWarning = true;
Q_FOREACH( KMime::Content *content, contents ) {
if ( !editAttachment( content, showWarning ) )
return;
if ( !job->addAttachment( content, showWarning ) )
break;
showWarning = false;
}
job->canDeleteJob();
}
void ViewerPrivate::slotAttachmentEditDone( MessageViewer::EditorWatcher* editorWatcher )
{
const QString name = editorWatcher->url().fileName();
if ( editorWatcher->fileChanged() ) {
QFile file( name );
if ( file.open( QIODevice::ReadOnly ) ) {
QByteArray data = file.readAll();
KMime::Content *node = mEditorWatchers[editorWatcher];
node->setBody( data );
file.close();
mMessageItem.setPayloadFromData( mMessage->encodedContent() );
Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( mMessageItem );
connect( job, SIGNAL(result(KJob*)), SLOT(itemModifiedResult(KJob*)) );
}
}
mEditorWatchers.remove( editorWatcher );
QFile::remove( name );
}
void ViewerPrivate::slotLevelQuote( int l )
{
if (mLevelQuote != l) {
......@@ -3268,3 +3232,10 @@ void ViewerPrivate::slotCreateEvent(const KCalCore::Event::Ptr &eventPtr, const
CreateEventJob *createJob = new CreateEventJob(eventPtr, collection, mMessageItem, this);
createJob->start();
}
void ViewerPrivate::slotRefreshMessage(const Akonadi::Item &item)
{
if (item.id() == mMessageItem.id()) {
setMessageItem( item, MessageViewer::Viewer::Force );
}
}
......@@ -65,7 +65,6 @@ class SlideContainer;
namespace MessageViewer {
class TodoEdit;
class EventEdit;
class EditorWatcher;
class HtmlWriter;
class CSSHelper;
class AttachmentStrategy;
......@@ -207,7 +206,7 @@ public:
/** Edit the attachment the @param node points to. Returns false if the user
cancelled the editing, true in all other cases! */
bool editAttachment( KMime::Content* node, bool showWarning = true );
void editAttachment( KMime::Content* node, bool showWarning = true );
/** Access to the MailWebView used for the viewer. Use with
care! */
......@@ -493,6 +492,7 @@ private slots:
void slotCreateEvent(const KCalCore::Event::Ptr &eventPtr, const Akonadi::Collection &collection);
void slotRefreshMessage(const Akonadi::Item &item);
public slots:
/** An URL has been activate with a click. */
void slotUrlOpen( const QUrl &url = QUrl());
......@@ -560,7 +560,6 @@ public slots:
void slotAttachmentCopy();
void slotAttachmentDelete();
void slotAttachmentEdit();
void slotAttachmentEditDone(MessageViewer::EditorWatcher* editorWatcher);
void slotLevelQuote( int l );
/** Toggle display mode between HTML and plain text. */
......@@ -707,7 +706,6 @@ public:
KMime::Content *mMessagePartNode;
QString mCurrentFileName;
QString mMessagePath;
QMap<EditorWatcher*, KMime::Content*> mEditorWatchers;
Viewer *const q;
bool mShowFullToAddressList;
bool mShowFullCcAddressList;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment