2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "bookmarksmanager.h"
|
|
|
|
#include "ui_bookmarksmanager.h"
|
2014-02-05 15:26:51 +01:00
|
|
|
#include "bookmarks.h"
|
2014-02-08 18:14:38 +01:00
|
|
|
#include "bookmarkitem.h"
|
2014-02-08 23:01:01 +01:00
|
|
|
#include "bookmarksmodel.h"
|
|
|
|
#include "bookmarkstools.h"
|
2014-02-09 11:02:51 +01:00
|
|
|
#include "bookmarksimportdialog.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "qztools.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
2014-02-08 18:14:38 +01:00
|
|
|
|
2011-10-28 17:52:42 +02:00
|
|
|
BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::BookmarksManager)
|
|
|
|
, p_QupZilla(mainClass)
|
2014-02-05 15:26:51 +01:00
|
|
|
, m_bookmarks(mApp->bookmarks())
|
2014-02-08 20:01:07 +01:00
|
|
|
, m_selectedBookmark(0)
|
2014-02-08 18:14:38 +01:00
|
|
|
, m_blockDescriptionChangedSignal(false)
|
2014-02-08 20:01:07 +01:00
|
|
|
, m_adjustHeaderSizesOnShow(true)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-02-09 11:02:51 +01:00
|
|
|
ui->tree->setViewType(BookmarksTreeView::BookmarksManagerViewType);
|
2014-02-08 18:14:38 +01:00
|
|
|
|
|
|
|
connect(ui->tree, SIGNAL(bookmarkActivated(BookmarkItem*)), this, SLOT(bookmarkActivated(BookmarkItem*)));
|
|
|
|
connect(ui->tree, SIGNAL(bookmarkCtrlActivated(BookmarkItem*)), this, SLOT(bookmarkCtrlActivated(BookmarkItem*)));
|
|
|
|
connect(ui->tree, SIGNAL(bookmarkShiftActivated(BookmarkItem*)), this, SLOT(bookmarkShiftActivated(BookmarkItem*)));
|
|
|
|
connect(ui->tree, SIGNAL(bookmarksSelected(QList<BookmarkItem*>)), this, SLOT(bookmarksSelected(QList<BookmarkItem*>)));
|
2014-02-08 20:01:07 +01:00
|
|
|
connect(ui->tree, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(createContextMenu(QPoint)));
|
2014-02-08 18:14:38 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
// Box for editing bookmarks
|
2014-02-08 18:14:38 +01:00
|
|
|
updateEditBox(0);
|
|
|
|
connect(ui->title, SIGNAL(textEdited(QString)), this, SLOT(bookmarkEdited()));
|
|
|
|
connect(ui->address, SIGNAL(textEdited(QString)), this, SLOT(bookmarkEdited()));
|
|
|
|
connect(ui->keyword, SIGNAL(textEdited(QString)), this, SLOT(bookmarkEdited()));
|
|
|
|
connect(ui->description, SIGNAL(textChanged()), this, SLOT(descriptionEdited()));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
BookmarksManager::~BookmarksManager()
|
2011-12-12 21:14:43 +01:00
|
|
|
{
|
2014-02-08 18:14:38 +01:00
|
|
|
delete ui;
|
2011-12-12 21:14:43 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 23:01:01 +01:00
|
|
|
void BookmarksManager::setMainWindow(QupZilla* window)
|
|
|
|
{
|
|
|
|
if (window) {
|
|
|
|
p_QupZilla = window;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::search(const QString &string)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
ui->tree->search(string);
|
2014-02-08 23:01:01 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
void BookmarksManager::bookmarkActivated(BookmarkItem* item)
|
2013-12-30 00:35:01 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
openBookmark(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::bookmarkCtrlActivated(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
openBookmarkInNewTab(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::bookmarkShiftActivated(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
openBookmarkInNewWindow(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::bookmarksSelected(const QList<BookmarkItem*> &items)
|
|
|
|
{
|
|
|
|
if (items.size() != 1) {
|
|
|
|
m_selectedBookmark = 0;
|
|
|
|
updateEditBox(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_selectedBookmark = items.first();
|
|
|
|
updateEditBox(m_selectedBookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::createContextMenu(const QPoint &pos)
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
QAction* actNewTab = menu.addAction(QIcon::fromTheme("tab-new", QIcon(":/icons/menu/tab-new.png")), tr("Open in new tab"));
|
|
|
|
QAction* actNewWindow = menu.addAction(QIcon::fromTheme("window-new"), tr("Open in new window"));
|
|
|
|
menu.addSeparator();
|
|
|
|
menu.addAction(tr("New Bookmark"), this, SLOT(addBookmark()));
|
|
|
|
menu.addAction(tr("New Folder"), this, SLOT(addFolder()));
|
|
|
|
menu.addAction(tr("New Separator"), this, SLOT(addSeparator()));
|
|
|
|
menu.addSeparator();
|
|
|
|
QAction* actDelete = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
|
|
|
|
|
|
|
|
connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab()));
|
|
|
|
connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow()));
|
|
|
|
connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteBookmarks()));
|
|
|
|
|
|
|
|
bool canBeDeleted = false;
|
|
|
|
QList<BookmarkItem*> items = ui->tree->selectedBookmarks();
|
|
|
|
|
|
|
|
foreach (BookmarkItem* item, items) {
|
|
|
|
if (m_bookmarks->canBeModified(item)) {
|
|
|
|
canBeDeleted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canBeDeleted) {
|
|
|
|
actDelete->setDisabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_selectedBookmark || !m_selectedBookmark->isUrl()) {
|
|
|
|
actNewTab->setDisabled(true);
|
|
|
|
actNewWindow->setDisabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.exec(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::openBookmark(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
item = item ? item : m_selectedBookmark;
|
2014-02-09 11:02:51 +01:00
|
|
|
BookmarksTools::openBookmark(getQupZilla(), item);
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::openBookmarkInNewTab(BookmarkItem* item)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
item = item ? item : m_selectedBookmark;
|
2014-02-09 11:02:51 +01:00
|
|
|
BookmarksTools::openBookmarkInNewTab(getQupZilla(), item);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::openBookmarkInNewWindow(BookmarkItem* item)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
item = item ? item : m_selectedBookmark;
|
2014-02-09 11:02:51 +01:00
|
|
|
BookmarksTools::openBookmarkInNewWindow(item);
|
2014-02-08 18:14:38 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::addBookmark()
|
2014-02-08 18:14:38 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
BookmarkItem* item = new BookmarkItem(BookmarkItem::Url);
|
|
|
|
item->setTitle(tr("New Bookmark"));
|
|
|
|
item->setUrl(QUrl("http://"));
|
|
|
|
addBookmark(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::addFolder()
|
|
|
|
{
|
|
|
|
BookmarkItem* item = new BookmarkItem(BookmarkItem::Folder);
|
|
|
|
item->setTitle(tr("New Folder"));
|
|
|
|
addBookmark(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::addSeparator()
|
|
|
|
{
|
|
|
|
BookmarkItem* item = new BookmarkItem(BookmarkItem::Separator);
|
|
|
|
addBookmark(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::deleteBookmarks()
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items = ui->tree->selectedBookmarks();
|
|
|
|
|
|
|
|
foreach (BookmarkItem* item, items) {
|
|
|
|
if (m_bookmarks->canBeModified(item)) {
|
|
|
|
m_bookmarks->removeBookmark(item);
|
|
|
|
}
|
2013-01-09 13:59:37 +01:00
|
|
|
}
|
2014-02-08 18:14:38 +01:00
|
|
|
}
|
2013-01-09 13:59:37 +01:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
void BookmarksManager::bookmarkEdited()
|
|
|
|
{
|
|
|
|
Q_ASSERT(ui->tree->selectedBookmarks().count() == 1);
|
2013-01-09 13:59:37 +01:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
BookmarkItem* item = ui->tree->selectedBookmarks().first();
|
|
|
|
item->setTitle(ui->title->text());
|
|
|
|
item->setUrl(QUrl::fromEncoded(ui->address->text().toUtf8()));
|
|
|
|
item->setKeyword(ui->keyword->text());
|
|
|
|
item->setDescription(ui->description->toPlainText());
|
|
|
|
|
|
|
|
m_bookmarks->notifyBookmarkChanged(item);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
void BookmarksManager::descriptionEdited()
|
2011-10-28 17:52:42 +02:00
|
|
|
{
|
2014-02-08 18:14:38 +01:00
|
|
|
// There is no textEdited() signal in QPlainTextEdit
|
|
|
|
// textChanged() is emitted also when text is changed programatically
|
|
|
|
if (!m_blockDescriptionChangedSignal) {
|
|
|
|
bookmarkEdited();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-02-08 18:14:38 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
void BookmarksManager::importBookmarks()
|
|
|
|
{
|
|
|
|
BookmarksImportDialog* b = new BookmarksImportDialog(this);
|
|
|
|
b->show();
|
2011-10-28 17:52:42 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
void BookmarksManager::exportBookmarks()
|
2011-10-28 17:52:42 +02:00
|
|
|
{
|
2014-02-08 18:14:38 +01:00
|
|
|
QString file = QzTools::getSaveFileName("BookmarksManager-Export", this, tr("Export to HTML..."), QDir::homePath() + "/bookmarks.html");
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
if (!file.isEmpty()) {
|
|
|
|
m_bookmarks->exportToHtml(file);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-02-08 18:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksManager::updateEditBox(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
setUpdatesEnabled(false);
|
|
|
|
m_blockDescriptionChangedSignal = true;
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
bool editable = bookmarkEditable(item);
|
2014-02-08 18:28:01 +01:00
|
|
|
bool showAddressAndKeyword = item && item->isUrl();
|
2014-02-08 18:14:38 +01:00
|
|
|
bool clearBox = !item;
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
if (clearBox) {
|
|
|
|
ui->title->clear();
|
|
|
|
ui->address->clear();
|
|
|
|
ui->keyword->clear();
|
|
|
|
ui->description->clear();
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
ui->title->setReadOnly(true);
|
|
|
|
ui->address->setReadOnly(true);
|
|
|
|
ui->keyword->setReadOnly(true);
|
|
|
|
ui->description->setReadOnly(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui->title->setText(item->title());
|
|
|
|
ui->address->setText(item->url().toEncoded());
|
|
|
|
ui->keyword->setText(item->keyword());
|
|
|
|
ui->description->setPlainText(item->description());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
ui->title->setReadOnly(!editable);
|
|
|
|
ui->address->setReadOnly(!editable);
|
|
|
|
ui->keyword->setReadOnly(!editable);
|
|
|
|
ui->description->setReadOnly(!editable);
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
ui->labelAddress->setVisible(showAddressAndKeyword);
|
|
|
|
ui->address->setVisible(showAddressAndKeyword);
|
|
|
|
ui->labelKeyword->setVisible(showAddressAndKeyword);
|
|
|
|
ui->keyword->setVisible(showAddressAndKeyword);
|
2011-12-12 21:14:43 +01:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
// Without removing widgets from layout, there is unwanted extra spacing
|
|
|
|
QFormLayout* l = static_cast<QFormLayout*>(ui->editBox->layout());
|
2011-12-12 21:14:43 +01:00
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
if (showAddressAndKeyword) {
|
|
|
|
// Show Address + Keyword
|
|
|
|
l->insertRow(1, ui->labelAddress, ui->address);
|
|
|
|
l->insertRow(2, ui->labelKeyword, ui->keyword);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Hide Address + Keyword
|
|
|
|
l->removeWidget(ui->labelAddress);
|
|
|
|
l->removeWidget(ui->labelKeyword);
|
|
|
|
l->removeWidget(ui->address);
|
|
|
|
l->removeWidget(ui->keyword);
|
2012-05-22 11:46:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:38 +01:00
|
|
|
m_blockDescriptionChangedSignal = false;
|
|
|
|
setUpdatesEnabled(true);
|
|
|
|
}
|
2012-05-22 11:46:22 +02:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
bool BookmarksManager::bookmarkEditable(BookmarkItem* item) const
|
2014-02-08 18:14:38 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
return item && (item->isFolder() || item->isUrl()) && m_bookmarks->canBeModified(item);
|
2012-05-22 11:46:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::addBookmark(BookmarkItem* item)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
BookmarkItem* parent = parentForNewBookmark();
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
|
|
|
|
m_bookmarks->addBookmark(parent, item);
|
2014-02-09 15:07:19 +01:00
|
|
|
|
|
|
|
ui->tree->selectBookmark(item);
|
|
|
|
ui->tree->ensureBookmarkVisible(item);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
BookmarkItem* BookmarksManager::parentForNewBookmark() const
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
if (m_selectedBookmark && m_selectedBookmark->isFolder()) {
|
|
|
|
return m_selectedBookmark;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-05-22 11:46:22 +02:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (!m_selectedBookmark || m_selectedBookmark->parent() == m_bookmarks->rootItem()) {
|
|
|
|
return m_bookmarks->unsortedFolder();
|
|
|
|
}
|
2011-12-12 21:14:43 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
return m_selectedBookmark->parent();
|
|
|
|
}
|
2011-12-12 21:14:43 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
deleteBookmarks();
|
|
|
|
break;
|
|
|
|
}
|
2011-12-12 21:14:43 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
QupZilla* BookmarksManager::getQupZilla()
|
|
|
|
{
|
|
|
|
if (!p_QupZilla) {
|
|
|
|
p_QupZilla = mApp->getWindow();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-02-08 20:01:07 +01:00
|
|
|
return p_QupZilla.data();
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
void BookmarksManager::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
QWidget::showEvent(event);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (m_adjustHeaderSizesOnShow) {
|
|
|
|
ui->tree->header()->resizeSection(0, ui->tree->header()->width() / 1.9);
|
|
|
|
m_adjustHeaderSizesOnShow = false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2014-02-08 20:01:07 +01:00
|
|
|
}
|