QFuture<Platform::QueryChangeStateResult> UPower::CanChangeState (State state) { return QtConcurrent::run ([state] () -> QueryChangeStateResult { QDBusInterface face ("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus ()); if (!face.isValid ()) return { false, tr ("Cannot connect to UPower daemon.") }; return { face.property ("Can" + State2Method (state)).toBool (), {} }; }); }
void DBusConnector::requeryDevice (const QString& id) { QDBusInterface face ("org.freedesktop.UPower", id, "org.freedesktop.UPower.Device", SB_); if (face.property ("Type").toInt () != 2) return; BatteryInfo info; info.ID_ = id; info.Percentage_ = face.property ("Percentage").toInt (); info.TimeToFull_ = face.property ("TimeToFull").toLongLong (); info.TimeToEmpty_ = face.property ("TimeToEmpty").toLongLong (); info.Voltage_ = face.property ("Voltage").toDouble (); info.Energy_ = face.property ("Energy").toDouble (); info.EnergyFull_ = face.property ("EnergyFull").toDouble (); info.DesignEnergyFull_ = face.property ("EnergyFullDesign").toDouble (); info.EnergyRate_ = face.property ("EnergyRate").toDouble (); info.Technology_ = TechIdToString (face.property ("Technology").toInt ()); info.Temperature_ = 0; emit batteryInfoUpdated (info); }
int main(int argc, char *argv[]) { // TODO: Clean this up, update API, etc. if (argc < 5 || argc > 6) { qWarning() << "Error detected! Insufficient number of arguments."; qWarning() << ""; qWarning() << "Usage: ambdbusaccess <R/W> <Object> <Property> <Zone> [Value]"; qWarning() << "- <R/W>"; qWarning() << " Used for specifying [R]ead or [W]rite."; qWarning() << "- <Object>"; qWarning() << " The AMB object to access."; qWarning() << "- <Property>"; qWarning() << " The property within the object to access."; qWarning() << "- <Zone>"; qWarning() << " The AMB zone to access."; qWarning() << "- [Value]"; qWarning() << " The value to write, if writing."; qWarning() << "Example: ambdbusaccess Write ClimateControl FanSpeedLevel 0 7"; qWarning() << ""; qWarning() << "This program returns an int under the following conditions:"; qWarning() << "Successful Read: <Value Read>"; qWarning() << "Unsuccessful Read: -1"; qWarning() << "Successful Write: <Value Written>"; qWarning() << "Unsuccessful Write: -1"; return -1; } // TODO: Error check input. bool read = !strncmp(argv[1], "R", 1); QString object = argv[2]; QString property = argv[3]; qint32 zone = atoi(argv[4]); qint32 value = 0; if (argc == 6) { value = atoi(argv[5]); } // Necessary to suppress Qt messages about touching the D-Bus before the application was created. QCoreApplication a(argc, argv); // Sanity check that the system bus is actually present. if (!QDBusConnection::systemBus().isConnected()) { qCritical() << "Could not access system D-Bus!"; return -1; } // Get ahold of the manager object. QDBusInterface *manager = new QDBusInterface("org.automotive.message.broker", "/", "org.automotive.Manager", QDBusConnection::systemBus()); // Go fetch the path for the AMB object we are concerned with. qDebug().nospace() << "Looking for property " << property.toStdString().c_str() << " of object " << object.toStdString().c_str() << " in zone " << zone << "."; QDBusReply<QDBusObjectPath> propertyPath = manager->call("FindObjectForZone", object.toStdString().c_str(), zone); if (!propertyPath.isValid()) { qDebug() << "Invalid reply!" << propertyPath.error() << "Got the path:" << propertyPath.value().path(); } // Now that we have the path, open an interface to the object. QDBusInterface *propertyInterface = new QDBusInterface("org.automotive.message.broker", propertyPath.value().path(), "org.automotive.ClimateControl", QDBusConnection::systemBus()); // Perform our read or write operation. if (read) { QVariant reply = propertyInterface->property(property.toStdString().c_str()); if (!reply.isValid()) { qDebug() << "Invalid reply when reading the property!" << propertyInterface->lastError() << "Property:" << reply.toString(); value = -1; } else { qDebug().nospace() << "Got a valid reply for the property of " << reply.toString().toStdString().c_str() << "."; value = reply.toInt(); } } else { QVariant reply = propertyInterface->setProperty(property.toStdString().c_str(), value); if (reply.toBool()) { qDebug() << "Successfully wrote the property."; } else { qDebug() << "Failed to write the property."; value = -1; } } // Clean up. delete propertyInterface; delete manager; // Either provide the read value or some feedback to the calling application. return value; }
QVariant UnitModel::data(const QModelIndex & index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::DisplayRole) { if (index.column() == 0) return unitList->at(index.row()).load_state; else if (index.column() == 1) return unitList->at(index.row()).active_state; else if (index.column() == 2) return unitList->at(index.row()).sub_state; else if (index.column() == 3) return unitList->at(index.row()).id; } else if (role == Qt::ForegroundRole) { const KColorScheme scheme(QPalette::Normal); if (unitList->at(index.row()).active_state == "active") return scheme.foreground(KColorScheme::PositiveText); else if (unitList->at(index.row()).active_state == "failed") return scheme.foreground(KColorScheme::NegativeText); else if (unitList->at(index.row()).active_state == "-") return scheme.foreground(KColorScheme::InactiveText); else return QVariant(); } else if (role == Qt::ToolTipRole) { QString selUnit = unitList->at(index.row()).id; QString selUnitPath = unitList->at(index.row()).unit_path.path(); QString selUnitFile = unitList->at(index.row()).unit_file; QString toolTipText; toolTipText.append("<FONT COLOR=white>"); toolTipText.append("<b>" + selUnit + "</b><hr>"); // Create a DBus interface QDBusConnection bus(""); if (!userBus.isEmpty()) bus = QDBusConnection::connectToBus(userBus, "org.freedesktop.systemd1"); else bus = QDBusConnection::systemBus(); QDBusInterface *iface; // Use the DBus interface to get unit properties if (!selUnitPath.isEmpty()) { // Unit has a valid path iface = new QDBusInterface ("org.freedesktop.systemd1", selUnitPath, "org.freedesktop.systemd1.Unit", bus); if (iface->isValid()) { // Unit has a valid unit DBus object toolTipText.append(i18n("<b>Description: </b>")); toolTipText.append(iface->property("Description").toString()); toolTipText.append(i18n("<br><b>Unit file: </b>")); toolTipText.append(iface->property("FragmentPath").toString()); toolTipText.append(i18n("<br><b>Unit file state: </b>")); toolTipText.append(iface->property("UnitFileState").toString()); qulonglong ActiveEnterTimestamp = iface->property("ActiveEnterTimestamp").toULongLong(); toolTipText.append(i18n("<br><b>Activated: </b>")); if (ActiveEnterTimestamp == 0) toolTipText.append("n/a"); else { QDateTime timeActivated; timeActivated.setMSecsSinceEpoch(ActiveEnterTimestamp/1000); toolTipText.append(timeActivated.toString()); } qulonglong InactiveEnterTimestamp = iface->property("InactiveEnterTimestamp").toULongLong(); toolTipText.append(i18n("<br><b>Deactivated: </b>")); if (InactiveEnterTimestamp == 0) toolTipText.append("n/a"); else { QDateTime timeDeactivated; timeDeactivated.setMSecsSinceEpoch(InactiveEnterTimestamp/1000); toolTipText.append(timeDeactivated.toString()); } } delete iface; } else { // Unit does not have a valid unit DBus object // Retrieve UnitFileState from Manager object iface = new QDBusInterface ("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", bus); QList<QVariant> args; args << selUnit; toolTipText.append(i18n("<b>Unit file: </b>")); if (!selUnitFile.isEmpty()) toolTipText.append(selUnitFile); toolTipText.append(i18n("<br><b>Unit file state: </b>")); if (!selUnitFile.isEmpty()) toolTipText.append(iface->callWithArgumentList(QDBus::AutoDetect, "GetUnitFileState", args).arguments().at(0).toString()); delete iface; } // Journal entries for units toolTipText.append(i18n("<hr><b>Last log entries:</b>")); QStringList log = getLastJrnlEntries(selUnit); if (log.isEmpty()) toolTipText.append(i18n("<br><i>No log entries found for this unit.</i>")); else { for(int i = log.count()-1; i >= 0; --i) { if (!log.at(i).isEmpty()) toolTipText.append(QString("<br>" + log.at(i))); } } toolTipText.append("</FONT"); return toolTipText; } return QVariant(); }