35 #include "kmmimeparttree.h"
37 #include "kmreaderwin.h"
39 #include "kmmsgpart.h"
41 #include "kmcommands.h"
44 #include <tdelocale.h>
45 #include <tdefiledialog.h>
46 #include <tdemessagebox.h>
47 #include <kiconloader.h>
49 #include <tqclipboard.h>
51 #include <tqpopupmenu.h>
57 KMMimePartTree::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, TQ_SIGNAL( clicked( TQListViewItem* ) ),
72 this, TQ_SLOT( itemClicked( TQListViewItem* ) ) );
73 connect(
this, TQ_SIGNAL( contextMenuRequested( TQListViewItem*,
74 const TQPoint&,
int ) ),
75 this, TQ_SLOT( itemRightClicked( TQListViewItem*,
const TQPoint& ) ) );
76 setSelectionMode( TQListView::Extended );
77 setRootIsDecorated(
false );
78 setAllColumnsShowFocus(
true );
79 setShowToolTips(
true );
81 setDragEnabled(
true );
85 static const char configGroup[] =
"MimePartTree";
87 KMMimePartTree::~KMMimePartTree() {
88 saveLayout( KMKernel::config(), configGroup );
92 void 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 );
111 void 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;
123 void 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 mCurrentContextMenuItem->node()->subTypeString() ==
"HTML" ) {
136 popup->insertItem( SmallIcon(
"document-open"), i18n(
"to open",
"Open"),
this, TQ_SLOT(slotOpen()) );
137 popup->insertItem( i18n(
"Open With..."),
this, TQ_SLOT(slotOpenWith()) );
138 popup->insertItem( i18n(
"to view something",
"View"),
this, TQ_SLOT(slotView()) );
140 popup->insertItem( SmallIcon(
"document-save-as"),i18n(
"Save &As..." ),
this, TQ_SLOT( slotSaveAs() ) );
146 popup->insertItem( i18n(
"Save All Attachments..." ),
this,
147 TQ_SLOT( slotSaveAll() ) );
149 if ( mCurrentContextMenuItem->node()->nodeId() > 2 &&
150 mCurrentContextMenuItem->node()->typeString() !=
"Multipart" ) {
151 popup->insertItem( SmallIcon(
"edit-copy"), i18n(
"Copy"),
this, TQ_SLOT(slotCopy()) );
152 if ( GlobalSettings::self()->allowAttachmentDeletion() )
153 popup->insertItem( SmallIcon(
"edit-delete"), i18n(
"Delete Attachment" ),
154 this, TQ_SLOT( slotDelete() ) );
155 if ( GlobalSettings::self()->allowAttachmentEditing() )
156 popup->insertItem( SmallIcon(
"edit" ), i18n(
"Edit Attachment" ),
157 this, TQ_SLOT( slotEdit() ) );
159 if ( mCurrentContextMenuItem->node()->nodeId() > 0 )
160 popup->insertItem( i18n(
"Properties"),
this, TQ_SLOT(slotProperties()) );
161 popup->exec( point );
163 mCurrentContextMenuItem = 0;
168 void KMMimePartTree::slotSaveAs()
170 saveSelectedBodyParts(
false );
174 void KMMimePartTree::slotSaveAsEncoded()
176 saveSelectedBodyParts(
true );
180 void KMMimePartTree::saveSelectedBodyParts(
bool encoded )
182 TQPtrList<TQListViewItem> selected = selectedItems();
184 Q_ASSERT( !selected.isEmpty() );
185 if ( selected.isEmpty() )
188 TQPtrListIterator<TQListViewItem> it( selected );
189 TQPtrList<partNode> parts;
190 while ( it.current() ) {
191 parts.append(
static_cast<KMMimePartTreeItem *
>(it.current())->node() );
194 mReaderWin->setUpdateAttachment();
195 KMSaveAttachmentsCommand *command =
196 new KMSaveAttachmentsCommand(
this, parts, mReaderWin->message(), encoded );
201 void KMMimePartTree::slotSaveAll()
203 if( childCount() == 0)
206 mReaderWin->setUpdateAttachment();
208 new KMSaveAttachmentsCommand(
this, mReaderWin->message() );
213 void KMMimePartTree::setStyleDependantFrameWidth()
217 if( style().isA(
"KeramikStyle") )
218 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1;
220 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth );
221 if ( frameWidth < 0 )
223 if ( frameWidth != lineWidth() )
224 setLineWidth( frameWidth );
229 void KMMimePartTree::styleChange( TQStyle& oldStyle )
231 setStyleDependantFrameWidth();
232 TDEListView::styleChange( oldStyle );
236 void KMMimePartTree::correctSize( TQListViewItem * item )
240 TDEIO::filesize_t totalSize = 0;
241 TQListViewItem * myChild = item->firstChild();
244 totalSize +=
static_cast<KMMimePartTreeItem*
>(myChild)->origSize();
245 myChild = myChild->nextSibling();
247 if ( totalSize >
static_cast<KMMimePartTreeItem*
>(item)->origSize() )
248 item->setText( mSizeColumn, TDEIO::convertSize(totalSize) );
249 if ( item->parent() )
250 correctSize( item->parent() );
253 void KMMimePartTree::slotDelete()
255 TQPtrList<TQListViewItem> selected = selectedItems();
256 if ( selected.count() != 1 )
258 mReaderWin->slotDeleteAttachment(
static_cast<KMMimePartTreeItem*
>( selected.first() )->node() );
261 void KMMimePartTree::slotEdit()
263 TQPtrList<TQListViewItem> selected = selectedItems();
264 if ( selected.count() != 1 )
266 mReaderWin->slotEditAttachment(
static_cast<KMMimePartTreeItem*
>( selected.first() )->node() );
269 void KMMimePartTree::slotOpen()
271 startHandleAttachmentCommand( KMHandleAttachmentCommand::Open );
274 void KMMimePartTree::slotOpenWith()
276 startHandleAttachmentCommand( KMHandleAttachmentCommand::OpenWith );
279 void KMMimePartTree::slotView()
281 startHandleAttachmentCommand( KMHandleAttachmentCommand::View );
284 void KMMimePartTree::slotProperties()
286 startHandleAttachmentCommand( KMHandleAttachmentCommand::Properties );
289 void KMMimePartTree::startHandleAttachmentCommand(
int type)
291 TQPtrList<TQListViewItem> selected = selectedItems();
292 if ( selected.count() != 1 )
294 partNode* node =
static_cast<KMMimePartTreeItem*
>( selected.first() )->node();
295 TQString name = mReaderWin->tempFileUrlFromPartNode( node ).path();
296 KMHandleAttachmentCommand* command =
new KMHandleAttachmentCommand(
297 node, mReaderWin->message(), node->nodeId(), name,
298 KMHandleAttachmentCommand::AttachmentAction( type ), 0,
this );
299 connect( command, TQ_SIGNAL( showAttachment(
int,
const TQString& ) ),
300 mReaderWin, TQ_SLOT( slotAtmView(
int,
const TQString& ) ) );
304 void KMMimePartTree::slotCopy()
307 KMMimePartTreeItem *item =
static_cast<KMMimePartTreeItem*
>( currentItem() );
309 KURL url = mReaderWin->tempFileUrlFromPartNode( item->node() );
310 if ( !url.isValid() )
return;
312 KURLDrag* drag =
new KURLDrag( urls,
this );
313 TQApplication::clipboard()->setData( drag, TQClipboard::Clipboard );
317 KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTree * parent,
319 const TQString & description,
320 const TQString & mimetype,
321 const TQString & encoding,
322 TDEIO::filesize_t size )
323 : TQListViewItem( parent, description,
326 TDEIO::convertSize( size ) ),
327 mPartNode( node ), mOrigSize(size)
330 node->setMimePartTreeItem(
this );
331 setIconAndTextForType( mimetype );
333 parent->correctSize(
this);
336 KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTreeItem * parent,
338 const TQString & description,
339 const TQString & mimetype,
340 const TQString & encoding,
341 TDEIO::filesize_t size,
343 : TQListViewItem( parent, description,
346 TDEIO::convertSize( size ) ),
347 mPartNode( node ), mOrigSize(size)
349 if( revertOrder && nextSibling() ){
350 TQListViewItem* sib = nextSibling();
351 while( sib->nextSibling() )
352 sib = sib->nextSibling();
356 node->setMimePartTreeItem(
this );
357 setIconAndTextForType( mimetype );
359 static_cast<KMMimePartTree*
>(listView())->correctSize(
this);
362 void KMMimePartTreeItem::setIconAndTextForType(
const TQString & mime )
364 TQString mimetype = mime.lower();
365 if ( mimetype.startsWith(
"multipart/" ) ) {
366 setText( 1, mimetype );
367 setPixmap( 0, SmallIcon(
"folder") );
368 }
else if ( mimetype ==
"application/octet-stream" ) {
369 setText( 1, i18n(
"Unspecified Binary Data") );
370 setPixmap( 0, SmallIcon(
"unknown") );
372 KMimeType::Ptr mtp = KMimeType::mimeType( mimetype );
373 setText( 1, (mtp && !mtp->comment().isEmpty()) ? mtp->comment() : mimetype );
374 setPixmap( 0, mtp ? mtp->pixmap( TDEIcon::Small) : SmallIcon(
"unknown") );
379 void KMMimePartTree::startDrag()
382 KMMimePartTreeItem *item =
static_cast<KMMimePartTreeItem*
>( currentItem() );
384 partNode *node = item->node();
386 KURL url = mReaderWin->tempFileUrlFromPartNode( node );
387 if (!url.isValid() )
return;
389 KURLDrag* drag =
new KURLDrag( urls,
this );
393 #include "kmmimeparttree.moc"
This class implements a "reader window", that is a window used for reading or viewing messages.