File indexing completed on 2024-05-19 16:41:36
0001 /* 0002 SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be> 0003 SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "chrome.h" 0009 #include "browsers/findprofile.h" 0010 #include "faviconfromblob.h" 0011 0012 #include <QDebug> 0013 #include <QFileInfo> 0014 #include <QJsonArray> 0015 #include <QJsonObject> 0016 #include <QJsonValue> 0017 0018 class ProfileBookmarks 0019 { 0020 public: 0021 ProfileBookmarks(const Profile &profile) 0022 : m_profile(profile) 0023 { 0024 } 0025 inline QJsonArray bookmarks() 0026 { 0027 return m_bookmarks; 0028 } 0029 inline Profile profile() 0030 { 0031 return m_profile; 0032 } 0033 void tearDown() 0034 { 0035 m_profile.favicon()->teardown(); 0036 clear(); 0037 } 0038 void add(const QJsonObject &bookmarkEntry) 0039 { 0040 m_bookmarks << bookmarkEntry; 0041 } 0042 void add(const QJsonArray &entries) 0043 { 0044 for (const auto &e : entries) 0045 m_bookmarks << e; 0046 } 0047 void clear() 0048 { 0049 m_bookmarks = QJsonArray(); 0050 } 0051 0052 private: 0053 Profile m_profile; 0054 QJsonArray m_bookmarks; 0055 }; 0056 0057 Chrome::Chrome(FindProfile *findProfile, QObject *parent) 0058 : QObject(parent) 0059 , m_watcher(new KDirWatch(this)) 0060 , m_dirty(false) 0061 { 0062 const auto profiles = findProfile->find(); 0063 for (const Profile &profile : profiles) { 0064 updateCacheFile(profile.faviconSource(), profile.faviconCache()); 0065 m_profileBookmarks << new ProfileBookmarks(profile); 0066 m_watcher->addFile(profile.path()); 0067 } 0068 connect(m_watcher, &KDirWatch::created, this, [this] { 0069 m_dirty = true; 0070 }); 0071 } 0072 0073 Chrome::~Chrome() 0074 { 0075 for (ProfileBookmarks *profileBookmark : qAsConst(m_profileBookmarks)) { 0076 delete profileBookmark; 0077 } 0078 } 0079 0080 QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing) 0081 { 0082 if (m_dirty) { 0083 prepare(); 0084 } 0085 QList<BookmarkMatch> results; 0086 for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { 0087 results << match(term, addEveryThing, profileBookmarks); 0088 } 0089 return results; 0090 } 0091 0092 QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) 0093 { 0094 QList<BookmarkMatch> results; 0095 0096 const auto bookmarks = profileBookmarks->bookmarks(); 0097 Favicon *favicon = profileBookmarks->profile().favicon(); 0098 for (const QJsonValue &bookmarkValue : bookmarks) { 0099 const QJsonObject bookmark = bookmarkValue.toObject(); 0100 const QString url = bookmark.value(QStringLiteral("url")).toString(); 0101 BookmarkMatch bookmarkMatch(favicon->iconFor(url), term, bookmark.value(QStringLiteral("name")).toString(), url); 0102 bookmarkMatch.addTo(results, addEveryThing); 0103 } 0104 return results; 0105 } 0106 0107 void Chrome::prepare() 0108 { 0109 m_dirty = false; 0110 for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { 0111 Profile profile = profileBookmarks->profile(); 0112 profileBookmarks->clear(); 0113 const QJsonArray bookmarks = readChromeFormatBookmarks(profile.path()); 0114 if (bookmarks.isEmpty()) { 0115 continue; 0116 } 0117 profileBookmarks->add(bookmarks); 0118 updateCacheFile(profile.faviconSource(), profile.faviconCache()); 0119 profile.favicon()->prepare(); 0120 } 0121 } 0122 0123 void Chrome::teardown() 0124 { 0125 for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { 0126 profileBookmarks->tearDown(); 0127 } 0128 }