Falkon Develop
Cross-platform Qt-based web browser
chromeimporter.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "chromeimporter.h"
19#include "bookmarkitem.h"
20
21#include <QDir>
22#include <QFileDialog>
23#include <QJsonDocument>
24#include <QMetaType>
25
27 : BookmarksImporter(parent)
28{
29}
30
32{
33 return BookmarksImporter::tr("Google Chrome stores its bookmarks in <b>Bookmarks</b> text file. "
34 "This file is usually located in");
35}
36
38{
39#if defined(Q_OS_WIN)
40 return QString("%APPDATA%/Chrome/");
41#elif defined(Q_OS_OSX)
42 return QDir::homePath() + QLatin1String("/Library/Application Support/Google/Chrome/");
43#else
44 return QDir::homePath() + QLatin1String("/.config/chrome/");
45#endif
46}
47
48QString ChromeImporter::getPath(QWidget* parent)
49{
50 m_path = QFileDialog::getOpenFileName(parent, BookmarksImporter::tr("Choose file..."), standardPath(), QSL("Bookmarks (Bookmarks)"));
51 return m_path;
52}
53
55{
56 m_file.setFileName(m_path);
57
58 if (!m_file.open(QFile::ReadOnly)) {
59 setError(BookmarksImporter::tr("Unable to open file."));
60 return false;
61 }
62
63 return true;
64}
65
67{
68 const QByteArray data = m_file.readAll();
69 m_file.close();
70
71 QJsonParseError err;
72 QJsonDocument json = QJsonDocument::fromJson(data, &err);
73 const QVariant res = json.toVariant();
74
75 if (err.error != QJsonParseError::NoError || res.typeId() != QMetaType::QVariantMap) {
76 setError(BookmarksImporter::tr("Cannot parse JSON file!"));
77 return nullptr;
78 }
79
80 QVariantMap rootMap = res.toMap().value(QSL("roots")).toMap();
81
82 auto* root = new BookmarkItem(BookmarkItem::Folder);
83 root->setTitle(QSL("Chrome Import"));
84
85 auto* toolbar = new BookmarkItem(BookmarkItem::Folder, root);
86 toolbar->setTitle(rootMap.value(QSL("bookmark_bar")).toMap().value(QSL("name")).toString());
87 readBookmarks(rootMap.value(QSL("bookmark_bar")).toMap().value(QSL("children")).toList(), toolbar);
88
89 auto* other = new BookmarkItem(BookmarkItem::Folder, root);
90 other->setTitle(rootMap.value(QSL("other")).toMap().value(QSL("name")).toString());
91 readBookmarks(rootMap.value(QSL("other")).toMap().value(QSL("children")).toList(), other);
92
93 auto* synced = new BookmarkItem(BookmarkItem::Folder, root);
94 synced->setTitle(rootMap.value(QSL("synced")).toMap().value(QSL("name")).toString());
95 readBookmarks(rootMap.value(QSL("synced")).toMap().value(QSL("synced")).toList(), other);
96
97 return root;
98}
99
100void ChromeImporter::readBookmarks(const QVariantList &list, BookmarkItem* parent)
101{
102 Q_ASSERT(parent);
103
104 for (const QVariant &entry : list) {
105 const QVariantMap map = entry.toMap();
106 const QString typeString = map.value(QSL("type")).toString();
108
109 if (typeString == QLatin1String("url")) {
110 type = BookmarkItem::Url;
111 }
112 else if (typeString == QLatin1String("folder")) {
114 }
115 else {
116 continue;
117 }
118
119 auto* item = new BookmarkItem(type, parent);
120 item->setTitle(map.value(QSL("name")).toString());
121
122 if (item->isUrl()) {
123 item->setUrl(QUrl::fromEncoded(map.value(QSL("url")).toByteArray()));
124 }
125
126 if (map.contains(QSL("children"))) {
127 readBookmarks(map.value(QSL("children")).toList(), item);
128 }
129 }
130}
void setError(const QString &error)
ChromeImporter(QObject *parent=nullptr)
bool prepareImport() override
QString getPath(QWidget *parent) override
QString description() const override
BookmarkItem * importBookmarks() override
QString standardPath() const override
int value(const QColor &c)
Definition: colors.cpp:238
#define QSL(x)
Definition: qzcommon.h:40