@@ -0,0 +1,94 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Lukáš Tinkl <lukas.tinkl@canonical.com>
17+ */
18+
19+#include <QTimeZone>
20+#include <QDebug>
21+
22+#include "timezonemodel.h"
23+
24+
25+TimeZoneModel::TimeZoneModel(QObject *parent)
26+ : QAbstractListModel(parent)
27+{
28+ QMetaObject::invokeMethod(this, "init");
29+}
30+
31+QString TimeZoneModel::selectedZoneId() const
32+{
33+ return m_selectedZoneId;
34+}
35+
36+void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
37+{
38+ if (m_selectedZoneId == selectedZoneId)
39+ return;
40+
41+ m_selectedZoneId = selectedZoneId;
42+ Q_EMIT selectedZoneIdChanged(selectedZoneId);
43+}
44+
45+int TimeZoneModel::rowCount(const QModelIndex &parent) const
46+{
47+ Q_UNUSED(parent)
48+ return m_zoneIds.count();
49+}
50+
51+QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
52+{
53+ if (index.isValid()) {
54+ const QByteArray tzid = m_zoneIds.at(index.row());
55+ QTimeZone tz(tzid);
56+
57+ if (!tz.isValid()) {
58+ qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
59+ return QVariant();
60+ }
61+
62+ switch (role) {
63+ case IdRole:
64+ return tz.id();
65+ case Abbreviation:
66+ return tz.abbreviation(QDateTime::currentDateTime());
67+ case Country:
68+ return QLocale::countryToString(tz.country());
69+ case City: {
70+ const QString cityName = QString::fromUtf8(tzid.split('/').last());
71+ return cityName;
72+ }
73+ case Comment:
74+ return tz.comment();
75+ default:
76+ qWarning() << Q_FUNC_INFO << "Unsupported data role";
77+ break;
78+ }
79+ }
80+
81+ return QVariant();
82+}
83+
84+QHash<int, QByteArray> TimeZoneModel::roleNames() const
85+{
86+ return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}};
87+}
88+
89+void TimeZoneModel::init()
90+{
91+ beginResetModel();
92+ m_zoneIds = QTimeZone::availableTimeZoneIds();
93+ endResetModel();
94+}