[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-hardware-devel
Subject: [Kde-hardware-devel] Making Solid give a visible name for products
From: Albert Astals Cid <aacid () kde ! org>
Date: 2009-04-10 15:00:28
Message-ID: 200904101700.28853.aacid () kde ! org
[Download RAW message or body]
Hi, i'm attaching a patch that adds the visibleName function for Solid
devices, that is useful for places like the places viewer in dolphin or the
device notifier in plasma. Currently when i connect my phone i get "Volume
(fat32)" which sucks for two reasons:
* It's not much detailed
* Can't be translated
With the current patch, the "Can't be translated" part is fixed and now i get
"7,4 GiB External Media" that is not a great improvement but at least can be
translated.
The code is using "weird" variable names like drive_is_hotpluggable because it
tries to be as similar to the original libhal-storage code i've been inspired
by.
I'm not much happy about having to do things like
const OpticalDisc disc(const_cast<HalDevice*>(this));
but didn't find a better way to do it.
Comments?
Albert
["solid_visible_name.patch" (text/x-patch)]
Index: device.h
===================================================================
--- device.h (revision 951665)
+++ device.h (working copy)
@@ -185,6 +185,12 @@
*/
QString icon() const;
+ /**
+ * Retrieves the visible name of device.
+ *
+ * @return the visible name
+ */
+ QString visibleName() const;
/**
* Tests if a device interface is available from the device.
Index: device.cpp
===================================================================
--- device.cpp (revision 951665)
+++ device.cpp (working copy)
@@ -131,6 +131,11 @@
return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), icon());
}
+QString Solid::Device::visibleName() const
+{
+ return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), \
visibleName()); +}
+
bool Solid::Device::isDeviceInterface(const DeviceInterface::Type &type) const
{
return_SOLID_CALL(Ifaces::Device *, d->backendObject(), false, \
queryDeviceInterface(type));
Index: ifaces/device.h
===================================================================
--- ifaces/device.h (revision 951665)
+++ ifaces/device.h (working copy)
@@ -95,6 +95,12 @@
*/
virtual QString icon() const = 0;
+ /**
+ * Retrieves the visible name of device.
+ *
+ * @return the visible name
+ */
+ virtual QString visibleName() const = 0;
/**
* Tests if a property exist.
Index: backends/hal/haldevice.cpp
===================================================================
--- backends/hal/haldevice.cpp (revision 951665)
+++ backends/hal/haldevice.cpp (working copy)
@@ -49,6 +49,52 @@
using namespace Solid::Backends::Hal;
+// Adapted from KLocale as Solid needs to be Qt-only
+static QString formatByteSize(double size)
+{
+ // Per IEC 60027-2
+
+ // Binary prefixes
+ //Tebi-byte TiB 2^40 1,099,511,627,776 bytes
+ //Gibi-byte GiB 2^30 1,073,741,824 bytes
+ //Mebi-byte MiB 2^20 1,048,576 bytes
+ //Kibi-byte KiB 2^10 1,024 bytes
+
+ QString s;
+ // Gibi-byte
+ if ( size >= 1073741824.0 )
+ {
+ size /= 1073741824.0;
+ if ( size > 1024 ) // Tebi-byte
+ s = QObject::tr("%1 TiB").arg(QLocale().toString(size / 1024.0, 'f', \
1)); + else
+ s = QObject::tr("%1 GiB").arg(QLocale().toString(size, 'f', 1));
+ }
+ // Mebi-byte
+ else if ( size >= 1048576.0 )
+ {
+ size /= 1048576.0;
+ s = QObject::tr("%1 MiB").arg(QLocale().toString(size, 'f', 1));
+ }
+ // Kibi-byte
+ else if ( size >= 1024.0 )
+ {
+ size /= 1024.0;
+ s = QObject::tr("%1 KiB").arg(QLocale().toString(size, 'f', 1));
+ }
+ // Just byte
+ else if ( size > 0 )
+ {
+ s = QObject::tr("%1 B").arg(QLocale().toString(size, 'f', 1));
+ }
+ // Nothing
+ else
+ {
+ s = QObject::tr("0 B");
+ }
+ return s;
+}
+
class Solid::Backends::Hal::HalDevicePrivate
{
public:
@@ -256,6 +302,19 @@
return QString();
}
+QString HalDevice::visibleName() const
+{
+ QString category = property("info.category").toString();
+
+ if (category=="storage") {
+ return storageVisibleName();
+ } else if (category=="volume") {
+ return volumeVisibleName();
+ } else {
+ return product();
+ }
+}
+
QVariant HalDevice::property(const QString &key) const
{
if (d->cache.contains(key))
@@ -473,4 +532,284 @@
emit conditionRaised(condition, reason);
}
+QString HalDevice::storageVisibleName() const
+{
+ QString name;
+ const Storage storageDrive(const_cast<HalDevice*>(this));
+ Solid::StorageDrive::DriveType drive_type = storageDrive.driveType();
+ bool drive_is_hotpluggable = storageDrive.isHotpluggable();
+
+ if (drive_type == Solid::StorageDrive::CdromDrive) {
+ const Cdrom opticalDrive(const_cast<HalDevice*>(this));
+ Solid::OpticalDrive::MediumTypes mediumTypes = \
opticalDrive.supportedMedia(); + QString first;
+ QString second;
+
+ first = QObject::tr("CD-ROM");
+ if (mediumTypes & Solid::OpticalDrive::Cdr)
+ first = QObject::tr("CD-R");
+ if (mediumTypes & Solid::OpticalDrive::Cdrw)
+ first = QObject::tr("CD-RW");
+
+ if (mediumTypes & Solid::OpticalDrive::Dvd)
+ second = QObject::tr("/DVD-ROM");
+ if (mediumTypes & Solid::OpticalDrive::Dvdplusr)
+ second = QObject::tr("/DVD+R");
+ if (mediumTypes & Solid::OpticalDrive::Dvdplusrw)
+ second = QObject::tr("/DVD+RW");
+ if (mediumTypes & Solid::OpticalDrive::Dvdr)
+ second = QObject::tr("/DVD-R");
+ if (mediumTypes & Solid::OpticalDrive::Dvdrw)
+ second = QObject::tr("/DVD-RW");
+ if (mediumTypes & Solid::OpticalDrive::Dvdram)
+ second = QObject::tr("/DVD-RAM");
+ if ((mediumTypes & Solid::OpticalDrive::Dvdr) && (mediumTypes & \
Solid::OpticalDrive::Dvdplusr)) { + if(mediumTypes & \
Solid::OpticalDrive::Dvdplusdl) + second = QObject::tr("/DVD ±R DL");
+ else
+ second = QObject::tr("/DVD ±R");
+ }
+ if ((mediumTypes & Solid::OpticalDrive::Dvdrw) && (mediumTypes & \
Solid::OpticalDrive::Dvdplusrw)) { + if((mediumTypes & \
Solid::OpticalDrive::Dvdplusdl) || (mediumTypes & Solid::OpticalDrive::Dvdplusdlrw)) \
+ second = QObject::tr("/DVD ±RW DL"); + else
+ second = QObject::tr("/DVD ±RW");
+ }
+ if (mediumTypes & Solid::OpticalDrive::Bd)
+ second = QObject::tr("/BD-ROM");
+ if (mediumTypes & Solid::OpticalDrive::Bdr)
+ second = QObject::tr("/BD-R");
+ if (mediumTypes & Solid::OpticalDrive::Bdre)
+ second = QObject::tr("/BD-RE");
+ if (mediumTypes & Solid::OpticalDrive::HdDvd)
+ second = QObject::tr("/HD DVD-ROM");
+ if (mediumTypes & Solid::OpticalDrive::HdDvdr)
+ second = QObject::tr("/HD DVD-R");
+ if (mediumTypes & Solid::OpticalDrive::HdDvdrw)
+ second = QObject::tr("/HD DVD-RW");
+
+ if (drive_is_hotpluggable) {
+ name = QObject::tr("External %1%2 Drive").arg(first).arg(second);
+ } else {
+ name = QObject::tr("%1%2 Drive").arg(first).arg(second);
+ }
+
+ return name;
+ }
+
+ if (drive_type == Solid::StorageDrive::Floppy) {
+ if (drive_is_hotpluggable)
+ name = QObject::tr("External Floppy Drive");
+ else
+ name = QObject::tr("Floppy Drive");
+
+ return name;
+ }
+
+ bool drive_is_removable = storageDrive.isRemovable();
+
+ if (drive_type == Solid::StorageDrive::HardDisk && !drive_is_removable) {
+ QString size_str = formatByteSize(property("storage.size").toInt());
+ if (!size_str.isEmpty()) {
+ if (drive_is_hotpluggable) {
+ name = QObject::tr("%1 External Hard Drive", "%1 is the \
size").arg(size_str); + } else {
+ name = QObject::tr("%1 Hard Drive", "%1 is the size").arg(size_str);
+ }
+ } else {
+ if (drive_is_hotpluggable)
+ name = QObject::tr("External Hard Drive");
+ else
+ name = QObject::tr("Hard Drive");
+ }
+
+ return name;
+ }
+
+ QString vendormodel_str;
+ QString model = property("storage.model").toString();
+ QString vendor = property("storage.vendor").toString();
+
+ if (vendor.isEmpty()) {
+ if (!model.isEmpty())
+ vendormodel_str = model;
+ } else {
+ if (model.isEmpty())
+ vendormodel_str = vendor;
+ else
+ vendormodel_str = QObject::tr("%1 %2", "%1 is the vendor, %2 is the \
model of the device").arg(vendor).arg(model); + }
+
+ if (vendormodel_str.isEmpty())
+ name = QObject::tr("Drive");
+ else
+ name = vendormodel_str;
+
+ return name;
+}
+
+QString HalDevice::volumeVisibleName() const
+{
+ QString name;
+ QString volume_label = property("volume.label").toString();
+
+ if (!volume_label.isEmpty()) {
+ return volume_label;
+ }
+
+ if (!d->parent) {
+ d->parent = new HalDevice(parentUdi());
+ }
+ const Storage storageDrive(const_cast<HalDevice*>(d->parent));
+ Solid::StorageDrive::DriveType drive_type = storageDrive.driveType();
+
+ /* Handle media in optical drives */
+ if (drive_type == Solid::StorageDrive::CdromDrive) {
+ const OpticalDisc disc(const_cast<HalDevice*>(this));
+ switch (disc.discType()) {
+ case Solid::OpticalDisc::UnknownDiscType:
+ case Solid::OpticalDisc::CdRom:
+ name = QObject::tr("CD-ROM");
+ break;
+
+ case Solid::OpticalDisc::CdRecordable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank CD-R");
+ else
+ name = QObject::tr("CD-R");
+ break;
+
+ case Solid::OpticalDisc::CdRewritable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank CD-RW");
+ else
+ name = QObject::tr("CD-RW");
+ break;
+
+ case Solid::OpticalDisc::DvdRom:
+ name = QObject::tr("DVD-ROM");
+ break;
+
+ case Solid::OpticalDisc::DvdRam:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD-RAM");
+ else
+ name = QObject::tr("DVD-RAM");
+ break;
+
+ case Solid::OpticalDisc::DvdRecordable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD-R");
+ else
+ name = QObject::tr("DVD-R");
+ break;
+
+ case Solid::OpticalDisc::DvdPlusRecordableDuallayer:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD-R Dual-Layer");
+ else
+ name = QObject::tr("DVD-R Dual-Layer");
+ break;
+
+ case Solid::OpticalDisc::DvdRewritable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD-RW");
+ else
+ name = QObject::tr("DVD-RW");
+ break;
+
+ case Solid::OpticalDisc::DvdPlusRecordable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD+R");
+ else
+ name = QObject::tr("DVD+R");
+ break;
+
+ case Solid::OpticalDisc::DvdPlusRewritable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD+RW");
+ else
+ name = QObject::tr("DVD+RW");
+ break;
+
+ case Solid::OpticalDisc::DvdPlusRewritableDuallayer:
+ if (disc.isBlank())
+ name = QObject::tr("Blank DVD+R Dual-Layer");
+ else
+ name = QObject::tr("DVD+R Dual-Layer");
+ break;
+
+ case Solid::OpticalDisc::BluRayRom:
+ name = QObject::tr("BD-ROM");
+ break;
+
+ case Solid::OpticalDisc::BluRayRecordable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank BD-R");
+ else
+ name = QObject::tr("BD-R");
+ break;
+
+ case Solid::OpticalDisc::BluRayRewritable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank BD-RE");
+ else
+ name = QObject::tr("BD-RE");
+ break;
+
+ case Solid::OpticalDisc::HdDvdRom:
+ name = QObject::tr("HD DVD-ROM");
+ break;
+
+ case Solid::OpticalDisc::HdDvdRecordable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank HD DVD-R");
+ else
+ name = QObject::tr("HD DVD-R");
+ break;
+
+ case Solid::OpticalDisc::HdDvdRewritable:
+ if (disc.isBlank())
+ name = QObject::tr("Blank HD DVD-RW");
+ else
+ name = QObject::tr("HD DVD-RW");
+ break;
+ }
+
+ /* Special case for pure audio disc */
+ if (disc.availableContent() == Solid::OpticalDisc::Audio) {
+ name = QObject::tr("Audio CD");
+ }
+
+ return name;
+ }
+
+ bool drive_is_removable = storageDrive.isRemovable();
+ bool drive_is_hotpluggable = storageDrive.isHotpluggable();
+
+ QString size_str = formatByteSize(property("volume.size").toULongLong());
+ if (drive_type == Solid::StorageDrive::HardDisk && !drive_is_removable) {
+ if (!size_str.isEmpty()) {
+ if (drive_is_hotpluggable) {
+ name = QObject::tr("%1 External Hard Drive", "%1 is the \
size").arg(size_str); + } else {
+ name = QObject::tr("%1 Hard Drive", "%1 is the size").arg(size_str);
+ }
+ } else {
+ if (drive_is_hotpluggable)
+ name = QObject::tr("External Hard Drive");
+ else
+ name = QObject::tr("Hard Drive");
+ }
+ } else {
+ if (drive_is_removable) {
+ name = QObject::tr("%1 Removable Media").arg(size_str);
+ } else {
+ name = QObject::tr("%1 Media").arg(size_str);
+ }
+ }
+
+ return name;
+}
+
#include "backends/hal/haldevice.moc"
Index: backends/hal/haldevice.h
===================================================================
--- backends/hal/haldevice.h (revision 951665)
+++ backends/hal/haldevice.h (working copy)
@@ -52,6 +52,7 @@
virtual QString vendor() const;
virtual QString product() const;
virtual QString icon() const;
+ virtual QString visibleName() const;
virtual QVariant property(const QString &key) const;
@@ -71,6 +72,9 @@
void slotCondition(const QString &condition, const QString &reason);
private:
+ QString storageVisibleName() const;
+ QString volumeVisibleName() const;
+
HalDevicePrivate *d;
};
}
Index: backends/fakehw/fakedevice.h
===================================================================
--- backends/fakehw/fakedevice.h (revision 951665)
+++ backends/fakehw/fakedevice.h (working copy)
@@ -42,6 +42,7 @@
virtual QString vendor() const;
virtual QString product() const;
virtual QString icon() const;
+ virtual QString visibleName() const;
virtual QVariant property(const QString &key) const;
virtual QMap<QString, QVariant> allProperties() const;
Index: backends/fakehw/fakedevice.cpp
===================================================================
--- backends/fakehw/fakedevice.cpp (revision 951665)
+++ backends/fakehw/fakedevice.cpp (working copy)
@@ -123,6 +123,11 @@
}
}
+QString FakeDevice::visibleName() const
+{
+ return product();
+}
+
QVariant FakeDevice::property(const QString &key) const
{
return d->propertyMap[key];
_______________________________________________
Kde-hardware-devel mailing list
Kde-hardware-devel@kde.org
https://mail.kde.org/mailman/listinfo/kde-hardware-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic