From 75f3460a279cd0a002915ac2820868c4e8da78dd Mon Sep 17 00:00:00 2001 From: nowrep Date: Mon, 10 Feb 2014 16:58:05 +0100 Subject: [PATCH] [Bookmarks] Automatically migrate bookmarks from SQLite to JSON --- src/lib/bookmarks/bookmarks.cpp | 14 ++++-- src/lib/bookmarks/bookmarks.h | 1 + src/lib/bookmarks/bookmarksitemdelegate.cpp | 2 +- src/lib/bookmarks/bookmarksmanager.cpp | 7 +++ src/lib/bookmarks/bookmarkstoolbarbutton.h | 1 - src/lib/bookmarks/bookmarkstools.cpp | 53 +++++++++++++++++++++ src/lib/bookmarks/bookmarkstools.h | 4 ++ src/lib/tools/iconprovider.cpp | 2 +- 8 files changed, 78 insertions(+), 6 deletions(-) diff --git a/src/lib/bookmarks/bookmarks.cpp b/src/lib/bookmarks/bookmarks.cpp index a27cf1eb9..b35037a8d 100644 --- a/src/lib/bookmarks/bookmarks.cpp +++ b/src/lib/bookmarks/bookmarks.cpp @@ -18,6 +18,7 @@ #include "bookmarks.h" #include "bookmarkitem.h" #include "bookmarksmodel.h" +#include "bookmarkstools.h" #include "mainapplication.h" #include "qztools.h" #include "webview.h" @@ -263,6 +264,16 @@ void Bookmarks::init() m_folderUnsorted->setTitle(tr("Unsorted Bookmarks")); m_folderUnsorted->setDescription(tr("All other bookmarks")); + if (!BookmarksTools::migrateBookmarksIfNecessary(this)) { + loadBookmarks(); + } + + m_lastFolder = m_folderUnsorted; + m_model = new BookmarksModel(this, this); +} + +void Bookmarks::loadBookmarks() +{ const QString bookmarksFile = mApp->currentProfilePath() + QLatin1String("/bookmarks.json"); const QString backupFile = bookmarksFile + QLatin1String(".old"); @@ -293,9 +304,6 @@ void Bookmarks::init() else { loadBookmarksFromMap(res.toMap().value("roots").toMap()); } - - m_lastFolder = m_folderUnsorted; - m_model = new BookmarksModel(this, this); } void Bookmarks::saveBookmarks() diff --git a/src/lib/bookmarks/bookmarks.h b/src/lib/bookmarks/bookmarks.h index 794d4533b..d0fda69bf 100644 --- a/src/lib/bookmarks/bookmarks.h +++ b/src/lib/bookmarks/bookmarks.h @@ -78,6 +78,7 @@ signals: private: void init(); + void loadBookmarks(); void saveBookmarks(); void loadBookmarksFromMap(const QVariantMap &map); diff --git a/src/lib/bookmarks/bookmarksitemdelegate.cpp b/src/lib/bookmarks/bookmarksitemdelegate.cpp index b5c2725a6..7a2ef294e 100644 --- a/src/lib/bookmarks/bookmarksitemdelegate.cpp +++ b/src/lib/bookmarks/bookmarksitemdelegate.cpp @@ -39,7 +39,7 @@ void BookmarksItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem opt.frameShape = QFrame::HLine; opt.rect = option.rect; - // We need to fake continous line over 2 columns + // We need to fake continuous line over 2 columns if (m_tree->viewType() == BookmarksTreeView::BookmarksManagerViewType) { if (index.column() == 1) { opt.rect = m_lastRect; diff --git a/src/lib/bookmarks/bookmarksmanager.cpp b/src/lib/bookmarks/bookmarksmanager.cpp index b2e925aba..ea845c6a3 100644 --- a/src/lib/bookmarks/bookmarksmanager.cpp +++ b/src/lib/bookmarks/bookmarksmanager.cpp @@ -300,8 +300,15 @@ void BookmarksManager::addBookmark(BookmarkItem* item) m_bookmarks->addBookmark(parent, item); + // Select newly added bookmark ui->tree->selectBookmark(item); ui->tree->ensureBookmarkVisible(item); + + // Start editing title + if (!item->isSeparator()) { + ui->title->setFocus(); + ui->title->selectAll(); + } } BookmarkItem* BookmarksManager::parentForNewBookmark() const diff --git a/src/lib/bookmarks/bookmarkstoolbarbutton.h b/src/lib/bookmarks/bookmarkstoolbarbutton.h index fcc6de87b..2ceaad8b7 100644 --- a/src/lib/bookmarks/bookmarkstoolbarbutton.h +++ b/src/lib/bookmarks/bookmarkstoolbarbutton.h @@ -43,7 +43,6 @@ public: QSize minimumSizeHint() const; private slots: - void bookmarkRemoved(BookmarkItem* bookmark); void createMenu(); void menuMiddleClicked(Menu* menu); diff --git a/src/lib/bookmarks/bookmarkstools.cpp b/src/lib/bookmarks/bookmarkstools.cpp index f85cf47fe..853205c78 100644 --- a/src/lib/bookmarks/bookmarkstools.cpp +++ b/src/lib/bookmarks/bookmarkstools.cpp @@ -25,6 +25,8 @@ #include "qzsettings.h" #include "qupzilla.h" +#include +#include #include #include #include @@ -339,3 +341,54 @@ void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator) menu->addSeparator(); } + +bool BookmarksTools::migrateBookmarksIfNecessary(Bookmarks* bookmarks) +{ + QSqlQuery query; + query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='folders'"); + + if (!query.next()) { + return false; + } + + std::cout << "Bookmarks: Migrating your bookmarks from SQLite to JSON..." << std::endl; + + QHash folders; + folders.insert("bookmarksToolbar", bookmarks->toolbarFolder()); + folders.insert("bookmarksMenu", bookmarks->menuFolder()); + folders.insert("unsorted", bookmarks->unsortedFolder()); + + query.exec("SELECT name, subfolder FROM folders"); + while (query.next()) { + const QString title = query.value(0).toString(); + bool subfolder = query.value(1).toString() == QLatin1String("yes"); + + BookmarkItem* folder = new BookmarkItem(BookmarkItem::Folder, subfolder ? bookmarks->toolbarFolder() : 0); + folder->setTitle(title); + folders.insert(folder->title(), folder); + } + + query.exec("SELECT title, folder, url FROM bookmarks ORDER BY position ASC"); + while (query.next()) { + const QString title = query.value(0).toString(); + const QString folder = query.value(1).toString(); + const QUrl url = query.value(2).toUrl(); + + BookmarkItem* parent = folders.value(folder); + if (folder.isEmpty()) { + parent = bookmarks->unsortedFolder(); + } + Q_ASSERT(parent); + + BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url, parent); + bookmark->setTitle(title); + bookmark->setUrl(url); + } + + query.exec("DROP TABLE folders"); + query.exec("DROP TABLE bookmarks"); + query.exec("VACUUM"); + + std::cout << "Bookmarks: Bookmarks successfully migrated!" << std::endl; + return true; +} diff --git a/src/lib/bookmarks/bookmarkstools.h b/src/lib/bookmarks/bookmarkstools.h index da68841f2..8b2f676a8 100644 --- a/src/lib/bookmarks/bookmarkstools.h +++ b/src/lib/bookmarks/bookmarkstools.h @@ -23,6 +23,7 @@ #include "qz_namespace.h" +class Bookmarks; class BookmarkItem; class QT_QUPZILLA_EXPORT BookmarksFoldersMenu : public QMenu @@ -92,6 +93,9 @@ public: static void addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder); static void addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark); static void addSeparatorToMenu(Menu* menu, BookmarkItem* separator); + + // Migration from Sql Bookmarks (returns true if bookmarks migrated) + static bool migrateBookmarksIfNecessary(Bookmarks* bookmarks); }; #endif // BOOKMARKSTOOLS_H diff --git a/src/lib/tools/iconprovider.cpp b/src/lib/tools/iconprovider.cpp index 708405bef..723f7218d 100644 --- a/src/lib/tools/iconprovider.cpp +++ b/src/lib/tools/iconprovider.cpp @@ -70,7 +70,7 @@ void IconProvider::saveIcon(WebView* view) QImage IconProvider::iconForUrl(const QUrl &url) { - if (url.isEmpty()) { + if (url.path().isEmpty()) { return IconProvider::emptyWebImage(); }