Changeset View
Changeset View
Standalone View
Standalone View
runners/bookmarks/browsers/chrome.cpp
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Line(s) | 43 | private: | |||
---|---|---|---|---|---|
45 | QJsonArray m_bookmarks; | 45 | QJsonArray m_bookmarks; | ||
46 | }; | 46 | }; | ||
47 | 47 | | |||
48 | Chrome::Chrome( FindProfile* findProfile, QObject* parent ) | 48 | Chrome::Chrome( FindProfile* findProfile, QObject* parent ) | ||
49 | : QObject(parent), | 49 | : QObject(parent), | ||
50 | m_watcher(new KDirWatch(this)), | 50 | m_watcher(new KDirWatch(this)), | ||
51 | m_dirty(false) | 51 | m_dirty(false) | ||
52 | { | 52 | { | ||
53 | foreach(Profile profile, findProfile->find()) { | 53 | const auto profiles = findProfile->find(); | ||
54 | for (Profile profile : profiles) { | ||||
apol: const&? | |||||
Not trivially possible because of the 'new ProfileBookmarks(profile)' below nicolasfella: Not trivially possible because of the 'new ProfileBookmarks(profile)' below | |||||
54 | m_profileBookmarks << new ProfileBookmarks(profile); | 55 | m_profileBookmarks << new ProfileBookmarks(profile); | ||
55 | m_watcher->addFile(profile.path()); | 56 | m_watcher->addFile(profile.path()); | ||
56 | } | 57 | } | ||
57 | connect(m_watcher, &KDirWatch::created, [=] { m_dirty = true; }); | 58 | connect(m_watcher, &KDirWatch::created, [=] { m_dirty = true; }); | ||
58 | } | 59 | } | ||
59 | 60 | | |||
60 | Chrome::~Chrome() | 61 | Chrome::~Chrome() | ||
61 | { | 62 | { | ||
62 | foreach(ProfileBookmarks *profileBookmark, m_profileBookmarks) { | 63 | for (ProfileBookmarks *profileBookmark : qAsConst(m_profileBookmarks)) { | ||
63 | delete profileBookmark; | 64 | delete profileBookmark; | ||
64 | } | 65 | } | ||
65 | } | 66 | } | ||
66 | 67 | | |||
67 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing) | 68 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing) | ||
68 | { | 69 | { | ||
69 | if (m_dirty) { | 70 | if (m_dirty) { | ||
70 | prepare(); | 71 | prepare(); | ||
71 | } | 72 | } | ||
72 | QList<BookmarkMatch> results; | 73 | QList<BookmarkMatch> results; | ||
73 | foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) { | 74 | for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
74 | results << match(term, addEveryThing, profileBookmarks); | 75 | results << match(term, addEveryThing, profileBookmarks); | ||
75 | } | 76 | } | ||
76 | return results; | 77 | return results; | ||
77 | } | 78 | } | ||
78 | 79 | | |||
79 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) | 80 | QList<BookmarkMatch> Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) | ||
80 | { | 81 | { | ||
81 | QList<BookmarkMatch> results; | 82 | QList<BookmarkMatch> results; | ||
82 | 83 | | |||
83 | const auto bookmarks = profileBookmarks->bookmarks(); | 84 | const auto bookmarks = profileBookmarks->bookmarks(); | ||
84 | for (const QJsonValue &bookmarkValue : bookmarks) { | 85 | for (const QJsonValue &bookmarkValue : bookmarks) { | ||
85 | const QJsonObject bookmark = bookmarkValue.toObject(); | 86 | const QJsonObject bookmark = bookmarkValue.toObject(); | ||
86 | 87 | | |||
87 | QString url = bookmark.value(QStringLiteral("url")).toString(); | 88 | QString url = bookmark.value(QStringLiteral("url")).toString(); | ||
88 | 89 | | |||
89 | BookmarkMatch bookmarkMatch(profileBookmarks->profile().favicon(), term, bookmark.value(QStringLiteral("name")).toString(), url); | 90 | BookmarkMatch bookmarkMatch(profileBookmarks->profile().favicon(), term, bookmark.value(QStringLiteral("name")).toString(), url); | ||
90 | bookmarkMatch.addTo(results, addEveryThing); | 91 | bookmarkMatch.addTo(results, addEveryThing); | ||
91 | } | 92 | } | ||
92 | return results; | 93 | return results; | ||
93 | } | 94 | } | ||
94 | 95 | | |||
95 | void Chrome::prepare() | 96 | void Chrome::prepare() | ||
96 | { | 97 | { | ||
97 | m_dirty = false; | 98 | m_dirty = false; | ||
98 | foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) { | 99 | for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
99 | Profile profile = profileBookmarks->profile(); | 100 | Profile profile = profileBookmarks->profile(); | ||
100 | profileBookmarks->clear(); | 101 | profileBookmarks->clear(); | ||
101 | QFile bookmarksFile(profile.path()); | 102 | QFile bookmarksFile(profile.path()); | ||
102 | if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | 103 | if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
103 | continue; | 104 | continue; | ||
104 | } | 105 | } | ||
105 | QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll()); | 106 | QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll()); | ||
106 | if (jdoc.isNull()) { | 107 | if (jdoc.isNull()) { | ||
107 | continue; | 108 | continue; | ||
108 | } | 109 | } | ||
109 | const QJsonObject resultMap = jdoc.object(); | 110 | const QJsonObject resultMap = jdoc.object(); | ||
110 | if (!resultMap.contains(QStringLiteral("roots"))) { | 111 | if (!resultMap.contains(QStringLiteral("roots"))) { | ||
111 | return; | 112 | return; | ||
112 | } | 113 | } | ||
113 | const QJsonObject entries = resultMap.value(QStringLiteral("roots")).toObject(); | 114 | const QJsonObject entries = resultMap.value(QStringLiteral("roots")).toObject(); | ||
114 | for (const QJsonValue &folder : entries) { | 115 | for (const QJsonValue &folder : entries) { | ||
115 | parseFolder(folder.toObject(), profileBookmarks); | 116 | parseFolder(folder.toObject(), profileBookmarks); | ||
116 | } | 117 | } | ||
117 | profile.favicon()->prepare(); | 118 | profile.favicon()->prepare(); | ||
118 | } | 119 | } | ||
119 | } | 120 | } | ||
120 | 121 | | |||
121 | void Chrome::teardown() | 122 | void Chrome::teardown() | ||
122 | { | 123 | { | ||
123 | foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) { | 124 | for (ProfileBookmarks *profileBookmarks : qAsConst(m_profileBookmarks)) { | ||
124 | profileBookmarks->tearDown(); | 125 | profileBookmarks->tearDown(); | ||
125 | } | 126 | } | ||
126 | } | 127 | } | ||
127 | 128 | | |||
128 | void Chrome::parseFolder(const QJsonObject &entry, ProfileBookmarks *profile) | 129 | void Chrome::parseFolder(const QJsonObject &entry, ProfileBookmarks *profile) | ||
129 | { | 130 | { | ||
130 | const QJsonArray children = entry.value(QStringLiteral("children")).toArray(); | 131 | const QJsonArray children = entry.value(QStringLiteral("children")).toArray(); | ||
131 | for (const QJsonValue &child : children) { | 132 | for (const QJsonValue &child : children) { | ||
132 | const QJsonObject entry = child.toObject(); | 133 | const QJsonObject entry = child.toObject(); | ||
133 | if(entry.value(QStringLiteral("type")).toString() == QLatin1String("folder")) | 134 | if(entry.value(QStringLiteral("type")).toString() == QLatin1String("folder")) | ||
134 | parseFolder(entry, profile); | 135 | parseFolder(entry, profile); | ||
135 | else { | 136 | else { | ||
136 | profile->add(entry); | 137 | profile->add(entry); | ||
137 | } | 138 | } | ||
138 | } | 139 | } | ||
139 | } | 140 | } |
const&?