annotate hgrunner.cpp @ 45:4286836bb3c9

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