akonadi
messagemodel.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "messagemodel.h"
00021 #include "messageparts.h"
00022
00023 #include <akonadi/itemfetchscope.h>
00024 #include <akonadi/monitor.h>
00025 #include <akonadi/session.h>
00026
00027 #include <kmime/kmime_message.h>
00028 #include <boost/shared_ptr.hpp>
00029 typedef boost::shared_ptr<KMime::Message> MessagePtr;
00030
00031 #include <kdebug.h>
00032 #include <kglobal.h>
00033 #include <klocale.h>
00034 #include <kio/job.h>
00035
00036 #include <QtCore/QDebug>
00037
00038 using namespace Akonadi;
00039
00040 class Akonadi::MessageModel::Private
00041 {
00042 public:
00043 };
00044
00045 MessageModel::MessageModel( QObject *parent ) :
00046 ItemModel( parent ),
00047 d( new Private() )
00048 {
00049 fetchScope().fetchPayloadPart( MessagePart::Envelope );
00050 }
00051
00052 MessageModel::~MessageModel( )
00053 {
00054 delete d;
00055 }
00056
00057 QStringList MessageModel::mimeTypes() const
00058 {
00059 return QStringList()
00060 << QLatin1String("text/uri-list")
00061 << QLatin1String("message/rfc822");
00062 }
00063
00064 int MessageModel::rowCount( const QModelIndex & parent ) const
00065 {
00066 if ( collection().isValid()
00067 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
00068 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
00069 return 1;
00070
00071 return ItemModel::rowCount();
00072 }
00073
00074 int MessageModel::columnCount( const QModelIndex & parent ) const
00075 {
00076 if ( collection().isValid()
00077 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
00078 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
00079 return 1;
00080
00081 if ( !parent.isValid() )
00082 return 5;
00083
00084 return 0;
00085 }
00086
00087 QVariant MessageModel::data( const QModelIndex & index, int role ) const
00088 {
00089 if ( !index.isValid() )
00090 return QVariant();
00091 if ( index.row() >= rowCount() )
00092 return QVariant();
00093
00094 if ( !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) ) {
00095 if ( role == Qt::DisplayRole )
00096
00097 return QString::fromLatin1( "This model can only handle email folders. The current collection holds mimetypes: %1").arg(
00098 collection().contentMimeTypes().join( QLatin1String(",") ) );
00099 else
00100 return QVariant();
00101 }
00102
00103 Item item = itemForIndex( index );
00104 if ( !item.hasPayload<MessagePtr>() )
00105 return QVariant();
00106 MessagePtr msg = item.payload<MessagePtr>();
00107 if ( role == Qt::DisplayRole ) {
00108 switch ( index.column() ) {
00109 case Subject:
00110 return msg->subject()->asUnicodeString();
00111 case Sender:
00112 return msg->from()->asUnicodeString();
00113 case Receiver:
00114 return msg->to()->asUnicodeString();
00115 case Date:
00116 return KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate );
00117 case Size:
00118 if ( item.size() == 0 )
00119 return i18nc( "No size available", "-" );
00120 else
00121 return KIO::convertSize( KIO::filesize_t( item.size() ) );
00122 default:
00123 return QVariant();
00124 }
00125 } else if ( role == Qt::EditRole ) {
00126 switch ( index.column() ) {
00127 case Subject:
00128 return msg->subject()->asUnicodeString();
00129 case Sender:
00130 return msg->from()->asUnicodeString();
00131 case Receiver:
00132 return msg->to()->asUnicodeString();
00133 case Date:
00134 return msg->date()->dateTime().dateTime();
00135 case Size:
00136 return item.size();
00137 default:
00138 return QVariant();
00139 }
00140 }
00141 return ItemModel::data( index, role );
00142 }
00143
00144 QVariant MessageModel::headerData( int section, Qt::Orientation orientation, int role ) const
00145 {
00146
00147 if ( collection().isValid()
00148 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") )
00149 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) )
00150 return QVariant();
00151
00152 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
00153 switch ( section ) {
00154 case Subject:
00155 return i18nc( "@title:column, message (e.g. email) subject", "Subject" );
00156 case Sender:
00157 return i18nc( "@title:column, sender of message (e.g. email)", "Sender" );
00158 case Receiver:
00159 return i18nc( "@title:column, receiver of message (e.g. email)", "Receiver" );
00160 case Date:
00161 return i18nc( "@title:column, message (e.g. email) timestamp", "Date" );
00162 case Size:
00163 return i18nc( "@title:column, message (e.g. email) size", "Size" );
00164 default:
00165 return QString();
00166 }
00167 }
00168 return ItemModel::headerData( section, orientation, role );
00169 }
00170
00171 #include "messagemodel.moc"