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

calendarsupport

  • sources
  • kde-4.12
  • kdepim
  • calendarsupport
attachmenthandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarlvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3  Author: Allen Winter <allen.winter@kdab.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
30 #include "attachmenthandler.h"
31 #include "next/incidencesearchjob.h"
32 
33 #include <KFileDialog>
34 #include <KLocale>
35 #include <KMessageBox>
36 #include <KMimeType>
37 #include <KRun>
38 #include <KTemporaryFile>
39 #include <KToolInvocation>
40 #include <KIO/NetAccess>
41 #include <KJob>
42 
43 #include <QFile>
44 #include <QPointer>
45 
46 using namespace KCalCore;
47 
48 namespace CalendarSupport {
49 
50 struct ReceivedInfo {
51  QString uid;
52  QString attachmentName;
53 };
54 
55 class AttachmentHandler::Private
56 {
57  public:
58  Private( QWidget *parent )
59  {
60  mParent = parent;
61  }
62  QMap<KJob *,ReceivedInfo> mJobToReceivedInfo;
63  QPointer<QWidget> mParent;
64 };
65 
66 AttachmentHandler::AttachmentHandler( QWidget *parent ) : QObject( parent ), d( new Private( parent ) )
67 {
68 
69 }
70 
71 AttachmentHandler::~AttachmentHandler()
72 {
73  delete d;
74 }
75 
76 Attachment::Ptr AttachmentHandler::find( const QString &attachmentName,
77  const Incidence::Ptr &incidence )
78 {
79  if ( !incidence ) {
80  return Attachment::Ptr();
81  }
82 
83  // get the attachment by name from the incidence
84  const Attachment::List as = incidence->attachments();
85  Attachment::Ptr a;
86  if ( !as.isEmpty() ) {
87  Attachment::List::ConstIterator it;
88  Attachment::List::ConstIterator end( as.constEnd() );
89 
90  for ( it = as.constBegin(); it != end; ++it ) {
91  if ( (*it)->label() == attachmentName ) {
92  a = *it;
93  break;
94  }
95  }
96  }
97 
98  if ( !a ) {
99  KMessageBox::error(
100  d->mParent,
101  i18n( "No attachment named \"%1\" found in the incidence.", attachmentName ) );
102  return Attachment::Ptr();
103  }
104 
105  if ( a->isUri() ) {
106  if ( !KIO::NetAccess::exists( a->uri(), KIO::NetAccess::SourceSide, d->mParent ) ) {
107  KMessageBox::sorry(
108  d->mParent,
109  i18n( "The attachment \"%1\" is a web link that is inaccessible from this computer. ",
110  KUrl::fromPercentEncoding( a->uri().toLatin1() ) ) );
111  return Attachment::Ptr();
112  }
113  }
114  return a;
115 }
116 
117 Attachment::Ptr AttachmentHandler::find( const QString &attachmentName,
118  const ScheduleMessage::Ptr &message )
119 {
120  if ( !message ) {
121  return Attachment::Ptr();
122  }
123 
124  Incidence::Ptr incidence = message->event().dynamicCast<Incidence>();
125  if ( !incidence ) {
126  KMessageBox::error(
127  d->mParent,
128  i18n( "The calendar invitation stored in this email message is broken in some way. "
129  "Unable to continue." ) );
130  return Attachment::Ptr();
131  }
132 
133  return find( attachmentName, incidence );
134 }
135 
136 static KTemporaryFile *s_tempFile = 0;
137 
138 static KUrl tempFileForAttachment( const Attachment::Ptr &attachment )
139 {
140  KUrl url;
141 
142  s_tempFile = new KTemporaryFile();
143  s_tempFile->setAutoRemove( false );
144  QStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns();
145  if ( !patterns.empty() ) {
146  s_tempFile->setSuffix( QString( patterns.first() ).remove( QLatin1Char('*') ) );
147  }
148  s_tempFile->open();
149  s_tempFile->setPermissions( QFile::ReadUser );
150  s_tempFile->write( QByteArray::fromBase64( attachment->data() ) );
151  s_tempFile->close();
152  QFile tf( s_tempFile->fileName() );
153  if ( tf.size() != attachment->size() ) {
154  //whoops. failed to write the entire attachment. return an invalid URL.
155  delete s_tempFile;
156  s_tempFile = 0;
157  return url;
158  }
159 
160  url.setPath( s_tempFile->fileName() );
161  return url;
162 }
163 
164 bool AttachmentHandler::view( const Attachment::Ptr &attachment )
165 {
166  if ( !attachment ) {
167  return false;
168  }
169 
170  bool stat = true;
171  if ( attachment->isUri() ) {
172  KToolInvocation::invokeBrowser( attachment->uri() );
173  } else {
174  // put the attachment in a temporary file and launch it
175  KUrl tempUrl = tempFileForAttachment( attachment );
176  if ( tempUrl.isValid() ) {
177  stat = KRun::runUrl( tempUrl, attachment->mimeType(), 0, true );
178  } else {
179  stat = false;
180  KMessageBox::error(
181  d->mParent,
182  i18n( "Unable to create a temporary file for the attachment." ) );
183  }
184  delete s_tempFile;
185  s_tempFile = 0;
186  }
187  return stat;
188 }
189 
190 bool AttachmentHandler::view( const QString &attachmentName,
191  const Incidence::Ptr &incidence )
192 {
193  return view( find( attachmentName, incidence ) );
194 }
195 
196 void AttachmentHandler::view( const QString &attachmentName, const QString &uid )
197 {
198  IncidenceSearchJob *job = new IncidenceSearchJob();
199  job->setQuery( CalendarSupport::IncidenceSearchJob::IncidenceUid, uid,
200  IncidenceSearchJob::ExactMatch );
201  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFinishView(KJob*)) );
202  ReceivedInfo info;
203  info.attachmentName = attachmentName;
204  info.uid = uid;
205  d->mJobToReceivedInfo[job] = info;
206 }
207 
208 bool AttachmentHandler::view( const QString &attachmentName,
209  const ScheduleMessage::Ptr &message )
210 {
211  return view( find( attachmentName, message ) );
212 }
213 
214 bool AttachmentHandler::saveAs( const Attachment::Ptr &attachment )
215 {
216  // get the saveas file name
217  QString saveAsFile = KFileDialog::getSaveFileName( attachment->label(), QString(), d->mParent,
218  i18n( "Save Attachment" ) );
219  if ( saveAsFile.isEmpty() ||
220  ( QFile( saveAsFile ).exists() &&
221  ( KMessageBox::warningYesNo(
222  d->mParent,
223  i18n( "%1 already exists. Do you want to overwrite it?",
224  saveAsFile ) ) == KMessageBox::No ) ) ) {
225  return false;
226  }
227 
228  bool stat = false;
229  if ( attachment->isUri() ) {
230  // save the attachment url
231  stat = KIO::NetAccess::file_copy( attachment->uri(), KUrl( saveAsFile ) );
232  } else {
233  // put the attachment in a temporary file and save it
234  KUrl tempUrl = tempFileForAttachment( attachment );
235  if ( tempUrl.isValid() ) {
236  stat = KIO::NetAccess::file_copy( tempUrl, KUrl( saveAsFile ) );
237  if ( !stat && KIO::NetAccess::lastError() ) {
238  KMessageBox::error( d->mParent, KIO::NetAccess::lastErrorString() );
239  }
240  } else {
241  stat = false;
242  KMessageBox::error(
243  d->mParent,
244  i18n( "Unable to create a temporary file for the attachment." ) );
245  }
246  delete s_tempFile;
247  s_tempFile = 0;
248  }
249  return stat;
250 }
251 
252 bool AttachmentHandler::saveAs( const QString &attachmentName,
253  const Incidence::Ptr &incidence )
254 {
255  return saveAs( find( attachmentName, incidence ) );
256 }
257 
258 void AttachmentHandler::saveAs( const QString &attachmentName, const QString &uid )
259 {
260  IncidenceSearchJob *job = new IncidenceSearchJob();
261  job->setQuery( CalendarSupport::IncidenceSearchJob::IncidenceUid, uid,
262  IncidenceSearchJob::ExactMatch );
263  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFinishView(KJob*)) );
264 
265  ReceivedInfo info;
266  info.attachmentName = attachmentName;
267  info.uid = uid;
268  d->mJobToReceivedInfo[job] = info;
269 }
270 
271 bool AttachmentHandler::saveAs( const QString &attachmentName,
272  const ScheduleMessage::Ptr &message )
273 {
274  return saveAs( find( attachmentName, message ) );
275 }
276 
277 void AttachmentHandler::slotFinishSaveAs( KJob *job )
278 {
279  IncidenceSearchJob *searchJob = qobject_cast<IncidenceSearchJob*>( job );
280  const KCalCore::Incidence::List incidences = searchJob->incidences();
281  ReceivedInfo info = d->mJobToReceivedInfo[job];
282 
283  bool success = false;
284  if ( !incidences.isEmpty() ) {
285  if ( saveAs( info.attachmentName, incidences.first() ) ) {
286  success = true;
287  }
288  }
289 
290  emit saveAsFinished( info.uid, info.attachmentName, success );
291  d->mJobToReceivedInfo.remove( job );
292 }
293 
294 void AttachmentHandler::slotFinishView( KJob *job )
295 {
296  IncidenceSearchJob *searchJob = qobject_cast<IncidenceSearchJob*>( job );
297  const KCalCore::Incidence::List incidences = searchJob->incidences();
298  ReceivedInfo info = d->mJobToReceivedInfo[job];
299 
300  bool success = false;
301  if ( !incidences.isEmpty() ) {
302  if ( view( info.attachmentName, incidences.first() ) ) {
303  success = true;
304  }
305  }
306 
307  emit viewFinished( info.uid, info.attachmentName, success );
308  d->mJobToReceivedInfo.remove( job );
309 }
310 
311 } // namespace CalendarSupport
312 
CalendarSupport::IncidenceSearchJob::ExactMatch
The result must match exactly the pattern (case sensitive).
Definition: incidencesearchjob.h:115
CalendarSupport::tempFileForAttachment
static KUrl tempFileForAttachment(const Attachment::Ptr &attachment)
Definition: attachmenthandler.cpp:138
QWidget
CalendarSupport::AttachmentHandler::find
KCalCore::Attachment::Ptr find(const QString &attachmentName, const KCalCore::Incidence::Ptr &incidence)
Finds the attachment in the user's calendar, by attachmentName and incidence.
CalendarSupport::IncidenceSearchJob::IncidenceUid
The global unique identifier of the incidence.
Definition: incidencesearchjob.h:106
QObject
CalendarSupport::incidence
CALENDARSUPPORT_EXPORT KCalCore::Incidence::Ptr incidence(const Akonadi::Item &item)
returns the incidence from an akonadi item, or a null pointer if the item has no such payload ...
Definition: utils.cpp:75
CalendarSupport::s_tempFile
static KTemporaryFile * s_tempFile
Definition: attachmenthandler.cpp:136
CalendarSupport::AttachmentHandler::saveAs
bool saveAs(const KCalCore::Attachment::Ptr &attachment)
Saves the specified attachment to a file of the user's choice.
CalendarSupport::incidences
CALENDARSUPPORT_EXPORT KCalCore::Incidence::List incidences(const QMimeData *mimeData, const KDateTime::Spec &timeSpec)
Definition: utils.cpp:374
attachmenthandler.h
This file is part of the API for handling calendar data and provides static functions for dealing wit...
CalendarSupport::AttachmentHandler::view
bool view(const KCalCore::Attachment::Ptr &attachment)
Launches a viewer on the specified attachment.
CalendarSupport::IncidenceSearchJob
Job that searches for calendar incidences in the Akonadi storage.
Definition: incidencesearchjob.h:85
CalendarSupport::AttachmentHandler::~AttachmentHandler
~AttachmentHandler()
Definition: attachmenthandler.cpp:71
CalendarSupport::AttachmentHandler::viewFinished
void viewFinished(const QString &uid, const QString &attachmentName, bool success)
CalendarSupport::IncidenceSearchJob::setQuery
void setQuery(Criterion criterion, const QString &value, Match match)
Sets the criterion and value for the search with match.
Definition: incidencesearchjob.cpp:72
incidencesearchjob.h
CalendarSupport::AttachmentHandler::saveAsFinished
void saveAsFinished(const QString &uid, const QString &attachmentName, bool success)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

calendarsupport

Skip menu "calendarsupport"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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

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