summaryrefslogtreecommitdiffstats
path: root/client/browser/src
diff options
context:
space:
mode:
Diffstat (limited to 'client/browser/src')
-rw-r--r--client/browser/src/mainwindow.cpp258
-rw-r--r--client/browser/src/mainwindow.h13
2 files changed, 179 insertions, 92 deletions
diff --git a/client/browser/src/mainwindow.cpp b/client/browser/src/mainwindow.cpp
index 2e799c5e..1d89a1a8 100644
--- a/client/browser/src/mainwindow.cpp
+++ b/client/browser/src/mainwindow.cpp
@@ -12,10 +12,10 @@
#include <QDateTime>
#include <QProgressBar>
#include <QTabWidget>
-#include <QWebView>
#include <QLineEdit>
#include <QNetworkReply>
#include <QSslError>
+#include <QTimer>
#include <libintl.h>
#include "qtermwidget.h"
@@ -24,7 +24,7 @@
#define BUFFERSIZE 2048
#define REGEXP_STRING "^\\[(\\d+)\\]"
-#define CURRENT_TIME() QDateTime::currentDateTime().toString("dd/MM/yy hh:mm:ss")
+#define CURRENT_TIME() QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss")
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),m_web(new QWebView()),m_output(new QTextEdit()),
@@ -33,15 +33,28 @@ MainWindow::MainWindow(QWidget *parent)
{
// Graphic
showFullScreen();
-
setWindowTitle(tr("OpenGnsys Browser"));
-
setCentralWidget(m_web);
-
readEnvironmentValues();
+ // Open the log file for append
+ if(m_env.contains("OGLOGFILE") && m_env["OGLOGFILE"]!="")
+ {
+ QFile* m_logfile=new QFile(m_env["OGLOGFILE"]);
+ if(!m_logfile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
+ {
+ delete m_logfile;
+ print(tr(gettext("El fichero de log no ha podido ser abierto: "))+m_env["OGLOGFILE"]+".");
+ }
+ else
+ {
+ m_logstream=new QTextStream(m_logfile);
+ }
+ }
+
// Output
m_output->setReadOnly(true);
+ m_output->setFontPointSize(16);
// Button Dock
QDockWidget* dock=new QDockWidget();
@@ -56,16 +69,13 @@ MainWindow::MainWindow(QWidget *parent)
button->setFocusPolicy(Qt::TabFocus);
m_tabs->setCornerWidget(button);
m_tabs->setFocusPolicy(Qt::NoFocus);
-
m_tabs->addTab(m_output,tr(gettext("Salida")));
slotCreateTerminal();
-
// Assign tabs to dock
dock->setWidget(m_tabs);
-
// Assign tabs dock to the mainwindow if admin mode is active
if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
- addDockWidget(Qt::BottomDockWidgetArea,dock);
+ addDockWidget(Qt::BottomDockWidgetArea,dock);
// Top Dock
dock=new QDockWidget();
@@ -73,16 +83,13 @@ MainWindow::MainWindow(QWidget *parent)
QWidget* dummy2=new QWidget();
dummy2->setMaximumHeight(0);
dock->setTitleBarWidget(dummy2);
-
// WebBar
m_webBar=new QLineEdit(dock);
-
// WebBar to dock
dock->setWidget(m_webBar);
-
// Assign top dock to the mainwindow if admin mode is active
if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
- addDockWidget(Qt::TopDockWidgetArea,dock);
+ addDockWidget(Qt::TopDockWidgetArea,dock);
// Status bar
QStatusBar* st=statusBar();
@@ -91,9 +98,9 @@ MainWindow::MainWindow(QWidget *parent)
m_logo=new QLabel();
QPixmap logo;
if(logo.load("/opt/opengnsys/lib/pictures/oglogo.png"))
- m_logo->setPixmap(logo);
+ m_logo->setPixmap(logo);
else
- m_logo->setText("OG");
+ m_logo->setText("OG");
m_logo->setToolTip(tr(gettext("Proyecto OpenGnsys"))+"\nhttps://opengnsys.es");
// Progress bar
m_progressBar=new QProgressBar(this);
@@ -103,8 +110,8 @@ MainWindow::MainWindow(QWidget *parent)
m_speedInfo=new QLabel(speed);
m_speedInfo->setAlignment(Qt::AlignCenter);
if(m_env.contains("DEFAULTSPEED") && m_env["DEFAULTSPEED"]!="")
- if(speed.compare(m_env["DEFAULTSPEED"])!=0)
- m_speedInfo->setStyleSheet("background-color: darkred; color: white; font-weight: bold;");
+ if(speed.compare(m_env["DEFAULTSPEED"])!=0)
+ m_speedInfo->setStyleSheet("background-color: darkred; color: white; font-weight: bold;");
// Clock
m_clock=new DigitalClock(this);
@@ -126,10 +133,8 @@ MainWindow::MainWindow(QWidget *parent)
connect(m_process,SIGNAL(started()),this,SLOT(slotProcessStarted()));
connect(m_process,SIGNAL(finished(int,QProcess::ExitStatus)),
this,SLOT(slotProcessFinished(int,QProcess::ExitStatus)));
-
connect(m_process,SIGNAL(error(QProcess::ProcessError)),
this,SLOT(slotProcessError(QProcess::ProcessError)));
-
connect(m_process,SIGNAL(readyReadStandardOutput()),this,SLOT(slotProcessOutput()));
connect(m_process,SIGNAL(readyReadStandardError()),
this,SLOT(slotProcessErrorOutput()));
@@ -138,21 +143,6 @@ MainWindow::MainWindow(QWidget *parent)
connect(button,SIGNAL(clicked()),this,SLOT(slotCreateTerminal()));
connect(m_webBar,SIGNAL(returnPressed()),this,SLOT(slotWebBarReturnPressed()));
- // Open the log file for append
- if(m_env.contains("OGLOGFILE") && m_env["OGLOGFILE"]!="")
- {
- QFile* m_logfile=new QFile(m_env["OGLOGFILE"]);
- if(!m_logfile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
- {
- delete m_logfile;
- print(tr(gettext("El fichero de log no ha podido ser abierto: "))+m_env["OGLOGFILE"]+".");
- }
- else
- {
- m_logstream=new QTextStream(m_logfile);
- }
- }
-
QStringList arguments=QCoreApplication::arguments();
m_webBar->setText(arguments[1]);
m_web->load(QUrl(arguments[1]));
@@ -172,35 +162,64 @@ MainWindow::~MainWindow()
void MainWindow::slotLinkHandle(const QUrl &url)
{
// Check if it's executing another process
- if(m_process->state()!=QProcess::NotRunning)
+ if(m_process->state() != QProcess::NotRunning)
{
- print(tr(gettext("Hay otro proceso en ejecución. Por favor espere.")));
- return;
+ print(tr(gettext("Hay otro proceso en ejecución. Por favor espere.")));
+ return;
}
-
QString urlString = url.toString();
- if(urlString.startsWith(COMMAND))
+ QString urlScheme = url.scheme();
+ // Clear the output widget for a normal user
+ if(! m_env.contains("ogactiveadmin") || m_env["ogactiveadmin"] != "true")
{
- // For COMMAND link, execute
- executeCommand(urlString.remove(0,QString(COMMAND).length()));
+ m_output->clear();
}
- else if(urlString.startsWith(COMMAND_WITH_CONFIRMATION))
+ if(urlScheme == COMMAND_CONFIRM || urlScheme == COMMAND_CONFIRM_OUTPUT ||
+ urlScheme == COMMAND_OUTPUT_CONFIRM || urlScheme == COMMAND_WITH_CONFIRMATION)
{
- // For COMMAND_WITH_CONFIRMATION link, show confirmation box and execute, if accepted
+ // For all command with confirmation links, show a popup box
QMessageBox msgBox;
- msgBox.setIcon(QMessageBox::Question);
+ msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
msgBox.setWindowTitle(tr(gettext("AVISO")));
+ msgBox.setIcon(QMessageBox::Question);
msgBox.setTextFormat(Qt::RichText);
msgBox.setText(tr(gettext("La siguiente acci&oacute;n puede modificar datos o tardar varios minutos. El equipo no podr&aacute; ser utilizado durante su ejecuci&oacute;n.")));
QPushButton *execButton = msgBox.addButton(tr(gettext("Ejecutar")), QMessageBox::ActionRole);
msgBox.addButton(tr(gettext("Cancelar")), QMessageBox::RejectRole);
msgBox.setDefaultButton(execButton);
msgBox.exec();
+ // Continue if user press the execution button
if (msgBox.clickedButton() == execButton)
{
- executeCommand(urlString.remove(0,QString(COMMAND_WITH_CONFIRMATION).length()));
+ // For command with confirmation and output link, show an output window to non-admin user
+ if((urlScheme == COMMAND_CONFIRM_OUTPUT || urlScheme == COMMAND_OUTPUT_CONFIRM) &&
+ (! m_env.contains("ogactiveadmin") || m_env["ogactiveadmin"] != "true"))
+ {
+ int w=MainWindow::width(), h=MainWindow::height();
+ m_output->setWindowFlags(Qt::Window);
+ m_output->move(100, 100);
+ m_output->setFixedSize(w*0.8-100, h*0.8-100);
+ m_output->show();
+ }
+ // Execute the command
+ executeCommand(urlString.remove(0, urlScheme.length()+1));
}
}
+ else if(urlScheme == COMMAND || urlScheme == COMMAND_OUTPUT)
+ {
+ // For command with output link, show an output window to non-admin user
+ if(urlScheme == COMMAND_OUTPUT &&
+ (! m_env.contains("ogactiveadmin") || m_env["ogactiveadmin"] != "true"))
+ {
+ int w=MainWindow::width(), h=MainWindow::height();
+ m_output->setWindowFlags(Qt::Window);
+ m_output->move(100, 100);
+ m_output->setFixedSize(w*0.8-100, h*0.8-100);
+ m_output->show();
+ }
+ // Execute the command
+ executeCommand(urlString.remove(0, urlScheme.length()+1));
+ }
else
{
// For other link, load webpage
@@ -227,14 +246,14 @@ void MainWindow::slotWebLoadFinished(bool ok)
if(ok == false)
{
QMessageBox msgBox;
- msgBox.setIcon(QMessageBox::Question);
+ msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
msgBox.setWindowTitle(tr(gettext("AVISO")));
+ msgBox.setIcon(QMessageBox::Question);
msgBox.setTextFormat(Qt::RichText);
msgBox.setText(tr(gettext("La p&aacute;gina no se puede cargar.")));
QPushButton *reloadButton = msgBox.addButton(tr(gettext("Recargar")), QMessageBox::ActionRole);
msgBox.addButton(tr(gettext("Abortar")), QMessageBox::RejectRole);
-
msgBox.exec();
if (msgBox.clickedButton() == reloadButton)
@@ -264,7 +283,6 @@ void MainWindow::slotSslErrors(QNetworkReply* reply)
void MainWindow::slotProcessStarted()
{
- m_output->insertPlainText(tr(gettext("Lanzado satisfactoriamente.")));
startProgressBar();
}
@@ -275,7 +293,11 @@ void MainWindow::slotProcessOutput()
while((m_process->readLine(buf,BUFFERSIZE) > 0))
{
QString s(buf);
- print(tr("Proc. stdout: ")+s);
+ if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
+ {
+ m_output->insertPlainText(tr("Proc. stdout: "));
+ }
+ print(s);
captureOutputForStatusBar(s);
}
}
@@ -287,39 +309,65 @@ void MainWindow::slotProcessErrorOutput()
while((m_process->readLine(buf,BUFFERSIZE) > 0))
{
QString s(buf);
- m_output->insertPlainText(tr("Proc. stderr: "));
+ if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
+ {
+ m_output->insertPlainText(tr("Proc. stderr: "));
+ }
m_output->setTextColor(QColor(Qt::darkBlue));
print(s);
m_output->setTextColor(QColor(Qt::black));
}
}
-void MainWindow::slotProcessFinished(int code,QProcess::ExitStatus status)
+void MainWindow::slotProcessFinished(int code, QProcess::ExitStatus status)
{
- if(status==QProcess::NormalExit)
+ if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
{
- print(tr(gettext("Proceso acabado correctamente. Valor de retorno: "))+QString::number(code));
+ // Admin user: show process status
+ if(status==QProcess::NormalExit)
+ {
+ if(code > 0)
+ {
+ m_output->setTextColor(QColor(Qt::darkRed));
+ }
+ print("\n"+tr(gettext("Fin del proceso. Valor de retorno: "))+QString::number(code));
+ }
+ else
+ {
+ m_output->setTextColor(QColor(Qt::darkRed));
+ print("\n"+tr(gettext("El proceso ha fallado inesperadamente. Salida: ")+code));
+ }
+ m_output->setTextColor(QColor(Qt::black));
}
else
{
- print(tr(gettext("El proceso ha fallado inesperadamente. Salida: ")+code));
+ // Non-admin user: show instruction to close the popup window
+ write(tr(gettext("Fin del proceso. Valor de retorno: "))+QString::number(code));
+ m_output->setFontUnderline(true);
+ print("\n\n"+tr(gettext("AVISO: Pulsar el botón superior derecho para cerrar"))+" [X]");
+ m_output->setFontUnderline(false);
+ }
+ // On error, show a message box
+ if(code > 0 && ! m_output->isActiveWindow())
+ {
+ showErrorMessage(gettext("Código de salida: ")+QString::number(code));
}
finishProgressBar();
}
void MainWindow::slotProcessError(QProcess::ProcessError error)
{
- m_output->setTextColor(QColor(Qt::darkRed));
+ QString errorMsg;
switch(error)
{
case QProcess::FailedToStart:
- print(tr(gettext("Imposible lanzar el proceso.")));
+ errorMsg=tr(gettext("Imposible lanzar el proceso."));
break;
case QProcess::WriteError:
- print(tr(gettext("Error de escritura en el proceso.")));
+ errorMsg=tr(gettext("Error de escritura en el proceso."));
break;
case QProcess::ReadError:
- print(tr(gettext("Error de lectura del proceso.")));
+ errorMsg=tr(gettext("Error de lectura del proceso."));
break;
// No capturo crashed porque la pillo por finished
case QProcess::Crashed:
@@ -327,11 +375,17 @@ void MainWindow::slotProcessError(QProcess::ProcessError error)
break;
case QProcess::UnknownError:
default:
- print(tr(gettext("Error desconocido.")));
+ errorMsg=tr(gettext("Error desconocido."));
break;
}
+ // Print error and show message box with timeout.
+ if(!errorMsg.isNull()) {
+ m_output->setTextColor(QColor(Qt::darkRed));
+ print(errorMsg);
+ m_output->setTextColor(QColor(Qt::black));
+ showErrorMessage(errorMsg);
+ }
finishProgressBar();
- m_output->setTextColor(QColor(Qt::black));
}
void MainWindow::slotCreateTerminal()
@@ -343,8 +397,6 @@ void MainWindow::slotCreateTerminal()
console->setTerminalFont(font);
console->setFocusPolicy(Qt::StrongFocus);
-
- //console->setColorScheme(COLOR_SCHEME_BLACK_ON_LIGHT_YELLOW);
console->setScrollBarPosition(QTermWidget::ScrollBarRight);
++m_numberTerminal;
@@ -367,7 +419,7 @@ void MainWindow::slotWebBarReturnPressed()
{
QUrl url(m_webBar->text());
if(url.isValid())
- slotLinkHandle(url);
+ slotLinkHandle(url);
}
int MainWindow::readEnvironmentValues()
@@ -403,32 +455,42 @@ int MainWindow::readEnvironmentValues()
return ret;
}
+// Write a string to the log file
+void MainWindow::write(QString s)
+{
+ if(! s.endsWith("\n"))
+ s+="\n";
+ if(m_logstream)
+ {
+ *m_logstream<<CURRENT_TIME()<<": browser: "<<s;
+ m_logstream->flush();
+ }
+}
+
+// Print and log a string
void MainWindow::print(QString s)
{
- if(!s.endsWith("\n"))
- s+="\n";
- if(m_logstream)
- {
- *m_logstream<<CURRENT_TIME()<<": "<<s;
- m_logstream->flush();
- }
- if(m_output)
- m_output->insertPlainText(s);
+ if(! s.endsWith("\n"))
+ s+="\n";
+ write(s);
+ if(m_output)
+ m_output->insertPlainText(s);
}
+// Show message in status bar
void MainWindow::captureOutputForStatusBar(QString output)
{
- // Capturar para modificar status bar
- output=output.trimmed();
-
- QRegExp regexp(REGEXP_STRING);
- if(regexp.indexIn(output) != -1)
- {
- int pass=regexp.cap(1).toInt();
- output.replace(regexp,"");
- m_progressBar->setValue(pass);
- m_progressBar->setFormat("%p%"+output);
- }
+ // Modify the status bar
+ output=output.trimmed();
+ // Get percentage (string starts with "[Number]")
+ QRegExp regexp(REGEXP_STRING);
+ if(regexp.indexIn(output) != -1)
+ {
+ int pass=regexp.cap(1).toInt();
+ output.replace(regexp,"");
+ m_progressBar->setValue(pass);
+ m_progressBar->setFormat("%p%"+output);
+ }
}
// Init status bar
@@ -445,13 +507,14 @@ void MainWindow::startProgressBar()
m_web->setEnabled(false);
}
+// Reset status bar
void MainWindow::finishProgressBar()
{
m_progressBar->reset();
m_web->setEnabled(true);
}
-
+// Execute a command
void MainWindow::executeCommand(QString &string)
{
QStringList list=string.split(" ",QString::SkipEmptyParts);
@@ -460,10 +523,17 @@ void MainWindow::executeCommand(QString &string)
// Assign the same Browser's environment to the process
m_process->setEnvironment(QProcess::systemEnvironment());
m_process->start(program,list);
- print(tr(gettext("Lanzando el comando: ")));
- m_output->setTextColor(QColor(Qt::darkGreen));
- print(program+" "+list.join(" "));
- m_output->setTextColor(QColor(Qt::black));
+ // Only show the command line to admin user
+ if(m_env.contains("ogactiveadmin") && m_env["ogactiveadmin"] == "true")
+ {
+ m_output->setTextColor(QColor(Qt::darkGreen));
+ print(tr(gettext("Lanzando el comando: "))+string);
+ m_output->setTextColor(QColor(Qt::black));
+ }
+ else
+ {
+ write(tr(gettext("Lanzando el comando: "))+string);
+ }
startProgressBar();
}
@@ -484,3 +554,15 @@ QString MainWindow::readSpeed() {
return QString("");
}
}
+
+// Show an error box with timeout
+void MainWindow::showErrorMessage(QString text)
+{
+ QMessageBox* msgBox=new QMessageBox();
+ msgBox->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
+ msgBox->setWindowTitle(gettext("ERROR"));
+ msgBox->setIcon(QMessageBox::Warning);
+ msgBox->setText(text);
+ msgBox->show();
+ QTimer::singleShot(5000, msgBox, SLOT(close()));
+}
diff --git a/client/browser/src/mainwindow.h b/client/browser/src/mainwindow.h
index e4e02fa3..664d2f00 100644
--- a/client/browser/src/mainwindow.h
+++ b/client/browser/src/mainwindow.h
@@ -1,8 +1,12 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
-#define COMMAND "command:"
-#define COMMAND_WITH_CONFIRMATION "commandwithconfirmation:"
+#define COMMAND "command"
+#define COMMAND_CONFIRM "command+confirm"
+#define COMMAND_WITH_CONFIRMATION "commandwithconfirmation" // Backwards compatibility
+#define COMMAND_OUTPUT "command+output"
+#define COMMAND_CONFIRM_OUTPUT "command+confirm+output"
+#define COMMAND_OUTPUT_CONFIRM "command+output+confirm"
#define ENVIRONMENT "OGLOGFILE,ogactiveadmin,DEFAULTSPEED"
#include <QWidget>
@@ -41,7 +45,7 @@ class MainWindow : public QMainWindow
void slotWebLoadStarted();
void slotWebLoadFinished(bool ok);
void slotWebLoadProgress(int progress);
- void slotSslErrors(QNetworkReply* reply);
+ void slotSslErrors(QNetworkReply* reply);
// Funciones que manejan cada vez que el proceso hace algo
void slotProcessStarted();
@@ -62,12 +66,13 @@ class MainWindow : public QMainWindow
protected:
int readEnvironmentValues();
void print(QString s);
+ void write(QString s);
void captureOutputForStatusBar(QString output);
void startProgressBar();
void finishProgressBar();
void executeCommand(QString &string);
QString readSpeed();
-
+ void showErrorMessage(QString string);
protected:
QWebView *m_web;