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

0001 /* ============================================================
0002 * TabManager plugin for Falkon
0003 * Copyright (C) 2013-2017  S. Razi Alavizadeh <s.r.alavizadeh@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 "tabmanagerplugin.h"
0019 #include "tabmanagerwidget.h"
0020 #include "browserwindow.h"
0021 #include "pluginproxy.h"
0022 #include "mainapplication.h"
0023 #include "sidebar.h"
0024 #include "tabwidget.h"
0025 #include "tabbar.h"
0026 #include "tabmanagersettings.h"
0027 #include "../config.h"
0028 
0029 #include <QInputDialog>
0030 #include <QSettings>
0031 #include <QAction>
0032 #include <QTimer>
0033 #include <QMenu>
0034 
0035 QString TabManagerPlugin::s_settingsPath;
0036 
0037 TabManagerPlugin::TabManagerPlugin()
0038     : QObject()
0039     , m_controller(nullptr)
0040     , m_tabManagerWidget(nullptr)
0041     , m_viewType(Undefined)
0042     , m_initState(false)
0043     , m_asTabBarReplacement(false)
0044 {
0045 }
0046 
0047 void TabManagerPlugin::init(InitState state, const QString &settingsPath)
0048 {
0049     Q_UNUSED(state)
0050 
0051     m_controller = new TabManagerWidgetController(this);
0052     connect(mApp->plugins(), SIGNAL(mainWindowCreated(BrowserWindow*)), this, SLOT(mainWindowCreated(BrowserWindow*)));
0053     connect(mApp->plugins(), SIGNAL(mainWindowDeleted(BrowserWindow*)), m_controller, SLOT(mainWindowDeleted(BrowserWindow*)));
0054     connect(mApp->plugins(), SIGNAL(webPageCreated(WebPage*)), m_controller, SIGNAL(requestRefreshTree()));
0055     connect(mApp->plugins(), SIGNAL(webPageDeleted(WebPage*)), m_controller, SIGNAL(requestRefreshTree(WebPage*)));
0056 
0057     s_settingsPath = settingsPath + QL1S("/TabManager");
0058     m_initState = true;
0059 
0060     // load settings
0061     QSettings settings(s_settingsPath + QL1S("/tabmanager.ini"), QSettings::IniFormat);
0062     settings.beginGroup("View");
0063     m_controller->setGroupType(TabManagerWidget::GroupType(settings.value("GroupType", TabManagerWidget::GroupByWindow).toInt()));
0064     m_viewType = ViewType(settings.value("ViewType", ShowAsWindow).toInt());
0065     m_asTabBarReplacement = settings.value("AsTabBarReplacement", false).toBool();
0066     settings.endGroup();
0067 
0068     setAsTabBarReplacement(m_asTabBarReplacement);
0069     insertManagerWidget();
0070 }
0071 
0072 void TabManagerPlugin::unload()
0073 {
0074     saveSettings();
0075 
0076     setTabBarVisible(true);
0077     removeManagerWidget();
0078 
0079     delete m_controller;
0080 }
0081 
0082 bool TabManagerPlugin::testPlugin()
0083 {
0084     return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
0085 }
0086 
0087 void TabManagerPlugin::showSettings(QWidget* parent)
0088 {
0089     auto* settings = new TabManagerSettings(this, parent);
0090     settings->exec();
0091 }
0092 
0093 void TabManagerPlugin::populateExtensionsMenu(QMenu* menu)
0094 {
0095     if (viewType() == ShowAsWindow) {
0096         QAction* showAction = m_controller->createMenuAction();
0097         showAction->setCheckable(false);
0098         connect(showAction, SIGNAL(triggered()), m_controller, SLOT(raiseTabManager()));
0099         menu->addAction(showAction);
0100     }
0101 }
0102 
0103 void TabManagerPlugin::insertManagerWidget()
0104 {
0105     if (viewType() == ShowAsSideBar) {
0106         SideBarManager::addSidebar(QStringLiteral("TabManager"), m_controller);
0107     }
0108     else if (viewType() == ShowAsWindow) {
0109         if (!m_tabManagerWidget) {
0110             m_tabManagerWidget = m_controller->createTabManagerWidget(mApp->getWindow(), nullptr, true);
0111             m_tabManagerWidget->setWindowFlags(Qt::Window);
0112         }
0113     }
0114 
0115     if (m_initState) {
0116         const auto windows = mApp->windows();
0117         for (BrowserWindow* window : windows) {
0118             mainWindowCreated(window, false);
0119         }
0120         m_initState = false;
0121     }
0122 }
0123 
0124 void TabManagerPlugin::mainWindowCreated(BrowserWindow* window, bool refresh)
0125 {
0126     if (window) {
0127         window->tabWidget()->tabBar()->setForceHidden(m_asTabBarReplacement);
0128 
0129         if (m_viewType == ShowAsWindow) {
0130             m_controller->addStatusBarIcon(window);
0131         }
0132 
0133         connect(window->tabWidget(), SIGNAL(currentChanged(int)), m_controller, SIGNAL(requestRefreshTree()));
0134         connect(window->tabWidget(), SIGNAL(pinStateChanged(int,bool)), m_controller, SIGNAL(requestRefreshTree()));
0135     }
0136 
0137     if (refresh) {
0138         m_controller->emitRefreshTree();
0139     }
0140 }
0141 
0142 void TabManagerPlugin::setTabBarVisible(bool visible)
0143 {
0144     const auto windows = mApp->windows();
0145     for (BrowserWindow* window : windows) {
0146         window->tabWidget()->tabBar()->setForceHidden(!visible);
0147     }
0148 }
0149 
0150 void TabManagerPlugin::removeManagerWidget()
0151 {
0152     if (viewType() == ShowAsSideBar) {
0153         SideBarManager::removeSidebar(m_controller);
0154     }
0155     else if (viewType() == ShowAsWindow) {
0156         // remove statusbar icon
0157         const auto windows = mApp->windows();
0158         for (BrowserWindow* window : windows) {
0159             m_controller->removeStatusBarIcon(window);
0160         }
0161 
0162         m_tabManagerWidget->close();
0163         delete m_tabManagerWidget;
0164         m_tabManagerWidget = nullptr;
0165     }
0166 }
0167 
0168 
0169 TabManagerPlugin::ViewType TabManagerPlugin::viewType()
0170 {
0171     return m_viewType;
0172 }
0173 
0174 void TabManagerPlugin::setViewType(ViewType type)
0175 {
0176     if (m_viewType != type) {
0177         removeManagerWidget();
0178         m_viewType  = type;
0179         insertManagerWidget();
0180 
0181         if (!m_initState) {
0182             if (m_viewType == ShowAsSideBar) {
0183                 mApp->getWindow()->sideBarManager()->showSideBar(QStringLiteral("TabManager"));
0184             }
0185             else if (m_viewType == ShowAsWindow) {
0186                 // add statusbar icon
0187                 const auto windows = mApp->windows();
0188                 for (BrowserWindow* window : windows) {
0189                     m_controller->addStatusBarIcon(window);
0190                 }
0191             }
0192         }
0193     }
0194 }
0195 
0196 QString TabManagerPlugin::settingsPath()
0197 {
0198     return s_settingsPath;
0199 }
0200 
0201 void TabManagerPlugin::saveSettings()
0202 {
0203     QSettings settings(s_settingsPath + QL1S("/tabmanager.ini"), QSettings::IniFormat);
0204     settings.beginGroup("View");
0205     settings.setValue("GroupType", m_controller->groupType());
0206     settings.setValue("ViewType", viewType());
0207     settings.setValue("AsTabBarReplacement", asTabBarReplacement());
0208     settings.endGroup();
0209 }
0210 
0211 bool TabManagerPlugin::asTabBarReplacement() const
0212 {
0213     return m_asTabBarReplacement;
0214 }
0215 
0216 void TabManagerPlugin::setAsTabBarReplacement(bool yes)
0217 {
0218     m_asTabBarReplacement = yes;
0219     setTabBarVisible(!m_asTabBarReplacement);
0220 }
0221