Commit bbed524a authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Add bookmarksrunner integration for Falkon

CCBUG: 393544
parent 19c41453
......@@ -37,6 +37,7 @@ set(krunner_bookmarksrunner_SRCS
browsers/firefox.cpp
browsers/kdebrowser.cpp
browsers/opera.cpp
browsers/falkon.cpp
)
......
......@@ -19,7 +19,7 @@
*/
#include "bookmarksrunner.h"
#include "browser.h"
#include "browsers/browser.h"
#include <QList>
#include <QStack>
......
......@@ -19,12 +19,13 @@
*/
#include "browserfactory.h"
#include "browser.h"
#include "browsers/browser.h"
#include "browsers/kdebrowser.h"
#include "browsers/firefox.h"
#include "browsers/opera.h"
#include "browsers/chromefindprofile.h"
#include "browsers/chrome.h"
#include "browsers/falkon.h"
Browser *BrowserFactory::find(const QString &browserName, QObject *parent)
{
......@@ -41,6 +42,8 @@ Browser *BrowserFactory::find(const QString &browserName, QObject *parent)
m_previousBrowser = new Chrome(new FindChromeProfile(QStringLiteral("google-chrome"), QDir::homePath(), parent), parent);
} else if (browserName.contains(QLatin1String("chromium"), Qt::CaseInsensitive)) {
m_previousBrowser = new Chrome(new FindChromeProfile(QStringLiteral("chromium"), QDir::homePath(), parent), parent);
} else if (browserName.contains(QLatin1String("falkon"), Qt::CaseInsensitive)) {
m_previousBrowser = new Falkon(parent);
} else {
m_previousBrowser = new KDEBrowser(parent);
}
......
......@@ -26,6 +26,9 @@
#include <QFileInfo>
#include <QDateTime>
#include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "bookmarkmatch.h"
class Browser
......@@ -64,6 +67,40 @@ protected:
}
return Unchanged;
}
QJsonArray readChromeFormatBookmarks(const QString &path) {
QJsonArray bookmarks;
QFile bookmarksFile(path);
if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
return bookmarks;
}
const QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll());
if (jdoc.isNull()) {
return bookmarks;
}
const QJsonObject resultMap = jdoc.object();
if (!resultMap.contains(QLatin1String("roots"))) {
return bookmarks;
}
const QJsonObject entries = resultMap.value(QLatin1String("roots")).toObject();
for (const QJsonValue &folder : entries) {
parseFolder(folder.toObject(), bookmarks);
}
return bookmarks;
}
private:
void parseFolder(const QJsonObject &obj, QJsonArray &bookmarks) {
const QJsonArray children = obj.value(QStringLiteral("children")).toArray();
for (const QJsonValue &child : children) {
const QJsonObject entry = child.toObject();
if (entry.value(QLatin1String("type")).toString() == QLatin1String("folder"))
parseFolder(entry, bookmarks);
else {
bookmarks.append(entry);
}
}
}
};
......
......@@ -24,13 +24,10 @@
#include "browsers/findprofile.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QFileInfo>
#include <QDebug>
#include "bookmarksrunner_defs.h"
#include <QDir>
class ProfileBookmarks {
public:
......@@ -39,6 +36,7 @@ public:
inline Profile profile() { return m_profile; }
void tearDown() { m_profile.favicon()->teardown(); clear(); }
void add(const QJsonObject &bookmarkEntry) { m_bookmarks << bookmarkEntry; }
void add(const QJsonArray &entries) { for (const auto &e: entries) m_bookmarks << e; }
void clear() { m_bookmarks = QJsonArray(); }
private:
Profile m_profile;
......@@ -99,22 +97,11 @@ void Chrome::prepare()
for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) {
Profile profile = profileBookmarks->profile();
profileBookmarks->clear();
QFile bookmarksFile(profile.path());
if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QJsonArray bookmarks = readChromeFormatBookmarks(profile.path());
if (bookmarks.isEmpty()) {
continue;
}
QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll());
if (jdoc.isNull()) {
continue;
}
const QJsonObject resultMap = jdoc.object();
if (!resultMap.contains(QLatin1String("roots"))) {
return;
}
const QJsonObject entries = resultMap.value(QStringLiteral("roots")).toObject();
for (const QJsonValue &folder : entries) {
parseFolder(folder.toObject(), profileBookmarks);
}
profileBookmarks->add(bookmarks);
updateCacheFile(profile.faviconSource(), profile.faviconCache());
profile.favicon()->prepare();
}
......@@ -126,16 +113,3 @@ void Chrome::teardown()
profileBookmarks->tearDown();
}
}
void Chrome::parseFolder(const QJsonObject &entry, ProfileBookmarks *profile)
{
const QJsonArray children = entry.value(QStringLiteral("children")).toArray();
for (const QJsonValue &child : children) {
const QJsonObject entry = child.toObject();
if(entry.value(QStringLiteral("type")).toString() == QLatin1String("folder"))
parseFolder(entry, profile);
else {
profile->add(entry);
}
}
}
/*
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "falkon.h"
#include <QDir>
#include <QStandardPaths>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <KSharedConfig>
#include <KConfigGroup>
#include "faviconfromblob.h"
#include "favicon.h"
Falkon::Falkon(QObject* parent)
: QObject(parent)
, m_startupProfile(getStartupProfileDir())
, m_favicon(FaviconFromBlob::falkon(m_startupProfile, this))
{
}
QList<BookmarkMatch> Falkon::match(const QString& term, bool addEverything)
{
QList<BookmarkMatch> matches;
for(const auto &bookmark : qAsConst(m_falkonBookmarkEntries)) {
const auto obj = bookmark.toObject();
const QString url = obj.value(QStringLiteral("url")).toString();
BookmarkMatch bookmarkMatch(m_favicon->iconFor(url), term, obj.value(QStringLiteral("name")).toString(), url);
bookmarkMatch.addTo(matches, addEverything);
}
return matches;
}
void Falkon::prepare()
{
m_falkonBookmarkEntries = readChromeFormatBookmarks(m_startupProfile + QStringLiteral("/bookmarks.json"));
}
void Falkon::teardown()
{
m_falkonBookmarkEntries = QJsonArray();
}
QString Falkon::getStartupProfileDir()
{
const QString profilesIni = QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("/falkon/profiles/profiles.ini"));
const QString startupProfile =KSharedConfig::openConfig(profilesIni)->
group("Profiles").readEntry("startProfile", QStringLiteral("default"));
return QFileInfo(profilesIni).dir().absoluteFilePath(startupProfile);
}
/*
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef FALKON_H
#define FALKON_H
#include "browser.h"
class Favicon;
class Falkon : public QObject, public Browser
{
Q_OBJECT
public:
explicit Falkon(QObject* parent = nullptr);
QList<BookmarkMatch> match(const QString& term, bool addEverything) override;
public Q_SLOTS:
void prepare() override;
void teardown() override;
private:
QString getStartupProfileDir();
QJsonArray m_falkonBookmarkEntries;
QString m_startupProfile;
Favicon * m_favicon;
};
#endif // FALKON_H
......@@ -67,6 +67,13 @@ FaviconFromBlob *FaviconFromBlob::firefox(FetchSqlite *fetchSqlite, QObject *par
return new FaviconFromBlob(QStringLiteral("firefox-default"), faviconQuery, QStringLiteral("data"), fetchSqlite, parent);
}
FaviconFromBlob *FaviconFromBlob::falkon(const QString &profileDirectory, QObject *parent)
{
const QString dbPath = profileDirectory + QStringLiteral("/browsedata.db");
FetchSqlite *fetchSqlite = new FetchSqlite(dbPath, parent);
const QString faviconQuery = QStringLiteral("SELECT icon FROM icons WHERE url = :url LIMIT 1;");
return new FaviconFromBlob(faviconQuery, faviconQuery, QStringLiteral("icon"), fetchSqlite, parent);
}
FaviconFromBlob::FaviconFromBlob(const QString &profileName, const QString &query, const QString &blobColumn, FetchSqlite *fetchSqlite, QObject *parent)
: Favicon(parent), m_query(query), m_blobcolumn(blobColumn), m_fetchsqlite(fetchSqlite)
......
......@@ -31,6 +31,7 @@ class FaviconFromBlob : public Favicon
public:
static FaviconFromBlob *chrome(const QString &profileDirectory, QObject *parent = nullptr);
static FaviconFromBlob *firefox(FetchSqlite *fetchSqlite, QObject *parent = nullptr);
static FaviconFromBlob *falkon(const QString &profileDirectory, QObject *parent = nullptr);
~FaviconFromBlob() override;
QIcon iconFor(const QString &url) override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment