• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • job
attachmenteditjob.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "attachmenteditjob.h"
19 #include "viewer/editorwatcher.h"
20 #include <KMessageBox>
21 #include <KLocalizedString>
22 #include <KTemporaryFile>
23 #include <KUrl>
24 #include <KDebug>
25 #include <Akonadi/ItemModifyJob>
26 #include <KMime/Content>
27 
28 using namespace MessageViewer;
29 
30 AttachmentEditJob::AttachmentEditJob(QObject *parent)
31  : QObject(parent),
32  mShowWarning(true),
33  mMainWindow(0)
34 {
35 
36 }
37 
38 AttachmentEditJob::~AttachmentEditJob()
39 {
40  qDebug()<<" AttachmentEditJob::~AttachmentEditJob()";
41 }
42 
43 bool AttachmentEditJob::addAttachment(KMime::Content * node, bool showWarning)
44 {
45  if ( showWarning && KMessageBox::warningContinueCancel( mMainWindow,
46  i18n("Modifying an attachment might invalidate any digital signature on this message."),
47  i18n("Edit Attachment"), KGuiItem( i18n("Edit"), QLatin1String("document-properties") ), KStandardGuiItem::cancel(),
48  QLatin1String("EditAttachmentSignatureWarning") )
49  != KMessageBox::Continue ) {
50  return false;
51  }
52 
53  KTemporaryFile file;
54  file.setAutoRemove( false );
55  if ( !file.open() ) {
56  kWarning() << "Edit Attachment: Unable to open temp file.";
57  return true;
58  }
59  file.write( node->decodedContent() );
60  file.flush();
61 
62  EditorWatcher *watcher =
63  new EditorWatcher( KUrl( file.fileName() ), QLatin1String(node->contentType()->mimeType()),
64  MessageViewer::EditorWatcher::NoOpenWithDialog, this, mMainWindow );
65  mEditorWatchers[ watcher ] = node;
66 
67  connect( watcher, SIGNAL(editDone(MessageViewer::EditorWatcher*)), SLOT(slotAttachmentEditDone(MessageViewer::EditorWatcher*)) );
68 
69  if ( (watcher->start() != EditorWatcher::NoError) ) {
70  removeEditorWatcher(watcher, file.fileName());
71  }
72  return true;
73 }
74 
75 void AttachmentEditJob::setMainWindow(QWidget *mainWindow)
76 {
77  mMainWindow = mainWindow;
78 }
79 
80 void AttachmentEditJob::slotAttachmentEditDone( MessageViewer::EditorWatcher* editorWatcher )
81 {
82  const QString name = editorWatcher->url().path();
83  if ( editorWatcher->fileChanged() ) {
84  QFile file( name );
85  if ( file.open( QIODevice::ReadOnly ) ) {
86  QByteArray data = file.readAll();
87  KMime::Content *node = mEditorWatchers[editorWatcher];
88  node->setBody( data );
89  file.close();
90 
91  mMessageItem.setPayloadFromData( mMessage->encodedContent() );
92  Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( mMessageItem );
93  connect( job, SIGNAL(result(KJob*)), SLOT(slotItemModifiedResult(KJob*)) );
94  removeEditorWatcher(editorWatcher, name);
95  }
96  } else {
97  removeEditorWatcher(editorWatcher, name);
98  canDeleteJob();
99  }
100 }
101 
102 void AttachmentEditJob::setMessageItem(const Akonadi::Item &messageItem)
103 {
104  mMessageItem = messageItem;
105 }
106 
107 void AttachmentEditJob::slotItemModifiedResult(KJob *job)
108 {
109  if ( job->error() ) {
110  kDebug() << "Item update failed:" << job->errorString();
111  } else {
112  Q_EMIT refreshMessage(mMessageItem);
113  }
114  canDeleteJob();
115 }
116 
117 void AttachmentEditJob::canDeleteJob()
118 {
119  if (mEditorWatchers.isEmpty())
120  deleteLater();
121 }
122 
123 void AttachmentEditJob::removeEditorWatcher(MessageViewer::EditorWatcher* editorWatcher, const QString &name)
124 {
125  mEditorWatchers.remove( editorWatcher );
126  QFile::remove( name );
127 }
128 
129 void AttachmentEditJob::setMessage(const KMime::Message::Ptr &message)
130 {
131  mMessage = message;
132 }
133 
editorwatcher.h
QWidget
MessageViewer::AttachmentEditJob::refreshMessage
void refreshMessage(const Akonadi::Item &item)
QByteArray
QFile::remove
bool remove()
MessageViewer::AttachmentEditJob::canDeleteJob
void canDeleteJob()
Definition: attachmenteditjob.cpp:117
MessageViewer::EditorWatcher
Starts an editor for the given URL and emits an signal when editing has been finished.
Definition: editorwatcher.h:39
QFile
MessageViewer::EditorWatcher::start
ErrorEditorWatcher start()
Definition: editorwatcher.cpp:72
QObject::name
const char * name() const
QObject
MessageViewer::AttachmentEditJob::setMainWindow
void setMainWindow(QWidget *mainWindow)
Definition: attachmenteditjob.cpp:75
attachmenteditjob.h
MessageViewer::EditorWatcher::NoError
Definition: editorwatcher.h:52
QObject::deleteLater
void deleteLater()
MessageViewer::AttachmentEditJob::~AttachmentEditJob
~AttachmentEditJob()
Definition: attachmenteditjob.cpp:38
QString
MessageViewer::EditorWatcher::NoOpenWithDialog
Definition: editorwatcher.h:45
MessageViewer::EditorWatcher::fileChanged
bool fileChanged() const
Definition: editorwatcher.cpp:123
QLatin1String
MessageViewer::EditorWatcher::url
KUrl url() const
Definition: editorwatcher.cpp:128
MessageViewer::AttachmentEditJob::setMessageItem
void setMessageItem(const Akonadi::Item &messageItem)
Definition: attachmenteditjob.cpp:102
MessageViewer::AttachmentEditJob::AttachmentEditJob
AttachmentEditJob(QObject *parent=0)
Definition: attachmenteditjob.cpp:30
MessageViewer::AttachmentEditJob::setMessage
void setMessage(const KMime::Message::Ptr &message)
Definition: attachmenteditjob.cpp:129
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
MessageViewer::AttachmentEditJob::addAttachment
bool addAttachment(KMime::Content *node, bool showWarning)
Definition: attachmenteditjob.cpp:43
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal