kmail Library API Documentation

kmmimeparttree.cpp

00001 
00002 #include <config.h>
00003 
00004 #include "kmmimeparttree.h"
00005 
00006 #include "kmreaderwin.h"
00007 #include "partNode.h"
00008 #include "kmmsgpart.h"
00009 #include "kmkernel.h"
00010 #include "kmcommands.h"
00011 
00012 #include <kdebug.h>
00013 #include <klocale.h>
00014 #include <kfiledialog.h>
00015 #include <kmessagebox.h>
00016 #include <kiconloader.h>
00017 
00018 #include <qheader.h>
00019 #include <qpopupmenu.h>
00020 #include <qstyle.h>
00021 
00022 KMMimePartTree::KMMimePartTree( KMReaderWin* readerWin,
00023                                 QWidget* parent,
00024                                 const char* name )
00025     : KListView(  parent, name ),
00026       mReaderWin( readerWin ), mSizeColumn(0)
00027 {
00028     setStyleDependantFrameWidth();
00029     addColumn( i18n("Description") );
00030     addColumn( i18n("Type") );
00031     addColumn( i18n("Encoding") );
00032     mSizeColumn = addColumn( i18n("Size") );
00033     setColumnAlignment( 3, Qt::AlignRight );
00034 
00035     restoreLayoutIfPresent();
00036     connect( this, SIGNAL( clicked( QListViewItem* ) ),
00037              this, SLOT( itemClicked( QListViewItem* ) ) );
00038     connect( this, SIGNAL( contextMenuRequested( QListViewItem*,
00039                                                  const QPoint&, int ) ),
00040              this, SLOT( itemRightClicked( QListViewItem*, const QPoint& ) ) );
00041     setSelectionMode( QListView::Extended );
00042     setRootIsDecorated( false );
00043     setAllColumnsShowFocus( true );
00044     setShowToolTips( true );
00045     setSorting(-1);
00046 }
00047 
00048 
00049 static const char configGroup[] = "MimePartTree";
00050 
00051 KMMimePartTree::~KMMimePartTree() {
00052   saveLayout( KMKernel::config(), configGroup );
00053 }
00054 
00055 
00056 void KMMimePartTree::restoreLayoutIfPresent() {
00057   // first column: soaks up the rest of the space:
00058   setColumnWidthMode( 0, Manual );
00059   header()->setStretchEnabled( true, 0 );
00060   // rest of the columns:
00061   if ( KMKernel::config()->hasGroup( configGroup ) ) {
00062     // there is a saved layout. use it...
00063     restoreLayout( KMKernel::config(), configGroup );
00064     // and disable Maximum mode:
00065     for ( int i = 1 ; i < 4 ; ++i )
00066       setColumnWidthMode( i, Manual );
00067   } else {
00068     // columns grow with their contents:
00069     for ( int i = 1 ; i < 4 ; ++i )
00070       setColumnWidthMode( i, Maximum );
00071   }
00072 }
00073 
00074 
00075 void KMMimePartTree::itemClicked( QListViewItem* item )
00076 {
00077   if ( const KMMimePartTreeItem * i = dynamic_cast<KMMimePartTreeItem*>( item ) ) {
00078     if( mReaderWin->mRootNode == i->node() )
00079       mReaderWin->update( true ); // Force update
00080     else
00081       mReaderWin->setMsgPart( i->node() );
00082   } else
00083     kdWarning(5006) << "Item was not a KMMimePartTreeItem!" << endl;
00084 }
00085 
00086 
00087 void KMMimePartTree::itemRightClicked( QListViewItem* item,
00088                                        const QPoint& point )
00089 {
00090     // TODO: remove this member var?
00091     mCurrentContextMenuItem = dynamic_cast<KMMimePartTreeItem*>( item );
00092     if ( 0 == mCurrentContextMenuItem ) {
00093         kdDebug(5006) << "Item was not a KMMimePartTreeItem!" << endl;
00094     }
00095     else {
00096         kdDebug(5006) << "\n**\n** KMMimePartTree::itemRightClicked() **\n**" << endl;
00097 
00098         QPopupMenu* popup = new QPopupMenu;
00099         popup->insertItem( SmallIcon("filesaveas"),i18n( "Save &As..." ), this, SLOT( slotSaveAs() ) );
00100         popup->insertItem( i18n( "Save as &Encoded..." ), this,
00101                            SLOT( slotSaveAsEncoded() ) );
00102         popup->insertItem( i18n( "Save All Attachments..." ), this,
00103                            SLOT( slotSaveAll() ) );
00104         popup->exec( point );
00105         delete popup;
00106         mCurrentContextMenuItem = 0;
00107     }
00108 }
00109 
00110 //-----------------------------------------------------------------------------
00111 void KMMimePartTree::slotSaveAs()
00112 {
00113   saveSelectedBodyParts( false );
00114 }
00115 
00116 //-----------------------------------------------------------------------------
00117 void KMMimePartTree::slotSaveAsEncoded()
00118 {
00119   saveSelectedBodyParts( true );
00120 }
00121 
00122 //-----------------------------------------------------------------------------
00123 void KMMimePartTree::saveSelectedBodyParts( bool encoded )
00124 {
00125   QPtrList<QListViewItem> selected = selectedItems();
00126 
00127   Q_ASSERT( !selected.isEmpty() );
00128   if ( selected.isEmpty() )
00129     return;
00130 
00131   QPtrListIterator<QListViewItem> it( selected );
00132   QPtrList<partNode> parts;
00133   while ( it.current() ) {
00134     parts.append( static_cast<KMMimePartTreeItem *>(it.current())->node() );
00135     ++it;
00136   }
00137   mReaderWin->setUpdateAttachment();
00138   KMSaveAttachmentsCommand *command =
00139     new KMSaveAttachmentsCommand( this, parts, mReaderWin->message(), encoded );
00140   command->start();
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 void KMMimePartTree::slotSaveAll()
00145 {
00146     if( childCount() == 0)
00147         return;
00148 
00149     mReaderWin->setUpdateAttachment();
00150     KMCommand *command =
00151       new KMSaveAttachmentsCommand( this, mReaderWin->message() );
00152     command->start();
00153 }
00154 
00155 //-----------------------------------------------------------------------------
00156 void KMMimePartTree::setStyleDependantFrameWidth()
00157 {
00158   // set the width of the frame to a reasonable value for the current GUI style
00159   int frameWidth;
00160   if( style().isA("KeramikStyle") )
00161     frameWidth = style().pixelMetric( QStyle::PM_DefaultFrameWidth ) - 1;
00162   else
00163     frameWidth = style().pixelMetric( QStyle::PM_DefaultFrameWidth );
00164   if ( frameWidth < 0 )
00165     frameWidth = 0;
00166   if ( frameWidth != lineWidth() )
00167     setLineWidth( frameWidth );
00168 }
00169 
00170 
00171 //-----------------------------------------------------------------------------
00172 void KMMimePartTree::styleChange( QStyle& oldStyle )
00173 {
00174   setStyleDependantFrameWidth();
00175   KListView::styleChange( oldStyle );
00176 }
00177 
00178 //-----------------------------------------------------------------------------
00179 void KMMimePartTree::correctSize( QListViewItem * item )
00180 {
00181   if (!item) return;
00182 
00183   KIO::filesize_t totalSize = 0;
00184   QListViewItem * myChild = item->firstChild();
00185   while ( myChild )
00186   {
00187     totalSize += static_cast<KMMimePartTreeItem*>(myChild)->origSize();
00188     myChild = myChild->nextSibling();
00189   }
00190   if ( totalSize > static_cast<KMMimePartTreeItem*>(item)->origSize() )
00191     item->setText( mSizeColumn, KIO::convertSize(totalSize) );
00192   if ( item->parent() )
00193     correctSize( item->parent() );
00194 }
00195 
00196 //=============================================================================
00197 KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTree * parent,
00198                                         partNode* node,
00199                                         const QString & description,
00200                                         const QString & mimetype,
00201                                         const QString & encoding,
00202                                         KIO::filesize_t size )
00203   : QListViewItem( parent, description,
00204            QString::null, // set by setIconAndTextForType()
00205            encoding,
00206            KIO::convertSize( size ) ),
00207     mPartNode( node ), mOrigSize(size)
00208 {
00209   if( node )
00210     node->setMimePartTreeItem( this );
00211   setIconAndTextForType( mimetype );
00212   if ( parent )
00213     parent->correctSize(this);
00214 }
00215 
00216 KMMimePartTreeItem::KMMimePartTreeItem( KMMimePartTreeItem * parent,
00217                                         partNode* node,
00218                                         const QString & description,
00219                                         const QString & mimetype,
00220                                         const QString & encoding,
00221                                         KIO::filesize_t size,
00222                                         bool revertOrder )
00223   : QListViewItem( parent, description,
00224            QString::null, // set by setIconAndTextForType()
00225            encoding,
00226            KIO::convertSize( size ) ),
00227     mPartNode( node ), mOrigSize(size)
00228 {
00229   if( revertOrder && nextSibling() ){
00230     QListViewItem* sib = nextSibling();
00231     while( sib->nextSibling() )
00232       sib = sib->nextSibling();
00233     moveItem( sib );
00234   }
00235   if( node )
00236     node->setMimePartTreeItem( this );
00237   setIconAndTextForType( mimetype );
00238   if ( listView() )
00239     static_cast<KMMimePartTree*>(listView())->correctSize(this);
00240 }
00241 
00242 void KMMimePartTreeItem::setIconAndTextForType( const QString & mime )
00243 {
00244   QString mimetype = mime.lower();
00245   if ( mimetype.startsWith( "multipart/" ) ) {
00246     setText( 1, mimetype );
00247     setPixmap( 0, SmallIcon("folder") );
00248   } else if ( mimetype == "application/octet-stream" ) {
00249     setText( 1, i18n("Unspecified Binary Data") ); // don't show "Unknown"...
00250     setPixmap( 0, SmallIcon("unknown") );
00251   } else {
00252     KMimeType::Ptr mtp = KMimeType::mimeType( mimetype );
00253     setText( 1, (mtp && !mtp->comment().isEmpty()) ? mtp->comment() : mimetype );
00254     setPixmap( 0, mtp ? mtp->pixmap( KIcon::Small) : SmallIcon("unknown") );
00255   }
00256 }
00257 
00258 
00259 #include "kmmimeparttree.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:20:15 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003