Falkon Develop
Cross-platform Qt-based web browser
sessionmanagerdialog.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "ui_sessionmanagerdialog.h"
20#include "mainapplication.h"
21#include "sessionmanager.h"
23
24#include <QFileInfo>
25#include <QDateTime>
26
28 QDialog(parent),
30{
31 ui->setupUi(this);
32 setAttribute(Qt::WA_DeleteOnClose);
33
34 ui->treeWidget->setItemDelegate(new RemoveItemFocusDelegate(ui->treeWidget));
35
36 connect(ui->newButton, &QPushButton::clicked, this, &SessionManagerDialog::newSession);
37 connect(ui->renameButton, &QPushButton::clicked, this, &SessionManagerDialog::renameSession);
38 connect(ui->cloneButton, &QPushButton::clicked, this, &SessionManagerDialog::cloneSession);
39 connect(ui->deleteButton, &QPushButton::clicked, this, &SessionManagerDialog::deleteSession);
40 connect(ui->switchToButton, &QPushButton::clicked, this, &SessionManagerDialog::switchToSession);
41 connect(ui->treeWidget, &QTreeWidget::currentItemChanged, this, &SessionManagerDialog::updateButtons);
42
43 refresh();
44 connect(mApp->sessionManager(), &SessionManager::sessionsMetaDataChanged, this, &SessionManagerDialog::refresh);
45}
46
48{
49 delete ui;
50}
51
52void SessionManagerDialog::newSession()
53{
54 mApp->sessionManager()->newSession();
55}
56
57void SessionManagerDialog::renameSession()
58{
59 QTreeWidgetItem *item = ui->treeWidget->currentItem();
60 if (!item) {
61 return;
62 }
63 const QString filePath = item->data(0, SessionFileRole).toString();
64 if (!filePath.isEmpty()) {
65 mApp->sessionManager()->renameSession(filePath);
66 }
67}
68
69void SessionManagerDialog::cloneSession()
70{
71 QTreeWidgetItem *item = ui->treeWidget->currentItem();
72 if (!item) {
73 return;
74 }
75 const QString filePath = item->data(0, SessionFileRole).toString();
76 if (!filePath.isEmpty()) {
77 mApp->sessionManager()->cloneSession(filePath);
78 }
79}
80
81void SessionManagerDialog::deleteSession()
82{
83 QTreeWidgetItem *item = ui->treeWidget->currentItem();
84 if (!item) {
85 return;
86 }
87 const QString filePath = item->data(0, SessionFileRole).toString();
88 if (!filePath.isEmpty()) {
89 mApp->sessionManager()->deleteSession(filePath);
90 }
91}
92
93void SessionManagerDialog::switchToSession()
94{
95 QTreeWidgetItem *item = ui->treeWidget->currentItem();
96 if (!item) {
97 return;
98 }
99 const QString filePath = item->data(0, SessionFileRole).toString();
100 if (!filePath.isEmpty()) {
101 if (item->data(0, IsBackupSessionRole).toBool()) {
102 mApp->sessionManager()->replaceSession(filePath);
103 } else {
104 mApp->sessionManager()->switchToSession(filePath);
105 }
106 }
107}
108
109void SessionManagerDialog::refresh()
110{
111 ui->treeWidget->clear();
112
113 const auto sessions = mApp->sessionManager()->sessionMetaData();
114 for (const auto &session : sessions) {
115 auto *item = new QTreeWidgetItem;
116 item->setText(0, session.name);
117 item->setText(1, QLocale().toString(QFileInfo(session.filePath).lastModified(), QLocale::ShortFormat));
118 item->setData(0, SessionFileRole, session.filePath);
119 item->setData(0, IsBackupSessionRole, session.isBackup);
120 item->setData(0, IsActiveSessionRole, session.isActive);
121 item->setData(0, IsDefaultSessionRole, session.isDefault);
122 updateItem(item);
123 ui->treeWidget->addTopLevelItem(item);
124 }
125
126 updateButtons();
127}
128
129void SessionManagerDialog::updateButtons()
130{
131 QTreeWidgetItem *item = ui->treeWidget->currentItem();
132 const bool isBackup = item && item->data(0, IsBackupSessionRole).toBool();
133 const bool isActive = item && item->data(0, IsActiveSessionRole).toBool();
134 const bool isDefault = item && item->data(0, IsDefaultSessionRole).toBool();
135
136 ui->renameButton->setEnabled(item && !isDefault && !isBackup);
137 ui->cloneButton->setEnabled(item && !isBackup);
138 ui->deleteButton->setEnabled(item && !isBackup && !isDefault && !isActive);
139 ui->switchToButton->setEnabled(item && !isActive);
140 ui->switchToButton->setText(isBackup ? tr("Restore") : tr("Switch To"));
141}
142
143void SessionManagerDialog::updateItem(QTreeWidgetItem *item)
144{
145 const bool isBackup = item->data(0, IsBackupSessionRole).toBool();
146 const bool isActive = item->data(0, IsActiveSessionRole).toBool();
147 const bool isDefault = item->data(0, IsDefaultSessionRole).toBool();
148
149 QFont font = item->font(0);
150
151 if (isBackup) {
152 const QColor color = palette().color(QPalette::Disabled, QPalette::WindowText);
153 item->setForeground(0, color);
154 item->setForeground(1, color);
155 }
156
157 if (isActive) {
158 font.setBold(true);
159 item->setFont(0, font);
160 item->setFont(1, font);
161 }
162
163 if (isDefault) {
164 font.setItalic(true);
165 item->setFont(0, font);
166 item->setFont(1, font);
167 }
168}
169
170void SessionManagerDialog::showEvent(QShowEvent *e)
171{
172 QDialog::showEvent(e);
173 resizeViewHeader();
174}
175
176void SessionManagerDialog::resizeEvent(QResizeEvent *e)
177{
178 QDialog::resizeEvent(e);
179 resizeViewHeader();
180}
181
182void SessionManagerDialog::resizeViewHeader()
183{
184 const int headerWidth = ui->treeWidget->header()->width();
185 ui->treeWidget->header()->resizeSection(0, headerWidth - headerWidth / 2.5);
186}
SessionManagerDialog(QWidget *parent=nullptr)
void sessionsMetaDataChanged()
#define mApp