Unity 8
timezonemodel.h
1 /*
2  * Copyright (C) 2016 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  */
16 
17 #ifndef TIMEZONEMODEL_H
18 #define TIMEZONEMODEL_H
19 
20 #include <geonames.h>
21 #include <glib.h>
22 #include <QAbstractListModel>
23 
24 class TimeZoneLocationModel: public QAbstractListModel
25 {
26  Q_OBJECT
27  Q_PROPERTY(bool listUpdating READ listUpdating NOTIFY listUpdatingChanged)
28  Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
29  Q_PROPERTY(QString country READ country WRITE setCountry NOTIFY countryChanged)
30  Q_ENUMS(Roles)
31 
32 public:
33  explicit TimeZoneLocationModel(QObject *parent = nullptr);
34  ~TimeZoneLocationModel();
35 
36  enum Roles {
37  TimeZoneRole = Qt::UserRole + 1,
38  CityRole,
39  CountryRole,
40  SimpleRole,
41  OffsetRole,
42  LatitudeRole,
43  LongitudeRole
44  };
45 
46  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
47  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
48  QHash<int, QByteArray> roleNames() const override;
49 
50  bool listUpdating() const;
51 
52  QString filter() const;
53  void setFilter(const QString &filter);
54 
55  QString country() const;
56  void setCountry(const QString &country);
57 
58 Q_SIGNALS:
59  void listUpdatingChanged();
60  void filterChanged();
61  void countryChanged(const QString &country);
62 
63 private:
64  void setModel(const QList<GeonamesCity *> &locations);
65  void setListUpdating(bool listUpdating);
66  static void filterFinished(GObject *source_object,
67  GAsyncResult *res,
68  gpointer user_data);
69 
70 
71  bool m_listUpdating;
72  QString m_filter;
73  QString m_country;
74  GCancellable *m_cancellable;
75  QHash<int, QByteArray> m_roleNames;
76  QList<GeonamesCity *> m_locations;
77  QList<GeonamesCity *> m_countryLocations;
78 };
79 
80 #endif