aboutsummaryrefslogtreecommitdiff
path: root/src/session/sessiondialog.cpp
blob: b73408882c7066deff9d2d21d3cad93ba38082d0 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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 "sessiondialog.h"
#include "../browser.h"
#include "../util.h"
#include "sessionform.h"
#include "ui_sessiondialog.h"
#include "ui_sessionform.h"
#include <QFile>
#include <QFileDialog>
#include <QToolButton>
#include <QStyle>

SessionDialog::SessionDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SessionDialog)
{
    ui->setupUi(this);
    ui->open->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon));

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

    for(const QString &path : Util::files(browser->configuration("browser.session.path"), { "*.json" })) {
        addItem(path);
    }

    ui->listWidget->addItem(tr("Create a new window"));

    connect(ui->listWidget, &QListWidget::currentItemChanged, this, [this](QListWidgetItem *currentItem, QListWidgetItem *previousItem) {
        auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(currentItem));
        if(currentWidget != nullptr)
            currentWidget->ui->delete_toolButton->show();

        auto *previousWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(previousItem));
        if(previousWidget != nullptr)
            previousWidget->ui->delete_toolButton->hide();
    });

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

    accepted_connection = connect(this, &SessionDialog::accepted, this, [this, browser]() {
        auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(ui->listWidget->currentItem()));
        if(currentWidget != nullptr)
            this->openSession(currentWidget->ui->label->text());
        else
            browser->createWindow();
    });

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

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

std::optional<QJsonObject> SessionDialog::pickSession()
{
    disconnect(accepted_connection);

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

    auto *currentWidget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(ui->listWidget->currentItem()));
    if(currentWidget != nullptr) {
        QFile json(currentWidget->ui->label->text());
        if(json.open(QIODevice::ReadOnly | QIODevice::Text)) {
            auto doc = QJsonDocument::fromJson(json.readAll());
            return doc.object();
        }
    }

    return std::nullopt;
}

QListWidgetItem *SessionDialog::addItem(const QString &path)
{
    auto *item = new QListWidgetItem(ui->listWidget);
    auto *widget = new SessionForm(path, this);
    connect(widget->ui->delete_toolButton, &QToolButton::clicked, this, [item, widget]() {
        delete item;
        delete widget;
    });
    ui->listWidget->setItemWidget(item, widget);
    return item;
}

void SessionDialog::search(const QString &text)
{
    for(int i = 0; i < ui->listWidget->count(); ++i) {
        auto *item = ui->listWidget->item(i);
        auto *widget = qobject_cast<SessionForm *>(ui->listWidget->itemWidget(item));

        if(widget == nullptr)
            item->setHidden(!text.isEmpty());
        else
            item->setHidden(!widget->ui->label->text().contains(text));
    }
}

void SessionDialog::openSession(const QString &filename)
{
    auto *browser = qobject_cast<Browser *>(qApp);
    Q_CHECK_PTR(browser);

    QFile json(filename);
    if(json.open(QIODevice::ReadOnly | QIODevice::Text)) {
        auto doc = QJsonDocument::fromJson(json.readAll());
        Session::restoreSession(doc.object());
        json.close();
    }
}