2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 #include "notificationmodel.h"
21 #include "notificationmanagerinterface.h"
23 #include <akonadi/private/imapparser_p.h>
28 #include <QDBusConnection>
30 using Akonadi::NotificationMessage
;
32 NotificationModel::NotificationBlock::NotificationBlock( const NotificationMessage::List
&_msgs
) :
35 timestamp
= QDateTime::currentDateTime();
38 NotificationModel::NotificationModel(QObject
* parent
) :
39 QAbstractItemModel( parent
),
42 NotificationMessage::registerDBusTypes();
44 org::freedesktop::Akonadi::NotificationManager
* nm
45 = new org::freedesktop::Akonadi::NotificationManager( QLatin1String( "org.freedesktop.Akonadi" ),
46 QLatin1String( "/notifications" ),
47 QDBusConnection::sessionBus(), this );
49 kWarning( 5250 ) << "Unable to connect to notification manager";
51 connect( nm
, SIGNAL(notify(Akonadi::NotificationMessage::List
)), this, SLOT(slotNotify(Akonadi::NotificationMessage::List
)) );
55 int NotificationModel::columnCount(const QModelIndex
& parent
) const
61 int NotificationModel::rowCount(const QModelIndex
& parent
) const
63 if ( !parent
.isValid() )
65 if ( parent
.internalId() >= 0 )
67 if ( parent
.row() < m_data
.size() ) {
68 const NotificationBlock block
= m_data
.at( parent
.row() );
69 return block
.msgs
.count();
74 QModelIndex
NotificationModel::index(int row
, int column
, const QModelIndex
& parent
) const
76 if ( !parent
.isValid() ) {
77 if ( row
< 0 || row
>= m_data
.size() )
79 return createIndex( row
, column
, -1 );
82 if ( parent
.row() < 0 || parent
.row() >= m_data
.size() )
84 const NotificationBlock block
= m_data
.at( parent
.row() );
85 if ( row
>= block
.msgs
.size() )
87 return createIndex( row
, column
, parent
.row() );
90 QModelIndex
NotificationModel::parent(const QModelIndex
& child
) const
92 if ( !child
.isValid() || child
.internalId() < 0 || child
.internalId() > m_data
.count() )
94 return createIndex( child
.internalId(), 0, -1 );
97 QVariant
NotificationModel::data(const QModelIndex
& index
, int role
) const
99 if ( index
.parent().isValid() ) {
100 if ( index
.parent().row() < 0 || index
.parent().row() >= m_data
.size() )
102 const NotificationBlock block
= m_data
.at( index
.parent().row() );
103 if ( index
.row() < 0 || index
.row() >= block
.msgs
.size() )
105 const NotificationMessage msg
= block
.msgs
.at( index
.row() );
106 if ( role
== Qt::DisplayRole
) {
107 switch ( index
.column() ) {
110 switch ( msg
.operation() ) {
111 case NotificationMessage::Add
: return QString( "Add" );
112 case NotificationMessage::Modify
: return QString( "Modify" );
113 case NotificationMessage::Move
: return QString( "Move" );
114 case NotificationMessage::Remove
: return QString( "Delete" );
115 default: return QString( "Invalid" );
120 switch ( msg
.type() ) {
121 case NotificationMessage::Collection
: return QString( "Collection" );
122 case NotificationMessage::Item
: return QString( "Item" );
123 default: return QString( "Invalid" );
127 return QString::number( msg
.uid() );
129 return msg
.remoteId();
131 return QString::fromUtf8( msg
.sessionId() );
133 return QString::fromUtf8( msg
.resource() );
135 return msg
.mimeType();
137 return QString::number( msg
.parentCollection() );
139 return QString::number( msg
.parentDestCollection() );
141 return QString::fromUtf8( Akonadi::ImapParser::join( msg
.itemParts(), ", " ) );
145 if ( index
.row() < 0 || index
.row() >= m_data
.size() )
147 const NotificationBlock block
= m_data
.at( index
.row() );
148 if ( role
== Qt::DisplayRole
) {
149 if ( index
.column() == 0 ) {
150 return KGlobal::locale()->formatTime( block
.timestamp
.time(), true )
151 + QString::fromLatin1( ".%1" ).arg( block
.timestamp
.time().msec(), 3, 10, QLatin1Char('0') );
152 } else if ( index
.column() == 1 ) {
153 return block
.msgs
.size();
160 QVariant
NotificationModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
162 if ( role
== Qt::DisplayRole
&& orientation
== Qt::Horizontal
) {
164 case 0: return QString( "Operation" );
165 case 1: return QString( "Type" );
166 case 2: return QString( "UID" );
167 case 3: return QString( "RID" );
168 case 4: return QString( "Session" );
169 case 5: return QString( "Resource" );
170 case 6: return QString( "Mimetype" );
171 case 7: return QString( "Parent" );
172 case 8: return QString( "Destination" );
173 case 9: return QString( "Parts" );
176 return QAbstractItemModel::headerData(section
, orientation
, role
);
180 void NotificationModel::slotNotify(const Akonadi::NotificationMessage::List
& msgs
)
184 beginInsertRows( QModelIndex(), m_data
.size(), m_data
.size() );
185 m_data
.append( NotificationBlock( msgs
) );
189 void NotificationModel::clear()
196 #include "notificationmodel.moc"