1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-13 10:32:11 +01:00

[FirefoxImporter] Support importing full bookmarks structure

This commit is contained in:
nowrep 2014-02-16 15:39:59 +01:00
parent 680ed27c22
commit daa3f07426
2 changed files with 80 additions and 22 deletions

View File

@ -17,19 +17,26 @@
* ============================================================ */ * ============================================================ */
#include "firefoximporter.h" #include "firefoximporter.h"
#include "bookmarksimportdialog.h" #include "bookmarksimportdialog.h"
#include "bookmarkitem.h"
#include <QDir> #include <QDir>
#include <QVariant> #include <QVariant>
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlError> #include <QSqlError>
#include <QFileDialog> #include <QFileDialog>
#include <QSqlDatabase>
#define CONNECTION "firefox-places-import"
FirefoxImporter::FirefoxImporter(QObject* parent) FirefoxImporter::FirefoxImporter(QObject* parent)
: BookmarksImporter(parent) : BookmarksImporter(parent)
{ {
} }
FirefoxImporter::~FirefoxImporter()
{
QSqlDatabase::removeDatabase(CONNECTION);
}
QString FirefoxImporter::description() const QString FirefoxImporter::description() const
{ {
return BookmarksImporter::tr("Mozilla Firefox stores its bookmarks in <b>places.sqlite</b> SQLite " return BookmarksImporter::tr("Mozilla Firefox stores its bookmarks in <b>places.sqlite</b> SQLite "
@ -53,16 +60,20 @@ QString FirefoxImporter::getPath(QWidget* parent)
bool FirefoxImporter::prepareImport() bool FirefoxImporter::prepareImport()
{ {
m_db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(), "firefox-import"); // Make sure this connection is properly closed if already opened
QSqlDatabase::removeDatabase(CONNECTION);
// Create new connection
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION);
if (!QFile::exists(m_path)) { if (!QFile::exists(m_path)) {
setError(BookmarksImportDialog::tr("File does not exist.")); setError(BookmarksImportDialog::tr("File does not exist."));
return false; return false;
} }
m_db.setDatabaseName(m_path); db.setDatabaseName(m_path);
if (!m_db.open()) { if (!db.open()) {
setError(BookmarksImportDialog::tr("Unable to open database. Is Firefox running?")); setError(BookmarksImportDialog::tr("Unable to open database. Is Firefox running?"));
return false; return false;
} }
@ -72,41 +83,70 @@ bool FirefoxImporter::prepareImport()
BookmarkItem* FirefoxImporter::importBookmarks() BookmarkItem* FirefoxImporter::importBookmarks()
{ {
QList<Item> items;
BookmarkItem* root = new BookmarkItem(BookmarkItem::Folder); BookmarkItem* root = new BookmarkItem(BookmarkItem::Folder);
root->setTitle("Firefox Import"); root->setTitle("Firefox Import");
QSqlQuery query(m_db); QSqlQuery query(QSqlDatabase::database(CONNECTION));
query.exec("SELECT title, fk FROM moz_bookmarks WHERE title != ''"); query.exec("SELECT id, parent, type, title, fk FROM moz_bookmarks WHERE title != '' OR type = 3");
while (query.next()) { while (query.next()) {
QString title = query.value(0).toString(); Item item;
int placesId = query.value(1).toInt(); item.id = query.value(0).toInt();
item.parent = query.value(1).toInt();
item.type = typeFromValue(query.value(2).toInt());
item.title = query.value(3).toString();
int fk = query.value(4).toInt();
QSqlQuery query2(m_db); if (item.type == BookmarkItem::Invalid) {
query2.exec("SELECT url FROM moz_places WHERE id=" + QString::number(placesId));
if (!query2.next()) {
continue; continue;
} }
QUrl url = query2.value(0).toUrl(); QSqlQuery query(QSqlDatabase::database(CONNECTION));
query.prepare("SELECT url FROM moz_places WHERE id=?");
query.addBindValue(fk);
query.exec();
if (title.isEmpty() || url.isEmpty() || url.scheme() == QLatin1String("place") if (query.next()) {
|| url.scheme() == QLatin1String("about")) { item.url = query.value(0).toUrl();
}
if (item.url.scheme() == QLatin1String("place")) {
continue; continue;
} }
BookmarkItem* b = new BookmarkItem(BookmarkItem::Url, root); items.append(item);
b->setTitle(title);
b->setUrl(url);
} }
if (query.lastError().isValid()) { if (query.lastError().isValid()) {
setError(query.lastError().text()); setError(query.lastError().text());
} }
m_db.close(); QHash<int, BookmarkItem*> hash;
QSqlDatabase::removeDatabase("firefox-import");
foreach (const Item &item, items) {
BookmarkItem* parent = hash.value(item.parent);
BookmarkItem* bookmark = new BookmarkItem(item.type, parent ? parent : root);
bookmark->setTitle(item.title);
bookmark->setUrl(item.url);
hash.insert(item.id, bookmark);
}
return root; return root;
} }
BookmarkItem::Type FirefoxImporter::typeFromValue(int value)
{
switch (value) {
case 1:
return BookmarkItem::Url;
case 2:
return BookmarkItem::Folder;
case 3:
return BookmarkItem::Separator;
default:
return BookmarkItem::Invalid;
}
}

View File

@ -18,14 +18,16 @@
#ifndef FIREFOXIMPORTER_H #ifndef FIREFOXIMPORTER_H
#define FIREFOXIMPORTER_H #define FIREFOXIMPORTER_H
#include <QSqlDatabase> #include <QUrl>
#include "bookmarksimporter.h" #include "bookmarksimporter.h"
#include "bookmarkitem.h"
class QT_QUPZILLA_EXPORT FirefoxImporter : public BookmarksImporter class QT_QUPZILLA_EXPORT FirefoxImporter : public BookmarksImporter
{ {
public: public:
explicit FirefoxImporter(QObject* parent = 0); explicit FirefoxImporter(QObject* parent = 0);
~FirefoxImporter();
QString description() const; QString description() const;
QString standardPath() const; QString standardPath() const;
@ -36,8 +38,24 @@ public:
BookmarkItem* importBookmarks(); BookmarkItem* importBookmarks();
private: private:
enum Type {
Url,
Folder,
Separator,
Invalid
};
struct Item {
int id;
int parent;
BookmarkItem::Type type;
QString title;
QUrl url;
};
BookmarkItem::Type typeFromValue(int value);
QString m_path; QString m_path;
QSqlDatabase m_db;
}; };