1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* Copyright (C) 2013-2014 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Iain Lane <iain.lane@canonical.com>
*
*/
#include "timezonelocationmodel.h"
#include <glib.h>
#include <glib-object.h>
#include <QDebug>
TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
QAbstractTableModel(parent),
modelUpdating(false),
m_cancellable(nullptr)
{
}
void TimeZoneLocationModel::setModel(const QList<GeonamesCity *> &locations)
{
beginResetModel();
Q_FOREACH(GeonamesCity *city, m_locations) {
geonames_city_free(city);
}
m_locations = locations;
endResetModel();
}
int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_locations.count();
}
int TimeZoneLocationModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 3; //TZ, City, Country
}
QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= m_locations.count() ||
index.row() < 0)
return QVariant();
GeonamesCity *city = m_locations[index.row()];
switch (role) {
case Qt::DisplayRole:
return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
.arg(geonames_city_get_state(city))
.arg(geonames_city_get_country(city)));
break;
case SimpleRole:
return QVariant(QString("%1, %2").arg(geonames_city_get_name(city))
.arg(geonames_city_get_country(city)));
break;
case TimeZoneRole:
return geonames_city_get_timezone(city);
break;
case CountryRole:
return geonames_city_get_country(city);
break;
case CityRole:
return geonames_city_get_name(city);
break;
default:
return QVariant();
break;
}
throw "Unreachable code";
}
QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
{
QHash<int, QByteArray> m_roleNames;
m_roleNames[Qt::DisplayRole] = "displayName";
m_roleNames[TimeZoneRole] = "timeZone";
m_roleNames[CityRole] = "city";
m_roleNames[CountryRole] = "country";
return m_roleNames;
}
void TimeZoneLocationModel::filterFinished(GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
Q_UNUSED(source_object);
g_autofree gint *cities = nullptr;
guint cities_len = 0;
g_autoptr(GError) error = nullptr;
cities = geonames_query_cities_finish(res, &cities_len, &error);
if (error) {
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
TimeZoneLocationModel *model = static_cast<TimeZoneLocationModel *>(user_data);
g_clear_object(&model->m_cancellable);
qWarning() << "Could not filter timezones:" << error->message;
}
return;
}
QList<GeonamesCity *> locations;
for (guint i = 0; i < cities_len; ++i) {
GeonamesCity *city = geonames_get_city(cities[i]);
if (city) {
locations.append(city);
}
}
TimeZoneLocationModel *model = static_cast<TimeZoneLocationModel *>(user_data);
g_clear_object(&model->m_cancellable);
model->setModel(locations);
model->modelUpdating = false;
Q_EMIT model->filterComplete();
}
void TimeZoneLocationModel::filter(const QString& pattern)
{
modelUpdating = true;
Q_EMIT filterBegin();
if (m_cancellable) {
g_cancellable_cancel(m_cancellable);
g_clear_object(&m_cancellable);
}
setModel(QList<GeonamesCity *>());
if (pattern.isEmpty()) {
modelUpdating = false;
Q_EMIT filterComplete();
return;
}
m_cancellable = g_cancellable_new();
geonames_query_cities(pattern.toUtf8().data(),
GEONAMES_QUERY_DEFAULT,
m_cancellable,
filterFinished,
this);
}
TimeZoneLocationModel::~TimeZoneLocationModel()
{
if (m_cancellable) {
g_cancellable_cancel(m_cancellable);
g_clear_object(&m_cancellable);
}
Q_FOREACH(GeonamesCity *city, m_locations) {
geonames_city_free(city);
}
}
|