From 47eecba4daa471d6e2994262c4aafc5eeb73752e Mon Sep 17 00:00:00 2001 From: nowrep Date: Sun, 3 Feb 2013 18:14:57 +0100 Subject: [PATCH] Added QtWebKit platform plugin for notifications and spellcheck. It's mostly just copied implementation from qtwebkit-plugins repo. Spellcheck is only very basic. Spellcheck will be automaticaly compiled if pkg-config detects installed hunspell library. --- src/defines.pri | 2 +- src/lib/lib.pro | 7 +- src/lib/other/registerqappassociation.cpp | 2 - src/lib/other/registerqappassociation.h | 3 - .../notifications/notificationpresenter.cpp | 39 ++++ .../notifications/notificationpresenter.h | 31 +++ src/lib/plugins/qtwebkit/qtwebkit-plugins.pri | 24 +++ src/lib/plugins/qtwebkit/qtwebkitplugin.cpp | 59 ++++++ src/lib/plugins/qtwebkit/qtwebkitplugin.h | 39 ++++ .../qtwebkit/spellcheck/spellcheck.cpp | 183 ++++++++++++++++++ .../plugins/qtwebkit/spellcheck/spellcheck.h | 61 ++++++ .../plugins/qtwebkit/spellcheck/speller.cpp | 180 +++++++++++++++++ src/lib/plugins/qtwebkit/spellcheck/speller.h | 57 ++++++ 13 files changed, 679 insertions(+), 8 deletions(-) create mode 100644 src/lib/plugins/qtwebkit/notifications/notificationpresenter.cpp create mode 100644 src/lib/plugins/qtwebkit/notifications/notificationpresenter.h create mode 100644 src/lib/plugins/qtwebkit/qtwebkit-plugins.pri create mode 100644 src/lib/plugins/qtwebkit/qtwebkitplugin.cpp create mode 100644 src/lib/plugins/qtwebkit/qtwebkitplugin.h create mode 100644 src/lib/plugins/qtwebkit/spellcheck/spellcheck.cpp create mode 100644 src/lib/plugins/qtwebkit/spellcheck/spellcheck.h create mode 100644 src/lib/plugins/qtwebkit/spellcheck/speller.cpp create mode 100644 src/lib/plugins/qtwebkit/spellcheck/speller.h diff --git a/src/defines.pri b/src/defines.pri index 743894ff3..74ce441a4 100644 --- a/src/defines.pri +++ b/src/defines.pri @@ -17,7 +17,7 @@ win32-msvc* { # Check for pkg-config availability system(pkg-config --version > /dev/null) { - QTWEBKIT_VERSION = $$system(pkg-config --modversion QtWebKit) + QTWEBKIT_VERSION = $$system(PKG_CONFIG_PATH=$$[QT_INSTALL_LIBS]/pkgconfig pkg-config --modversion QtWebKit) QTWEBKIT_VERSION_MAJOR = $$section(QTWEBKIT_VERSION, ".", 0, 0) QTWEBKIT_VERSION_MINOR = $$section(QTWEBKIT_VERSION, ".", 1, 1) diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 68f795dab..89edca26a 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -202,7 +202,6 @@ SOURCES += \ network/schemehandlers/qupzillaschemehandler.cpp \ network/schemehandlers/adblockschemehandler.cpp \ network/schemehandlers/fileschemehandler.cpp \ - other/registerqappassociation.cpp \ tools/listitemdelegate.cpp \ bookmarks/bookmarkstree.cpp \ tools/html5permissions/html5permissionsmanager.cpp \ @@ -367,7 +366,6 @@ HEADERS += \ network/schemehandlers/qupzillaschemehandler.h \ network/schemehandlers/adblockschemehandler.h \ network/schemehandlers/fileschemehandler.h \ - other/registerqappassociation.h \ tools/listitemdelegate.h \ bookmarks/bookmarkstree.h \ tools/html5permissions/html5permissionsmanager.h \ @@ -438,6 +436,11 @@ RESOURCES += \ LIBS += -lX11 } +win { + HEADERS += other/registerqappassociation.h + SOURCES += other/registerqappassociation.cpp +} + message(===========================================) message( Using following defines:) message( $$DEFINES) diff --git a/src/lib/other/registerqappassociation.cpp b/src/lib/other/registerqappassociation.cpp index 8163bd77a..1b9b02b08 100644 --- a/src/lib/other/registerqappassociation.cpp +++ b/src/lib/other/registerqappassociation.cpp @@ -19,7 +19,6 @@ #include "registerqappassociation.h" -#ifdef Q_OS_WIN #include "ShlObj.h" #include #include @@ -347,4 +346,3 @@ bool RegisterQAppAssociation::isDefaultForAllCapabilities() } return result; } -#endif diff --git a/src/lib/other/registerqappassociation.h b/src/lib/other/registerqappassociation.h index aec16c308..52324824f 100644 --- a/src/lib/other/registerqappassociation.h +++ b/src/lib/other/registerqappassociation.h @@ -25,8 +25,6 @@ #include "qz_namespace.h" -#ifdef Q_OS_WIN - class QT_QUPZILLA_EXPORT RegisterQAppAssociation : public QObject { Q_OBJECT @@ -71,6 +69,5 @@ private: QHash _urlAssocHash; // (protocol, progId) QHash > _assocDescHash; // (progId, (desc, icon)) }; -#endif #endif // REGISTERQAPPASSOCIATION_H diff --git a/src/lib/plugins/qtwebkit/notifications/notificationpresenter.cpp b/src/lib/plugins/qtwebkit/notifications/notificationpresenter.cpp new file mode 100644 index 000000000..b369f91ec --- /dev/null +++ b/src/lib/plugins/qtwebkit/notifications/notificationpresenter.cpp @@ -0,0 +1,39 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "notificationpresenter.h" +#include "desktopnotificationsfactory.h" +#include "mainapplication.h" + +#include + +NotificationPresenter::NotificationPresenter() + : QWebNotificationPresenter() +{ +} + +void NotificationPresenter::showNotification(const QWebNotificationData* data) +{ + if (data->message().isEmpty()) { + qDebug() << "NotificationPresenter: HTML notifications are not supported."; + return; + } + + mApp->desktopNotifications()->showNotification(QPixmap(":qupzilla.png"), + data->title(), + data->message()); +} diff --git a/src/lib/plugins/qtwebkit/notifications/notificationpresenter.h b/src/lib/plugins/qtwebkit/notifications/notificationpresenter.h new file mode 100644 index 000000000..abf9f803c --- /dev/null +++ b/src/lib/plugins/qtwebkit/notifications/notificationpresenter.h @@ -0,0 +1,31 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef NOTIFICATIONPRESENTER_H +#define NOTIFICATIONPRESENTER_H + +#include "qwebkitplatformplugin.h" + +class NotificationPresenter : public QWebNotificationPresenter +{ +public: + explicit NotificationPresenter(); + + void showNotification(const QWebNotificationData* data); +}; + +#endif // NOTIFICATIONPRESENTER_H diff --git a/src/lib/plugins/qtwebkit/qtwebkit-plugins.pri b/src/lib/plugins/qtwebkit/qtwebkit-plugins.pri new file mode 100644 index 000000000..d95e2cfa4 --- /dev/null +++ b/src/lib/plugins/qtwebkit/qtwebkit-plugins.pri @@ -0,0 +1,24 @@ +HEADERS += $$PWD/qtwebkitplugin.h \ + $$PWD/notifications/notificationpresenter.h \ + $$[QT_INSTALL_HEADERS]/QtWebKit/qwebkitplatformplugin.h + + +SOURCES += $$PWD/qtwebkitplugin.cpp \ + $$PWD/notifications/notificationpresenter.cpp \ + +DEFINES *= QT_STATICPLUGIN + +unix:contains(DEFINES, USE_QTWEBKIT_2_3):system(pkg-config --exists hunspell) { + HEADERS += $$PWD/spellcheck/spellcheck.h \ + $$PWD/spellcheck/speller.h \ + + SOURCES += $$PWD/spellcheck/spellcheck.cpp \ + $$PWD/spellcheck/speller.cpp \ + + DEFINES *= USE_HUNSPELL + LIBS += $$system(pkg-config --libs hunspell) +} + +win{ +# TODO +} diff --git a/src/lib/plugins/qtwebkit/qtwebkitplugin.cpp b/src/lib/plugins/qtwebkit/qtwebkitplugin.cpp new file mode 100644 index 000000000..07ae8414d --- /dev/null +++ b/src/lib/plugins/qtwebkit/qtwebkitplugin.cpp @@ -0,0 +1,59 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "qtwebkitplugin.h" +#include "notifications/notificationpresenter.h" + +#ifdef USE_HUNSPELL +#include "spellcheck/spellcheck.h" +#endif + +#include + +QtWebKitPlugin::QtWebKitPlugin() +{ +} + +bool QtWebKitPlugin::supportsExtension(Extension ext) const +{ + return (ext == Notifications +#ifdef USE_HUNSPELL + || ext == SpellChecker +#endif + ); +} + +QObject* QtWebKitPlugin::createExtension(Extension ext) const +{ + switch (ext) { +#ifdef USE_HUNSPELL + case SpellChecker: + return new SpellCheck(); +#endif + case Notifications: + return new NotificationPresenter(); + + default: + return 0; + } +} + +#if QT_VERSION < 0x050000 +Q_EXPORT_PLUGIN2(qtwebkitplugins, QtWebKitPlugin) +#endif + +Q_IMPORT_PLUGIN(qtwebkitplugins) diff --git a/src/lib/plugins/qtwebkit/qtwebkitplugin.h b/src/lib/plugins/qtwebkit/qtwebkitplugin.h new file mode 100644 index 000000000..e27d51bdc --- /dev/null +++ b/src/lib/plugins/qtwebkit/qtwebkitplugin.h @@ -0,0 +1,39 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef QTWEBKITPLUGIN_H +#define QTWEBKITPLUGIN_H + +#include "qwebkitplatformplugin.h" + +class QtWebKitPlugin : public QObject, public QWebKitPlatformPlugin +{ + Q_OBJECT + Q_INTERFACES(QWebKitPlatformPlugin) + +#if QT_VERSION >= 0x050000 + Q_PLUGIN_METADATA(IID "org.qtwebkit.QtWebKit.QtWebKitPlugins") +#endif + +public: + explicit QtWebKitPlugin(); + + bool supportsExtension(Extension ext) const; + QObject* createExtension(Extension ext) const; +}; + +#endif // QTWEBKITPLUGIN_H diff --git a/src/lib/plugins/qtwebkit/spellcheck/spellcheck.cpp b/src/lib/plugins/qtwebkit/spellcheck/spellcheck.cpp new file mode 100644 index 000000000..170af63f0 --- /dev/null +++ b/src/lib/plugins/qtwebkit/spellcheck/spellcheck.cpp @@ -0,0 +1,183 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ + +// Based on https://gitorious.org/kdewebkit-spellcheck + +#include "spellcheck.h" +#include "speller.h" + +SpellCheck::SpellCheck() + : QWebSpellChecker() + , m_speller(0) +{ + m_speller = new Speller(); + + if (!m_speller->initialize()) { + delete m_speller; + m_speller = 0; + } +} + +bool SpellCheck::isContinousSpellCheckingEnabled() const +{ + return true; +} + +void SpellCheck::toggleContinousSpellChecking() +{ +} + +void SpellCheck::learnWord(const QString &word) +{ + Q_UNUSED(word); +} + +void SpellCheck::ignoreWordInSpellDocument(const QString &word) +{ + Q_UNUSED(word); +} + +void SpellCheck::checkSpellingOfString(const QString &word, + int* misspellingLocation, int* misspellingLength) +{ + if (misspellingLocation == NULL || misspellingLength == NULL || !m_speller) { + return; + } + + *misspellingLocation = -1; + *misspellingLength = 0; + + QTextBoundaryFinder finder = QTextBoundaryFinder(QTextBoundaryFinder::Word, word); + + QTextBoundaryFinder::BoundaryReasons boundary = finder.boundaryReasons(); + int start = finder.position(), end = finder.position(); + bool inWord = startOfWord(boundary, finder.type()); + + while (finder.toNextBoundary() > 0) { + boundary = finder.boundaryReasons(); + + if (endOfWord(boundary, finder.type()) && inWord) { + end = finder.position(); + QString str = finder.string().mid(start, end - start); + if (isValidWord(str)) { + if (m_speller->isMisspelled(str)) { + *misspellingLocation = start; + *misspellingLength = end - start; + } + return; + } + inWord = false; + } + + if (startOfWord(boundary, finder.type())) { + start = finder.position(); + inWord = true; + } + } +} + +QString SpellCheck::autoCorrectSuggestionForMisspelledWord(const QString &word) +{ + /* Auto correcting mispelled words is really not a great idea */ + Q_UNUSED(word) + return QString(); +#if 0 + QStringList words = m_speller->suggest(word); + if (words.size() > 0) { + return words[0]; + } + else { + return QString(); + } + + return QString(); +#endif +} + +void SpellCheck::guessesForWord(const QString &word, + const QString &context, QStringList &guesses) +{ + Q_UNUSED(context); + + if (!m_speller) { + return; + } + + QStringList words = m_speller->suggest(word); + guesses = words; +} + +bool SpellCheck::isGrammarCheckingEnabled() +{ + return false; +} + +void SpellCheck::toggleGrammarChecking() +{ +} + +void SpellCheck::checkGrammarOfString(const QString &, QList &, + int* badGrammarLocation, int* badGrammarLength) +{ + Q_UNUSED(badGrammarLocation); + Q_UNUSED(badGrammarLength); +} + +bool SpellCheck::isValidWord(const QString &str) +{ + if (str.isEmpty() || (str.length() == 1 && !str[0].isLetter())) { + return false; + } + const int length = str.length(); + for (int i = 0; i < length; ++i) { + if (!str[i].isNumber()) { + return true; + } + } + // 'str' only contains numbers + return false; +} + +bool SpellCheck::endOfWord(const QTextBoundaryFinder::BoundaryReasons &reasons, + const QTextBoundaryFinder::BoundaryType &type) +{ +#if QT_VERSION < 0x050000 + Q_UNUSED(type) + return reasons & QTextBoundaryFinder::EndWord; +#else + return (reasons & QTextBoundaryFinder::EndOfItem) && + (type & QTextBoundaryFinder::Word); +#endif +} + +bool SpellCheck::startOfWord(const QTextBoundaryFinder::BoundaryReasons &reasons, + const QTextBoundaryFinder::BoundaryType &type) +{ +#if QT_VERSION < 0x050000 + Q_UNUSED(type) + return reasons & QTextBoundaryFinder::StartWord; +#else + return (reasons & QTextBoundaryFinder::StartOfItem) && + (type & QTextBoundaryFinder::Word); +#endif +} + +SpellCheck::~SpellCheck() +{ + delete m_speller; +} diff --git a/src/lib/plugins/qtwebkit/spellcheck/spellcheck.h b/src/lib/plugins/qtwebkit/spellcheck/spellcheck.h new file mode 100644 index 000000000..409d45154 --- /dev/null +++ b/src/lib/plugins/qtwebkit/spellcheck/spellcheck.h @@ -0,0 +1,61 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef SPELLCHECKER_H +#define SPELLCHECKER_H + +#include +#include "qwebkitplatformplugin.h" + +class Speller; + +class SpellCheck : public QWebSpellChecker +{ + Q_OBJECT + +public: + SpellCheck(); + ~SpellCheck(); + + bool isContinousSpellCheckingEnabled() const; + void toggleContinousSpellChecking(); + + void learnWord(const QString &word); + void ignoreWordInSpellDocument(const QString &word); + void checkSpellingOfString(const QString &word, + int* misspellingLocation, int* misspellingLength); + QString autoCorrectSuggestionForMisspelledWord(const QString &word); + void guessesForWord(const QString &word, + const QString &context, QStringList &guesses); + + bool isGrammarCheckingEnabled(); + void toggleGrammarChecking(); + void checkGrammarOfString(const QString &, QList &, + int* badGrammarLocation, int* badGrammarLength); + +private: + bool isValidWord(const QString &str); + bool endOfWord(const QTextBoundaryFinder::BoundaryReasons &reasons, + const QTextBoundaryFinder::BoundaryType &type); + bool startOfWord(const QTextBoundaryFinder::BoundaryReasons &reasons, + const QTextBoundaryFinder::BoundaryType &type); + + Speller* m_speller; + +}; + +#endif // SPELLCHECKER_H diff --git a/src/lib/plugins/qtwebkit/spellcheck/speller.cpp b/src/lib/plugins/qtwebkit/spellcheck/speller.cpp new file mode 100644 index 000000000..2b4cb17b4 --- /dev/null +++ b/src/lib/plugins/qtwebkit/spellcheck/speller.cpp @@ -0,0 +1,180 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "speller.h" + +#include +#include +#include +#include +#include +#include + +#include + +Hunspell* Speller::s_hunspell = 0; +QTextCodec* Speller::s_codec = 0; +QString Speller::s_dictionaryPath; +QString Speller::s_langugage; +bool Speller::s_initialized = false; + +Speller::Speller() +{ +} + +bool Speller::initialize() +{ + if (s_initialized) { + return s_hunspell != 0; + } + + s_dictionaryPath = getDictionaryPath(); + s_langugage = parseLanguage(s_dictionaryPath); + + if (s_dictionaryPath.isEmpty() || s_langugage.isEmpty()) { + qWarning() << "SpellCheck: Initialization failed!"; + return false; + } + + QString dicPath = s_dictionaryPath + ".dic"; + QString affPath = s_dictionaryPath + ".aff"; + + s_hunspell = new Hunspell(affPath.toLocal8Bit().constData(), + dicPath .toLocal8Bit().constData()); + + s_codec = QTextCodec::codecForName(s_hunspell->get_dic_encoding()); + + qDebug() << "SpellCheck: Language =" << language(); + s_initialized = true; + return true; +} + +QString Speller::backend() const +{ + return QString("Hunspell"); +} + +QString Speller::language() const +{ + return s_langugage; +} + +void Speller::learnWord(const QString &word) +{ + const char* encodedWord = s_codec->fromUnicode(word).constData(); + s_hunspell->add(encodedWord); +} + +void Speller::ignoreWordInSpellDocument(const QString &word) +{ + m_ignoredWords.append(word); +} + +bool Speller::isMisspelled(const QString &string) +{ + if (m_ignoredWords.contains(string)) { + return false; + } + + const char* encodedString = s_codec->fromUnicode(string).constData(); + return s_hunspell->spell(encodedString) == 0; +} + +QStringList Speller::suggest(const QString &word) +{ + char** suggestions; + const char* encodedWord = s_codec->fromUnicode(word).constData(); + int count = s_hunspell->suggest(&suggestions, encodedWord); + + QStringList suggests; + for (int i = 0; i < count; ++i) { + suggests.append(s_codec->toUnicode(suggestions[i])); + } + s_hunspell->free_list(&suggestions, count); + + return suggests; +} + +bool Speller::dictionaryExists(const QString &path) +{ + return QFile(path + ".dic").exists() && + QFile(path + ".aff").exists(); +} + +QString Speller::parseLanguage(const QString &path) +{ + if (path.contains(QLatin1Char('/'))) { + int pos = path.lastIndexOf(QLatin1Char('/')); + return path.mid(pos + 1); + } + else { + return path; + } +} + +QString Speller::getDictionaryPath() +{ + QString dictName; + QString defaultDicPath = "/usr/share/hunspell/"; + + QString env = QString::fromLocal8Bit(qgetenv("DICTIONARY")); + if (!env.isEmpty()) { + if (env.contains(QLatin1Char(','))) { + dictName = env.split(QLatin1Char(',')).first().trimmed(); + } + else { + dictName = env.trimmed(); + } + + if (dictName.contains(QLatin1Char('/')) && dictionaryExists(dictName)) { + return dictName; + } + } + + QString dicPath = QString::fromLocal8Bit(qgetenv("DICPATH")); + if (!dicPath.isEmpty() && !dicPath.endsWith(QLatin1Char('/'))) { + dicPath.append(QLatin1Char('/')); + } + + if (!dicPath.isEmpty() && dictionaryExists(dicPath + dictName)) { + return dicPath + dictName; + } + + if (!dictName.isEmpty()) { + if (dictionaryExists(defaultDicPath + dictName)) { + return defaultDicPath + dictName; + } + } + + QString locale = QLocale::system().name(); + + if (dictionaryExists(dicPath + locale)) { + return dicPath + locale; + } + else if (dictionaryExists(defaultDicPath + locale)) { + return defaultDicPath + locale; + } + else { + qWarning() << "SpellCheck: Cannot find dictionaries for" << defaultDicPath + locale; + } + + return QString(); +} + +Speller::~Speller() +{ +} diff --git a/src/lib/plugins/qtwebkit/spellcheck/speller.h b/src/lib/plugins/qtwebkit/spellcheck/speller.h new file mode 100644 index 000000000..7c910a4f0 --- /dev/null +++ b/src/lib/plugins/qtwebkit/spellcheck/speller.h @@ -0,0 +1,57 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef SPELLER_H +#define SPELLER_H + +#include + +class QTextCodec; +class Hunspell; + +class Speller +{ +public: + explicit Speller(); + ~Speller(); + + bool initialize(); + + QString backend() const; + QString language() const; + + void learnWord(const QString &word); + void ignoreWordInSpellDocument(const QString &word); + + bool isMisspelled(const QString &string); + QStringList suggest(const QString &word); + +private: + bool dictionaryExists(const QString &path); + QString parseLanguage(const QString &path); + QString getDictionaryPath(); + + static Hunspell* s_hunspell; + static QTextCodec* s_codec; + static QString s_dictionaryPath; + static QString s_langugage; + static bool s_initialized; + + QStringList m_ignoredWords; +}; + +#endif // SPELLER_H