mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 10:46:35 +01:00
[ChromeImporter] Support importing full bookmarks structure
This commit is contained in:
parent
9a4193f0b3
commit
650558dea6
@ -17,13 +17,11 @@
|
|||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
#include "chromeimporter.h"
|
#include "chromeimporter.h"
|
||||||
#include "bookmarkitem.h"
|
#include "bookmarkitem.h"
|
||||||
#include "qzregexp.h"
|
#include "json.h"
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QScriptEngine>
|
#include <QVariantList>
|
||||||
#include <QScriptValue>
|
|
||||||
#include <QScriptValueIterator>
|
|
||||||
|
|
||||||
ChromeImporter::ChromeImporter(QObject* parent)
|
ChromeImporter::ChromeImporter(QObject* parent)
|
||||||
: BookmarksImporter(parent)
|
: BookmarksImporter(parent)
|
||||||
@ -68,41 +66,62 @@ BookmarkItem* ChromeImporter::importBookmarks()
|
|||||||
QString bookmarks = QString::fromUtf8(m_file.readAll());
|
QString bookmarks = QString::fromUtf8(m_file.readAll());
|
||||||
m_file.close();
|
m_file.close();
|
||||||
|
|
||||||
QStringList parsedBookmarks;
|
Json json;
|
||||||
QzRegExp rx("\\{(\\s*)\"date_added(.*)\"(\\s*)\\}", Qt::CaseSensitive);
|
const QVariant res = json.parse(bookmarks);
|
||||||
rx.setMinimal(true);
|
|
||||||
|
|
||||||
int pos = 0;
|
if (!json.ok() || res.type() != QVariant::Map) {
|
||||||
while ((pos = rx.indexIn(bookmarks, pos)) != -1) {
|
setError(BookmarksImporter::tr("Cannot parse JSON file!"));
|
||||||
parsedBookmarks << rx.cap(0);
|
return 0;
|
||||||
pos += rx.matchedLength();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariantMap rootMap = res.toMap().value("roots").toMap();
|
||||||
|
|
||||||
BookmarkItem* root = new BookmarkItem(BookmarkItem::Folder);
|
BookmarkItem* root = new BookmarkItem(BookmarkItem::Folder);
|
||||||
root->setTitle("Chrome Import");
|
root->setTitle("Chrome Import");
|
||||||
|
|
||||||
QScriptEngine* scriptEngine = new QScriptEngine();
|
BookmarkItem* toolbar = new BookmarkItem(BookmarkItem::Folder, root);
|
||||||
foreach (QString parsedString, parsedBookmarks) {
|
toolbar->setTitle(rootMap.value("bookmark_bar").toMap().value("name").toString());
|
||||||
parsedString = "(" + parsedString + ")";
|
readBookmarks(rootMap.value("bookmark_bar").toMap().value("children").toList(), toolbar);
|
||||||
if (scriptEngine->canEvaluate(parsedString)) {
|
|
||||||
QScriptValue object = scriptEngine->evaluate(parsedString);
|
|
||||||
QString name = object.property("name").toString().trimmed();
|
|
||||||
QUrl url = QUrl::fromEncoded(object.property("url").toString().trimmed().toUtf8());
|
|
||||||
|
|
||||||
if (name.isEmpty() || url.isEmpty()) {
|
BookmarkItem* other = new BookmarkItem(BookmarkItem::Folder, root);
|
||||||
continue;
|
other->setTitle(rootMap.value("other").toMap().value("name").toString());
|
||||||
}
|
readBookmarks(rootMap.value("other").toMap().value("children").toList(), other);
|
||||||
|
|
||||||
BookmarkItem* b = new BookmarkItem(BookmarkItem::Url, root);
|
BookmarkItem* synced = new BookmarkItem(BookmarkItem::Folder, root);
|
||||||
b->setTitle(name);
|
synced->setTitle(rootMap.value("synced").toMap().value("name").toString());
|
||||||
b->setUrl(url);
|
readBookmarks(rootMap.value("synced").toMap().value("synced").toList(), other);
|
||||||
}
|
|
||||||
else {
|
|
||||||
m_error = true;
|
|
||||||
m_errorString = BookmarksImporter::tr("Cannot evaluate JSON code.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChromeImporter::readBookmarks(const QVariantList &list, BookmarkItem* parent)
|
||||||
|
{
|
||||||
|
Q_ASSERT(parent);
|
||||||
|
|
||||||
|
foreach (const QVariant &entry, list) {
|
||||||
|
const QVariantMap map = entry.toMap();
|
||||||
|
const QString typeString = map.value("type").toString();
|
||||||
|
BookmarkItem::Type type;
|
||||||
|
|
||||||
|
if (typeString == QLatin1String("url")) {
|
||||||
|
type = BookmarkItem::Url;
|
||||||
|
}
|
||||||
|
else if (typeString == QLatin1String("folder")) {
|
||||||
|
type = BookmarkItem::Folder;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookmarkItem* item = new BookmarkItem(type, parent);
|
||||||
|
item->setTitle(map.value("name").toString());
|
||||||
|
|
||||||
|
if (item->isUrl()) {
|
||||||
|
item->setUrl(QUrl::fromEncoded(map.value("url").toByteArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map.contains("children")) {
|
||||||
|
readBookmarks(map.value("children").toList(), item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#define CHROMEIMPORTER_H
|
#define CHROMEIMPORTER_H
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QVariantList>
|
||||||
|
|
||||||
#include "bookmarksimporter.h"
|
#include "bookmarksimporter.h"
|
||||||
|
|
||||||
@ -36,12 +37,10 @@ public:
|
|||||||
BookmarkItem* importBookmarks();
|
BookmarkItem* importBookmarks();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void readBookmarks(const QVariantList &list, BookmarkItem* parent);
|
||||||
|
|
||||||
QString m_path;
|
QString m_path;
|
||||||
QFile m_file;
|
QFile m_file;
|
||||||
|
|
||||||
bool m_error;
|
|
||||||
QString m_errorString;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CHROMEIMPORTER_H
|
#endif // CHROMEIMPORTER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user