/* * Copyright 2007 Glenn Ergeerts * Copyright 2012 Glenn Ergeerts * * 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 #include #include #include #include "chrome.h" #include "faviconfromblob.h" #include "browsers/findprofile.h" #include "bookmarksrunner_defs.h" #ifndef QT_KATIE # include #else # include #endif class ProfileBookmarks { public: ProfileBookmarks(Profile &profile) : m_profile(profile) {} inline QList bookmarks() { return m_bookmarks; } inline Profile profile() { return m_profile; } void tearDown() { m_profile.favicon()->teardown(); m_bookmarks.clear(); } void add(QVariantMap &bookmarkEntry) { m_bookmarks << bookmarkEntry; } private: Profile m_profile; QList m_bookmarks; }; Chrome::Chrome( FindProfile* findProfile, QObject* parent ) : QObject(parent) { foreach(Profile profile, findProfile->find()) { m_profileBookmarks << new ProfileBookmarks(profile); } } Chrome::~Chrome() { foreach(ProfileBookmarks *profileBookmark, m_profileBookmarks) { delete profileBookmark; } } QList Chrome::match(const QString &term, bool addEveryThing) { QList results; foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) { results << match(term, addEveryThing, profileBookmarks); } return results; } QList Chrome::match(const QString &term, bool addEveryThing, ProfileBookmarks *profileBookmarks) { QList results; foreach(QVariantMap bookmark, profileBookmarks->bookmarks()) { QString url = bookmark.value("url").toString(); BookmarkMatch bookmarkMatch(profileBookmarks->profile().favicon(), term, bookmark.value("name").toString(), url); bookmarkMatch.addTo(results, addEveryThing); } return results; } void Chrome::prepare() { foreach(ProfileBookmarks *profileBookmarks, m_profileBookmarks) { Profile profile = profileBookmarks->profile(); QFile bookmarksFile(profile.path()); #ifndef QT_KATIE QJson::Parser parser; bool ok; QVariant result = parser.parse(&bookmarksFile, &ok); if(!ok || !result.toMap().contains("roots")) { return; } #else QJsonParseError error; QVariant result = QJsonDocument::fromJson(bookmarksFile.readAll(), &error).toVariant(); if(error.error != QJsonParseError::NoError || !result.toMap().contains("roots")) { return; } #endif QVariantMap entries = result.toMap().value("roots").toMap(); foreach(QVariant folder, entries.values()) { 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(); foreach(QVariant child, children) { QVariantMap entry = child.toMap(); if(entry.value("type").toString() == "folder") parseFolder(entry, profile); else { profile->add(entry); } } }