From 7c35daca74fb4a4f8ed7e2fadc22ec18e0295744 Mon Sep 17 00:00:00 2001 From: Basil Semuonov Date: Sun, 10 Aug 2014 16:33:24 +0400 Subject: General transliteration with ICU rule "Any-Latin; Latin-ASCII" --- app/qml/pages/ManagerPage.qml | 9 ++--- daemon/daemon.pro | 1 + daemon/manager.cpp | 81 ++++++++++++++++++------------------------- daemon/manager.h | 6 +++- daemon/settings.h | 6 ++-- rpm/pebble.yaml | 1 + 6 files changed, 48 insertions(+), 56 deletions(-) diff --git a/app/qml/pages/ManagerPage.qml b/app/qml/pages/ManagerPage.qml index 14a6165..00a4b72 100644 --- a/app/qml/pages/ManagerPage.qml +++ b/app/qml/pages/ManagerPage.qml @@ -41,7 +41,7 @@ Page { id: settings path: "/org/pebbled/settings" property bool silentWhenConnected: false - property bool transliterateCyrillic: false + property bool transliterateMessage: false property bool incomingCallNotification: true property bool notificationsCommhistoryd: true property bool notificationsMissedCall: true @@ -159,11 +159,12 @@ Page { } } TextSwitch { - text: qsTr("Transliterate Cyrillic") - checked: settings.transliterateCyrillic + text: qsTr("Transliterate messages") + description: qsTr("Messages will be transliterated to ASCII before sending to Pebble") + checked: settings.transliterateMessage automaticCheck: false onClicked: { - settings.transliterateCyrillic = !settings.transliterateCyrillic; + settings.transliterateMessage = !settings.transliterateMessage; } } diff --git a/daemon/daemon.pro b/daemon/daemon.pro index bc0c2e7..0528ec9 100644 --- a/daemon/daemon.pro +++ b/daemon/daemon.pro @@ -9,6 +9,7 @@ PKGCONFIG += mlite5 QMAKE_CXXFLAGS += -std=c++0x LIBS += -llog4qt +LIBS += -licuuc -licui18n DEFINES += APP_VERSION=\\\"$$VERSION\\\" diff --git a/daemon/manager.cpp b/daemon/manager.cpp index 9f2a484..f964f1a 100644 --- a/daemon/manager.cpp +++ b/daemon/manager.cpp @@ -189,8 +189,8 @@ QString Manager::findPersonByNumber(QString number) person = found[0].detail(QContactDetail::TypeDisplayLabel).value(0).toString(); } - if (settings->property("transliterateCyrillic").toBool()) { - transliterateCyrillic(person); + if (settings->property("transliterateMessage").toBool()) { + transliterateMessage(person); } return person; } @@ -208,18 +208,18 @@ void Manager::onNotifyError(const QString &message) void Manager::onSmsNotify(const QString &sender, const QString &data) { - if (settings->property("transliterateCyrillic").toBool()) { - transliterateCyrillic(sender); - transliterateCyrillic(data); + if (settings->property("transliterateMessage").toBool()) { + transliterateMessage(sender); + transliterateMessage(data); } watch->sendSMSNotification(sender, data); } void Manager::onTwitterNotify(const QString &sender, const QString &data) { - if (settings->property("transliterateCyrillic").toBool()) { - transliterateCyrillic(sender); - transliterateCyrillic(data); + if (settings->property("transliterateMessage").toBool()) { + transliterateMessage(sender); + transliterateMessage(data); } watch->sendTwitterNotification(sender, data); } @@ -227,9 +227,9 @@ void Manager::onTwitterNotify(const QString &sender, const QString &data) void Manager::onFacebookNotify(const QString &sender, const QString &data) { - if (settings->property("transliterateCyrillic").toBool()) { - transliterateCyrillic(sender); - transliterateCyrillic(data); + if (settings->property("transliterateMessage").toBool()) { + transliterateMessage(sender); + transliterateMessage(data); } watch->sendFacebookNotification(sender, data); } @@ -237,10 +237,10 @@ void Manager::onFacebookNotify(const QString &sender, const QString &data) void Manager::onEmailNotify(const QString &sender, const QString &data,const QString &subject) { - if (settings->property("transliterateCyrillic").toBool()) { - transliterateCyrillic(sender); - transliterateCyrillic(data); - transliterateCyrillic(subject); + if (settings->property("transliterateMessage").toBool()) { + transliterateMessage(sender); + transliterateMessage(data); + transliterateMessage(subject); } watch->sendEmailNotification(sender, data, subject); } @@ -345,40 +345,25 @@ void Manager::applyProfile() } } -void Manager::transliterateCyrillic(const QString &text) +void Manager::transliterateMessage(const QString &text) { - QString translit; - int ru; - static QString rusUpper; - static QString rusLower; - static QStringList latUpper; - static QStringList latLower; - if (rusLower.isEmpty()) { - rusLower = QString::fromUtf8("абвгдеёжзийклмнопрстуфхцчшщъыьэюя"); - rusUpper = QString::fromUtf8("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"); - latUpper <<"A"<<"B"<<"V"<<"G"<<"D"<<"E"<<"Jo"<<"Zh"<<"Z"<<"I"<<"J"<<"K"<<"L"<<"M"<<"N" - <<"O"<<"P"<<"R"<<"S"<<"T"<<"U"<<"F"<<"H"<<"C"<<"Ch"<<"Sh"<<"Sh'"<<""<<"I"<<"'"<<"E"<<"Ju"<<"Ja"; - latLower <<"a"<<"b"<<"v"<<"g"<<"d"<<"e"<<"jo"<<"zh"<<"z"<<"i"<<"j"<<"k"<<"l"<<"m"<<"n" - <<"o"<<"p"<<"r"<<"s"<<"t"<<"u"<<"f"<<"h"<<"c"<<"ch"<<"sh"<<"sh'"<<""<<"i"<<"'"<<"e"<<"ju"<<"ja"; - } - for (int i=0; i < text.size(); ++i){ - QChar ch = text[i]; - if (ch.isLetter()) { - if (ch.isUpper()) { - ru = rusUpper.indexOf(ch); - if (ru >= 0) { - translit.append(latUpper[ru]); - continue; - } - } else if (ch.isLower()) { - ru = rusLower.indexOf(ch); - if (ru >= 0) { - translit.append(latLower[ru]); - continue; - } - } + if (transliterator.isNull()) { + UErrorCode status = U_ZERO_ERROR; + transliterator.reset(icu::Transliterator::createInstance(icu::UnicodeString::fromUTF8("Any-Latin; Latin-ASCII"),UTRANS_FORWARD, status)); + if (U_FAILURE(status)) { + logger()->warn() << "Error creaing ICU Transliterator \"Any-Latin; Latin-ASCII\":" << u_errorName(status); } - translit.append(text[i]); } - const_cast(text) = translit; + if (!transliterator.isNull()) { + logger()->debug() << "String before transliteration:" << text; + + icu::UnicodeString uword = icu::UnicodeString::fromUTF8(text.toStdString()); + transliterator->transliterate(uword); + + std::string translited; + uword.toUTF8String(translited); + + const_cast(text) = QString::fromStdString(translited); + logger()->debug() << "String after transliteration:" << text; + } } diff --git a/daemon/manager.h b/daemon/manager.h index cf40aed..9de5667 100644 --- a/daemon/manager.h +++ b/daemon/manager.h @@ -16,6 +16,8 @@ #include #include +#include + using namespace QtContacts; class Manager : @@ -50,6 +52,8 @@ class Manager : QString lastSeenMpris; + QScopedPointer transliterator; + public: explicit Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallManager *voice, NotificationManager *notifications, Settings *settings); @@ -60,7 +64,7 @@ public: QVariantMap getMprisMetadata() { return mprisMetadata; } protected: - void transliterateCyrillic(const QString &text); + void transliterateMessage(const QString &text); signals: void mprisMetadataChanged(QVariantMap); diff --git a/daemon/settings.h b/daemon/settings.h index 87e4919..d6db9b6 100644 --- a/daemon/settings.h +++ b/daemon/settings.h @@ -8,7 +8,7 @@ class Settings : public MDConfGroup Q_OBJECT Q_PROPERTY(bool silentWhenConnected MEMBER silentWhenConnected NOTIFY silentWhenConnectedChanged) - Q_PROPERTY(bool transliterateCyrillic MEMBER transliterateCyrillic NOTIFY transliterateCyrillicChanged) + Q_PROPERTY(bool transliterateMessage MEMBER transliterateMessage NOTIFY transliterateMessageChanged) Q_PROPERTY(bool incomingCallNotification MEMBER incomingCallNotification NOTIFY incomingCallNotificationChanged) Q_PROPERTY(bool notificationsCommhistoryd MEMBER notificationsCommhistoryd NOTIFY notificationsCommhistorydChanged) Q_PROPERTY(bool notificationsMissedCall MEMBER notificationsMissedCall NOTIFY notificationsMissedCallChanged) @@ -19,7 +19,7 @@ class Settings : public MDConfGroup Q_PROPERTY(bool notificationsOther MEMBER notificationsOther NOTIFY notificationsOtherChanged) Q_PROPERTY(bool notificationsAll MEMBER notificationsAll NOTIFY notificationsAllChanged) bool silentWhenConnected; - bool transliterateCyrillic; + bool transliterateMessage; bool incomingCallNotification; bool notificationsCommhistoryd; bool notificationsMissedCall; @@ -37,7 +37,7 @@ public: signals: void silentWhenConnectedChanged(bool); - void transliterateCyrillicChanged(bool); + void transliterateMessageChanged(bool); void incomingCallNotificationChanged(bool); void notificationsCommhistorydChanged(bool); void notificationsMissedCallChanged(bool); diff --git a/rpm/pebble.yaml b/rpm/pebble.yaml index b41bc4b..955d024 100644 --- a/rpm/pebble.yaml +++ b/rpm/pebble.yaml @@ -24,6 +24,7 @@ PkgConfigBR: - sailfishapp >= 0.0.10 PkgBR: - log4qt-devel +- libicu-devel Requires: - sailfishsilica-qt5 >= 0.10.9 - systemd-user-session-targets -- cgit v1.2.3