1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

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.
This commit is contained in:
nowrep 2013-02-03 18:14:57 +01:00
parent c0c0d51b25
commit 47eecba4da
13 changed files with 679 additions and 8 deletions

View File

@ -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)

View File

@ -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)

View File

@ -19,7 +19,6 @@
#include "registerqappassociation.h"
#ifdef Q_OS_WIN
#include "ShlObj.h"
#include <QMessageBox>
#include <QStringList>
@ -347,4 +346,3 @@ bool RegisterQAppAssociation::isDefaultForAllCapabilities()
}
return result;
}
#endif

View File

@ -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<QString, QString> _urlAssocHash; // (protocol, progId)
QHash<QString, QPair<QString, QString> > _assocDescHash; // (progId, (desc, icon))
};
#endif
#endif // REGISTERQAPPASSOCIATION_H

View File

@ -0,0 +1,39 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "notificationpresenter.h"
#include "desktopnotificationsfactory.h"
#include "mainapplication.h"
#include <QDebug>
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());
}

View File

@ -0,0 +1,31 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef NOTIFICATIONPRESENTER_H
#define NOTIFICATIONPRESENTER_H
#include "qwebkitplatformplugin.h"
class NotificationPresenter : public QWebNotificationPresenter
{
public:
explicit NotificationPresenter();
void showNotification(const QWebNotificationData* data);
};
#endif // NOTIFICATIONPRESENTER_H

View File

@ -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
}

View File

@ -0,0 +1,59 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qtwebkitplugin.h"
#include "notifications/notificationpresenter.h"
#ifdef USE_HUNSPELL
#include "spellcheck/spellcheck.h"
#endif
#include <QtPlugin>
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)

View File

@ -0,0 +1,39 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#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

View File

@ -0,0 +1,183 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
// 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<GrammarDetail> &,
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;
}

View File

@ -0,0 +1,61 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef SPELLCHECKER_H
#define SPELLCHECKER_H
#include <QTextBoundaryFinder>
#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<GrammarDetail> &,
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

View File

@ -0,0 +1,180 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "speller.h"
#include <QStringList>
#include <QTextCodec>
#include <QTextStream>
#include <QFile>
#include <QRegExp>
#include <QDebug>
#include <hunspell/hunspell.hxx>
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()
{
}

View File

@ -0,0 +1,57 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef SPELLER_H
#define SPELLER_H
#include <QStringList>
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