1/****************************************************************************
2**
3** Copyright (C) 2009 Tony Murray <murraytony@gmail.com>
4**
5** This file is part of KDE.
6**
7** This program is free software; you can redistribute it and/or modify
8** it under the terms of the GNU General Public License as published by
9** the Free Software Foundation; either version 2 of the License, or
10** (at your option) any later version.
11**
12** This program is distributed in the hope that it will be useful,
13** but WITHOUT ANY WARRANTY; without even the implied warranty of
14** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15** GNU General Public License for more details.
16**
17** You should have received a copy of the GNU General Public License
18** along with this program; see the file COPYING. If not, write to
19** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20** Boston, MA 02110-1301, USA.
21**
22****************************************************************************/
23
24#include "connectiondelegate.h"
25#include "remotedesktopsmodel.h"
26
27#include <KDateTime>
28#include <KDebug>
29#include <KIcon>
30#include <KIconLoader>
31#include <KLocale>
32
33ConnectionDelegate::ConnectionDelegate(QObject *parent) :
34 QStyledItemDelegate(parent)
35{
36}
37
38QString ConnectionDelegate::displayText(const QVariant &value, const QLocale& locale) const
39{
40 if (value.type() == QVariant::DateTime) {
41 KDateTime lastConnected = KDateTime(value.toDateTime());
42 KDateTime currentTime = KDateTime::currentUtcDateTime();
43
44 int daysAgo = lastConnected.daysTo(currentTime);
45 if (daysAgo <= 1 && lastConnected.secsTo(currentTime) < 86400) {
46 int minutesAgo = lastConnected.secsTo(currentTime) / 60;
47 int hoursAgo = minutesAgo / 60;
48 if (hoursAgo < 1) {
49 if (minutesAgo < 1)
50 return i18n("Less than a minute ago");
51 return i18np("A minute ago", "%1 minutes ago", minutesAgo);
52 } else {
53 return i18np("An hour ago", "%1 hours ago", hoursAgo);
54 }
55 } else { // 1 day or more
56 if (daysAgo < 30)
57 return i18np("Yesterday", "%1 days ago", daysAgo);
58 if (daysAgo < 365)
59 return i18np("Over a month ago", "%1 months ago", daysAgo / 30);
60 return i18np("A year ago", "%1 years ago", daysAgo / 365);
61 }
62
63 }
64 // These aren't the strings you're looking for, move along.
65 return QStyledItemDelegate::displayText(value, locale);
66}
67
68void ConnectionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
69{
70 if (index.column() == RemoteDesktopsModel::Favorite) {
71 QVariant value = index.data(Qt::CheckStateRole);
72 if (value.isValid()) {
73 Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt());
74 KIcon favIcon = KIcon("bookmarks");
75 KIcon::Mode mode = (checkState == Qt::Checked) ? KIcon::Active : KIcon::Disabled;
76 favIcon.paint(painter, option.rect, option.decorationAlignment, mode);
77
78 }
79 } else {
80 QStyledItemDelegate::paint(painter, option, index);
81 }
82}
83
84QSize ConnectionDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
85{
86 if (index.column() == RemoteDesktopsModel::Favorite)
87 return QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
88 return QStyledItemDelegate::sizeHint(option, index);
89}
90