#include "dialog.h"
#include <QMessageBox>
#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) {
// 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;
}
if (s->run()) {
if (ifaceDBus_->isValid()) {
writePVSSettings();
}
writeSessionName(s->shortDescription());
close();
} 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() {
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 (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());
}