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 #pragma once 0009 0010 #include "bookmarkmatch.h" 0011 #include <QDateTime> 0012 #include <QFile> 0013 #include <QFileInfo> 0014 #include <QJsonArray> 0015 #include <QJsonDocument> 0016 #include <QJsonObject> 0017 #include <QObject> 0018 #include <QString> 0019 0020 class Browser 0021 { 0022 public: 0023 virtual ~Browser() 0024 { 0025 } 0026 virtual QList<BookmarkMatch> match(const QString &term, bool addEveryThing) = 0; 0027 virtual void prepare() 0028 { 0029 } 0030 0031 enum CacheResult { 0032 Error, 0033 Copied, 0034 Unchanged, 0035 }; 0036 0037 public Q_SLOTS: 0038 virtual void teardown() 0039 { 0040 } 0041 0042 protected: 0043 /* 0044 * Updates the cached file if the source has been modified 0045 */ 0046 CacheResult updateCacheFile(const QString &source, const QString &cache) 0047 { 0048 if (source.isEmpty() || cache.isEmpty()) { 0049 return Error; 0050 } 0051 QFileInfo cacheInfo(cache); 0052 if (!QFileInfo::exists(cache) || !cacheInfo.isFile()) { 0053 return QFile(source).copy(cache) ? Copied : Error; 0054 } 0055 0056 QFileInfo sourceInfo(source); 0057 if (sourceInfo.lastModified() > cacheInfo.lastModified()) { 0058 QFile::remove(cache); 0059 return QFile(source).copy(cache) ? Copied : Error; 0060 } 0061 return Unchanged; 0062 } 0063 0064 QJsonArray readChromeFormatBookmarks(const QString &path) 0065 { 0066 QJsonArray bookmarks; 0067 QFile bookmarksFile(path); 0068 if (!bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { 0069 return bookmarks; 0070 } 0071 const QJsonDocument jdoc = QJsonDocument::fromJson(bookmarksFile.readAll()); 0072 if (jdoc.isNull()) { 0073 return bookmarks; 0074 } 0075 const QJsonObject resultMap = jdoc.object(); 0076 if (!resultMap.contains(QLatin1String("roots"))) { 0077 return bookmarks; 0078 } 0079 const QJsonObject entries = resultMap.value(QLatin1String("roots")).toObject(); 0080 for (const QJsonValue &folder : entries) { 0081 parseFolder(folder.toObject(), bookmarks); 0082 } 0083 return bookmarks; 0084 } 0085 0086 private: 0087 void parseFolder(const QJsonObject &obj, QJsonArray &bookmarks) 0088 { 0089 const QJsonArray children = obj.value(QStringLiteral("children")).toArray(); 0090 for (const QJsonValue &child : children) { 0091 const QJsonObject entry = child.toObject(); 0092 if (entry.value(QLatin1String("type")).toString() == QLatin1String("folder")) 0093 parseFolder(entry, bookmarks); 0094 else { 0095 bookmarks.append(entry); 0096 } 0097 } 0098 } 0099 };