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@62
|
19 #include "common.h"
|
Chris@57
|
20 #include "debug.h"
|
Chris@50
|
21
|
Chris@50
|
22 #include <QPushButton>
|
Chris@50
|
23 #include <QListWidget>
|
Chris@50
|
24 #include <QDialog>
|
Chris@50
|
25 #include <QLabel>
|
Chris@50
|
26 #include <QVBoxLayout>
|
Chris@62
|
27 #include <QSettings>
|
jtkorhonen@0
|
28
|
Chris@43
|
29 #include <iostream>
|
jtkorhonen@0
|
30 #include <unistd.h>
|
jtkorhonen@0
|
31
|
jtkorhonen@0
|
32 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
33 {
|
jtkorhonen@0
|
34 proc = new QProcess(this);
|
jtkorhonen@0
|
35
|
cannam@45
|
36 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
cannam@45
|
37 env.insert("LANG", "en_US.utf8");
|
cannam@45
|
38 env.insert("LC_ALL", "en_US.utf8");
|
cannam@45
|
39 proc->setProcessEnvironment(env);
|
cannam@45
|
40
|
jtkorhonen@0
|
41 setTextVisible(false);
|
jtkorhonen@0
|
42 setVisible(false);
|
jtkorhonen@0
|
43 isRunning = false;
|
jtkorhonen@0
|
44
|
jtkorhonen@0
|
45 stdOut.clear();
|
jtkorhonen@0
|
46 stdErr.clear();
|
jtkorhonen@0
|
47
|
jtkorhonen@0
|
48 connect(proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@62
|
49 connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@62
|
50 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@62
|
51
|
Chris@62
|
52 reportErrors = false;
|
jtkorhonen@0
|
53 }
|
jtkorhonen@0
|
54
|
jtkorhonen@0
|
55 HgRunner::~HgRunner()
|
jtkorhonen@0
|
56 {
|
jtkorhonen@0
|
57 delete proc;
|
jtkorhonen@0
|
58 }
|
jtkorhonen@0
|
59
|
Chris@62
|
60 QString HgRunner::getHgBinaryName()
|
Chris@62
|
61 {
|
Chris@62
|
62 QSettings settings;
|
Chris@62
|
63 QString hg = settings.value("hgbinary", "hg").toString();
|
Chris@62
|
64 if (hg == "") hg = "hg";
|
Chris@62
|
65 hg = findExecutable(hg);
|
Chris@62
|
66 settings.setValue("hgbinary", hg);
|
Chris@62
|
67 return hg;
|
Chris@62
|
68 }
|
Chris@62
|
69
|
jtkorhonen@0
|
70 void HgRunner::started()
|
jtkorhonen@0
|
71 {
|
jtkorhonen@0
|
72 proc -> closeWriteChannel();
|
jtkorhonen@0
|
73 }
|
jtkorhonen@0
|
74
|
Chris@62
|
75 void HgRunner::saveOutput()
|
Chris@62
|
76 {
|
Chris@62
|
77 stdOut = QString::fromUtf8(proc -> readAllStandardOutput());
|
Chris@62
|
78 stdErr = QString::fromUtf8(proc -> readAllStandardError());
|
Chris@62
|
79 }
|
Chris@62
|
80
|
jtkorhonen@0
|
81 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
82 {
|
jtkorhonen@0
|
83 exitCode = procExitCode;
|
jtkorhonen@0
|
84 exitStatus = procExitStatus;
|
Chris@58
|
85
|
Chris@59
|
86 DEBUG << "setProcExitInfo: " << stdOut.split("\n").size() << " line(s) of stdout, " << stdErr.split("\n").size() << " line(s) of stderr";
|
Chris@58
|
87
|
Chris@58
|
88 // std::cerr << "stdout was " << stdOut.toStdString() << std::endl;
|
jtkorhonen@0
|
89 }
|
jtkorhonen@0
|
90
|
jtkorhonen@0
|
91 QString HgRunner::getLastCommandLine()
|
jtkorhonen@0
|
92 {
|
jtkorhonen@0
|
93 return QString("Command line: " + lastHgCommand + " " + lastParams);
|
jtkorhonen@0
|
94 }
|
jtkorhonen@0
|
95
|
jtkorhonen@0
|
96 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
97 {
|
jtkorhonen@0
|
98 setProcExitInfo(procExitCode, procExitStatus);
|
Chris@62
|
99 isRunning = false;
|
jtkorhonen@0
|
100
|
Chris@62
|
101 if (procExitCode == 0 || procExitStatus == QProcess::NormalExit) {
|
Chris@62
|
102 emit commandCompleted();
|
Chris@62
|
103 } else {
|
Chris@62
|
104 emit commandFailed();
|
jtkorhonen@0
|
105 }
|
jtkorhonen@0
|
106 }
|
jtkorhonen@0
|
107
|
Chris@62
|
108 bool HgRunner::isCommandRunning()
|
jtkorhonen@0
|
109 {
|
jtkorhonen@0
|
110 return isRunning;
|
jtkorhonen@0
|
111 }
|
jtkorhonen@0
|
112
|
Chris@62
|
113 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
114 {
|
Chris@62
|
115 if (isCommandRunning()) {
|
jtkorhonen@0
|
116 proc -> kill();
|
jtkorhonen@0
|
117 }
|
jtkorhonen@0
|
118 }
|
jtkorhonen@0
|
119
|
Chris@62
|
120 void HgRunner::startHgCommand(QString workingDir, QStringList params)
|
Chris@62
|
121 {
|
Chris@62
|
122 startCommand(getHgBinaryName(), workingDir, params);
|
Chris@62
|
123 }
|
jtkorhonen@0
|
124
|
Chris@62
|
125 void HgRunner::startCommand(QString command, QString workingDir, QStringList params)
|
jtkorhonen@0
|
126 {
|
jtkorhonen@0
|
127 isRunning = true;
|
jtkorhonen@0
|
128 setRange(0, 0);
|
jtkorhonen@0
|
129 setVisible(true);
|
jtkorhonen@0
|
130 stdOut.clear();
|
jtkorhonen@0
|
131 stdErr.clear();
|
jtkorhonen@0
|
132 exitCode = 0;
|
jtkorhonen@0
|
133 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
134
|
jtkorhonen@0
|
135 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
136 {
|
jtkorhonen@0
|
137 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
138 }
|
jtkorhonen@0
|
139
|
Chris@62
|
140 lastHgCommand = command;
|
jtkorhonen@0
|
141 lastParams = params.join(" ");
|
jtkorhonen@0
|
142
|
Chris@62
|
143 QString cmdline = command;
|
Chris@57
|
144 foreach (QString param, params) cmdline += " " + param;
|
Chris@57
|
145 DEBUG << "HgRunner: starting: " << cmdline;
|
Chris@43
|
146
|
Chris@62
|
147 proc -> start(command, params);
|
jtkorhonen@0
|
148 }
|
jtkorhonen@0
|
149
|
jtkorhonen@0
|
150 int HgRunner::getExitCode()
|
jtkorhonen@0
|
151 {
|
jtkorhonen@0
|
152 return exitCode;
|
jtkorhonen@0
|
153 }
|
jtkorhonen@0
|
154
|
jtkorhonen@0
|
155 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
156 {
|
jtkorhonen@0
|
157 return stdOut;
|
jtkorhonen@0
|
158 }
|
jtkorhonen@0
|
159
|
jtkorhonen@0
|
160 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
161 {
|
jtkorhonen@0
|
162 setVisible(false);
|
jtkorhonen@0
|
163 }
|
jtkorhonen@0
|
164
|
jtkorhonen@0
|
165
|