on c2977cb help sign in
unity8/plugins/Wizard/timezonemodel.cpp
229 lines 6.6 kB history raw
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 */
17#include <QDebug>
19#include <glib.h>
20#include <glib-object.h>
21#include <timezonemap/tz.h>
23#include "LocalePlugin.h"
24#include "timezonemodel.h"
27TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
28 QAbstractListModel(parent),
29 m_workerThread(new TimeZonePopulateWorker())
31 qRegisterMetaType<TzLocation>();
33 m_roleNames[Qt::DisplayRole] = "displayName";
34 m_roleNames[TimeZoneRole] = "timeZone";
35 m_roleNames[CityRole] = "city";
36 m_roleNames[CountryRole] = "country";
37 m_roleNames[OffsetRole] = "offset";
38 m_roleNames[LatitudeRole] = "latitude";
39 m_roleNames[LongitudeRole] = "longitude";
41 QObject::connect(m_workerThread,
42 &TimeZonePopulateWorker::resultReady,
43 this,
44 &TimeZoneLocationModel::processModelResult);
45 QObject::connect(m_workerThread,
46 &TimeZonePopulateWorker::finished,
47 this,
48 &TimeZoneLocationModel::store);
49 QObject::connect(m_workerThread,
50 &TimeZonePopulateWorker::finished,
51 m_workerThread,
52 &QObject::deleteLater);
54 init();
57void TimeZoneLocationModel::init()
59 beginResetModel();
60 m_workerThread->start();
63void TimeZoneLocationModel::store()
65 m_workerThread = nullptr;
66 endResetModel();
69void TimeZoneLocationModel::processModelResult(const TzLocation &location)
71 m_locations.append(location);
74int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
76 if (parent.isValid())
77 return 0;
78 return m_locations.count();
81QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
83 if (index.row() >= m_locations.count() || index.row() < 0)
84 return QVariant();
86 const TzLocation tz = m_locations.at(index.row());
88 const QString country(tz.full_country.isEmpty() ? tz.country : tz.full_country);
90 switch (role) {
91 case Qt::DisplayRole:
92 if (!tz.state.isEmpty())
93 return QStringLiteral("%1, %2, %3").arg(tz.city).arg(tz.state).arg(country);
94 else
95 return QStringLiteral("%1, %2").arg(tz.city).arg(country);
96 case SimpleRole:
97 return QStringLiteral("%1, %2").arg(tz.city).arg(country);
98 case TimeZoneRole:
99 return tz.timezone;
100 case CountryRole:
101 return tz.country;
102 case CityRole:
103 return tz.city;
104 case OffsetRole: {
105 QTimeZone tmp(tz.timezone.toLatin1());
106 return static_cast<double>(tmp.standardTimeOffset(QDateTime::currentDateTime())) / 3600;
107 }
108 case LatitudeRole:
109 return tz.latitude;
110 case LongitudeRole:
111 return tz.longitude;
112 default:
113 qWarning() << Q_FUNC_INFO << "Unknown role";
114 return QVariant();
115 }
118QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
120 return m_roleNames;
123void TimeZonePopulateWorker::run()
125 buildCityMap();
128void TimeZonePopulateWorker::buildCityMap()
130 TzDB *tzdb = tz_load_db();
131 GPtrArray *tz_locations = tz_get_locations(tzdb);
133 TimeZoneLocationModel::TzLocation tmpTz;
135 for (guint i = 0; i < tz_locations->len; ++i) {
136 auto tmp = static_cast<CcTimezoneLocation *>(g_ptr_array_index(tz_locations, i));
137 gchar *en_name, *country, *zone, *state, *full_country;
138 gdouble latitude;
139 gdouble longitude;
140 g_object_get (tmp, "en_name", &en_name,
141 "country", &country,
142 "zone", &zone,
143 "state", &state,
144 "full_country", &full_country,
145 "latitude", &latitude,
146 "longitude", &longitude,
147 nullptr);
148 // There are empty entries in the DB
149 if (g_strcmp0(en_name, "") != 0) {
150 tmpTz.city = en_name;
151 tmpTz.country = country;
152 tmpTz.timezone = zone;
153 tmpTz.state = state;
154 tmpTz.full_country = full_country;
155 tmpTz.latitude = latitude;
156 tmpTz.longitude = longitude;
158 Q_EMIT (resultReady(tmpTz));
159 }
160 g_free (en_name);
161 g_free (country);
162 g_free (zone);
163 g_free (state);
164 g_free (full_country);
165 }
167 g_ptr_array_free (tz_locations, TRUE);
168 tz_db_free(tzdb);
172TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
173 : QSortFilterProxyModel(parent)
175 setDynamicSortFilter(false);
176 setSortLocaleAware(true);
177 setSortRole(TimeZoneLocationModel::CityRole);
178 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
179 sort(0);
182bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
184 if (!sourceModel()) {
185 return true;
186 }
188 if (!m_filter.isEmpty()) { // filtering by freeform text input, cf setFilter(QString)
189 const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CityRole).toString();
191 if (m_stringMatcher.indexIn(city) == 0) { // match at the beginning of the city name
192 return true;
193 }
194 } else if (!m_country.isEmpty()) { // filter by country code
195 const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
196 return m_country.compare(countryCode, Qt::CaseInsensitive) == 0;
197 }
199 return false;
202QString TimeZoneFilterModel::filter() const
204 return m_filter;
207void TimeZoneFilterModel::setFilter(const QString &filter)
209 if (filter != m_filter) {
210 m_filter = filter;
211 m_stringMatcher.setPattern(m_filter);
212 Q_EMIT filterChanged();
213 invalidate();
214 }
217QString TimeZoneFilterModel::country() const
219 return m_country;
222void TimeZoneFilterModel::setCountry(const QString &country)
224 if (m_country == country)
225 return;
227 m_country = country;
228 Q_EMIT countryChanged(country);