116 changes: 116 additions & 0 deletions src/daemon/Process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/***************************************************************************
* Copyright (C) 2015 Leslie Zhai <xiang.zhai@i-soft.com.cn>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
***************************************************************************/

#include "Process.h"

#include <QtCore/QProcess>

class Process::Private : public QProcess
{
Q_OBJECT
public:
Private(Process *parent);

QString program;
QStringList arguments;
QString dir;
private:
Process *q;
};

Process::Private::Private(Process *parent)
: QProcess(parent)
, q(parent)
{
connect(this, SIGNAL(started()), q, SIGNAL(started()));
connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), q, SIGNAL(finished(int)));
connect(this, SIGNAL(readyReadStandardOutput()), q, SIGNAL(readyReadStandardOutput()));
connect(this, SIGNAL(readyReadStandardError()), q, SIGNAL(readyReadStandardError()));
}

Process::Process(QObject *parent)
: QObject(parent)
, d(new Private(this))
{
}

Process::~Process()
{
}

const QString &Process::program() const
{
return d->program;
}

void Process::setProgram(const QString &program)
{
if (program == d->program) return;
d->program = program;
emit programChanged(d->program);
}

const QStringList &Process::arguments() const
{
return d->arguments;
}

void Process::setArguments(const QStringList &arguments)
{
if (arguments == d->arguments) return;
d->arguments = arguments;
emit argumentsChanged(d->arguments);
}

const QString &Process::dir() const { return d->dir; }

void Process::setDir(const QString &dir)
{
if (dir == d->dir) return;
d->dir = dir;
d->setWorkingDirectory(d->dir);
emit dirChanged();
}

void Process::start()
{
d->start(d->program, d->arguments);
}

void Process::terminate()
{
d->terminate();
}

void Process::kill()
{
d->kill();
}

QByteArray Process::readAllStandardError()
{
return d->readAllStandardError();
}

QByteArray Process::readAllStandardOutput()
{
return d->readAllStandardOutput();
}

#include "Process.moc"
69 changes: 69 additions & 0 deletions src/daemon/Process.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/***************************************************************************
* Copyright (C) 2015 Leslie Zhai <xiang.zhai@i-soft.com.cn>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
***************************************************************************/

#ifndef PROCESS_H
#define PROCESS_H

#include <QObject>
#include <QStringList>

class Process : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Process)
Q_PROPERTY(QString program READ program WRITE setProgram NOTIFY programChanged)
Q_PROPERTY(QStringList arguments READ arguments WRITE setArguments NOTIFY argumentsChanged)
Q_PROPERTY(QString dir READ dir WRITE setDir NOTIFY dirChanged)

public:
Process(QObject *parent = 0);
~Process();

const QString &program() const;
const QStringList &arguments() const;
const QString &dir() const;

Q_INVOKABLE QByteArray readAllStandardError();
Q_INVOKABLE QByteArray readAllStandardOutput();

public slots:
void setProgram(const QString &program);
void setArguments(const QStringList &arguments);
void setDir(const QString &dir);

void start();
void terminate();
void kill();

signals:
void programChanged(const QString &program);
void argumentsChanged(const QStringList &arguments);
void dirChanged();

void finished(int exitCode);
void readyReadStandardError();
void readyReadStandardOutput();
void started();

private:
class Private;
Private *d;
};

#endif // PROCESS_H