35#include "kmmimeparttree.h"
37#include "kmreaderwin.h"
41#include "kmcommands.h"
45#include <tdefiledialog.h>
46#include <tdemessagebox.h>
47#include <kiconloader.h>
49#include <tqclipboard.h>
51#include <tqpopupmenu.h>
57KMMimePartTree::KMMimePartTree(
KMReaderWin* readerWin,
60 : TDEListView( parent, name ),
61 mReaderWin( readerWin ), mSizeColumn(0)
63 setStyleDependantFrameWidth();
64 addColumn( i18n(
"Description") );
65 addColumn( i18n(
"Type") );
66 addColumn( i18n(
"Encoding") );
67 mSizeColumn = addColumn( i18n(
"Size") );
68 setColumnAlignment( 3, TQt::AlignRight );
70 restoreLayoutIfPresent();
71 connect(
this, TQT_SIGNAL( clicked( TQListViewItem* ) ),
72 this, TQT_SLOT( itemClicked( TQListViewItem* ) ) );
73 connect(
this, TQT_SIGNAL( contextMenuRequested( TQListViewItem*,
74 const TQPoint&,
int ) ),
75 this, TQT_SLOT( itemRightClicked( TQListViewItem*,
const TQPoint& ) ) );
76 setSelectionMode( TQListView::Extended );
77 setRootIsDecorated(
false );
78 setAllColumnsShowFocus(
true );
79 setShowToolTips(
true );
81 setDragEnabled(
true );
85static const char configGroup[] =
"MimePartTree";
87KMMimePartTree::~KMMimePartTree() {
88 saveLayout( KMKernel::config(), configGroup );
92void KMMimePartTree::restoreLayoutIfPresent() {
94 setColumnWidthMode( 0, Manual );
95 header()->setStretchEnabled(
true, 0 );
97 if ( KMKernel::config()->hasGroup( configGroup ) ) {
99 restoreLayout( KMKernel::config(), configGroup );
101 for (
int i = 1 ; i < 4 ; ++i )
102 setColumnWidthMode( i, Manual );
105 for (
int i = 1 ; i < 4 ; ++i )
106 setColumnWidthMode( i, Maximum );
111void KMMimePartTree::itemClicked( TQListViewItem* item )
113 if (
const KMMimePartTreeItem * i =
dynamic_cast<KMMimePartTreeItem*
>( item ) ) {
114 if( mReaderWin->mRootNode == i->node() )
115 mReaderWin->update(
true );
117 mReaderWin->setMsgPart( i->node() );
119 kdWarning(5006) <<
"Item was not a KMMimePartTreeItem!" << endl;
123void KMMimePartTree::itemRightClicked( TQListViewItem* item,
124 const TQPoint& point )
127 mCurrentContextMenuItem =
dynamic_cast<KMMimePartTreeItem*
>( item );
128 if ( 0 == mCurrentContextMenuItem ) {
129 kdDebug(5006) <<
"Item was not a KMMimePartTreeItem!" << endl;
132 TQPopupMenu* popup =
new TQPopupMenu;
133 if ( mCurrentContextMenuItem->node()->nodeId() > 2 &&
134 mCurrentContextMenuItem->node()->typeString() !=
"Multipart" ) {
135 popup->insertItem( SmallIcon(
"document-open"), i18n(
"to open",
"Open"),
this, TQT_SLOT(slotOpen()) );
136 popup->insertItem( i18n(
"Open With..."),
this, TQT_SLOT(slotOpenWith()) );
137 popup->insertItem( i18n(
"to view something",
"View"),
this, TQT_SLOT(slotView()) );
139 popup->insertItem( SmallIcon(
"document-save-as"),i18n(
"Save &As..." ),
this, TQT_SLOT( slotSaveAs() ) );
145 popup->insertItem( i18n(
"Save All Attachments..." ),
this,
146 TQT_SLOT( slotSaveAll() ) );
148 if ( mCurrentContextMenuItem->node()->nodeId() > 2 &&
149 mCurrentContextMenuItem->node()->typeString() !=
"Multipart" ) {
150 popup->insertItem( SmallIcon(
"edit-copy"), i18n(
"Copy"),
this, TQT_SLOT(slotCopy()) );
151 if ( GlobalSettings::self()->allowAttachmentDeletion() )
152 popup->insertItem( SmallIcon(
"edit-delete"), i18n(
"Delete Attachment" ),
153 this, TQT_SLOT( slotDelete() ) );
154 if ( GlobalSettings::self()->allowAttachmentEditing() )
155 popup->insertItem( SmallIcon(
"edit" ), i18n(
"Edit Attachment" ),
156 this, TQT_SLOT( slotEdit() ) );
158 if ( mCurrentContextMenuItem->node()->nodeId() > 0 )
159 popup->insertItem( i18n(
"Properties"),
this, TQT_SLOT(slotProperties()) );
160 popup->exec( point );
162 mCurrentContextMenuItem = 0;
167void KMMimePartTree::slotSaveAs()
169 saveSelectedBodyParts(
false );
173void KMMimePartTree::slotSaveAsEncoded()
175 saveSelectedBodyParts(
true );
179void KMMimePartTree::saveSelectedBodyParts(
bool encoded )
181 TQPtrList<TQListViewItem> selected = selectedItems();
183 Q_ASSERT( !selected.isEmpty() );
184 if ( selected.isEmpty() )
187 TQPtrListIterator<TQListViewItem> it( selected );
188 TQPtrList<partNode> parts;
189 while ( it.current() ) {
190 parts.append(
static_cast<KMMimePartTreeItem *
>(it.current())->node() );
193 mReaderWin->setUpdateAttachment();
194 KMSaveAttachmentsCommand *command =
195 new KMSaveAttachmentsCommand(
this, parts, mReaderWin->message(), encoded );
200void KMMimePartTree::slotSaveAll()
202 if( childCount() == 0)
205 mReaderWin->setUpdateAttachment();
207 new KMSaveAttachmentsCommand(
this, mReaderWin->message() );
212void KMMimePartTree::setStyleDependantFrameWidth()
216 if( style().isA(
"KeramikStyle") )
217 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1;
219 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth );
220 if ( frameWidth < 0 )
222 if ( frameWidth != lineWidth() )
223 setLineWidth( frameWidth );
228void KMMimePartTree::styleChange( TQStyle& oldStyle )
230 setStyleDependantFrameWidth();
231 TDEListView::styleChange( oldStyle );
235void KMMimePartTree::correctSize( TQListViewItem * item )
239 TDEIO::filesize_t totalSize = 0;
240 TQListViewItem * myChild = item->firstChild();
243 totalSize +=
static_cast<KMMimePartTreeItem*
>(myChild)->origSize();
244 myChild = myChild->nextSibling();
246 if ( totalSize >
static_cast<KMMimePartTreeItem*
>(item)->origSize() )
247 item->setText( mSizeColumn, TDEIO::convertSize(totalSize) );
248 if ( item->parent() )
249 correctSize( item->parent() );
252void KMMimePartTree::slotDelete()
254 TQPtrList<TQListViewItem> selected = selectedItems();
255 if ( selected.count() != 1 )
257 mReaderWin->slotDeleteAttachment(
static_cast<KMMimePartTreeItem*
>( selected.first() )->node() );
260void KMMimePartTree::slotEdit()
262 TQPtrList<TQListViewItem> selected = selectedItems();
263 if ( selected.count() != 1 )
265 mReaderWin->slotEditAttachment(
static_cast<KMMimePartTreeItem*
>( selected.first() )->node() );
268void KMMimePartTree::slotOpen()
270 startHandleAttachmentCommand( KMHandleAttachmentCommand::Open );
273void KMMimePartTree::slotOpenWith()
275 startHandleAttachmentCommand( KMHandleAttachmentCommand::OpenWith );
278void KMMimePartTree::slotView()
280 startHandleAttachmentCommand( KMHandleAttachmentCommand::View );
283void KMMimePartTree::slotProperties()
285 startHandleAttachmentCommand( KMHandleAttachmentCommand::Properties );
288void KMMimePartTree::startHandleAttachmentCommand(
int type)
290 TQPtrList<TQListViewItem> selected = selectedItems();
291 if ( selected.count() != 1 )
293 partNode* node =
static_cast<KMMimePartTreeItem*
>( selected.first() )->node();
294 TQString name = mReaderWin->tempFileUrlFromPartNode( node ).path();
295 KMHandleAttachmentCommand* command =
new KMHandleAttachmentCommand(
296 node, mReaderWin->message(), node->nodeId(), name,
297 KMHandleAttachmentCommand::AttachmentAction( type ), 0,
this );
298 connect( command, TQT_SIGNAL( showAttachment(
int,
const TQString& ) ),
299 mReaderWin, TQT_SLOT( slotAtmView(
int,
const TQString& ) ) );
303void KMMimePartTree::slotCopy()
306 KMMimePartTreeItem *item =
static_cast<KMMimePartTreeItem*
>( currentItem() );
308 KURL url = mReaderWin->tempFileUrlFromPartNode( item->node() );
309 if ( !url.isValid() )
return;
311 KURLDrag* drag =
new KURLDrag( urls,
this );
312 TQApplication::clipboard()->setData( drag, TQClipboard::Clipboard );
316KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTree * parent,
318 const TQString & description,
319 const TQString & mimetype,
320 const TQString & encoding,
321 TDEIO::filesize_t size )
322 : TQListViewItem( parent, description,
325 TDEIO::convertSize( size ) ),
326 mPartNode( node ), mOrigSize(size)
329 node->setMimePartTreeItem(
this );
330 setIconAndTextForType( mimetype );
332 parent->correctSize(
this);
335KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTreeItem * parent,
337 const TQString & description,
338 const TQString & mimetype,
339 const TQString & encoding,
340 TDEIO::filesize_t size,
342 : TQListViewItem( parent, description,
345 TDEIO::convertSize( size ) ),
346 mPartNode( node ), mOrigSize(size)
348 if( revertOrder && nextSibling() ){
349 TQListViewItem* sib = nextSibling();
350 while( sib->nextSibling() )
351 sib = sib->nextSibling();
355 node->setMimePartTreeItem(
this );
356 setIconAndTextForType( mimetype );
358 static_cast<KMMimePartTree*
>(listView())->correctSize(
this);
361void KMMimePartTreeItem::setIconAndTextForType(
const TQString & mime )
363 TQString mimetype = mime.lower();
364 if ( mimetype.startsWith(
"multipart/" ) ) {
365 setText( 1, mimetype );
366 setPixmap( 0, SmallIcon(
"folder") );
367 }
else if ( mimetype ==
"application/octet-stream" ) {
368 setText( 1, i18n(
"Unspecified Binary Data") );
369 setPixmap( 0, SmallIcon(
"unknown") );
371 KMimeType::Ptr mtp = KMimeType::mimeType( mimetype );
372 setText( 1, (mtp && !mtp->comment().isEmpty()) ? mtp->comment() : mimetype );
373 setPixmap( 0, mtp ? mtp->pixmap( TDEIcon::Small) : SmallIcon(
"unknown") );
378void KMMimePartTree::startDrag()
381 KMMimePartTreeItem *item =
static_cast<KMMimePartTreeItem*
>( currentItem() );
383 partNode *node = item->node();
385 KURL url = mReaderWin->tempFileUrlFromPartNode( node );
386 if (!url.isValid() )
return;
388 KURLDrag* drag =
new KURLDrag( urls,
this );
392#include "kmmimeparttree.moc"
This class implements a "reader window", that is a window used for reading or viewing messages.