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

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2017 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 "sessionmanagerdialog.h"
0019 #include "ui_sessionmanagerdialog.h"
0020 #include "mainapplication.h"
0021 #include "sessionmanager.h"
0022 #include "removeitemfocusdelegate.h"
0023 
0024 #include <QFileInfo>
0025 #include <QDateTime>
0026 
0027 SessionManagerDialog::SessionManagerDialog(QWidget *parent) :
0028     QDialog(parent),
0029     ui(new Ui::SessionManagerDialog)
0030 {
0031     ui->setupUi(this);
0032     setAttribute(Qt::WA_DeleteOnClose);
0033 
0034     ui->treeWidget->setItemDelegate(new RemoveItemFocusDelegate(ui->treeWidget));
0035 
0036     connect(ui->newButton, &QPushButton::clicked, this, &SessionManagerDialog::newSession);
0037     connect(ui->renameButton, &QPushButton::clicked, this, &SessionManagerDialog::renameSession);
0038     connect(ui->cloneButton, &QPushButton::clicked, this, &SessionManagerDialog::cloneSession);
0039     connect(ui->deleteButton, &QPushButton::clicked, this, &SessionManagerDialog::deleteSession);
0040     connect(ui->switchToButton, &QPushButton::clicked, this, &SessionManagerDialog::switchToSession);
0041     connect(ui->treeWidget, &QTreeWidget::currentItemChanged, this, &SessionManagerDialog::updateButtons);
0042 
0043     refresh();
0044     connect(mApp->sessionManager(), &SessionManager::sessionsMetaDataChanged, this, &SessionManagerDialog::refresh);
0045 }
0046 
0047 SessionManagerDialog::~SessionManagerDialog()
0048 {
0049     delete ui;
0050 }
0051 
0052 void SessionManagerDialog::newSession()
0053 {
0054     mApp->sessionManager()->newSession();
0055 }
0056 
0057 void SessionManagerDialog::renameSession()
0058 {
0059     QTreeWidgetItem *item = ui->treeWidget->currentItem();
0060     if (!item) {
0061         return;
0062     }
0063     const QString filePath = item->data(0, SessionFileRole).toString();
0064     if (!filePath.isEmpty()) {
0065         mApp->sessionManager()->renameSession(filePath);
0066     }
0067 }
0068 
0069 void SessionManagerDialog::cloneSession()
0070 {
0071     QTreeWidgetItem *item = ui->treeWidget->currentItem();
0072     if (!item) {
0073         return;
0074     }
0075     const QString filePath = item->data(0, SessionFileRole).toString();
0076     if (!filePath.isEmpty()) {
0077         mApp->sessionManager()->cloneSession(filePath);
0078     }
0079 }
0080 
0081 void SessionManagerDialog::deleteSession()
0082 {
0083     QTreeWidgetItem *item = ui->treeWidget->currentItem();
0084     if (!item) {
0085         return;
0086     }
0087     const QString filePath = item->data(0, SessionFileRole).toString();
0088     if (!filePath.isEmpty()) {
0089         mApp->sessionManager()->deleteSession(filePath);
0090     }
0091 }
0092 
0093 void SessionManagerDialog::switchToSession()
0094 {
0095     QTreeWidgetItem *item = ui->treeWidget->currentItem();
0096     if (!item) {
0097         return;
0098     }
0099     const QString filePath = item->data(0, SessionFileRole).toString();
0100     if (!filePath.isEmpty()) {
0101         if (item->data(0, IsBackupSessionRole).toBool()) {
0102             mApp->sessionManager()->replaceSession(filePath);
0103         } else {
0104             mApp->sessionManager()->switchToSession(filePath);
0105         }
0106     }
0107 }
0108 
0109 void SessionManagerDialog::refresh()
0110 {
0111     ui->treeWidget->clear();
0112 
0113     const auto sessions = mApp->sessionManager()->sessionMetaData();
0114     for (const auto &session : sessions) {
0115         auto *item = new QTreeWidgetItem;
0116         item->setText(0, session.name);
0117         item->setText(1, QLocale().toString(QFileInfo(session.filePath).lastModified(), QLocale::ShortFormat));
0118         item->setData(0, SessionFileRole, session.filePath);
0119         item->setData(0, IsBackupSessionRole, session.isBackup);
0120         item->setData(0, IsActiveSessionRole, session.isActive);
0121         item->setData(0, IsDefaultSessionRole, session.isDefault);
0122         updateItem(item);
0123         ui->treeWidget->addTopLevelItem(item);
0124     }
0125 
0126     updateButtons();
0127 }
0128 
0129 void SessionManagerDialog::updateButtons()
0130 {
0131     QTreeWidgetItem *item = ui->treeWidget->currentItem();
0132     const bool isBackup = item && item->data(0, IsBackupSessionRole).toBool();
0133     const bool isActive = item && item->data(0, IsActiveSessionRole).toBool();
0134     const bool isDefault = item && item->data(0, IsDefaultSessionRole).toBool();
0135 
0136     ui->renameButton->setEnabled(item && !isDefault && !isBackup);
0137     ui->cloneButton->setEnabled(item && !isBackup);
0138     ui->deleteButton->setEnabled(item && !isBackup && !isDefault && !isActive);
0139     ui->switchToButton->setEnabled(item && !isActive);
0140     ui->switchToButton->setText(isBackup ? tr("Restore") : tr("Switch To"));
0141 }
0142 
0143 void SessionManagerDialog::updateItem(QTreeWidgetItem *item)
0144 {
0145     const bool isBackup = item->data(0, IsBackupSessionRole).toBool();
0146     const bool isActive = item->data(0, IsActiveSessionRole).toBool();
0147     const bool isDefault = item->data(0, IsDefaultSessionRole).toBool();
0148 
0149     QFont font = item->font(0);
0150 
0151     if (isBackup) {
0152         const QColor color = palette().color(QPalette::Disabled, QPalette::WindowText);
0153         item->setForeground(0, color);
0154         item->setForeground(1, color);
0155     }
0156 
0157     if (isActive) {
0158         font.setBold(true);
0159         item->setFont(0, font);
0160         item->setFont(1, font);
0161     }
0162 
0163     if (isDefault) {
0164         font.setItalic(true);
0165         item->setFont(0, font);
0166         item->setFont(1, font);
0167     }
0168 }
0169 
0170 void SessionManagerDialog::showEvent(QShowEvent *e)
0171 {
0172     QDialog::showEvent(e);
0173     resizeViewHeader();
0174 }
0175 
0176 void SessionManagerDialog::resizeEvent(QResizeEvent *e)
0177 {
0178     QDialog::resizeEvent(e);
0179     resizeViewHeader();
0180 }
0181 
0182 void SessionManagerDialog::resizeViewHeader()
0183 {
0184     const int headerWidth = ui->treeWidget->header()->width();
0185     ui->treeWidget->header()->resizeSection(0, headerWidth - headerWidth / 2.5);
0186 }