@@ -38,18 +38,26 @@ TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
39int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
39int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
41− if (parent.isValid())
41+ if (parent.isValid()) {
43+ } else if (m_filter.isEmpty()) {
44+ return m_countryLocations.count();
43− return m_locations.count();
46+ return m_locations.count();
46QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
50QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
53+ if (m_filter.isEmpty()) {
54+ city = m_countryLocations.value(index.row());
48− if (index.row() >= m_locations.count() || index.row() < 0)
56+ city = m_locations.value(index.row());
49 return QVariant();
59 return QVariant();
51− GeonamesCity *city = m_locations[index.row()];
53 switch (role) {
61 switch (role) {
54 case Qt::DisplayRole:
62 case Qt::DisplayRole:
55 return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
63 return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
@@ -185,12 +193,32 @@ QString TimeZoneLocationModel::country() const
185 return m_country;
193 return m_country;
196+static bool citycmp(GeonamesCity *a, GeonamesCity *b)
198+ return geonames_city_get_population(b) < geonames_city_get_population(a);
188void TimeZoneLocationModel::setCountry(const QString &country)
201void TimeZoneLocationModel::setCountry(const QString &country)
190 if (m_country == country)
203 if (m_country == country)
193 m_country = country;
206 m_country = country;
208+ Q_FOREACH(GeonamesCity *city, m_countryLocations) {
209+ geonames_city_free(city);
212+ gint num_cities = geonames_get_n_cities();
213+ for (gint i = 0; i < num_cities; i++) {
214+ GeonamesCity *city = geonames_get_city(i);
215+ if (city && m_country == geonames_city_get_country_code(city)) {
216+ m_countryLocations.append(city);
220+ std::sort(m_countryLocations.begin(), m_countryLocations.end(), citycmp);
194 Q_EMIT countryChanged(country);
222 Q_EMIT countryChanged(country);
@@ -201,6 +229,10 @@ TimeZoneLocationModel::~TimeZoneLocationModel()
201 g_clear_object(&m_cancellable);
229 g_clear_object(&m_cancellable);
232+ Q_FOREACH(GeonamesCity *city, m_countryLocations) {
233+ geonames_city_free(city);
204 Q_FOREACH(GeonamesCity *city, m_locations) {
236 Q_FOREACH(GeonamesCity *city, m_locations) {
205 geonames_city_free(city);
237 geonames_city_free(city);