@@ -14,94 +14,141 @@
1414 * with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include <QTimeZone>
1817#include <QDebug>
1918
19+#include <glib.h>
20+#include <glib-object.h>
21+#include <timezonemap/tz.h>
22+
2023#include "LocalePlugin.h"
2124#include "timezonemodel.h"
2225
2326
24−TimeZoneModel::TimeZoneModel(QObject *parent)
25− : QAbstractListModel(parent)
27+TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
28+ QAbstractListModel(parent),
29+ m_workerThread(new TimeZonePopulateWorker())
2630{
27− m_roleNames = {{IdRole, "id"},
28− {Abbreviation, "abbreviation"},
29− {Country, "country"},
30− {CountryCode, "countryCode"},
31− {City, "city"},
32− {Comment, "comment"},
33− {Time, "time"}};
31+ qRegisterMetaType<TzLocation>();
32+
33+ m_roleNames[Qt::DisplayRole] = "displayName";
34+ m_roleNames[TimeZoneRole] = "timeZone";
35+ m_roleNames[CityRole] = "city";
36+ m_roleNames[CountryRole] = "country";
37+
38+ QObject::connect(m_workerThread,
39+ &TimeZonePopulateWorker::resultReady,
40+ this,
41+ &TimeZoneLocationModel::processModelResult);
42+ QObject::connect(m_workerThread,
43+ &TimeZonePopulateWorker::finished,
44+ this,
45+ &TimeZoneLocationModel::store);
46+ QObject::connect(m_workerThread,
47+ &TimeZonePopulateWorker::finished,
48+ m_workerThread,
49+ &QObject::deleteLater);
50+
3451 init();
3552}
3653
37−QString TimeZoneModel::selectedZoneId() const
54+void TimeZoneLocationModel::init()
3855{
39− return m_selectedZoneId;
56+ beginResetModel();
57+ m_workerThread->start();
4058}
4159
42−void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
60+void TimeZoneLocationModel::store()
4361{
44− if (m_selectedZoneId == selectedZoneId)
45− return;
46−
47− m_selectedZoneId = selectedZoneId;
48− Q_EMIT selectedZoneIdChanged(selectedZoneId);
62+ m_workerThread = nullptr;
63+ endResetModel();
4964}
5065
51−int TimeZoneModel::rowCount(const QModelIndex &parent) const
66+void TimeZoneLocationModel::processModelResult(const TzLocation &location)
5267{
53− Q_UNUSED(parent)
54− return m_zoneIds.count();
68+ m_locations.append(location);
5569}
5670
57−QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
71+int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
5872{
59− if (index.isValid()) {
60− const QByteArray tzid = m_zoneIds.at(index.row());
61− QTimeZone tz(tzid);
62−
63− if (!tz.isValid()) {
64− qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
65− return QVariant();
66− }
73+ if (parent.isValid())
74+ return 0;
75+ return m_locations.count();
76+}
6777
68− switch (role) {
69− case IdRole:
70− return QString(tz.id()); // to let QML compare it effortlessly with a QString
71− case Abbreviation:
72− return tz.abbreviation(QDateTime::currentDateTime());
73− case Country:
74− return LocaleAttached::countryToString(tz.country());
75− case CountryCode: {
76− return LocaleAttached::qlocToCountryCode(tz.country());
77− }
78− case City: {
79− const QString cityName = QString::fromUtf8(tzid.split('/').last().replace('_', ' ')); // take the last part, replace _ by a space
80− return cityName;
81− }
82− case Comment:
83− return tz.comment();
84− case Time:
85− return QDateTime::currentDateTime().toTimeZone(tz).toString("h:mm");
86− default:
87− qWarning() << Q_FUNC_INFO << "Unsupported data role" << role;
88− break;
89− }
78+QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
79+{
80+ if (index.row() >= m_locations.count() || index.row() < 0)
81+ return QVariant();
82+
83+ const TzLocation tz = m_locations.at(index.row());
84+
85+ const QString country(tz.full_country.isEmpty() ? tz.country : tz.full_country);
86+
87+ switch (role) {
88+ case Qt::DisplayRole:
89+ if (!tz.state.isEmpty())
90+ return QStringLiteral("%1, %2, %3").arg(tz.city).arg(tz.state).arg(country);
91+ else
92+ return QStringLiteral("%1, %2").arg(tz.city).arg(country);
93+ case SimpleRole:
94+ return QStringLiteral("%1, %2").arg(tz.city).arg(country);
95+ case TimeZoneRole:
96+ return tz.timezone;
97+ case CountryRole:
98+ return tz.country;
99+ case CityRole:
100+ return tz.city;
101+ default:
102+ qWarning() << Q_FUNC_INFO << "Unknown role";
103+ return QVariant();
90104 }
91−
92− return QVariant();
93105}
94106
95−QHash<int, QByteArray> TimeZoneModel::roleNames() const
107+QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
96108{
97109 return m_roleNames;
98110}
99111
100−void TimeZoneModel::init()
112+void TimeZonePopulateWorker::run()
101113{
102− beginResetModel();
103− m_zoneIds = QTimeZone::availableTimeZoneIds();
104− endResetModel();
114+ buildCityMap();
115+}
116+
117+void TimeZonePopulateWorker::buildCityMap()
118+{
119+ TzDB *tzdb = tz_load_db();
120+ GPtrArray *tz_locations = tz_get_locations(tzdb);
121+
122+ TimeZoneLocationModel::TzLocation tmpTz;
123+
124+ for (guint i = 0; i < tz_locations->len; ++i) {
125+ auto tmp = static_cast<CcTimezoneLocation *>(g_ptr_array_index(tz_locations, i));
126+ gchar *en_name, *country, *zone, *state, *full_country;
127+ g_object_get (tmp, "en_name", &en_name,
128+ "country", &country,
129+ "zone", &zone,
130+ "state", &state,
131+ "full_country", &full_country,
132+ nullptr);
133+ // There are empty entries in the DB
134+ if (g_strcmp0(en_name, "") != 0) {
135+ tmpTz.city = en_name;
136+ tmpTz.country = country;
137+ tmpTz.timezone = zone;
138+ tmpTz.state = state;
139+ tmpTz.full_country = full_country;
140+ }
141+ g_free (en_name);
142+ g_free (country);
143+ g_free (zone);
144+ g_free (state);
145+ g_free (full_country);
146+
147+ Q_EMIT (resultReady(tmpTz));
148+ }
149+
150+ g_ptr_array_free (tz_locations, TRUE);
151+ tz_db_free(tzdb);
105152}
106153
107154
@@ -110,7 +157,7 @@ TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
110157{
111158 setDynamicSortFilter(false);
112159 setSortLocaleAware(true);
113− setSortRole(TimeZoneModel::City);
160+ setSortRole(TimeZoneLocationModel::CityRole);
114161 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
115162 sort(0);
116163}
@@ -122,14 +169,14 @@ bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentInd
122169 }
123170
124171 if (!m_filter.isEmpty()) { // filtering by freeform text input, cf setFilter(QString)
125− const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::City).toString();
126− //const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::Country).toString();
172+ const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CityRole).toString();
173+ //const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
127174
128175 if (m_stringMatcher.indexIn(city) == 0 /*|| m_stringMatcher.indexIn(country) == 0*/) { // match at the beginning of the city name
129176 return true;
130177 }
131178 } else if (!m_country.isEmpty()) { // filter by country code
132− const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::CountryCode).toString();
179+ const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
133180 return m_country.compare(countryCode, Qt::CaseInsensitive) == 0;
134181 }
135182