[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    Re: Dataengnies
From:       Petri =?iso-8859-15?q?Damst=E9n?= <petri.damsten () gmail ! com>
Date:       2008-08-27 11:51:01
Message-ID: 200808271451.07520.petri.damsten () gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


On Tuesday 26 August 2008 23:38:47 Aaron J. Seigo wrote:
> and yes, if the engine is doing any sort of expensive work then the code
> needs to protect against it being called while the async job is in
> progreess.

Yes, that's true.

> doing async calls inside the DataEngine subclas itself is really not the
> best way to do things. encapsulating it inside a DataContainer subclass
> makes it waaaay easier to manage.

This was new to me :-) Converted ExecutableEngine to use custom Container 
(attached). One problem. exec is called in constructor to start process for 
the first time. checkForUpdate is called when process finished but seems that 
m_queued is false and dataUpdated is not called. On second exec (first 
updateRequested signal, which comes after first timeout) it works:
plasmoidviewer(22365) ScriptedHtml::connectToEngine: 
"/home/damu/scripted_test.py"
plasmoidviewer(22365) ExecutableEngine::sourceRequestEvent: 
"/home/damu/scripted_test.py"
plasmoidviewer(22365) ExecutableContainer::exec: "/home/damu/scripted_test.py"
plasmoidviewer(22365) ScriptedHtml::dataUpdated: "/home/damu/scripted_test.py" 
QHash()
plasmoidviewer(22365)/libplasma Plasma::DataContainer::checkForUpdate: Dirty: 
false
plasmoidviewer(22365) ExecutableContainer::finished: 
"/home/damu/scripted_test.py"
plasmoidviewer(22365)/libplasma Plasma::DataContainer::checkForUpdate: Dirty: 
true
plasmoidviewer(22365)/libplasma Plasma::SignalRelay::checkQueueing: Queued is 
false
plasmoidviewer(22365) ExecutableContainer::exec: "/home/damu/scripted_test.py"
plasmoidviewer(22365)/libplasma Plasma::SignalRelay::timerEvent: Queued set 
to: true
plasmoidviewer(22365) ExecutableContainer::finished: 
"/home/damu/scripted_test.py"
plasmoidviewer(22365)/libplasma Plasma::DataContainer::checkForUpdate: Dirty: 
true
plasmoidviewer(22365)/libplasma Plasma::SignalRelay::checkQueueing: Queued is 
true
plasmoidviewer(22365) ScriptedHtml::dataUpdated: "/home/damu/scripted_test.py" 
QHash(("stderr", QVariant(QString, "") ) ( "exit code" ,  QVariant(int, 0) ) ( 
"exit status" ,  QVariant(int, 0) ) ( "stdout" ,  QVariant(QString, 
"/home/damu/scripted_test.html
") ) )

One other thing. If query() is run first for the DataEngine doing async calls 
it won't work, but I'm not sure if that is important. Applet writer does not 
have a way to know it (if engine is async or not), though.

Petri


["executable.cpp" (text/x-c++src)]

/*
 *   Copyright (C) 2007, 2008 Petri Damsten <damu@iki.fi>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   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 Library 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 "executable.h"
#include <KDebug>

ExecutableContainer::ExecutableContainer(const QString& command, QObject* parent)
    : Plasma::DataContainer(parent), m_process(0)
{
    setObjectName(command);
    connect(this, SIGNAL(updateRequested(DataContainer*)), this, SLOT(exec()));
    exec();
}

ExecutableContainer::~ExecutableContainer()
{
    delete m_process;
}

void ExecutableContainer::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
    kDebug() << objectName();
    setData("exit code", exitCode);
    setData("exit status", exitStatus);
    setData("stdout", QString::fromLocal8Bit(m_process->readAllStandardOutput()));
    setData("stderr", QString::fromLocal8Bit(m_process->readAllStandardError()));
    checkForUpdate();
}

void ExecutableContainer::exec()
{
    kDebug() << objectName();
    if (!m_process) {
        m_process = new KProcess();
        connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
                this, SLOT(finished(int, QProcess::ExitStatus)));
        m_process->setOutputChannelMode(KProcess::SeparateChannels);
        m_process->setShellCommand(objectName());
    }
    if (m_process->state() == QProcess::NotRunning) {
        m_process->start();
    } else {
        kDebug() << "Process" << objectName() << "already running. Pid:" << m_process->pid();
    }
}


ExecutableEngine::ExecutableEngine(QObject* parent, const QVariantList& args)
    : Plasma::DataEngine(parent, args)
{
    setMinimumPollingInterval(1000);
}

bool ExecutableEngine::sourceRequestEvent(const QString& source)
{
    kDebug() << source;
    addSource(new ExecutableContainer(source, this));
    return true;
}

#include "executable.moc"

["executable.h" (text/x-chdr)]

/*
 *   Copyright (C) 2007, 2008 Petri Damsten <damu@iki.fi>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   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 Library 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 EXECUTABLE_DATAENGINE_H
#define EXECUTABLE_DATAENGINE_H

#include <KProcess>
#include <Plasma/DataEngine>
#include <Plasma/DataContainer>

class ExecutableContainer : public Plasma::DataContainer
{
    Q_OBJECT
    public:
        explicit ExecutableContainer(const QString& command, QObject *parent = 0);
        virtual ~ExecutableContainer();

    protected slots:
        void finished(int exitCode, QProcess::ExitStatus exitStatus);
        void exec();

    private:
        KProcess* m_process;
};

class ExecutableEngine : public Plasma::DataEngine
{
    Q_OBJECT
    public:
        ExecutableEngine(QObject *parent, const QVariantList &args);

    protected:
        virtual bool sourceRequestEvent(const QString& source);
};

K_EXPORT_PLASMA_DATAENGINE(executable, ExecutableEngine)

#endif

["signature.asc" (application/pgp-signature)]

_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic