00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "koeditorattachments.h"
00027
00028 #include <libkcal/incidence.h>
00029 #include <libkdepim/kpimurlrequesterdlg.h>
00030
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033 #include <kmdcodec.h>
00034 #include <kmessagebox.h>
00035 #include <klistview.h>
00036 #include <krun.h>
00037 #include <kurldrag.h>
00038 #include <ktempfile.h>
00039 #include <kio/netaccess.h>
00040
00041 #include <qfile.h>
00042 #include <qlayout.h>
00043 #include <qlistview.h>
00044 #include <qpushbutton.h>
00045 #include <qdragobject.h>
00046 #include <qwhatsthis.h>
00047
00048 class AttachmentListItem : public KListViewItem
00049 {
00050 public:
00051 AttachmentListItem( KCal::Attachment*att, QListView *parent ) :
00052 KListViewItem( parent )
00053 {
00054 if ( att ) {
00055 mAttachment = new KCal::Attachment( *att );
00056 } else {
00057 mAttachment = new KCal::Attachment( QString::null );
00058 }
00059 readAttachment();
00060 }
00061 ~AttachmentListItem() { delete mAttachment; }
00062 KCal::Attachment *attachment() const { return mAttachment; }
00063
00064 void setUri( const QString &uri )
00065 {
00066 mAttachment->setUri( uri );
00067 readAttachment();
00068 }
00069 void setData( const char *base64 )
00070 {
00071 mAttachment->setData( base64 );
00072 readAttachment();
00073 }
00074 void setMimeType( const QString &mime )
00075 {
00076 mAttachment->setMimeType( mime );
00077 readAttachment();
00078 }
00079 void setLabel( const QString &label )
00080 {
00081 mAttachment->setLabel( label );
00082 readAttachment();
00083 }
00084
00085 void readAttachment()
00086 {
00087 if ( mAttachment->isUri() )
00088 setText( 0, mAttachment->uri() );
00089 else {
00090 if ( mAttachment->label().isEmpty() )
00091 setText( 0, i18n("[Binary data]") );
00092 else
00093 setText( 0, mAttachment->label() );
00094 }
00095 setText( 1, mAttachment->mimeType() );
00096 }
00097
00098 private:
00099 KCal::Attachment *mAttachment;
00100 };
00101
00102 KOEditorAttachments::KOEditorAttachments( int spacing, QWidget *parent,
00103 const char *name )
00104 : QWidget( parent, name )
00105 {
00106 QBoxLayout *topLayout = new QVBoxLayout( this );
00107 topLayout->setSpacing( spacing );
00108
00109 mAttachments = new KListView( this );
00110 QWhatsThis::add( mAttachments,
00111 i18n("Displays a list of current items (files, mail, etc.) "
00112 "that have been associated with this event or to-do. "
00113 "The URI column displays the location of the file.") );
00114 mAttachments->addColumn( i18n("Label / URI") );
00115 mAttachments->addColumn( i18n("MIME Type") );
00116 topLayout->addWidget( mAttachments );
00117 connect( mAttachments, SIGNAL( doubleClicked( QListViewItem * ) ),
00118 SLOT( showAttachment( QListViewItem * ) ) );
00119
00120 QBoxLayout *buttonLayout = new QHBoxLayout( topLayout );
00121
00122 QPushButton *button = new QPushButton( i18n("&Add URI..."), this );
00123 QWhatsThis::add( button,
00124 i18n("Shows a dialog used to select an attachment "
00125 "to add to this event or to-do as link.") );
00126 buttonLayout->addWidget( button );
00127 connect( button, SIGNAL( clicked() ), SLOT( slotAdd() ) );
00128
00129 button = new QPushButton( i18n("&Add File..."), this );
00130 QWhatsThis::add( button,
00131 i18n("Shows a dialog used to select an attachment "
00132 "to add to this event or to-do as link as inline data.") );
00133 buttonLayout->addWidget( button );
00134 connect( button, SIGNAL( clicked() ), SLOT( slotAddData() ) );
00135
00136 button = new QPushButton( i18n("&Edit..."), this );
00137 QWhatsThis::add( button,
00138 i18n("Shows a dialog used to edit the attachment "
00139 "currently selected in the list above.") );
00140 buttonLayout->addWidget( button );
00141 connect( button, SIGNAL( clicked() ), SLOT( slotEdit() ) );
00142
00143 button = new QPushButton( i18n("&Remove"), this );
00144 QWhatsThis::add( button,
00145 i18n("Removes the attachment selected in the list above "
00146 "from this event or to-do.") );
00147 buttonLayout->addWidget( button );
00148 connect( button, SIGNAL( clicked() ), SLOT( slotRemove() ) );
00149
00150 button = new QPushButton( i18n("&Show"), this );
00151 QWhatsThis::add( button,
00152 i18n("Opens the attachment selected in the list above "
00153 "in the viewer that is associated with it in your "
00154 "KDE preferences.") );
00155 buttonLayout->addWidget( button );
00156 connect( button, SIGNAL( clicked() ), SLOT( slotShow() ) );
00157
00158 setAcceptDrops( TRUE );
00159 }
00160
00161 KOEditorAttachments::~KOEditorAttachments()
00162 {
00163 }
00164
00165 bool KOEditorAttachments::hasAttachments()
00166 {
00167 return mAttachments->childCount() > 0;
00168 }
00169
00170 void KOEditorAttachments::dragEnterEvent( QDragEnterEvent* event ) {
00171 event->accept( KURLDrag::canDecode( event ) | QTextDrag::canDecode( event ) );
00172 }
00173
00174 void KOEditorAttachments::dropEvent( QDropEvent* event ) {
00175 KURL::List urls;
00176 QString text;
00177 if ( KURLDrag::decode( event, urls ) ) {
00178 for ( KURL::List::ConstIterator it = urls.begin(); it != urls.end(); ++it ) {
00179 addAttachment( (*it).url() );
00180 }
00181 } else if ( QTextDrag::decode( event, text ) ) {
00182 QStringList lst = QStringList::split( '\n', text );
00183 for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00184 addAttachment( (*it) );
00185 }
00186 }
00187
00188 }
00189
00190 void KOEditorAttachments::showAttachment( QListViewItem *item )
00191 {
00192 AttachmentListItem *attitem = static_cast<AttachmentListItem*>(item);
00193 if ( !attitem || !attitem->attachment() ) return;
00194
00195 KCal::Attachment *att = attitem->attachment();
00196 if ( att->isUri() ) {
00197 emit openURL( att->uri() );
00198 } else {
00199 KTempFile f;
00200 if ( !f.file() )
00201 return;
00202 QCString decoded = KCodecs::base64Decode( QCString( att->data() ) );
00203 f.file()->writeBlock( decoded.data(), decoded.length() );
00204 f.file()->close();
00205 KRun::runURL( f.name(), att->mimeType(), true, false );
00206 }
00207 }
00208
00209
00210
00211 void KOEditorAttachments::slotAdd()
00212 {
00213 KURL uri = KPimURLRequesterDlg::getURL( QString::null, i18n(
00214 "URL (e.g. a web page) or file to be attached (only "
00215 "the link will be attached, not the file itself):"), this,
00216 i18n("Add Attachment") );
00217 if ( !uri.isEmpty() ) {
00218 addAttachment( uri );
00219 }
00220 }
00221
00222 void KOEditorAttachments::slotAddData()
00223 {
00224 KURL uri = KPimURLRequesterDlg::getURL( QString::null, i18n(
00225 "File to be attached:"), this, i18n("Add Attachment") );
00226 if ( !uri.isEmpty() ) {
00227 addAttachment( uri, QString::null, false );
00228 }
00229 }
00230
00231 void KOEditorAttachments::slotEdit()
00232 {
00233 QListViewItem *item = mAttachments->currentItem();
00234 AttachmentListItem *attitem = static_cast<AttachmentListItem*>(item);
00235 if ( !attitem || !attitem->attachment() ) return;
00236
00237 KCal::Attachment *att = attitem->attachment();
00238 if ( att->isUri() ) {
00239 KURL uri = KPimURLRequesterDlg::getURL( att->uri(), i18n(
00240 "URL (e.g. a web page) or file to be attached (only "
00241 "the link will be attached, not the file itself):"), this,
00242 i18n("Edit Attachment") );
00243
00244 if ( !uri.isEmpty() )
00245 attitem->setUri( uri.url() );
00246 } else {
00247 KURL uri = KPimURLRequesterDlg::getURL( QString::null, i18n(
00248 "File to be attached:"), this, i18n("Add Attachment") );
00249 if ( !uri.isEmpty() ) {
00250 QString tmpFile;
00251 if ( KIO::NetAccess::download( uri, tmpFile, this ) ) {
00252 QFile f( tmpFile );
00253 if ( !f.open( IO_ReadOnly ) )
00254 return;
00255 QByteArray data = f.readAll();
00256 f.close();
00257 attitem->setData( KCodecs::base64Encode( data ) );
00258 attitem->setMimeType( KIO::NetAccess::mimetype( uri, this ) );
00259 QString label = uri.fileName();
00260 if ( label.isEmpty() )
00261 label = uri.prettyURL();
00262 attitem->setLabel( label );
00263 KIO::NetAccess::removeTempFile( tmpFile );
00264 }
00265 }
00266 }
00267 }
00268
00269 void KOEditorAttachments::slotRemove()
00270 {
00271 QListViewItem *item = mAttachments->currentItem();
00272 if ( !item ) return;
00273
00274 if ( KMessageBox::warningContinueCancel(this,
00275 i18n("This item will be permanently deleted."),
00276 i18n("KOrganizer Confirmation"),KStdGuiItem::del()) == KMessageBox::Continue )
00277 delete item;
00278 }
00279
00280 void KOEditorAttachments::slotShow()
00281 {
00282 showAttachment( mAttachments->currentItem() );
00283 }
00284
00285 void KOEditorAttachments::setDefaults()
00286 {
00287 mAttachments->clear();
00288 }
00289
00290 void KOEditorAttachments::addAttachment( const KURL &uri,
00291 const QString &mimeType, bool asUri )
00292 {
00293 AttachmentListItem *item = new AttachmentListItem( 0, mAttachments );
00294 if ( asUri ) {
00295 item->setUri( uri.url() );
00296 if ( !mimeType.isEmpty() ) item->setMimeType( mimeType );
00297 } else {
00298 QString tmpFile;
00299 if ( KIO::NetAccess::download( uri, tmpFile, this ) ) {
00300 QFile f( tmpFile );
00301 if ( !f.open( IO_ReadOnly ) )
00302 return;
00303 QByteArray data = f.readAll();
00304 f.close();
00305 item->setData( KCodecs::base64Encode( data ) );
00306 if ( !mimeType.isEmpty() )
00307 item->setMimeType( mimeType );
00308 else
00309 item->setMimeType( KIO::NetAccess::mimetype( uri, this ) );
00310 QString label = uri.fileName();
00311 if ( label.isEmpty() )
00312 label = uri.prettyURL();
00313 item->setLabel( label );
00314 KIO::NetAccess::removeTempFile( tmpFile );
00315 }
00316 }
00317 }
00318
00319
00320 void KOEditorAttachments::addAttachment( KCal::Attachment *attachment )
00321 {
00322 new AttachmentListItem( attachment, mAttachments );
00323 }
00324
00325 void KOEditorAttachments::readIncidence( KCal::Incidence *i )
00326 {
00327 mAttachments->clear();
00328
00329 KCal::Attachment::List attachments = i->attachments();
00330 KCal::Attachment::List::ConstIterator it;
00331 for( it = attachments.begin(); it != attachments.end(); ++it ) {
00332 addAttachment( (*it) );
00333 }
00334 }
00335
00336 void KOEditorAttachments::writeIncidence( KCal::Incidence *i )
00337 {
00338 i->clearAttachments();
00339
00340 QListViewItem *item;
00341 AttachmentListItem *attitem;
00342 for( item = mAttachments->firstChild(); item; item = item->nextSibling() ) {
00343 attitem = static_cast<AttachmentListItem*>(item);
00344 if ( attitem )
00345 i->addAttachment( new KCal::Attachment( *(attitem->attachment() ) ) );
00346 }
00347 }
00348
00349 #include "koeditorattachments.moc"