From 40e3b10727452efa67a827ac467a9c2453ea0dfa Mon Sep 17 00:00:00 2001 From: Malte Marquarding Date: Sat, 20 Nov 2010 20:34:49 +1100 Subject: [PATCH] Added qml plugin exposing QProcess functionality. --- plugins/qmlprocess/ProcessObject.cpp | 157 +++++++++++++++++ plugins/qmlprocess/ProcessObject.h | 77 +++++++++ plugins/qmlprocess/qmldir | 1 + plugins/qmlprocess/qmlprocess.cpp | 51 ++++++ plugins/qmlprocess/qmlprocess.h | 56 ++++++ plugins/qmlprocess/qmlprocess.pro | 17 ++ plugins/qmlprocess/qmlprocess.pro.user | 291 ++++++++++++++++++++++++++++++++ 7 files changed, 650 insertions(+) create mode 100644 plugins/qmlprocess/ProcessObject.cpp create mode 100644 plugins/qmlprocess/ProcessObject.h create mode 100644 plugins/qmlprocess/qmldir create mode 100644 plugins/qmlprocess/qmlprocess.cpp create mode 100644 plugins/qmlprocess/qmlprocess.h create mode 100644 plugins/qmlprocess/qmlprocess.pro create mode 100644 plugins/qmlprocess/qmlprocess.pro.user diff --git a/plugins/qmlprocess/ProcessObject.cpp b/plugins/qmlprocess/ProcessObject.cpp new file mode 100644 index 0000000..9e68381 --- /dev/null +++ b/plugins/qmlprocess/ProcessObject.cpp @@ -0,0 +1,157 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include +#include + +#include +#include +#include + +#include "ProcessObject.h" + +ProcessObject::ProcessObject(QObject *parent): + QObject(parent), + m_suspend(false), m_started(false) +{ + m_process = new QProcess(this); + connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), + this, + SLOT(processFinished(int, QProcess::ExitStatus))); + connect(m_process, SIGNAL(error(QProcess::ProcessError)),this, + SLOT(processErrored(QProcess::ProcessError))); +} + +ProcessObject::~ProcessObject() +{ + if (m_started) + { + terminate(); + } + delete m_process; +} + +void ProcessObject::processErrored(QProcess::ProcessError error) { + QString err(m_process->readAllStandardError()); + qDebug() << "failed with exitCode" << m_process->exitCode(); + emit failed(m_process->exitCode(), err, error); + m_started = false; +} + +void ProcessObject::processFinished(int exitCode, + QProcess::ExitStatus exitStatus) +{ + if (exitStatus == QProcess::NormalExit) + { + if (exitCode == 0) + { + QString out(m_process->readAllStandardOutput()); + qDebug() << "normal"; + emit completed(out); + } + else + { + QString err(m_process->readAllStandardError()); + qDebug() << "failed"; + emit failed(exitCode, err, m_process->error()); + } + } + else + { + QString err(m_process->readAllStandardError()); + qDebug() << "failed"; + emit failed(exitCode, err, m_process->error()); + } + m_started = false; +} + +QString ProcessObject::command() const +{ + return m_command; +} + +void ProcessObject::setCommand(const QString &command) +{ + if (m_command != command) { + m_command = command; + } +} + +void ProcessObject::run() +{ + if (m_started) + { + return; + qDebug() << "already started"; + } + qDebug() << m_command; + m_process->start(m_command); + qDebug() << "started"; + m_started = true; +} + +bool ProcessObject::isSuspended() const +{ + return m_suspend; +} + +void ProcessObject::setSuspend(bool suspend) +{ + if (!m_started || m_process->pid() == 0) + { + qDebug() << "No process running"; + return; + } + if (m_suspend != suspend) + { + if (m_suspend) + { + ::kill(m_process->pid(), SIGCONT); + qDebug() << "Process resumed"; + + } else { + ::kill(m_process->pid(), SIGSTOP); + qDebug() << "Process suspended"; + + } + emit suspendChanged(suspend); + m_suspend = suspend; + } +} + +void ProcessObject::terminate() +{ + if (m_started && m_process->pid() != 0) + { + m_process->kill(); + } +} + + +QML_DECLARE_TYPE(ProcessObject); diff --git a/plugins/qmlprocess/ProcessObject.h b/plugins/qmlprocess/ProcessObject.h new file mode 100644 index 0000000..572bfee --- /dev/null +++ b/plugins/qmlprocess/ProcessObject.h @@ -0,0 +1,77 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef EXAMPLEITEM_H +#define EXAMPLEITEM_H + +#include +#include +#include + +class ProcessObject : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString command READ command WRITE setCommand) + Q_PROPERTY(bool suspend READ isSuspended WRITE setSuspend NOTIFY suspendChanged) + +public: + ProcessObject(QObject *parent = 0); + ~ProcessObject(); + + Q_INVOKABLE void run(); + Q_INVOKABLE void terminate(); + + QString command() const; + void setCommand(const QString &command); + bool isSuspended() const; + void setSuspend(bool suspend); + +signals: + void completed(const QString& stdout); + void failed(int exitCode, const QString& stderr, + QProcess::ProcessError error); + void suspendChanged(bool); + +private slots: + void processFinished(int exitCode, + QProcess::ExitStatus exitStatus); + void processErrored(QProcess::ProcessError error); + +private: + + QString m_command; + QProcess* m_process; + bool m_suspend; + bool m_started; + + Q_DISABLE_COPY(ProcessObject) +}; + +#endif // EXAMPLEITEM_H diff --git a/plugins/qmlprocess/qmldir b/plugins/qmlprocess/qmldir new file mode 100644 index 0000000..53b91bb --- /dev/null +++ b/plugins/qmlprocess/qmldir @@ -0,0 +1 @@ +plugin qmlprocess diff --git a/plugins/qmlprocess/qmlprocess.cpp b/plugins/qmlprocess/qmlprocess.cpp new file mode 100644 index 0000000..0ec99d1 --- /dev/null +++ b/plugins/qmlprocess/qmlprocess.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlprocess.h" +#include "ProcessObject.h" + +void qmlprocess::registerTypes(const char *uri) +{ + Q_ASSERT(uri == QLatin1String("quickwidget")); + qmlRegisterType(uri, 1, 0, "Process"); +} + +Q_EXPORT_PLUGIN2(qmlprocess, qmlprocess); diff --git a/plugins/qmlprocess/qmlprocess.h b/plugins/qmlprocess/qmlprocess.h new file mode 100644 index 0000000..c29abce --- /dev/null +++ b/plugins/qmlprocess/qmlprocess.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EXAMPLECOREPLUGIN_H +#define EXAMPLECOREPLUGIN_H + +#include +#include + +class qmlprocess : public QDeclarativeExtensionPlugin +{ + Q_OBJECT + +public: + void registerTypes(const char *uri); +}; + +#endif // EXAMPLECOREPLUGIN_H diff --git a/plugins/qmlprocess/qmlprocess.pro b/plugins/qmlprocess/qmlprocess.pro new file mode 100644 index 0000000..73e7fbd --- /dev/null +++ b/plugins/qmlprocess/qmlprocess.pro @@ -0,0 +1,17 @@ +TEMPLATE = lib +TARGET = qmlprocess +QT += declarative +CONFIG += qt plugin + +TARGET = $$qtLibraryTarget($$TARGET) + +# Input +SOURCES += \ + qmlprocess.cpp \ + ProcessObject.cpp + +OTHER_FILES=qmldir + +HEADERS += \ + qmlprocess.h \ + ProcessObject.h diff --git a/plugins/qmlprocess/qmlprocess.pro.user b/plugins/qmlprocess/qmlprocess.pro.user new file mode 100644 index 0000000..91bd8a4 --- /dev/null +++ b/plugins/qmlprocess/qmlprocess.pro.user @@ -0,0 +1,291 @@ + + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + System + + + + ProjectExplorer.Project.Target.0 + + Desktop + Qt4ProjectManager.Target.DesktopTarget + 0 + 0 + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + 2 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + qtlocal Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + /home/mar637/Documents/qmlprocess-build-desktop + 6 + 0 + true + + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + 2 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + qtlocal Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + /home/mar637/Documents/qmlprocess-build-desktop + 6 + 0 + true + + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + 2 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + sbox-qt Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + /home/mar637/Documents/qmlprocess-build-desktop + 13 + 0 + true + + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + 2 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + sbox-qt Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + /home/mar637/Documents/qmlprocess-build-desktop + 13 + 0 + true + + 4 + + + 2 + + false + + + false + $BUILDDIR + Custom Executable + ProjectExplorer.CustomExecutableRunConfiguration + + 1 + + + + ProjectExplorer.Project.Target.1 + + Maemo + Qt4ProjectManager.Target.MaemoDeviceTarget + 0 + 0 + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + + true + + Qt4ProjectManager.MaemoPackageCreationStep + + true + + + 0.0.1 + + 3 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + /home/mar637/Documents/qmlprocess-build-maemo + 9 + 9 + true + + + + qmake + QtProjectManager.QMakeBuildStep + + + + Make + Qt4ProjectManager.MakeStep + false + + + + + true + + Qt4ProjectManager.MaemoPackageCreationStep + + true + + + 0.0.1 + + 3 + + Make + Qt4ProjectManager.MakeStep + true + + clean + + + + 1 + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + /home/mar637/Documents/qmlprocess-build-maemo + 9 + 9 + true + + 2 + + + 2 + + false + + + false + $BUILDDIR + Custom Executable + ProjectExplorer.CustomExecutableRunConfiguration + + 1 + + + + ProjectExplorer.Project.TargetCount + 2 + + + ProjectExplorer.Project.Updater.FileVersion + 4 + + -- 1.7.9.5