summaryrefslogtreecommitdiffstats
path: root/src/dialog.cpp
blob: 8ff215e1fd64de1c4bc19d952a7825524630af12 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "dialog.h"

#include <QMessageBox>
#include <QDebug>
#include <QRegExp>
#include <QFile>
#include <QProcess>
#include <QTimer>
#include <QDesktopWidget>

#include "ui_dialog.h"
#include "save_restore_session.h"
#include "sessiontreeitem.h"
#include "globals.h"

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

    pvsSettings_ = NULL;
    ui->PVSOptionsGroupBox->hide();

    // Re-center dialog every second to account for resolution changes
    QRect desktopRect = QApplication::desktop()->availableGeometry(this);
    oldCenter_ = desktopRect.center();
    centerTimer_ = new QTimer(this);
    connect(centerTimer_, SIGNAL(timeout()), this, SLOT(on_centerTimer()));
    centerTimer_->start(1000);
}

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

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) {
    // this method gets called when a Session has been activated

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

    const Session* s(item->session());
    if (!s) {
        // no valid session has been selected, do nothing
        return;
    }
	
    // Run session start script
    if (QFile::exists(sessionStartScript)) {
        QProcess scriptProcess;
        scriptProcess.start(sessionStartScript, QIODevice::ReadOnly);
        scriptProcess.waitForFinished();
        scriptProcess.close();
    }

    if (s->run()) {
        writePVSSettings();
        writeSessionName(s->shortDescription());
        setVisible(false);

    } else {
        QMessageBox::warning(
                this, trUtf8("vmchooser"),
                trUtf8("Vmchooser failed to run the selected session!"));
    }
}

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

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

void Dialog::on_pushButtonStart_clicked() {
    this->on_treeView_activated(ui->treeView->selectionModel()->currentIndex());
}

void Dialog::readPVSSettings() {
    if (!pvsSettings_) return;
    QString value;

    value = pvsSettings_->value("Permissions/vnc_lecturer").toString();
    if (value == "rw") {
        ui->comboBoxLecturer->setCurrentIndex(2);
    } else if (value == "ro") {
        ui->comboBoxLecturer->setCurrentIndex(1);
    } else {
        ui->comboBoxLecturer->setCurrentIndex(0);
    }

    value = pvsSettings_->value("Permissions/vnc_other").toString();
    if (value == "rw") {
        ui->comboBoxOthers->setCurrentIndex(2);
    } else if (value == "ro") {
        ui->comboBoxOthers->setCurrentIndex(1);
    } else {
        ui->comboBoxOthers->setCurrentIndex(0);
    }
}

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

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

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

void Dialog::on_comboBoxOthers_currentIndexChanged(int index) {
    // TODO (Jan): 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());
}

void Dialog::showSettingsPVS() {
    pvsSettings_ = new QSettings("openslx", "pvs", this);
    QStringList accessOptions;
    accessOptions <<trUtf8("None") << trUtf8("View Only") << trUtf8("Full");
    ui->comboBoxLecturer->insertItems(0, accessOptions);
    ui->comboBoxOthers->insertItems(0, accessOptions);
    readPVSSettings();
    ui->PVSOptionsGroupBox->show();
}

void Dialog::setTheme() {
	QString label_l_style, label_r_style;
	QString backgroundColor, imageLeft, imageRight;
	QString themePathBase, themePathIni, themePathImgLeft, themePathImgRight;

	if (theme.isEmpty()) return;

	themePathBase = QString("%1/%2/").arg(VMCHOOSER_THEME_BASE).arg(theme);
	themePathIni = QString("%1%2.ini").arg(themePathBase).arg(theme);

	if (!QFile::exists(themePathIni)) return;


	QSettings themeSettings(themePathIni, QSettings::IniFormat);
	backgroundColor = themeSettings.value("background-color").toString();
	imageLeft = themeSettings.value("image-left").toString();
	imageRight = themeSettings.value("image-right").toString();

	themePathImgLeft = QString("%1%2").arg(themePathBase).arg(imageLeft);
	themePathImgRight = QString("%1%2").arg(themePathBase).arg(imageRight);

	QRegExp re;

	ui->label_l->setPixmap(QPixmap(themePathImgLeft));
	ui->label_r->setPixmap(QPixmap(themePathImgRight));
	label_l_style = ui->label_l->styleSheet();
	label_r_style = ui->label_r->styleSheet();
	backgroundColor.prepend("\\1").append("\\2");
	label_l_style.replace(QRegExp("(.*background-color:)#[^;]*(;.*)"), backgroundColor);
	label_r_style.replace(QRegExp("(.*background-color:)#[^;]*(;.*)"), backgroundColor);
    //qDebug() << label_r_style << label_l_style;
	ui->label_l->setStyleSheet(label_l_style);
	ui->label_r->setStyleSheet(label_r_style);
}

void Dialog::on_centerTimer() {
    // center dialog on primary screen
    QRect desktopRect = QApplication::desktop()->availableGeometry(this);
    QPoint center = desktopRect.center();
    if (center != oldCenter_) {
        this->move(center.x() - this->width() / 2, center.y() - this->height() / 2);
        oldCenter_ = center;
    }
}