Falkon Develop
Cross-platform Qt-based web browser
bookmarkswidget.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "bookmarkswidget.h"
19#include "ui_bookmarkswidget.h"
20#include "bookmarks.h"
21#include "bookmarkitem.h"
22#include "mainapplication.h"
23#include "pluginproxy.h"
24#include "speeddial.h"
25#include "webview.h"
26#include "browserwindow.h"
27
28#include <QTimer>
29
30#define HIDE_DELAY 270
31
32BookmarksWidget::BookmarksWidget(WebView* view, BookmarkItem* bookmark, QWidget* parent)
33 : LocationBarPopup(parent)
34 , ui(new Ui::BookmarksWidget)
35 , m_view(view)
36 , m_bookmark(bookmark)
37 , m_bookmarks(mApp->bookmarks())
38 , m_speedDial(mApp->plugins()->speedDial())
39 , m_edited(false)
40{
41 ui->setupUi(this);
42 ui->bookmarksButton->setIcon(QIcon::fromTheme(QSL("bookmark-new")));
43
44 init();
45}
46
48{
49 delete ui;
50}
51
52void BookmarksWidget::toggleSpeedDial()
53{
54 const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
55
56 if (page.url.isEmpty()) {
57 QString title = m_view->title();
58 m_speedDial->addPage(m_view->url(), title);
59 }
60 else {
61 m_speedDial->removePage(page);
62 }
63
64 closePopup();
65}
66
67void BookmarksWidget::toggleBookmark()
68{
69 if (m_bookmark) {
70 if (m_edited) {
71 // Change folder
72 m_bookmarks->removeBookmark(m_bookmark);
73 m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), m_bookmark);
74 }
75 else {
76 // Remove
77 m_bookmarks->removeBookmark(m_bookmark);
78 }
79 }
80 else {
81 // Save bookmark
82 auto* bookmark = new BookmarkItem(BookmarkItem::Url);
83 bookmark->setTitle(m_view->title());
84 bookmark->setUrl(m_view->url());
85 m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), bookmark);
86 }
87
88 closePopup();
89}
90
91void BookmarksWidget::bookmarkEdited()
92{
93 if (m_edited) {
94 return;
95 }
96
97 m_edited = true;
98 ui->bookmarksButton->setText(tr("Update Bookmark"));
99 ui->bookmarksButton->setFlat(true);
100}
101
102void BookmarksWidget::init()
103{
104 // The locationbar's direction is direction of its text,
105 // it dynamically changes and so, it's not good choice for this widget.
106 setLayoutDirection(QApplication::layoutDirection());
107
108 // Init SpeedDial button
109 const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
110 if (page.url.isEmpty()) {
111 ui->speeddialButton->setFlat(true);
112 ui->speeddialButton->setText(tr("Add to Speed Dial"));
113 }
114 else {
115 ui->speeddialButton->setFlat(false);
116 ui->speeddialButton->setText(tr("Remove from Speed Dial"));
117 }
118
119 // Init Bookmarks button
120 if (m_bookmark) {
121 ui->bookmarksButton->setText(tr("Remove from Bookmarks"));
122 ui->bookmarksButton->setFlat(false);
123
124 Q_ASSERT(m_bookmark->parent());
125 ui->folderButton->setSelectedFolder(m_bookmark->parent());
126 connect(ui->folderButton, &BookmarksFoldersButton::selectedFolderChanged, this, &BookmarksWidget::bookmarkEdited);
127 }
128
129 connect(ui->speeddialButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleSpeedDial);
130 connect(ui->bookmarksButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleBookmark);
131
132}
133
134void BookmarksWidget::closePopup()
135{
136 // Prevent clicking again on buttons while popup is being closed
137 disconnect(ui->speeddialButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleSpeedDial);
138 disconnect(ui->bookmarksButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleBookmark);
139
140 QTimer::singleShot(HIDE_DELAY, this, &QWidget::close);
141}
142
#define HIDE_DELAY
BookmarkItem * parent() const
void selectedFolderChanged(BookmarkItem *folder)
void addBookmark(BookmarkItem *parent, BookmarkItem *item)
Definition: bookmarks.cpp:135
bool removeBookmark(BookmarkItem *item)
Definition: bookmarks.cpp:157
BookmarksWidget(WebView *view, BookmarkItem *bookmark, QWidget *parent=nullptr)
void removePage(const Page &page)
Definition: speeddial.cpp:144
Page pageForUrl(const QUrl &url)
Definition: speeddial.cpp:98
void addPage(const QUrl &url, const QString &title)
Definition: speeddial.cpp:126
QString title(bool allowEmpty=false) const
Definition: webview.cpp:107
#define mApp
#define QSL(x)
Definition: qzcommon.h:40
QString url
Definition: speeddial.h:39