diff options
author | joe | 2011-04-21 23:35:02 +0200 |
---|---|---|
committer | joe | 2011-04-21 23:35:02 +0200 |
commit | 7cb508eb877ae0a696427073b1105ee8d9c19876 (patch) | |
tree | 5dd6a3f734fa3de750785d17ec470ccc27e16af7 /src/sysinfo.cpp | |
parent | mainboard serial formatting if empty (diff) | |
download | fbgui-7cb508eb877ae0a696427073b1105ee8d9c19876.tar.gz fbgui-7cb508eb877ae0a696427073b1105ee8d9c19876.tar.xz fbgui-7cb508eb877ae0a696427073b1105ee8d9c19876.zip |
uniformed ident 3-spaced....
Diffstat (limited to 'src/sysinfo.cpp')
-rw-r--r-- | src/sysinfo.cpp | 332 |
1 files changed, 165 insertions, 167 deletions
diff --git a/src/sysinfo.cpp b/src/sysinfo.cpp index ef7dc04..2de7b92 100644 --- a/src/sysinfo.cpp +++ b/src/sysinfo.cpp @@ -34,22 +34,22 @@ SysInfo::~SysInfo() { * @see JavascriptInterface::getSysInfo(const QString& info) */ const QString SysInfo::getInfo(const QString& infoName) { - qxtLog->debug() << "[sysinfo] requested " << infoName; - if (infoName == QString("mac")) - return getMACAddress(); - else if (infoName == QString("ip")) - return getIPAddress(); - else if (infoName == QString("all")) - return getAllInfos(); - else if (infoName == QString("mbserial")) - return getMainboardSerial(); - else if (infoName == QString("usb")) - return getUsbVendorIdProductIdSerialNumber(); - else if (infoName == QString("json")) - return getNames(); - /* unknown code */ - qxtLog->debug() << "[sysinfo] unknown requested"; - return "info_error"; + qxtLog->debug() << "[sysinfo] requested " << infoName; + if (infoName == QString("mac")) + return getMACAddress(); + else if (infoName == QString("ip")) + return getIPAddress(); + else if (infoName == QString("all")) + return getAllInfos(); + else if (infoName == QString("mbserial")) + return getMainboardSerial(); + else if (infoName == QString("usb")) + return getUsbVendorIdProductIdSerialNumber(); + else if (infoName == QString("json")) + return getNames(); + /* unknown code */ + qxtLog->debug() << "[sysinfo] unknown requested"; + return "info_error"; } // ------------------------------------------------------------------------------------------------ /** @@ -68,16 +68,16 @@ const QString SysInfo::getInfo(const QString& infoName) { * @see SysInfo::getInfo(const QString& infoName) */ const QString SysInfo::getMACAddress() { - // Returns MAC address of eth0 for now - QNetworkInterface qni = QNetworkInterface::interfaceFromName( - QString("eth0")); - if (!qni.isValid()) { - qxtLog->debug() - << "[sysinfo] MAC Address: No interface matching \"eth0\" found."; - return "no_eth0"; - } - //eth0_index = qni.index(); - return qni.hardwareAddress(); + // Returns MAC address of eth0 for now + QNetworkInterface qni = + QNetworkInterface::interfaceFromName(QString("eth0")); + if (!qni.isValid()) { + qxtLog->debug() + << "[sysinfo] MAC Address: No interface matching \"eth0\" found."; + return "no_eth0"; + } + //eth0_index = qni.index(); + return qni.hardwareAddress(); } // ------------------------------------------------------------------------------------------------ /** @@ -94,22 +94,22 @@ const QString SysInfo::getMACAddress() { * @see SysInfo::getInfo(const QString& infoName) */ const QString SysInfo::getIPAddress() { - // Again for eth0 only at the moment. - // TODO: this doesn't quite work yet... - QNetworkInterface qni = QNetworkInterface::interfaceFromName( - QString("eth0")); - QList<QHostAddress> addrlist = qni.allAddresses(); - // This is does not return the right IP atm... - foreach(QHostAddress addr, addrlist) - { - if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr - != QHostAddress::LocalHost) { - return addr.toString(); - } - } - // still here? - qxtLog->debug() << "[sysinfo] IP Address: ip_error"; - return "ip_error"; + // Again for eth0 only at the moment. + // TODO: this doesn't quite work yet... + QNetworkInterface qni = + QNetworkInterface::interfaceFromName(QString("eth0")); + QList<QHostAddress> addrlist = qni.allAddresses(); + // This is does not return the right IP atm... + foreach(QHostAddress addr, addrlist) + { + if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr + != QHostAddress::LocalHost) { + return addr.toString(); + } + } + // still here? + qxtLog->debug() << "[sysinfo] IP Address: ip_error"; + return "ip_error"; } // ------------------------------------------------------------------------------------------------ /** @@ -117,22 +117,22 @@ const QString SysInfo::getIPAddress() { */ const QByteArray SysInfo::getNames() { - QVariantMap foo; - foo.insert("name", "foo"); - foo.insert("type", 123); + QVariantMap foo; + foo.insert("name", "foo"); + foo.insert("type", 123); - QVariantMap fooo; - fooo.insert("name", "boo"); - fooo.insert("type", 321); + QVariantMap fooo; + fooo.insert("name", "boo"); + fooo.insert("type", 321); - QVariantList jsonV; - jsonV << foo << fooo; + QVariantList jsonV; + jsonV << foo << fooo; - QJson::Serializer serializer; - QByteArray json = serializer.serialize(jsonV); + QJson::Serializer serializer; + QByteArray json = serializer.serialize(jsonV); - qxtLog->debug() << json; - return json; + qxtLog->debug() << json; + return json; } // ------------------------------------------------------------------------------------------------ @@ -140,17 +140,17 @@ const QByteArray SysInfo::getNames() { * just a test method for json. */ QString SysInfo::getAllInfos() { - QVariantMap infos; - infos.insert("mac", getMACAddress()); - infos.insert("ip", getIPAddress()); - infos.insert("whoami", getScriptOutput("whoami")); - //infos.insert("pwd", getScriptOutput("pwd")); + QVariantMap infos; + infos.insert("mac", getMACAddress()); + infos.insert("ip", getIPAddress()); + infos.insert("whoami", getScriptOutput("whoami")); + //infos.insert("pwd", getScriptOutput("pwd")); - //QJson::Serializer serializer; - QByteArray json = serializer.serialize(infos); + //QJson::Serializer serializer; + QByteArray json = serializer.serialize(infos); - qxtLog->debug() << json; - return json; + qxtLog->debug() << json; + return json; } // ------------------------------------------------------------------------------------------------ @@ -170,27 +170,27 @@ QString SysInfo::getAllInfos() { * @see SysInfo::getInfo(const QString& infoName) */ const QString SysInfo::getMainboardSerial() { - QString out = ""; - struct sysfs_class_device *class_device = sysfs_open_class_device("dmi", - "id"); - struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); - struct sysfs_device *device = sysfs_get_classdev_device(class_device); + QString out = ""; + struct sysfs_class_device *class_device = sysfs_open_class_device("dmi", + "id"); + struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); + struct sysfs_device *device = sysfs_get_classdev_device(class_device); - if (attrlist != NULL) { - struct sysfs_attribute *attr = NULL; - dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { - QVariantMap a; - if(QString(attr->name) == QString("board_serial")) { - out = QString(attr->value); - } - } - qxtLog->debug() << "[sysinfo] Mainboard Serial: " + out; - return out; - } - qxtLog->debug() - << "[sysinfo] Mainboard Serial: attrlist is null! return: mainboard_serial_error"; - sysfs_close_class_device(class_device); - return "mainboard_serial_error"; + if (attrlist != NULL) { + struct sysfs_attribute *attr = NULL; + dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { + QVariantMap a; + if(QString(attr->name) == QString("board_serial")) { + out = QString(attr->value); + } + } + qxtLog->debug() << "[sysinfo] Mainboard Serial: " + out; + return out; + } + qxtLog->debug() + << "[sysinfo] Mainboard Serial: attrlist is null! return: mainboard_serial_error"; + sysfs_close_class_device(class_device); + return "mainboard_serial_error"; } // ------------------------------------------------------------------------------------------------ /** @@ -215,76 +215,75 @@ const QString SysInfo::getMainboardSerial() { * @see SysInfo::getInfo(const QString& infoName) */ const QString SysInfo::getUsbVendorIdProductIdSerialNumber() { - QString tag = "[sysinfo] Usb Serial:"; - QString out = ""; - QVariantList list; + QString tag = "[sysinfo] Usb Serial:"; + QString out = ""; + QVariantList list; - libusb_device **devs; - libusb_context *ctx = NULL; //a libusb session - ssize_t cnt; //holding number of devices in list - int r = 1; - r = libusb_init(&ctx); - if (r < 0) { - qxtLog->debug() << tag + "failed to initialise libusb"; - return "error"; - } - cnt = libusb_get_device_list(ctx, &devs); //get the list of devices - if (cnt < 0) { - qxtLog->debug() << tag + "Get Device Error"; //there was an error - } - qxtLog->debug() << tag + cnt + " Devices in list."; //print total number of usb devices - ssize_t i; //for iterating through the list# - for (i = 0; i < cnt; i++) { - //printdev(devs[i]); //print specs of this device - QVariantMap infos; - libusb_device *dev = devs[i]; - libusb_device_descriptor desc; - int re = libusb_get_device_descriptor(dev, &desc); - if (re < 0) { - qxtLog->debug() << tag + "failed to get device descriptor"; - return "error"; - } - infos.insert("vendorId", desc.idVendor); - infos.insert("productId", desc.idProduct); - unsigned char string[256]; - libusb_device_handle *handle; - re = libusb_open(dev, &handle); - if (re != 0) { - qxtLog->debug() << tag - + "failed to get handler / fail to open device"; - return "error"; - } - re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, - string, sizeof(string)); - if (re < 0) { - qxtLog->debug() << tag + "failed to get SerialNumber"; - return "error"; - } - infos.insert("serialnumber", QString((const char *) string)); - re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, - sizeof(string)); - if (re < 0) { - qxtLog->debug() << tag + "failed to get Product"; - return "error"; - } - infos.insert("product", QString((const char *) string)); - re = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, - string, sizeof(string)); - if (re < 0) { - qxtLog->debug() << tag + "failed to get Product"; - return "error"; - } - infos.insert("manuacturer", QString((const char *) string)); + libusb_device **devs; + libusb_context *ctx = NULL; //a libusb session + ssize_t cnt; //holding number of devices in list + int r = 1; + r = libusb_init(&ctx); + if (r < 0) { + qxtLog->debug() << tag + "failed to initialise libusb"; + return "error"; + } + cnt = libusb_get_device_list(ctx, &devs); //get the list of devices + if (cnt < 0) { + qxtLog->debug() << tag + "Get Device Error"; //there was an error + } + qxtLog->debug() << tag + cnt + " Devices in list."; //print total number of usb devices + ssize_t i; //for iterating through the list# + for (i = 0; i < cnt; i++) { + //printdev(devs[i]); //print specs of this device + QVariantMap infos; + libusb_device *dev = devs[i]; + libusb_device_descriptor desc; + int re = libusb_get_device_descriptor(dev, &desc); + if (re < 0) { + qxtLog->debug() << tag + "failed to get device descriptor"; + return "error"; + } + infos.insert("vendorId", desc.idVendor); + infos.insert("productId", desc.idProduct); + unsigned char string[256]; + libusb_device_handle *handle; + re = libusb_open(dev, &handle); + if (re != 0) { + qxtLog->debug() << tag + "failed to get handler / fail to open device"; + return "error"; + } + re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, + string, sizeof(string)); + if (re < 0) { + qxtLog->debug() << tag + "failed to get SerialNumber"; + return "error"; + } + infos.insert("serialnumber", QString((const char *) string)); + re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, + sizeof(string)); + if (re < 0) { + qxtLog->debug() << tag + "failed to get Product"; + return "error"; + } + infos.insert("product", QString((const char *) string)); + re = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, + string, sizeof(string)); + if (re < 0) { + qxtLog->debug() << tag + "failed to get Product"; + return "error"; + } + infos.insert("manuacturer", QString((const char *) string)); - list << infos; - libusb_close(handle); - } - libusb_free_device_list(devs, 1); //free the list, unref the devices in it - libusb_exit(ctx); //close the session + list << infos; + libusb_close(handle); + } + libusb_free_device_list(devs, 1); //free the list, unref the devices in it + libusb_exit(ctx); //close the session - QByteArray json = serializer.serialize(list); - qxtLog->debug() << tag + "json object: " + json; - return json; + QByteArray json = serializer.serialize(list); + qxtLog->debug() << tag + "json object: " + json; + return json; } // ------------------------------------------------------------------------------------------------ @@ -302,26 +301,25 @@ const QString SysInfo::getUsbVendorIdProductIdSerialNumber() { * output of the script. */ QString SysInfo::getScriptOutput(QString cmd) { - QProcess *process = new QProcess(); - qxtLog->debug() << "[sysinfo] Script Output: try to open: " << cmd; - process->start(cmd, QIODevice::ReadOnly); + QProcess *process = new QProcess(); + qxtLog->debug() << "[sysinfo] Script Output: try to open: " << cmd; + process->start(cmd, QIODevice::ReadOnly); - if (!process->waitForStarted()) - qxtLog->debug() - << "[sysinfo] Script Output: process couldn't get opened"; + if (!process->waitForStarted()) + qxtLog->debug() << "[sysinfo] Script Output: process couldn't get opened"; - QString output; - process->waitForFinished(); + QString output; + process->waitForFinished(); - QTextStream *txt_stream = new QTextStream(process); + QTextStream *txt_stream = new QTextStream(process); - while (!txt_stream->atEnd()) { - qxtLog->debug() << "[sysinfo] Script Output: read output: "; - QString tmp_str = txt_stream->readLine(); - output += tmp_str; - qxtLog->debug() << "[sysinfo] Script Output: " << tmp_str; - } - qxtLog->debug() << "[sysinfo] Script Output: process finished: "; - return output; + while (!txt_stream->atEnd()) { + qxtLog->debug() << "[sysinfo] Script Output: read output: "; + QString tmp_str = txt_stream->readLine(); + output += tmp_str; + qxtLog->debug() << "[sysinfo] Script Output: " << tmp_str; + } + qxtLog->debug() << "[sysinfo] Script Output: process finished: "; + return output; } |