@@ -26,28 +26,60 @@ LocalComponents.Page {
2626 title: i18n.tr("Region")
2727 forwardButtonSourceComponent: forwardButton
2828
29− readonly property var preferedCountries: LocalePlugin.countriesForLanguage(root.language)
30−
3129 Component.onCompleted: {
32− populateModel(preferedCountries);
30+ if (!modemManager.available) { // don't wait for the modem if it's not there
31+ init()
32+ }
33+ }
34+
35+ Connections {
36+ target: modemManager
37+ onModemsChanged: {
38+ print("Modems changed")
39+ init()
40+ }
41+ }
42+
43+ function init()
44+ {
45+ var detectedCountry
46+ if (simManager0.present && simManager0.mobileCountryCode) {
47+ detectedCountry = LocalePlugin.mccToCountryCode(simManager0.mobileCountryCode)
48+ print("SIM 0 detected country:", detectedCountry)
49+ } else if (simManager1.present && simManager1.mobileCountryCode) {
50+ detectedCountry = LocalePlugin.mccToCountryCode(simManager1.mobileCountryCode)
51+ print("SIM 1 detected country:", detectedCountry)
52+ } else if (!detectedCountry) {
53+ detectedCountry = preferedCountry() // fallback to prefered country
54+ print("No SIM country detected, falling back to prefered country:", detectedCountry)
55+ }
56+
57+ populateModel(detectedCountry)
58+ }
59+
60+ // return a prefered country for a language, but only if there's exactly one
61+ function preferedCountry()
62+ {
63+ var countries = Object.keys(LocalePlugin.countriesForLanguage(root.language))
64+ if (countries.length === 1) {
65+ return countries[0]
66+ }
67+ // TODO should we ultimately fallback to "US" here?
3368 }
3469
35− function populateModel(preferedCountries)
70+ function populateModel(preferedCountry)
3671 {
3772 var countries = LocalePlugin.countries();
73+ var index = 0
74+ var selectedIndex = -1
3875 //console.log("All countries:" + countries);
76+ print("Prefered country", preferedCountry)
3977 Object.keys(countries).map(function(code) { // prepare the object
4078 //console.log("Got country:" + code);
41− return { "code": code, "country": countries[code] || code,
42− "dept": Object.keys(preferedCountries).indexOf(code) !== -1 ? i18n.tr("Prefered") : i18n.tr("All") };
79+ return { "code": code, "country": countries[code] || code };
4380 })
44− .sort(function(a, b) { // group by status, sort by country name
45− if (a.dept === b.dept) {
46− return a.country.localeCompare(b.country);
47− } else if (a.dept === i18n.tr("Prefered")) {
48− return -1
49− }
50− return 1
81+ .sort(function(a, b) { // sort by country name
82+ return a.country.localeCompare(b.country);
5183 })
5284 .forEach(function(country) { // build the model
5385 //console.debug("Adding country:" + country.code);
@@ -55,9 +87,18 @@ LocalComponents.Page {
5587 return;
5688 }
5789 model.append(country);
90+ if (preferedCountry && country.code === preferedCountry) { // preselect the prefered country, if any
91+ selectedIndex = index;
92+ selectCountry(country.code, selectedIndex)
93+ }
94+ index++;
5895 });
5996 busyIndicator.running = false;
6097 busyIndicator.visible = false;
98+
99+ if (selectedIndex !== -1) {
100+ regionsListView.positionViewAtIndex(selectedIndex, ListView.Center)
101+ }
61102 }
62103
63104 function selectCountry(code, index)