aboutsummaryrefslogtreecommitdiff
path: root/src/session/savesessiondialog.cpp
blob: ffc53f78355a4b5cacaa184cf24e9ac67881fd7b (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://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "savesessiondialog.h"
#include "browser.h"
#include "mainwindow/mainwindow.h"
#include "webengine/webprofilemanager.h"
#include "subwindow/subwindow.h"
#include "ui_savesessiondialog.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, browser->getProfileManager()->id(subwindow->profile()));

            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, browser->getProfileManager()->id(view->profile()));
            }
        }
    }
}

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<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 *>());
                Q_CHECK_PTR(window);
                windows.append(window);
            }
        }

        auto data = Session::_session(windows);
        output.write(QJsonDocument(data).toJson());
        output.close();
    }
}