Changeset View
Changeset View
Standalone View
Standalone View
runners/bookmarks/browsers/chrome.cpp
Show All 15 Lines | |||||
16 | * License along with this program; if not, write to the | 16 | * License along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | 21 | | |||
22 | #include "chrome.h" | 22 | #include "chrome.h" | ||
23 | #include "faviconfromblob.h" | 23 | #include "faviconfromblob.h" | ||
24 | #include "browsers/findprofile.h" | | |||
25 | 24 | | |||
26 | #include <QJsonArray> | 25 | #include <QJsonArray> | ||
27 | #include <QJsonDocument> | 26 | #include <QJsonDocument> | ||
28 | #include <QJsonObject> | 27 | #include <QJsonObject> | ||
29 | #include <QJsonValue> | 28 | #include <QJsonValue> | ||
30 | #include <QFileInfo> | 29 | #include <QFileInfo> | ||
31 | #include <QDebug> | 30 | #include <QDebug> | ||
32 | #include "bookmarksrunner_defs.h" | 31 | #include "chromeprofile.h" | ||
33 | #include <QDir> | | |||
34 | 32 | | |||
35 | class ProfileBookmarks { | | |||
36 | public: | | |||
37 | ProfileBookmarks(const Profile &profile) : m_profile(profile) {} | | |||
38 | inline QJsonArray bookmarks() { return m_bookmarks; } | | |||
39 | inline Profile profile() { return m_profile; } | | |||
40 | void tearDown() { m_profile.favicon()->teardown(); clear(); } | | |||
41 | void add(const QJsonObject &bookmarkEntry) { m_bookmarks << bookmarkEntry; } | | |||
42 | void clear() { m_bookmarks = QJsonArray(); } | | |||
43 | private: | | |||
44 | Profile m_profile; | | |||
45 | QJsonArray m_bookmarks; | | |||
46 | }; | | |||
47 | 33 | | |||
48 | Chrome::Chrome( FindProfile* findProfile, QObject* parent ) | 34 | Chrome::Chrome(const QString &appName, QObject *parent) | ||
49 | : QObject(parent), | 35 | : QObject(parent), | ||
50 | m_watcher(new KDirWatch(this)), | 36 | m_watcher(new KDirWatch(this)), | ||
51 | m_dirty(false) | 37 | m_dirty(false) | ||
52 | { | 38 | { | ||
53 | const auto profiles = findProfile->find(); | 39 | m_profileBookmarks = ProfileBookmarks::findAllProfiles(appName); | ||
54 | for(const Profile &profile : profiles) { | 40 | for (ProfileBookmarks *profile : qAsConst(m_profileBookmarks)) { | ||
55 | updateCacheFile(profile.faviconSource(), profile.faviconCache()); | 41 | // Update cache file, then create favicon | ||
56 | m_profileBookmarks << new ProfileBookmarks(profile); | 42 | if (updateCacheFile(profile->faviconSource(), profile->faviconCache()) != Error) { | ||
57 | m_watcher->addFile(profile.path()); | 43 | profile->setFavicon(FaviconFromBlob::chrome(profile->name(), profile->faviconCache(), parent)); | ||
44 | } else { | ||||
45 | profile->setFavicon(new FallbackFavicon(parent)); | ||||
46 | } | ||||
47 | m_watcher->addFile(profile->path()); | ||||
58 | } | 48 | } | ||
59 | connect(m_watcher, &KDirWatch::created, [=] { m_dirty = true; }); | 49 | connect(m_watcher, &KDirWatch::created, [=] { m_dirty = true; }); | ||
60 | } | 50 | } | ||
61 | 51 | | |||
62 | Chrome::~Chrome() | 52 | Chrome::~Chrome() | ||
63 | { | 53 | { | ||
64 | for(ProfileBookmarks *profileBookmark : qAsConst(m_profileBookmarks)) { | 54 | qDeleteAll(m_profileBookmarks); | ||
65 | delete profileBookmark; | | |||
66 | } | | |||
67 | } | 55 | } | ||
68 | 56 | | |||
69 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing) | 57 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing) | ||
70 | { | 58 | { | ||
71 | if (m_dirty) { | 59 | if (m_dirty) { | ||
72 | prepare(); | 60 | prepare(); | ||
73 | } | 61 | } | ||
74 | QList<BookmarkMatch> results; | 62 | QList<BookmarkMatch> results; | ||
75 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | 63 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
76 | results << match(term, addEveryThing, profileBookmarks); | 64 | results << match(term, addEveryThing, profileBookmarks); | ||
77 | } | 65 | } | ||
78 | return results; | 66 | return results; | ||
79 | } | 67 | } | ||
80 | 68 | | |||
81 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) | 69 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) | ||
82 | { | 70 | { | ||
83 | QList<BookmarkMatch> results; | 71 | QList<BookmarkMatch> results; | ||
84 | 72 | | |||
85 | const auto bookmarks = profileBookmarks->bookmarks(); | 73 | const auto bookmarks = profileBookmarks->bookmarks(); | ||
86 | Favicon *favicon = profileBookmarks->profile().favicon(); | | |||
87 | for (const QJsonValue &bookmarkValue : bookmarks) { | 74 | for (const QJsonValue &bookmarkValue : bookmarks) { | ||
88 | const QJsonObject bookmark = bookmarkValue.toObject(); | 75 | const QJsonObject bookmark = bookmarkValue.toObject(); | ||
89 | const QString url = bookmark.value(QStringLiteral("url")).toString(); | 76 | const QString url = bookmark.value(QStringLiteral("url")).toString(); | ||
90 | BookmarkMatch bookmarkMatch(favicon->iconFor(url), term, bookmark.value(QStringLiteral("name")).toString(), url); | 77 | BookmarkMatch bookmarkMatch(profileBookmarks->iconFor(url), term, bookmark.value(QStringLiteral("name")).toString(), url); | ||
91 | bookmarkMatch.addTo(results, addEveryThing); | 78 | bookmarkMatch.addTo(results, addEveryThing); | ||
92 | } | 79 | } | ||
93 | return results; | 80 | return results; | ||
94 | } | 81 | } | ||
95 | 82 | | |||
96 | void Chrome::prepare() | 83 | void Chrome::prepare() | ||
97 | { | 84 | { | ||
98 | m_dirty = false; | 85 | m_dirty = false; | ||
99 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | 86 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
100 | Profile profile = profileBookmarks->profile(); | | |||
101 | profileBookmarks->clear(); | 87 | profileBookmarks->clear(); | ||
102 | QFile bookmarksFile(profile.path()); | 88 | QFile bookmarksFile(profileBookmarks->path()); | ||
103 | if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | 89 | if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
104 | continue; | 90 | continue; | ||
105 | } | 91 | } | ||
106 | QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll()); | 92 | QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll()); | ||
107 | if (jdoc.isNull()) { | 93 | if (jdoc.isNull()) { | ||
108 | continue; | 94 | continue; | ||
109 | } | 95 | } | ||
110 | const QJsonObject resultMap = jdoc.object(); | 96 | const QJsonObject resultMap = jdoc.object(); | ||
111 | if (!resultMap.contains(QLatin1String("roots"))) { | 97 | if (!resultMap.contains(QLatin1String("roots"))) { | ||
112 | return; | 98 | return; | ||
113 | } | 99 | } | ||
114 | const QJsonObject entries = resultMap.value(QStringLiteral("roots")).toObject(); | 100 | const QJsonObject entries = resultMap.value(QStringLiteral("roots")).toObject(); | ||
115 | for (const QJsonValue &folder : entries) { | 101 | for (const QJsonValue &folder : entries) { | ||
116 | parseFolder(folder.toObject(), profileBookmarks); | 102 | parseFolder(folder.toObject(), profileBookmarks); | ||
117 | } | 103 | } | ||
118 | updateCacheFile(profile.faviconSource(), profile.faviconCache()); | 104 | updateCacheFile(profileBookmarks->faviconSource(), profileBookmarks->faviconCache()); | ||
119 | profile.favicon()->prepare(); | 105 | profileBookmarks->favicon()->prepare(); | ||
120 | } | 106 | } | ||
121 | } | 107 | } | ||
122 | 108 | | |||
123 | void Chrome::teardown() | 109 | void Chrome::teardown() | ||
124 | { | 110 | { | ||
125 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | 111 | for(ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
126 | profileBookmarks->tearDown(); | 112 | profileBookmarks->tearDown(); | ||
127 | } | 113 | } | ||
Show All 14 Lines |