akonadi/kmime
messagemodel.cpp
00001 /* 00002 Copyright (c) 2006 Volker Krause <vkrause@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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 00035 #include <QtCore/QDebug> 00036 00037 using namespace Akonadi; 00038 00039 class Akonadi::MessageModel::Private 00040 { 00041 public: 00042 }; 00043 00044 MessageModel::MessageModel( QObject *parent ) : 00045 ItemModel( parent ), 00046 d( new Private() ) 00047 { 00048 fetchScope().fetchPayloadPart( MessagePart::Envelope ); 00049 } 00050 00051 MessageModel::~MessageModel( ) 00052 { 00053 delete d; 00054 } 00055 00056 QStringList MessageModel::mimeTypes() const 00057 { 00058 return QStringList() 00059 << QLatin1String("text/uri-list") 00060 << QLatin1String("message/rfc822"); 00061 } 00062 00063 int MessageModel::rowCount( const QModelIndex& ) const 00064 { 00065 if ( collection().isValid() 00066 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) 00067 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) ) 00068 return 1; 00069 00070 return ItemModel::rowCount(); 00071 } 00072 00073 int MessageModel::columnCount( const QModelIndex & parent ) const 00074 { 00075 if ( collection().isValid() 00076 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) 00077 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) ) 00078 return 1; 00079 00080 if ( !parent.isValid() ) 00081 return 5; // keep in sync with the column type enum 00082 00083 return 0; 00084 } 00085 00086 QVariant MessageModel::data( const QModelIndex & index, int role ) const 00087 { 00088 if ( !index.isValid() ) 00089 return QVariant(); 00090 if ( index.row() >= rowCount() ) 00091 return QVariant(); 00092 00093 if ( !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) ) { 00094 if ( role == Qt::DisplayRole ) 00095 return i18nc( "@label", "This model can only handle email folders. The current collection holds mimetypes: %1", 00096 collection().contentMimeTypes().join( QLatin1String(",") ) ); 00097 else 00098 return QVariant(); 00099 } 00100 00101 Item item = itemForIndex( index ); 00102 if ( !item.hasPayload<MessagePtr>() ) 00103 return QVariant(); 00104 MessagePtr msg = item.payload<MessagePtr>(); 00105 if ( role == Qt::DisplayRole ) { 00106 switch ( index.column() ) { 00107 case Subject: 00108 return msg->subject()->asUnicodeString(); 00109 case Sender: 00110 return msg->from()->asUnicodeString(); 00111 case Receiver: 00112 return msg->to()->asUnicodeString(); 00113 case Date: 00114 return KGlobal::locale()->formatDateTime( msg->date()->dateTime().toLocalZone(), KLocale::FancyLongDate ); 00115 case Size: 00116 if ( item.size() == 0 ) 00117 return i18nc( "@label No size available", "-" ); 00118 else 00119 return KGlobal::locale()->formatByteSize( item.size() ); 00120 default: 00121 return QVariant(); 00122 } 00123 } else if ( role == Qt::EditRole ) { 00124 switch ( index.column() ) { 00125 case Subject: 00126 return msg->subject()->asUnicodeString(); 00127 case Sender: 00128 return msg->from()->asUnicodeString(); 00129 case Receiver: 00130 return msg->to()->asUnicodeString(); 00131 case Date: 00132 return msg->date()->dateTime().dateTime(); 00133 case Size: 00134 return item.size(); 00135 default: 00136 return QVariant(); 00137 } 00138 } 00139 return ItemModel::data( index, role ); 00140 } 00141 00142 QVariant MessageModel::headerData( int section, Qt::Orientation orientation, int role ) const 00143 { 00144 00145 if ( collection().isValid() 00146 && !collection().contentMimeTypes().contains( QLatin1String("message/rfc822") ) 00147 && collection().contentMimeTypes() != QStringList( QLatin1String("inode/directory") ) ) 00148 return QVariant(); 00149 00150 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) { 00151 switch ( section ) { 00152 case Subject: 00153 return i18nc( "@title:column, message (e.g. email) subject", "Subject" ); 00154 case Sender: 00155 return i18nc( "@title:column, sender of message (e.g. email)", "Sender" ); 00156 case Receiver: 00157 return i18nc( "@title:column, receiver of message (e.g. email)", "Receiver" ); 00158 case Date: 00159 return i18nc( "@title:column, message (e.g. email) timestamp", "Date" ); 00160 case Size: 00161 return i18nc( "@title:column, message (e.g. email) size", "Size" ); 00162 default: 00163 return QString(); 00164 } 00165 } 00166 return ItemModel::headerData( section, orientation, role ); 00167 } 00168 00169 #include "messagemodel.moc"