attachmenthandler.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the kcal library. 00003 00004 Copyright (c) 2010 Klar�lvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00031 #include "attachmenthandler.h" 00032 #include "attachment.h" 00033 #include "calendarresources.h" 00034 #include "incidence.h" 00035 #include "scheduler.h" 00036 00037 #include <kapplication.h> 00038 #include <kfiledialog.h> 00039 #include <klocale.h> 00040 #include <kmessagebox.h> 00041 #include <kmimetype.h> 00042 #include <krun.h> 00043 #include <ktempfile.h> 00044 #include <kio/netaccess.h> 00045 00046 #include <tqfile.h> 00047 00048 namespace KCal { 00049 00050 Attachment *AttachmentHandler::find( TQWidget *parent, const TQString &attachmentName, 00051 Incidence *incidence ) 00052 { 00053 if ( !incidence ) { 00054 return 0; 00055 } 00056 00057 // get the attachment by name from the incidence 00058 Attachment::List as = incidence->attachments(); 00059 Attachment *a = 0; 00060 if ( as.count() > 0 ) { 00061 Attachment::List::ConstIterator it; 00062 for ( it = as.begin(); it != as.end(); ++it ) { 00063 if ( (*it)->label() == attachmentName ) { 00064 a = *it; 00065 break; 00066 } 00067 } 00068 } 00069 00070 if ( !a ) { 00071 KMessageBox::error( 00072 parent, 00073 i18n( "No attachment named \"%1\" found in the incidence." ).arg( attachmentName ) ); 00074 return 0; 00075 } 00076 00077 if ( a->isUri() ) { 00078 if ( !KIO::NetAccess::exists( a->uri(), true, parent ) ) { 00079 KMessageBox::sorry( 00080 parent, 00081 i18n( "The attachment \"%1\" is a web link that is inaccessible from this computer. " ). 00082 arg( KURL::decode_string( a->uri() ) ) ); 00083 return 0; 00084 } 00085 } 00086 return a; 00087 } 00088 00089 Attachment *AttachmentHandler::find( TQWidget *parent, 00090 const TQString &attachmentName, const TQString &uid ) 00091 { 00092 if ( uid.isEmpty() ) { 00093 return 0; 00094 } 00095 00096 CalendarResources *cal = new CalendarResources( "UTC" ); 00097 cal->readConfig(); 00098 cal->load(); 00099 Incidence *incidence = cal->incidence( uid ); 00100 if ( !incidence ) { 00101 KMessageBox::error( 00102 parent, 00103 i18n( "The incidence that owns the attachment named \"%1\" could not be found. " 00104 "Perhaps it was removed from your calendar?" ).arg( attachmentName ) ); 00105 return 0; 00106 } 00107 00108 return find( parent, attachmentName, incidence ); 00109 } 00110 00111 Attachment *AttachmentHandler::find( TQWidget *parent, const TQString &attachmentName, 00112 ScheduleMessage *message ) 00113 { 00114 if ( !message ) { 00115 return 0; 00116 } 00117 00118 Incidence *incidence = dynamic_cast<Incidence*>( message->event() ); 00119 if ( !incidence ) { 00120 KMessageBox::error( 00121 parent, 00122 i18n( "The calendar invitation stored in this email message is broken in some way. " 00123 "Unable to continue." ) ); 00124 return 0; 00125 } 00126 00127 return find( parent, attachmentName, incidence ); 00128 } 00129 00130 static KTempFile *s_tempFile = 0; 00131 00132 static KURL tempFileForAttachment( Attachment *attachment ) 00133 { 00134 KURL url; 00135 TQStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns(); 00136 if ( !patterns.empty() ) { 00137 s_tempFile = new KTempFile( TQString(), 00138 TQString( patterns.first() ).remove( '*' ), 0600 ); 00139 } else { 00140 s_tempFile = new KTempFile( TQString(), TQString(), 0600 ); 00141 } 00142 00143 TQFile *qfile = s_tempFile->file(); 00144 qfile->open( IO_WriteOnly ); 00145 TQTextStream stream( qfile ); 00146 stream.writeRawBytes( attachment->decodedData().data(), attachment->size() ); 00147 s_tempFile->close(); 00148 TQFile tf( s_tempFile->name() ); 00149 if ( tf.size() != attachment->size() ) { 00150 //whoops. failed to write the entire attachment. return an invalid URL. 00151 delete s_tempFile; 00152 s_tempFile = 0; 00153 return url; 00154 } 00155 00156 url.setPath( s_tempFile->name() ); 00157 return url; 00158 } 00159 00160 bool AttachmentHandler::view( TQWidget *parent, Attachment *attachment ) 00161 { 00162 if ( !attachment ) { 00163 return false; 00164 } 00165 00166 bool stat = true; 00167 if ( attachment->isUri() ) { 00168 kapp->invokeBrowser( attachment->uri() ); 00169 } else { 00170 // put the attachment in a temporary file and launch it 00171 KURL tempUrl = tempFileForAttachment( attachment ); 00172 if ( tempUrl.isValid() ) { 00173 stat = KRun::runURL( tempUrl, attachment->mimeType(), false, true ); 00174 } else { 00175 stat = false; 00176 KMessageBox::error( 00177 parent, 00178 i18n( "Unable to create a temporary file for the attachment." ) ); 00179 } 00180 delete s_tempFile; 00181 s_tempFile = 0; 00182 } 00183 return stat; 00184 } 00185 00186 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName, Incidence *incidence ) 00187 { 00188 return view( parent, find( parent, attachmentName, incidence ) ); 00189 } 00190 00191 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName, const TQString &uid ) 00192 { 00193 return view( parent, find( parent, attachmentName, uid ) ); 00194 } 00195 00196 bool AttachmentHandler::view( TQWidget *parent, const TQString &attachmentName, 00197 ScheduleMessage *message ) 00198 { 00199 return view( parent, find( parent, attachmentName, message ) ); 00200 } 00201 00202 bool AttachmentHandler::saveAs( TQWidget *parent, Attachment *attachment ) 00203 { 00204 // get the saveas file name 00205 TQString saveAsFile = KFileDialog::getSaveFileName( attachment->label(), TQString(), parent, 00206 i18n( "Save Attachment" ) ); 00207 if ( saveAsFile.isEmpty() || 00208 ( TQFile( saveAsFile ).exists() && 00209 ( KMessageBox::warningYesNo( 00210 parent, 00211 i18n( "%1 already exists. Do you want to overwrite it?"). 00212 arg( saveAsFile ) ) == KMessageBox::No ) ) ) { 00213 return false; 00214 } 00215 00216 bool stat = false; 00217 if ( attachment->isUri() ) { 00218 // save the attachment url 00219 stat = KIO::NetAccess::file_copy( attachment->uri(), KURL( saveAsFile ), -1, true ); 00220 } else { 00221 // put the attachment in a temporary file and save it 00222 KURL tempUrl = tempFileForAttachment( attachment ); 00223 if ( tempUrl.isValid() ) { 00224 stat = KIO::NetAccess::file_copy( tempUrl, KURL( saveAsFile ), -1, true ); 00225 if ( !stat && KIO::NetAccess::lastError() ) { 00226 KMessageBox::error( parent, KIO::NetAccess::lastErrorString() ); 00227 } 00228 } else { 00229 stat = false; 00230 KMessageBox::error( 00231 parent, 00232 i18n( "Unable to create a temporary file for the attachment." ) ); 00233 } 00234 delete s_tempFile; 00235 s_tempFile = 0; 00236 } 00237 return stat; 00238 } 00239 00240 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName, 00241 Incidence *incidence ) 00242 { 00243 return saveAs( parent, find( parent, attachmentName, incidence ) ); 00244 } 00245 00246 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName, const TQString &uid ) 00247 { 00248 return saveAs( parent, find( parent, attachmentName, uid ) ); 00249 } 00250 00251 bool AttachmentHandler::saveAs( TQWidget *parent, const TQString &attachmentName, 00252 ScheduleMessage *message ) 00253 { 00254 return saveAs( parent, find( parent, attachmentName, message ) ); 00255 } 00256 00257 } 00258