diff options
| author | Basil Semuonov <basil.semuonov@gmail.com> | 2014-08-10 16:33:24 +0400 |
|---|---|---|
| committer | Basil Semuonov <basil.semuonov@gmail.com> | 2014-08-10 16:33:24 +0400 |
| commit | 7c35daca74fb4a4f8ed7e2fadc22ec18e0295744 (patch) | |
| tree | e652d0f728c1f0c396518caea009acdd744ff03d /daemon | |
| parent | c460e2d73be49313fb989fe9cbc4f1ae2c4d4d5b (diff) | |
General transliteration with ICU rule "Any-Latin; Latin-ASCII"
Diffstat (limited to 'daemon')
| -rw-r--r-- | daemon/daemon.pro | 1 | ||||
| -rw-r--r-- | daemon/manager.cpp | 81 | ||||
| -rw-r--r-- | daemon/manager.h | 6 | ||||
| -rw-r--r-- | daemon/settings.h | 6 |
4 files changed, 42 insertions, 52 deletions
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<QString&>(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<QString&>(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 <MNotification> #include <Log4Qt/Logger> +#include <unicode/translit.h> + using namespace QtContacts; class Manager : @@ -50,6 +52,8 @@ class Manager : QString lastSeenMpris; + QScopedPointer<icu::Transliterator> 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); |
