1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

improved code

This commit is contained in:
Anmol Gautam 2018-05-28 22:54:57 +05:30
parent be46b0f84c
commit 8e7dc4e103
30 changed files with 204 additions and 341 deletions

View File

@ -15,8 +15,7 @@
* 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 QMLBOOKMARKSAPITEST_H
#define QMLBOOKMARKSAPITEST_H
#pragma once
#include <QObject>
@ -36,6 +35,3 @@ private Q_SLOTS:
void testBookmarksModification();
void testBookmarksRemoval();
};
#endif // QMLBOOKMARKSAPITEST_H

View File

@ -15,8 +15,7 @@
* 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 QMLCOOKIESAPITEST_H
#define QMLCOOKIESAPITEST_H
#pragma once
#include <QObject>
@ -31,6 +30,3 @@ private Q_SLOTS:
void testCookieAdditionRemoval();
void testCookieGet();
};
#endif // QMLCOOKIESAPITEST_H

View File

@ -15,8 +15,7 @@
* 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 QMLHISTORYAPITEST_H
#define QMLHSITORYAPITEST_H
#pragma once
#include <QObject>
@ -33,6 +32,3 @@ private Q_SLOTS:
void testVisits();
void testRemoval();
};
#endif // QMLHSITORYAPITEST_H

View File

@ -15,8 +15,7 @@
* 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 QMLTESTHELPER_H
#define QMLTESTHELPER_H
#pragma once
#include "qmltestitem.h"
#include <QQmlEngine>
@ -30,5 +29,3 @@ public:
QQmlEngine engine;
QmlTestItem *testItem;
};
#endif // QMLTESTHELPER_H

View File

@ -15,8 +15,7 @@
* 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 QMLTESTITEM_H
#define QMLTESTITEM_H
#pragma once
#include <QObject>
#include <QJSValue>
@ -35,5 +34,3 @@ public:
private:
QJSValue m_evalFunc;
};
#endif // QMLTESTITEM_H

View File

@ -15,8 +15,7 @@
* 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 QMLTOPSITESAPITEST_H
#define QMLTOPSITESAPITEST_H
#pragma once
#include <QObject>
@ -30,6 +29,3 @@ private Q_SLOTS:
void testTopSites();
};
#endif // QMLTOPSITESAPITEST_H

View File

@ -209,7 +209,7 @@ void Plugins::loadAvailablePlugins()
}
// QmlPlugin
for (QString dir: dirs) {
for (QString dir : dirs) {
// qml plugins will be loaded from subdirectory qml
dir.append(QSL("/qml"));
const auto qmlDirs = QDir(dir).entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
@ -389,11 +389,6 @@ Plugins::Plugin Plugins::loadQmlPlugin(const QString &name)
plugin.pluginId = QSL("qml:%1").arg(QFileInfo(name).fileName());
plugin.pluginSpec = createSpec(DesktopFile(fullPath + QSL("/metadata.desktop")));
plugin.qmlPluginLoader = new QmlPluginLoader(QDir(fullPath).filePath(plugin.pluginSpec.entryPoint));
if (!plugin.qmlPluginLoader->instance()) {
qWarning() << "Loading" << fullPath << "failed:" << plugin.qmlPluginLoader->component()->errorString();
return Plugin();
}
return plugin;
}
@ -475,7 +470,15 @@ void Plugins::initQmlPlugin(Plugin *plugin)
{
Q_ASSERT(plugin->type == Plugin::QmlPlugin);
plugin->qmlPluginLoader->setName(plugin->pluginSpec.name);
const QString name = plugin->pluginSpec.name;
plugin->qmlPluginLoader->createComponent();
if (!plugin->qmlPluginLoader->instance()) {
qWarning() << "Initializing" << name << "failed:" << plugin->qmlPluginLoader->component()->errorString();
return;
}
plugin->qmlPluginLoader->setName(name);
plugin->instance = qobject_cast<PluginInterface*>(plugin->qmlPluginLoader->instance());
}

View File

@ -20,26 +20,26 @@
#include <QDebug>
QmlBookmarks::QmlBookmarks(QObject *parent) :
QObject(parent)
QmlBookmarks::QmlBookmarks(QObject *parent)
: QObject(parent)
{
connect(mApp->bookmarks(), &Bookmarks::bookmarkAdded, this, [=](BookmarkItem *item){
connect(mApp->bookmarks(), &Bookmarks::bookmarkAdded, this, [this](BookmarkItem *item){
auto treeNode = QmlBookmarkTreeNode::fromBookmarkItem(item);
emit created(treeNode);
});
connect(mApp->bookmarks(), &Bookmarks::bookmarkChanged, this, [=](BookmarkItem *item){
connect(mApp->bookmarks(), &Bookmarks::bookmarkChanged, this, [this](BookmarkItem *item){
auto treeNode = QmlBookmarkTreeNode::fromBookmarkItem(item);
emit changed(treeNode);
});
connect(mApp->bookmarks(), &Bookmarks::bookmarkRemoved, this, [=](BookmarkItem *item){
connect(mApp->bookmarks(), &Bookmarks::bookmarkRemoved, this, [this](BookmarkItem *item){
auto treeNode = QmlBookmarkTreeNode::fromBookmarkItem(item);
emit removed(treeNode);
});
}
BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode)
BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode) const
{
auto bookmarks = mApp->bookmarks();
QList<BookmarkItem*> items;
@ -60,7 +60,7 @@ BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode)
items = bookmarks->searchBookmarks(treeNode->title());
} else {
items = bookmarks->searchBookmarks(QUrl(treeNode->url()));
items = bookmarks->searchBookmarks(QUrl::fromEncoded(treeNode->url().toUtf8()));
}
for (auto item : items) {
@ -72,7 +72,7 @@ BookmarkItem *QmlBookmarks::getBookmarkItem(QmlBookmarkTreeNode *treeNode)
return nullptr;
}
BookmarkItem *QmlBookmarks::getBookmarkItem(QObject *object)
BookmarkItem *QmlBookmarks::getBookmarkItem(QObject *object) const
{
auto treeNode = qobject_cast<QmlBookmarkTreeNode*>(object);
if (!treeNode) {
@ -87,7 +87,7 @@ BookmarkItem *QmlBookmarks::getBookmarkItem(QObject *object)
return item;
}
bool QmlBookmarks::isBookmarked(const QUrl &url)
bool QmlBookmarks::isBookmarked(const QUrl &url) const
{
return mApp->bookmarks()->isBookmarked(url);
}
@ -121,19 +121,19 @@ QmlBookmarkTreeNode *QmlBookmarks::lastUsedFolder() const
return QmlBookmarkTreeNode::fromBookmarkItem(mApp->bookmarks()->lastUsedFolder());
}
bool QmlBookmarks::create(const QVariantMap &map)
bool QmlBookmarks::create(const QVariantMap &map) const
{
if (!map["parent"].isValid()) {
if (!map.contains(QSL("parent"))) {
qWarning() << "Unable to create new bookmark:" << "parent not found";
return false;
}
QString title = map["title"].toString();
QString urlString = map["url"].toString();
QString description = map["description"].toString();
QString title = map.value(QSL("title")).toString();
QString urlString = map.value(QSL("url")).toString();
QString description = map.value(QSL("description")).toString();
BookmarkItem::Type type;
if (map["type"].isValid()) {
type = BookmarkItem::Type(map["type"].toInt());
if (map.contains(QSL("type"))) {
type = BookmarkItem::Type(map.value(QSL("type")).toInt());
} else if (urlString.isEmpty()){
if (!title.isEmpty()) {
type = BookmarkItem::Folder;
@ -144,8 +144,7 @@ bool QmlBookmarks::create(const QVariantMap &map)
type = BookmarkItem::Url;
}
// FIXME: try removing qvariant_cast
auto object = qvariant_cast<QObject*>(map["parent"]);
auto object = map.value(QSL("parent")).value<QObject*>();
auto parent = getBookmarkItem(object);
if (!parent) {
qWarning() << "Unable to create new bookmark:" << "parent not found";
@ -153,15 +152,15 @@ bool QmlBookmarks::create(const QVariantMap &map)
}
auto item = new BookmarkItem(type);
item->setTitle(title);
item->setUrl(QUrl(urlString));
item->setUrl(QUrl::fromEncoded(urlString.toUtf8()));
item->setDescription(description);
mApp->bookmarks()->addBookmark(parent, item);
return true;
}
bool QmlBookmarks::remove(QObject *object)
bool QmlBookmarks::remove(QmlBookmarkTreeNode *treeNode) const
{
auto item = getBookmarkItem(object);
auto item = getBookmarkItem(treeNode);
if (!item) {
qWarning() << "Unable to remove bookmark:" <<"BookmarkItem not found";
return false;
@ -170,20 +169,20 @@ bool QmlBookmarks::remove(QObject *object)
return true;
}
QList<QObject*> QmlBookmarks::search(const QVariantMap &map)
QList<QObject*> QmlBookmarks::search(const QVariantMap &map) const
{
if (!map["query"].isValid() && !map["url"].isValid()) {
if (!map.contains(QSL("query")) && !map.contains(QSL("url"))) {
qWarning() << "Unable to search bookmarks";
return QList<QObject*>();
}
QString query = map["query"].toString();
QString urlString = map["url"].toString();
QString query = map.value(QSL("query")).toString();
QString urlString = map.value(QSL("url")).toString();
QList<BookmarkItem*> items;
if (urlString.isEmpty()) {
items = mApp->bookmarks()->searchBookmarks(query);
} else {
items = mApp->bookmarks()->searchBookmarks(QUrl(urlString));
items = mApp->bookmarks()->searchBookmarks(QUrl::fromEncoded(urlString.toUtf8()));
}
QList<QObject*> ret;
for (auto item : items) {
@ -192,7 +191,7 @@ QList<QObject*> QmlBookmarks::search(const QVariantMap &map)
return ret;
}
bool QmlBookmarks::update(QObject *object, const QVariantMap &changes)
bool QmlBookmarks::update(QObject *object, const QVariantMap &changes) const
{
auto treeNode = qobject_cast<QmlBookmarkTreeNode*>(object);
if (!treeNode) {
@ -210,9 +209,18 @@ bool QmlBookmarks::update(QObject *object, const QVariantMap &changes)
qWarning() << "Unable to update bookmark:" << "bookmark can not be modified";
}
QString newTitle = changes["title"].toString().isNull() ? treeNode->title() : changes["title"].toString();
QString newDescription = changes["description"].toString().isNull() ? treeNode->description() : changes["description"].toString();
QString newKeyword = changes["keyword"].toString().isNull() ? treeNode->keyword() : changes["keyword"].toString();
QString newTitle = treeNode->title();
if (changes.contains(QSL("title"))) {
newTitle = changes.value(QSL("title")).toString();
}
QString newDescription = treeNode->description();
if (changes.contains(QSL("description"))) {
newDescription = changes.value(QSL("description")).toString();
}
QString newKeyword = treeNode->keyword();
if (changes.contains(QSL("keyword"))) {
newKeyword = changes.value(QSL("keyword")).toString();
}
item->setTitle(newTitle);
item->setDescription(newDescription);
@ -221,7 +229,7 @@ bool QmlBookmarks::update(QObject *object, const QVariantMap &changes)
return true;
}
QmlBookmarkTreeNode *QmlBookmarks::get(const QString &string)
QmlBookmarkTreeNode *QmlBookmarks::get(const QString &string) const
{
auto items = mApp->bookmarks()->searchBookmarks(QUrl(string));
for (auto item : items) {
@ -233,7 +241,7 @@ QmlBookmarkTreeNode *QmlBookmarks::get(const QString &string)
return new QmlBookmarkTreeNode();
}
QList<QObject*> QmlBookmarks::getChildren(QObject *object)
QList<QObject*> QmlBookmarks::getChildren(QObject *object) const
{
QList<QObject*> ret;
@ -251,7 +259,7 @@ QList<QObject*> QmlBookmarks::getChildren(QObject *object)
return ret;
}
bool QmlBookmarks::isTreeNodeEqualsItem(QmlBookmarkTreeNode *treeNode, BookmarkItem *item)
bool QmlBookmarks::isTreeNodeEqualsItem(QmlBookmarkTreeNode *treeNode, BookmarkItem *item) const
{
return treeNode->title() == item->title()
&& treeNode->url() == item->urlString()

View File

@ -15,8 +15,7 @@
* 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 QMLBOOKMARKS_H
#define QMLBOOKMARKS_H
#pragma once
#include "qmlbookmarktreenode.h"
#include "mainapplication.h"
@ -28,29 +27,28 @@ class QmlBookmarks : public QObject
Q_OBJECT
public:
QmlBookmarks(QObject *parent = 0);
explicit QmlBookmarks(QObject *parent = 0);
Q_INVOKABLE bool isBookmarked(const QUrl &url);
Q_INVOKABLE bool isBookmarked(const QUrl &url) const;
Q_INVOKABLE QmlBookmarkTreeNode *rootItem() const;
Q_INVOKABLE QmlBookmarkTreeNode *toolbarFolder() const;
Q_INVOKABLE QmlBookmarkTreeNode *menuFolder() const;
Q_INVOKABLE QmlBookmarkTreeNode *unsortedFolder() const;
Q_INVOKABLE QmlBookmarkTreeNode *lastUsedFolder() const;
Q_INVOKABLE bool create(const QVariantMap &map);
Q_INVOKABLE bool remove(QObject *object);
Q_INVOKABLE QList<QObject*> search(const QVariantMap &map);
Q_INVOKABLE bool update(QObject *object, const QVariantMap &changes);
Q_INVOKABLE QmlBookmarkTreeNode *get(const QString &string);
Q_INVOKABLE QList<QObject*> getChildren(QObject *object);
signals:
Q_INVOKABLE bool create(const QVariantMap &map) const;
Q_INVOKABLE bool remove(QmlBookmarkTreeNode *treeNode) const;
Q_INVOKABLE QList<QObject*> search(const QVariantMap &map) const;
Q_INVOKABLE bool update(QObject *object, const QVariantMap &changes) const;
Q_INVOKABLE QmlBookmarkTreeNode *get(const QString &string) const;
Q_INVOKABLE QList<QObject*> getChildren(QObject *object) const;
Q_SIGNALS:
void created(QmlBookmarkTreeNode *treeNode);
void changed(QmlBookmarkTreeNode *treeNode);
void removed(QmlBookmarkTreeNode *treeNode);
private:
bool isTreeNodeEqualsItem(QmlBookmarkTreeNode *treeNode, BookmarkItem *item);
BookmarkItem *getBookmarkItem(QmlBookmarkTreeNode *treeNode);
BookmarkItem *getBookmarkItem(QObject *object);
bool isTreeNodeEqualsItem(QmlBookmarkTreeNode *treeNode, BookmarkItem *item) const;
BookmarkItem *getBookmarkItem(QmlBookmarkTreeNode *treeNode) const;
BookmarkItem *getBookmarkItem(QObject *object) const;
};
#endif // QMLBOOKMARKS_H

View File

@ -19,8 +19,8 @@
#include "mainapplication.h"
#include "bookmarks.h"
QmlBookmarkTreeNode::QmlBookmarkTreeNode(BookmarkItem *item) :
m_item(item)
QmlBookmarkTreeNode::QmlBookmarkTreeNode(BookmarkItem *item)
: m_item(item)
{
}

View File

@ -15,27 +15,12 @@
* 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 QMLBOOKMARKTREENODE_H
#define QMLBOOKMARKTREENODE_H
#pragma once
#include "bookmarkitem.h"
class QmlBookmarkTreeNode : public QObject
{
Q_OBJECT
public:
QmlBookmarkTreeNode(BookmarkItem *item = nullptr);
enum Type {
Root = BookmarkItem::Root,
Url = BookmarkItem::Url,
Folder = BookmarkItem::Folder,
Separator = BookmarkItem::Separator,
Invalid = BookmarkItem::Invalid
};
Q_ENUMS(Type)
Q_PROPERTY(Type type READ type CONSTANT)
Q_PROPERTY(QString title READ title CONSTANT)
Q_PROPERTY(QString url READ url CONSTANT)
@ -46,6 +31,18 @@ public:
Q_PROPERTY(bool unmodifiable READ unmodifiable CONSTANT)
Q_PROPERTY(QList<QObject*> children READ children CONSTANT)
public:
enum Type {
Root = BookmarkItem::Root,
Url = BookmarkItem::Url,
Folder = BookmarkItem::Folder,
Separator = BookmarkItem::Separator,
Invalid = BookmarkItem::Invalid
};
Q_ENUMS(Type)
explicit QmlBookmarkTreeNode(BookmarkItem *item = nullptr);
Type type() const;
QString title() const;
QString url() const;
@ -61,5 +58,3 @@ public:
private:
BookmarkItem *m_item;
};
#endif // QMLBOOKMARKTREENODE_H

View File

@ -18,9 +18,9 @@
#include "qmlcookie.h"
#include <QDebug>
QmlCookie::QmlCookie(QNetworkCookie *cookie, QObject *parent) :
QObject(parent)
, m_cookie(cookie)
QmlCookie::QmlCookie(QNetworkCookie *cookie, QObject *parent)
: QObject(parent)
, m_cookie(cookie)
{
}

View File

@ -15,8 +15,7 @@
* 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 QMLCOOKIE_H
#define QMLCOOKIE_H
#pragma once
#include <QObject>
#include <QDateTime>
@ -44,5 +43,3 @@ public:
private:
QNetworkCookie *m_cookie;
};
#endif // QMLCOOKIE_H

View File

@ -20,16 +20,16 @@
#include "cookiejar.h"
#include "qwebengineprofile.h"
QmlCookies::QmlCookies(QObject *parent) :
QObject(parent)
QmlCookies::QmlCookies(QObject *parent)
: QObject(parent)
{
connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [=](QNetworkCookie network_cookie){
connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [this](QNetworkCookie network_cookie){
// FIXME: improve this
QmlCookie *cookie = new QmlCookie(new QNetworkCookie(network_cookie));
emit changed(cookie, false);
});
connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [=](QNetworkCookie network_cookie){
connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [this](QNetworkCookie network_cookie){
// FIXME: improve this
QmlCookie *cookie = new QmlCookie(new QNetworkCookie(network_cookie));
emit changed(cookie, true);
@ -38,12 +38,12 @@ QmlCookies::QmlCookies(QObject *parent) :
QNetworkCookie *QmlCookies::getNetworkCookie(const QVariantMap &map)
{
if (!map["name"].isValid() || !map["url"].isValid()) {
if (!map.contains(QSL("name")) || !map.contains(QSL("url"))) {
qWarning() << "Error:" << "Wrong arguments passed to" << __FUNCTION__;
return nullptr;
}
QString name = map["name"].toString();
QString url = map["url"].toString();
QString name = map.value(QSL("name")).toString();
QString url = map.value(QSL("url")).toString();
QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
for (QNetworkCookie cookie : cookies) {
if (cookie.name() == name && cookie.domain() == url) {
@ -66,18 +66,18 @@ QmlCookie *QmlCookies::get(const QVariantMap &map)
QList<QObject*> QmlCookies::getAll(const QVariantMap &map)
{
QList<QObject*> qmlCookies;
QString name = map["name"].toString();
QString url = map["url"].toString();
QString path = map["path"].toString();
bool secure = map["secure"].toBool();
bool session = map["session"].toBool();
QString name = map.value(QSL("name")).toString();
QString url = map.value(QSL("url")).toString();
QString path = map.value(QSL("path")).toString();
bool secure = map.value(QSL("secure")).toBool();
bool session = map.value(QSL("session")).toBool();
QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
for (QNetworkCookie cookie : cookies) {
if ((!map["name"].isValid() || cookie.name() == name)
&& (!map["url"].isValid() || cookie.domain() == url)
&& (!map["path"].isValid() || cookie.path() == path)
&& (!map["secure"].isValid() || cookie.isSecure() == secure)
&& (!map["session"].isValid() || cookie.isSessionCookie() == session)) {
if ((!map.contains(QSL("name")) || cookie.name() == name)
&& (!map.contains(QSL("url")) || cookie.domain() == url)
&& (!map.contains(QSL("path")) || cookie.path() == path)
&& (!map.contains(QSL("secure")) || cookie.isSecure() == secure)
&& (!map.contains(QSL("session")) || cookie.isSessionCookie() == session)) {
QNetworkCookie *netCookie = new QNetworkCookie(cookie);
QmlCookie *qmlCookie = new QmlCookie(netCookie);
qmlCookies.append(qmlCookie);
@ -88,21 +88,21 @@ QList<QObject*> QmlCookies::getAll(const QVariantMap &map)
void QmlCookies::set(const QVariantMap &map)
{
QString name = map["name"].toString();
QString url = map["url"].toString();
QString path = map["path"].toString();
bool secure = map["secure"].toBool();
QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map["expirationDate"].toDouble());
bool httpOnly = map["httpOnly"].toBool();
QString value = map["value"].toString();
QString name = map.value(QSL("name")).toString();
QString url = map.value(QSL("url")).toString();
QString path = map.value(QSL("path")).toString();
bool secure = map.value(QSL("secure")).toBool();
QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map.value(QSL("expirationDate")).toDouble());
bool httpOnly = map.value(QSL("httpOnly")).toBool();
QString value = map.value(QSL("value")).toString();
QNetworkCookie cookie;
cookie.setName(name.toLocal8Bit());
cookie.setName(name.toUtf8());
cookie.setDomain(url);
cookie.setPath(path);
cookie.setSecure(secure);
cookie.setExpirationDate(expirationDate);
cookie.setHttpOnly(httpOnly);
cookie.setValue(value.toLocal8Bit());
cookie.setValue(value.toUtf8());
mApp->webProfile()->cookieStore()->setCookie(cookie);
}

View File

@ -15,8 +15,7 @@
* 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 QMLCOOKIES_H
#define QMLCOOKIES_H
#pragma once
#include <QObject>
#include "qmlcookie.h"
@ -30,10 +29,8 @@ public:
Q_INVOKABLE QList<QObject*> getAll(const QVariantMap &map);
Q_INVOKABLE void set(const QVariantMap &map);
Q_INVOKABLE void remove(const QVariantMap &map);
signals:
Q_SIGNALS:
void changed(QmlCookie *cookie, bool removed);
private:
QNetworkCookie *getNetworkCookie(const QVariantMap &map);
};
#endif // QMLCOOKIES_H

View File

@ -19,15 +19,15 @@
#include "mainapplication.h"
#include "history.h"
QmlHistory::QmlHistory(QObject *parent) :
QObject(parent)
QmlHistory::QmlHistory(QObject *parent)
: QObject(parent)
{
connect(mApp->history(), &History::historyEntryAdded, this, [=](HistoryEntry entry){
connect(mApp->history(), &History::historyEntryAdded, this, [this](HistoryEntry entry){
QmlHistoryItem *historyItem = new QmlHistoryItem(&entry);
emit visited(historyItem);
});
connect(mApp->history(), &History::historyEntryDeleted, this, [=](HistoryEntry entry){
connect(mApp->history(), &History::historyEntryDeleted, this, [this](HistoryEntry entry){
QmlHistoryItem *historyItem = new QmlHistoryItem(&entry);
emit visitRemoved(historyItem);
});
@ -37,11 +37,11 @@ QList<QObject*> QmlHistory::search(const QVariantMap &map)
{
QList<QObject*> list;
if (!map["text"].isValid()) {
if (!map.contains(QSL("text"))) {
qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
return list;
}
QString text = map["text"].toString();
QString text = map.value(QSL("text")).toString();
QList<HistoryEntry*> result = mApp->history()->searchHistoryEntry(text);
foreach(auto entry, result) {
@ -53,47 +53,47 @@ QList<QObject*> QmlHistory::search(const QVariantMap &map)
int QmlHistory::getVisits(const QVariantMap &map)
{
if (!map["url"].isValid()) {
if (!map.contains(QSL("url"))) {
qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
return 0;
}
QString url = map["url"].toString();
QString url = map.value(QSL("url")).toString();
HistoryEntry *entry = mApp->history()->getHistoryEntry(url);
return entry->count;
}
void QmlHistory::addUrl(const QVariantMap &map)
{
if (!map["title"].isValid() || !map["url"].isValid()) {
if (!map.contains(QSL("title")) || !map.contains(QSL("url"))) {
qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
return;
}
QString title = map["title"].toString();
QString url = map["url"].toString();
QString title = map.value(QSL("title")).toString();
QString url = map.value(QSL("url")).toString();
title = title.isEmpty() ? url : title;
mApp->history()->addHistoryEntry(QUrl(url), title);
mApp->history()->addHistoryEntry(QUrl::fromEncoded(url.toUtf8()), title);
}
void QmlHistory::deleteUrl(const QVariantMap &map)
{
if (!map["url"].isValid()) {
if (!map.contains(QSL("url"))) {
qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
return;
}
QString url = map["url"].toString();
QString url = map.value(QSL("url")).toString();
mApp->history()->deleteHistoryEntry(url);
}
void QmlHistory::deleteRange(const QVariantMap &map)
{
if (!map["startTime"].isValid() || !map["endTime"].isValid()) {
if (!map.contains(QSL("startTime")) || !map.contains(QSL("endTime"))) {
qWarning() << "Error:" << "wrong arguments passed to" << __FUNCTION__;
return;
}
double startTime = map["startTime"].toDouble();
double endTime = map["endTime"].toDouble();
double startTime = map.value(QSL("startTime")).toDouble();
double endTime = map.value(QSL("endTime")).toDouble();
mApp->history()->deleteRange(startTime, endTime);
}

View File

@ -15,8 +15,7 @@
* 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 QMLHISTORY_H
#define QMLHISTORY_H
#pragma once
#include <QObject>
#include "qmlhistoryitem.h"
@ -32,9 +31,7 @@ public:
Q_INVOKABLE void deleteUrl(const QVariantMap &map);
Q_INVOKABLE void deleteRange(const QVariantMap &map);
Q_INVOKABLE void deleteAll();
signals:
Q_SIGNALS:
void visited(QmlHistoryItem *historyItem);
void visitRemoved(QmlHistoryItem *historyItem);
};
#endif // QMLHISTORY_H

View File

@ -17,9 +17,9 @@
* ============================================================ */
#include "qmlhistoryitem.h"
QmlHistoryItem::QmlHistoryItem(HistoryEntry *entry, QObject *parent) :
QObject(parent)
, m_entry(entry)
QmlHistoryItem::QmlHistoryItem(HistoryEntry *entry, QObject *parent)
: QObject(parent)
, m_entry(entry)
{
}
@ -36,7 +36,7 @@ QString QmlHistoryItem::url() const
if (!m_entry) {
return QString();
}
return m_entry->url.toString();
return QString::fromUtf8(m_entry->url.toEncoded());
}
QString QmlHistoryItem::title() const

View File

@ -15,8 +15,7 @@
* 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 QMLHISTORYITEM_H
#define QMLHISTORYITEM_H
#pragma once
#include <QObject>
#include "history.h"
@ -39,5 +38,3 @@ public:
private:
HistoryEntry *m_entry;
};
#endif // QMLHISTORYITEM_H

View File

@ -17,10 +17,10 @@
* ============================================================ */
#include "qmlmostvisitedurl.h"
QmlMostVisitedUrl::QmlMostVisitedUrl(QString title, QString url, QObject *parent) :
QObject(parent)
, m_title(title)
, m_url(url)
QmlMostVisitedUrl::QmlMostVisitedUrl(QString title, QString url, QObject *parent)
: QObject(parent)
, m_title(title)
, m_url(url)
{
}

View File

@ -15,8 +15,7 @@
* 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 QMLMOSTVISITEDURL_H
#define QMLMOSTVISITEDURL_H
#pragma once
#include <QObject>
@ -33,5 +32,3 @@ private:
QString m_title;
QString m_url;
};
#endif // QMLMOSTVISITEDURL_H

View File

@ -20,8 +20,8 @@
#include "mainapplication.h"
#include "pluginproxy.h"
QmlTopSites::QmlTopSites(QObject *parent) :
QObject(parent)
QmlTopSites::QmlTopSites(QObject *parent)
: QObject(parent)
{
}

View File

@ -15,8 +15,7 @@
* 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 QMLTOPSITES_H
#define QMLTOPSITES_H
#pragma once
#include <QObject>
#include "qmlmostvisitedurl.h"
@ -28,5 +27,3 @@ public:
explicit QmlTopSites(QObject *parent = 0);
Q_INVOKABLE QList<QObject*> get() const;
};
#endif // QMLTOPSITES_H

View File

@ -24,14 +24,15 @@ QmlPluginInterface::QmlPluginInterface()
void QmlPluginInterface::init(InitState state, const QString &settingsPath)
{
if (m_jsInit.isCallable()) {
QJSValueList args;
args.append(state);
args.append(settingsPath);
m_jsInit.call(args);
} else {
qWarning() << "Unable to call init on" << m_name << "plugin";
if (!m_jsInit.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return;
}
QJSValueList args;
args.append(state);
args.append(settingsPath);
m_jsInit.call(args);
}
DesktopFile QmlPluginInterface::metaData() const
@ -41,35 +42,26 @@ DesktopFile QmlPluginInterface::metaData() const
void QmlPluginInterface::unload()
{
if (m_jsUnload.isCallable()) {
m_jsUnload.call();
} else {
qWarning() << "Unable to call unload on" << m_name << "plugin";
if (!m_jsUnload.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return;
}
m_jsUnload.call();
}
bool QmlPluginInterface::testPlugin()
{
if (m_jsTestPlugin.isCallable()) {
QJSValue ret = m_jsTestPlugin.call();
return ret.toBool();
} else {
qWarning() << "Unable to call testPlugin on" << m_name << "plugin";
if (!m_jsTestPlugin.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return false;
}
QJSValue ret = m_jsTestPlugin.call();
return ret.toBool();
}
QString QmlPluginInterface::name()
{
return m_name;
}
void QmlPluginInterface::setName(const QString &name)
{
m_name = name;
}
QJSValue QmlPluginInterface::jsInit()
QJSValue QmlPluginInterface::jsInit() const
{
return m_jsInit;
}
@ -79,7 +71,7 @@ void QmlPluginInterface::setJsInit(const QJSValue &init)
m_jsInit = init;
}
QJSValue QmlPluginInterface::jsUnload()
QJSValue QmlPluginInterface::jsUnload() const
{
return m_jsUnload;
}
@ -89,7 +81,7 @@ void QmlPluginInterface::setJsUnload(const QJSValue &unload)
m_jsUnload = unload;
}
QJSValue QmlPluginInterface::jsTestPlugin()
QJSValue QmlPluginInterface::jsTestPlugin() const
{
return m_jsTestPlugin;
}
@ -98,3 +90,8 @@ void QmlPluginInterface::setJsTestPlugin(const QJSValue &testPlugin)
{
m_jsTestPlugin = testPlugin;
}
void QmlPluginInterface::setName(const QString &name)
{
m_name = name;
}

View File

@ -15,8 +15,7 @@
* 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 QMLPLUGININTERFACE_H
#define QMLPLUGININTERFACE_H
#pragma once
#include <QJSValue>
#include <QObject>
@ -29,7 +28,6 @@ class QmlPluginInterface : public QObject, public PluginInterface
Q_OBJECT
Q_INTERFACES(PluginInterface)
Q_ENUMS(InitState)
Q_PROPERTY(QString __name__ READ name CONSTANT)
Q_PROPERTY(QJSValue init READ jsInit WRITE setJsInit)
Q_PROPERTY(QJSValue unload READ jsUnload WRITE setJsUnload)
Q_PROPERTY(QJSValue testPlugin READ jsTestPlugin WRITE setJsTestPlugin)
@ -40,7 +38,6 @@ public:
void init(InitState state, const QString &settingsPath);
void unload();
bool testPlugin();
void setName(const QString &name);
private:
@ -49,13 +46,10 @@ private:
QJSValue m_jsUnload;
QJSValue m_jsTestPlugin;
QString name();
QJSValue jsInit();
QJSValue jsInit() const;
void setJsInit(const QJSValue &init);
QJSValue jsUnload();
QJSValue jsUnload() const;
void setJsUnload(const QJSValue &unload);
QJSValue jsTestPlugin();
QJSValue jsTestPlugin() const;
void setJsTestPlugin(const QJSValue &testPlugin);
};
#endif // QMLPLUGININTERFACE_H

View File

@ -21,7 +21,6 @@ QmlPluginLoader::QmlPluginLoader(const QString &path)
{
m_engine = new QQmlEngine();
m_component = new QQmlComponent(m_engine, path);
createComponent();
}
void QmlPluginLoader::createComponent()
@ -29,15 +28,6 @@ void QmlPluginLoader::createComponent()
m_interface = qobject_cast<QmlPluginInterface*>(m_component->create());
}
void QmlPluginLoader::setName(const QString &name)
{
if (!m_interface) {
return;
}
m_interface->setName(name);
}
QQmlComponent *QmlPluginLoader::component() const
{
return m_component;
@ -47,3 +37,8 @@ QmlPluginInterface *QmlPluginLoader::instance() const
{
return m_interface;
}
void QmlPluginLoader::setName(const QString &name)
{
m_interface->setName(name);
}

View File

@ -15,8 +15,7 @@
* 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 QMLPLUGINLOADER_H
#define QMLPLUGINLOADER_H
#pragma once
#include <QQmlEngine>
#include <QQmlComponent>
@ -25,15 +24,14 @@
class QmlPluginLoader
{
public:
explicit QmlPluginLoader(const QString &path);
void createComponent();
QQmlComponent *component() const;
QmlPluginInterface *instance() const;
void setName(const QString &name);
private:
QQmlEngine *m_engine;
QQmlComponent *m_component;
QmlPluginInterface *m_interface;
public:
QmlPluginLoader(const QString &path);
void createComponent();
void setName(const QString &name);
QQmlComponent *component() const;
QmlPluginInterface *instance() const;
};
#endif // QMLPLUGINLOADER_H

View File

@ -31,42 +31,12 @@
// static
void QmlPlugins::registerQmlTypes()
{
registerQmlPluginInterface();
registerQmlBookmarkTreeNode();
registerQmlBookmarks();
registerQmlMostVisitedUrl();
registerQmlTopSites();
registerQmlHistoryItem();
registerQmlHistory();
registerQmlCookie();
registerQmlCookies();
}
// private static
void QmlPlugins::registerQmlPluginInterface()
{
// PluginInterface
qmlRegisterType<QmlPluginInterface>("org.kde.falkon", 1, 0, "PluginInterface");
}
// private static
void QmlPlugins::registerQmlBookmarkTreeNode()
{
qmlRegisterSingletonType<QmlBookmarkTreeNode>("org.kde.falkon", 1, 0, "BookmarkTreeNode", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
// Bookmarks
qmlRegisterUncreatableType<QmlBookmarkTreeNode>("org.kde.falkon", 1, 0, "BookmarkTreeNode", "Unable to create Type: BookmarkTreeNode");
auto *object = new QmlBookmarkTreeNode();
return object;
});
}
// private static
void QmlPlugins::registerQmlBookmarks()
{
qmlRegisterSingletonType<QmlBookmarks>("org.kde.falkon", 1, 0, "Bookmarks", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
@ -74,23 +44,10 @@ void QmlPlugins::registerQmlBookmarks()
auto *object = new QmlBookmarks();
return object;
});
}
// private static
void QmlPlugins::registerQmlMostVisitedUrl()
{
qmlRegisterSingletonType<QmlMostVisitedUrl>("org.kde.falkon", 1, 0, "MostVisitedURL", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
// TopSites
qmlRegisterUncreatableType<QmlMostVisitedUrl>("org.kde.falkon", 1, 0, "MostVisitedURL", "Unable to create Type: MostVisitedURL");
auto *object = new QmlMostVisitedUrl();
return object;
});
}
// private static
void QmlPlugins::registerQmlTopSites()
{
qmlRegisterSingletonType<QmlTopSites>("org.kde.falkon", 1, 0, "TopSites", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
@ -98,23 +55,10 @@ void QmlPlugins::registerQmlTopSites()
auto *object = new QmlTopSites();
return object;
});
}
// private static
void QmlPlugins::registerQmlHistoryItem()
{
qmlRegisterSingletonType<QmlHistoryItem>("org.kde.falkon", 1, 0, "HistoryItem", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
// History
qmlRegisterUncreatableType<QmlHistoryItem>("org.kde.falkon", 1, 0, "HistoryItem", "Unable to create Type: HistoryItem");
auto *object = new QmlHistoryItem();
return object;
});
}
// private static
void QmlPlugins::registerQmlHistory()
{
qmlRegisterSingletonType<QmlHistory>("org.kde.falkon", 1, 0, "History", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
@ -122,23 +66,10 @@ void QmlPlugins::registerQmlHistory()
auto *object = new QmlHistory();
return object;
});
}
// private static
void QmlPlugins::registerQmlCookie()
{
qmlRegisterSingletonType<QmlCookie>("org.kde.falkon", 1, 0, "Cookie", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
// Cookies
qmlRegisterUncreatableType<QmlCookie>("org.kde.falkon", 1, 0, "Cookie", "Unable to create Type: Cookie");
auto *object = new QmlCookie();
return object;
});
}
// private static
void QmlPlugins::registerQmlCookies()
{
qmlRegisterSingletonType<QmlCookies>("org.kde.falkon", 1, 0, "Cookies", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)

View File

@ -15,26 +15,10 @@
* 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 QMLPLUGINS_H
#define QMLPLUGINS_H
#pragma once
class QmlPlugins
{
static void registerQmlPluginInterface();
static void registerQmlBookmarkTreeNode();
static void registerQmlBookmarks();
static void registerQmlMostVisitedUrl();
static void registerQmlTopSites();
static void registerQmlHistoryItem();
static void registerQmlHistory();
static void registerQmlCookie();
static void registerQmlCookies();
public:
static void registerQmlTypes();
};
#endif // QMLPLUGINS_H

View File

@ -2,7 +2,7 @@ import org.kde.falkon 1.0 as Falkon
Falkon.PluginInterface {
init: function(state, settingsPath){
console.log(__name__ + " Plugin loaded")
console.log("'Hello Qml' plugin loaded")
}
testPlugin: function() {