jtkorhonen@0
|
1 //** Copyright (C) Jari Korhonen, 2010 (under lgpl)
|
jtkorhonen@0
|
2
|
jtkorhonen@0
|
3 #include "hgrunner.h"
|
jtkorhonen@0
|
4 #include <QtCore>
|
jtkorhonen@0
|
5 #include <QtGui>
|
jtkorhonen@0
|
6
|
jtkorhonen@0
|
7 #include <unistd.h>
|
jtkorhonen@0
|
8
|
jtkorhonen@0
|
9 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
10 {
|
jtkorhonen@0
|
11 proc = new QProcess(this);
|
jtkorhonen@0
|
12
|
jtkorhonen@0
|
13 setTextVisible(false);
|
jtkorhonen@0
|
14 setVisible(false);
|
jtkorhonen@0
|
15 isRunning = false;
|
jtkorhonen@0
|
16
|
jtkorhonen@0
|
17 stdOut.clear();
|
jtkorhonen@0
|
18 stdErr.clear();
|
jtkorhonen@0
|
19
|
jtkorhonen@0
|
20 connect(proc, SIGNAL(started()), this, SLOT(started()));
|
jtkorhonen@0
|
21 connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
|
jtkorhonen@0
|
22 connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
|
jtkorhonen@0
|
23 }
|
jtkorhonen@0
|
24
|
jtkorhonen@0
|
25 HgRunner::~HgRunner()
|
jtkorhonen@0
|
26 {
|
jtkorhonen@0
|
27 delete proc;
|
jtkorhonen@0
|
28 }
|
jtkorhonen@0
|
29
|
jtkorhonen@0
|
30 void HgRunner::started()
|
jtkorhonen@0
|
31 {
|
jtkorhonen@0
|
32 proc -> closeWriteChannel();
|
jtkorhonen@0
|
33 }
|
jtkorhonen@0
|
34
|
jtkorhonen@0
|
35 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
36 {
|
jtkorhonen@0
|
37 exitCode = procExitCode;
|
jtkorhonen@0
|
38 exitStatus = procExitStatus;
|
jtkorhonen@0
|
39 stdOut = proc -> readAllStandardOutput();
|
jtkorhonen@0
|
40 stdErr = proc -> readAllStandardError();
|
jtkorhonen@0
|
41 }
|
jtkorhonen@0
|
42
|
jtkorhonen@0
|
43 void HgRunner::presentErrorToUser()
|
jtkorhonen@0
|
44 {
|
jtkorhonen@0
|
45 QPushButton *okButton;
|
jtkorhonen@0
|
46 QListWidget *stdoL;
|
jtkorhonen@0
|
47 QListWidget *stdeL;
|
jtkorhonen@0
|
48 QString tmp;
|
jtkorhonen@0
|
49
|
jtkorhonen@0
|
50 QDialog *dlg = new QDialog(this);
|
jtkorhonen@0
|
51 dlg -> setMinimumWidth(800);
|
jtkorhonen@0
|
52 QVBoxLayout layout;
|
jtkorhonen@0
|
53
|
jtkorhonen@0
|
54 dlg -> setWindowTitle(tr("Mercurial error / warning"));
|
jtkorhonen@0
|
55
|
jtkorhonen@0
|
56 tmp.sprintf("%s: %d, %s: %d", "Exitcode", exitCode, "Exit status", exitStatus);
|
jtkorhonen@0
|
57 layout.addWidget(new QLabel(getLastCommandLine()));
|
jtkorhonen@0
|
58 layout.addWidget(new QLabel(tmp));
|
jtkorhonen@0
|
59 layout.addWidget(new QLabel(tr("Standard out:")));
|
jtkorhonen@0
|
60 stdoL = new QListWidget();
|
jtkorhonen@0
|
61 stdoL -> addItems(stdOut.split("\n"));
|
jtkorhonen@0
|
62 layout.addWidget(stdoL);
|
jtkorhonen@0
|
63
|
jtkorhonen@0
|
64 layout.addWidget(new QLabel(tr("Standard error:")));
|
jtkorhonen@0
|
65 stdeL = new QListWidget();
|
jtkorhonen@0
|
66 stdeL -> addItems(stdErr.split("\n"));
|
jtkorhonen@0
|
67 layout.addWidget(stdeL);
|
jtkorhonen@0
|
68
|
jtkorhonen@0
|
69 okButton = new QPushButton("Ok");
|
jtkorhonen@0
|
70 layout.addWidget(okButton);
|
jtkorhonen@0
|
71
|
jtkorhonen@0
|
72 connect(okButton, SIGNAL(clicked()), dlg, SLOT(accept()));
|
jtkorhonen@0
|
73 dlg -> setLayout(&layout);
|
jtkorhonen@0
|
74
|
jtkorhonen@0
|
75 dlg -> setModal(true);
|
jtkorhonen@0
|
76 dlg -> exec();
|
jtkorhonen@0
|
77 }
|
jtkorhonen@0
|
78
|
jtkorhonen@0
|
79
|
jtkorhonen@0
|
80
|
jtkorhonen@0
|
81 void HgRunner::error(QProcess::ProcessError)
|
jtkorhonen@0
|
82 {
|
jtkorhonen@0
|
83 setProcExitInfo(proc -> exitCode(), proc -> exitStatus());
|
jtkorhonen@0
|
84
|
jtkorhonen@11
|
85 if (reportErrors)
|
jtkorhonen@11
|
86 {
|
jtkorhonen@11
|
87 presentErrorToUser();
|
jtkorhonen@11
|
88 }
|
jtkorhonen@0
|
89
|
jtkorhonen@0
|
90 isRunning = false;
|
jtkorhonen@0
|
91 }
|
jtkorhonen@0
|
92
|
jtkorhonen@0
|
93 QString HgRunner::getLastCommandLine()
|
jtkorhonen@0
|
94 {
|
jtkorhonen@0
|
95 return QString("Command line: " + lastHgCommand + " " + lastParams);
|
jtkorhonen@0
|
96 }
|
jtkorhonen@0
|
97
|
jtkorhonen@0
|
98 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
99 {
|
jtkorhonen@0
|
100 setProcExitInfo(procExitCode, procExitStatus);
|
jtkorhonen@0
|
101
|
jtkorhonen@0
|
102 if (reportErrors)
|
jtkorhonen@0
|
103 {
|
jtkorhonen@0
|
104 if ((exitCode == 0) && (exitStatus == QProcess::NormalExit))
|
jtkorhonen@0
|
105 {
|
jtkorhonen@0
|
106 //All ok
|
jtkorhonen@0
|
107 }
|
jtkorhonen@0
|
108 else
|
jtkorhonen@0
|
109 {
|
jtkorhonen@0
|
110 presentErrorToUser();
|
jtkorhonen@0
|
111 }
|
jtkorhonen@0
|
112 }
|
jtkorhonen@0
|
113
|
jtkorhonen@0
|
114 isRunning = false;
|
jtkorhonen@0
|
115 }
|
jtkorhonen@0
|
116
|
jtkorhonen@0
|
117 bool HgRunner::isProcRunning()
|
jtkorhonen@0
|
118 {
|
jtkorhonen@0
|
119 return isRunning;
|
jtkorhonen@0
|
120 }
|
jtkorhonen@0
|
121
|
jtkorhonen@0
|
122 void HgRunner::killProc()
|
jtkorhonen@0
|
123 {
|
jtkorhonen@0
|
124 if (isProcRunning())
|
jtkorhonen@0
|
125 {
|
jtkorhonen@0
|
126 proc -> kill();
|
jtkorhonen@0
|
127 }
|
jtkorhonen@0
|
128 }
|
jtkorhonen@0
|
129
|
jtkorhonen@0
|
130
|
jtkorhonen@0
|
131 void HgRunner::startProc(QString hgExePathAndName, QString workingDir, QStringList params, bool reportErrors)
|
jtkorhonen@0
|
132 {
|
jtkorhonen@0
|
133 this -> reportErrors = reportErrors;
|
jtkorhonen@0
|
134 isRunning = true;
|
jtkorhonen@0
|
135 setRange(0, 0);
|
jtkorhonen@0
|
136 setVisible(true);
|
jtkorhonen@0
|
137 stdOut.clear();
|
jtkorhonen@0
|
138 stdErr.clear();
|
jtkorhonen@0
|
139 exitCode = 0;
|
jtkorhonen@0
|
140 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
141
|
jtkorhonen@0
|
142 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
143 {
|
jtkorhonen@0
|
144 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
145 }
|
jtkorhonen@0
|
146
|
jtkorhonen@0
|
147 lastHgCommand = hgExePathAndName;
|
jtkorhonen@0
|
148 lastParams = params.join(" ");
|
jtkorhonen@0
|
149
|
jtkorhonen@0
|
150 proc -> start(hgExePathAndName, params);
|
jtkorhonen@0
|
151
|
jtkorhonen@0
|
152 }
|
jtkorhonen@0
|
153
|
jtkorhonen@0
|
154 int HgRunner::getExitCode()
|
jtkorhonen@0
|
155 {
|
jtkorhonen@0
|
156 return exitCode;
|
jtkorhonen@0
|
157 }
|
jtkorhonen@0
|
158
|
jtkorhonen@0
|
159 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
160 {
|
jtkorhonen@0
|
161 return stdOut;
|
jtkorhonen@0
|
162 }
|
jtkorhonen@0
|
163
|
jtkorhonen@0
|
164 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
165 {
|
jtkorhonen@0
|
166 setVisible(false);
|
jtkorhonen@0
|
167 }
|
jtkorhonen@0
|
168
|
jtkorhonen@0
|
169
|