34 #include <Akonadi/ItemFetchJob> 
   35 #include <KFileDialog> 
   36 #include <KLocalizedString> 
   37 #include <KMessageBox> 
   40 #include <KTemporaryFile> 
   41 #include <KToolInvocation> 
   42 #include <KIO/NetAccess> 
   48 using namespace KCalCore;
 
   49 using namespace Akonadi;
 
   51 namespace CalendarSupport {
 
   58 class AttachmentHandler::Private
 
   69 AttachmentHandler::AttachmentHandler( 
QWidget *parent ) : 
QObject( parent ), d( new Private( parent ) )
 
   83     return Attachment::Ptr();
 
   87   const Attachment::List as = incidence->attachments();
 
   89   if ( !as.isEmpty() ) {
 
   90     Attachment::List::ConstIterator it;
 
   91     Attachment::List::ConstIterator end( as.constEnd() );
 
   93     for ( it = as.constBegin(); it != end; ++it ) {
 
   94       if ( (*it)->label() == attachmentName ) {
 
  104       i18n( 
"No attachment named \"%1\" found in the incidence.", attachmentName ) );
 
  105     return Attachment::Ptr();
 
  109     if ( !KIO::NetAccess::exists( a->uri(), KIO::NetAccess::SourceSide, d->mParent ) ) {
 
  112         i18n( 
"The attachment \"%1\" is a web link that is inaccessible from this computer. ",
 
  113               KUrl::fromPercentEncoding( a->uri().toLatin1() ) ) );
 
  114       return Attachment::Ptr();
 
  121                                          const ScheduleMessage::Ptr &message )
 
  124     return Attachment::Ptr();
 
  127   Incidence::Ptr incidence = message->event().dynamicCast<Incidence>();
 
  131       i18n( 
"The calendar invitation stored in this email message is broken in some way. " 
  132             "Unable to continue." ) );
 
  133     return Attachment::Ptr();
 
  136   return find( attachmentName, incidence );
 
  147   QStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns();
 
  148   if ( !patterns.
empty() ) {
 
  152   s_tempFile->setPermissions( QFile::ReadUser );
 
  156   if ( tf.size() != attachment->size() ) {
 
  174   if ( attachment->isUri() ) {
 
  175     KToolInvocation::invokeBrowser( attachment->uri() );
 
  179     if ( tempUrl.isValid() ) {
 
  180       stat = KRun::runUrl( tempUrl, attachment->mimeType(), 0, true );
 
  185         i18n( 
"Unable to create a temporary file for the attachment." ) );
 
  194                               const Incidence::Ptr &incidence )
 
  196   return view( 
find( attachmentName, incidence ) );
 
  203   ItemFetchJob *job = 
new ItemFetchJob(item);
 
  204   connect( job, SIGNAL(result(KJob*)), 
this, SLOT(slotFinishView(KJob*)) );
 
  206   info.attachmentName = attachmentName;
 
  208   d->mJobToReceivedInfo[job] = info;
 
  212                               const ScheduleMessage::Ptr &message )
 
  214   return view( 
find( attachmentName, message ) );
 
  220   QString saveAsFile = KFileDialog::getSaveFileName( attachment->label(), 
QString(), d->mParent,
 
  221                                                      i18n( 
"Save Attachment" ), KFileDialog::ConfirmOverwrite );
 
  224          ( KMessageBox::warningYesNo(
 
  226              i18n( 
"%1 already exists. Do you want to overwrite it?",
 
  227                    saveAsFile ) ) == KMessageBox::No ) ) ) {
 
  232   if ( attachment->isUri() ) {
 
  234     stat = KIO::NetAccess::file_copy( attachment->uri(), KUrl( saveAsFile ) );
 
  238     if ( tempUrl.isValid() ) {
 
  239       stat = KIO::NetAccess::file_copy( tempUrl, KUrl( saveAsFile ) );
 
  240       if ( !stat && KIO::NetAccess::lastError() ) {
 
  241         KMessageBox::error( d->mParent, KIO::NetAccess::lastErrorString() );
 
  247         i18n( 
"Unable to create a temporary file for the attachment." ) );
 
  256                                 const Incidence::Ptr &incidence )
 
  258   return saveAs( 
find( attachmentName, incidence ) );
 
  265   ItemFetchJob *job = 
new ItemFetchJob(item);
 
  266   connect( job, SIGNAL(result(KJob*)), 
this, SLOT(slotFinishView(KJob*)) );
 
  269   info.attachmentName = attachmentName;
 
  271   d->mJobToReceivedInfo[job] = info;
 
  275                                 const ScheduleMessage::Ptr &message )
 
  277   return saveAs( 
find( attachmentName, message ) );
 
  280 void AttachmentHandler::slotFinishSaveAs( KJob *job )
 
  282   ReceivedInfo info = d->mJobToReceivedInfo[job];
 
  283   bool success = 
false;
 
  285   if ( job->error() != 0 ) {
 
  286     ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
 
  287     const Item::List items = fetchJob->items();
 
  288     if ( !items.isEmpty() ) {
 
  290       success = incidence && 
saveAs( info.attachmentName, incidence );
 
  292       kWarning() << Q_FUNC_INFO << 
"No item found";
 
  295     kWarning() << Q_FUNC_INFO << 
"Job error:" << job->errorString();
 
  299   d->mJobToReceivedInfo.remove( job );
 
  302 void AttachmentHandler::slotFinishView( KJob *job )
 
  304   ReceivedInfo info = d->mJobToReceivedInfo[job];
 
  305   bool success = 
false;
 
  307   if ( job->error() != 0 ) {
 
  308     ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
 
  309     const Item::List items = fetchJob->items();
 
  310     if ( !items.isEmpty() ) {
 
  312       success = incidence && 
view( info.attachmentName, incidence );
 
  314       kWarning() << Q_FUNC_INFO << 
"No item found";
 
  317     kWarning() << Q_FUNC_INFO << 
"Job error:" << job->errorString();
 
  320   emit 
viewFinished( info.uid, info.attachmentName, success );
 
  321   d->mJobToReceivedInfo.remove( job );
 
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. 
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. 
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. 
QByteArray fromBase64(const QByteArray &base64)
void viewFinished(const QString &uid, const QString &attachmentName, bool success)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void saveAsFinished(const QString &uid, const QString &attachmentName, bool success)