File indexing completed on 2024-05-12 04:57:59

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "bookmarkswidget.h"
0019 #include "ui_bookmarkswidget.h"
0020 #include "bookmarks.h"
0021 #include "bookmarkitem.h"
0022 #include "mainapplication.h"
0023 #include "pluginproxy.h"
0024 #include "speeddial.h"
0025 #include "webview.h"
0026 #include "browserwindow.h"
0027 
0028 #include <QTimer>
0029 
0030 #define HIDE_DELAY 270
0031 
0032 BookmarksWidget::BookmarksWidget(WebView* view, BookmarkItem* bookmark, QWidget* parent)
0033     : LocationBarPopup(parent)
0034     , ui(new Ui::BookmarksWidget)
0035     , m_view(view)
0036     , m_bookmark(bookmark)
0037     , m_bookmarks(mApp->bookmarks())
0038     , m_speedDial(mApp->plugins()->speedDial())
0039     , m_edited(false)
0040 {
0041     ui->setupUi(this);
0042     ui->bookmarksButton->setIcon(QIcon::fromTheme(QSL("bookmark-new")));
0043 
0044     init();
0045 }
0046 
0047 BookmarksWidget::~BookmarksWidget()
0048 {
0049     delete ui;
0050 }
0051 
0052 void BookmarksWidget::toggleSpeedDial()
0053 {
0054     const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
0055 
0056     if (page.url.isEmpty()) {
0057         QString title = m_view->title();
0058         m_speedDial->addPage(m_view->url(), title);
0059     }
0060     else {
0061         m_speedDial->removePage(page);
0062     }
0063 
0064     closePopup();
0065 }
0066 
0067 void BookmarksWidget::toggleBookmark()
0068 {
0069     if (m_bookmark) {
0070         if (m_edited) {
0071             // Change folder
0072             m_bookmarks->removeBookmark(m_bookmark);
0073             m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), m_bookmark);
0074         }
0075         else {
0076             // Remove
0077             m_bookmarks->removeBookmark(m_bookmark);
0078         }
0079     }
0080     else {
0081         // Save bookmark
0082         auto* bookmark = new BookmarkItem(BookmarkItem::Url);
0083         bookmark->setTitle(m_view->title());
0084         bookmark->setUrl(m_view->url());
0085         m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), bookmark);
0086     }
0087 
0088     closePopup();
0089 }
0090 
0091 void BookmarksWidget::bookmarkEdited()
0092 {
0093     if (m_edited) {
0094         return;
0095     }
0096 
0097     m_edited = true;
0098     ui->bookmarksButton->setText(tr("Update Bookmark"));
0099     ui->bookmarksButton->setFlat(true);
0100 }
0101 
0102 void BookmarksWidget::init()
0103 {
0104     // The locationbar's direction is direction of its text,
0105     // it dynamically changes and so, it's not good choice for this widget.
0106     setLayoutDirection(QApplication::layoutDirection());
0107 
0108     // Init SpeedDial button
0109     const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
0110     if (page.url.isEmpty()) {
0111         ui->speeddialButton->setFlat(true);
0112         ui->speeddialButton->setText(tr("Add to Speed Dial"));
0113     }
0114     else {
0115         ui->speeddialButton->setFlat(false);
0116         ui->speeddialButton->setText(tr("Remove from Speed Dial"));
0117     }
0118 
0119     // Init Bookmarks button
0120     if (m_bookmark) {
0121         ui->bookmarksButton->setText(tr("Remove from Bookmarks"));
0122         ui->bookmarksButton->setFlat(false);
0123 
0124         Q_ASSERT(m_bookmark->parent());
0125         ui->folderButton->setSelectedFolder(m_bookmark->parent());
0126         connect(ui->folderButton, &BookmarksFoldersButton::selectedFolderChanged, this, &BookmarksWidget::bookmarkEdited);
0127     }
0128 
0129     connect(ui->speeddialButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleSpeedDial);
0130     connect(ui->bookmarksButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleBookmark);
0131 
0132 }
0133 
0134 void BookmarksWidget::closePopup()
0135 {
0136     // Prevent clicking again on buttons while popup is being closed
0137     disconnect(ui->speeddialButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleSpeedDial);
0138     disconnect(ui->bookmarksButton, &QAbstractButton::clicked, this, &BookmarksWidget::toggleBookmark);
0139 
0140     QTimer::singleShot(HIDE_DELAY, this, &QWidget::close);
0141 }
0142