New Appointment -> New Task
[kdepim.git] / akonadiconsole / notificationmodel.cpp
blobf6ed85c6ce6ce11a5085d7a2b63c43ac72f0f582
1 /*
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,
17 USA.
20 #include "notificationmodel.h"
21 #include "notificationmanagerinterface.h"
23 #include <akonadi/private/imapparser_p.h>
25 #include <KDebug>
26 #include <KGlobal>
27 #include <KLocale>
28 #include <QDBusConnection>
30 using Akonadi::NotificationMessage;
32 NotificationModel::NotificationBlock::NotificationBlock( const NotificationMessage::List &_msgs ) :
33 msgs( _msgs )
35 timestamp = QDateTime::currentDateTime();
38 NotificationModel::NotificationModel(QObject* parent) :
39 QAbstractItemModel( parent ),
40 m_enabled( true )
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 );
48 if ( !nm ) {
49 kWarning( 5250 ) << "Unable to connect to notification manager";
50 } else {
51 connect( nm, SIGNAL(notify(Akonadi::NotificationMessage::List)), this, SLOT(slotNotify(Akonadi::NotificationMessage::List)) );
55 int NotificationModel::columnCount(const QModelIndex& parent) const
57 Q_UNUSED( parent );
58 return 10;
61 int NotificationModel::rowCount(const QModelIndex& parent) const
63 if ( !parent.isValid() )
64 return m_data.size();
65 if ( parent.internalId() >= 0 )
66 return 0;
67 if ( parent.row() < m_data.size() ) {
68 const NotificationBlock block = m_data.at( parent.row() );
69 return block.msgs.count();
71 return 0;
74 QModelIndex NotificationModel::index(int row, int column, const QModelIndex& parent) const
76 if ( !parent.isValid() ) {
77 if ( row < 0 || row >= m_data.size() )
78 return QModelIndex();
79 return createIndex( row, column, -1 );
82 if ( parent.row() < 0 || parent.row() >= m_data.size() )
83 return QModelIndex();
84 const NotificationBlock block = m_data.at( parent.row() );
85 if ( row >= block.msgs.size() )
86 return QModelIndex();
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() )
93 return QModelIndex();
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() )
101 return QVariant();
102 const NotificationBlock block = m_data.at( index.parent().row() );
103 if ( index.row() < 0 || index.row() >= block.msgs.size() )
104 return QVariant();
105 const NotificationMessage msg = block.msgs.at( index.row() );
106 if ( role == Qt::DisplayRole ) {
107 switch ( index.column() ) {
108 case 0:
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" );
118 case 1:
120 switch ( msg.type() ) {
121 case NotificationMessage::Collection: return QString( "Collection" );
122 case NotificationMessage::Item: return QString( "Item" );
123 default: return QString( "Invalid" );
126 case 2:
127 return QString::number( msg.uid() );
128 case 3:
129 return msg.remoteId();
130 case 4:
131 return QString::fromUtf8( msg.sessionId() );
132 case 5:
133 return QString::fromUtf8( msg.resource() );
134 case 6:
135 return msg.mimeType();
136 case 7:
137 return QString::number( msg.parentCollection() );
138 case 8:
139 return QString::number( msg.parentDestCollection() );
140 case 9:
141 return QString::fromUtf8( Akonadi::ImapParser::join( msg.itemParts(), ", " ) );
144 } else {
145 if ( index.row() < 0 || index.row() >= m_data.size() )
146 return QVariant();
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();
157 return QVariant();
160 QVariant NotificationModel::headerData(int section, Qt::Orientation orientation, int role) const
162 if ( role == Qt::DisplayRole && orientation == Qt::Horizontal ) {
163 switch ( section ) {
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)
182 if ( !m_enabled )
183 return;
184 beginInsertRows( QModelIndex(), m_data.size(), m_data.size() );
185 m_data.append( NotificationBlock( msgs ) );
186 endInsertRows();
189 void NotificationModel::clear()
191 m_data.clear();
192 reset();
196 #include "notificationmodel.moc"