almost ready to merge into master
[avrdudequi:avrdudequi.git] / runprocessdialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Maurilio                                        *
3  *   mau_ri_tec@yahoo.it                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21
22 #include "runprocessdialog.h"
23 #include <QScrollBar>
24 #include <QDebug>
25
26
27 RunProcessDialog::RunProcessDialog(QString command, QWidget *parent) :
28     QDialog(parent)
29 {
30     resize(QSize(400, 150));
31     cmd = command.split(" ");
32     if (cmd.isEmpty()) {
33         qDebug() << "Errore: Il processo non può essere eseguito" ;
34         return;
35     }
36     program = cmd.takeFirst();
37     programName = program;
38
39     cmd.prepend("-u"); // provvisorio, poi da qui si leggono le impostazione del backend e si inserisco in cmd
40     cmd.prepend("-q");
41     commandString = program + " " + cmd.join(" ");
42
43     standardButton = new QDialogButtonBox(this);
44     replyButton = standardButton->addButton("Reply", QDialogButtonBox::ActionRole);
45     closeButton = standardButton->addButton(QDialogButtonBox::Close);
46     connect(replyButton, SIGNAL(clicked()), this, SLOT(reply()));
47     connect(closeButton, SIGNAL(clicked()), this, SLOT(doneDialog()));
48
49     processOut = new QTextEdit(this);
50     processOut->setReadOnly(true);
51
52     mainLayout = new QVBoxLayout;
53     mainLayout->addWidget(processOut);
54     mainLayout->addWidget(standardButton);
55
56     setLayout(mainLayout);
57
58     process = new QProcess(this);
59     process->setProcessChannelMode(QProcess::MergedChannels);
60     connect(process, SIGNAL(error(QProcess::ProcessError)),
61             this, SLOT(error_proc()));
62     connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
63             this, SLOT(end_proc(int, QProcess::ExitStatus)));
64     connect(process, SIGNAL(readyReadStandardOutput()),
65              this, SLOT(print_output()));
66     //connect(process, SIGNAL(readyReadStandardError()), this, SLOT(print_error()));
67
68     run();
69 }
70
71 RunProcessDialog::~RunProcessDialog()
72 {
73     delete process;
74     delete processOut;
75     //qDebug() << "delete dtor";
76
77 }
78
79 void RunProcessDialog::reply()
80 {
81     run();
82 }
83
84 void RunProcessDialog::doneDialog()
85 {
86
87     done(process->exitCode());
88 }
89
90 void RunProcessDialog::end_proc(int exit_code, QProcess::ExitStatus exit_status)
91 {
92     processOut->setTextColor(QColor(Qt::blue));
93     processOut->insertPlainText("The process "
94                                + program
95                                + " exit with "
96                                + QString("Exit code: %1").arg(exit_code) + (" ")
97                                + QString("Exit status: %1").arg(exit_status)
98                                + "\n");
99
100     processOut->ensureCursorVisible();
101     lastOutput = processOut->document()->toPlainText().split("\n");
102     replyButton->setDisabled(false);
103     closeButton->setDisabled(false);
104
105
106 }
107
108 void RunProcessDialog::error_proc()
109 {
110     processOut->setTextColor(QColor(Qt::red));
111     processOut->insertPlainText("Error: program"
112                                + program
113                                + " not found "
114                                + "\n");
115     processOut->ensureCursorVisible();
116 }
117
118 void RunProcessDialog::print_output()
119 {
120
121     // ricava l'output dal processo corrente
122     QByteArray result = process->readAllStandardOutput();
123     // lo converte in codifica
124     QString out = QString::fromLocal8Bit(result);
125     //lastOutput.append(out);
126     // scrivo l'output del processo appena convertito
127     processOut->setTextColor(QColor(Qt::black));
128     processOut->insertPlainText(out);
129
130     processOut->ensureCursorVisible();
131
132
133 }
134
135 void RunProcessDialog::print_error()
136 {
137     // ricava l'output dal processo corrente
138     QByteArray result = process->readAllStandardError();
139     // lo converte in codifica
140     QString out = QString::fromLocal8Bit(result);
141     // scrivo l'output del processo appena convertito
142     processOut->setTextColor(QColor(Qt::red));
143
144     processOut->insertPlainText(out);
145     processOut->ensureCursorVisible();
146 }
147
148 void RunProcessDialog::run()
149 {
150     lastOutput.clear();
151
152     if (!processOut->document()->isEmpty())
153         processOut->insertPlainText("\n");
154     processOut->clear();
155     processOut->setTextColor(QColor(Qt::blue));
156     processOut->insertPlainText("Run: " + commandString + "\n");
157
158     processOut->ensureCursorVisible();
159
160     process->start(program, cmd);
161     replyButton->setDisabled(true);
162     closeButton->setDisabled(true);
163
164 }
165
166 QStringList RunProcessDialog::getOutput()
167 {
168     return lastOutput;
169 }
170
171 ParserOutput::ParserOutput(const QStringList &output)
172 {
173     QStringList _output = output;
174     QString dummyFuse;
175     while (_output.removeOne(programName + ": writing output file \"<stdout>\""))
176         ;
177     //qDebug() << _output;
178     for (int i=0; i<_output.count(); i++){
179         if (_output.at(i).contains("reading fuse memory:"))
180             fuses.insert("fuse", _output.at(i+1));
181         if (_output.at(i).contains("reading lfuse memory:"))
182             fuses.insert("lfuse", _output.at(i+1));
183         if (_output.at(i).contains("reading hfuse memory:"))
184             fuses.insert("hfuse", _output.at(i+1));
185         if (_output.at(i).contains("reading efuse memory:"))
186             fuses.insert("efuse", _output.at(i+1));
187         if (_output.at(i).contains("reading lock memory:"))
188             lockBit.insert("lockBit", _output.at(i+1));
189     }
190
191 }
192
193 QMap<QString, QString> ParserOutput::getAllFuseValue()
194 {
195     return fuses;
196 }
197
198 QMap<QString, QString> ParserOutput::getLockBitValue()
199 {
200     return lockBit;
201 }
202
203
204