Changeset View
Changeset View
Standalone View
Standalone View
src/address.cpp
Show First 20 Lines • Show All 637 Lines • ▼ Show 20 Line(s) | 629 | if (!country().isEmpty()) { | |||
---|---|---|---|---|---|
638 | } else if (QLatin1String("above") == cpos) { | 638 | } else if (QLatin1String("above") == cpos) { | ||
639 | ret = country() + QLatin1String("\n\n") + ret; | 639 | ret = country() + QLatin1String("\n\n") + ret; | ||
640 | } | 640 | } | ||
641 | } | 641 | } | ||
642 | 642 | | |||
643 | return ret; | 643 | return ret; | ||
644 | } | 644 | } | ||
645 | 645 | | |||
646 | typedef QMap<QString, QString> StringMap; | 646 | struct IsoCache { | ||
647 | Q_GLOBAL_STATIC(StringMap, sISOMap) | 647 | QHash<QString, QString> countryToIso; | ||
648 | QHash<QString, QString> isoToCountry; | ||||
649 | }; | ||||
650 | Q_GLOBAL_STATIC(IsoCache, sIsoCache) | ||||
648 | 651 | | |||
649 | QString Address::countryToISO(const QString &cname) | 652 | QString Address::countryToISO(const QString &cname) | ||
650 | { | 653 | { | ||
651 | // we search a map file for translations from country names to | 654 | // we search a map file for translations from country names to | ||
652 | // iso codes, storing caching things in a QMap for faster future | 655 | // iso codes, storing caching things in a QHash for faster future | ||
dvratil: s/QMap/QHash/ | |||||
653 | // access. | 656 | // access. | ||
654 | 657 | | |||
655 | QMap<QString, QString>::ConstIterator it; | 658 | const auto it = sIsoCache->countryToIso.constFind(cname); | ||
656 | it = sISOMap->constFind(cname); | 659 | if (it != sIsoCache->countryToIso.constEnd()) { | ||
657 | if (it != sISOMap->constEnd()) { | | |||
658 | return it.value(); | 660 | return it.value(); | ||
659 | } | 661 | } | ||
660 | 662 | | |||
661 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | 663 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | ||
662 | if (file.open(QIODevice::ReadOnly)) { | 664 | if (file.open(QIODevice::ReadOnly)) { | ||
663 | QTextStream s(&file); | 665 | QTextStream s(&file); | ||
664 | QString strbuf = s.readLine(); | 666 | QString strbuf = s.readLine(); | ||
665 | while (!strbuf.isEmpty()) { | 667 | while (!strbuf.isEmpty()) { | ||
666 | QStringList countryInfo = strbuf.split(QLatin1Char('\t'), QString::KeepEmptyParts); | 668 | const auto countryInfo = strbuf.splitRef(QLatin1Char('\t'), QString::KeepEmptyParts); | ||
667 | if (countryInfo[0] == cname) { | 669 | if (countryInfo[0] == cname) { | ||
668 | file.close(); | 670 | file.close(); | ||
669 | sISOMap->insert(cname, countryInfo[1]); | 671 | const auto iso = countryInfo[1].toString(); | ||
670 | return countryInfo[1]; | 672 | sIsoCache->countryToIso.insert(cname, iso); | ||
Would it make sense to also insert the strings to the isoToCountry hash table to benefit QString's explicit sharing (and vice-verase in ISOToCountry()), or is it unlikely any code would benefit from this? dvratil: Would it make sense to also insert the strings to the `isoToCountry` hash table to benefit… | |||||
This could work in ISOtoCountry indeed, but here we would need to check if the language of the country name is the local one before inserting, which we have no way of detecting. vkrause: This could work in ISOtoCountry indeed, but here we would need to check if the language of the… | |||||
673 | return iso; | ||||
671 | } | 674 | } | ||
672 | strbuf = s.readLine(); | 675 | strbuf = s.readLine(); | ||
673 | } | 676 | } | ||
674 | file.close(); | 677 | file.close(); | ||
675 | } | 678 | } | ||
676 | 679 | | |||
677 | return {}; | 680 | return {}; | ||
678 | } | 681 | } | ||
679 | 682 | | |||
680 | QString Address::ISOtoCountry(const QString &ISOname) | 683 | QString Address::ISOtoCountry(const QString &ISOname) | ||
681 | { | 684 | { | ||
682 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | 685 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | ||
683 | if (ISOname.simplified().isEmpty()) { | 686 | const auto iso = ISOname.simplified().toLower(); | ||
687 | if (iso.simplified().isEmpty()) { | ||||
684 | return QString(); | 688 | return QString(); | ||
685 | } | 689 | } | ||
686 | 690 | | |||
691 | const auto it = sIsoCache->isoToCountry.constFind(iso); | ||||
692 | if (it != sIsoCache->isoToCountry.constEnd()) { | ||||
693 | return it.value(); | ||||
694 | } | ||||
695 | | ||||
687 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | 696 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | ||
688 | if (file.open(QIODevice::ReadOnly)) { | 697 | if (file.open(QIODevice::ReadOnly)) { | ||
689 | QTextStream s(&file); | 698 | QTextStream s(&file); | ||
690 | QString searchStr = QLatin1Char('\t') + ISOname.simplified().toLower(); | 699 | QString searchStr = QLatin1Char('\t') + iso; | ||
691 | QString strbuf = s.readLine(); | 700 | QString strbuf = s.readLine(); | ||
692 | while (!strbuf.isEmpty()) { | 701 | while (!strbuf.isEmpty()) { | ||
693 | int pos; | 702 | int pos; | ||
694 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | 703 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | ||
695 | file.close(); | 704 | file.close(); | ||
696 | return i18n(strbuf.left(pos).toUtf8().constData()); | 705 | const auto country = i18n(strbuf.leftRef(pos).toUtf8().constData()); | ||
706 | sIsoCache->isoToCountry.insert(iso, country); | ||||
707 | sIsoCache->countryToIso.insert(country, iso); | ||||
708 | return country; | ||||
697 | } | 709 | } | ||
698 | strbuf = s.readLine(); | 710 | strbuf = s.readLine(); | ||
699 | } | 711 | } | ||
700 | file.close(); | 712 | file.close(); | ||
701 | } | 713 | } | ||
702 | 714 | | |||
703 | return ISOname; | 715 | return ISOname; | ||
704 | } | 716 | } | ||
Show All 21 Lines |
s/QMap/QHash/