1. Is it possible to send a message via <dbus/dbus.h> and receive it using Qt one ?
2. Is it possible to send a message as root and receive it from app which runs on different user ? (linux)
1a. I have troubles to read the message that's why the question. This is my code:
Send <dbus/dbus.h>
dbus_error_init(&_dbusError);
_dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &_dbusError);
if (dbus_error_is_set(&_dbusError)) {
logger("Dbus Connection Error");
dbus_error_free(&_dbusError);
return;
} else {
logger("Dbus Connected");
}
_dbusMessage = dbus_message_new_signal("/", "com.app.message", "app");
if (NULL == _dbusMessage) {
logger("Message Null");
exit(1);
} else {
logger("Dbus new signal");
}
dbus_message_iter_init_append(_dbusMessage, &_dbusMessageIter);
if (!dbus_message_iter_append_basic(&_dbusMessageIter, DBUS_TYPE_STRING, &message)) {
logger("Couldn't append dbus message");
} else {
stringstream s;
s << message;
logger("Dbus Messgae append - " + s.str());
}
if (!dbus_connection_send(_dbusConnection, _dbusMessage, &_dbusSerial)) {
logger("Couldn't send dbus message");
} else {
logger("Dbus Messgae sent");
}
dbus_connection_flush(_dbusConnection);
dbus_message_unref(_dbusMessage);
dbus_error_init(&_dbusError);
_dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &_dbusError);
if (dbus_error_is_set(&_dbusError)) {
logger("Dbus Connection Error");
dbus_error_free(&_dbusError);
return;
} else {
logger("Dbus Connected");
}
_dbusMessage = dbus_message_new_signal("/", "com.app.message", "app");
if (NULL == _dbusMessage) {
logger("Message Null");
exit(1);
} else {
logger("Dbus new signal");
}
dbus_message_iter_init_append(_dbusMessage, &_dbusMessageIter);
if (!dbus_message_iter_append_basic(&_dbusMessageIter, DBUS_TYPE_STRING, &message)) {
logger("Couldn't append dbus message");
} else {
stringstream s;
s << message;
logger("Dbus Messgae append - " + s.str());
}
if (!dbus_connection_send(_dbusConnection, _dbusMessage, &_dbusSerial)) {
logger("Couldn't send dbus message");
} else {
logger("Dbus Messgae sent");
}
dbus_connection_flush(_dbusConnection);
dbus_message_unref(_dbusMessage);
To copy to clipboard, switch view to plain text mode
Read <QtDBus/QtDBus> example from ping pong
{
return QString("ping(\"%1\") got called").
arg(arg
);
}
int main(int argc, char **argv)
{
cout << "Cannot connect to the D-Bus session bus" << endl;
return 1;
}
cout << "error" << endl;
exit(1);
}
Pong pong;
app.exec();
return 0;
}
QString Pong::ping(const QString &arg)
{
QMetaObject::invokeMethod(QCoreApplication::instance(), "quit");
return QString("ping(\"%1\") got called").arg(arg);
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
cout << "Cannot connect to the D-Bus session bus" << endl;
return 1;
}
if (!QDBusConnection::sessionBus().registerService("com.app.message")) {
cout << "error" << endl;
exit(1);
}
Pong pong;
QDBusConnection::sessionBus().registerObject("/", &pong, QDBusConnection::ExportAllSlots);
app.exec();
return 0;
}
To copy to clipboard, switch view to plain text mode
Thanks for looking
Bookmarks