on xenial help sign in
extend the timezone model with country code
a3651e8Lukáš Tinkl
plugins/Wizard/LocalePlugin.cpp+10
@@ -337,6 +337,16 @@ QString LocaleAttached::countryToString(QLocale::Country c)
337 return d->countryToString(c);
337 return d->countryToString(c);
338}
338}
339
339
340+QString LocaleAttached::qlocToCountryCode(QLocale::Country c)
341+{
342+ const QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, c);
343+ if (!locales.isEmpty()) {
344+ return locales.first().name().split('_').at(1);
345+ }
346+
347+ return QString();
348+}
349+
340
350
341LocalePlugin::LocalePlugin(QObject* parent)
351LocalePlugin::LocalePlugin(QObject* parent)
342 : QObject(parent)
352 : QObject(parent)
plugins/Wizard/LocalePlugin.h+1
@@ -33,6 +33,7 @@ public:
33 Q_INVOKABLE QJsonObject countriesForLanguage(const QString &code) const;
33 Q_INVOKABLE QJsonObject countriesForLanguage(const QString &code) const;
34 Q_INVOKABLE QJsonObject countries() const;
34 Q_INVOKABLE QJsonObject countries() const;
35 Q_INVOKABLE static QString countryToString(QLocale::Country c);
35 Q_INVOKABLE static QString countryToString(QLocale::Country c);
36+ Q_INVOKABLE static QString qlocToCountryCode(QLocale::Country c);
36
37
37 friend class LocalePlugin;
38 friend class LocalePlugin;
38};
39};
plugins/Wizard/timezonemodel.cpp+12−4
@@ -12,8 +12,6 @@
12 *
12 *
13 * You should have received a copy of the GNU General Public License along
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/>.
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15− *
16− * Author: Lukáš Tinkl <lukas.tinkl@canonical.com>
17 */
15 */
18
16
19#include <QTimeZone>
17#include <QTimeZone>
@@ -26,6 +24,13 @@
26TimeZoneModel::TimeZoneModel(QObject *parent)
24TimeZoneModel::TimeZoneModel(QObject *parent)
27 : QAbstractListModel(parent)
25 : QAbstractListModel(parent)
28{
26{
27+ m_roleNames = {{IdRole, "id"},
28+ {Abbreviation, "abbreviation"},
29+ {Country, "country"},
30+ {CountryCode, "countryCode"},
31+ {City, "city"},
32+ {Comment, "comment"},
33+ {Time, "time"}};
29 init();
34 init();
30}
35}
31
36
@@ -67,8 +72,11 @@ QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
67 return tz.abbreviation(QDateTime::currentDateTime());
72 return tz.abbreviation(QDateTime::currentDateTime());
68 case Country:
73 case Country:
69 return LocaleAttached::countryToString(tz.country());
74 return LocaleAttached::countryToString(tz.country());
75+ case CountryCode: {
76+ return LocaleAttached::qlocToCountryCode(tz.country());
77+ }
70 case City: {
78 case City: {
71− const QString cityName = QString::fromUtf8(tzid.split('/').last().replace("_", " ")); // take the last part, replace _ by a space
79+ const QString cityName = QString::fromUtf8(tzid.split('/').last().replace('_', ' ')); // take the last part, replace _ by a space
72 return cityName;
80 return cityName;
73 }
81 }
74 case Comment:
82 case Comment:
@@ -86,7 +94,7 @@ QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
86
94
87QHash<int, QByteArray> TimeZoneModel::roleNames() const
95QHash<int, QByteArray> TimeZoneModel::roleNames() const
88{
96{
89− return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}, {Time, "time"}};
97+ return m_roleNames;
90}
98}
91
99
92void TimeZoneModel::init()
100void TimeZoneModel::init()
plugins/Wizard/timezonemodel.h+2
@@ -51,6 +51,7 @@ public:
51 IdRole = Qt::UserRole + 1,
51 IdRole = Qt::UserRole + 1,
52 Abbreviation,
52 Abbreviation,
53 Country,
53 Country,
54+ CountryCode,
54 City,
55 City,
55 Comment,
56 Comment,
56 Time
57 Time
@@ -74,6 +75,7 @@ private:
74 void init();
75 void init();
75 QByteArrayList m_zoneIds;
76 QByteArrayList m_zoneIds;
76 QString m_selectedZoneId;
77 QString m_selectedZoneId;
78+ QHash<int, QByteArray> m_roleNames;
77};
79};
78
80
79#endif
81#endif
qml/Wizard/Pages.qml+2−1
@@ -31,7 +31,8 @@ Item {
31 // the user exits the wizard.
31 // the user exits the wizard.
32 property int passwordMethod: UbuntuSecurityPrivacyPanel.Passphrase
32 property int passwordMethod: UbuntuSecurityPrivacyPanel.Passphrase
33 property string password: ""
33 property string password: ""
34− property string language: ""
34+ property string language: "" // language code
35+ property string country: "" // country code, uppercase
35
36
36 UbuntuSecurityPrivacyPanel {
37 UbuntuSecurityPrivacyPanel {
37 id: securityPrivacy
38 id: securityPrivacy
qml/Wizard/Pages/15-country.qml+5−6
@@ -26,7 +26,6 @@ LocalComponents.Page {
26 title: i18n.tr("Country")
26 title: i18n.tr("Country")
27 forwardButtonSourceComponent: forwardButton
27 forwardButtonSourceComponent: forwardButton
28
28
29− property string selectedCountry: ""
30 readonly property var preferedCountries: LocalePlugin.countriesForLanguage(root.language)
29 readonly property var preferedCountries: LocalePlugin.countriesForLanguage(root.language)
31
30
32 Component.onCompleted: {
31 Component.onCompleted: {
@@ -63,8 +62,10 @@ LocalComponents.Page {
63
62
64 function selectCountry(code, index)
63 function selectCountry(code, index)
65 {
64 {
66− selectedCountry = code
65+ root.country = code
67 regionsListView.currentIndex = index
66 regionsListView.currentIndex = index
67+ print("Selected country: " + root.country)
68+ print("Current index: " + regionsListView.currentIndex)
68 }
69 }
69
70
70 UbuntuLanguagePlugin {
71 UbuntuLanguagePlugin {
@@ -143,8 +144,6 @@ LocalComponents.Page {
143
144
144 onClicked: {
145 onClicked: {
145 selectCountry(code, index)
146 selectCountry(code, index)
146− print("Selected country: " + selectedCountry)
147− print("Current index: " + ListView.view.currentIndex)
148 }
147 }
149 }
148 }
150 }
149 }
@@ -154,11 +153,11 @@ LocalComponents.Page {
154 id: forwardButton
153 id: forwardButton
155 LocalComponents.StackButton {
154 LocalComponents.StackButton {
156 text: i18n.tr("Next")
155 text: i18n.tr("Next")
157− enabled: selectedCountry !== ""
156+ enabled: root.country !== ""
158 onClicked: {
157 onClicked: {
159 if (root.language !== plugin.languageCodes[plugin.currentLanguage]) {
158 if (root.language !== plugin.languageCodes[plugin.currentLanguage]) {
160 plugin.currentLanguage = plugin.languageCodes.indexOf(root.language)
159 plugin.currentLanguage = plugin.languageCodes.indexOf(root.language)
161− System.updateSessionLanguage(root.language + "_" + selectedCountry); // also updates the locale
160+ System.updateSessionLanguage(root.language + "_" + root.country); // also updates the locale
162 }
161 }
163 pageStack.next()
162 pageStack.next()
164 }
163 }
qml/Wizard/Pages/50-timezone.qml+1
@@ -82,6 +82,7 @@ LocalComponents.Page {
82 onClicked: {
82 onClicked: {
83 tzModel.selectedZoneId = id
83 tzModel.selectedZoneId = id
84 print("Selected tz: " + selectedTimeZone)
84 print("Selected tz: " + selectedTimeZone)
85+ print("Clicked country", countryCode)
85 }
86 }
86 }
87 }
87 }
88 }