File indexing completed on 2024-05-19 04:59:22

0001 /* ============================================================
0002 * VerticalTabs plugin for Falkon
0003 * Copyright (C) 2018 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 "verticaltabsplugin.h"
0019 #include "verticaltabssettings.h"
0020 #include "verticaltabscontroller.h"
0021 #include "verticaltabsschemehandler.h"
0022 
0023 #include "browserwindow.h"
0024 #include "pluginproxy.h"
0025 #include "mainapplication.h"
0026 #include "tabwidget.h"
0027 #include "tabbar.h"
0028 #include "sidebar.h"
0029 #include "networkmanager.h"
0030 #include "../config.h"
0031 
0032 #include <QSettings>
0033 #include <QFile>
0034 
0035 VerticalTabsPlugin::VerticalTabsPlugin()
0036     : QObject()
0037 {
0038 }
0039 
0040 void VerticalTabsPlugin::init(InitState state, const QString &settingsPath)
0041 {
0042     m_settingsPath = settingsPath + QL1S("/extensions.ini");
0043 
0044     QSettings settings(m_settingsPath, QSettings::IniFormat);
0045     settings.beginGroup(QSL("VerticalTabs"));
0046     m_viewType = static_cast<ViewType>(settings.value(QSL("ViewType"), TabListView).toInt());
0047     m_replaceTabBar = settings.value(QSL("ReplaceTabBar"), false).toBool();
0048     m_addChildBehavior = static_cast<AddChildBehavior>(settings.value(QSL("AddChildBehavior"), AppendChild).toInt());
0049     m_theme = settings.value(QSL("Theme"), QSL(":verticaltabs/data/themes/default.css")).toString();
0050     settings.endGroup();
0051 
0052     m_controller = new VerticalTabsController(this);
0053     SideBarManager::addSidebar(QSL("VerticalTabs"), m_controller);
0054 
0055     m_schemeHandler = new VerticalTabsSchemeHandler(this);
0056     mApp->networkManager()->registerExtensionSchemeHandler(QSL("verticaltabs"), m_schemeHandler);
0057 
0058     mApp->plugins()->registerAppEventHandler(PluginProxy::KeyPressHandler, this);
0059 
0060     setWebTabBehavior(m_addChildBehavior);
0061     loadStyleSheet(m_theme);
0062 
0063     connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &VerticalTabsPlugin::mainWindowCreated);
0064 
0065     if (state == LateInitState) {
0066         const auto windows = mApp->windows();
0067         for (BrowserWindow *window : windows) {
0068             mainWindowCreated(window);
0069             if (window->sideBarManager()->activeSideBar().isEmpty()) {
0070                 window->sideBarManager()->showSideBar(QSL("VerticalTabs"));
0071             }
0072         }
0073     }
0074 }
0075 
0076 void VerticalTabsPlugin::unload()
0077 {
0078     setTabBarVisible(true);
0079 
0080     SideBarManager::removeSidebar(m_controller);
0081     delete m_controller;
0082     m_controller = nullptr;
0083 
0084     mApp->networkManager()->unregisterExtensionSchemeHandler(m_schemeHandler);
0085 }
0086 
0087 bool VerticalTabsPlugin::testPlugin()
0088 {
0089     return (QString::fromLatin1(Qz::VERSION) == QSL(FALKON_VERSION));
0090 }
0091 
0092 void VerticalTabsPlugin::showSettings(QWidget *parent)
0093 {
0094     auto *settings = new VerticalTabsSettings(this, parent);
0095     settings->exec();
0096 }
0097 
0098 bool VerticalTabsPlugin::keyPress(Qz::ObjectName type, QObject *obj, QKeyEvent *event)
0099 {
0100     if (type == Qz::ON_TabWidget) {
0101         return m_controller->handleKeyPress(event, static_cast<TabWidget*>(obj));
0102     }
0103     return false;
0104 }
0105 
0106 VerticalTabsPlugin::ViewType VerticalTabsPlugin::viewType() const
0107 {
0108     return m_viewType;
0109 }
0110 
0111 void VerticalTabsPlugin::setViewType(ViewType type)
0112 {
0113     if (m_viewType == type) {
0114         return;
0115     }
0116 
0117     m_viewType = type;
0118 
0119     QSettings settings(m_settingsPath, QSettings::IniFormat);
0120     settings.setValue(QSL("VerticalTabs/ViewType"), m_viewType);
0121 
0122     Q_EMIT viewTypeChanged(m_viewType);
0123 }
0124 
0125 bool VerticalTabsPlugin::replaceTabBar() const
0126 {
0127     return m_replaceTabBar;
0128 }
0129 
0130 void VerticalTabsPlugin::setReplaceTabBar(bool replace)
0131 {
0132     if (m_replaceTabBar == replace) {
0133         return;
0134     }
0135 
0136     m_replaceTabBar = replace;
0137     setTabBarVisible(!m_replaceTabBar);
0138 
0139     QSettings settings(m_settingsPath, QSettings::IniFormat);
0140     settings.setValue(QSL("VerticalTabs/ReplaceTabBar"), m_replaceTabBar);
0141 }
0142 
0143 VerticalTabsPlugin::AddChildBehavior VerticalTabsPlugin::addChildBehavior() const
0144 {
0145     return m_addChildBehavior;
0146 }
0147 
0148 void VerticalTabsPlugin::setAddChildBehavior(AddChildBehavior behavior)
0149 {
0150     if (m_addChildBehavior == behavior) {
0151         return;
0152     }
0153 
0154     m_addChildBehavior = behavior;
0155     setWebTabBehavior(m_addChildBehavior);
0156 
0157     QSettings settings(m_settingsPath, QSettings::IniFormat);
0158     settings.setValue(QSL("VerticalTabs/AddChildBehavior"), m_addChildBehavior);
0159 }
0160 
0161 QString VerticalTabsPlugin::theme() const
0162 {
0163     return m_theme;
0164 }
0165 
0166 void VerticalTabsPlugin::setTheme(const QString &theme)
0167 {
0168     if (theme.isEmpty()) {
0169         return;
0170     }
0171 
0172     // Don't check if same to allow live reloading stylesheet
0173 
0174     m_theme = theme;
0175     loadStyleSheet(m_theme);
0176 
0177     QSettings settings(m_settingsPath, QSettings::IniFormat);
0178     settings.setValue(QSL("VerticalTabs/Theme"), m_theme);
0179 }
0180 
0181 QString VerticalTabsPlugin::styleSheet() const
0182 {
0183     return m_styleSheet;
0184 }
0185 
0186 void VerticalTabsPlugin::mainWindowCreated(BrowserWindow *window)
0187 {
0188     Q_UNUSED(window)
0189     setTabBarVisible(!m_replaceTabBar);
0190 }
0191 
0192 void VerticalTabsPlugin::setTabBarVisible(bool visible)
0193 {
0194     const auto windows = mApp->windows();
0195     for (BrowserWindow *window : windows) {
0196         window->tabWidget()->tabBar()->setForceHidden(!visible);
0197     }
0198 }
0199 
0200 void VerticalTabsPlugin::setWebTabBehavior(AddChildBehavior behavior)
0201 {
0202     WebTab::setAddChildBehavior(behavior == AppendChild ? WebTab::AppendChild : WebTab::PrependChild);
0203 }
0204 
0205 void VerticalTabsPlugin::loadStyleSheet(const QString &theme)
0206 {
0207     QFile file(theme);
0208     if (!file.open(QFile::ReadOnly)) {
0209         qWarning() << "Failed to open stylesheet file" << theme;
0210         file.setFileName(QSL(":verticaltabs/data/themes/default.css"));
0211         file.open(QFile::ReadOnly);
0212     }
0213 
0214     m_styleSheet = QString::fromUtf8(file.readAll());
0215     Q_EMIT styleSheetChanged(m_styleSheet);
0216 }