annotate hgrunner.cpp @ 53:3c46b2ac45d3

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