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

Commit

Permalink
tool: allow to write property
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 14, 2014
1 parent 82b2adf commit 4268258
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -43,6 +43,7 @@ include_directories(
link_directories(
# ${STATEFS_LIBS}
${COR_LIBRARY_DIRS}
${QTAROUND_LIBRARY_DIRS}
)

add_custom_target(doc
Expand Down
17 changes: 10 additions & 7 deletions src/contextkit-subscriber/property.cpp
Expand Up @@ -208,8 +208,8 @@ class WriteRequest : public QObject, public Event
, key_(key)
, value_(std::move(value))
{
connect(tgt, &PropertyWriterImpl::updated
, this, &WriteRequest::updated
connect(this, &WriteRequest::updated
, tgt, &PropertyWriterImpl::updated
, Qt::QueuedConnection);
}

Expand Down Expand Up @@ -267,17 +267,19 @@ bool PropertyMonitor::event(QEvent *e)

void PropertyMonitor::write(WriteRequest *req)
{
static const auto mode
= QIODevice::WriteOnly | QIODevice::Unbuffered;
auto isOk = false;
auto emit_on_exit = cor::on_scope_exit([req, isOk]() {
emit req->updated(false);
emit req->updated(isOk);
});
// implementation is quick and dirty: one redundant try to access
// session(user) file
auto const &key = req->key_;
QFile file{statefs::qt::getPath(key)};
if (open(file, QIODevice::WriteOnly) != FileStatus::Opened) {
QFile file(statefs::qt::getPath(key));
if (open(file, mode) != FileStatus::Opened) {
file.setFileName(statefs::qt::getSystemPath(key));
if (open(file, QIODevice::WriteOnly) != FileStatus::Opened) {
if (open(file, mode) != FileStatus::Opened) {
debug::warning("Can't access", key);
return;
}
Expand All @@ -290,7 +292,8 @@ void PropertyMonitor::write(WriteRequest *req)
if (len == data.size()) {
isOk = true;
} else {
debug::warning("Wrong len ", len, " writing", s, " to ", file.fileName());
debug::warning("Wrong len", len, "writing", s, "to"
, file.fileName(), "error", file.error());
}
}

Expand Down
53 changes: 36 additions & 17 deletions tools/monitor/monitor.cpp
Expand Up @@ -12,6 +12,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <QSocketNotifier>
#include <statefs/qt/client.hpp>

static int sigFd[2];

Expand All @@ -21,15 +22,26 @@ void onExit(int)
::write(sigFd[0], &a, sizeof(a));
}

int usage(QStringList const &args, int rc)
{
qDebug() << "Usage: " << args[0] << " <namespace_path>...";
return rc;
}

void writeProp(QString const &key, QString const &v)
{
auto w = new statefs::qt::PropertyWriter{key};
w->set(v);
}

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QSocketNotifier *sigNot;
auto args = app.arguments();
if (args.size() <= 1) {
qDebug() << "Usage: " << args[0] << " <namespace_path>...";
return -1;
}
if (args.size() <= 1)
return usage(args, -1);

::socketpair(AF_UNIX, SOCK_STREAM, 0, sigFd);
sigNot = new QSocketNotifier(sigFd[1], QSocketNotifier::Read, &app);
app.connect(sigNot, &QSocketNotifier::activated, []() {
Expand All @@ -39,19 +51,26 @@ int main(int argc, char *argv[])
});
for (auto i : {SIGTERM, SIGINT})
::signal(i, onExit);
auto dirname = args[1];
QDir d(dirname);
auto files = d.entryList(QDir::Files);
auto prefix = d.dirName() + ".";
files = files.replaceInStrings(QRegExp("^"), prefix);
qDebug() << files;

auto begin = files.begin(), end = files.end();
for (auto pos = begin; pos != end; ++pos) {
auto p = std::make_shared<ContextProperty>(*pos);
app.connect(p.get(), &ContextProperty::valueChanged, [p]() {
qDebug() << p->key() << "=" << p->value();
});
if (args[1] == "-w") {
if (args.size() <= 3)
return usage(args, -1);

writeProp(args[2], args[3]);
} else {
auto dirname = args[1];
QDir d(dirname);
auto files = d.entryList(QDir::Files);
auto prefix = d.dirName() + ".";
files = files.replaceInStrings(QRegExp("^"), prefix);
qDebug() << files;

auto begin = files.begin(), end = files.end();
for (auto pos = begin; pos != end; ++pos) {
auto p = std::make_shared<ContextProperty>(*pos);
app.connect(p.get(), &ContextProperty::valueChanged, [p]() {
qDebug() << p->key() << "=" << p->value();
});
}
}
return app.exec();
}

0 comments on commit 4268258

Please sign in to comment.