aboutsummaryrefslogtreecommitdiff
path: root/src/session/sessiondialog.cpp
blob: 548f73e806da7280b32f703406a3e1b7c14acd19 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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 "sessiondialog.h"
#include "../browser.h"
#include "util.h"
#include "configuration.h"
#include "session_json.hpp"
#include "ui_sessiondialog.h"
#include <QFile>
#include <QFileDialog>
#include <QStyle>
#include <QToolButton>

SessionDialog::SessionDialog(QWidget *parent)
    : QDialog(parent)
    , create_window_str(QObject::tr("Create a new window"))
    , ui(new Ui::SessionDialog)
{
    ui->setupUi(this);

    auto *browser = qobject_cast<Browser *>(qApp);
    Q_CHECK_PTR(browser);

    Configuration conf;
    for(const QString &path : Util::files(conf.value<QString>("session.path").value(), { "*.json" })) {
        ui->listWidget->addItem(path);
    }

    ui->listWidget->addItem(create_window_str);

    connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem * /*previousItem*/) {
        ui->delete_toolButton->setEnabled(currentItem->text() != create_window_str);
    });

    connect(ui->open_toolButton, &QToolButton::clicked, this, [this]() {
        const QString filename = QFileDialog::getOpenFileName(this, tr("Select Session file"), QDir::homePath(), tr("JSON (*.json)"));
        if(!filename.isEmpty()) {
            ui->listWidget->addItem(filename);
            ui->listWidget->setCurrentRow(ui->listWidget->count() - 1);
            accept();
        }
    });

    connect(ui->delete_toolButton, &QToolButton::clicked, this, [this]() {
        const auto path = ui->listWidget->currentItem()->text();
        if(path != create_window_str) {
            QFile(path).remove();
            delete ui->listWidget->currentItem();
        }
    });

    accepted_connection = connect(this, &SessionDialog::accepted, this, [this, browser]() {
        const auto path = ui->listWidget->currentItem()->text();
        if(path != create_window_str) {
            openSession(path);
        } else {
            browser->open({});
        }
    });

    connect(ui->search, &QLineEdit::textEdited, this, &SessionDialog::search);
}

SessionDialog::~SessionDialog()
{
    delete ui;
}

std::optional<QVector<Session::MainWindow>> SessionDialog::pickSession()
{
    disconnect(accepted_connection);

    if(this->exec() == QDialog::Rejected) {
        return std::nullopt;
    }

    const auto path = ui->listWidget->currentItem()->text();
    if(path != create_window_str) {
        QFile json(path);
        if(json.open(QIODevice::ReadOnly | QIODevice::Text)) {
            JsonSession session(json.readAll());
            return session.get();
        }
    }

    return std::nullopt;
}

void SessionDialog::search(const QString &text)
{
    for(int i = 0; i < ui->listWidget->count(); ++i) {
        auto *item = ui->listWidget->item(i);
        item->setHidden(!item->text().contains(text));
    }
}

void SessionDialog::openSession(const QString &filename)
{
    auto *browser = qobject_cast<Browser *>(qApp);
    QFile json(filename);
    if(json.open(QIODevice::ReadOnly | QIODevice::Text) && browser != nullptr) {
        JsonSession session(json.readAll());
        browser->open(session.get());
    }
}