libkcal

attachmenthandler.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcal library.
3 
4  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
31 #include "attachmenthandler.h"
32 #include "attachment.h"
33 #include "calendarresources.h"
34 #include "incidence.h"
35 #include "scheduler.h"
36 
37 #include <tdeapplication.h>
38 #include <tdefiledialog.h>
39 #include <tdelocale.h>
40 #include <tdemessagebox.h>
41 #include <kmimetype.h>
42 #include <krun.h>
43 #include <tdetempfile.h>
44 #include <tdeio/netaccess.h>
45 
46 #include <tqfile.h>
47 
48 namespace KCal {
49 
50 Attachment *AttachmentHandler::find( TQWidget *parent, const TQString &attachmentName,
51  Incidence *incidence )
52 {
53  if ( !incidence ) {
54  return 0;
55  }
56 
57  // get the attachment by name from the incidence
58  Attachment::List as = incidence->attachments();
59  Attachment *a = 0;
60  if ( as.count() > 0 ) {
61  Attachment::List::ConstIterator it;
62  for ( it = as.begin(); it != as.end(); ++it ) {
63  if ( (*it)->label() == attachmentName ) {
64  a = *it;
65  break;
66  }
67  }
68  }
69 
70  if ( !a ) {
71  KMessageBox::error(
72  parent,
73  i18n( "No attachment named \"%1\" found in the incidence." ).arg( attachmentName ) );
74  return 0;
75  }
76 
77  if ( a->isUri() ) {
78  if ( !TDEIO::NetAccess::exists( a->uri(), true, parent ) ) {
79  KMessageBox::sorry(
80  parent,
81  i18n( "The attachment \"%1\" is a web link that is inaccessible from this computer. " ).
82  arg( KURL::decode_string( a->uri() ) ) );
83  return 0;
84  }
85  }
86  return a;
87 }
88 
90  const TQString &attachmentName, const TQString &uid )
91 {
92  if ( uid.isEmpty() ) {
93  return 0;
94  }
95 
96  CalendarResources *cal = new CalendarResources( "UTC" );
97  cal->readConfig();
98  cal->load();
99  Incidence *incidence = cal->incidence( uid );
100  if ( !incidence ) {
101  KMessageBox::error(
102  parent,
103  i18n( "The incidence that owns the attachment named \"%1\" could not be found. "
104  "Perhaps it was removed from your calendar?" ).arg( attachmentName ) );
105  return 0;
106  }
107 
108  return find( parent, attachmentName, incidence );
109 }
110 
111 Attachment *AttachmentHandler::find( TQWidget *parent, const TQString &attachmentName,
112  ScheduleMessage *message )
113 {
114  if ( !message ) {
115  return 0;
116  }
117 
118  Incidence *incidence = dynamic_cast<Incidence*>( message->event() );
119  if ( !incidence ) {
120  KMessageBox::error(
121  parent,
122  i18n( "The calendar invitation stored in this email message is broken in some way. "
123  "Unable to continue." ) );
124  return 0;
125  }
126 
127  return find( parent, attachmentName, incidence );
128 }
129 
130 static KTempFile *s_tempFile = 0;
131 
132 static KURL tempFileForAttachment( Attachment *attachment )
133 {
134  KURL url;
135  TQStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns();
136  if ( !patterns.empty() ) {
137  s_tempFile = new KTempFile( TQString(),
138  TQString( patterns.first() ).remove( '*' ), 0600 );
139  } else {
140  s_tempFile = new KTempFile( TQString(), TQString(), 0600 );
141  }
142 
143  TQFile *qfile = s_tempFile->file();
144  qfile->open( IO_WriteOnly );
145  TQTextStream stream( qfile );
146  stream.writeRawBytes( attachment->decodedData().data(), attachment->size() );
147  s_tempFile->close();
148  TQFile tf( s_tempFile->name() );
149  if ( tf.size() != attachment->size() ) {
150  //whoops. failed to write the entire attachment. return an invalid URL.
151  delete s_tempFile;
152  s_tempFile = 0;
153  return url;
154  }
155 
156  url.setPath( s_tempFile->name() );
157  return url;
158 }
159 
160 bool AttachmentHandler::view( TQWidget *parent, Attachment *attachment )
161 {
162  if ( !attachment ) {
163  return false;
164  }
165 
166  bool stat = true;
167  if ( attachment->isUri() ) {
168  kapp->invokeBrowser( attachment->uri() );
169  } else {
170  // put the attachment in a temporary file and launch it
171  KURL tempUrl = tempFileForAttachment( attachment );
172  if ( tempUrl.isValid() ) {
173  stat = KRun::runURL( tempUrl, attachment->mimeType(), false, true );
174  } else {
175  stat = false;
176  KMessageBox::error(
177  parent,
178  i18n( "Unable to create a temporary file for the attachment." ) );
179  }
180  delete s_tempFile;
181  s_tempFile = 0;
182  }
183  return stat;
184 }
185 
186 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName, Incidence *incidence )
187 {
188  return view( parent, find( parent, attachmentName, incidence ) );
189 }
190 
191 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName, const TQString &uid )
192 {
193  return view( parent, find( parent, attachmentName, uid ) );
194 }
195 
196 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName,
197  ScheduleMessage *message )
198 {
199  return view( parent, find( parent, attachmentName, message ) );
200 }
201 
202 bool AttachmentHandler::saveAs( TQWidget *parent, Attachment *attachment )
203 {
204  // get the saveas file name
205  TQString saveAsFile = KFileDialog::getSaveFileName( attachment->label(), TQString(), parent,
206  i18n( "Save Attachment" ) );
207  if ( saveAsFile.isEmpty() ||
208  ( TQFile( saveAsFile ).exists() &&
209  ( KMessageBox::warningYesNo(
210  parent,
211  i18n( "%1 already exists. Do you want to overwrite it?").
212  arg( saveAsFile ) ) == KMessageBox::No ) ) ) {
213  return false;
214  }
215 
216  bool stat = false;
217  if ( attachment->isUri() ) {
218  // save the attachment url
219  stat = TDEIO::NetAccess::file_copy( attachment->uri(), KURL( saveAsFile ), -1, true );
220  } else {
221  // put the attachment in a temporary file and save it
222  KURL tempUrl = tempFileForAttachment( attachment );
223  if ( tempUrl.isValid() ) {
224  stat = TDEIO::NetAccess::file_copy( tempUrl, KURL( saveAsFile ), -1, true );
225  if ( !stat && TDEIO::NetAccess::lastError() ) {
226  KMessageBox::error( parent, TDEIO::NetAccess::lastErrorString() );
227  }
228  } else {
229  stat = false;
230  KMessageBox::error(
231  parent,
232  i18n( "Unable to create a temporary file for the attachment." ) );
233  }
234  delete s_tempFile;
235  s_tempFile = 0;
236  }
237  return stat;
238 }
239 
240 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName,
241  Incidence *incidence )
242 {
243  return saveAs( parent, find( parent, attachmentName, incidence ) );
244 }
245 
246 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName, const TQString &uid )
247 {
248  return saveAs( parent, find( parent, attachmentName, uid ) );
249 }
250 
251 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName,
252  ScheduleMessage *message )
253 {
254  return saveAs( parent, find( parent, attachmentName, message ) );
255 }
256 
257 }
258 
This file is part of the API for handling calendar data and provides static functions for dealing wit...
Provides a Calendar composed of several Calendar Resources.
This class represents information related to an attachment.
Definition: attachment.h:35
This class provides a Calendar which is composed of other Calendars known as "Resources".
void load()
Loads all Incidences from the Resources.
void readConfig(TDEConfig *config=0)
Read the Resources settings from a config file.
Incidence * incidence(const TQString &uid)
Returns the Incidence associated with the given unique identifier.
Definition: calendar.cpp:576
This class provides the base class common to all calendar components.
Definition: incidence.h:48
Attachment::List attachments() const
Return list of all associated attachments.
Definition: incidence.cpp:695
This class provides an encapsulation of a scheduling message.
Definition: scheduler.h:45
IncidenceBase * event()
Return event associated with this message.
Definition: scheduler.h:63
bool view(TQWidget *parent, Attachment *attachment)
Launches a viewer on the specified attachment.
bool saveAs(TQWidget *parent, Attachment *attachment)
Saves the specified attachment to a file of the user's choice.
Attachment * find(TQWidget *parent, const TQString &attachmentName, Incidence *incidence)
Finds the attachment in the user's calendar, by attachmentName and incidence.
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38