summaryrefslogtreecommitdiffstats
path: root/src/dialog.cpp
blob: 2d6fecff3cb59013a96c1c9c105c9c7bd478d84f (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "dialog.h"
#include "ui_dialog.h"
#include "save_restore_session.h"
#include "sessiontreeitem.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::Dialog) {
    model_ = new SessionTreeModel(parent);
    ui->setupUi(this);

    QDBusConnection dbus = QDBusConnection::sessionBus();
    ifaceDBus_ = new OrgOpenslxPvsInterface("org.openslx.pvs", "/", dbus, this);

    if (ifaceDBus_->isValid()) {
        // PVS is running
        QStringList accessOptions;
        accessOptions << trUtf8("None") << trUtf8("View Only") << trUtf8("Full");
        ui->comboBoxLecturer->insertItems(0, accessOptions);
        ui->comboBoxOthers->insertItems(0, accessOptions);

        readPVSSettings();
        ui->PVSOptionsGroupBox->show();
    } else {
        ui->PVSOptionsGroupBox->hide();
    }
}

Dialog::~Dialog() {
    delete ui;
    delete model_;
    delete ifaceDBus_;
}

void Dialog::changeEvent(QEvent *e) {
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Dialog::on_treeView_activated(QModelIndex index) {
    // TODO handle failures
    // TODO get rid of this->entries, storing them in the model should be enough
    // alternatively use references instead of copies?

    SessionTreeItem* item =
            static_cast<SessionTreeItem*>(index.internalPointer());

    const Session* s(item->session());
    if (!s) {
        return;
    }
    if (s->run()) {
        if (ifaceDBus_->isValid()) {
            writePVSSettings();
        }
        writeSessionName(s->shortDescription());
        close();
    } else {
        // TODO: error instead of close
        close();
    }
}

void Dialog::addItems(const QList<Session*>& entries, const QString& section) {
    this->model_->addItems(entries, section);

    // TODO: do this only once?
    ui->treeView->setModel(model_);
    ui->treeView->expandAll();
}

void Dialog::on_pushButtonAbort_clicked() {
    close();
}

void Dialog::on_pushButtonStart_clicked() {
    // TODO: check if a model is selected
    this->on_treeView_activated(ui->treeView->selectionModel()->currentIndex());
}

void Dialog::readPVSSettings() {
    QDBusPendingReply<QString> reply;

    reply = ifaceDBus_->getConfigValue("Permissions/vnc_lecturer");
    reply.waitForFinished();
    if (reply.isValid()) {
        if (reply.value() == "rw") {
            ui->comboBoxLecturer->setCurrentIndex(2);
        } else if (reply.value() == "ro") {
            ui->comboBoxLecturer->setCurrentIndex(1);
        } else {
            ui->comboBoxLecturer->setCurrentIndex(0);
        }
    }

    reply = ifaceDBus_->getConfigValue("Permissions/vnc_other");
    reply.waitForFinished();
    if (reply.isValid()) {
        if (reply.value() == "rw") {
            ui->comboBoxOthers->setCurrentIndex(2);
        } else if (reply.value() == "ro") {
            ui->comboBoxOthers->setCurrentIndex(1);
        } else {
            ui->comboBoxOthers->setCurrentIndex(0);
        }
    }

    reply = ifaceDBus_->getConfigValue("Permissions/allow_chat");
    reply.waitForFinished();
    if (reply.isValid())
        ui->checkBoxChat->setChecked(reply.value() == "T");

    reply = ifaceDBus_->getConfigValue("Permissions/allow_filetransfer");
    reply.waitForFinished();
    if (reply.isValid())
        ui->checkBoxFileTransfer->setChecked(reply.value() == "T");
}

void Dialog::writePVSSettings() {
    int accessLecturer = ui->comboBoxLecturer->currentIndex();
    if (accessLecturer == 2) {
        ifaceDBus_->setConfigValue("Permissions/vnc_lecturer", "rw");
    } else if (accessLecturer == 1) {
        ifaceDBus_->setConfigValue("Permissions/vnc_lecturer", "ro");
    } else {
        ifaceDBus_->setConfigValue("Permissions/vnc_lecturer", "no");
    }

    int accessOthers = ui->comboBoxOthers->currentIndex();
    if (accessOthers == 2) {
        ifaceDBus_->setConfigValue("Permissions/vnc_other", "rw");
    } else if (accessOthers == 1) {
        ifaceDBus_->setConfigValue("Permissions/vnc_other", "ro");
    } else {
        ifaceDBus_->setConfigValue("Permissions/vnc_other", "no");
    }

    ifaceDBus_->setConfigValue("Permissions/allow_chat",
            QString(ui->checkBoxChat->isChecked() ? "T" : "F"));
    ifaceDBus_->setConfigValue("Permissions/allow_filetransfer",
            QString(ui->checkBoxFileTransfer->isChecked() ? "T" : "F"));
}

void Dialog::on_comboBoxLecturer_currentIndexChanged(int index) {
    // TODO: may others have more access than lecturer?
    if (index < ui->comboBoxOthers->currentIndex()) {
        ui->comboBoxOthers->setCurrentIndex(index);
    }
}

void Dialog::on_comboBoxOthers_currentIndexChanged(int index) {
    // TODO: may others have more access than lecturer?
    if (index > ui->comboBoxLecturer->currentIndex()) {
        ui->comboBoxLecturer->setCurrentIndex(index);
    }
}

void Dialog::selectSession(const QString& name) {
    QModelIndex root(ui->treeView->rootIndex());

    for (int i = 0; i < model_->rowCount(root); ++i) {
        QModelIndex section = model_->index(i, 0, root);
        if (!section.isValid()) break;
        for (int row = 0; row < model_->rowCount(section); ++row) {
            QModelIndex index = model_->index(row, 0, section);
            if (!index.isValid()) break;

            SessionTreeItem* item =
                    static_cast<SessionTreeItem*>(index.internalPointer());
            const Session* s(item->session());
            if (!s) continue;
            if (s->shortDescription() == name) {
                ui->treeView->selectionModel()
                        ->setCurrentIndex(index, QItemSelectionModel::Select);
                return;
            }
        }
    }
}

void Dialog::selectPreviousSession() {
    selectSession(readSessionName());
}