/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://library.iserlohn-fortress.net/aqua/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #include "savesessiondialog.h" #include "browser.h" #include "mainwindow/mainwindow.h" #include "session_json.hpp" #include "subwindow/subwindow.h" #include "ui_savesessiondialog.h" #include "webengine/webprofile.h" #include "webengine/webview.h" #include #include #include SaveSessionDialog::SaveSessionDialog(QWidget *parent) : QDialog(parent) , ui(new Ui::SaveSessionDialog) { ui->setupUi(this); auto *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); for(MainWindow *window : browser->windows()) { auto *windowItem = new QTreeWidgetItem(ui->treeWidget); windowItem->setText(0, tr("Main Window")); windowItem->setData(0, Qt::UserRole, QVariant::fromValue(static_cast(window))); windowItem->setCheckState(0, Qt::Checked); ui->treeWidget->expandItem(windowItem); for(const SubWindow *subwindow : window->subWindows()) { auto *subwindowItem = new QTreeWidgetItem(windowItem); subwindowItem->setText(0, tr("Subwindow")); subwindowItem->setText(1, subwindow->profile()->getId()); ui->treeWidget->expandItem(subwindowItem); for(int i = 0; i < subwindow->tabCount(); ++i) { auto *tabItem = new QTreeWidgetItem(subwindowItem); auto *view = subwindow->view(i); tabItem->setText(0, view->title()); tabItem->setText(1, view->profile()->getId()); } } } } SaveSessionDialog::~SaveSessionDialog() { delete ui; } void SaveSessionDialog::save(const QString &sessionPath) { const QString filename = QFileDialog::getSaveFileName(this, tr("Save Session"), sessionPath, tr("JSON (*.json)")); QFile output(filename); if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { QVector windows; for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) { QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i); if(item->checkState(0) == Qt::Checked) { auto *window = static_cast(item->data(0, Qt::UserRole).value()); windows.append(window->serialize()); } } JsonSession session(windows); output.write(session.serialize()); output.close(); } }