2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-10-29 12:24:12 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, 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 General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-10-22 22:29:33 +02:00
|
|
|
#include "chromeimporter.h"
|
2014-02-10 14:57:48 +01:00
|
|
|
#include "bookmarkitem.h"
|
2014-02-15 20:53:03 +01:00
|
|
|
#include "json.h"
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2014-02-15 00:17:31 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileDialog>
|
2014-02-15 20:53:03 +01:00
|
|
|
#include <QVariantList>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2011-10-22 22:29:33 +02:00
|
|
|
ChromeImporter::ChromeImporter(QObject* parent)
|
2014-02-15 00:17:31 +01:00
|
|
|
: BookmarksImporter(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChromeImporter::description() const
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
2014-02-15 00:17:31 +01:00
|
|
|
return BookmarksImporter::tr("Google Chrome stores its bookmarks in <b>Bookmarks</b> text file. "
|
|
|
|
"This file is usually located in");
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
2014-02-15 00:17:31 +01:00
|
|
|
QString ChromeImporter::standardPath() const
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
2014-02-15 00:17:31 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
return QString("%APPDATA%/Chrome/");
|
|
|
|
#else
|
|
|
|
return QDir::homePath() + QLatin1String("/.config/chrome/");
|
|
|
|
#endif
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
2014-02-15 00:17:31 +01:00
|
|
|
QString ChromeImporter::getPath(QWidget* parent)
|
|
|
|
{
|
|
|
|
m_path = QFileDialog::getOpenFileName(parent, BookmarksImporter::tr("Choose file..."), standardPath(), "Bookmarks (Bookmarks)");
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChromeImporter::prepareImport()
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
|
|
|
m_file.setFileName(m_path);
|
|
|
|
|
|
|
|
if (!m_file.open(QFile::ReadOnly)) {
|
2014-02-15 00:17:31 +01:00
|
|
|
setError(BookmarksImporter::tr("Unable to open file."));
|
2011-10-22 22:29:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:17:31 +01:00
|
|
|
BookmarkItem* ChromeImporter::importBookmarks()
|
2011-10-22 22:29:33 +02:00
|
|
|
{
|
2012-03-06 15:28:52 +01:00
|
|
|
QString bookmarks = QString::fromUtf8(m_file.readAll());
|
2011-10-22 22:29:33 +02:00
|
|
|
m_file.close();
|
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
Json json;
|
|
|
|
const QVariant res = json.parse(bookmarks);
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
if (!json.ok() || res.type() != QVariant::Map) {
|
|
|
|
setError(BookmarksImporter::tr("Cannot parse JSON file!"));
|
|
|
|
return 0;
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
QVariantMap rootMap = res.toMap().value("roots").toMap();
|
|
|
|
|
2014-02-10 14:57:48 +01:00
|
|
|
BookmarkItem* root = new BookmarkItem(BookmarkItem::Folder);
|
|
|
|
root->setTitle("Chrome Import");
|
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
BookmarkItem* toolbar = new BookmarkItem(BookmarkItem::Folder, root);
|
|
|
|
toolbar->setTitle(rootMap.value("bookmark_bar").toMap().value("name").toString());
|
|
|
|
readBookmarks(rootMap.value("bookmark_bar").toMap().value("children").toList(), toolbar);
|
|
|
|
|
|
|
|
BookmarkItem* other = new BookmarkItem(BookmarkItem::Folder, root);
|
|
|
|
other->setTitle(rootMap.value("other").toMap().value("name").toString());
|
|
|
|
readBookmarks(rootMap.value("other").toMap().value("children").toList(), other);
|
|
|
|
|
|
|
|
BookmarkItem* synced = new BookmarkItem(BookmarkItem::Folder, root);
|
|
|
|
synced->setTitle(rootMap.value("synced").toMap().value("name").toString());
|
|
|
|
readBookmarks(rootMap.value("synced").toMap().value("synced").toList(), other);
|
|
|
|
|
|
|
|
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;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-02-15 20:53:03 +01:00
|
|
|
continue;
|
2011-10-22 22:29:33 +02:00
|
|
|
}
|
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
BookmarkItem* item = new BookmarkItem(type, parent);
|
|
|
|
item->setTitle(map.value("name").toString());
|
2011-10-22 22:29:33 +02:00
|
|
|
|
2014-02-15 20:53:03 +01:00
|
|
|
if (item->isUrl()) {
|
|
|
|
item->setUrl(QUrl::fromEncoded(map.value("url").toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("children")) {
|
|
|
|
readBookmarks(map.value("children").toList(), item);
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 00:17:31 +01:00
|
|
|
}
|