2 * Copyright (C) 2016 Canonical Ltd.
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.
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.
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/>.
20#include <glib-object.h>
22#include "LocalePlugin.h"
23#include "timezonemodel.h"
25TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
26 QAbstractListModel(parent),
27 m_listUpdating(false),
28 m_cancellable(nullptr)
30 m_roleNames[Qt::DisplayRole] = "displayName";
31 m_roleNames[TimeZoneRole] = "timeZone";
32 m_roleNames[CityRole] = "city";
33 m_roleNames[CountryRole] = "country";
34 m_roleNames[OffsetRole] = "offset";
35 m_roleNames[LatitudeRole] = "latitude";
36 m_roleNames[LongitudeRole] = "longitude";
39int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
41 if (parent.isValid()) {
43 } else if (m_filter.isEmpty()) {
44 return m_countryLocations.count();
46 return m_locations.count();
50QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
53 if (m_filter.isEmpty()) {
54 city = m_countryLocations.value(index.row());
56 city = m_locations.value(index.row());
63 return QStringLiteral("%1, %2, %3").arg(geonames_city_get_name(city),
64 geonames_city_get_state(city),
65 geonames_city_get_country(city));
67 return QStringLiteral("%1, %2").arg(geonames_city_get_name(city),
68 geonames_city_get_country(city));
70 return geonames_city_get_timezone(city);
72 return geonames_city_get_country(city);
74 return geonames_city_get_name(city);
76 QTimeZone tmp(geonames_city_get_timezone(city));
77 return static_cast<double>(tmp.standardTimeOffset(QDateTime::currentDateTime())) / 3600;
80 return geonames_city_get_latitude(city);
82 return geonames_city_get_longitude(city);
84 qWarning() << Q_FUNC_INFO << "Unknown role";
89QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
94void TimeZoneLocationModel::setModel(const QList<GeonamesCity *> &locations)
98 Q_FOREACH(GeonamesCity *city, m_locations) {
99 geonames_city_free(city);
102 m_locations = locations;
106void TimeZoneLocationModel::filterFinished(GObject *source_object,
110 Q_UNUSED(source_object);
112 g_autofree gint *cities = nullptr;
113 guint cities_len = 0;
114 g_autoptr(GError) error = nullptr;
116 cities = geonames_query_cities_finish(res, &cities_len, &error);
118 if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
119 TimeZoneLocationModel *model = static_cast<TimeZoneLocationModel *>(user_data);
120 g_clear_object(&model->m_cancellable);
121 model->setListUpdating(false);
122 qWarning() << "Could not filter timezones:" << error->message;
127 QList<GeonamesCity *> locations;
129 for (guint i = 0; i < cities_len; ++i) {
130 GeonamesCity *city = geonames_get_city(cities[i]);
132 locations.append(city);
136 TimeZoneLocationModel *model = static_cast<TimeZoneLocationModel *>(user_data);
138 g_clear_object(&model->m_cancellable);
140 model->setModel(locations);
141 model->setListUpdating(false);
144bool TimeZoneLocationModel::listUpdating() const
146 return m_listUpdating;
149void TimeZoneLocationModel::setListUpdating(bool listUpdating)
151 if (m_listUpdating != listUpdating) {
152 m_listUpdating = listUpdating;
153 Q_EMIT listUpdatingChanged();
157QString TimeZoneLocationModel::filter() const
162void TimeZoneLocationModel::setFilter(const QString &filter)
164 if (filter != m_filter) {
166 Q_EMIT filterChanged();
169 setListUpdating(true);
172 g_cancellable_cancel(m_cancellable);
173 g_clear_object(&m_cancellable);
176 setModel(QList<GeonamesCity *>());
178 if (filter.isEmpty()) {
179 setListUpdating(false);
183 m_cancellable = g_cancellable_new();
184 geonames_query_cities(filter.toUtf8().data(),
185 GEONAMES_QUERY_DEFAULT,
191QString TimeZoneLocationModel::country() const
196static bool citycmp(GeonamesCity *a, GeonamesCity *b)
198 return geonames_city_get_population(b) < geonames_city_get_population(a);
201void TimeZoneLocationModel::setCountry(const QString &country)
203 if (m_country == country)
210 Q_FOREACH(GeonamesCity *city, m_countryLocations) {
211 geonames_city_free(city);
213 m_countryLocations.clear();
215 gint num_cities = geonames_get_n_cities();
216 for (gint i = 0; i < num_cities; i++) {
217 GeonamesCity *city = geonames_get_city(i);
218 if (city && m_country == geonames_city_get_country_code(city)) {
219 m_countryLocations.append(city);
223 std::sort(m_countryLocations.begin(), m_countryLocations.end(), citycmp);
227 Q_EMIT countryChanged(country);
230TimeZoneLocationModel::~TimeZoneLocationModel()
233 g_cancellable_cancel(m_cancellable);
234 g_clear_object(&m_cancellable);
237 Q_FOREACH(GeonamesCity *city, m_countryLocations) {
238 geonames_city_free(city);
241 Q_FOREACH(GeonamesCity *city, m_locations) {
242 geonames_city_free(city);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list