Falkon Develop
Cross-platform Qt-based web browser
firefoximporter.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 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 "firefoximporter.h"
20
21#include <QDir>
22#include <QVariant>
23#include <QSqlError>
24#include <QFileDialog>
25#include <QSqlQuery>
26#include <QSqlDatabase>
27
28#define CONNECTION QSL("firefox-places-import")
29
31 : BookmarksImporter(parent)
32{
33}
34
36{
37 QSqlDatabase::removeDatabase(CONNECTION);
38}
39
41{
42 return BookmarksImporter::tr("Mozilla Firefox stores its bookmarks in <b>places.sqlite</b> SQLite "
43 "database. This file is usually located in");
44}
45
47{
48#ifdef Q_OS_WIN
49 return QString("%APPDATA%/Mozilla/");
50#else
51 return QDir::homePath() + QLatin1String("/.mozilla/firefox/");
52#endif
53}
54
55QString FirefoxImporter::getPath(QWidget* parent)
56{
57 m_path = QFileDialog::getOpenFileName(parent, BookmarksImporter::tr("Choose file..."), standardPath(), QStringLiteral("Places (places.sqlite)"));
58 return m_path;
59}
60
62{
63 // Make sure this connection is properly closed if already opened
64 QSqlDatabase::removeDatabase(CONNECTION);
65
66 // Create new connection
67 QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION);
68
69 if (!QFile::exists(m_path)) {
70 setError(BookmarksImportDialog::tr("File does not exist."));
71 return false;
72 }
73
74 db.setDatabaseName(m_path);
75
76 if (!db.open()) {
77 setError(BookmarksImportDialog::tr("Unable to open database. Is Firefox running?"));
78 return false;
79 }
80
81 return true;
82}
83
85{
86 QList<Item> items;
87
88 auto* root = new BookmarkItem(BookmarkItem::Folder);
89 root->setTitle(QStringLiteral("Firefox Import"));
90
91 QSqlQuery query(QSqlDatabase::database(CONNECTION));
92 query.prepare(QStringLiteral("SELECT id, parent, type, title, fk FROM moz_bookmarks WHERE fk NOT NULL OR type = 3"));
93 query.exec();
94
95 while (query.next()) {
96 Item item;
97 item.id = query.value(0).toInt();
98 item.parent = query.value(1).toInt();
99 item.type = typeFromValue(query.value(2).toInt());
100 item.title = query.value(3).toString();
101 int fk = query.value(4).toInt();
102
103 if (item.type == BookmarkItem::Invalid) {
104 continue;
105 }
106
107 QSqlQuery query(QSqlDatabase::database(CONNECTION));
108 query.prepare(QStringLiteral("SELECT url FROM moz_places WHERE id=?"));
109 query.addBindValue(fk);
110 query.exec();
111
112 if (query.next()) {
113 item.url = query.value(0).toUrl();
114 }
115
116 if (item.url.scheme() == QLatin1String("place")) {
117 continue;
118 }
119
120 items.append(item);
121 }
122
123 if (query.lastError().isValid()) {
124 setError(query.lastError().text());
125 }
126
127 QHash<int, BookmarkItem*> hash;
128
129 for (const Item &item : std::as_const(items)) {
130 BookmarkItem* parent = hash.value(item.parent);
131 auto* bookmark = new BookmarkItem(item.type, parent ? parent : root);
132 bookmark->setTitle(item.title.isEmpty() ? item.url.toString() : item.title);
133 bookmark->setUrl(item.url);
134
135 hash.insert(item.id, bookmark);
136 }
137
138 return root;
139}
140
141BookmarkItem::Type FirefoxImporter::typeFromValue(int value)
142{
143 switch (value) {
144 case 1:
145 return BookmarkItem::Url;
146 case 2:
148 case 3:
150 default:
152 }
153}
void setError(const QString &error)
BookmarkItem * importBookmarks() override
QString description() const override
bool prepareImport() override
QString standardPath() const override
~FirefoxImporter() override
QString getPath(QWidget *parent) override
FirefoxImporter(QObject *parent=nullptr)
#define CONNECTION
int value(const QColor &c)
Definition: colors.cpp:238