// Copyright © 2010-2012 sss // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); #ifdef MINIMAL ui->halt->hide(); ui->reboot->hide(); ui->reboot->hide(); ui->restart_cups->hide(); ui->restart_ppp->hide(); ui->state_lbl->hide(); this->setFixedSize(ui->restart_vbox_btn->geometry().width() +17, ui->restart_vbox_btn->geometry().height() +17); #else this->setFixedSize(this->size()); #endif ui->state_lbl->setText(" "); } QSslSocket *MainWindow::get_socket() { QSslSocket *s = new QSslSocket; QFile in(":/res/ca.crt"); in.open(QIODevice::ReadOnly); QByteArray buf = in.readAll(); in.close(); QList ca_list; ca_list.push_back(QSslCertificate(buf)); s->setCaCertificates(ca_list); in.setFileName(":/res/client.crt"); in.open(QIODevice::ReadOnly); buf = in.readAll(); in.close(); QSslCertificate c(buf); s->setLocalCertificate(c); in.setFileName(":/res/client.key"); in.open(QIODevice::ReadOnly); buf = in.readAll(); QSslKey key(buf, QSsl::Rsa); s->setPrivateKey(key); s->setPeerVerifyMode(QSslSocket::VerifyPeer); s->setProtocol(QSsl::SslV3); connect(s, SIGNAL(peerVerifyError(QSslError)), this, SLOT(ssl_verify_error_handler(QSslError))); return s; } void MainWindow::ssl_verify_error_handler(const QSslError error) { switch(error.error()) { case QSslError::InvalidCaCertificate: case QSslError::NoPeerCertificate: case QSslError::UnspecifiedError: case QSslError::AuthorityIssuerSerialNumberMismatch: ui->state_lbl->setText(QString::fromUtf8("Ошибка соединения !")); sock->abort(); return; default: break; } sock->ignoreSslErrors(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::enable_buttons(bool enable) { ui->restart_vbox_btn->setEnabled(enable); ui->reboot->setEnabled(enable); ui->halt->setEnabled(enable); ui->restart_cups->setEnabled(enable); ui->restart_ppp->setEnabled(enable); ui->restart_vbox_btn->setEnabled(enable); } void MainWindow::on_restart_vbox_btn_clicked() { ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); sock = get_socket(); ui->state_lbl->setText(QString::fromUtf8("Соединение..")); sock->connectToHostEncrypted(host, 1313); connect(sock, SIGNAL(encrypted()), SLOT(restart_vbox_sock_connected())); connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); } void MainWindow::restart_vbox_sock_connected() { ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); enable_buttons(false); sock->write("restart vbox\0"); } void MainWindow::reboot() { ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); enable_buttons(false); sock->write("reboot now\0"); } void MainWindow::halt() { ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); enable_buttons(false); sock->write("halt now\0"); } void MainWindow::restart_cups() { ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); enable_buttons(false); sock->write("restart cups\0"); } void MainWindow::restart_ppp() { ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); enable_buttons(false); sock->write("restart ppp\0"); } void MainWindow::disconnected() { enable_buttons(true); ui->state_lbl->setText(QString::fromUtf8(" ")); sock->deleteLater(); } void MainWindow::on_reboot_clicked() { #ifndef MINIMAL QMessageBox msg; msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(msg.exec() == QMessageBox::Yes) { QMessageBox msg; msg.setText(QString::fromUtf8("Уверены что хотите перезагрузить сервер ?")); msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(msg.exec() == QMessageBox::Yes) { ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); sock = get_socket(); ui->state_lbl->setText(QString::fromUtf8("Соединение..")); sock->connectToHostEncrypted(host, 1313); connect(sock, SIGNAL(encrypted()), SLOT(reboot())); connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); } } #endif } void MainWindow::on_halt_clicked() { #ifndef MINIMAL QMessageBox msg; msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(msg.exec() == QMessageBox::Yes) { QMessageBox msg; msg.setText(QString::fromUtf8("Уверены что хотите выключить сервер ?")); msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(msg.exec() == QMessageBox::Yes) { ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); sock = get_socket(); ui->state_lbl->setText(QString::fromUtf8("Соединение..")); sock->connectToHostEncrypted(host, 1313); connect(sock, SIGNAL(encrypted()), SLOT(halt())); connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); } } #endif } void MainWindow::on_restart_cups_clicked() { #ifndef MINIMAL ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); sock = get_socket(); ui->state_lbl->setText(QString::fromUtf8("Соединение..")); sock->connectToHostEncrypted(host, 1313); connect(sock, SIGNAL(encrypted()), SLOT(restart_cups())); connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); #endif } void MainWindow::on_restart_ppp_clicked() { #ifndef MINIMAL ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); sock = get_socket(); ui->state_lbl->setText(QString::fromUtf8("Соединение..")); sock->connectToHostEncrypted(host, 1313); connect(sock, SIGNAL(encrypted()), SLOT(restart_ppp())); connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); #endif }