| 1 | /* |
| 2 | * Copyright (c) 2002-2003 Christian Loose <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "cvsjob.h" |
| 22 | |
| 23 | #include "../debug.h" |
| 24 | #include "sshagent.h" |
| 25 | |
| 26 | #include <kprocess.h> |
| 27 | |
| 28 | #include <cvsjobadaptor.h> |
| 29 | |
| 30 | struct CvsJob::Private { |
| 31 | Private() |
| 32 | : isRunning(false) |
| 33 | { |
| 34 | childproc = new KProcess; |
| 35 | } |
| 36 | ~Private() |
| 37 | { |
| 38 | delete childproc; |
| 39 | } |
| 40 | |
| 41 | KProcess *childproc; |
| 42 | QString server; |
| 43 | QString rsh; |
| 44 | QString directory; |
| 45 | bool isRunning; |
| 46 | QStringList outputLines; |
| 47 | QString dbusObjectPath; |
| 48 | }; |
| 49 | |
| 50 | CvsJob::CvsJob(unsigned jobNum) |
| 51 | : QObject() |
| 52 | , d(new Private) |
| 53 | { |
| 54 | (void)new CvsjobAdaptor(this); |
| 55 | QDBusConnection dbus = QDBusConnection::sessionBus(); |
| 56 | d->dbusObjectPath = "/CvsJob" + QString::number(jobNum); |
| 57 | qCDebug(log_cervisia) << "dbusObjectPath:" << d->dbusObjectPath; |
| 58 | dbus.registerObject(d->dbusObjectPath, this); |
| 59 | } |
| 60 | |
| 61 | CvsJob::CvsJob(const QString &objId) |
| 62 | : QObject() |
| 63 | , d(new Private) |
| 64 | { |
| 65 | (void)new CvsjobAdaptor(this); |
| 66 | // TODO register it with good name |
| 67 | d->dbusObjectPath = '/' + objId; |
| 68 | qCDebug(log_cervisia) << "dbusObjectPath:" << d->dbusObjectPath; |
| 69 | QDBusConnection::sessionBus().registerObject(d->dbusObjectPath, this); |
| 70 | } |
| 71 | |
| 72 | CvsJob::~CvsJob() |
| 73 | { |
| 74 | delete d; |
| 75 | } |
| 76 | |
| 77 | QString CvsJob::dbusObjectPath() const |
| 78 | { |
| 79 | return d->dbusObjectPath; |
| 80 | } |
| 81 | |
| 82 | void CvsJob::clearCvsCommand() |
| 83 | { |
| 84 | d->childproc->clearProgram(); |
| 85 | } |
| 86 | |
| 87 | void CvsJob::setRSH(const QString &rsh) |
| 88 | { |
| 89 | d->rsh = rsh; |
| 90 | } |
| 91 | |
| 92 | void CvsJob::setServer(const QString &server) |
| 93 | { |
| 94 | d->server = server; |
| 95 | } |
| 96 | |
| 97 | void CvsJob::setDirectory(const QString &directory) |
| 98 | { |
| 99 | d->directory = directory; |
| 100 | } |
| 101 | |
| 102 | bool CvsJob::isRunning() const |
| 103 | { |
| 104 | return d->isRunning; |
| 105 | } |
| 106 | |
| 107 | CvsJob &CvsJob::operator<<(const QString &arg) |
| 108 | { |
| 109 | *d->childproc << arg; |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | CvsJob &CvsJob::operator<<(const char *arg) |
| 114 | { |
| 115 | *d->childproc << arg; |
| 116 | return *this; |
| 117 | } |
| 118 | |
| 119 | CvsJob &CvsJob::operator<<(const QStringList &args) |
| 120 | { |
| 121 | *d->childproc << args; |
| 122 | return *this; |
| 123 | } |
| 124 | |
| 125 | QString CvsJob::cvsCommand() const |
| 126 | { |
| 127 | return d->childproc->program().join(QLatin1String(" ")); |
| 128 | } |
| 129 | |
| 130 | QStringList CvsJob::output() const |
| 131 | { |
| 132 | return d->outputLines; |
| 133 | } |
| 134 | |
| 135 | bool CvsJob::execute() |
| 136 | { |
| 137 | // setup job environment to use the ssh-agent (if it is running) |
| 138 | SshAgent ssh; |
| 139 | if (!ssh.pid().isEmpty()) { |
| 140 | // qCDebug(log_cervisia) << "PID = " << ssh.pid(); |
| 141 | // qCDebug(log_cervisia) << "SOCK = " << ssh.authSock(); |
| 142 | |
| 143 | d->childproc->setEnv("SSH_AGENT_PID", ssh.pid()); |
| 144 | d->childproc->setEnv("SSH_AUTH_SOCK", ssh.authSock()); |
| 145 | } |
| 146 | |
| 147 | d->childproc->setEnv("SSH_ASKPASS", "cvsaskpass"); |
| 148 | |
| 149 | if (!d->rsh.isEmpty()) |
| 150 | d->childproc->setEnv("CVS_RSH", d->rsh); |
| 151 | |
| 152 | if (!d->server.isEmpty()) |
| 153 | d->childproc->setEnv("CVS_SERVER", d->server); |
| 154 | |
| 155 | if (!d->directory.isEmpty()) |
| 156 | d->childproc->setWorkingDirectory(d->directory); |
| 157 | |
| 158 | connect(d->childproc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(slotProcessFinished())); |
| 159 | connect(d->childproc, SIGNAL(readyReadStandardOutput()), SLOT(slotReceivedStdout())); |
| 160 | connect(d->childproc, SIGNAL(readyReadStandardError()), SLOT(slotReceivedStderr())); |
| 161 | |
| 162 | qCDebug(log_cervisia) << "Execute cvs command:" << cvsCommand(); |
| 163 | |
| 164 | d->isRunning = true; |
| 165 | d->childproc->setOutputChannelMode(KProcess::SeparateChannels); |
| 166 | d->childproc->setShellCommand(cvsCommand()); |
| 167 | d->childproc->start(); |
| 168 | return d->childproc->waitForStarted(); |
| 169 | } |
| 170 | |
| 171 | void CvsJob::cancel() |
| 172 | { |
| 173 | d->childproc->kill(); |
| 174 | } |
| 175 | |
| 176 | void CvsJob::slotProcessFinished() |
| 177 | { |
| 178 | qCDebug(log_cervisia); |
| 179 | // disconnect all connections to childproc's signals |
| 180 | d->childproc->disconnect(); |
| 181 | d->childproc->clearProgram(); |
| 182 | |
| 183 | d->isRunning = false; |
| 184 | |
| 185 | Q_EMIT jobExited(d->childproc->exitStatus() == QProcess::NormalExit, d->childproc->exitCode()); |
| 186 | } |
| 187 | |
| 188 | void CvsJob::slotReceivedStdout() |
| 189 | { |
| 190 | const QString output(QString::fromLocal8Bit(d->childproc->readAllStandardOutput())); |
| 191 | |
| 192 | // accumulate output |
| 193 | d->outputLines += output.split('\n'); |
| 194 | |
| 195 | qCDebug(log_cervisia) << "output:" << output; |
| 196 | Q_EMIT receivedStdout(output); |
| 197 | } |
| 198 | |
| 199 | void CvsJob::slotReceivedStderr() |
| 200 | { |
| 201 | const QString output(QString::fromLocal8Bit(d->childproc->readAllStandardError())); |
| 202 | |
| 203 | // accumulate output |
| 204 | d->outputLines += output.split('\n'); |
| 205 | |
| 206 | qCDebug(log_cervisia) << "output:" << output; |
| 207 | Q_EMIT receivedStderr(output); |
| 208 | } |