From 3bc180c219db2def09d5e7fdb22b16b12c0f6cfa Mon Sep 17 00:00:00 2001 From: mar637 Date: Mon, 21 Mar 2011 23:38:19 +1100 Subject: [PATCH] Added ability to add widgets from command-line. --- ProcessObject.cpp | 160 +++++++++++++++++++++++++++++++++++++++++++++++++- ProcessObject.h | 78 +++++++++++++++++++++++- appeventlistener.cpp | 10 +++- appeventlistener.hpp | 2 +- debian/changelog | 12 ++++ debian/control | 2 +- main.cpp | 17 ++++-- quickwidget.cpp | 1 + 8 files changed, 271 insertions(+), 11 deletions(-) mode change 120000 => 100644 ProcessObject.cpp mode change 120000 => 100644 ProcessObject.h diff --git a/ProcessObject.cpp b/ProcessObject.cpp deleted file mode 120000 index e64c8a1..0000000 --- a/ProcessObject.cpp +++ /dev/null @@ -1 +0,0 @@ -plugins/qmlprocess/ProcessObject.cpp \ No newline at end of file diff --git a/ProcessObject.cpp b/ProcessObject.cpp new file mode 100644 index 0000000..a921af9 --- /dev/null +++ b/ProcessObject.cpp @@ -0,0 +1,159 @@ +/************************************************************************** +** +** 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 + +#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) +{ + QTextCodec* codec = QTextCodec::codecForName("utf-8"); + if (exitStatus == QProcess::NormalExit) + { + if (exitCode == 0) + { + QString out = codec->toUnicode(m_process->readAllStandardOutput()); + qDebug() << "normal"; + emit completed(out.trimmed()); + } + else + { + QString err = codec->toUnicode(m_process->readAllStandardError()); + qDebug() << "failed"; + emit failed(exitCode, err.trimmed(), m_process->error()); + } + } + else + { + QString err = codec->toUnicode(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/ProcessObject.h b/ProcessObject.h deleted file mode 120000 index a243678..0000000 --- a/ProcessObject.h +++ /dev/null @@ -1 +0,0 @@ -plugins/qmlprocess/ProcessObject.h \ No newline at end of file diff --git a/ProcessObject.h b/ProcessObject.h new file mode 100644 index 0000000..572bfee --- /dev/null +++ b/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/appeventlistener.cpp b/appeventlistener.cpp index 4dfdd81..8d1d302 100644 --- a/appeventlistener.cpp +++ b/appeventlistener.cpp @@ -4,6 +4,7 @@ #include "appeventlistener.hpp" #include "quickwidgetsettings.hpp" +#include "quickwidget.hpp" static AppEventListener *_instance = NULL; @@ -19,14 +20,17 @@ AppEventListener::AppEventListener(QObject *parent) : QObject(parent) { // Connect to the QtSingleApplication's event - QObject::connect(QtSingleApplication::instance(), SIGNAL(messageReceived(const QString&)), this, SLOT(receiveMessage(const QString&))); + QObject::connect(QtSingleApplication::instance(), SIGNAL(messageReceived(const QString&)), + this, SLOT(receiveMessage(const QString&))); } void AppEventListener::receiveMessage(const QString& message) { - if (message == APP_MESSAGE_ADDWIDGET) + if (message.endsWith(".qml")) { + QuickWidget::createAndShowNew(message); + + } else if (message == APP_MESSAGE_SETWIDGET) { - qDebug() << "TTTTTTEEEEST"; QuickWidgetSettings settings; settings.exec(); } diff --git a/appeventlistener.hpp b/appeventlistener.hpp index 9b5e76e..5e5a952 100644 --- a/appeventlistener.hpp +++ b/appeventlistener.hpp @@ -3,7 +3,7 @@ #include -#define APP_MESSAGE_ADDWIDGET "Add widget" +#define APP_MESSAGE_SETWIDGET "Add widget" #define APP_MESSAGE_CLOSE "Close" class AppEventListener : public QObject diff --git a/debian/changelog b/debian/changelog index 65799f0..526b133 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,15 @@ +quick-widgets (0.2.4) unstable; urgency=low + + * enabled adding widgets from the command-line + + -- Malte Marquarding Mon, 21 Mar 2011 23:29:30 +1100 + +quick-widgets (0.2.3) unstable; urgency=low + + * added qt-mobility imports to search locations + + -- Malte Marquarding Thu, 17 Mar 2011 09:45:14 +1100 + quick-widgets (0.2.2) unstable; urgency=low * added unicode support for standard output diff --git a/debian/control b/debian/control index 923087c..9286bfc 100644 --- a/debian/control +++ b/debian/control @@ -8,7 +8,7 @@ Homepage: http://quick-widgets.garage.maemo.org/ Package: quick-widgets Architecture: any -Depends: ${shlibs:Depends}, libhildon-extras1, q-extras (>=1.0.5) +Depends: ${shlibs:Depends}, libhildon-extras1, q-extras (>=1.0.5), libqtm-12-declarative (>=1.2.0) XB-Maemo-Display-Name: Quick Widgets Description: This application simplifies running Qt Quick applications as home desktop widgets. It only needs the file. Thanks to Timur Kristóf (Venemo) diff --git a/main.cpp b/main.cpp index 24a201f..5e99551 100644 --- a/main.cpp +++ b/main.cpp @@ -36,7 +36,13 @@ int main(int argc, char *argv[]) else { qDebug() << "Asking it to offer to create a new widget."; - app.sendMessage(APP_MESSAGE_ADDWIDGET); + QStringList result; + result = app.arguments().filter(".qml"); + if (result.size() == 1 && QFile(result.last()).exists()) { + app.sendMessage(QFileInfo(result.last()).absoluteFilePath()); + } else { + app.sendMessage(APP_MESSAGE_SETWIDGET); + } return 0; } } @@ -54,13 +60,16 @@ int main(int argc, char *argv[]) qDebug() << "Restoring all widgets"; QeMaemo5DynamicWidgetHelper::globalInstance()->restoreWidgets(); } - - if (!app.arguments().contains("in-background")) + QStringList result; + result = app.arguments().filter(".qml"); + if (result.size() == 1 && QFile(result.last()).exists()) { + app.sendMessage(QFileInfo(result.last()).absoluteFilePath()); + } else if (!app.arguments().contains("in-background")) { // setting for new widget QuickWidgetSettings settingsDialog; settingsDialog.exec(); - //app.sendMessage(APP_MESSAGE_ADDWIDGET); + //app.sendMessage(APP_MESSAGE_SETWIDGET); } return app.exec(); } diff --git a/quickwidget.cpp b/quickwidget.cpp index 6047117..9c9dffd 100644 --- a/quickwidget.cpp +++ b/quickwidget.cpp @@ -61,6 +61,7 @@ QuickWidget::QuickWidget(QWidget *parent) } //layout()->setSizeConstraint(QLayout::SetNoConstraint); view_ = new QDeclarativeView(this); + view_->engine()->addImportPath(QString("/opt/qtm12/imports")); view_->setStyleSheet("background:transparent"); view_->setAttribute(Qt::WA_TranslucentBackground); wrapper_ = new QuickWidgetWrapper(this); -- 1.7.9.5