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@64
|
79
|
Chris@64
|
80 DEBUG << "saveOutput: " << stdOut.split("\n").size() << " line(s) of stdout, " << stdErr.split("\n").size() << " line(s) of stderr" << endl;
|
Chris@64
|
81
|
Chris@64
|
82 // std::cerr << "stdout was " << stdOut.toStdString() << std::endl;
|
Chris@62
|
83 }
|
Chris@62
|
84
|
jtkorhonen@0
|
85 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
86 {
|
jtkorhonen@0
|
87 exitCode = procExitCode;
|
jtkorhonen@0
|
88 exitStatus = procExitStatus;
|
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@64
|
99 saveOutput();
|
Chris@62
|
100 isRunning = false;
|
jtkorhonen@0
|
101
|
Chris@62
|
102 if (procExitCode == 0 || procExitStatus == QProcess::NormalExit) {
|
Chris@62
|
103 emit commandCompleted();
|
Chris@62
|
104 } else {
|
Chris@62
|
105 emit commandFailed();
|
jtkorhonen@0
|
106 }
|
jtkorhonen@0
|
107 }
|
jtkorhonen@0
|
108
|
Chris@62
|
109 bool HgRunner::isCommandRunning()
|
jtkorhonen@0
|
110 {
|
jtkorhonen@0
|
111 return isRunning;
|
jtkorhonen@0
|
112 }
|
jtkorhonen@0
|
113
|
Chris@62
|
114 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
115 {
|
Chris@62
|
116 if (isCommandRunning()) {
|
jtkorhonen@0
|
117 proc -> kill();
|
jtkorhonen@0
|
118 }
|
jtkorhonen@0
|
119 }
|
jtkorhonen@0
|
120
|
Chris@62
|
121 void HgRunner::startHgCommand(QString workingDir, QStringList params)
|
Chris@62
|
122 {
|
Chris@62
|
123 startCommand(getHgBinaryName(), workingDir, params);
|
Chris@62
|
124 }
|
jtkorhonen@0
|
125
|
Chris@62
|
126 void HgRunner::startCommand(QString command, QString workingDir, QStringList params)
|
jtkorhonen@0
|
127 {
|
jtkorhonen@0
|
128 isRunning = true;
|
jtkorhonen@0
|
129 setRange(0, 0);
|
jtkorhonen@0
|
130 setVisible(true);
|
jtkorhonen@0
|
131 stdOut.clear();
|
jtkorhonen@0
|
132 stdErr.clear();
|
jtkorhonen@0
|
133 exitCode = 0;
|
jtkorhonen@0
|
134 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
135
|
jtkorhonen@0
|
136 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
137 {
|
jtkorhonen@0
|
138 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
139 }
|
jtkorhonen@0
|
140
|
Chris@62
|
141 lastHgCommand = command;
|
jtkorhonen@0
|
142 lastParams = params.join(" ");
|
jtkorhonen@0
|
143
|
Chris@62
|
144 QString cmdline = command;
|
Chris@57
|
145 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
146 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@64
|
147 << workingDir << endl;
|
Chris@43
|
148
|
Chris@62
|
149 proc -> start(command, params);
|
jtkorhonen@0
|
150 }
|
jtkorhonen@0
|
151
|
jtkorhonen@0
|
152 int HgRunner::getExitCode()
|
jtkorhonen@0
|
153 {
|
jtkorhonen@0
|
154 return exitCode;
|
jtkorhonen@0
|
155 }
|
jtkorhonen@0
|
156
|
jtkorhonen@0
|
157 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
158 {
|
jtkorhonen@0
|
159 return stdOut;
|
jtkorhonen@0
|
160 }
|
jtkorhonen@0
|
161
|
jtkorhonen@0
|
162 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
163 {
|
jtkorhonen@0
|
164 setVisible(false);
|
jtkorhonen@0
|
165 }
|
jtkorhonen@0
|
166
|
jtkorhonen@0
|
167
|