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

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 "bookmarksicon.h"
0019 #include "bookmarkswidget.h"
0020 #include "bookmarks.h"
0021 #include "mainapplication.h"
0022 #include "webview.h"
0023 #include "locationbar.h"
0024 #include "pluginproxy.h"
0025 #include "speeddial.h"
0026 
0027 #include <QStyle>
0028 #include <QContextMenuEvent>
0029 
0030 BookmarksIcon::BookmarksIcon(QWidget* parent)
0031     : ClickableLabel(parent)
0032     , m_view(nullptr)
0033     , m_bookmark(nullptr)
0034 {
0035     setObjectName("locationbar-bookmarkicon");
0036     setCursor(Qt::PointingHandCursor);
0037     setToolTip(tr("Bookmark this Page"));
0038     setFocusPolicy(Qt::ClickFocus);
0039 
0040     connect(mApp->bookmarks(), SIGNAL(bookmarkAdded(BookmarkItem*)), this, SLOT(bookmarksChanged()));
0041     connect(mApp->bookmarks(), SIGNAL(bookmarkRemoved(BookmarkItem*)), this, SLOT(bookmarksChanged()));
0042     connect(mApp->bookmarks(), SIGNAL(bookmarkChanged(BookmarkItem*)), this, SLOT(bookmarksChanged()));
0043     connect(mApp->plugins()->speedDial(), SIGNAL(pagesChanged()), this, SLOT(speedDialChanged()));
0044 
0045     connect(this, SIGNAL(clicked(QPoint)), this, SLOT(iconClicked()));
0046 }
0047 
0048 void BookmarksIcon::setWebView(WebView* view)
0049 {
0050     m_view = view;
0051     connect(view, &WebView::urlChanged, this, [this](const QUrl &url) {
0052         checkBookmark(url);
0053     });
0054 }
0055 
0056 void BookmarksIcon::checkBookmark(const QUrl &url, bool forceCheck)
0057 {
0058     if (!forceCheck && m_lastUrl == url) {
0059         return;
0060     }
0061 
0062     QList<BookmarkItem*> items = mApp->bookmarks()->searchBookmarks(url);
0063     m_bookmark = items.isEmpty() ? nullptr : items.at(0);
0064 
0065     if (m_bookmark || mApp->plugins()->speedDial()->pageForUrl(url).isValid()) {
0066         setBookmarkSaved();
0067     }
0068     else {
0069         setBookmarkDisabled();
0070     }
0071 
0072     m_lastUrl = url;
0073 }
0074 
0075 void BookmarksIcon::bookmarksChanged()
0076 {
0077     checkBookmark(m_lastUrl, true);
0078 }
0079 
0080 void BookmarksIcon::speedDialChanged()
0081 {
0082     checkBookmark(m_lastUrl, true);
0083 }
0084 
0085 void BookmarksIcon::iconClicked()
0086 {
0087     if (!m_view) {
0088         return;
0089     }
0090 
0091     auto* widget = new BookmarksWidget(m_view, m_bookmark, parentWidget());
0092     widget->showAt(parentWidget());
0093 }
0094 
0095 void BookmarksIcon::setBookmarkSaved()
0096 {
0097     setProperty("bookmarked", QVariant(true));
0098     style()->unpolish(this);
0099     style()->polish(this);
0100     setToolTip(tr("Edit this bookmark"));
0101 }
0102 
0103 void BookmarksIcon::setBookmarkDisabled()
0104 {
0105     setProperty("bookmarked", QVariant(false));
0106     style()->unpolish(this);
0107     style()->polish(this);
0108     setToolTip(tr("Bookmark this Page"));
0109 }
0110 
0111 void BookmarksIcon::contextMenuEvent(QContextMenuEvent* ev)
0112 {
0113     // Prevent propagating to LocationBar
0114     ev->accept();
0115 }
0116 
0117 void BookmarksIcon::mousePressEvent(QMouseEvent* ev)
0118 {
0119     ClickableLabel::mousePressEvent(ev);
0120 
0121     // Prevent propagating to LocationBar
0122     ev->accept();
0123 }