1 | #include "mainwindow.h" |
---|
2 | #include <QtWebKit> |
---|
3 | #include <QStringList> |
---|
4 | #include <QWebView> |
---|
5 | #include <QDockWidget> |
---|
6 | #include <QtDebug> |
---|
7 | #include <QWebPage> |
---|
8 | #include <QProcess> |
---|
9 | #include <QTextEdit> |
---|
10 | #include <QMessageBox> |
---|
11 | #include <QPushButton> |
---|
12 | #include <QDateTime> |
---|
13 | #include <QProgressBar> |
---|
14 | #include <QTabWidget> |
---|
15 | #include <QLineEdit> |
---|
16 | |
---|
17 | #include "qtermwidget.h" |
---|
18 | |
---|
19 | #define BUFFERSIZE 2048 |
---|
20 | #define EXPREG_RESET "^\\[(\\d+),(\\d+)\\]\\n" |
---|
21 | #define EXPREG_A_PASS "^\\[(\\d+)\\]\\s" |
---|
22 | |
---|
23 | #define CURRENT_TIME() QDateTime::currentDateTime().toString("dd/MM/yy hh:mm:ss") |
---|
24 | |
---|
25 | MainWindow::MainWindow(QWidget *parent) |
---|
26 | : QMainWindow(parent),m_web(new QWebView()),m_output(new QTextEdit()), |
---|
27 | m_process(new QProcess(this)), |
---|
28 | m_logfile(0),m_logstream(0),m_numberTerminal(0) |
---|
29 | { |
---|
30 | // Graphic |
---|
31 | showFullScreen(); |
---|
32 | |
---|
33 | setWindowTitle(tr("OpenGNSys Browser")); |
---|
34 | |
---|
35 | setCentralWidget(m_web); |
---|
36 | |
---|
37 | // Output |
---|
38 | m_output->setReadOnly(true); |
---|
39 | |
---|
40 | // Button Dock |
---|
41 | QDockWidget* dock=new QDockWidget(); |
---|
42 | dock->setAllowedAreas(Qt::BottomDockWidgetArea); |
---|
43 | QWidget* dummy=new QWidget(); |
---|
44 | dummy->setMaximumHeight(0); |
---|
45 | dock->setTitleBarWidget(dummy); |
---|
46 | |
---|
47 | // TabWidget |
---|
48 | m_tabs=new QTabWidget(dock); |
---|
49 | QPushButton *button=new QPushButton(tr("&New Term")); |
---|
50 | button->setFocusPolicy(Qt::TabFocus); |
---|
51 | m_tabs->setCornerWidget(button); |
---|
52 | m_tabs->setFocusPolicy(Qt::NoFocus); |
---|
53 | |
---|
54 | m_tabs->addTab(m_output,tr("Output")); |
---|
55 | slotCreateTerminal(); |
---|
56 | |
---|
57 | // Las pestanyas al dock |
---|
58 | dock->setWidget(m_tabs); |
---|
59 | |
---|
60 | // Y el dock al mainwindow |
---|
61 | addDockWidget(Qt::BottomDockWidgetArea,dock); |
---|
62 | |
---|
63 | // Top Dock |
---|
64 | dock=new QDockWidget(); |
---|
65 | dock->setAllowedAreas(Qt::TopDockWidgetArea); |
---|
66 | QWidget* dummy2=new QWidget(); |
---|
67 | dummy2->setMaximumHeight(0); |
---|
68 | dock->setTitleBarWidget(dummy2); |
---|
69 | |
---|
70 | // WebBar |
---|
71 | m_webBar=new QLineEdit(dock); |
---|
72 | |
---|
73 | // WebBar al dock |
---|
74 | dock->setWidget(m_webBar); |
---|
75 | |
---|
76 | // dock al mainwindow |
---|
77 | addDockWidget(Qt::TopDockWidgetArea,dock); |
---|
78 | |
---|
79 | // Status bar |
---|
80 | QStatusBar* st=statusBar(); |
---|
81 | st->setSizeGripEnabled(false); |
---|
82 | m_progressBar=new QProgressBar(this); |
---|
83 | |
---|
84 | m_web->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); |
---|
85 | |
---|
86 | // Web signals |
---|
87 | connect(m_web,SIGNAL(linkClicked(const QUrl&)),this, |
---|
88 | SLOT(slotLinkHandle(const QUrl&))); |
---|
89 | connect(m_web,SIGNAL(loadStarted()),this,SLOT(slotWebLoadStarted())); |
---|
90 | connect(m_web,SIGNAL(loadFinished(bool)),this,SLOT(slotWebLoadFinished(bool))); |
---|
91 | connect(m_web,SIGNAL(loadProgress(int)),this,SLOT(slotWebLoadProgress(int))); |
---|
92 | connect(m_web,SIGNAL(urlChanged(const QUrl&)),this, |
---|
93 | SLOT(slotUrlChanged(const QUrl&))); |
---|
94 | |
---|
95 | // Process signals |
---|
96 | connect(m_process,SIGNAL(started()),this,SLOT(slotProcessStarted())); |
---|
97 | connect(m_process,SIGNAL(finished(int,QProcess::ExitStatus)), |
---|
98 | this,SLOT(slotProcessFinished(int,QProcess::ExitStatus))); |
---|
99 | |
---|
100 | connect(m_process,SIGNAL(error(QProcess::ProcessError)), |
---|
101 | this,SLOT(slotProcessError(QProcess::ProcessError))); |
---|
102 | |
---|
103 | connect(m_process,SIGNAL(readyReadStandardOutput()),this,SLOT(slotProcessOutput())); |
---|
104 | connect(m_process,SIGNAL(readyReadStandardError()), |
---|
105 | this,SLOT(slotProcessErrorOutput())); |
---|
106 | |
---|
107 | // Dock signals |
---|
108 | connect(button,SIGNAL(clicked()),this,SLOT(slotCreateTerminal())); |
---|
109 | connect(m_webBar,SIGNAL(returnPressed()),this,SLOT(slotWebBarReturnPressed())); |
---|
110 | |
---|
111 | if(!readEnvironmentValues()) |
---|
112 | print(tr("Any environment variable/s didn't be setted.")); |
---|
113 | |
---|
114 | if(m_env.contains("OGLOGFILE") && m_env["OGLOGFILE"]!="") |
---|
115 | { |
---|
116 | QFile* file=new QFile(m_env["OGLOGFILE"]); |
---|
117 | if(!file->open(QIODevice::WriteOnly | QIODevice::Text | |
---|
118 | QIODevice::Append)) |
---|
119 | { |
---|
120 | delete file; |
---|
121 | print(tr("The log file couldn't be opened: ")+m_env["OGLOGFILE"]+"."); |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | m_logfile=file; |
---|
126 | m_logstream=new QTextStream(m_logfile); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | QStringList arguments=QCoreApplication::arguments(); |
---|
131 | m_webBar->setText(arguments[1]); |
---|
132 | m_web->load(QUrl(arguments[1])); |
---|
133 | } |
---|
134 | |
---|
135 | MainWindow::~MainWindow() |
---|
136 | { |
---|
137 | if(m_logfile) |
---|
138 | { |
---|
139 | m_logfile->close(); |
---|
140 | delete m_logfile; |
---|
141 | } |
---|
142 | if(m_logstream) |
---|
143 | delete m_logstream; |
---|
144 | } |
---|
145 | |
---|
146 | void MainWindow::slotLinkHandle(const QUrl &url) |
---|
147 | { |
---|
148 | // Si ya hay un proceso ejectuandose |
---|
149 | if(m_process->state()!=QProcess::NotRunning) |
---|
150 | { |
---|
151 | print(tr("There is a process running. Please wait a moment.")); |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | QString urlString = url.toString(); |
---|
156 | // Si es un link del tipo PROTOCOL lo ejecutamos |
---|
157 | if(urlString.startsWith(PROTOCOL)) |
---|
158 | { |
---|
159 | urlString=urlString.remove(0,QString(PROTOCOL).length()); |
---|
160 | QStringList list=urlString.split(" ",QString::SkipEmptyParts); |
---|
161 | QString program=list.takeFirst(); |
---|
162 | m_process->setReadChannel(QProcess::StandardOutput); |
---|
163 | // Le ponemos el mismo entorno que tiene el browser ahora mismo |
---|
164 | m_process->setEnvironment(QProcess::systemEnvironment()); |
---|
165 | m_process->start(program,list); |
---|
166 | print(tr("Launching the command: ")+program+" "+list.join(" ")+"."); |
---|
167 | startProgressBar(); |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | m_web->load(url); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | void MainWindow::slotWebLoadStarted() |
---|
176 | { |
---|
177 | startProgressBar(); |
---|
178 | m_progressBar->setFormat("%p% Loading"); |
---|
179 | m_progressBar->setMinimum(0); |
---|
180 | m_progressBar->setMaximum(100); |
---|
181 | } |
---|
182 | |
---|
183 | void MainWindow::slotWebLoadProgress(int progress) |
---|
184 | { |
---|
185 | m_progressBar->setValue(progress); |
---|
186 | } |
---|
187 | |
---|
188 | void MainWindow::slotWebLoadFinished(bool ok) |
---|
189 | { |
---|
190 | // If any error ocurred, show a pop up |
---|
191 | // Sometimes when the url hasn't got a dot, i.e /var/www/pageweb, |
---|
192 | // the return value is always true so we check the bytes received too |
---|
193 | if(ok == false || m_web->page()->totalBytes() == 0) |
---|
194 | { |
---|
195 | QMessageBox msgBox; |
---|
196 | msgBox.setText(tr("The web page couldn't load. What do you want to do?")); |
---|
197 | |
---|
198 | QPushButton *reloadButton = msgBox.addButton(tr("Reload"), QMessageBox::ActionRole); |
---|
199 | msgBox.addButton(QMessageBox::Abort); |
---|
200 | |
---|
201 | msgBox.exec(); |
---|
202 | |
---|
203 | if (msgBox.clickedButton() == reloadButton) |
---|
204 | { |
---|
205 | m_web->reload(); |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | close(); |
---|
210 | } |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | finishProgressBar(); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | void MainWindow::slotUrlChanged(const QUrl &url) |
---|
219 | { |
---|
220 | m_webBar->setText(url.toString()); |
---|
221 | } |
---|
222 | |
---|
223 | void MainWindow::slotProcessStarted() |
---|
224 | { |
---|
225 | print(tr("Launched successfully.")); |
---|
226 | startProgressBar(); |
---|
227 | m_progressBar->setMinimum(0); |
---|
228 | m_progressBar->setMaximum(0); |
---|
229 | } |
---|
230 | |
---|
231 | void MainWindow::slotProcessOutput() |
---|
232 | { |
---|
233 | m_process->setReadChannel(QProcess::StandardOutput); |
---|
234 | char buf[BUFFERSIZE]; |
---|
235 | while((m_process->readLine(buf,BUFFERSIZE) > 0)) |
---|
236 | { |
---|
237 | print(tr("Proc. Output: ")+buf,false); |
---|
238 | QString s(buf); |
---|
239 | captureOutputForStatusBar(s); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | void MainWindow::slotProcessErrorOutput() |
---|
244 | { |
---|
245 | m_process->setReadChannel(QProcess::StandardError); |
---|
246 | char buf[BUFFERSIZE]; |
---|
247 | while((m_process->readLine(buf,BUFFERSIZE) > 0)) |
---|
248 | { |
---|
249 | print(tr("Proc. Error: ")+buf); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | void MainWindow::slotProcessFinished(int code,QProcess::ExitStatus status) |
---|
254 | { |
---|
255 | if(status==QProcess::NormalExit) |
---|
256 | { |
---|
257 | print(tr("Process fisnished correctly. Return value: ")+QString::number(code)); |
---|
258 | } |
---|
259 | else |
---|
260 | { |
---|
261 | print(tr("Process crashed. Output: "+code)); |
---|
262 | } |
---|
263 | finishProgressBar(); |
---|
264 | } |
---|
265 | |
---|
266 | void MainWindow::slotProcessError(QProcess::ProcessError error) |
---|
267 | { |
---|
268 | switch(error) |
---|
269 | { |
---|
270 | case QProcess::FailedToStart: |
---|
271 | print(tr("Impossible to launch the process.")); |
---|
272 | break; |
---|
273 | case QProcess::WriteError: |
---|
274 | print(tr("Write error happened in the process.")); |
---|
275 | break; |
---|
276 | case QProcess::ReadError: |
---|
277 | print(tr("Read error happened in the process.")); |
---|
278 | break; |
---|
279 | // No capturo crashed porque la pillo por finished |
---|
280 | case QProcess::Crashed: |
---|
281 | case QProcess::Timedout: |
---|
282 | break; |
---|
283 | case QProcess::UnknownError: |
---|
284 | default: |
---|
285 | print(tr("Unknown error.")); |
---|
286 | break; |
---|
287 | } |
---|
288 | startProgressBar(); |
---|
289 | } |
---|
290 | |
---|
291 | void MainWindow::slotCreateTerminal() |
---|
292 | { |
---|
293 | QTermWidget* console = new QTermWidget(1,this); |
---|
294 | QFont font = QApplication::font(); |
---|
295 | font.setFamily("Terminus"); |
---|
296 | font.setPointSize(12); |
---|
297 | |
---|
298 | console->setTerminalFont(font); |
---|
299 | console->setFocusPolicy(Qt::StrongFocus); |
---|
300 | |
---|
301 | //console->setColorScheme(COLOR_SCHEME_BLACK_ON_LIGHT_YELLOW); |
---|
302 | console->setScrollBarPosition(QTermWidget::ScrollBarRight); |
---|
303 | |
---|
304 | ++m_numberTerminal; |
---|
305 | |
---|
306 | connect(console,SIGNAL(finished()),this,SLOT(slotDeleteTerminal())); |
---|
307 | |
---|
308 | QString name=tr("Term ")+QString::number(m_numberTerminal); |
---|
309 | m_tabs->addTab(console,name); |
---|
310 | } |
---|
311 | |
---|
312 | void MainWindow::slotDeleteTerminal() |
---|
313 | { |
---|
314 | QWidget *widget = qobject_cast<QWidget *>(sender()); |
---|
315 | Q_ASSERT(widget); |
---|
316 | m_tabs->removeTab(m_tabs->indexOf(widget)); |
---|
317 | delete widget; |
---|
318 | } |
---|
319 | |
---|
320 | void MainWindow::slotWebBarReturnPressed() |
---|
321 | { |
---|
322 | QUrl url(m_webBar->text()); |
---|
323 | if(url.isValid()) |
---|
324 | slotLinkHandle(url); |
---|
325 | } |
---|
326 | |
---|
327 | int MainWindow::readEnvironmentValues() |
---|
328 | { |
---|
329 | // The return value |
---|
330 | int ret=true; |
---|
331 | |
---|
332 | // Get all environment variables |
---|
333 | QStringList environmentlist=QProcess::systemEnvironment(); |
---|
334 | // This is the list of the important variables |
---|
335 | QStringList variablelist=QString(ENVIRONMENT).split(","); |
---|
336 | |
---|
337 | // This is an auxiliar variable |
---|
338 | QStringList stringlist; |
---|
339 | |
---|
340 | foreach (QString str,variablelist) |
---|
341 | { |
---|
342 | // Look for the variable in the environment |
---|
343 | stringlist=environmentlist.filter(str+"="); |
---|
344 | |
---|
345 | if(stringlist.isEmpty()) |
---|
346 | { |
---|
347 | m_env[str]=""; |
---|
348 | ret=false; |
---|
349 | } |
---|
350 | else |
---|
351 | { |
---|
352 | // Get the first element and get the value part |
---|
353 | m_env[str]=(stringlist.first().split("="))[1]; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | return ret; |
---|
358 | } |
---|
359 | |
---|
360 | void MainWindow::print(QString s,bool newLine) |
---|
361 | { |
---|
362 | if(!s.endsWith("\n")) |
---|
363 | s+="\n"; |
---|
364 | if(m_logstream) |
---|
365 | *m_logstream<<CURRENT_TIME()<<": "<<s; |
---|
366 | if(m_output) |
---|
367 | m_output->insertPlainText(s); |
---|
368 | } |
---|
369 | |
---|
370 | void MainWindow::captureOutputForStatusBar(QString output) |
---|
371 | { |
---|
372 | // Capturar para modificar status bar |
---|
373 | |
---|
374 | output.trimmed(); |
---|
375 | |
---|
376 | QRegExp rxReset(EXPREG_RESET); |
---|
377 | QRegExp rxPass(EXPREG_A_PASS); |
---|
378 | if(rxReset.exactMatch(output)) |
---|
379 | { |
---|
380 | int minimum=rxReset.cap(1).toInt(); |
---|
381 | int maximum=rxReset.cap(2).toInt(); |
---|
382 | m_progressBar->setMinimum(minimum); |
---|
383 | m_progressBar->setMaximum(maximum); |
---|
384 | m_progressBar->setValue(minimum); |
---|
385 | } |
---|
386 | else if(rxPass.indexIn(output)!=-1) |
---|
387 | { |
---|
388 | print("goo"); |
---|
389 | int pass=rxPass.cap(1).toInt(); |
---|
390 | output.replace(rxPass,""); |
---|
391 | qDebug()<<pass<<output; |
---|
392 | m_progressBar->setValue(pass); |
---|
393 | m_progressBar->setFormat("%p% "+output); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | void MainWindow::startProgressBar() |
---|
398 | { |
---|
399 | QStatusBar* st=statusBar(); |
---|
400 | st->clearMessage(); |
---|
401 | st->addWidget(m_progressBar,100); |
---|
402 | m_progressBar->show(); |
---|
403 | m_web->setEnabled(false); |
---|
404 | } |
---|
405 | |
---|
406 | void MainWindow::finishProgressBar() |
---|
407 | { |
---|
408 | QStatusBar* st=statusBar(); |
---|
409 | st->removeWidget(m_progressBar); |
---|
410 | st->showMessage(tr("Ready")); |
---|
411 | m_web->setEnabled(true); |
---|
412 | } |
---|