comparison hgrunner.cpp @ 0:a9098eba2ee5

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