Commit 323b3c8a authored by Volker Krause's avatar Volker Krause
Browse files

Port to KI18nLocaleData

This replaces some of our self-made country localization code and the use
of some deprecated KContacts API.
parent fc18dcd5
......@@ -10,7 +10,7 @@ set (RELEASE_SERVICE_VERSION_MICRO "70")
set (RELEASE_SERVICE_VERSION "${RELEASE_SERVICE_VERSION_MAJOR}.${RELEASE_SERVICE_VERSION_MINOR}.${RELEASE_SERVICE_VERSION_MICRO}")
project(itinerary VERSION ${RELEASE_SERVICE_VERSION})
set(KF5_MIN_VERSION 5.85)
set(KF5_MIN_VERSION 5.88)
find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_SOURCE_DIR}/cmake)
......@@ -41,10 +41,7 @@ if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
endif()
endif()
set(QT_MIN_VERSION 5.15)
if (ANDROID)
set(QT_MIN_VERSION 5.15.1) # for content: support
endif()
set(QT_MIN_VERSION 5.15.2)
# build-time dependencies
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS Test Quick Positioning Location QuickControls2)
......@@ -52,7 +49,7 @@ find_package(Qt5 CONFIG QUIET OPTIONAL_COMPONENTS QuickCompiler)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS I18n CoreAddons Contacts Notifications)
find_package(KF5 ${KF5_MIN_VERSION} OPTIONAL_COMPONENTS Holidays NetworkManagerQt)
find_package(KPimPkPass CONFIG REQUIRED)
find_package(KPimItinerary 5.17.41 CONFIG REQUIRED)
find_package(KPimItinerary 5.18.0 CONFIG REQUIRED)
find_package(KPublicTransport CONFIG REQUIRED)
find_package(KOSMIndoorMap CONFIG REQUIRED)
find_package(KHealthCertificate CONFIG)
......
......@@ -56,6 +56,7 @@ target_link_libraries(itinerary PUBLIC
KPim::PkPass
KF5::Mime
KF5::I18n
KF5::I18nLocaleData
KF5::Contacts
KF5::CoreAddons
KF5::Notifications
......
......@@ -7,6 +7,7 @@ import QtQuick 2.15
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.prison 1.0 as Prison
import org.kde.khealthcertificate 1.0 as KHC
import org.kde.itinerary 1.0
......@@ -87,8 +88,10 @@ ColumnLayout {
visible: text !== ""
}
QQC2.Label {
text: Localizer.countryFlag(certificate.country) + " " + Localizer.countryName(certificate.country)
readonly property var country: Country.fromAlpha2(certificate.country)
text: country.emojiFlag + " " + country.name
Kirigami.FormData.label: i18n("Country:")
visible: certificate.country
}
Kirigami.Separator {
......
......@@ -7,6 +7,7 @@ import QtQuick 2.15
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.prison 1.0 as Prison
import org.kde.khealthcertificate 1.0 as KHC
import org.kde.itinerary 1.0
......@@ -83,7 +84,8 @@ ColumnLayout {
visible: certificate.dose > 0
}
QQC2.Label {
text: Localizer.countryFlag(certificate.country) + " " + Localizer.countryName(certificate.country)
readonly property var country: Country.fromAlpha2(certificate.country)
text: country.emojiFlag + " " + country.name
Kirigami.FormData.label: i18n("Country:")
visible: certificate.country
}
......
......@@ -8,6 +8,7 @@ import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.kitinerary 1.0 as KItinerary
import org.kde.itinerary 1.0
import "." as App
......@@ -40,7 +41,7 @@ Kirigami.AbstractCard {
isMask: true
}
QQC2.Label {
text: i18n("Entering %1", Localizer.countryName(locationInfo.isoCode))
text: i18n("Entering %1", Country.fromAlpha2(locationInfo.isoCode).name)
color: Kirigami.Theme.neutralTextColor
Layout.fillWidth: true
}
......
......@@ -8,6 +8,7 @@ import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.kpublictransport 1.0 as KPublicTransport
import org.kde.itinerary 1.0
......@@ -82,7 +83,18 @@ Kirigami.ScrollablePage {
section.property: "countryCode"
section.delegate: Kirigami.ListSectionHeader {
text: (section == "" || section == "UN") ? i18n("Global") : Localizer.countryFlag(section) + " " + Localizer.countryName(section)
text: {
switch (section) {
case "":
case "UN":
return i18n("Global")
case "EU":
return i18n("🇪🇺 European Union");
default:
const c = Country.fromAlpha2(section);
return c.emojiFlag + " " + c.name;
}
}
}
section.criteria: ViewSection.FullString
section.labelPositioning: ViewSection.CurrentLabelAtStart | ViewSection.InlineLabels
......
......@@ -8,6 +8,7 @@ import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.17 as Kirigami
import org.kde.i18n.localeData 1.0
import org.kde.itinerary 1.0
import "." as App
......@@ -50,8 +51,8 @@ Kirigami.ScrollablePage {
StatisticsDelegate {
statItem: model.visitedCountries
label.text: model.visitedCountries.value.split(" ").map(countryCode => Localizer.countryFlag(countryCode)).join(" ")
visible: !countryDetailsLink.checked
label.text: model.visitedCountries.value.split(" ").map(countryCode => Country.fromAlpha2(countryCode).emojiFlag).join(" ")
visible: !countryDetailsLink.checked && model.visitedCountries.value !== ""
}
ColumnLayout {
......@@ -66,7 +67,8 @@ Kirigami.ScrollablePage {
QQC2.Label {
Layout.fillWidth: true
wrapMode: Text.WordWrap
text: Localizer.countryFlag(modelData) + " " + Localizer.countryName(modelData);
readonly property var country: Country.fromAlpha2(modelData)
text: country.emojiFlag + " " + country.name
}
}
}
......@@ -76,6 +78,7 @@ Kirigami.ScrollablePage {
property bool checked: false
text: checked ? i18nc("@action", "Hide Names") : i18nc("@action", "Show Names")
onClicked: checked = !checked
visible: model.visitedCountries.value !== ""
}
Kirigami.Separator {
......
......@@ -7,6 +7,7 @@
import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
import org.kde.i18n.localeData 1.0
import org.kde.kirigami 2.17 as Kirigami
import org.kde.itinerary 1.0
import "." as App
......@@ -90,11 +91,11 @@ Kirigami.AbstractCard {
text: {
if (modelData.powerPlugCompatibility == LocationInformation.PartiallyCompatible) {
if (modelData.powerPlugTypes == "")
return i18n("%1: some incompatible power sockets (%2)", Localizer.countryName(modelData.isoCode), modelData.powerSocketTypes);
return i18n("%1: some incompatible power sockets (%2)", Country.fromAlpha2(modelData.isoCode).name, modelData.powerSocketTypes);
else
return i18n("%1: some incompatible power plugs (%2)", Localizer.countryName(modelData.isoCode), modelData.powerPlugTypes);
return i18n("%1: some incompatible power plugs (%2)", Country.fromAlpha2(modelData.isoCode).name, modelData.powerPlugTypes);
} else {
return i18n("%1: no compatible power plugs (%2)", Localizer.countryName(modelData.isoCode), modelData.powerSocketTypes);
return i18n("%1: no compatible power plugs (%2)", Country.fromAlpha2(modelData.isoCode).name, modelData.powerSocketTypes);
}
}
color: modelData.powerPlugCompatibility == LocationInformation.PartiallyCompatible ? Kirigami.Theme.neutralTextColor : Kirigami.Theme.negativeTextColor
......
......@@ -6,33 +6,23 @@
#include "countrymodel.h"
#include <KItinerary/CountryDb>
#include <KContacts/Address>
using namespace KItinerary;
static QString countryName(const KnowledgeDb::Country *country)
{
return KContacts::Address::ISOtoCountry(country->id.toString());
}
#include <KCountry>
#include <QCollator>
CountryModel::CountryModel(QObject *parent)
: QAbstractListModel(parent)
{
m_countries.resize(std::distance(KnowledgeDb::countriesBegin(), KnowledgeDb::countriesEnd()));
std::transform(KnowledgeDb::countriesBegin(), KnowledgeDb::countriesEnd(), m_countries.begin(), [](const KnowledgeDb::Country& country) { return &country; });
std::sort(m_countries.begin(), m_countries.end(), [](const KnowledgeDb::Country *lhs, const KnowledgeDb::Country *rhs) { return countryName(lhs) < countryName(rhs); });
m_countries = KCountry::allCountries();
QCollator collator;
std::sort(m_countries.begin(), m_countries.end(), [&collator](const auto &lhs, const auto &rhs) { return collator.compare(lhs.name(), rhs.name()) < 0; });
}
CountryModel::~CountryModel() = default;
int CountryModel::isoCodeToIndex(const QString &isoCode) const
{
const auto id = KnowledgeDb::CountryId{isoCode};
const auto it = std::find_if(m_countries.constBegin(), m_countries.constEnd(), [id](const KnowledgeDb::Country *country) {
return country->id == id;
});
const auto id = KCountry::fromAlpha2(isoCode);
const auto it = std::find(m_countries.constBegin(), m_countries.constEnd(), id);
if (it == m_countries.constEnd()) {
return -1;
}
......@@ -44,7 +34,7 @@ QString CountryModel::isoCodeFromIndex(int index) const
if (index < 0 || index >= m_countries.size()) {
return {};
}
return m_countries.at(index)->id.toString();
return m_countries.at(index).alpha2();
}
int CountryModel::rowCount(const QModelIndex& parent) const
......@@ -62,12 +52,12 @@ QVariant CountryModel::data(const QModelIndex& index, int role) const
return {};
}
const auto* country = m_countries.at(index.row());
const auto country = m_countries.at(index.row());
switch (role) {
case Qt::DisplayRole:
return countryName(country);
return country.name();
case Qt::EditRole:
return country->id.toString();
return country.alpha2();
}
return {};
......
......@@ -8,11 +8,8 @@
#define COUNTRYMODEL_H
#include <QAbstractListModel>
namespace KItinerary {
namespace KnowledgeDb {
struct Country;
}
}
class KCountry;
/** Country model for selecting the home country. */
class CountryModel : public QAbstractListModel
......@@ -29,7 +26,7 @@ public:
QVariant data(const QModelIndex& index, int role) const override;
private:
QVector<const KItinerary::KnowledgeDb::Country *> m_countries;
QList<KCountry> m_countries;
};
#endif // COUNTRYMODEL_H
......@@ -10,6 +10,8 @@
#include <KItinerary/Place>
#include <KContacts/Address>
#include <KCountry>
#include <KFormat>
#include <KLocalizedString>
......@@ -33,26 +35,6 @@ using namespace KAndroidExtras;
using namespace KItinerary;
QString Localizer::countryName(const QString& isoCode) const
{
return KContacts::Address::ISOtoCountry(isoCode);
}
QString Localizer::countryFlag(const QString& isoCode) const
{
if (isoCode.size() != 2) {
return {};
}
QString flag;
char flagA[] = "\xF0\x9F\x87\xA6";
flagA[3] = 0xA6 + (isoCode[0].toLatin1() - 'A');
flag += QString::fromUtf8(flagA);
flagA[3] = 0xA6 + (isoCode[1].toLatin1() - 'A');
flag += QString::fromUtf8(flagA);
return flag;
}
static QString readFromGadget(const QMetaObject *mo, const QVariant &gadget, const char *propName)
{
const auto propIdx = mo->indexOfProperty(propName);
......@@ -75,14 +57,14 @@ QString Localizer::formatAddress(const QVariant &obj) const
address.setPostalCode(a.postalCode());
address.setLocality(a.addressLocality());
address.setRegion(a.addressRegion());
address.setCountry(KContacts::Address::ISOtoCountry(a.addressCountry()));
address.setCountry(KCountry::fromAlpha2(a.addressCountry()).name());
} else if (std::strcmp(obj.typeName(), "KOSMIndoorMap::OSMAddress") == 0) {
const auto mo = QMetaType::metaObjectForType(obj.userType());
address.setStreet(readFromGadget(mo, obj, "street") + QLatin1Char(' ') + readFromGadget(mo, obj, "houseNumber"));
address.setPostalCode(readFromGadget(mo, obj, "postalCode"));
address.setLocality(readFromGadget(mo, obj, "city"));
address.setRegion(readFromGadget(mo, obj, "state"));
address.setCountry(KContacts::Address::ISOtoCountry(readFromGadget(mo, obj, "country")));
address.setCountry(KCountry::fromAlpha2(readFromGadget(mo, obj, "country")).name());
} else {
return {};
}
......
......@@ -19,11 +19,6 @@ class Localizer
{
Q_GADGET
public:
Q_INVOKABLE QString countryName(const QString &isoCode) const;
/** Emoji representation of @p isoCode.
* @see https://en.wikipedia.org/wiki/Regional_Indicator_Symbol
*/
Q_INVOKABLE QString countryFlag(const QString &isoCode) const;
Q_INVOKABLE QString formatAddress(const QVariant &obj) const;
Q_INVOKABLE QString formatTime(const QVariant &obj, const QString &propertyName) const;
Q_INVOKABLE QString formatDate(const QVariant &obj, const QString &propertyName) const;
......
......@@ -6,7 +6,7 @@
#include "settings.h"
#include <KContacts/Address>
#include <KCountry>
#include <QDebug>
#include <QLocale>
......@@ -19,7 +19,7 @@ Settings::Settings(QObject *parent)
s.beginGroup(QLatin1String("Settings"));
m_weatherEnabled = s.value(QLatin1String("WeatherForecastEnabled"), false).toBool();
const auto currentCountry = KContacts::Address::countryToISO(QLocale::countryToString(QLocale().country())).toUpper();
const auto currentCountry = KCountry::fromQLocale(QLocale().country()).alpha2();
m_homeCountry = s.value(QLatin1String("HomeCountry"), currentCountry).toString();
m_queryLiveData = s.value(QLatin1String("QueryLiveData"), false).toBool();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment