typo found by Andrey Cherepanov
[kdepim.git] / akonadiconsole / jobtrackermodel.cpp
blobd0d9661cf31a6da4bb12f9f8ba8ac6c4531c7cd5
1 /*
2 This file is part of Akonadi.
4 Copyright (c) 2009 Till Adam <adam@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA.
22 #include "jobtrackermodel.h"
23 #include "jobtracker.h"
25 #include <KGlobal>
26 #include <KLocale>
27 #include <QtCore/QStringList>
28 #include <QtCore/QModelIndex>
29 #include <QtCore/QDebug>
30 #include <QtCore/QDateTime>
31 #include <QFont>
33 #include <cassert>
35 class JobTrackerModel::Private
37 public:
38 Private( const char *name, JobTrackerModel* _q )
39 :tracker( name), q(_q)
43 JobTracker tracker;
44 private:
45 JobTrackerModel* const q;
48 JobTrackerModel::JobTrackerModel( const char *name, QObject* parent )
49 :QAbstractItemModel( parent ), d( new Private( name, this ) )
51 connect( &d->tracker, SIGNAL( updated() ),
52 this, SIGNAL( modelReset() ) );
55 JobTrackerModel::~JobTrackerModel()
57 delete d;
60 QModelIndex JobTrackerModel::index(int row, int column, const QModelIndex & parent) const
62 if ( !parent.isValid() ) // session, at top level
64 if ( row < 0 || row >= d->tracker.sessions().size() )
65 return QModelIndex();
66 return createIndex( row, column, d->tracker.idForSession( d->tracker.sessions().at(row) ) );
68 // non-toplevel job
69 const QList<JobInfo> jobs = d->tracker.jobs( parent.internalId() );
70 if ( row >= jobs.size() ) return QModelIndex();
71 return createIndex( row, column, d->tracker.idForJob( jobs.at( row ).id ) );
74 QModelIndex JobTrackerModel::parent(const QModelIndex & idx) const
76 if ( !idx.isValid() ) return QModelIndex();
78 const int parentid = d->tracker.parentId( idx.internalId() );
79 if ( parentid == -1 ) return QModelIndex(); // top level session
81 const int grandparentid = d->tracker.parentId( parentid );
82 int row = -1;
83 if ( grandparentid == -1 )
85 row = d->tracker.sessions().indexOf( d->tracker.sessionForId( parentid ) );
87 else
89 // offset of the parent in the list of children of the grandparent
90 row = d->tracker.jobs( grandparentid ).indexOf( d->tracker.info( parentid ) );
92 assert( row != -1 );
93 return createIndex( row, 0, parentid );
96 int JobTrackerModel::rowCount(const QModelIndex & parent) const
98 if ( !parent.isValid() )
100 return d->tracker.sessions().size();
102 else
104 return d->tracker.jobs( parent.internalId() ).size();
108 int JobTrackerModel::columnCount(const QModelIndex & parent) const
110 Q_UNUSED( parent );
111 return 4;
114 QVariant JobTrackerModel::data(const QModelIndex & idx, int role) const
116 // top level items are sessions
117 if ( !idx.parent().isValid() )
119 if ( role == Qt::DisplayRole )
121 if( idx.column() == 0 )
123 assert(d->tracker.sessions().size() > idx.row());
124 return d->tracker.sessions().at(idx.row());
128 else // not top level, so a job or subjob
130 const int id = idx.internalId();
131 const JobInfo info = d->tracker.info( id );
132 if ( role == Qt::DisplayRole )
134 if ( idx.column() == 0 )
135 return info.id;
136 if ( idx.column() == 1 )
137 return KGlobal::locale()->formatTime( info.timestamp.time(), true )
138 + QString::fromLatin1( ".%1" ).arg( info.timestamp.time().msec(), 3, 10, QLatin1Char('0') );
139 if ( idx.column() == 2 )
140 return info.type;
141 if ( idx.column() == 3 )
142 return info.stateAsString();
144 else if ( role == Qt::ForegroundRole ) {
145 if ( info.state == JobInfo::Failed )
146 return Qt::red;
148 else if ( role == Qt::FontRole ) {
149 if ( info.state == JobInfo::Running ) {
150 QFont f;
151 f.setBold( true );
152 return f;
155 else if ( role == Qt::ToolTipRole ) {
156 if ( info.state == JobInfo::Failed )
157 return info.error;
160 return QVariant();
163 QVariant JobTrackerModel::headerData(int section, Qt::Orientation orientation, int role) const
165 if ( role == Qt::DisplayRole )
167 if ( orientation == Qt::Horizontal )
169 if ( section == 0 )
170 return QLatin1String("Job ID");
171 if ( section == 1 )
172 return QLatin1String("Timestamp");
173 if ( section == 2 )
174 return QLatin1String("Job Type");
175 if ( section == 3 )
176 return QLatin1String("State");
180 return QVariant();
183 void JobTrackerModel::resetTracker()
185 d->tracker.reset();
189 bool JobTrackerModel::isEnabled() const
191 return d->tracker.isEnabled();
194 void JobTrackerModel::setEnabled( bool on )
196 d->tracker.setEnabled( on );
199 #include "jobtrackermodel.moc"