2014-02-08 23:01:01 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "bookmarkstools.h"
|
|
|
|
#include "bookmarkitem.h"
|
|
|
|
#include "bookmarks.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "iconprovider.h"
|
2014-02-10 11:54:58 +01:00
|
|
|
#include "enhancedmenu.h"
|
2014-02-08 23:01:01 +01:00
|
|
|
#include "tabwidget.h"
|
2014-02-09 11:02:51 +01:00
|
|
|
#include "qzsettings.h"
|
|
|
|
#include "qupzilla.h"
|
2014-02-08 23:01:01 +01:00
|
|
|
|
2014-02-10 16:58:05 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <QSqlQuery>
|
2014-02-08 23:01:01 +01:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QDialog>
|
|
|
|
|
|
|
|
// BookmarksFoldersMenu
|
|
|
|
BookmarksFoldersMenu::BookmarksFoldersMenu(QWidget* parent)
|
|
|
|
: QMenu(parent)
|
|
|
|
, m_selectedFolder(0)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* BookmarksFoldersMenu::selectedFolder() const
|
|
|
|
{
|
|
|
|
return m_selectedFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksFoldersMenu::folderChoosed()
|
|
|
|
{
|
|
|
|
if (QAction* act = qobject_cast<QAction*>(sender())) {
|
|
|
|
BookmarkItem* folder = static_cast<BookmarkItem*>(act->data().value<void*>());
|
|
|
|
emit folderSelected(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksFoldersMenu::init()
|
|
|
|
{
|
|
|
|
#define ADD_MENU(name) \
|
|
|
|
BookmarkItem* f_##name = mApp->bookmarks()->name(); \
|
2014-02-10 16:03:29 +01:00
|
|
|
QMenu* m_##name = addMenu(f_##name->icon(), f_##name->title()); \
|
2014-02-08 23:01:01 +01:00
|
|
|
createMenu(m_##name, f_##name);
|
|
|
|
|
|
|
|
ADD_MENU(toolbarFolder)
|
|
|
|
ADD_MENU(menuFolder)
|
|
|
|
ADD_MENU(unsortedFolder)
|
|
|
|
#undef ADD_MENU
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksFoldersMenu::createMenu(QMenu* menu, BookmarkItem* parent)
|
|
|
|
{
|
|
|
|
QAction* act = menu->addAction(tr("Choose %1").arg(parent->title()));
|
|
|
|
act->setData(QVariant::fromValue<void*>(static_cast<void*>(parent)));
|
|
|
|
connect(act, SIGNAL(triggered()), this, SLOT(folderChoosed()));
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
|
|
|
foreach (BookmarkItem* child, parent->children()) {
|
|
|
|
if (child->isFolder()) {
|
2014-02-10 16:03:29 +01:00
|
|
|
QMenu* m = menu->addMenu(child->icon(), child->title());
|
2014-02-08 23:01:01 +01:00
|
|
|
createMenu(m, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarksFoldersButton
|
|
|
|
BookmarksFoldersButton::BookmarksFoldersButton(QWidget* parent, BookmarkItem* folder)
|
|
|
|
: QPushButton(parent)
|
|
|
|
, m_menu(new BookmarksFoldersMenu(this))
|
2014-02-09 12:43:06 +01:00
|
|
|
, m_selectedFolder(folder ? folder : mApp->bookmarks()->lastUsedFolder())
|
2014-02-08 23:01:01 +01:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
connect(m_menu, SIGNAL(folderSelected(BookmarkItem*)), this, SLOT(setSelectedFolder(BookmarkItem*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* BookmarksFoldersButton::selectedFolder() const
|
|
|
|
{
|
|
|
|
return m_selectedFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksFoldersButton::setSelectedFolder(BookmarkItem* folder)
|
|
|
|
{
|
|
|
|
Q_ASSERT(folder);
|
|
|
|
Q_ASSERT(folder->isFolder());
|
|
|
|
|
|
|
|
m_selectedFolder = folder;
|
|
|
|
setText(folder->title());
|
2014-02-10 16:03:29 +01:00
|
|
|
setIcon(folder->icon());
|
2014-02-09 12:18:52 +01:00
|
|
|
|
|
|
|
if (sender()) {
|
|
|
|
emit selectedFolderChanged(folder);
|
|
|
|
}
|
2014-02-08 23:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksFoldersButton::init()
|
|
|
|
{
|
|
|
|
setMenu(m_menu);
|
|
|
|
setSelectedFolder(m_selectedFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarksTools
|
|
|
|
bool BookmarksTools::addBookmarkDialog(QWidget* parent, const QUrl &url, const QString &title, BookmarkItem* folder)
|
|
|
|
{
|
|
|
|
if (url.isEmpty() || title.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog* dialog = new QDialog(parent);
|
|
|
|
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, dialog);
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
QLineEdit* edit = new QLineEdit(dialog);
|
|
|
|
BookmarksFoldersButton* folderButton = new BookmarksFoldersButton(dialog, folder);
|
|
|
|
|
|
|
|
QDialogButtonBox* box = new QDialogButtonBox(dialog);
|
|
|
|
box->addButton(QDialogButtonBox::Ok);
|
|
|
|
box->addButton(QDialogButtonBox::Cancel);
|
|
|
|
QObject::connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
|
|
|
|
QObject::connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
|
|
|
|
|
|
|
|
layout->addWidget(label);
|
|
|
|
layout->addWidget(edit);
|
|
|
|
layout->addWidget(folderButton);
|
|
|
|
layout->addWidget(box);
|
|
|
|
|
|
|
|
label->setText(Bookmarks::tr("Choose name and location of this bookmark."));
|
|
|
|
edit->setText(title);
|
|
|
|
edit->setCursorPosition(0);
|
|
|
|
dialog->setWindowIcon(_iconForUrl(url));
|
|
|
|
dialog->setWindowTitle(Bookmarks::tr("Add New Bookmark"));
|
|
|
|
|
|
|
|
QSize size = dialog->size();
|
|
|
|
size.setWidth(350);
|
|
|
|
dialog->resize(size);
|
|
|
|
dialog->exec();
|
|
|
|
|
|
|
|
if (dialog->result() == QDialog::Rejected || edit->text().isEmpty()) {
|
|
|
|
delete dialog;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url);
|
|
|
|
bookmark->setTitle(edit->text());
|
|
|
|
bookmark->setUrl(url);
|
|
|
|
mApp->bookmarks()->addBookmark(folderButton->selectedFolder(), bookmark);
|
|
|
|
|
|
|
|
delete dialog;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarksTools::bookmarkAllTabsDialog(QWidget* parent, TabWidget* tabWidget, BookmarkItem* folder)
|
|
|
|
{
|
|
|
|
Q_ASSERT(tabWidget);
|
|
|
|
|
|
|
|
QDialog* dialog = new QDialog(parent);
|
|
|
|
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, dialog);
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
BookmarksFoldersButton* folderButton = new BookmarksFoldersButton(dialog, folder);
|
|
|
|
|
|
|
|
QDialogButtonBox* box = new QDialogButtonBox(dialog);
|
|
|
|
box->addButton(QDialogButtonBox::Ok);
|
|
|
|
box->addButton(QDialogButtonBox::Cancel);
|
|
|
|
QObject::connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
|
|
|
|
QObject::connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
|
|
|
|
|
|
|
|
layout->addWidget(label);
|
|
|
|
layout->addWidget(folderButton);
|
|
|
|
layout->addWidget(box);
|
|
|
|
|
|
|
|
label->setText(Bookmarks::tr("Choose folder for bookmarks:"));
|
|
|
|
dialog->setWindowTitle(Bookmarks::tr("Bookmark All Tabs"));
|
|
|
|
|
|
|
|
QSize size = dialog->size();
|
|
|
|
size.setWidth(350);
|
|
|
|
dialog->resize(size);
|
|
|
|
dialog->exec();
|
|
|
|
|
|
|
|
if (dialog->result() == QDialog::Rejected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (WebTab* tab, tabWidget->allTabs(false)) {
|
|
|
|
if (!tab->url().isEmpty()) {
|
|
|
|
BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url);
|
|
|
|
bookmark->setTitle(tab->title());
|
|
|
|
bookmark->setUrl(tab->url());
|
|
|
|
mApp->bookmarks()->addBookmark(folderButton->selectedFolder(), bookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete dialog;
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-09 11:02:51 +01:00
|
|
|
|
|
|
|
void BookmarksTools::openBookmark(QupZilla* window, BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(window);
|
|
|
|
|
|
|
|
if (!item || !item->isUrl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 15:07:19 +01:00
|
|
|
if (item->isFolder()) {
|
|
|
|
openFolderInTabs(window, item);
|
|
|
|
}
|
|
|
|
else if (item->isUrl()) {
|
|
|
|
item->setVisitCount(item->visitCount() + 1);
|
|
|
|
window->loadAddress(item->url());
|
|
|
|
}
|
2014-02-09 11:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTools::openBookmarkInNewTab(QupZilla* window, BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(window);
|
|
|
|
|
2014-02-09 15:07:19 +01:00
|
|
|
if (!item) {
|
2014-02-09 11:02:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 15:07:19 +01:00
|
|
|
if (item->isFolder()) {
|
|
|
|
openFolderInTabs(window, item);
|
|
|
|
}
|
|
|
|
else if (item->isUrl()) {
|
|
|
|
item->setVisitCount(item->visitCount() + 1);
|
|
|
|
window->tabWidget()->addView(item->url(), item->title(), qzSettings->newTabPosition);
|
|
|
|
}
|
2014-02-09 11:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTools::openBookmarkInNewWindow(BookmarkItem* item)
|
|
|
|
{
|
2014-02-09 17:27:55 +01:00
|
|
|
if (!item->isUrl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setVisitCount(item->visitCount() + 1);
|
2014-02-09 11:02:51 +01:00
|
|
|
mApp->makeNewWindow(Qz::BW_NewWindow, item->url());
|
|
|
|
}
|
2014-02-09 15:07:19 +01:00
|
|
|
|
|
|
|
void BookmarksTools::openFolderInTabs(QupZilla* window, BookmarkItem* folder)
|
|
|
|
{
|
|
|
|
Q_ASSERT(window);
|
|
|
|
Q_ASSERT(folder->isFolder());
|
|
|
|
|
|
|
|
foreach (BookmarkItem* child, folder->children()) {
|
|
|
|
if (child->isUrl()) {
|
|
|
|
openBookmarkInNewTab(window, child);
|
|
|
|
}
|
|
|
|
else if (child->isFolder()) {
|
|
|
|
openFolderInTabs(window, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 11:54:58 +01:00
|
|
|
|
|
|
|
void BookmarksTools::addActionToMenu(QObject* receiver, Menu* menu, BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(menu);
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
switch (item->type()) {
|
|
|
|
case BookmarkItem::Url:
|
|
|
|
addUrlToMenu(receiver, menu, item);
|
|
|
|
break;
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
addFolderToMenu(receiver, menu, item);
|
|
|
|
break;
|
|
|
|
case BookmarkItem::Separator:
|
|
|
|
addSeparatorToMenu(menu, item);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTools::addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder)
|
|
|
|
{
|
|
|
|
Q_ASSERT(menu);
|
|
|
|
Q_ASSERT(folder);
|
|
|
|
Q_ASSERT(folder->isFolder());
|
|
|
|
|
|
|
|
Menu* m = new Menu(folder->title());
|
2014-02-10 16:03:29 +01:00
|
|
|
m->setIcon(folder->icon());
|
2014-02-10 11:54:58 +01:00
|
|
|
QObject::connect(m, SIGNAL(menuMiddleClicked(Menu*)), receiver, SLOT(menuMiddleClicked(Menu*)));
|
|
|
|
|
|
|
|
QAction* act = menu->addMenu(m);
|
|
|
|
act->setData(QVariant::fromValue<void*>(static_cast<void*>(folder)));
|
|
|
|
|
|
|
|
foreach (BookmarkItem* child, folder->children()) {
|
|
|
|
addActionToMenu(receiver, m, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->isEmpty()) {
|
|
|
|
m->addAction(Bookmarks::tr("Empty"))->setDisabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark)
|
|
|
|
{
|
|
|
|
Q_ASSERT(menu);
|
|
|
|
Q_ASSERT(bookmark);
|
|
|
|
Q_ASSERT(bookmark->isUrl());
|
|
|
|
|
2014-02-10 16:03:29 +01:00
|
|
|
Action* act = new Action(bookmark->icon(), bookmark->title());
|
2014-02-10 11:54:58 +01:00
|
|
|
act->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)));
|
|
|
|
|
|
|
|
QObject::connect(act, SIGNAL(triggered()), receiver, SLOT(bookmarkActivated()));
|
|
|
|
QObject::connect(act, SIGNAL(ctrlTriggered()), receiver, SLOT(bookmarkCtrlActivated()));
|
|
|
|
QObject::connect(act, SIGNAL(shiftTriggered()), receiver, SLOT(bookmarkShiftActivated()));
|
|
|
|
|
|
|
|
menu->addAction(act);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator)
|
|
|
|
{
|
2014-02-10 12:58:34 +01:00
|
|
|
Q_UNUSED(separator)
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
Q_ASSERT(menu);
|
|
|
|
Q_ASSERT(separator->isSeparator());
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
}
|
2014-02-10 16:58:05 +01:00
|
|
|
|
|
|
|
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<QString, BookmarkItem*> 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");
|
|
|
|
|
2014-02-10 21:12:08 +01:00
|
|
|
BookmarkItem* parent = subfolder ? bookmarks->toolbarFolder() : bookmarks->unsortedFolder();
|
|
|
|
BookmarkItem* folder = new BookmarkItem(BookmarkItem::Folder, parent);
|
2014-02-10 16:58:05 +01:00
|
|
|
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;
|
|
|
|
}
|