Changeset View
Changeset View
Standalone View
Standalone View
src/address.cpp
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "address.h" | 21 | #include "address.h" | ||
22 | #include "geo.h" | 22 | #include "geo.h" | ||
23 | #include "countrytoisomap_data.cpp" | ||||
23 | 24 | | |||
24 | #include "kcontacts_debug.h" | 25 | #include "kcontacts_debug.h" | ||
25 | #include <krandom.h> | 26 | #include <krandom.h> | ||
26 | #include <klocalizedstring.h> | 27 | #include <klocalizedstring.h> | ||
27 | #include <kconfig.h> | 28 | #include <kconfig.h> | ||
28 | 29 | | |||
29 | #include <kconfiggroup.h> | 30 | #include <kconfiggroup.h> | ||
30 | 31 | | |||
▲ Show 20 Lines • Show All 608 Lines • ▼ Show 20 Line(s) | 639 | } else if (QLatin1String("above") == cpos) { | |||
639 | ret = country() + QLatin1String("\n\n") + ret; | 640 | ret = country() + QLatin1String("\n\n") + ret; | ||
640 | } | 641 | } | ||
641 | } | 642 | } | ||
642 | 643 | | |||
643 | return ret; | 644 | return ret; | ||
644 | } | 645 | } | ||
645 | 646 | | |||
646 | struct IsoCache { | 647 | struct IsoCache { | ||
647 | QHash<QString, QString> countryToIso; | | |||
648 | QHash<QString, QString> isoToCountry; | 648 | QHash<QString, QString> isoToCountry; | ||
649 | }; | 649 | }; | ||
650 | Q_GLOBAL_STATIC(IsoCache, sIsoCache) | 650 | Q_GLOBAL_STATIC(IsoCache, sIsoCache) | ||
651 | 651 | | |||
652 | QString Address::countryToISO(const QString &cname) | 652 | QString Address::countryToISO(const QString &cname) | ||
653 | { | 653 | { | ||
654 | // we search a map file for translations from country names to | 654 | const auto lookupKey = cname.toCaseFolded().toUtf8(); | ||
655 | // iso codes, storing caching things in a QHash for faster future | | |||
656 | // access. | | |||
657 | 655 | | |||
658 | const auto it = sIsoCache->countryToIso.constFind(cname); | 656 | const auto it = std::lower_bound(std::begin(country_to_iso_index), std::end(country_to_iso_index), lookupKey, [](const CountryToIsoIndex &lhs, const QByteArray &rhs) { | ||
659 | if (it != sIsoCache->countryToIso.constEnd()) { | 657 | return strcmp(country_name_stringtable + lhs.m_offset, rhs.constData()) < 0; | ||
660 | return it.value(); | 658 | }); | ||
661 | } | | |||
662 | 659 | | |||
663 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | 660 | if (it == std::end(country_to_iso_index) || strcmp(country_name_stringtable + (*it).m_offset, lookupKey.constData()) != 0) { | ||
664 | if (file.open(QIODevice::ReadOnly)) { | 661 | return {}; | ||
665 | QTextStream s(&file); | | |||
666 | QString strbuf = s.readLine(); | | |||
667 | while (!strbuf.isEmpty()) { | | |||
668 | const auto countryInfo = strbuf.splitRef(QLatin1Char('\t'), QString::KeepEmptyParts); | | |||
669 | if (countryInfo[0] == cname) { | | |||
670 | file.close(); | | |||
671 | const auto iso = countryInfo[1].toString(); | | |||
672 | sIsoCache->countryToIso.insert(cname, iso); | | |||
673 | return iso; | | |||
674 | } | | |||
675 | strbuf = s.readLine(); | | |||
676 | } | | |||
677 | file.close(); | | |||
678 | } | 662 | } | ||
679 | 663 | | |||
680 | return {}; | 664 | return (*it).isoCode(); | ||
681 | } | 665 | } | ||
682 | 666 | | |||
683 | QString Address::ISOtoCountry(const QString &ISOname) | 667 | QString Address::ISOtoCountry(const QString &ISOname) | ||
684 | { | 668 | { | ||
685 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | 669 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | ||
686 | const auto iso = ISOname.simplified().toLower(); | 670 | const auto iso = ISOname.simplified().toLower(); | ||
687 | if (iso.simplified().isEmpty()) { | 671 | if (iso.simplified().isEmpty()) { | ||
688 | return QString(); | 672 | return QString(); | ||
Show All 10 Lines | 681 | if (file.open(QIODevice::ReadOnly)) { | |||
699 | QString searchStr = QLatin1Char('\t') + iso; | 683 | QString searchStr = QLatin1Char('\t') + iso; | ||
700 | QString strbuf = s.readLine(); | 684 | QString strbuf = s.readLine(); | ||
701 | while (!strbuf.isEmpty()) { | 685 | while (!strbuf.isEmpty()) { | ||
702 | int pos; | 686 | int pos; | ||
703 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | 687 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | ||
704 | file.close(); | 688 | file.close(); | ||
705 | const auto country = i18nd("iso_3166-1", strbuf.leftRef(pos).toUtf8().constData()); | 689 | const auto country = i18nd("iso_3166-1", strbuf.leftRef(pos).toUtf8().constData()); | ||
706 | sIsoCache->isoToCountry.insert(iso, country); | 690 | sIsoCache->isoToCountry.insert(iso, country); | ||
707 | sIsoCache->countryToIso.insert(country, iso); | | |||
708 | return country; | 691 | return country; | ||
709 | } | 692 | } | ||
710 | strbuf = s.readLine(); | 693 | strbuf = s.readLine(); | ||
711 | } | 694 | } | ||
712 | file.close(); | 695 | file.close(); | ||
713 | } | 696 | } | ||
714 | 697 | | |||
715 | return ISOname; | 698 | return ISOname; | ||
Show All 22 Lines |