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

korganizer

koeditorattachments.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of KOrganizer.
00003 
00004   Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005   Copyright (C) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>
00006   Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
00007 
00008   This program is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 2 of the License, or
00011   (at your option) any later version.
00012 
00013   This program is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016   GNU General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License along
00019   with this program; if not, write to the Free Software Foundation, Inc.,
00020   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 
00022   As a special exception, permission is given to link this program
00023   with any edition of Qt, and distribute the resulting executable,
00024   without including the source code for Qt in the source distribution.
00025 */
00026 
00027 #include "koeditorattachments.h"
00028 
00029 #include <kcal/incidence.h>
00030 
00031 #include <libkdepim/kdepimprotocols.h>
00032 #include <libkdepim/kvcarddrag.h>
00033 
00034 #include <kio/job.h>
00035 #include <kio/copyjob.h>
00036 #include <kio/jobclasses.h>
00037 #include <kio/jobuidelegate.h>
00038 #include <kio/netaccess.h>
00039 
00040 #include <k3iconview.h>
00041 #include <klocale.h>
00042 #include <kdebug.h>
00043 #include <kdialog.h>
00044 #include <kglobal.h>
00045 #include <kiconloader.h>
00046 #include <klineedit.h>
00047 #include <kmessagebox.h>
00048 #include <kmimetype.h>
00049 #include <kmenu.h>
00050 #include <kprotocolmanager.h>
00051 #include <krecentdocument.h>
00052 #include <krun.h>
00053 #include <kseparator.h>
00054 #include <kstandarddirs.h>
00055 #include <ktemporaryfile.h>
00056 #include <kurlrequester.h>
00057 #include <krandom.h>
00058 #include <kaction.h>
00059 #include <kstdaction.h>
00060 #include <kactioncollection.h>
00061 
00062 #include <QCheckBox>
00063 #include <QCursor>
00064 #include <QFile>
00065 #include <QFileInfo>
00066 #include <QLabel>
00067 #include <QLayout>
00068 #include <QPushButton>
00069 #include <QRegExp>
00070 #include <QString>
00071 #include <QStringList>
00072 #include <QStyle>
00073 #include <QPixmap>
00074 #include <QGridLayout>
00075 #include <QBoxLayout>
00076 #include <QDropEvent>
00077 #include <QHBoxLayout>
00078 #include <QVBoxLayout>
00079 #include <QDragEnterEvent>
00080 #include <qapplication.h>
00081 #include <qclipboard.h>
00082 
00083 class AttachmentIconItem : public K3IconViewItem
00084 {
00085   public:
00086     AttachmentIconItem( KCal::Attachment *att, Q3IconView *parent )
00087       : K3IconViewItem( parent )
00088     {
00089       if ( att ) {
00090         mAttachment = new KCal::Attachment( *att );
00091       } else {
00092         mAttachment = new KCal::Attachment( QString() );
00093       }
00094       readAttachment();
00095     }
00096     ~AttachmentIconItem() { delete mAttachment; }
00097     KCal::Attachment *attachment() const
00098     {
00099       return mAttachment;
00100     }
00101     const QString uri() const
00102     {
00103       return mAttachment->uri();
00104     }
00105     void setUri( const QString &uri )
00106     {
00107       mAttachment->setUri( uri );
00108       readAttachment();
00109     }
00110     void setData( const QByteArray &data )
00111     {
00112       mAttachment->setDecodedData( data );
00113       readAttachment();
00114     }
00115     const QString mimeType() const
00116     {
00117       return mAttachment->mimeType();
00118     }
00119     void setMimeType( const QString &mime )
00120     {
00121       mAttachment->setMimeType( mime );
00122       readAttachment();
00123     }
00124     const QString label() const
00125     {
00126       return mAttachment->label();
00127     }
00128     void setLabel( const QString &description )
00129     {
00130       mAttachment->setLabel( description );
00131       readAttachment();
00132     }
00133     bool isBinary() const
00134     {
00135       return mAttachment->isBinary();
00136     }
00137     QPixmap icon() const
00138     {
00139       return icon( KMimeType::mimeType( mAttachment->mimeType() ),
00140                    mAttachment->uri(), mAttachment->isBinary() );
00141     }
00142     static QPixmap icon( KMimeType::Ptr mimeType, const QString &uri,
00143                          bool binary = false )
00144     {
00145       QString iconStr = mimeType->iconName( uri );
00146       QStringList overlays;
00147       if ( !uri.isEmpty() && !binary ) {
00148         overlays << "emblem-link";
00149       }
00150 
00151       return KIconLoader::global()->loadIcon( iconStr, KIconLoader::Desktop, 0,
00152                                               KIconLoader::DefaultState,
00153                                               overlays );
00154     }
00155 
00156     void readAttachment()
00157     {
00158       if ( mAttachment->label().isEmpty() ) {
00159         if ( mAttachment->isUri() ) {
00160           setText( mAttachment->uri() );
00161         } else {
00162           setText( i18nc( "@label attachment contains binary data", "[Binary data]" ) );
00163         }
00164       } else {
00165         setText( mAttachment->label() );
00166       }
00167 
00168       setRenameEnabled( true );
00169 
00170       KMimeType::Ptr mimeType;
00171       if ( !mAttachment->mimeType().isEmpty() ) {
00172         mimeType = KMimeType::mimeType( mAttachment->mimeType() );
00173       } else {
00174         if ( mAttachment->isUri() ) {
00175           mimeType = KMimeType::findByUrl( mAttachment->uri() );
00176         } else {
00177           mimeType = KMimeType::findByContent( mAttachment->decodedData() );
00178         }
00179         mAttachment->setMimeType( mimeType->name() );
00180       }
00181 
00182       setPixmap( icon() );
00183     }
00184 
00185   private:
00186     KCal::Attachment *mAttachment;
00187 };
00188 
00189 AttachmentEditDialog::AttachmentEditDialog( AttachmentIconItem *item,
00190                                             QWidget *parent, bool modal )
00191   : KDialog ( parent ), mItem( item ), mURLRequester( 0 )
00192 {
00193   // based loosely on KPropertiesDialog code
00194   QWidget *page = new QWidget(this);
00195   setMainWidget( page );
00196   setCaption( i18nc( "@title", "Properties for %1",
00197                      item->label().isEmpty() ? item->uri() : item->label() ) );
00198   setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
00199   setDefaultButton( KDialog::Ok );
00200   setModal( modal );
00201   QVBoxLayout *vbl = new QVBoxLayout( page );
00202   vbl->setSpacing( KDialog::spacingHint() );
00203   vbl->setMargin( 0 );
00204   QGridLayout *grid = new QGridLayout();
00205   grid->setColumnStretch( 0, 0 );
00206   grid->setColumnStretch( 1, 0 );
00207   grid->setColumnStretch( 2, 1 );
00208   grid->addItem( new QSpacerItem( KDialog::spacingHint(), 0 ), 0, 1 );
00209   vbl->addLayout( grid );
00210 
00211   mIcon = new QLabel( page );
00212   int bsize = 66 + 2 * mIcon->style()->pixelMetric( QStyle::PM_ButtonMargin );
00213   mIcon->setFixedSize( bsize, bsize );
00214   mIcon->setPixmap( item->icon() );
00215   grid->addWidget( mIcon, 0, 0, Qt::AlignLeft );
00216 
00217   mLabelEdit = new KLineEdit( page );
00218   mLabelEdit->setText( item->label().isEmpty() ? item->uri() : item->label() );
00219   mLabelEdit->setClickMessage( i18nc( "@label", "Attachment name" ) );
00220   grid->addWidget( mLabelEdit, 0, 2 );
00221 
00222   KSeparator *sep = new KSeparator( Qt::Horizontal, page );
00223   grid->addWidget( sep, 1, 0, 1, 3 );
00224 
00225   QLabel *label = new QLabel( i18nc( "@label", "Type:" ), page );
00226   grid->addWidget( label, 2, 0 );
00227   QString typecomment = item->mimeType().isEmpty() ?
00228                         i18nc( "@label unknown mimetype", "Unknown" ) :
00229                         KMimeType::mimeType( item->mimeType() )->comment();
00230   mTypeLabel = new QLabel( typecomment, page );
00231   grid->addWidget( mTypeLabel, 2, 2 );
00232   mMimeType = KMimeType::mimeType( item->mimeType() );
00233 
00234   mInline = new QCheckBox( i18nc( "@option:check", "Store attachment inline" ), page );
00235   grid->addWidget( mInline, 3, 0, 1, 3 );
00236   mInline->setChecked( item->isBinary() );
00237 
00238   if ( item->attachment()->isUri() ) {
00239     label = new QLabel( i18nc( "@label", "Location:" ), page );
00240     grid->addWidget( label, 4, 0 );
00241     mURLRequester = new KUrlRequester( item->uri(), page );
00242     grid->addWidget( mURLRequester, 4, 2 );
00243     connect( mURLRequester, SIGNAL(urlSelected(const KUrl &)),
00244              SLOT(urlChanged(const KUrl &)) );
00245   } else {
00246     grid->addWidget( new QLabel( i18nc( "@label", "Size:" ), page ), 4, 0 );
00247     grid->addWidget( new QLabel( QString::fromLatin1( "%1 (%2)" ).
00248                                  arg( KIO::convertSize( item->attachment()->size() ) ).
00249                                  arg( KGlobal::locale()->formatNumber(
00250                                         item->attachment()->size(), 0 ) ), page ), 4, 2 );
00251   }
00252   vbl->addStretch( 10 );
00253   connect( this, SIGNAL(applyClicked()), this, SLOT(slotApply()) );
00254 }
00255 
00256 void AttachmentEditDialog::slotApply()
00257 {
00258   if ( mLabelEdit->text().isEmpty() ) {
00259     if ( mURLRequester->url().isLocalFile() ) {
00260       mItem->setLabel( mURLRequester->url().fileName() );
00261     } else {
00262       mItem->setLabel( mURLRequester->url().url() );
00263     }
00264   } else {
00265     mItem->setLabel( mLabelEdit->text() );
00266   }
00267   if ( mItem->label().isEmpty() ) {
00268     mItem->setLabel( i18nc( "@label", "New attachment" ) );
00269   }
00270   mItem->setMimeType( mMimeType->name() );
00271   if ( mURLRequester ) {
00272     if ( mInline->isChecked() ) {
00273       QString tmpFile;
00274       if ( KIO::NetAccess::download( mURLRequester->url(), tmpFile, this ) ) {
00275         QFile f( tmpFile );
00276         if ( !f.open( QIODevice::ReadOnly ) ) {
00277           return;
00278         }
00279         QByteArray data = f.readAll();
00280         f.close();
00281         mItem->setData( data );
00282       }
00283       KIO::NetAccess::removeTempFile( tmpFile );
00284     } else {
00285       mItem->setUri( mURLRequester->url().url() );
00286     }
00287   }
00288 }
00289 
00290 void AttachmentEditDialog::accept()
00291 {
00292   slotApply();
00293   KDialog::accept();
00294 }
00295 
00296 void AttachmentEditDialog::urlChanged( const KUrl &url )
00297 {
00298   mMimeType = KMimeType::findByUrl( url );
00299   mTypeLabel->setText( mMimeType->comment() );
00300   mIcon->setPixmap( AttachmentIconItem::icon( mMimeType, url.path() ) );
00301 }
00302 
00303 class AttachmentIconView : public K3IconView
00304 {
00305   friend class KOEditorAttachments;
00306   public:
00307     AttachmentIconView( QWidget *parent ) : K3IconView( parent )
00308     {
00309       setAcceptDrops( true );
00310       setSelectionMode( Q3IconView::Extended );
00311       setMode( K3IconView::Select );
00312       setItemTextPos( Q3IconView::Right );
00313       setArrangement( Q3IconView::LeftToRight );
00314       setMaxItemWidth( qMax( maxItemWidth(), 250 ) );
00315     }
00316 
00317     KUrl tempFileForAttachment( KCal::Attachment *attachment )
00318     {
00319       if ( mTempFiles.contains( attachment ) ) {
00320         return mTempFiles.value( attachment );
00321       }
00322       KTemporaryFile *file = new KTemporaryFile();
00323       file->setParent( this );
00324       file->setSuffix(
00325         QString( KMimeType::mimeType( attachment->mimeType() )->patterns().first() ).remove( '*' ) );
00326       file->setAutoRemove( true );
00327       file->open();
00328       // read-only not to give the idea that it could be written to
00329       file->setPermissions( QFile::ReadUser );
00330       file->write( QByteArray::fromBase64( attachment->data() ) );
00331       mTempFiles.insert( attachment, file->fileName() );
00332       file->close();
00333       return mTempFiles.value( attachment );
00334     }
00335 
00336   protected:
00337 
00338     QMimeData *mimeData()
00339     {
00340       // create a list of the URL:s that we want to drag
00341       KUrl::List urls;
00342       QStringList labels;
00343       for ( Q3IconViewItem *it = firstItem(); it; it = it->nextItem() ) {
00344         if ( it->isSelected() ) {
00345           AttachmentIconItem *item = static_cast<AttachmentIconItem *>( it );
00346           if ( item->isBinary() ) {
00347             urls.append( tempFileForAttachment( item->attachment() ) );
00348           } else {
00349             urls.append( item->uri() );
00350           }
00351           labels.append( KUrl::toPercentEncoding( item->label() ) );
00352         }
00353       }
00354       if ( selectionMode() == Q3IconView::NoSelection ) {
00355         AttachmentIconItem *item = static_cast<AttachmentIconItem *>( currentItem() );
00356         if ( item ) {
00357           urls.append( item->uri() );
00358           labels.append( KUrl::toPercentEncoding( item->label() ) );
00359         }
00360       }
00361 
00362       QMap<QString, QString> metadata;
00363       metadata["labels"] = labels.join( ":" );
00364 
00365       QMimeData *mimeData = new QMimeData;
00366       urls.populateMimeData( mimeData, metadata );
00367       return mimeData;
00368     }
00369 
00370 #ifdef __GNUC__
00371 #warning Port to QDrag instead of Q3DragObject once we port the view from K3IconView
00372 #endif
00373     virtual Q3DragObject *dragObject ()
00374     {
00375       int count = 0;
00376       for ( Q3IconViewItem *it = firstItem(); it; it = it->nextItem() ) {
00377         if ( it->isSelected() ) {
00378           ++count;
00379         }
00380       }
00381 
00382       QPixmap pixmap;
00383       if ( count > 1 ) {
00384         pixmap = KIconLoader::global()->loadIcon( "mail-attachment", KIconLoader::Desktop );
00385       }
00386       if ( pixmap.isNull() ) {
00387         pixmap = static_cast<AttachmentIconItem *>( currentItem() )->icon();
00388       }
00389 
00390       QPoint hotspot( pixmap.width() / 2, pixmap.height() / 2 );
00391 
00392       QDrag *drag = new QDrag( this );
00393       drag->setMimeData( mimeData() );
00394 
00395       drag->setPixmap( pixmap );
00396       drag->setHotSpot( hotspot );
00397       drag->exec( Qt::CopyAction );
00398       return 0;
00399     }
00400 
00401   private:
00402     QHash<KCal::Attachment*, KUrl> mTempFiles;
00403 };
00404 
00405 KOEditorAttachments::KOEditorAttachments( int spacing, QWidget *parent )
00406   : QWidget( parent )
00407 {
00408   QBoxLayout *topLayout = new QHBoxLayout( this );
00409   topLayout->setSpacing( spacing );
00410 
00411   QLabel *label = new QLabel( i18nc( "@label", "Attachments:" ), this );
00412   topLayout->addWidget( label );
00413 
00414   mAttachments = new AttachmentIconView( this );
00415   mAttachments->setWhatsThis( i18nc( "@info",
00416                                      "Displays items (files, mail, etc.) that "
00417                                      "have been associated with this event or to-do." ) );
00418   mAttachments->setItemsMovable( false );
00419   mAttachments->setSelectionMode( Q3IconView::Extended );
00420   topLayout->addWidget( mAttachments );
00421   connect( mAttachments, SIGNAL(returnPressed(Q3IconViewItem *)),
00422            SLOT(showAttachment(Q3IconViewItem *)) );
00423   connect( mAttachments, SIGNAL(doubleClicked(Q3IconViewItem *)),
00424            SLOT(showAttachment(Q3IconViewItem *)) );
00425   connect( mAttachments, SIGNAL(itemRenamed(Q3IconViewItem *,const QString &)),
00426            SLOT(slotItemRenamed(Q3IconViewItem *,const QString &)) );
00427   connect( mAttachments, SIGNAL(dropped(QDropEvent *,const Q3ValueList<Q3IconDragItem> &)),
00428            SLOT(dropped(QDropEvent *,const Q3ValueList<Q3IconDragItem> &)) );
00429   connect( mAttachments, SIGNAL(selectionChanged()),
00430            SLOT(selectionChanged()) );
00431   connect( mAttachments, SIGNAL(contextMenuRequested(Q3IconViewItem *,const QPoint &)),
00432            SLOT(contextMenu(Q3IconViewItem *,const QPoint &)) );
00433 
00434   QPushButton *addButton = new QPushButton( this );
00435   addButton->setIcon( KIcon( "list-add" ) );
00436   addButton->setToolTip( i18nc( "@action:button", "&Add..." ) );
00437   addButton->setWhatsThis( i18nc( "@info",
00438                                   "Shows a dialog used to select an attachment "
00439                                   "to add to this event or to-do as link or as "
00440                                   "inline data." ) );
00441   topLayout->addWidget( addButton );
00442   connect( addButton, SIGNAL(clicked()), SLOT(slotAdd()) );
00443 
00444   mRemoveBtn = new QPushButton( this );
00445   mRemoveBtn->setIcon( KIcon( "list-remove" ) );
00446   mRemoveBtn->setToolTip( i18nc( "@action:button", "&Remove" ) );
00447   mRemoveBtn->setWhatsThis( i18nc( "@info",
00448                                    "Removes the attachment selected in the "
00449                                    "list above from this event or to-do." ) );
00450   topLayout->addWidget( mRemoveBtn );
00451   connect( mRemoveBtn, SIGNAL(clicked()), SLOT(slotRemove()) );
00452 
00453   KActionCollection *ac = new KActionCollection( this );
00454   ac->addAssociatedWidget( this );
00455 
00456   mPopupMenu = new KMenu( this );
00457 
00458   mOpenAction = new KAction( i18nc( "@action:inmenu open the attachment in a viewer",
00459                                     "&Open" ), this );
00460   connect( mOpenAction, SIGNAL(triggered(bool)), this, SLOT(slotShow()) );
00461   ac->addAction( "view", mOpenAction );
00462   mPopupMenu->addAction( mOpenAction );
00463   mPopupMenu->addSeparator();
00464 
00465   mCopyAction = KStandardAction::copy( this, SLOT(slotCopy()), ac );
00466   mPopupMenu->addAction( mCopyAction );
00467   mCutAction = KStandardAction::cut( this, SLOT(slotCut()), ac );
00468   mPopupMenu->addAction( mCutAction );
00469   KAction *action = KStandardAction::paste( this, SLOT(slotPaste()), ac );
00470   mPopupMenu->addAction( action );
00471   mPopupMenu->addSeparator();
00472 
00473   mDeleteAction = new KAction( i18nc( "@action:inmenu remove the attachment",
00474                                       "&Delete" ), this );
00475   connect( mDeleteAction, SIGNAL(triggered(bool)), this, SLOT(slotRemove()) );
00476   ac->addAction( "remove", mDeleteAction );
00477   mPopupMenu->addAction( mDeleteAction );
00478   mPopupMenu->addSeparator();
00479 
00480   mEditAction = new KAction( i18nc( "@action:inmenu show a dialog used to edit the attachment",
00481                                     "&Properties..." ), this );
00482   connect( mEditAction, SIGNAL(triggered(bool)), this, SLOT(slotEdit()) );
00483   ac->addAction( "edit", mEditAction );
00484   mPopupMenu->addAction( mEditAction );
00485 
00486   selectionChanged();
00487   setAcceptDrops( true );
00488 }
00489 
00490 KOEditorAttachments::~KOEditorAttachments()
00491 {
00492 }
00493 
00494 bool KOEditorAttachments::hasAttachments()
00495 {
00496   return mAttachments->count() > 0;
00497 }
00498 
00499 void KOEditorAttachments::dragEnterEvent( QDragEnterEvent *event )
00500 {
00501   const QMimeData *md = event->mimeData();
00502   event->setAccepted( KUrl::List::canDecode( md ) || md->hasText() );
00503 }
00504 
00505 void KOEditorAttachments::handlePasteOrDrop( const QMimeData *mimeData )
00506 {
00507   KUrl::List urls;
00508   bool probablyWeHaveUris = false;
00509   bool weCanCopy = true;
00510   QStringList labels;
00511 
00512   if ( KPIM::KVCardDrag::canDecode( mimeData ) ) {
00513     KABC::Addressee::List addressees;
00514     KPIM::KVCardDrag::fromMimeData( mimeData, addressees );
00515     for ( KABC::Addressee::List::ConstIterator it = addressees.constBegin();
00516           it != addressees.constEnd(); ++it ) {
00517       urls.append( KDEPIMPROTOCOL_CONTACT + ( *it ).uid() );
00518       // there is some weirdness about realName(), hence fromUtf8
00519       labels.append( QString::fromUtf8( ( *it ).realName().toLatin1() ) );
00520     }
00521     probablyWeHaveUris = true;
00522   } else if ( KUrl::List::canDecode( mimeData ) ) {
00523     QMap<QString,QString> metadata;
00524 
00525     urls = KUrl::List::fromMimeData( mimeData, &metadata );
00526     probablyWeHaveUris = true;
00527     labels = metadata["labels"].split( ":", QString::SkipEmptyParts );
00528     for ( QStringList::Iterator it = labels.begin(); it != labels.end(); ++it ) {
00529       *it = KUrl::fromPercentEncoding( (*it).toLatin1() );
00530     }
00531   } else if ( mimeData->hasText() ) {
00532     QString text = mimeData->text();
00533     QStringList lst = text.split( '\n', QString::SkipEmptyParts );
00534     for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00535       urls.append( *it );
00536     }
00537     probablyWeHaveUris = true;
00538   }
00539 
00540   KMenu menu( this );
00541   QAction *linkAction = 0, *cancelAction;
00542   if ( probablyWeHaveUris ) {
00543     linkAction = menu.addAction( i18nc( "@action:inmenu", "&Link here" ) );
00544     // we need to check if we can reasonably expect to copy the objects
00545     for ( KUrl::List::ConstIterator it = urls.constBegin(); it != urls.constEnd(); ++it ) {
00546       if ( !( weCanCopy = KProtocolManager::supportsReading( *it ) ) ) {
00547         break; // either we can copy them all, or no copying at all
00548       }
00549     }
00550     if ( weCanCopy ) {
00551       menu.addAction( i18nc( "@action:inmenu", "&Copy here" ) );
00552     }
00553   } else {
00554     menu.addAction( i18nc( "@action:inmenu", "&Copy here" ) );
00555   }
00556 
00557   menu.addSeparator();
00558   cancelAction = menu.addAction( i18nc( "@action:inmenu", "C&ancel" ) );
00559 
00560   QAction *ret = menu.exec( QCursor::pos() );
00561   if ( linkAction == ret ) {
00562     QStringList::ConstIterator jt = labels.constBegin();
00563     for ( KUrl::List::ConstIterator it = urls.constBegin();
00564           it != urls.constEnd(); ++it ) {
00565       addAttachment( (*it).url(), QString(), ( jt == labels.constEnd() ?
00566                                                QString() : *( jt++ ) ) );
00567     }
00568   } else if ( cancelAction != ret ) {
00569     if ( probablyWeHaveUris ) {
00570       for ( KUrl::List::ConstIterator it = urls.constBegin();
00571             it != urls.constEnd(); ++it ) {
00572         KIO::Job *job = KIO::storedGet( *it );
00573         connect( job, SIGNAL(result(KJob *)), SLOT(downloadComplete(KJob *)) );
00574       }
00575     } else { // we take anything
00576       addAttachment( mimeData->data( mimeData->formats().first() ), mimeData->formats().first(),
00577                      KMimeType::mimeType( mimeData->formats().first() )->name() );
00578     }
00579   }
00580 }
00581 
00582 void KOEditorAttachments::dropEvent( QDropEvent *event )
00583 {
00584   handlePasteOrDrop( event->mimeData() );
00585 }
00586 
00587 void KOEditorAttachments::downloadComplete( KJob *job )
00588 {
00589   if ( job->error() ) {
00590     static_cast<KIO::Job*>(job)->ui()->setWindow( this );
00591     static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
00592   } else {
00593     addAttachment( static_cast<KIO::StoredTransferJob *>( job )->data(),
00594                    QString(),
00595                    static_cast<KIO::SimpleJob *>( job )->url().fileName() );
00596   }
00597 }
00598 
00599 void KOEditorAttachments::dropped ( QDropEvent *e, const Q3ValueList<Q3IconDragItem> &lst )
00600 {
00601   Q_UNUSED( lst );
00602   dropEvent( e );
00603 }
00604 
00605 void KOEditorAttachments::showAttachment( Q3IconViewItem *item )
00606 {
00607   AttachmentIconItem *attitem = static_cast<AttachmentIconItem*>( item );
00608   if ( !attitem || !attitem->attachment() ) {
00609     return;
00610   }
00611 
00612   KCal::Attachment *att = attitem->attachment();
00613   if ( att->isUri() ) {
00614     emit openURL( att->uri() );
00615   } else {
00616     KRun::runUrl( mAttachments->tempFileForAttachment( att ), att->mimeType(), 0, true );
00617   }
00618 }
00619 
00620 void KOEditorAttachments::slotAdd()
00621 {
00622   AttachmentIconItem *item = new AttachmentIconItem( 0, mAttachments );
00623   AttachmentEditDialog *dlg = new AttachmentEditDialog( item, mAttachments );
00624 
00625   dlg->setCaption( i18nc( "@title", "Add Attachment" ) );
00626   dlg->exec();
00627 
00628   if ( dlg->result() == KDialog::Rejected ) {
00629     delete dlg; // delete it first, as it hold item
00630     delete item;
00631   }
00632 }
00633 
00634 void KOEditorAttachments::slotEdit()
00635 {
00636   for ( Q3IconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
00637     if ( item->isSelected() ) {
00638       AttachmentIconItem *attitem = static_cast<AttachmentIconItem*>( item );
00639       if ( !attitem || !attitem->attachment() ) {
00640         return;
00641       }
00642 
00643       AttachmentEditDialog *dialog = new AttachmentEditDialog( attitem, mAttachments, false );
00644       dialog->setModal( false );
00645       connect( dialog, SIGNAL(hidden()), dialog, SLOT(slotDelayedDestruct()) );
00646       dialog->show();
00647     }
00648   }
00649 }
00650 
00651 void KOEditorAttachments::slotRemove()
00652 {
00653   QList<Q3IconViewItem *> toDelete;
00654   for ( Q3IconViewItem *it = mAttachments->firstItem(); it; it = it->nextItem() ) {
00655     if ( it->isSelected() ) {
00656       AttachmentIconItem *item = static_cast<AttachmentIconItem *>( it );
00657 
00658       if ( !item ) {
00659         continue;
00660       }
00661 
00662       if ( KMessageBox::warningContinueCancel(
00663              this,
00664              i18nc( "@info",
00665                     "The item labeled \"%1\" will be permanently deleted.", item->label() ),
00666              i18nc( "@title:window", "KOrganizer Confirmation" ),
00667              KStandardGuiItem::del() ) == KMessageBox::Continue ) {
00668         toDelete.append( it );
00669       }
00670     }
00671   }
00672 
00673   for ( QList<Q3IconViewItem *>::ConstIterator it = toDelete.constBegin();
00674         it != toDelete.constEnd(); ++it ) {
00675     delete *it;
00676   }
00677 }
00678 
00679 void KOEditorAttachments::slotShow()
00680 {
00681   for ( Q3IconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
00682     if ( item->isSelected() ) {
00683       showAttachment( item );
00684     }
00685   }
00686 }
00687 
00688 void KOEditorAttachments::setDefaults()
00689 {
00690   mAttachments->clear();
00691 }
00692 
00693 void KOEditorAttachments::addAttachment( const QString &uri,
00694                                          const QString &mimeType,
00695                                          const QString &label,
00696                                          bool binary )
00697 {
00698   if ( !binary ) {
00699     AttachmentIconItem *item = new AttachmentIconItem( 0, mAttachments );
00700     item->setUri( uri );
00701     item->setLabel( label );
00702     if ( mimeType.isEmpty() ) {
00703       if ( uri.startsWith( KDEPIMPROTOCOL_CONTACT ) ) {
00704         item->setMimeType( "text/directory" );
00705       } else if ( uri.startsWith( KDEPIMPROTOCOL_EMAIL ) ) {
00706         item->setMimeType( "message/rfc822" );
00707       } else if ( uri.startsWith( KDEPIMPROTOCOL_INCIDENCE ) ) {
00708         item->setMimeType( "text/calendar" );
00709       } else if ( uri.startsWith( KDEPIMPROTOCOL_NEWSARTICLE ) ) {
00710         item->setMimeType( "message/news" );
00711       } else {
00712         item->setMimeType( KMimeType::findByUrl( uri )->name() );
00713       }
00714     } else {
00715       QString tmpFile;
00716       if ( KIO::NetAccess::download( uri, tmpFile, this ) ) {
00717         QFile f( tmpFile );
00718         if ( !f.open( QIODevice::ReadOnly ) ) {
00719           return;
00720         }
00721         const QByteArray data = f.readAll();
00722         f.close();
00723         addAttachment( data, mimeType, label );
00724       }
00725       KIO::NetAccess::removeTempFile( tmpFile );
00726     }
00727   } else {
00728   }
00729 }
00730 
00731 void KOEditorAttachments::addAttachment( const QByteArray &data,
00732                                          const QString &mimeType,
00733                                          const QString &label )
00734 {
00735   AttachmentIconItem *item = new AttachmentIconItem( 0, mAttachments );
00736   item->setData( data );
00737   item->setLabel( label );
00738   if ( mimeType.isEmpty() ) {
00739     item->setMimeType( KMimeType::findByContent( data )->name() );
00740   } else {
00741     item->setMimeType( mimeType );
00742   }
00743 }
00744 
00745 void KOEditorAttachments::addAttachment( KCal::Attachment *attachment )
00746 {
00747   new AttachmentIconItem( attachment, mAttachments );
00748 }
00749 
00750 void KOEditorAttachments::readIncidence( KCal::Incidence *i )
00751 {
00752   mAttachments->clear();
00753 
00754   KCal::Attachment::List attachments = i->attachments();
00755   KCal::Attachment::List::ConstIterator it;
00756   for ( it = attachments.begin(); it != attachments.end(); ++it ) {
00757     addAttachment( (*it) );
00758   }
00759 
00760   mUid = i->uid();
00761 
00762   if ( mAttachments->count() > 0 ) {
00763     QTimer::singleShot( 0, mAttachments, SLOT(arrangeItemsInGrid()) );
00764   }
00765 }
00766 
00767 void KOEditorAttachments::writeIncidence( KCal::Incidence *i )
00768 {
00769   i->clearAttachments();
00770 
00771   Q3IconViewItem *item;
00772   AttachmentIconItem *attitem;
00773   for ( item = mAttachments->firstItem(); item; item = item->nextItem() ) {
00774     attitem = static_cast<AttachmentIconItem*>(item);
00775     if ( attitem ) {
00776       i->addAttachment( new KCal::Attachment( *( attitem->attachment() ) ) );
00777     }
00778   }
00779 }
00780 
00781 void KOEditorAttachments::slotItemRenamed ( Q3IconViewItem *item, const QString &text )
00782 {
00783   static_cast<AttachmentIconItem *>( item )->setLabel( text );
00784 }
00785 
00786 void KOEditorAttachments::applyChanges()
00787 {
00788 }
00789 
00790 void KOEditorAttachments::slotCopy()
00791 {
00792   QApplication::clipboard()->setMimeData( mAttachments->mimeData(), QClipboard::Clipboard );
00793 }
00794 
00795 void KOEditorAttachments::slotCut()
00796 {
00797     slotCopy();
00798     slotRemove();
00799 }
00800 
00801 void KOEditorAttachments::slotPaste()
00802 {
00803     handlePasteOrDrop( QApplication::clipboard()->mimeData() );
00804 }
00805 
00806 void KOEditorAttachments::selectionChanged()
00807 {
00808   bool selected = false;
00809   for ( Q3IconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
00810     if ( item->isSelected() ) {
00811       selected = true;
00812       break;
00813     }
00814   }
00815   mRemoveBtn->setEnabled( selected );
00816 }
00817 
00818 void KOEditorAttachments::contextMenu( Q3IconViewItem *item, const QPoint &pos )
00819 {
00820   const bool enable = item != 0;
00821   mOpenAction->setEnabled( enable );
00822   mCopyAction->setEnabled( enable );
00823   mCutAction->setEnabled( enable );
00824   mDeleteAction->setEnabled( enable );
00825   mEditAction->setEnabled( enable );
00826   mPopupMenu->exec( pos );
00827 }
00828 
00829 #include "koeditorattachments.moc"

korganizer

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

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal