2014-11-13 19:30:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
|
|
|
|
* Copyright 2012 Glenn Ergeerts <marco.gulino@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-02-03 03:07:33 +00:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2021-02-10 17:27:08 +02:00
|
|
|
#include <QJsonDocument>
|
2020-02-03 03:07:33 +00:00
|
|
|
#include <KDebug>
|
|
|
|
|
2014-11-13 19:30:51 +02:00
|
|
|
#include "chrome.h"
|
|
|
|
#include "browsers/findprofile.h"
|
|
|
|
#include "bookmarksrunner_defs.h"
|
2021-10-03 04:56:25 +03:00
|
|
|
#include "favicon.h"
|
2020-02-03 03:07:33 +00:00
|
|
|
|
2014-11-13 19:30:51 +02:00
|
|
|
class ProfileBookmarks {
|
|
|
|
public:
|
2022-12-27 19:20:37 +02:00
|
|
|
ProfileBookmarks(const Profile &profile) : m_profile(profile) {}
|
2014-11-13 19:30:51 +02:00
|
|
|
inline QList<QVariantMap> bookmarks() { return m_bookmarks; }
|
|
|
|
inline Profile profile() { return m_profile; }
|
|
|
|
void tearDown() { m_profile.favicon()->teardown(); m_bookmarks.clear(); }
|
2023-08-04 03:37:25 +03:00
|
|
|
void add(const QVariantMap &bookmarkEntry) { m_bookmarks << bookmarkEntry; }
|
2014-11-13 19:30:51 +02:00
|
|
|
private:
|
|
|
|
Profile m_profile;
|
|
|
|
QList<QVariantMap> m_bookmarks;
|
|
|
|
};
|
|
|
|
|
|
|
|
Chrome::Chrome( FindProfile* findProfile, QObject* parent )
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2022-12-27 19:20:37 +02:00
|
|
|
foreach (const Profile &profile, findProfile->find()) {
|
2014-11-13 19:30:51 +02:00
|
|
|
m_profileBookmarks << new ProfileBookmarks(profile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Chrome::~Chrome()
|
|
|
|
{
|
2022-12-27 19:20:37 +02:00
|
|
|
foreach (ProfileBookmarks *profileBookmark, m_profileBookmarks) {
|
2014-11-13 19:30:51 +02:00
|
|
|
delete profileBookmark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing)
|
|
|
|
{
|
|
|
|
QList<BookmarkMatch> results;
|
2022-12-27 19:20:37 +02:00
|
|
|
foreach (ProfileBookmarks *profileBookmarks, m_profileBookmarks) {
|
2014-11-13 19:30:51 +02:00
|
|
|
results << match(term, addEveryThing, profileBookmarks);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks)
|
|
|
|
{
|
|
|
|
QList<BookmarkMatch> results;
|
2023-08-04 03:37:25 +03:00
|
|
|
foreach(const QVariantMap &bookmark, profileBookmarks->bookmarks()) {
|
|
|
|
const QString url = bookmark.value("url").toString();
|
|
|
|
const QString name = bookmark.value("name").toString();
|
|
|
|
kDebug(kdbg_code) << "Match" << name << url;
|
|
|
|
BookmarkMatch bookmarkMatch(profileBookmarks->profile().favicon(), term, name, url);
|
2014-11-13 19:30:51 +02:00
|
|
|
bookmarkMatch.addTo(results, addEveryThing);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chrome::prepare()
|
|
|
|
{
|
2022-12-27 19:20:37 +02:00
|
|
|
foreach (ProfileBookmarks *profileBookmarks, m_profileBookmarks) {
|
2014-11-13 19:30:51 +02:00
|
|
|
Profile profile = profileBookmarks->profile();
|
|
|
|
QFile bookmarksFile(profile.path());
|
2020-02-03 03:45:28 +00:00
|
|
|
if (!bookmarksFile.open(QFile::ReadOnly)) {
|
2023-08-04 03:37:25 +03:00
|
|
|
continue;
|
2020-02-03 03:45:28 +00:00
|
|
|
}
|
2020-02-03 03:07:33 +00:00
|
|
|
|
2021-02-10 17:27:08 +02:00
|
|
|
QJsonDocument jsondoc = QJsonDocument::fromJson(bookmarksFile.readAll());
|
2023-08-04 03:37:25 +03:00
|
|
|
if (jsondoc.isNull()) {
|
|
|
|
kDebug(kdbg_code) << "Null profile document" << jsondoc.errorString();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const QVariantMap root = jsondoc.toVariant().toMap();
|
|
|
|
if (!root.contains("roots")) {
|
|
|
|
kDebug(kdbg_code) << "No roots in" << profile.path();
|
|
|
|
continue;
|
2020-02-03 03:07:33 +00:00
|
|
|
}
|
2023-08-04 03:37:25 +03:00
|
|
|
kDebug(kdbg_code) << "Filling entries from" << profile.path();
|
|
|
|
const QVariantMap roots = root.value("roots").toMap();
|
|
|
|
foreach (const QVariant &folder, roots.values()) {
|
2014-11-13 19:30:51 +02:00
|
|
|
parseFolder(folder.toMap(), profileBookmarks);
|
|
|
|
}
|
|
|
|
profile.favicon()->prepare();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chrome::teardown()
|
|
|
|
{
|
|
|
|
foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) {
|
|
|
|
profileBookmarks->tearDown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chrome::parseFolder(const QVariantMap &entry, ProfileBookmarks *profile)
|
|
|
|
{
|
|
|
|
QVariantList children = entry.value("children").toList();
|
2022-12-27 19:20:37 +02:00
|
|
|
foreach (const QVariant &child, children) {
|
2014-11-13 19:30:51 +02:00
|
|
|
QVariantMap entry = child.toMap();
|
2021-10-02 19:03:03 +03:00
|
|
|
if(entry.value("type").toString() == "folder") {
|
2014-11-13 19:30:51 +02:00
|
|
|
parseFolder(entry, profile);
|
2021-10-02 19:03:03 +03:00
|
|
|
} else {
|
2023-08-04 03:37:25 +03:00
|
|
|
// kDebug(kdbg_code) << "Adding entry" << entry;
|
2014-11-13 19:30:51 +02:00
|
|
|
profile->add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|