bool KHotKeys::init() { khotkeys_inited = true; // Check if khotkeys is running QDBusConnection bus = QDBusConnection::sessionBus(); khotkeysInterface = new OrgKdeKhotkeysInterface( "org.kde.kded", "/modules/khotkeys", bus, NULL); QDBusError err; if(!khotkeysInterface->isValid()) { err = khotkeysInterface->lastError(); if (err.isValid()) { kError() << err.name() << ":" << err.message(); } KMessageBox::error( NULL, "<qt>" + i18n("Unable to contact khotkeys. Your changes are saved, but they could not be activated.") + "</qt>" ); } khotkeys_present = khotkeysInterface->isValid(); return true; }
bool DBusLauncherImpl::launch(const QString& fileName, const QString& mimeType, QWidget* window) { QFileInfo fileInfo(fileName); if (!fileInfo.exists() || !fileInfo.isReadable()) return false; if (mimeType.isEmpty()) return launch(fileName, window); QDBusError error; QValueList<QVariant> params; params.append(fileInfo.absFilePath()); params.append(mimeType); QDBusMessage reply = m_proxy->sendWithReply("LaunchFile", params, &error); if (error.isValid()) { qWarning("Error while launching through D-Bus:\nerror '%s'\nmessage '%s'", error.name().local8Bit().data(), error.message().local8Bit().data()); return false; } if (reply.count() != 1 || reply[0].type() != QVariant::Bool) { qWarning("Unexpected launcher reply"); return false; } return reply[0].toBool(); }
bool DBusLauncherImpl::launch(const QUrl& url, const QString& mimeType, QWidget* window) { if (!url.isValid()) return false; if (url.isLocalFile()) return launch(url.path(), mimeType, window); if (mimeType.isEmpty()) return launch(url, window); QDBusError error; QValueList<QVariant> params; params.append(url.toString()); params.append(mimeType); QDBusMessage reply = m_proxy->sendWithReply("LaunchURL", params, &error); if (error.isValid()) { qWarning("Error while launching through D-Bus:\nerror '%s'\nmessage '%s'", error.name().local8Bit().data(), error.message().local8Bit().data()); return false; } if (reply.count() != 1 || reply[0].type() != QVariant::Bool) { qWarning("Unexpected launcher reply"); return false; } return reply[0].toBool(); }
QScriptValue qDBusErrorToScriptValue(QScriptEngine *engine, const QDBusError &error) { QScriptValue v = engine->newObject(); v.setProperty(QLatin1String("type"), QScriptValue(engine, error.type()), QScriptValue::ReadOnly); v.setProperty(QLatin1String("name"), QScriptValue(engine, error.name()), QScriptValue::ReadOnly); v.setProperty(QLatin1String("message"), QScriptValue(engine, error.message()), QScriptValue::ReadOnly); v.setProperty(QLatin1String("isValid"), QScriptValue(engine, error.isValid()), QScriptValue::ReadOnly); return v; }
bool TimeDateCtl::setUseNtp(bool value, QString& errorMessage) { mIface->call("SetNTP", value, true); QDBusError err = mIface->lastError(); if(err.isValid()) { errorMessage = err.message(); return false; } return true; }
bool TimeDateCtl::setTimeZone(QString timeZone, QString& errorMessage) { mIface->call("SetTimezone", timeZone, true); QDBusError err = mIface->lastError(); if(err.isValid()) { errorMessage = err.message(); return false; } return true; }
QString KWinWaylandTouchpad::valueWriter(const Prop<T> &prop) { if (!prop.changed()) { return QString(); } m_iface->setProperty(prop.dbus, prop.val); QDBusError error = m_iface->lastError(); if (error.isValid()) { qCCritical(KCM_TOUCHPAD) << error.message(); return error.message(); } return QString(); }
bool TimeDateCtl::setDateTime(QDateTime dateTime, QString& errorMessage) { // the timedatectl dbus service accepts "usec" input. // Qt can only get "msec" => convert to usec here. mIface->call("SetTime", dateTime.toMSecsSinceEpoch() * 1000, false, true); QDBusError err = mIface->lastError(); if(err.isValid()) { errorMessage = err.message(); return false; } return true; }
bool ServiceControl::stopService() { QDBusError reply; systemd->call("StopUnit", ROCKPOOLD_SYSTEMD_UNIT, "replace"); systemd->call("DisableUnitFiles", QStringList() << ROCKPOOLD_SYSTEMD_UNIT, false); if (reply.isValid()) { qWarning() << reply.message(); return false; } else { systemd->call("Reload"); return true; } }
void KCMHotkeysPrivate::save() { if (current) applyCurrentItem(); // Write the settings model->save(); if (!KHotKeys::Daemon::isRunning()) { if (!KHotKeys::Daemon::start()) { // On startup the demon does the updating stuff, therefore reload // the actions. model->load(); } else { KMessageBox::error( q, "<qt>" + i18n("Unable to contact khotkeys. Your changes are saved, but they could not be activated.") + "</qt>" ); } return; } // Inform kdedkhotkeys demon to reload settings QDBusConnection bus = QDBusConnection::sessionBus(); QPointer<OrgKdeKhotkeysInterface> iface = new OrgKdeKhotkeysInterface( "org.kde.kded", "/modules/khotkeys", bus, q); QDBusError err; if(!iface->isValid()) { err = iface->lastError(); if (err.isValid()) { kError() << err.name() << ":" << err.message(); } KMessageBox::error( q, "<qt>" + i18n("Unable to contact khotkeys. Your changes are saved, but they could not be activated.") + "</qt>" ); return; } // Reread the configuration. We have no possibility to check if it worked. iface->reread_configuration(); }
void LauncherService::sendReply(const QDBusMessage& message, bool ret, const QDBusError& error) { QDBusMessage reply; if (error.isValid()) { reply = QDBusMessage::methodError(message, error); } else { reply = QDBusMessage::methodReply(message); reply.append(QVariant(ret, ret)); } m_connection.send(reply); }
QDBusMessage QDBusMessage::methodError(const QDBusMessage &other, const QDBusError& error) { Q_ASSERT(other.d->msg); QDBusMessage message; if (!error.isValid()) { qWarning("QDBusMessage: error passed to methodError() is not valid!"); return message; } message.d->type = DBUS_MESSAGE_TYPE_ERROR; message.d->reply = dbus_message_ref(other.d->msg); message.d->error = error; return message; }
/*! \internal Fills in the QDBusReply data \a error and \a data from the reply message \a reply. */ void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data) { error = reply; if (error.isValid()) { data = QVariant(); // clear it return; } if (reply.arguments().count() >= 1 && reply.arguments().at(0).userType() == data.userType()) { data = reply.arguments().at(0); return; } const char *expectedSignature = 0; QByteArray receivedSignature; if (reply.arguments().count() >= 1 && reply.arguments().at(0).userType() == QDBusMetaTypeId::argument) { // compare signatures instead QDBusArgument arg = qvariant_cast<QDBusArgument>(reply.arguments().at(0)); expectedSignature = QDBusMetaType::typeToSignature(data.userType()); receivedSignature = arg.currentSignature().toLatin1(); if (receivedSignature == expectedSignature) { // matched. Demarshall it QDBusMetaType::demarshall(arg, data.userType(), data.data()); return; } } // error QString errorMsg = QLatin1String("Unexpected reply signature: got \"%1\", " "expected \"%2\" (%3)"); if (receivedSignature.isEmpty()) receivedSignature = "no signature"; error = QDBusError(QDBusError::InvalidSignature, errorMsg.arg(QLatin1String(receivedSignature), QLatin1String(expectedSignature), QLatin1String(data.typeName()))); data = QVariant(); // clear it }
// Extract a reply from an error. QPyDBusReply::QPyDBusReply(const QDBusError &error) { _q_value = 0; _q_is_valid = !error.isValid(); _q_error = error; }
QT_BEGIN_NAMESPACE /*! \class QDBusReply \inmodule QtDBus \since 4.2 \brief The QDBusReply class stores the reply for a method call to a remote object. A QDBusReply object is a subset of the QDBusMessage object that represents a method call's reply. It contains only the first output argument or the error code and is used by QDBusInterface-derived classes to allow returning the error code as the function's return argument. It can be used in the following manner: \snippet doc/src/snippets/code/src_qdbus_qdbusreply.cpp 0 If the remote method call cannot fail, you can skip the error checking: \snippet doc/src/snippets/code/src_qdbus_qdbusreply.cpp 1 However, if it does fail under those conditions, the value returned by QDBusReply::value() is a default-constructed value. It may be indistinguishable from a valid return value. QDBusReply objects are used for remote calls that have no output arguments or return values (i.e., they have a "void" return type). Use the isValid() function to test if the reply succeeded. \sa QDBusMessage, QDBusInterface */ /*! \fn QDBusReply::QDBusReply(const QDBusMessage &reply) Automatically construct a QDBusReply object from the reply message \a reply, extracting the first return value from it if it is a success reply. */ /*! \fn QDBusReply::QDBusReply(const QDBusPendingReply<T> &reply) Constructs a QDBusReply object from the pending reply message, \a reply. */ /*! \fn QDBusReply::QDBusReply(const QDBusPendingCall &pcall) Automatically construct a QDBusReply object from the asynchronous pending call \a pcall. If the call isn't finished yet, QDBusReply will call QDBusPendingCall::waitForFinished(), which is a blocking operation. If the return types patch, QDBusReply will extract the first return argument from the reply. */ /*! \fn QDBusReply::QDBusReply(const QDBusError &error) Constructs an error reply from the D-Bus error code given by \a error. */ /*! \fn QDBusReply::operator=(const QDBusReply &other) Makes this object be a copy of the object \a other. */ /*! \fn QDBusReply::operator=(const QDBusError &error) Sets this object to contain the error code given by \a error. You can later access it with error(). */ /*! \fn QDBusReply::operator=(const QDBusMessage &message) Makes this object contain the reply specified by message \a message. If \a message is an error message, this function will copy the error code and message into this object If \a message is a standard reply message and contains at least one parameter, it will be copied into this object, as long as it is of the correct type. If it's not of the same type as this QDBusError object, this function will instead set an error code indicating a type mismatch. */ /*! \fn QDBusReply::operator=(const QDBusPendingCall &pcall) Makes this object contain the reply specified by the pending asynchronous call \a pcall. If the call is not finished yet, this function will call QDBusPendingCall::waitForFinished() to block until the reply arrives. If \a pcall finishes with an error message, this function will copy the error code and message into this object If \a pcall finished with a standard reply message and contains at least one parameter, it will be copied into this object, as long as it is of the correct type. If it's not of the same type as this QDBusError object, this function will instead set an error code indicating a type mismatch. */ /*! \fn bool QDBusReply::isValid() const Returns true if no error occurred; otherwise, returns false. \sa error() */ /*! \fn QDBusReply::error() Returns the error code that was returned from the remote function call. If the remote call did not return an error (i.e., if it succeeded), then the QDBusError object that is returned will not be a valid error code (QDBusError::isValid() will return false). \sa isValid() */ /*! \fn QDBusReply::value() const Returns the remote function's calls return value. If the remote call returned with an error, the return value of this function is undefined and may be undistinguishable from a valid return value. This function is not available if the remote call returns \c void. */ /*! \fn QDBusReply::operator Type() const Returns the same as value(). This function is not available if the remote call returns \c void. */ /*! \internal Fills in the QDBusReply data \a error and \a data from the reply message \a reply. */ void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data) { error = reply; if (error.isValid()) { data = QVariant(); // clear it return; } if (reply.arguments().count() >= 1 && reply.arguments().at(0).userType() == data.userType()) { data = reply.arguments().at(0); return; } const char *expectedSignature = QDBusMetaType::typeToSignature(data.userType()); const char *receivedType = 0; QByteArray receivedSignature; if (reply.arguments().count() >= 1) { if (reply.arguments().at(0).userType() == QDBusMetaTypeId::argument) { // compare signatures instead QDBusArgument arg = qvariant_cast<QDBusArgument>(reply.arguments().at(0)); receivedSignature = arg.currentSignature().toLatin1(); if (receivedSignature == expectedSignature) { // matched. Demarshall it QDBusMetaType::demarshall(arg, data.userType(), data.data()); return; } } else { // not an argument and doesn't match? int type = reply.arguments().at(0).userType(); receivedType = QVariant::typeToName(QVariant::Type(type)); receivedSignature = QDBusMetaType::typeToSignature(type); } } // error if (receivedSignature.isEmpty()) receivedSignature = "no signature"; QString errorMsg; if (receivedType) { errorMsg = QString::fromLatin1("Unexpected reply signature: got \"%1\" (%4), " "expected \"%2\" (%3)") .arg(QLatin1String(receivedSignature), QLatin1String(expectedSignature), QLatin1String(data.typeName()), QLatin1String(receivedType)); } else { errorMsg = QString::fromLatin1("Unexpected reply signature: got \"%1\", " "expected \"%2\" (%3)") .arg(QLatin1String(receivedSignature), QLatin1String(expectedSignature), QLatin1String(data.typeName())); } error = QDBusError(QDBusError::InvalidSignature, errorMsg); data = QVariant(); // clear it }
void WatchCommands::musicControl(WatchConnector::MusicControl operation) { logger()->debug() << "Operation:" << operation; QString mpris = parent()->property("mpris").toString(); if (mpris.isEmpty()) { logger()->debug() << "No mpris interface active"; return; } QString method; switch(operation) { case WatchConnector::musicPLAY_PAUSE: method = "PlayPause"; break; case WatchConnector::musicPAUSE: method = "Pause"; break; case WatchConnector::musicPLAY: method = "Play"; break; case WatchConnector::musicNEXT: method = "Next"; break; case WatchConnector::musicPREVIOUS: method = "Previous"; break; case WatchConnector::musicVOLUME_UP: case WatchConnector::musicVOLUME_DOWN: { QDBusReply<QDBusVariant> VolumeReply = QDBusConnection::sessionBus().call( QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Get") << "org.mpris.MediaPlayer2.Player" << "Volume"); if (VolumeReply.isValid()) { double volume = VolumeReply.value().variant().toDouble(); if (operation == WatchConnector::musicVOLUME_UP) { volume += 0.1; } else { volume -= 0.1; } logger()->debug() << "Setting volume" << volume; QDBusError err = QDBusConnection::sessionBus().call( QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Set") << "org.mpris.MediaPlayer2.Player" << "Volume" << QVariant::fromValue(QDBusVariant(volume))); if (err.isValid()) { logger()->error() << err.message(); } } else { logger()->error() << VolumeReply.error().message(); } } return; case WatchConnector::musicGET_NOW_PLAYING: onMprisMetadataChanged(parent()->property("mprisMetadata").toMap()); return; case WatchConnector::musicSEND_NOW_PLAYING: logger()->warn() << "Operation" << operation << "not supported"; return; } if (method.isEmpty()) { logger()->error() << "Requested unsupported operation" << operation; return; } logger()->debug() << operation << "->" << method; QDBusError err = QDBusConnection::sessionBus().call( QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", method)); if (err.isValid()) { logger()->error() << err.message(); } }