33 #include <KFileDialog>
35 #include <KMessageBox>
38 #include <KTemporaryFile>
39 #include <KToolInvocation>
40 #include <KIO/NetAccess>
46 using namespace KCalCore;
48 namespace CalendarSupport {
52 QString attachmentName;
55 class AttachmentHandler::Private
62 QMap<KJob *,ReceivedInfo> mJobToReceivedInfo;
63 QPointer<QWidget> mParent;
66 AttachmentHandler::AttachmentHandler(
QWidget *parent ) :
QObject( parent ), d( new Private( parent ) )
80 return Attachment::Ptr();
84 const Attachment::List as = incidence->attachments();
86 if ( !as.isEmpty() ) {
87 Attachment::List::ConstIterator it;
88 Attachment::List::ConstIterator end( as.constEnd() );
90 for ( it = as.constBegin(); it != end; ++it ) {
91 if ( (*it)->label() == attachmentName ) {
101 i18n(
"No attachment named \"%1\" found in the incidence.", attachmentName ) );
102 return Attachment::Ptr();
106 if ( !KIO::NetAccess::exists( a->uri(), KIO::NetAccess::SourceSide, 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();
118 const ScheduleMessage::Ptr &message )
121 return Attachment::Ptr();
124 Incidence::Ptr incidence = message->event().dynamicCast<Incidence>();
128 i18n(
"The calendar invitation stored in this email message is broken in some way. "
129 "Unable to continue." ) );
130 return Attachment::Ptr();
133 return find( attachmentName, incidence );
144 QStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns();
145 if ( !patterns.empty() ) {
146 s_tempFile->setSuffix( QString( patterns.first() ).
remove( QLatin1Char(
'*') ) );
149 s_tempFile->setPermissions( QFile::ReadUser );
150 s_tempFile->write( QByteArray::fromBase64( attachment->data() ) );
153 if ( tf.size() != attachment->size() ) {
171 if ( attachment->isUri() ) {
172 KToolInvocation::invokeBrowser( attachment->uri() );
176 if ( tempUrl.isValid() ) {
177 stat = KRun::runUrl( tempUrl, attachment->mimeType(), 0, true );
182 i18n(
"Unable to create a temporary file for the attachment." ) );
191 const Incidence::Ptr &incidence )
193 return view(
find( attachmentName, incidence ) );
201 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFinishView(KJob*)) );
203 info.attachmentName = attachmentName;
205 d->mJobToReceivedInfo[job] = info;
209 const ScheduleMessage::Ptr &message )
211 return view(
find( attachmentName, message ) );
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(
223 i18n(
"%1 already exists. Do you want to overwrite it?",
224 saveAsFile ) ) == KMessageBox::No ) ) ) {
229 if ( attachment->isUri() ) {
231 stat = KIO::NetAccess::file_copy( attachment->uri(), KUrl( saveAsFile ) );
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() );
244 i18n(
"Unable to create a temporary file for the attachment." ) );
253 const Incidence::Ptr &incidence )
255 return saveAs(
find( attachmentName, incidence ) );
263 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFinishView(KJob*)) );
266 info.attachmentName = attachmentName;
268 d->mJobToReceivedInfo[job] = info;
272 const ScheduleMessage::Ptr &message )
274 return saveAs(
find( attachmentName, message ) );
277 void AttachmentHandler::slotFinishSaveAs( KJob *job )
279 IncidenceSearchJob *searchJob = qobject_cast<IncidenceSearchJob*>( job );
280 const KCalCore::Incidence::List
incidences = searchJob->incidences();
281 ReceivedInfo info = d->mJobToReceivedInfo[job];
283 bool success =
false;
284 if ( !incidences.isEmpty() ) {
285 if (
saveAs( info.attachmentName, incidences.first() ) ) {
291 d->mJobToReceivedInfo.remove( job );
294 void AttachmentHandler::slotFinishView( KJob *job )
296 IncidenceSearchJob *searchJob = qobject_cast<IncidenceSearchJob*>( job );
297 const KCalCore::Incidence::List incidences = searchJob->incidences();
298 ReceivedInfo info = d->mJobToReceivedInfo[job];
300 bool success =
false;
301 if ( !incidences.isEmpty() ) {
302 if (
view( info.attachmentName, incidences.first() ) ) {
307 emit
viewFinished( info.uid, info.attachmentName, success );
308 d->mJobToReceivedInfo.remove( job );
The result must match exactly the pattern (case sensitive).
static KUrl tempFileForAttachment(const Attachment::Ptr &attachment)
KCalCore::Attachment::Ptr find(const QString &attachmentName, const KCalCore::Incidence::Ptr &incidence)
Finds the attachment in the user's calendar, by attachmentName and incidence.
The global unique identifier of the 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 ...
static KTemporaryFile * s_tempFile
bool saveAs(const KCalCore::Attachment::Ptr &attachment)
Saves the specified attachment to a file of the user's choice.
CALENDARSUPPORT_EXPORT KCalCore::Incidence::List incidences(const QMimeData *mimeData, const KDateTime::Spec &timeSpec)
This file is part of the API for handling calendar data and provides static functions for dealing wit...
bool view(const KCalCore::Attachment::Ptr &attachment)
Launches a viewer on the specified attachment.
Job that searches for calendar incidences in the Akonadi storage.
void viewFinished(const QString &uid, const QString &attachmentName, bool success)
void setQuery(Criterion criterion, const QString &value, Match match)
Sets the criterion and value for the search with match.
void saveAsFinished(const QString &uid, const QString &attachmentName, bool success)