File indexing completed on 2024-05-12 04:58:21

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 "bookmarkssidebar.h"
0019 #include "ui_bookmarkssidebar.h"
0020 #include "bookmarkstools.h"
0021 #include "bookmarkitem.h"
0022 #include "bookmarks.h"
0023 #include "mainapplication.h"
0024 #include "iconprovider.h"
0025 
0026 #include <QMenu>
0027 
0028 BookmarksSidebar::BookmarksSidebar(BrowserWindow* window, QWidget* parent)
0029     : QWidget(parent)
0030     , ui(new Ui::BookmarksSideBar)
0031     , m_window(window)
0032     , m_bookmarks(mApp->bookmarks())
0033 {
0034     ui->setupUi(this);
0035     ui->tree->setViewType(BookmarksTreeView::BookmarksSidebarViewType);
0036 
0037     connect(ui->tree, &BookmarksTreeView::bookmarkActivated, this, &BookmarksSidebar::bookmarkActivated);
0038     connect(ui->tree, &BookmarksTreeView::bookmarkCtrlActivated, this, &BookmarksSidebar::bookmarkCtrlActivated);
0039     connect(ui->tree, &BookmarksTreeView::bookmarkShiftActivated, this, &BookmarksSidebar::bookmarkShiftActivated);
0040     connect(ui->tree, &BookmarksTreeView::contextMenuRequested, this, &BookmarksSidebar::createContextMenu);
0041 
0042     connect(ui->search, &QLineEdit::textChanged, ui->tree, &BookmarksTreeView::search);
0043 }
0044 
0045 BookmarksSidebar::~BookmarksSidebar()
0046 {
0047     delete ui;
0048 }
0049 
0050 void BookmarksSidebar::bookmarkActivated(BookmarkItem* item)
0051 {
0052     openBookmark(item);
0053 }
0054 
0055 void BookmarksSidebar::bookmarkCtrlActivated(BookmarkItem* item)
0056 {
0057     openBookmarkInNewTab(item);
0058 }
0059 
0060 void BookmarksSidebar::bookmarkShiftActivated(BookmarkItem* item)
0061 {
0062     openBookmarkInNewWindow(item);
0063 }
0064 
0065 void BookmarksSidebar::openBookmark(BookmarkItem* item)
0066 {
0067     item = item ? item : ui->tree->selectedBookmark();
0068     BookmarksTools::openBookmark(m_window, item);
0069 }
0070 
0071 void BookmarksSidebar::openBookmarkInNewTab(BookmarkItem* item)
0072 {
0073     item = item ? item : ui->tree->selectedBookmark();
0074     BookmarksTools::openBookmarkInNewTab(m_window, item);
0075 }
0076 
0077 void BookmarksSidebar::openBookmarkInNewWindow(BookmarkItem* item)
0078 {
0079     item = item ? item : ui->tree->selectedBookmark();
0080     BookmarksTools::openBookmarkInNewWindow(item);
0081 }
0082 
0083 void BookmarksSidebar::openBookmarkInNewPrivateWindow(BookmarkItem* item)
0084 {
0085     item = item ? item : ui->tree->selectedBookmark();
0086     BookmarksTools::openBookmarkInNewPrivateWindow(item);
0087 }
0088 
0089 void BookmarksSidebar::deleteBookmarks()
0090 {
0091     const QList<BookmarkItem*> items = ui->tree->selectedBookmarks();
0092 
0093     for (BookmarkItem* item : items) {
0094         if (m_bookmarks->canBeModified(item)) {
0095             m_bookmarks->removeBookmark(item);
0096         }
0097     }
0098 }
0099 
0100 void BookmarksSidebar::createContextMenu(const QPoint &pos)
0101 {
0102     QMenu menu;
0103     QAction* actNewTab = menu.addAction(IconProvider::newTabIcon(), tr("Open in new tab"));
0104     QAction* actNewWindow = menu.addAction(IconProvider::newWindowIcon(), tr("Open in new window"));
0105     QAction* actNewPrivateWindow = menu.addAction(IconProvider::privateBrowsingIcon(), tr("Open in new private window"));
0106 
0107     menu.addSeparator();
0108     QAction* actDelete = menu.addAction(QIcon::fromTheme(QSL("edit-delete")), tr("Delete"));
0109 
0110     connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab()));
0111     connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow()));
0112     connect(actNewPrivateWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewPrivateWindow()));
0113     connect(actDelete, &QAction::triggered, this, &BookmarksSidebar::deleteBookmarks);
0114 
0115     bool canBeDeleted = false;
0116     const QList<BookmarkItem*> items = ui->tree->selectedBookmarks();
0117 
0118     for (BookmarkItem* item : items) {
0119         if (m_bookmarks->canBeModified(item)) {
0120             canBeDeleted = true;
0121             break;
0122         }
0123     }
0124 
0125     if (!canBeDeleted) {
0126         actDelete->setDisabled(true);
0127     }
0128 
0129     if (!ui->tree->selectedBookmark() || !ui->tree->selectedBookmark()->isUrl()) {
0130         actNewTab->setDisabled(true);
0131         actNewWindow->setDisabled(true);
0132         actNewPrivateWindow->setDisabled(true);
0133     }
0134 
0135     menu.exec(pos);
0136 }
0137 
0138 void BookmarksSidebar::showEvent(QShowEvent *event)
0139 {
0140     QWidget::showEvent(event);
0141     ui->search->setFocus();
0142 }