1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[Bookmarks] Cleanup in Bookmarks class

This commit is contained in:
nowrep 2014-02-10 12:58:34 +01:00
parent df9703f4a3
commit 284e58ebca
6 changed files with 254 additions and 336 deletions

View File

@ -1189,7 +1189,7 @@ bool MainApplication::saveStateSlot()
// Save cookies & bookmarks // Save cookies & bookmarks
m_cookiejar->saveCookies(); m_cookiejar->saveCookies();
m_bookmarks->saveBookmarks(); m_bookmarks->saveSettings();
return true; return true;
} }

View File

@ -36,6 +36,217 @@ Bookmarks::Bookmarks(QObject* parent)
loadSettings(); loadSettings();
} }
Bookmarks::~Bookmarks()
{
delete m_root;
}
void Bookmarks::loadSettings()
{
Settings settings;
settings.beginGroup("Bookmarks");
m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool();
settings.endGroup();
}
void Bookmarks::saveSettings()
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("showOnlyIconsInToolbar", m_showOnlyIconsInToolbar);
settings.endGroup();
saveBookmarks();
}
bool Bookmarks::showOnlyIconsInToolbar() const
{
return m_showOnlyIconsInToolbar;
}
void Bookmarks::exportToHtml(const QString &fileName)
{
Q_UNUSED(fileName)
#if 0
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
qWarning() << "Bookmarks::exportHtml Cannot open file for writing!" << file.errorString();
}
QTextStream out(&file);
out << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
out << "<!-- This is an automatically generated file." << endl;
out << " It will be read and overwritten." << endl;
out << " DO NOT EDIT! -->" << endl;
out << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" << endl;
out << "<TITLE>Bookmarks</TITLE>" << endl;
out << "<H1>Bookmarks</H1>" << endl;
out << "<DL><p>" << endl;
QString indent = " ";
QList<QPair<QString, bool> > allFolders;
QPair<QString, bool> menu;
menu.first = "bookmarksMenu";
menu.second = false;
QPair<QString, bool> toolbar;
toolbar.first = "bookmarksToolbar";
toolbar.second = false;
allFolders.append(menu);
allFolders.append(toolbar);
QSqlQuery query;
query.exec("SELECT name, subfolder FROM folders");
while (query.next()) {
QPair<QString, bool> pair;
pair.first = query.value(0).toString();
pair.second = query.value(1).toString() == QLatin1String("yes");
allFolders.append(pair);
}
for (int i = 0; i < allFolders.size(); ++i) {
QPair<QString, bool> pair = allFolders.at(i);
out << indent << "<DT><H3 TOOLBAR_SUBFOLDER=\"" << (pair.second ? "yes" : "no") << "\">" << pair.first << "</H3>" << endl;
out << indent << "<DL><p>" << endl;
QSqlQuery q;
q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?");
q.addBindValue(pair.first);
q.exec();
while (q.next()) {
QString title = q.value(0).toString();
QString url = q.value(1).toString();
out << indent << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << indent << "</DL><p>" << endl;
}
query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'");
while (query.next()) {
QString title = query.value(0).toString();
QString url = query.value(1).toString();
out << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << "</DL><p>" << endl;
#endif
}
BookmarkItem* Bookmarks::rootItem() const
{
return m_root;
}
BookmarkItem* Bookmarks::toolbarFolder() const
{
return m_folderToolbar;
}
BookmarkItem* Bookmarks::menuFolder() const
{
return m_folderMenu;
}
BookmarkItem* Bookmarks::unsortedFolder() const
{
return m_folderUnsorted;
}
BookmarkItem* Bookmarks::lastUsedFolder() const
{
return m_lastFolder;
}
BookmarksModel* Bookmarks::model() const
{
return m_model;
}
bool Bookmarks::isBookmarked(const QUrl &url)
{
return !searchBookmarks(url).isEmpty();
}
bool Bookmarks::canBeModified(BookmarkItem* item) const
{
Q_ASSERT(item);
return item != m_root &&
item != m_folderToolbar &&
item != m_folderMenu &&
item != m_folderUnsorted;
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
{
QList<BookmarkItem*> items;
search(&items, m_root, url);
return items;
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const
{
QList<BookmarkItem*> items;
search(&items, m_root, string, sensitive);
return items;
}
void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->isFolder());
Q_ASSERT(item);
insertBookmark(parent, 0, item);
}
void Bookmarks::insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->isFolder());
Q_ASSERT(item);
m_lastFolder = parent;
m_model->addBookmark(parent, row, item);
emit bookmarkAdded(item);
}
bool Bookmarks::removeBookmark(BookmarkItem* item)
{
if (!canBeModified(item)) {
return false;
}
m_model->removeBookmark(item);
emit bookmarkRemoved(item);
return true;
}
void Bookmarks::notifyBookmarkChanged(BookmarkItem* item)
{
Q_ASSERT(item);
emit bookmarkChanged(item);
}
void Bookmarks::setShowOnlyIconsInToolbar(bool state)
{
m_showOnlyIconsInToolbar = state;
emit showOnlyIconsInToolbarChanged(state);
}
void Bookmarks::init() void Bookmarks::init()
{ {
m_root = new BookmarkItem(BookmarkItem::Root); m_root = new BookmarkItem(BookmarkItem::Root);
@ -87,19 +298,6 @@ void Bookmarks::init()
m_model = new BookmarksModel(this, this); m_model = new BookmarksModel(this, this);
} }
void Bookmarks::loadBookmarksFromMap(const QVariantMap &map)
{
#define READ_FOLDER(name, folder) \
readBookmarks(map.value(name).toMap().value("children").toList(), folder); \
folder->setExpanded(map.value(name).toMap().value("expanded").toBool()); \
folder->setSidebarExpanded(map.value(name).toMap().value("expanded_sidebar").toBool());
READ_FOLDER("bookmark_bar", m_folderToolbar)
READ_FOLDER("bookmark_menu", m_folderMenu)
READ_FOLDER("other", m_folderUnsorted)
#undef READ_FOLDER
}
void Bookmarks::saveBookmarks() void Bookmarks::saveBookmarks()
{ {
QVariantMap bookmarksMap; QVariantMap bookmarksMap;
@ -141,6 +339,19 @@ void Bookmarks::saveBookmarks()
file.close(); file.close();
} }
void Bookmarks::loadBookmarksFromMap(const QVariantMap &map)
{
#define READ_FOLDER(name, folder) \
readBookmarks(map.value(name).toMap().value("children").toList(), folder); \
folder->setExpanded(map.value(name).toMap().value("expanded").toBool()); \
folder->setSidebarExpanded(map.value(name).toMap().value("expanded_sidebar").toBool());
READ_FOLDER("bookmark_bar", m_folderToolbar)
READ_FOLDER("bookmark_menu", m_folderMenu)
READ_FOLDER("other", m_folderUnsorted)
#undef READ_FOLDER
}
void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent) void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent)
{ {
Q_ASSERT(parent); Q_ASSERT(parent);
@ -271,260 +482,3 @@ void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const
break; break;
} }
} }
void Bookmarks::loadSettings()
{
Settings settings;
settings.beginGroup("Bookmarks");
m_showMostVisited = settings.value("showMostVisited", true).toBool();
m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool();
settings.endGroup();
}
bool Bookmarks::isShowingMostVisited() const
{
return m_showMostVisited;
}
void Bookmarks::setShowingMostVisited(bool state)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("showMostVisited", state);
settings.endGroup();
m_showMostVisited = state;
}
bool Bookmarks::isShowingOnlyIconsInToolbar() const
{
return m_showOnlyIconsInToolbar;
}
void Bookmarks::setShowingOnlyIconsInToolbar(bool state)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("showOnlyIconsInToolbar", state);
settings.endGroup();
m_showOnlyIconsInToolbar = state;
}
void Bookmarks::setLastFolder(const QString &folder)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("lastFolder", folder);
settings.endGroup();
}
void Bookmarks::exportToHtml(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
qWarning() << "Bookmarks::exportHtml Cannot open file for writing!" << file.errorString();
}
QTextStream out(&file);
out << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
out << "<!-- This is an automatically generated file." << endl;
out << " It will be read and overwritten." << endl;
out << " DO NOT EDIT! -->" << endl;
out << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" << endl;
out << "<TITLE>Bookmarks</TITLE>" << endl;
out << "<H1>Bookmarks</H1>" << endl;
out << "<DL><p>" << endl;
QString indent = " ";
QList<QPair<QString, bool> > allFolders;
QPair<QString, bool> menu;
menu.first = "bookmarksMenu";
menu.second = false;
QPair<QString, bool> toolbar;
toolbar.first = "bookmarksToolbar";
toolbar.second = false;
allFolders.append(menu);
allFolders.append(toolbar);
QSqlQuery query;
query.exec("SELECT name, subfolder FROM folders");
while (query.next()) {
QPair<QString, bool> pair;
pair.first = query.value(0).toString();
pair.second = query.value(1).toString() == QLatin1String("yes");
allFolders.append(pair);
}
for (int i = 0; i < allFolders.size(); ++i) {
QPair<QString, bool> pair = allFolders.at(i);
out << indent << "<DT><H3 TOOLBAR_SUBFOLDER=\"" << (pair.second ? "yes" : "no") << "\">" << pair.first << "</H3>" << endl;
out << indent << "<DL><p>" << endl;
QSqlQuery q;
q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?");
q.addBindValue(pair.first);
q.exec();
while (q.next()) {
QString title = q.value(0).toString();
QString url = q.value(1).toString();
out << indent << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << indent << "</DL><p>" << endl;
}
query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'");
while (query.next()) {
QString title = query.value(0).toString();
QString url = query.value(1).toString();
out << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << "</DL><p>" << endl;
}
QString Bookmarks::toTranslatedFolder(const QString &name)
{
QString trFolder;
if (name == QLatin1String("bookmarksMenu")) {
trFolder = tr("Bookmarks In Menu");
}
else if (name == QLatin1String("bookmarksToolbar")) {
trFolder = tr("Bookmarks In ToolBar");
}
else if (name == QLatin1String("unsorted")) {
trFolder = tr("Unsorted Bookmarks");
}
else {
trFolder = name;
}
return trFolder;
}
QString Bookmarks::fromTranslatedFolder(const QString &name)
{
QString folder;
if (name == tr("Bookmarks In Menu")) {
folder = "bookmarksMenu";
}
else if (name == tr("Bookmarks In ToolBar")) {
folder = "bookmarksToolbar";
}
else if (name == tr("Unsorted Bookmarks")) {
folder = "unsorted";
}
else {
folder = name;
}
return folder;
}
BookmarksModel* Bookmarks::model() const
{
return m_model;
}
BookmarkItem* Bookmarks::rootItem() const
{
return m_root;
}
BookmarkItem* Bookmarks::toolbarFolder() const
{
return m_folderToolbar;
}
BookmarkItem* Bookmarks::menuFolder() const
{
return m_folderMenu;
}
BookmarkItem* Bookmarks::unsortedFolder() const
{
return m_folderUnsorted;
}
BookmarkItem* Bookmarks::lastUsedFolder() const
{
return m_lastFolder;
}
bool Bookmarks::isBookmarked(const QUrl &url)
{
return !searchBookmarks(url).isEmpty();
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
{
QList<BookmarkItem*> items;
search(&items, m_root, url);
return items;
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const
{
QList<BookmarkItem*> items;
search(&items, m_root, string, sensitive);
return items;
}
bool Bookmarks::removeBookmark(BookmarkItem* item)
{
if (!canBeModified(item)) {
return false;
}
m_model->removeBookmark(item);
emit bookmarkRemoved(item);
return true;
}
void Bookmarks::notifyBookmarkChanged(BookmarkItem* item)
{
Q_ASSERT(item);
emit bookmarkChanged(item);
}
bool Bookmarks::canBeModified(BookmarkItem* item) const
{
Q_ASSERT(item);
return item != m_root &&
item != m_folderToolbar &&
item != m_folderMenu &&
item != m_folderUnsorted;
}
void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->isFolder());
Q_ASSERT(item);
insertBookmark(parent, 0, item);
}
void Bookmarks::insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->isFolder());
Q_ASSERT(item);
m_lastFolder = parent;
m_model->addBookmark(parent, row, item);
emit bookmarkAdded(item);
}

View File

@ -18,20 +18,13 @@
#ifndef BOOKMARKS_H #ifndef BOOKMARKS_H
#define BOOKMARKS_H #define BOOKMARKS_H
#define _bookmarksToolbar Bookmarks::toTranslatedFolder("bookmarksToolbar")
#define _bookmarksMenu Bookmarks::toTranslatedFolder("bookmarksMenu")
#define _bookmarksUnsorted Bookmarks::toTranslatedFolder("unsorted")
#include <QObject> #include <QObject>
#include <QUrl>
#include <QImage>
#include <QVariant> #include <QVariant>
#include "qz_namespace.h" #include "qz_namespace.h"
class QIcon; class QUrl;
class WebView;
class BookmarkItem; class BookmarkItem;
class BookmarksModel; class BookmarksModel;
@ -40,63 +33,39 @@ class QT_QUPZILLA_EXPORT Bookmarks : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit Bookmarks(QObject* parent = 0); explicit Bookmarks(QObject* parent = 0);
~Bookmarks();
struct Bookmark {
int id;
QString title;
QString folder;
QUrl url;
QImage image;
bool inSubfolder;
Bookmark() : id(-1) , inSubfolder(false) { }
bool operator==(const Bookmark &other) const {
return (this->title == other.title &&
this->folder == other.folder &&
this->url == other.url &&
this->inSubfolder == other.inSubfolder);
}
};
void loadSettings(); void loadSettings();
void saveBookmarks(); void saveSettings();
bool isShowingMostVisited() const; bool showOnlyIconsInToolbar() const;
void setShowingMostVisited(bool state);
bool isShowingOnlyIconsInToolbar() const;
void setShowingOnlyIconsInToolbar(bool state);
void setLastFolder(const QString &folder);
void exportToHtml(const QString &fileName); void exportToHtml(const QString &fileName);
static QString toTranslatedFolder(const QString &name);
static QString fromTranslatedFolder(const QString &name);
BookmarkItem* rootItem() const; BookmarkItem* rootItem() const;
BookmarkItem* toolbarFolder() const; BookmarkItem* toolbarFolder() const;
BookmarkItem* menuFolder() const; BookmarkItem* menuFolder() const;
BookmarkItem* unsortedFolder() const; BookmarkItem* unsortedFolder() const;
BookmarkItem* lastUsedFolder() const; BookmarkItem* lastUsedFolder() const;
BookmarksModel* model() const; BookmarksModel* model() const;
bool isBookmarked(const QUrl &url); bool isBookmarked(const QUrl &url);
bool canBeModified(BookmarkItem* item) const;
// Search bookmarks (urls only) for exact url match // Search bookmarks (urls only) for exact url match
QList<BookmarkItem*> searchBookmarks(const QUrl &url) const; QList<BookmarkItem*> searchBookmarks(const QUrl &url) const;
// Search bookmarks (urls only) for contains match through all properties // Search bookmarks (urls only) for contains match through all properties
QList<BookmarkItem*> searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const; QList<BookmarkItem*> searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const;
bool canBeModified(BookmarkItem* item) const;
void addBookmark(BookmarkItem* parent, BookmarkItem* item); void addBookmark(BookmarkItem* parent, BookmarkItem* item);
void insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item); void insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item);
bool removeBookmark(BookmarkItem* item); bool removeBookmark(BookmarkItem* item);
void notifyBookmarkChanged(BookmarkItem* item); void notifyBookmarkChanged(BookmarkItem* item);
public slots:
void setShowOnlyIconsInToolbar(bool state);
signals: signals:
// Item was added to bookmarks // Item was added to bookmarks
void bookmarkAdded(BookmarkItem* item); void bookmarkAdded(BookmarkItem* item);
@ -105,8 +74,11 @@ signals:
// Item data has changed // Item data has changed
void bookmarkChanged(BookmarkItem* item); void bookmarkChanged(BookmarkItem* item);
void showOnlyIconsInToolbarChanged(bool show);
private: private:
void init(); void init();
void saveBookmarks();
void loadBookmarksFromMap(const QVariantMap &map); void loadBookmarksFromMap(const QVariantMap &map);
void readBookmarks(const QVariantList &list, BookmarkItem* parent); void readBookmarks(const QVariantList &list, BookmarkItem* parent);
@ -123,15 +95,7 @@ private:
BookmarkItem* m_lastFolder; BookmarkItem* m_lastFolder;
BookmarksModel* m_model; BookmarksModel* m_model;
bool m_showMostVisited;
bool m_showOnlyIconsInToolbar; bool m_showOnlyIconsInToolbar;
}; };
typedef Bookmarks::Bookmark Bookmark;
// Hint to QVector to use std::realloc on item moving
Q_DECLARE_TYPEINFO(Bookmark, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(Bookmark)
#endif // BOOKMARKS_H #endif // BOOKMARKS_H

View File

@ -70,8 +70,8 @@ void BookmarksToolbar::contextMenuRequested(const QPoint &pos)
menu.addSeparator(); menu.addSeparator();
QAction* act = menu.addAction(tr("Show Only Icons")); QAction* act = menu.addAction(tr("Show Only Icons"));
act->setCheckable(true); act->setCheckable(true);
act->setChecked(m_bookmarks->isShowingOnlyIconsInToolbar()); act->setChecked(m_bookmarks->showOnlyIconsInToolbar());
connect(act, SIGNAL(toggled(bool)), this, SLOT(setShowOnlyIcons(bool))); connect(act, SIGNAL(toggled(bool)), m_bookmarks, SLOT(setShowOnlyIconsInToolbar(bool)));
connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab())); connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab()));
connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow())); connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow()));
@ -86,19 +86,6 @@ void BookmarksToolbar::contextMenuRequested(const QPoint &pos)
m_clickedBookmark = 0; m_clickedBookmark = 0;
} }
void BookmarksToolbar::setShowOnlyIcons(bool show)
{
m_bookmarks->setShowingOnlyIconsInToolbar(show);
for (int i = 0; i < m_layout->count(); ++i) {
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
if (b) {
b->setShowOnlyIcon(show);
}
}
}
void BookmarksToolbar::addItem(BookmarkItem* item) void BookmarksToolbar::addItem(BookmarkItem* item)
{ {
Q_ASSERT(item); Q_ASSERT(item);
@ -108,7 +95,7 @@ void BookmarksToolbar::addItem(BookmarkItem* item)
case BookmarkItem::Url: { case BookmarkItem::Url: {
BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this); BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this);
button->setMainWindow(m_window); button->setMainWindow(m_window);
button->setShowOnlyIcon(m_bookmarks->isShowingOnlyIconsInToolbar()); button->setShowOnlyIcon(m_bookmarks->showOnlyIconsInToolbar());
m_layout->addWidget(button); m_layout->addWidget(button);
break; break;
} }
@ -157,6 +144,17 @@ void BookmarksToolbar::bookmarksChanged()
m_updateTimer->start(); m_updateTimer->start();
} }
void BookmarksToolbar::showOnlyIconsChanged(bool state)
{
for (int i = 0; i < m_layout->count(); ++i) {
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
if (b) {
b->setShowOnlyIcon(state);
}
}
}
void BookmarksToolbar::openBookmarkInNewTab() void BookmarksToolbar::openBookmarkInNewTab()
{ {
if (m_clickedBookmark) { if (m_clickedBookmark) {

View File

@ -37,10 +37,10 @@ public:
private slots: private slots:
void contextMenuRequested(const QPoint &pos); void contextMenuRequested(const QPoint &pos);
void setShowOnlyIcons(bool show);
void refresh(); void refresh();
void bookmarksChanged(); void bookmarksChanged();
void showOnlyIconsChanged(bool state);
void openBookmarkInNewTab(); void openBookmarkInNewTab();
void openBookmarkInNewWindow(); void openBookmarkInNewWindow();

View File

@ -337,6 +337,8 @@ void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* b
void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator) void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator)
{ {
Q_UNUSED(separator)
Q_ASSERT(menu); Q_ASSERT(menu);
Q_ASSERT(separator->isSeparator()); Q_ASSERT(separator->isSeparator());