26 #include "koeditorattachments.h"
29 #include <libkcal/incidence.h>
30 #include <libtdepim/kpimurlrequesterdlg.h>
31 #include <libtdepim/tdefileio.h>
32 #include <libtdepim/tdepimprotocols.h>
33 #include <libtdepim/maillistdrag.h>
34 #include <libtdepim/kvcarddrag.h>
35 #include <libtdepim/tdepimprotocols.h>
37 #include <tdelocale.h>
40 #include <tdemessagebox.h>
43 #include <tdetempfile.h>
45 #include <tdeio/netaccess.h>
46 #include <kmimetype.h>
47 #include <kiconloader.h>
48 #include <tdefiledialog.h>
49 #include <kstdaction.h>
50 #include <tdeactioncollection.h>
51 #include <tdepopupmenu.h>
52 #include <kprotocolinfo.h>
53 #include <klineedit.h>
54 #include <kseparator.h>
55 #include <kurlrequester.h>
56 #include <libkmime/kmime_message.h>
58 #include <tqcheckbox.h>
62 #include <tqlistview.h>
63 #include <tqpushbutton.h>
64 #include <tqdragobject.h>
65 #include <tqtooltip.h>
66 #include <tqwhatsthis.h>
67 #include <tqapplication.h>
68 #include <tqclipboard.h>
73 class AttachmentListItem :
public TDEIconViewItem
77 TDEIconViewItem( parent )
86 setDragEnabled(
true );
88 ~AttachmentListItem() {
delete mAttachment; }
91 const TQString uri()
const
93 return mAttachment->uri();
95 void setUri(
const TQString &uri )
97 mAttachment->setUri( uri );
100 void setData(
const TQByteArray data )
102 mAttachment->setDecodedData( data );
105 const TQString mimeType()
const
107 return mAttachment->mimeType();
109 void setMimeType(
const TQString &mime )
111 mAttachment->setMimeType( mime );
114 const TQString label()
const
116 return mAttachment->label();
118 void setLabel(
const TQString &label )
120 mAttachment->setLabel( label );
123 bool isBinary()
const
125 return mAttachment->isBinary();
127 TQPixmap icon()
const
129 return icon( KMimeType::mimeType( mAttachment->mimeType() ),
130 mAttachment->uri() );
132 static TQPixmap icon( KMimeType::Ptr mimeType,
const TQString &uri )
134 TQString iconStr = mimeType->icon( uri,
false );
135 return TDEGlobal::iconLoader()->loadIcon( iconStr, TDEIcon::Small );
137 void readAttachment()
139 if ( mAttachment->label().isEmpty() ) {
140 if ( mAttachment->isUri() ) {
141 setText( mAttachment->uri() );
143 setText( i18n(
"[Binary data]" ) );
146 setText( mAttachment->label() );
148 if ( mAttachment->mimeType().isEmpty() ||
149 !( KMimeType::mimeType( mAttachment->mimeType() ) ) ) {
150 KMimeType::Ptr mimeType;
151 if ( mAttachment->isUri() ) {
152 mimeType = KMimeType::findByURL( mAttachment->uri() );
154 mimeType = KMimeType::findByContent( mAttachment->decodedData() );
156 mAttachment->setMimeType( mimeType->name() );
166 AttachmentEditDialog::AttachmentEditDialog( AttachmentListItem *item,
168 : KDialogBase ( Plain, i18n(
"Add Attachment" ), Ok|Cancel, Ok, parent, 0, false, false ),
169 mItem( item ), mURLRequester( 0 )
171 TQFrame *topFrame = plainPage();
172 TQVBoxLayout *vbl =
new TQVBoxLayout( topFrame, 0, spacingHint() );
174 TQGridLayout *grid =
new TQGridLayout();
175 grid->setColStretch( 0, 0 );
176 grid->setColStretch( 1, 0 );
177 grid->setColStretch( 2, 1 );
178 vbl->addLayout( grid );
180 mIcon =
new TQLabel( topFrame );
181 mIcon->setPixmap( item->icon() );
182 grid->addWidget( mIcon, 0, 0 );
184 mLabelEdit =
new KLineEdit( topFrame );
185 mLabelEdit->setText( item->label().isEmpty() ? item->uri() : item->label() );
186 mLabelEdit->setClickMessage( i18n(
"Attachment name" ) );
187 TQToolTip::add( mLabelEdit, i18n(
"Give the attachment a name" ) );
188 TQWhatsThis::add( mLabelEdit,
189 i18n(
"Type any string you desire here for the name of the attachment" ) );
190 grid->addMultiCellWidget( mLabelEdit, 0, 0, 1, 2 );
192 KSeparator *sep =
new KSeparator( TQt::Horizontal, topFrame );
193 grid->addMultiCellWidget( sep, 1, 1, 0, 2 );
195 TQLabel *label =
new TQLabel( i18n(
"Type:" ), topFrame );
196 grid->addWidget( label, 2, 0 );
197 TQString typecomment = item->mimeType().isEmpty() ?
199 KMimeType::mimeType( item->mimeType() )->comment();
200 mTypeLabel =
new TQLabel( typecomment, topFrame );
201 grid->addWidget( mTypeLabel, 2, 1 );
202 mMimeType = KMimeType::mimeType( item->mimeType() );
204 mInline =
new TQCheckBox( i18n(
"Store attachment inline" ), topFrame );
205 grid->addMultiCellWidget( mInline, 3, 3, 0, 2 );
206 mInline->setChecked( item->isBinary() );
207 TQToolTip::add( mInline, i18n(
"Store the attachment file inside the calendar" ) );
210 i18n(
"Checking this option will cause the attachment to be stored inside "
211 "your calendar, which can take a lot of space depending on the size "
212 "of the attachment. If this option is not checked, then only a link "
213 "pointing to the attachment will be stored. Do not use a link for "
214 "attachments that change often or may be moved (or removed) from "
215 "their current location." ) );
217 if ( item->attachment()->isUri() || !item->attachment()->data() ) {
218 label =
new TQLabel( i18n(
"Location:" ), topFrame );
219 grid->addWidget( label, 4, 0 );
220 mURLRequester =
new KURLRequester( item->uri(), topFrame );
221 TQToolTip::add( mURLRequester, i18n(
"Provide a location for the attachment file" ) );
224 i18n(
"Enter the path to the attachment file or use the "
225 "file browser by pressing the adjacent button" ) );
226 grid->addMultiCellWidget( mURLRequester, 4, 4, 1, 2 );
227 connect( mURLRequester, TQ_SIGNAL(urlSelected(
const TQString &)),
228 TQ_SLOT(urlSelected(
const TQString &)) );
229 connect( mURLRequester, TQ_SIGNAL( textChanged(
const TQString& ) ),
230 TQ_SLOT( urlChanged(
const TQString& ) ) );
231 urlChanged( item->uri() );
233 uint size = item->attachment()->size();
234 grid->addWidget(
new TQLabel( i18n(
"Size:" ), topFrame ), 4, 0 );
235 grid->addWidget(
new TQLabel( TQString::fromLatin1(
"%1 (%2)" ).
236 arg( TDEIO::convertSize( size ) ).
237 arg( TDEGlobal::locale()->formatNumber(
238 size, 0 ) ), topFrame ), 4, 2 );
240 vbl->addStretch( 10 );
243 void AttachmentEditDialog::slotApply()
245 if ( !mLabelEdit->text().isEmpty() ) {
246 mItem->setLabel( mLabelEdit->text() );
248 if ( mURLRequester ) {
249 KURL url( mURLRequester->url() );
250 if ( url.isLocalFile() ) {
251 mItem->setLabel( url.fileName() );
253 mItem->setLabel( url.url() );
257 if ( mItem->label().isEmpty() ) {
258 mItem->setLabel( i18n(
"New attachment" ) );
260 mItem->setMimeType( mMimeType->name() );
261 if ( mURLRequester ) {
262 KURL url( mURLRequester->url() );
264 TQString correctedUrl = mURLRequester->url();
265 if ( !url.isValid() ) {
271 correctedUrl = TQDir::home().filePath( mURLRequester->url() );
272 url = KURL( correctedUrl );
273 if ( url.isValid() ) {
274 urlSelected( correctedUrl );
275 mItem->setMimeType( mMimeType->name() );
279 if ( mInline->isChecked() ) {
281 if ( TDEIO::NetAccess::download( correctedUrl, tmpFile,
this ) ) {
283 if ( !f.open( IO_ReadOnly ) ) {
286 TQByteArray data = f.readAll();
288 mItem->setData( data );
290 TDEIO::NetAccess::removeTempFile( tmpFile );
292 mItem->setUri( url.url() );
297 void AttachmentEditDialog::accept()
303 void AttachmentEditDialog::urlChanged(
const TQString &url )
305 enableButton( Ok, !url.isEmpty() );
308 void AttachmentEditDialog::urlSelected(
const TQString &url )
311 mMimeType = KMimeType::findByURL( kurl );
312 mTypeLabel->setText( mMimeType->comment() );
313 mIcon->setPixmap( AttachmentListItem::icon( mMimeType, kurl.path() ) );
316 AttachmentIconView::AttachmentIconView( KOEditorAttachments* parent )
317 : TDEIconView( parent ),
320 setSelectionMode( TQIconView::Extended );
321 setMode( TDEIconView::Select );
322 setItemTextPos( TQIconView::Right );
323 setArrangement( TQIconView::LeftToRight );
324 setMaxItemWidth( TQMAX(maxItemWidth(), 250) );
325 setMinimumHeight( TQMAX(fontMetrics().height(), 16) + 12 );
327 connect(
this, TQ_SIGNAL( dropped ( TQDropEvent *,
const TQValueList<TQIconDragItem> & ) ),
328 this, TQ_SLOT( handleDrop( TQDropEvent *,
const TQValueList<TQIconDragItem> & ) ) );
331 KURL AttachmentIconView::tempFileForAttachment(
KCal::Attachment *attachment )
333 if ( mTempFiles.contains( attachment ) ) {
334 return mTempFiles[attachment];
336 TQStringList patterns = KMimeType::mimeType( attachment->mimeType() )->patterns();
339 if ( !patterns.empty() ) {
340 file =
new KTempFile( TQString(),
341 TQString( patterns.first() ).remove(
'*' ),0600 );
343 file =
new KTempFile( TQString(), TQString(), 0600 );
345 file->setAutoDelete(
true );
346 file->file()->open( IO_WriteOnly );
347 TQTextStream stream( file->file() );
348 stream.writeRawBytes( attachment->decodedData().data(), attachment->size() );
349 KURL url( file->name() );
350 mTempFiles.insert( attachment, url );
352 return mTempFiles[attachment];
355 TQDragObject *AttachmentIconView::mimeData()
360 for ( TQIconViewItem *it = firstItem(); it; it = it->nextItem() ) {
361 if ( it->isSelected() ) {
362 AttachmentListItem *item =
static_cast<AttachmentListItem *
>( it );
363 if ( item->isBinary() ) {
364 urls.append( tempFileForAttachment( item->attachment() ) );
366 urls.append( item->uri() );
368 labels.append( KURL::encode_string( item->label() ) );
371 if ( selectionMode() == TQIconView::NoSelection ) {
372 AttachmentListItem *item =
static_cast<AttachmentListItem *
>( currentItem() );
374 urls.append( item->uri() );
375 labels.append( KURL::encode_string( item->label() ) );
379 TQMap<TQString, TQString> metadata;
380 metadata[
"labels"] = labels.join(
":" );
382 KURLDrag *drag =
new KURLDrag( urls, metadata );
386 AttachmentIconView::~AttachmentIconView()
388 for ( std::set<KTempDir*>::iterator it = mTempDirs.begin() ; it != mTempDirs.end() ; ++it ) {
393 TQDragObject * AttachmentIconView::dragObject()
396 for ( TQIconViewItem *it = firstItem( ); it; it = it->nextItem( ) ) {
397 if ( !it->isSelected() )
continue;
398 AttachmentListItem * item =
dynamic_cast<AttachmentListItem*
>( it );
399 if ( !item )
return 0;
403 if ( att->isUri() ) {
404 url.setPath( att->uri() );
406 KTempDir *tempDir =
new KTempDir();
407 tempDir->setAutoDelete(
true );
408 mTempDirs.insert( tempDir );
410 encoded.duplicate( att->data(), strlen( att->data() ) );
412 KCodecs::base64Decode( encoded, decoded );
413 const TQString fileName = tempDir->name( ) +
'/' + att->label();
414 KPIM::kByteArrayToFile( decoded, fileName,
false,
false,
false );
415 url.setPath( fileName );
419 KURLDrag *drag =
new KURLDrag( urls,
this );
423 void AttachmentIconView::handleDrop( TQDropEvent *event,
const TQValueList<TQIconDragItem> & list )
426 mParent->handlePasteOrDrop( event );
430 void AttachmentIconView::dragMoveEvent( TQDragMoveEvent *event )
432 mParent->dragMoveEvent( event );
435 void AttachmentIconView::contentsDragMoveEvent( TQDragMoveEvent *event )
437 mParent->dragMoveEvent( event );
440 void AttachmentIconView::contentsDragEnterEvent( TQDragEnterEvent *event )
442 mParent->dragMoveEvent( event );
445 void AttachmentIconView::dragEnterEvent( TQDragEnterEvent *event )
447 mParent->dragEnterEvent( event );
450 KOEditorAttachments::KOEditorAttachments(
int spacing, TQWidget *parent,
452 : TQWidget( parent, name )
454 TQBoxLayout *topLayout =
new TQHBoxLayout(
this );
455 topLayout->setSpacing( spacing );
457 TQLabel *label =
new TQLabel( i18n(
"Attachments:"),
this );
458 topLayout->addWidget( label );
460 mAttachments =
new AttachmentIconView(
this );
461 TQWhatsThis::add( mAttachments,
462 i18n(
"Displays a list of current items (files, mail, etc.) "
463 "that have been associated with this event or to-do. ") );
464 topLayout->addWidget( mAttachments );
465 connect( mAttachments, TQ_SIGNAL( doubleClicked( TQIconViewItem * ) ),
466 TQ_SLOT( showAttachment( TQIconViewItem * ) ) );
467 connect( mAttachments, TQ_SIGNAL(selectionChanged()),
468 TQ_SLOT(selectionChanged()) );
469 connect( mAttachments, TQ_SIGNAL(contextMenuRequested(TQIconViewItem*,
const TQPoint&)),
470 TQ_SLOT(contextMenu(TQIconViewItem*,
const TQPoint&)) );
472 TQPushButton *addButton =
new TQPushButton(
this );
473 addButton->setIconSet( SmallIconSet(
"add" ) );
474 TQToolTip::add( addButton, i18n(
"Add an attachment" ) );
475 TQWhatsThis::add( addButton,
476 i18n(
"Shows a dialog used to select an attachment "
477 "to add to this event or to-do as link or as "
479 topLayout->addWidget( addButton );
480 connect( addButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotAdd()) );
482 mRemoveBtn =
new TQPushButton(
this );
483 mRemoveBtn->setIconSet( SmallIconSet(
"remove" ) );
484 TQToolTip::add( mRemoveBtn, i18n(
"&Remove") );
485 TQWhatsThis::add( mRemoveBtn,
486 i18n(
"Removes the attachment selected in the list above "
487 "from this event or to-do.") );
488 topLayout->addWidget( mRemoveBtn );
489 connect( mRemoveBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotRemove()) );
491 mContextMenu =
new TDEPopupMenu(
this );
493 TDEActionCollection* ac =
new TDEActionCollection(
this,
this );
495 mOpenAction =
new TDEAction( i18n(
"Open"), 0,
this, TQ_SLOT(slotShow()), ac );
496 mOpenAction->plug( mContextMenu );
498 mSaveAsAction =
new TDEAction( i18n(
"Save As..." ), 0,
this, TQ_SLOT(slotSaveAs()), ac );
499 mSaveAsAction->plug( mContextMenu );
500 mContextMenu->insertSeparator();
502 mCopyAction = KStdAction::copy(
this, TQ_SLOT(slotCopy()), ac );
503 mCopyAction->plug( mContextMenu );
504 mCutAction = KStdAction::cut(
this, TQ_SLOT(slotCut()), ac );
505 mCutAction->plug( mContextMenu );
506 TDEAction *action = KStdAction::paste(
this, TQ_SLOT(slotPaste()), ac );
507 action->plug( mContextMenu );
508 mContextMenu->insertSeparator();
510 mDeleteAction =
new TDEAction( i18n(
"&Remove" ), 0,
this, TQ_SLOT(slotRemove()), ac );
511 mDeleteAction->plug( mContextMenu );
512 mDeleteAction->setShortcut( Key_Delete );
513 mContextMenu->insertSeparator();
515 mEditAction =
new TDEAction( i18n(
"&Properties..." ), 0,
this, TQ_SLOT(slotEdit()), ac );
516 mEditAction->plug( mContextMenu );
519 setAcceptDrops(
true );
522 KOEditorAttachments::~KOEditorAttachments()
526 bool KOEditorAttachments::hasAttachments()
528 return mAttachments->count() != 0;
531 void KOEditorAttachments::dragMoveEvent( TQDragMoveEvent *event )
533 event->accept( KURLDrag::canDecode( event ) ||
534 TQTextDrag::canDecode( event ) ||
535 KPIM::MailListDrag::canDecode( event ) ||
536 KVCardDrag::canDecode( event ) );
539 void KOEditorAttachments::dragEnterEvent( TQDragEnterEvent* event )
541 dragMoveEvent( event );
544 void KOEditorAttachments::handlePasteOrDrop( TQMimeSource* source )
547 bool probablyWeHaveUris =
false;
548 bool weCanCopy =
true;
551 if ( KVCardDrag::canDecode( source ) ) {
552 TDEABC::Addressee::List addressees;
553 KVCardDrag::decode( source, addressees );
554 for ( TDEABC::Addressee::List::ConstIterator it = addressees.constBegin();
555 it != addressees.constEnd(); ++it ) {
556 urls.append( TDEPIMPROTOCOL_CONTACT + ( *it ).uid() );
558 labels.append( TQString::fromUtf8( ( *it ).realName().latin1() ) );
560 probablyWeHaveUris =
true;
561 }
else if ( KURLDrag::canDecode( source ) ) {
562 TQMap<TQString,TQString> metadata;
563 if ( KURLDrag::decode( source, urls, metadata ) ) {
564 probablyWeHaveUris =
true;
565 labels = TQStringList::split(
':', metadata[
"labels"], FALSE );
566 for ( TQStringList::Iterator it = labels.begin(); it != labels.end(); ++it ) {
567 *it = KURL::decode_string( (*it).latin1() );
571 }
else if ( TQTextDrag::canDecode( source ) ) {
573 TQTextDrag::decode( source, text );
574 TQStringList lst = TQStringList::split(
'\n', text, FALSE );
575 for ( TQStringList::ConstIterator it = lst.constBegin(); it != lst.constEnd(); ++it ) {
577 labels.append( TQString() );
579 probablyWeHaveUris =
true;
584 if ( probablyWeHaveUris ) {
585 menu.insertItem( i18n(
"&Link here" ), DRAG_LINK, items++ );
587 for ( KURL::List::ConstIterator it = urls.constBegin(); it != urls.constEnd(); ++it ) {
588 if ( !( weCanCopy = KProtocolInfo::supportsReading( *it ) ) ) {
593 menu.insertItem( SmallIcon(
"edit-copy" ), i18n(
"&Copy Here" ), DRAG_COPY, items++ );
596 menu.insertItem( SmallIcon(
"edit-copy" ), i18n(
"&Copy Here" ), DRAG_COPY, items++ );
599 menu.insertSeparator();
601 menu.insertItem( SmallIcon(
"cancel" ), i18n(
"C&ancel" ), DRAG_CANCEL, items );
602 int action = menu.exec( TQCursor::pos(), 0 );
604 if ( action == DRAG_LINK ) {
605 TQStringList::ConstIterator jt = labels.constBegin();
606 for ( KURL::List::ConstIterator it = urls.constBegin();
607 it != urls.constEnd(); ++it ) {
608 TQString label = (*jt++);
609 if ( mAttachments->findItem( label ) ) {
610 label +=
'~' + randomString( 3 );
612 addUriAttachment( (*it).url(), TQString(), label,
true );
614 }
else if ( action != DRAG_CANCEL ) {
615 if ( probablyWeHaveUris ) {
616 for ( KURL::List::ConstIterator it = urls.constBegin();
617 it != urls.constEnd(); ++it ) {
618 TQString label = (*it).fileName();
619 if ( label.isEmpty() ) {
620 label = (*it).prettyURL();
622 if ( mAttachments->findItem( label ) ) {
623 label +=
'~' + randomString( 3 );
625 addUriAttachment( (*it).url(), TQString(), label,
true );
628 addDataAttachment( source->encodedData( source->format() ),
630 KMimeType::mimeType( source->format() )->name() );
635 void KOEditorAttachments::dropEvent( TQDropEvent* event )
637 handlePasteOrDrop( event );
640 void KOEditorAttachments::showAttachment( TQIconViewItem *item )
642 AttachmentListItem *attitem =
static_cast<AttachmentListItem*
>(item);
643 if ( !attitem || !attitem->attachment() )
return;
649 void KOEditorAttachments::saveAttachment( TQIconViewItem *item )
651 AttachmentListItem *attitem =
static_cast<AttachmentListItem*
>(item);
652 if ( !attitem || !attitem->attachment() )
return;
658 void KOEditorAttachments::slotAdd()
660 AttachmentListItem *item =
new AttachmentListItem( 0, mAttachments );
662 AttachmentEditDialog *dlg =
new AttachmentEditDialog( item, mAttachments )
664 if ( dlg->exec() == KDialog::Rejected ) {
670 void KOEditorAttachments::slotAddData()
672 KURL uri = KFileDialog::getOpenFileName( TQString(), TQString(),
this, i18n(
"Add Attachment") );
673 if ( !uri.isEmpty() ) {
674 TQString label = uri.fileName();
675 if ( label.isEmpty() ) {
676 label = uri.prettyURL();
678 addUriAttachment( uri.url(), TQString(), label,
true );
682 void KOEditorAttachments::slotEdit()
684 for ( TQIconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
685 if ( item->isSelected() ) {
686 AttachmentListItem *attitem =
static_cast<AttachmentListItem*
>( item );
687 if ( !attitem || !attitem->attachment() ) {
691 AttachmentEditDialog *dialog =
new AttachmentEditDialog( attitem, mAttachments );
692 dialog->mInline->setEnabled(
false );
693 dialog->setModal(
false );
694 connect( dialog, TQ_SIGNAL(hidden()), dialog, TQ_SLOT(delayedDestruct()) );
700 void KOEditorAttachments::slotRemove()
702 TQValueList<TQIconViewItem*> selected;
704 for ( TQIconViewItem *it = mAttachments->firstItem( ); it; it = it->nextItem( ) ) {
705 if ( !it->isSelected() )
continue;
708 AttachmentListItem *attitem =
static_cast<AttachmentListItem*
>(it);
710 labels << att->label();
713 if ( selected.isEmpty() ) {
717 TQString labelsStr = labels.join(
"<br>" );
719 if ( KMessageBox::questionYesNo(
721 i18n(
"<qt>Do you really want to remove these attachments?<p>%1</qt>" ).arg( labelsStr ),
722 i18n(
"Remove Attachment?" ),
723 KStdGuiItem::yes(), KStdGuiItem::no(),
724 "calendarRemoveAttachments" ) != KMessageBox::Yes ) {
728 for ( TQValueList<TQIconViewItem*>::iterator it( selected.begin() ), end( selected.end() );
730 if ( (*it)->nextItem() ) {
731 (*it)->nextItem()->setSelected(
true );
732 }
else if ( (*it)->prevItem() ) {
733 (*it)->prevItem()->setSelected(
true );
737 mAttachments->slotUpdate();
740 void KOEditorAttachments::slotShow()
742 for ( TQIconViewItem *it = mAttachments->firstItem(); it; it = it->nextItem() ) {
743 if ( !it->isSelected() )
745 showAttachment( it );
749 void KOEditorAttachments::slotSaveAs()
751 for ( TQIconViewItem *it = mAttachments->firstItem(); it; it = it->nextItem() ) {
752 if ( !it->isSelected() )
754 saveAttachment( it );
758 void KOEditorAttachments::setDefaults()
760 mAttachments->clear();
763 TQString KOEditorAttachments::randomString(
int length)
const
765 if (length <=0 )
return TQString();
767 TQString str; str.setLength( length );
781 void KOEditorAttachments::addUriAttachment(
const TQString &uri,
782 const TQString &mimeType,
783 const TQString &label,
787 AttachmentListItem *item =
new AttachmentListItem( 0, mAttachments );
789 item->setLabel( label );
790 if ( mimeType.isEmpty() ) {
791 if ( uri.startsWith( TDEPIMPROTOCOL_CONTACT ) ) {
792 item->setMimeType(
"text/directory" );
793 }
else if ( uri.startsWith( TDEPIMPROTOCOL_EMAIL ) ) {
794 item->setMimeType(
"message/rfc822" );
795 }
else if ( uri.startsWith( TDEPIMPROTOCOL_INCIDENCE ) ) {
796 item->setMimeType(
"text/calendar" );
797 }
else if ( uri.startsWith( TDEPIMPROTOCOL_NEWSARTICLE ) ) {
798 item->setMimeType(
"message/news" );
800 item->setMimeType( KMimeType::findByURL( uri )->name() );
805 if ( TDEIO::NetAccess::download( uri, tmpFile,
this ) ) {
807 if ( !f.open( IO_ReadOnly ) ) {
810 const TQByteArray data = f.readAll();
812 addDataAttachment( data, mimeType, label );
814 TDEIO::NetAccess::removeTempFile( tmpFile );
818 void KOEditorAttachments::addDataAttachment(
const TQByteArray &data,
819 const TQString &mimeType,
820 const TQString &label )
822 AttachmentListItem *item =
new AttachmentListItem( 0, mAttachments );
824 TQString nlabel = label;
825 if ( mimeType ==
"message/rfc822" ) {
828 msg.setContent( data.data() );
830 nlabel = msg.subject()->asUnicodeString();
833 item->setData( data );
834 item->setLabel( nlabel );
835 if ( mimeType.isEmpty() ) {
836 item->setMimeType( KMimeType::findByContent( data )->name() );
838 item->setMimeType( mimeType );
844 new AttachmentListItem( attachment, mAttachments );
849 mAttachments->clear();
851 KCal::Attachment::List attachments = i->
attachments();
852 KCal::Attachment::List::ConstIterator it;
853 for( it = attachments.begin(); it != attachments.end(); ++it ) {
854 addAttachment( (*it) );
856 if ( mAttachments->count() > 0 ) {
857 TQTimer::singleShot( 0, mAttachments, TQ_SLOT(arrangeItemsInGrid()) );
865 TQIconViewItem *item;
866 AttachmentListItem *attitem;
867 for( item = mAttachments->firstItem(); item; item = item->nextItem() ) {
868 attitem =
static_cast<AttachmentListItem*
>(item);
875 void KOEditorAttachments::slotCopy()
877 TQApplication::clipboard()->setData( mAttachments->mimeData(), TQClipboard::Clipboard );
880 void KOEditorAttachments::slotCut()
886 void KOEditorAttachments::slotPaste()
888 handlePasteOrDrop( TQApplication::clipboard()->data() );
891 void KOEditorAttachments::selectionChanged()
893 bool selected =
false;
894 for ( TQIconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
895 if ( item->isSelected() ) {
900 mRemoveBtn->setEnabled( selected );
903 void KOEditorAttachments::contextMenu(TQIconViewItem * item,
const TQPoint & pos)
905 const bool enable = item != 0;
908 for ( TQIconViewItem *item = mAttachments->firstItem(); item; item = item->nextItem() ) {
909 if ( item->isSelected() ) {
914 mOpenAction->setEnabled( enable );
916 mSaveAsAction->setEnabled( enable && numSelected == 1 );
917 mCopyAction->setEnabled( enable && numSelected == 1 );
918 mCutAction->setEnabled( enable && numSelected == 1 );
919 mDeleteAction->setEnabled( enable );
920 mEditAction->setEnabled( enable );
921 mContextMenu->exec( pos );
924 #include "koeditorattachments.moc"
void addAttachment(Attachment *attachment)
Attachment::List attachments() const
bool view(TQWidget *parent, Attachment *attachment)
bool saveAs(TQWidget *parent, Attachment *attachment)