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

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 "browsinglibrary.h"
0019 #include "ui_browsinglibrary.h"
0020 #include "bookmarksimport/bookmarksimportdialog.h"
0021 #include "bookmarksexport/bookmarksexportdialog.h"
0022 #include "historymanager.h"
0023 #include "bookmarksmanager.h"
0024 #include "mainapplication.h"
0025 #include "qztools.h"
0026 #include "settings.h"
0027 
0028 #include <QMenu>
0029 #include <QCloseEvent>
0030 
0031 BrowsingLibrary::BrowsingLibrary(BrowserWindow* window, QWidget* parent)
0032     : QWidget(parent)
0033     , ui(new Ui::BrowsingLibrary)
0034     , m_historyManager(new HistoryManager(window))
0035     , m_bookmarksManager(new BookmarksManager(window))
0036 {
0037     ui->setupUi(this);
0038 
0039     Settings settings;
0040     settings.beginGroup(QSL("BrowsingLibrary"));
0041     resize(settings.value(QSL("size"), QSize(760, 470)).toSize());
0042     m_historyManager->restoreState(settings.value(QSL("historyState"), QByteArray()).toByteArray());
0043     settings.endGroup();
0044 
0045     QzTools::centerWidgetOnScreen(this);
0046 
0047     QIcon historyIcon;
0048     historyIcon.addFile(QSL(":/icons/other/bighistory.svg"), QSize(), QIcon::Normal);
0049     historyIcon.addFile(QSL(":/icons/other/bighistory-selected.svg"), QSize(), QIcon::Selected);
0050 
0051     QIcon bookmarksIcon;
0052     bookmarksIcon.addFile(QSL(":/icons/other/bigstar.svg"), QSize(), QIcon::Normal);
0053     bookmarksIcon.addFile(QSL(":/icons/other/bigstar-selected.svg"), QSize(), QIcon::Selected);
0054 
0055     ui->tabs->AddTab(m_historyManager, historyIcon, tr("History"));
0056     ui->tabs->AddTab(m_bookmarksManager, bookmarksIcon, tr("Bookmarks"));
0057     ui->tabs->SetMode(FancyTabWidget::Mode_LargeSidebar);
0058     ui->tabs->setFocus();
0059 
0060     auto* m = new QMenu(this);
0061     m->addAction(tr("Import Bookmarks..."), this, &BrowsingLibrary::importBookmarks);
0062     m->addAction(tr("Export Bookmarks..."), this, &BrowsingLibrary::exportBookmarks);
0063     ui->importExport->setMenu(m);
0064 
0065     connect(ui->tabs, &FancyTabWidget::CurrentChanged, ui->searchLine, &QLineEdit::clear);
0066     connect(ui->searchLine, &QLineEdit::textChanged, this, &BrowsingLibrary::search);
0067 
0068     QzTools::setWmClass(QSL("Browsing Library"), this);
0069 }
0070 
0071 void BrowsingLibrary::search()
0072 {
0073     if (ui->tabs->current_index() == 0) {
0074         m_historyManager->search(ui->searchLine->text());
0075     }
0076     else {
0077         m_bookmarksManager->search(ui->searchLine->text());
0078     }
0079 }
0080 
0081 void BrowsingLibrary::importBookmarks()
0082 {
0083     auto* d = new BookmarksImportDialog(this);
0084     d->open();
0085 }
0086 
0087 void BrowsingLibrary::exportBookmarks()
0088 {
0089     auto* d = new BookmarksExportDialog(this);
0090     d->open();
0091 }
0092 
0093 void BrowsingLibrary::showHistory(BrowserWindow* window)
0094 {
0095     ui->tabs->SetCurrentIndex(0);
0096     show();
0097     m_historyManager->setMainWindow(window);
0098 
0099     raise();
0100     activateWindow();
0101 }
0102 
0103 void BrowsingLibrary::showBookmarks(BrowserWindow* window)
0104 {
0105     ui->tabs->SetCurrentIndex(1);
0106     show();
0107     m_bookmarksManager->setMainWindow(window);
0108 
0109     raise();
0110     activateWindow();
0111 }
0112 
0113 void BrowsingLibrary::closeEvent(QCloseEvent* e)
0114 {
0115     Settings settings;
0116     settings.beginGroup(QSL("BrowsingLibrary"));
0117     settings.setValue(QSL("size"), size());
0118     settings.setValue(QSL("historyState"), m_historyManager->saveState());
0119     settings.endGroup();
0120     e->accept();
0121 }
0122 
0123 void BrowsingLibrary::keyPressEvent(QKeyEvent* e)
0124 {
0125     if (e->key() == Qt::Key_Escape
0126         || (e->key() == Qt::Key_W && e->modifiers() == Qt::ControlModifier)) {
0127         close();
0128     }
0129 
0130     QWidget::keyPressEvent(e);
0131 }
0132 
0133 BrowsingLibrary::~BrowsingLibrary()
0134 {
0135     delete ui;
0136 }