diff options
author | Laszlo Papp <lpapp@kde.org> | 2014-01-21 17:10:30 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-01-22 09:04:06 +0100 |
commit | a7471c9159b254c2d5a21ddd9772fac8fe7d1736 (patch) | |
tree | 766b356ea398a0ee99a38653e7f9578d38cdb990 /src | |
parent | 2a83fbd6c70032d236bbdf0d87aaf51908cd5afd (diff) | |
download | qtserialport-a7471c9159b254c2d5a21ddd9772fac8fe7d1736.tar.gz |
Remove an unnecessary boolean interim variable
This is needless because it is only used once, right after the creation.
Moreover, we would probably need to separate the two calls later into distinct
introspection conditions to give a bit more correct warning whether the sysfs is
present, or it is just not readable for some reason.
It is also consistent with some other places.
It also eliminates the needless indentation level for that relatively long
block.
Change-Id: Ib66e3033c2439315a8034101129cadd2dcdc29d0
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/serialport/qserialportinfo_unix.cpp | 112 |
1 files changed, 56 insertions, 56 deletions
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 4433a4e..80b2f7d 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -115,81 +115,81 @@ QList<QSerialPortInfo> availablePortsByFiltersOfDevices() QList<QSerialPortInfo> availablePortsBySysfs() { - QList<QSerialPortInfo> serialPortInfoList; QDir ttySysClassDir(QStringLiteral("/sys/class/tty")); - const bool sysfsEnabled = ttySysClassDir.exists() && ttySysClassDir.isReadable(); - if (sysfsEnabled) { - ttySysClassDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QFileInfo &fileInfo, ttySysClassDir.entryInfoList()) { - if (!fileInfo.isSymLink()) - continue; + if (!(ttySysClassDir.exists() && ttySysClassDir.isReadable())) + return QList<QSerialPortInfo>(); - const QString targetPath = fileInfo.symLinkTarget(); - const int lastIndexOfSlash = targetPath.lastIndexOf(QLatin1Char('/')); - if (lastIndexOfSlash == -1) - continue; + QList<QSerialPortInfo> serialPortInfoList; + ttySysClassDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); + foreach (const QFileInfo &fileInfo, ttySysClassDir.entryInfoList()) { + if (!fileInfo.isSymLink()) + continue; - QSerialPortInfo serialPortInfo; + const QString targetPath = fileInfo.symLinkTarget(); + const int lastIndexOfSlash = targetPath.lastIndexOf(QLatin1Char('/')); + if (lastIndexOfSlash == -1) + continue; - if (targetPath.contains(QStringLiteral("pnp"))) { - // TODO: Obtain more information - } else if (targetPath.contains(QStringLiteral("platform"))) { - continue; - } else if (targetPath.contains(QStringLiteral("usb"))) { + QSerialPortInfo serialPortInfo; - QDir targetDir(targetPath); - targetDir.setFilter(QDir::Files | QDir::Readable); - targetDir.setNameFilters(QStringList(QStringLiteral("uevent"))); + if (targetPath.contains(QStringLiteral("pnp"))) { + // TODO: Obtain more information + } else if (targetPath.contains(QStringLiteral("platform"))) { + continue; + } else if (targetPath.contains(QStringLiteral("usb"))) { - do { - const QFileInfoList entryInfoList = targetDir.entryInfoList(); - if (entryInfoList.isEmpty()) - continue; + QDir targetDir(targetPath); + targetDir.setFilter(QDir::Files | QDir::Readable); + targetDir.setNameFilters(QStringList(QStringLiteral("uevent"))); - QFile uevent(entryInfoList.first().absoluteFilePath()); - if (!uevent.open(QIODevice::ReadOnly | QIODevice::Text)) - continue; + do { + const QFileInfoList entryInfoList = targetDir.entryInfoList(); + if (entryInfoList.isEmpty()) + continue; - const QString ueventContent = QString::fromLatin1(uevent.readAll()); + QFile uevent(entryInfoList.first().absoluteFilePath()); + if (!uevent.open(QIODevice::ReadOnly | QIODevice::Text)) + continue; - if (ueventContent.contains(QStringLiteral("DEVTYPE=usb_device")) - && ueventContent.contains(QStringLiteral("DRIVER=usb"))) { + const QString ueventContent = QString::fromLatin1(uevent.readAll()); - QFile description(QFileInfo(targetDir, QStringLiteral("product")).absoluteFilePath()); - if (description.open(QIODevice::ReadOnly | QIODevice::Text)) - serialPortInfo.d_ptr->description = QString::fromLatin1(description.readAll()).simplified(); + if (ueventContent.contains(QStringLiteral("DEVTYPE=usb_device")) + && ueventContent.contains(QStringLiteral("DRIVER=usb"))) { - QFile manufacturer(QFileInfo(targetDir, QStringLiteral("manufacturer")).absoluteFilePath()); - if (manufacturer.open(QIODevice::ReadOnly | QIODevice::Text)) - serialPortInfo.d_ptr->manufacturer = QString::fromLatin1(manufacturer.readAll()).simplified(); + QFile description(QFileInfo(targetDir, QStringLiteral("product")).absoluteFilePath()); + if (description.open(QIODevice::ReadOnly | QIODevice::Text)) + serialPortInfo.d_ptr->description = QString::fromLatin1(description.readAll()).simplified(); - QFile vendorIdentifier(QFileInfo(targetDir, QStringLiteral("idVendor")).absoluteFilePath()); - if (vendorIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16); - } + QFile manufacturer(QFileInfo(targetDir, QStringLiteral("manufacturer")).absoluteFilePath()); + if (manufacturer.open(QIODevice::ReadOnly | QIODevice::Text)) + serialPortInfo.d_ptr->manufacturer = QString::fromLatin1(manufacturer.readAll()).simplified(); - QFile productIdentifier(QFileInfo(targetDir, QStringLiteral("idProduct")).absoluteFilePath()); - if (productIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { - serialPortInfo.d_ptr->productIdentifier = QString::fromLatin1(productIdentifier.readAll()) - .toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16); - } + QFile vendorIdentifier(QFileInfo(targetDir, QStringLiteral("idVendor")).absoluteFilePath()); + if (vendorIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { + serialPortInfo.d_ptr->vendorIdentifier = QString::fromLatin1(vendorIdentifier.readAll()) + .toInt(&serialPortInfo.d_ptr->hasVendorIdentifier, 16); + } - break; + QFile productIdentifier(QFileInfo(targetDir, QStringLiteral("idProduct")).absoluteFilePath()); + if (productIdentifier.open(QIODevice::ReadOnly | QIODevice::Text)) { + serialPortInfo.d_ptr->productIdentifier = QString::fromLatin1(productIdentifier.readAll()) + .toInt(&serialPortInfo.d_ptr->hasProductIdentifier, 16); } - } while (targetDir.cdUp()); - } else if (targetPath.contains(QStringLiteral("pci"))) { - // TODO: Obtain more information about the device - } else { - continue; - } + break; + } + } while (targetDir.cdUp()); - serialPortInfo.d_ptr->portName = targetPath.mid(lastIndexOfSlash + 1); - serialPortInfo.d_ptr->device = QSerialPortPrivate::portNameToSystemLocation(serialPortInfo.d_ptr->portName); - serialPortInfoList.append(serialPortInfo); + } else if (targetPath.contains(QStringLiteral("pci"))) { + // TODO: Obtain more information about the device + } else { + continue; } + + serialPortInfo.d_ptr->portName = targetPath.mid(lastIndexOfSlash + 1); + serialPortInfo.d_ptr->device = QSerialPortPrivate::portNameToSystemLocation(serialPortInfo.d_ptr->portName); + serialPortInfoList.append(serialPortInfo); } return serialPortInfoList; |