korganizer

koeditorattachments.cpp

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 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
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 <kmessagebox.h>
00034 #include <klistview.h>
00035 #include <kurldrag.h>
00036 
00037 #include <qlayout.h>
00038 #include <qlistview.h>
00039 #include <qpushbutton.h>
00040 #include <qdragobject.h>
00041 #include <qwhatsthis.h>
00042 
00043 class AttachmentListItem : public KListViewItem
00044 {
00045   public:
00046     AttachmentListItem( KCal::Attachment*att, QListView *parent ) :
00047         KListViewItem( parent )
00048     {
00049       if ( att ) {
00050         mAttachment = new KCal::Attachment( *att );
00051       } else {
00052         mAttachment = new KCal::Attachment( QString::null );
00053       }
00054       readAttachment();
00055     }
00056     ~AttachmentListItem() { delete mAttachment; }
00057     KCal::Attachment *attachment() const { return mAttachment; }
00058 
00059     void setUri( const QString &uri )
00060     {
00061       mAttachment->setUri( uri );
00062       readAttachment();
00063     }
00064     void setData( const char *base64 )
00065     {
00066       mAttachment->setData( base64 );
00067       readAttachment();
00068     }
00069     void setMimeType( const QString &mime )
00070     {
00071       mAttachment->setMimeType( mime );
00072       readAttachment();
00073     }
00074 
00075     void readAttachment()
00076     {
00077       if ( mAttachment->isUri() )
00078         setText( 0, mAttachment->uri() );
00079       else
00080         setText( 0, i18n("[Binary data]") );
00081       setText( 1, mAttachment->mimeType() );
00082     }
00083 
00084   private:
00085     KCal::Attachment *mAttachment;
00086 };
00087 
00088 KOEditorAttachments::KOEditorAttachments( int spacing, QWidget *parent,
00089                                           const char *name )
00090   : QWidget( parent, name )
00091 {
00092   QBoxLayout *topLayout = new QVBoxLayout( this );
00093   topLayout->setSpacing( spacing );
00094 
00095   mAttachments = new KListView( this );
00096   QWhatsThis::add( mAttachments,
00097                    i18n("Displays a list of current items (files, mail, etc.) "
00098                         "that have been associated with this event or to-do. "
00099                         "The URI column displays the location of the file.") );
00100   mAttachments->addColumn( i18n("URI") );
00101   mAttachments->addColumn( i18n("MIME Type") );
00102   topLayout->addWidget( mAttachments );
00103   connect( mAttachments, SIGNAL( doubleClicked( QListViewItem * ) ),
00104            SLOT( showAttachment( QListViewItem * ) ) );
00105 
00106   QBoxLayout *buttonLayout = new QHBoxLayout( topLayout );
00107 
00108   QPushButton *button = new QPushButton( i18n("&Add..."), this );
00109   QWhatsThis::add( button,
00110                    i18n("Shows a dialog used to select an attachment "
00111                         "to add to this event or to-do.") );
00112   buttonLayout->addWidget( button );
00113   connect( button, SIGNAL( clicked() ), SLOT( slotAdd() ) );
00114 
00115   button = new QPushButton( i18n("&Edit..."), this );
00116   QWhatsThis::add( button,
00117                    i18n("Shows a dialog used to edit the attachment "
00118                         "currently selected in the list above.") );
00119   buttonLayout->addWidget( button );
00120   connect( button, SIGNAL( clicked() ), SLOT( slotEdit() ) );
00121 
00122   button = new QPushButton( i18n("&Remove"), this );
00123   QWhatsThis::add( button,
00124                    i18n("Removes the attachment selected in the list above "
00125                         "from this event or to-do.") );
00126   buttonLayout->addWidget( button );
00127   connect( button, SIGNAL( clicked() ), SLOT( slotRemove() ) );
00128 
00129   button = new QPushButton( i18n("&Show"), this );
00130   QWhatsThis::add( button,
00131                    i18n("Opens the attachment selected in the list above "
00132                         "in the viewer that is associated with it in your "
00133                         "KDE preferences.") );
00134   buttonLayout->addWidget( button );
00135   connect( button, SIGNAL( clicked() ), SLOT( slotShow() ) );
00136 
00137   setAcceptDrops( TRUE );
00138 }
00139 
00140 KOEditorAttachments::~KOEditorAttachments()
00141 {
00142 }
00143 
00144 bool KOEditorAttachments::hasAttachments()
00145 {
00146   return mAttachments->childCount() > 0;
00147 }
00148 
00149 void KOEditorAttachments::dragEnterEvent( QDragEnterEvent* event ) {
00150   event->accept( KURLDrag::canDecode( event ) | QTextDrag::canDecode( event ) );
00151 }
00152 
00153 void KOEditorAttachments::dropEvent( QDropEvent* event ) {
00154   KURL::List urls;
00155   QString text;
00156   if ( KURLDrag::decode( event, urls ) ) {
00157     for ( KURL::List::ConstIterator it = urls.begin(); it != urls.end(); ++it ) {
00158       addAttachment( (*it).url() );
00159     }
00160   } else if ( QTextDrag::decode( event, text ) ) {
00161     QStringList lst = QStringList::split( '\n', text );
00162     for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00163       addAttachment( (*it)  );
00164     }
00165   }
00166 
00167 }
00168 
00169 void KOEditorAttachments::showAttachment( QListViewItem *item )
00170 {
00171   AttachmentListItem *attitem = static_cast<AttachmentListItem*>(item);
00172   if ( !attitem || !attitem->attachment() ) return;
00173 
00174   KCal::Attachment *att = attitem->attachment();
00175   if ( att->isUri() ) {
00176     emit openURL( att->uri() );
00177   } else {
00178     // FIXME: Handle binary attachments
00179   }
00180 }
00181 
00182 
00183 
00184 void KOEditorAttachments::slotAdd()
00185 {
00186   KURL uri = KPimURLRequesterDlg::getURL( QString::null, i18n(
00187          "URL (e.g. a web page) or file to be attached (only "
00188          "the link will be attached, not the file itself):"), this,
00189                                        i18n("Add Attachment") );
00190   // TODO: Implement adding binary attachments
00191   if ( !uri.isEmpty() ) {
00192     addAttachment( uri.url() );
00193   }
00194 }
00195 
00196 void KOEditorAttachments::slotEdit()
00197 {
00198   QListViewItem *item = mAttachments->currentItem();
00199   AttachmentListItem *attitem = static_cast<AttachmentListItem*>(item);
00200   if ( !attitem || !attitem->attachment() ) return;
00201 
00202   KCal::Attachment *att = attitem->attachment();
00203   if ( att->isUri() ) {
00204     KURL uri = KPimURLRequesterDlg::getURL( att->uri(), i18n(
00205          "URL (e.g. a web page) or file to be attached (only "
00206          "the link will be attached, not the file itself):"), this,
00207                                          i18n("Edit Attachment") );
00208 
00209     if ( !uri.isEmpty() )
00210       attitem->setUri( uri.url() );
00211   } else {
00212     // FIXME: Handle binary attachments
00213   }
00214 }
00215 
00216 void KOEditorAttachments::slotRemove()
00217 {
00218   QListViewItem *item = mAttachments->currentItem();
00219   if ( !item ) return;
00220 
00221   if ( KMessageBox::warningContinueCancel(this,
00222         i18n("This item will be permanently deleted."),
00223   i18n("KOrganizer Confirmation"),KStdGuiItem::del()) == KMessageBox::Continue )
00224     delete item;
00225 }
00226 
00227 void KOEditorAttachments::slotShow()
00228 {
00229   showAttachment( mAttachments->currentItem() );
00230 }
00231 
00232 void KOEditorAttachments::setDefaults()
00233 {
00234   mAttachments->clear();
00235 }
00236 
00237 void KOEditorAttachments::addAttachment( const QString &uri,
00238                                          const QString &mimeType )
00239 {
00240   AttachmentListItem *item = new AttachmentListItem( 0, mAttachments );
00241   item->setUri( uri );
00242   if ( !mimeType.isEmpty() ) item->setMimeType( mimeType );
00243 }
00244 
00245 
00246 void KOEditorAttachments::addAttachment( KCal::Attachment *attachment )
00247 {
00248   new AttachmentListItem( attachment, mAttachments );
00249 }
00250 
00251 void KOEditorAttachments::readIncidence( KCal::Incidence *i )
00252 {
00253   mAttachments->clear();
00254 
00255   KCal::Attachment::List attachments = i->attachments();
00256   KCal::Attachment::List::ConstIterator it;
00257   for( it = attachments.begin(); it != attachments.end(); ++it ) {
00258     addAttachment( (*it) );
00259   }
00260 }
00261 
00262 void KOEditorAttachments::writeIncidence( KCal::Incidence *i )
00263 {
00264   i->clearAttachments();
00265 
00266   QListViewItem *item;
00267   AttachmentListItem *attitem;
00268   for( item = mAttachments->firstChild(); item; item = item->nextSibling() ) {
00269     attitem = static_cast<AttachmentListItem*>(item);
00270     if ( attitem )
00271       i->addAttachment( new KCal::Attachment( *(attitem->attachment() ) ) );
00272   }
00273 }
00274 
00275 #include "koeditorattachments.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys