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,
22 #include "jobtrackermodel.h"
23 #include "jobtracker.h"
27 #include <QtCore/QStringList>
28 #include <QtCore/QModelIndex>
29 #include <QtCore/QDebug>
30 #include <QtCore/QDateTime>
35 class JobTrackerModel::Private
38 Private( const char *name
, JobTrackerModel
* _q
)
39 :tracker( name
), q(_q
)
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()
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() )
66 return createIndex( row
, column
, d
->tracker
.idForSession( d
->tracker
.sessions().at(row
) ) );
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
);
83 if ( grandparentid
== -1 )
85 row
= d
->tracker
.sessions().indexOf( d
->tracker
.sessionForId( parentid
) );
89 // offset of the parent in the list of children of the grandparent
90 row
= d
->tracker
.jobs( grandparentid
).indexOf( d
->tracker
.info( parentid
) );
93 return createIndex( row
, 0, parentid
);
96 int JobTrackerModel::rowCount(const QModelIndex
& parent
) const
98 if ( !parent
.isValid() )
100 return d
->tracker
.sessions().size();
104 return d
->tracker
.jobs( parent
.internalId() ).size();
108 int JobTrackerModel::columnCount(const QModelIndex
& parent
) const
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 )
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 )
141 if ( idx
.column() == 3 )
142 return info
.stateAsString();
144 else if ( role
== Qt::ForegroundRole
) {
145 if ( info
.state
== JobInfo::Failed
)
148 else if ( role
== Qt::FontRole
) {
149 if ( info
.state
== JobInfo::Running
) {
155 else if ( role
== Qt::ToolTipRole
) {
156 if ( info
.state
== JobInfo::Failed
)
163 QVariant
JobTrackerModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
165 if ( role
== Qt::DisplayRole
)
167 if ( orientation
== Qt::Horizontal
)
170 return QLatin1String("Job ID");
172 return QLatin1String("Timestamp");
174 return QLatin1String("Job Type");
176 return QLatin1String("State");
183 void JobTrackerModel::resetTracker()
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"