aboutsummaryrefslogtreecommitdiff
path: root/src/session/savesessiondialog.cpp
blob: e22ce3a01c520490f199d998b7a0705b52cddfc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * 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 <QFileDialog>
#include <QPointer>
#include <QTreeWidgetItem>

SaveSessionDialog::SaveSessionDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SaveSessionDialog)
{
    ui->setupUi(this);

    auto *browser = qobject_cast<Browser *>(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<void *>(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<Session::MainWindow> 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<MainWindow *>(item->data(0, Qt::UserRole).value<void *>());
                windows.append(window->serialize());
            }
        }

        JsonSession session(windows);
        output.write(session.serialize());
        output.close();
    }
}