Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
jtkorhonen@0
|
17
|
jtkorhonen@0
|
18 #include "hgrunner.h"
|
Chris@57
|
19 #include "debug.h"
|
Chris@50
|
20
|
Chris@50
|
21 #include <QPushButton>
|
Chris@50
|
22 #include <QListWidget>
|
Chris@50
|
23 #include <QDialog>
|
Chris@50
|
24 #include <QLabel>
|
Chris@50
|
25 #include <QVBoxLayout>
|
jtkorhonen@0
|
26
|
Chris@43
|
27 #include <iostream>
|
jtkorhonen@0
|
28 #include <unistd.h>
|
jtkorhonen@0
|
29
|
jtkorhonen@0
|
30 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
31 {
|
jtkorhonen@0
|
32 proc = new QProcess(this);
|
jtkorhonen@0
|
33
|
cannam@45
|
34 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
cannam@45
|
35 env.insert("LANG", "en_US.utf8");
|
cannam@45
|
36 env.insert("LC_ALL", "en_US.utf8");
|
cannam@45
|
37 proc->setProcessEnvironment(env);
|
cannam@45
|
38
|
jtkorhonen@0
|
39 setTextVisible(false);
|
jtkorhonen@0
|
40 setVisible(false);
|
jtkorhonen@0
|
41 isRunning = false;
|
jtkorhonen@0
|
42
|
jtkorhonen@0
|
43 stdOut.clear();
|
jtkorhonen@0
|
44 stdErr.clear();
|
jtkorhonen@0
|
45
|
jtkorhonen@0
|
46 connect(proc, SIGNAL(started()), this, SLOT(started()));
|
jtkorhonen@0
|
47 connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
|
jtkorhonen@0
|
48 connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
|
jtkorhonen@0
|
49 }
|
jtkorhonen@0
|
50
|
jtkorhonen@0
|
51 HgRunner::~HgRunner()
|
jtkorhonen@0
|
52 {
|
jtkorhonen@0
|
53 delete proc;
|
jtkorhonen@0
|
54 }
|
jtkorhonen@0
|
55
|
jtkorhonen@0
|
56 void HgRunner::started()
|
jtkorhonen@0
|
57 {
|
jtkorhonen@0
|
58 proc -> closeWriteChannel();
|
jtkorhonen@0
|
59 }
|
jtkorhonen@0
|
60
|
jtkorhonen@0
|
61 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
62 {
|
jtkorhonen@0
|
63 exitCode = procExitCode;
|
jtkorhonen@0
|
64 exitStatus = procExitStatus;
|
cannam@45
|
65 stdOut = QString::fromUtf8(proc -> readAllStandardOutput());
|
cannam@45
|
66 stdErr = QString::fromUtf8(proc -> readAllStandardError());
|
Chris@58
|
67
|
Chris@58
|
68 DEBUG << "setProcExitInfo: received " << stdOut.split("\n").size() << " lines of stdout";
|
Chris@58
|
69 DEBUG << "setProcExitInfo: received " << stdErr.split("\n").size() << " lines of stderr";
|
Chris@58
|
70
|
Chris@58
|
71 // std::cerr << "stdout was " << stdOut.toStdString() << std::endl;
|
jtkorhonen@0
|
72 }
|
jtkorhonen@0
|
73
|
jtkorhonen@0
|
74 void HgRunner::presentErrorToUser()
|
jtkorhonen@0
|
75 {
|
jtkorhonen@0
|
76 QPushButton *okButton;
|
jtkorhonen@0
|
77 QListWidget *stdoL;
|
jtkorhonen@0
|
78 QListWidget *stdeL;
|
jtkorhonen@0
|
79 QString tmp;
|
jtkorhonen@0
|
80
|
jtkorhonen@0
|
81 QDialog *dlg = new QDialog(this);
|
jtkorhonen@0
|
82 dlg -> setMinimumWidth(800);
|
jtkorhonen@0
|
83 QVBoxLayout layout;
|
jtkorhonen@0
|
84
|
jtkorhonen@0
|
85 dlg -> setWindowTitle(tr("Mercurial error / warning"));
|
jtkorhonen@0
|
86
|
jtkorhonen@0
|
87 tmp.sprintf("%s: %d, %s: %d", "Exitcode", exitCode, "Exit status", exitStatus);
|
jtkorhonen@0
|
88 layout.addWidget(new QLabel(getLastCommandLine()));
|
jtkorhonen@0
|
89 layout.addWidget(new QLabel(tmp));
|
jtkorhonen@0
|
90 layout.addWidget(new QLabel(tr("Standard out:")));
|
jtkorhonen@0
|
91 stdoL = new QListWidget();
|
jtkorhonen@0
|
92 stdoL -> addItems(stdOut.split("\n"));
|
jtkorhonen@0
|
93 layout.addWidget(stdoL);
|
jtkorhonen@0
|
94
|
jtkorhonen@0
|
95 layout.addWidget(new QLabel(tr("Standard error:")));
|
jtkorhonen@0
|
96 stdeL = new QListWidget();
|
jtkorhonen@0
|
97 stdeL -> addItems(stdErr.split("\n"));
|
jtkorhonen@0
|
98 layout.addWidget(stdeL);
|
jtkorhonen@0
|
99
|
jtkorhonen@0
|
100 okButton = new QPushButton("Ok");
|
jtkorhonen@0
|
101 layout.addWidget(okButton);
|
jtkorhonen@0
|
102
|
jtkorhonen@0
|
103 connect(okButton, SIGNAL(clicked()), dlg, SLOT(accept()));
|
jtkorhonen@0
|
104 dlg -> setLayout(&layout);
|
jtkorhonen@0
|
105
|
jtkorhonen@0
|
106 dlg -> setModal(true);
|
jtkorhonen@0
|
107 dlg -> exec();
|
jtkorhonen@0
|
108 }
|
jtkorhonen@0
|
109
|
jtkorhonen@0
|
110
|
jtkorhonen@0
|
111
|
jtkorhonen@0
|
112 void HgRunner::error(QProcess::ProcessError)
|
jtkorhonen@0
|
113 {
|
jtkorhonen@0
|
114 setProcExitInfo(proc -> exitCode(), proc -> exitStatus());
|
jtkorhonen@0
|
115
|
jtkorhonen@11
|
116 if (reportErrors)
|
jtkorhonen@11
|
117 {
|
jtkorhonen@11
|
118 presentErrorToUser();
|
jtkorhonen@11
|
119 }
|
jtkorhonen@0
|
120
|
jtkorhonen@0
|
121 isRunning = false;
|
jtkorhonen@0
|
122 }
|
jtkorhonen@0
|
123
|
jtkorhonen@0
|
124 QString HgRunner::getLastCommandLine()
|
jtkorhonen@0
|
125 {
|
jtkorhonen@0
|
126 return QString("Command line: " + lastHgCommand + " " + lastParams);
|
jtkorhonen@0
|
127 }
|
jtkorhonen@0
|
128
|
jtkorhonen@0
|
129 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
130 {
|
jtkorhonen@0
|
131 setProcExitInfo(procExitCode, procExitStatus);
|
jtkorhonen@0
|
132
|
jtkorhonen@0
|
133 if (reportErrors)
|
jtkorhonen@0
|
134 {
|
jtkorhonen@0
|
135 if ((exitCode == 0) && (exitStatus == QProcess::NormalExit))
|
jtkorhonen@0
|
136 {
|
jtkorhonen@0
|
137 //All ok
|
jtkorhonen@0
|
138 }
|
jtkorhonen@0
|
139 else
|
jtkorhonen@0
|
140 {
|
jtkorhonen@0
|
141 presentErrorToUser();
|
jtkorhonen@0
|
142 }
|
jtkorhonen@0
|
143 }
|
jtkorhonen@0
|
144
|
jtkorhonen@0
|
145 isRunning = false;
|
jtkorhonen@0
|
146 }
|
jtkorhonen@0
|
147
|
jtkorhonen@0
|
148 bool HgRunner::isProcRunning()
|
jtkorhonen@0
|
149 {
|
jtkorhonen@0
|
150 return isRunning;
|
jtkorhonen@0
|
151 }
|
jtkorhonen@0
|
152
|
jtkorhonen@0
|
153 void HgRunner::killProc()
|
jtkorhonen@0
|
154 {
|
jtkorhonen@0
|
155 if (isProcRunning())
|
jtkorhonen@0
|
156 {
|
jtkorhonen@0
|
157 proc -> kill();
|
jtkorhonen@0
|
158 }
|
jtkorhonen@0
|
159 }
|
jtkorhonen@0
|
160
|
jtkorhonen@0
|
161
|
jtkorhonen@0
|
162 void HgRunner::startProc(QString hgExePathAndName, QString workingDir, QStringList params, bool reportErrors)
|
jtkorhonen@0
|
163 {
|
jtkorhonen@0
|
164 this -> reportErrors = reportErrors;
|
jtkorhonen@0
|
165 isRunning = true;
|
jtkorhonen@0
|
166 setRange(0, 0);
|
jtkorhonen@0
|
167 setVisible(true);
|
jtkorhonen@0
|
168 stdOut.clear();
|
jtkorhonen@0
|
169 stdErr.clear();
|
jtkorhonen@0
|
170 exitCode = 0;
|
jtkorhonen@0
|
171 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
172
|
jtkorhonen@0
|
173 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
174 {
|
jtkorhonen@0
|
175 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
176 }
|
jtkorhonen@0
|
177
|
jtkorhonen@0
|
178 lastHgCommand = hgExePathAndName;
|
jtkorhonen@0
|
179 lastParams = params.join(" ");
|
jtkorhonen@0
|
180
|
Chris@57
|
181 QString cmdline = hgExePathAndName;
|
Chris@57
|
182 foreach (QString param, params) cmdline += " " + param;
|
Chris@57
|
183 DEBUG << "HgRunner: starting: " << cmdline;
|
Chris@43
|
184
|
jtkorhonen@0
|
185 proc -> start(hgExePathAndName, params);
|
jtkorhonen@0
|
186
|
jtkorhonen@0
|
187 }
|
jtkorhonen@0
|
188
|
jtkorhonen@0
|
189 int HgRunner::getExitCode()
|
jtkorhonen@0
|
190 {
|
jtkorhonen@0
|
191 return exitCode;
|
jtkorhonen@0
|
192 }
|
jtkorhonen@0
|
193
|
jtkorhonen@0
|
194 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
195 {
|
jtkorhonen@0
|
196 return stdOut;
|
jtkorhonen@0
|
197 }
|
jtkorhonen@0
|
198
|
jtkorhonen@0
|
199 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
200 {
|
jtkorhonen@0
|
201 setVisible(false);
|
jtkorhonen@0
|
202 }
|
jtkorhonen@0
|
203
|
jtkorhonen@0
|
204
|