Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
pull properties using refresh() and allow to set
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Nov 17, 2014
1 parent a63f3f5 commit c8a1f56
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 21 deletions.
3 changes: 3 additions & 0 deletions include/statefs/qt/client.hpp
Expand Up @@ -21,8 +21,11 @@ class DiscreteProperty : public QObject
DiscreteProperty(QString const &, QObject *parent = nullptr);
~DiscreteProperty();

void refresh() const;

signals:
void changed(QVariant);

private:
DiscretePropertyImpl *impl_;
};
Expand Down
62 changes: 57 additions & 5 deletions src/contextkit-subscriber/property.cpp
Expand Up @@ -82,6 +82,8 @@ class DiscretePropertyImpl : public QObject
DiscretePropertyImpl(QString const &, QObject *parent = nullptr);
~DiscretePropertyImpl() {}

void refresh() const;

signals:
void changed(QVariant);
private slots:
Expand All @@ -97,13 +99,13 @@ class PropertyWriterImpl : public QObject
Q_OBJECT;
public:
PropertyWriterImpl(QString const &, QObject *parent = nullptr);
~PropertyWriterImpl() { impl_->unsubscribe(); }
~PropertyWriterImpl() {}

void set(QVariant &&);
signals:
void updated(bool);
private:
UNIQUE_PTR(ContextPropertyPrivate) impl_;
QString key_;
};

}}
Expand All @@ -119,7 +121,8 @@ class Event : public QEvent
Subscribe = QEvent::User,
Unsubscribe,
Subscribed,
Write
Write,
Refresh
};

virtual ~Event();
Expand Down Expand Up @@ -223,6 +226,22 @@ class WriteRequest : public QObject, public Event
void updated(bool);
};

class RefreshRequest : public Event
{
public:
RefreshRequest(ContextPropertyPrivate const *tgt
, QString const &key)
: Event(Event::Refresh)
, tgt_(tgt)
, key_(key)
{}
RefreshRequest(RefreshRequest const&) = delete;
virtual ~RefreshRequest() {}

ContextPropertyPrivate const *tgt_;
QString key_;
};

bool PropertyMonitor::event(QEvent *e)
{
if (e->type() < QEvent::User)
Expand Down Expand Up @@ -256,6 +275,14 @@ bool PropertyMonitor::event(QEvent *e)
debug::warning("Bad WriteRequest");
break;
}
case Event::Refresh: {
auto p = dynamic_cast<RefreshRequest*>(e);
if (p)
refresh(p);
else
debug::warning("PropertyMonitor: !Refresh");
break;
}
default:
debug::warning("Unknown user event");
res = QObject::event(e);
Expand Down Expand Up @@ -374,6 +401,17 @@ void PropertyMonitor::unsubscribe(UnsubscribeRequest *req)
}
}

void PropertyMonitor::refresh(RefreshRequest *req)
{
auto key = req->key_;
auto phandlers = properties_.find(key);
if (phandlers == properties_.end())
return;

auto handler = phandlers.value();
handler->update();
}

Property* PropertyMonitor::add(const QString &key)
{
auto it = properties_.insert(key, new Property(key, this));
Expand Down Expand Up @@ -723,6 +761,10 @@ void ContextPropertyPrivate::setTypeCheck(bool typeCheck)
{
}

void ContextPropertyPrivate::refresh() const
{
actor()->postEvent(new ckit::RefreshRequest(this, key_));
}

ContextProperty::ContextProperty(const QString &key, QObject *parent)
: QObject(parent)
Expand Down Expand Up @@ -801,6 +843,11 @@ DiscreteProperty::~DiscreteProperty()
{
}

void DiscreteProperty::refresh() const
{
impl_->refresh();
}

PropertyWriter::PropertyWriter
(QString const &key, QObject *parent)
: QObject(parent)
Expand Down Expand Up @@ -832,22 +879,27 @@ void DiscretePropertyImpl::onChanged()
emit changed(impl_->value());
}

void DiscretePropertyImpl::refresh() const
{
impl_->refresh();
}

PropertyWriter::~PropertyWriter()
{
}

PropertyWriterImpl::PropertyWriterImpl
(QString const &key, QObject *parent)
: QObject(parent)
, impl_(make_qobject_unique<ContextPropertyPrivate>(key, this))
, key_(key)
{
}

void PropertyWriterImpl::set(QVariant &&v)
{
using namespace ckit;
auto monitor = PropertyMonitor::instance();
monitor->postEvent(new WriteRequest(this, impl_->key(), std::move(v)));
monitor->postEvent(new WriteRequest(this, key_, std::move(v)));
}

}}
Expand Down
9 changes: 7 additions & 2 deletions src/contextkit-subscriber/property.hpp
Expand Up @@ -45,6 +45,8 @@ class Property : public QObject
QVariant subscribe();
void unsubscribe();

bool update();

signals:
void changed(QVariant) const;

Expand All @@ -57,8 +59,6 @@ private slots:
void resubscribe();
QVariant subscribe_();

bool update();

QString key_;
QFile user_file_;
QFile sys_file_;
Expand All @@ -74,6 +74,7 @@ private slots:
class SubscribeRequest;
class UnsubscribeRequest;
class WriteRequest;
class RefreshRequest;

class PropertyMonitor : public QObject
{
Expand All @@ -88,6 +89,8 @@ class PropertyMonitor : public QObject
void unsubscribe(UnsubscribeRequest*);
Property *add(const QString &);
void write(WriteRequest *);
void refresh(RefreshRequest*);

QMap<QString, QSet<ContextPropertyPrivate const*> > targets_;
QMap<QString, Property*> properties_;

Expand Down Expand Up @@ -121,6 +124,8 @@ class ContextPropertyPrivate : public QObject
static void ignoreCommander();
static void setTypeCheck(bool typeCheck);

void refresh() const;

signals:
void valueChanged() const;

Expand Down
2 changes: 1 addition & 1 deletion src/qml/plugin.cpp
Expand Up @@ -12,5 +12,5 @@

void StatefsPlugin::registerTypes(char const* uri)
{
qmlRegisterType<StateMonitor>(uri, 1, 0, "StateMonitor");
qmlRegisterType<StateProperty>(uri, 1, 0, "StateProperty");
}
36 changes: 26 additions & 10 deletions src/qml/property.cpp
Expand Up @@ -8,53 +8,69 @@
#include "property.hpp"

using statefs::qt::DiscreteProperty;
using statefs::qt::PropertyWriter;

StateMonitor::StateMonitor(QObject* parent)
StateProperty::StateProperty(QObject* parent)
: QObject(parent)
, isActive_(false)
, impl_(null_qobject_unique<DiscreteProperty>())
, writer_(null_qobject_unique<PropertyWriter>())
{
}

StateMonitor::~StateMonitor()
StateProperty::~StateProperty()
{
}

QString StateMonitor::getKey() const
QString StateProperty::getKey() const
{
return key_;
}

void StateMonitor::updateImpl()
void StateProperty::updateImpl()
{
if (isActive_ && !key_.isEmpty()) {
impl_ = make_qobject_unique<DiscreteProperty>(key_);
connect(impl_.get(), &DiscreteProperty::changed
, this, &StateMonitor::onValueChanged);
, this, &StateProperty::onValueChanged);
} else {
impl_.reset();
}
}

void StateMonitor::setKey(QString key)
void StateProperty::setKey(QString key)
{
if (key_ != key) {
key_ = std::move(key);
writer_.reset();
updateImpl();
}
}

QVariant StateMonitor::getValue() const
QVariant StateProperty::getValue() const
{
return value_;
}

bool StateMonitor::getActive() const
void StateProperty::setValue(QVariant v)
{
if (!writer_)
writer_ = make_qobject_unique<PropertyWriter>(key_, this);
writer_->set(std::move(v));
}

void StateProperty::refresh() const
{
if (impl_)
impl_->refresh();
}

bool StateProperty::getActive() const
{
return isActive_;
}

void StateMonitor::setActive(bool v)
void StateProperty::setActive(bool v)
{
if (v != isActive_) {
isActive_ = v;
Expand All @@ -63,7 +79,7 @@ void StateMonitor::setActive(bool v)
}
}

void StateMonitor::onValueChanged(QVariant v)
void StateProperty::onValueChanged(QVariant v)
{
value_ = std::move(v);
emit valueChanged();
Expand Down
15 changes: 12 additions & 3 deletions src/qml/property.hpp
Expand Up @@ -13,7 +13,7 @@
#include <statefs/qt/client.hpp>
#include <qtaround/util.hpp>

class StateMonitor : public QObject
class StateProperty : public QObject
{
Q_OBJECT
Q_CLASSINFO("DefaultProperty", "value")
Expand All @@ -24,6 +24,7 @@ class StateMonitor : public QObject

Q_PROPERTY(QVariant value
READ getValue
WRITE setValue
NOTIFY valueChanged)

Q_PROPERTY(bool active
Expand All @@ -32,17 +33,24 @@ class StateMonitor : public QObject
NOTIFY activeChanged)

public:
StateMonitor(QObject* parent = 0);
~StateMonitor();
StateProperty(QObject* parent = 0);
~StateProperty();

StateProperty(StateProperty const &) = delete;
StateProperty& operator = (StateProperty const &) = delete;

QString getKey() const;
void setKey(QString);

QVariant getValue() const;
void setValue(QVariant);

bool getActive() const;
void setActive(bool);

public slots:
void refresh() const;

signals:
void valueChanged();
void activeChanged();
Expand All @@ -57,6 +65,7 @@ private slots:
QVariant value_;
bool isActive_;
UNIQUE_PTR(statefs::qt::DiscreteProperty) impl_;
UNIQUE_PTR(statefs::qt::PropertyWriter) writer_;
};

#endif // _STATEFS_QML_PROPERTY_HPP_

0 comments on commit c8a1f56

Please sign in to comment.