From kde-hardware-devel Fri Apr 10 15:00:28 2009 From: Albert Astals Cid Date: Fri, 10 Apr 2009 15:00:28 +0000 To: kde-hardware-devel Subject: [Kde-hardware-devel] Making Solid give a visible name for products Message-Id: <200904101700.28853.aacid () kde ! org> X-MARC-Message: https://marc.info/?l=kde-hardware-devel&m=123937570010995 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--Boundary-00=_M813JgG4XMtByz9" --Boundary-00=_M813JgG4XMtByz9 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline 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(this)); but didn't find a better way to do it. Comments? Albert --Boundary-00=_M813JgG4XMtByz9 Content-Type: text/x-patch; charset="UTF-8"; name="solid_visible_name.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="solid_visible_name.patch" Index: device.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- device.h (revision 951665) +++ device.h (working copy) @@ -185,6 +185,12 @@ */ QString icon() const; =20 + /** + * Retrieves the visible name of device. + * + * @return the visible name + */ + QString visibleName() const; =20 /** * Tests if a device interface is available from the device. Index: device.cpp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- device.cpp (revision 951665) +++ device.cpp (working copy) @@ -131,6 +131,11 @@ return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), ico= n()); } =20 +QString Solid::Device::visibleName() const +{ + return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), vis= ibleName()); +} + bool Solid::Device::isDeviceInterface(const DeviceInterface::Type &type) c= onst { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), false, queryDe= viceInterface(type)); Index: ifaces/device.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- ifaces/device.h (revision 951665) +++ ifaces/device.h (working copy) @@ -95,6 +95,12 @@ */ virtual QString icon() const =3D 0; =20 + /** + * Retrieves the visible name of device. + * + * @return the visible name + */ + virtual QString visibleName() const =3D 0; =20 /** * Tests if a property exist. Index: backends/hal/haldevice.cpp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- backends/hal/haldevice.cpp (revision 951665) +++ backends/hal/haldevice.cpp (working copy) @@ -49,6 +49,52 @@ =20 using namespace Solid::Backends::Hal; =20 +// 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 >=3D 1073741824.0 ) + { + size /=3D 1073741824.0; + if ( size > 1024 ) // Tebi-byte + s =3D QObject::tr("%1 TiB").arg(QLocale().toString(size / 1024= =2E0, 'f', 1)); + else + s =3D QObject::tr("%1 GiB").arg(QLocale().toString(size, 'f', = 1)); + } + // Mebi-byte + else if ( size >=3D 1048576.0 ) + { + size /=3D 1048576.0; + s =3D QObject::tr("%1 MiB").arg(QLocale().toString(size, 'f', 1)); + } + // Kibi-byte + else if ( size >=3D 1024.0 ) + { + size /=3D 1024.0; + s =3D QObject::tr("%1 KiB").arg(QLocale().toString(size, 'f', 1)); + } + // Just byte + else if ( size > 0 ) + { + s =3D QObject::tr("%1 B").arg(QLocale().toString(size, 'f', 1)); + } + // Nothing + else + { + s =3D QObject::tr("0 B"); + } + return s; +} + class Solid::Backends::Hal::HalDevicePrivate { public: @@ -256,6 +302,19 @@ return QString(); } =20 +QString HalDevice::visibleName() const +{ + QString category =3D property("info.category").toString(); + + if (category=3D=3D"storage") { + return storageVisibleName(); + } else if (category=3D=3D"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); } =20 +QString HalDevice::storageVisibleName() const +{ + QString name; + const Storage storageDrive(const_cast(this)); + Solid::StorageDrive::DriveType drive_type =3D storageDrive.driveType(); + bool drive_is_hotpluggable =3D storageDrive.isHotpluggable(); + + if (drive_type =3D=3D Solid::StorageDrive::CdromDrive) { + const Cdrom opticalDrive(const_cast(this)); + Solid::OpticalDrive::MediumTypes mediumTypes =3D opticalDrive.supp= ortedMedia(); + QString first; + QString second; + + first =3D QObject::tr("CD-ROM"); + if (mediumTypes & Solid::OpticalDrive::Cdr) + first =3D QObject::tr("CD-R"); + if (mediumTypes & Solid::OpticalDrive::Cdrw) + first =3D QObject::tr("CD-RW"); + + if (mediumTypes & Solid::OpticalDrive::Dvd) + second =3D QObject::tr("/DVD-ROM"); + if (mediumTypes & Solid::OpticalDrive::Dvdplusr) + second =3D QObject::tr("/DVD+R"); + if (mediumTypes & Solid::OpticalDrive::Dvdplusrw) + second =3D QObject::tr("/DVD+RW"); + if (mediumTypes & Solid::OpticalDrive::Dvdr) + second =3D QObject::tr("/DVD-R"); + if (mediumTypes & Solid::OpticalDrive::Dvdrw) + second =3D QObject::tr("/DVD-RW"); + if (mediumTypes & Solid::OpticalDrive::Dvdram) + second =3D QObject::tr("/DVD-RAM"); + if ((mediumTypes & Solid::OpticalDrive::Dvdr) && (mediumTypes & So= lid::OpticalDrive::Dvdplusr)) { + if(mediumTypes & Solid::OpticalDrive::Dvdplusdl) + second =3D QObject::tr("/DVD=C2=B1R DL"); + else + second =3D QObject::tr("/DVD=C2=B1R"); + } + if ((mediumTypes & Solid::OpticalDrive::Dvdrw) && (mediumTypes & S= olid::OpticalDrive::Dvdplusrw)) { + if((mediumTypes & Solid::OpticalDrive::Dvdplusdl) || (mediumTy= pes & Solid::OpticalDrive::Dvdplusdlrw)) + second =3D QObject::tr("/DVD=C2=B1RW DL"); + else + second =3D QObject::tr("/DVD=C2=B1RW"); + } + if (mediumTypes & Solid::OpticalDrive::Bd) + second =3D QObject::tr("/BD-ROM"); + if (mediumTypes & Solid::OpticalDrive::Bdr) + second =3D QObject::tr("/BD-R"); + if (mediumTypes & Solid::OpticalDrive::Bdre) + second =3D QObject::tr("/BD-RE"); + if (mediumTypes & Solid::OpticalDrive::HdDvd) + second =3D QObject::tr("/HD DVD-ROM"); + if (mediumTypes & Solid::OpticalDrive::HdDvdr) + second =3D QObject::tr("/HD DVD-R"); + if (mediumTypes & Solid::OpticalDrive::HdDvdrw) + second =3D QObject::tr("/HD DVD-RW"); + + if (drive_is_hotpluggable) { + name =3D QObject::tr("External %1%2 Drive").arg(first).arg(sec= ond); + } else { + name =3D QObject::tr("%1%2 Drive").arg(first).arg(second); + } + + return name; + } + + if (drive_type =3D=3D Solid::StorageDrive::Floppy) { + if (drive_is_hotpluggable) + name =3D QObject::tr("External Floppy Drive"); + else + name =3D QObject::tr("Floppy Drive"); + + return name; + } + + bool drive_is_removable =3D storageDrive.isRemovable(); + + if (drive_type =3D=3D Solid::StorageDrive::HardDisk && !drive_is_remov= able) { + QString size_str =3D formatByteSize(property("storage.size").toInt= ()); + if (!size_str.isEmpty()) { + if (drive_is_hotpluggable) { + name =3D QObject::tr("%1 External Hard Drive", "%1 is the = size").arg(size_str); + } else { + name =3D QObject::tr("%1 Hard Drive", "%1 is the size").ar= g(size_str); + } + } else { + if (drive_is_hotpluggable) + name =3D QObject::tr("External Hard Drive"); + else + name =3D QObject::tr("Hard Drive"); + } + + return name; + } + + QString vendormodel_str; + QString model =3D property("storage.model").toString(); + QString vendor =3D property("storage.vendor").toString(); + + if (vendor.isEmpty()) { + if (!model.isEmpty()) + vendormodel_str =3D model; + } else { + if (model.isEmpty()) + vendormodel_str =3D vendor; + else + vendormodel_str =3D QObject::tr("%1 %2", "%1 is the vendor, %2= is the model of the device").arg(vendor).arg(model); + } + + if (vendormodel_str.isEmpty()) + name =3D QObject::tr("Drive"); + else + name =3D vendormodel_str; + + return name; +} + +QString HalDevice::volumeVisibleName() const +{ + QString name; + QString volume_label =3D property("volume.label").toString(); + + if (!volume_label.isEmpty()) { + return volume_label; + } + + if (!d->parent) { + d->parent =3D new HalDevice(parentUdi()); + } + const Storage storageDrive(const_cast(d->parent)); + Solid::StorageDrive::DriveType drive_type =3D storageDrive.driveType(); + + /* Handle media in optical drives */ + if (drive_type =3D=3D Solid::StorageDrive::CdromDrive) { + const OpticalDisc disc(const_cast(this)); + switch (disc.discType()) { + case Solid::OpticalDisc::UnknownDiscType: + case Solid::OpticalDisc::CdRom: + name =3D QObject::tr("CD-ROM"); + break; + + case Solid::OpticalDisc::CdRecordable: + if (disc.isBlank()) + name =3D QObject::tr("Blank CD-R"); + else + name =3D QObject::tr("CD-R"); + break; + + case Solid::OpticalDisc::CdRewritable: + if (disc.isBlank()) + name =3D QObject::tr("Blank CD-RW"); + else + name =3D QObject::tr("CD-RW"); + break; + + case Solid::OpticalDisc::DvdRom: + name =3D QObject::tr("DVD-ROM"); + break; + + case Solid::OpticalDisc::DvdRam: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD-RAM"); + else + name =3D QObject::tr("DVD-RAM"); + break; + + case Solid::OpticalDisc::DvdRecordable: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD-R"); + else + name =3D QObject::tr("DVD-R"); + break; + + case Solid::OpticalDisc::DvdPlusRecordableDuallayer: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD-R Dual-Layer"); + else + name =3D QObject::tr("DVD-R Dual-Layer"); + break; + + case Solid::OpticalDisc::DvdRewritable: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD-RW"); + else + name =3D QObject::tr("DVD-RW"); + break; + + case Solid::OpticalDisc::DvdPlusRecordable: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD+R"); + else + name =3D QObject::tr("DVD+R"); + break; + + case Solid::OpticalDisc::DvdPlusRewritable: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD+RW"); + else + name =3D QObject::tr("DVD+RW"); + break; + + case Solid::OpticalDisc::DvdPlusRewritableDuallayer: + if (disc.isBlank()) + name =3D QObject::tr("Blank DVD+R Dual-Layer"); + else + name =3D QObject::tr("DVD+R Dual-Layer"); + break; + + case Solid::OpticalDisc::BluRayRom: + name =3D QObject::tr("BD-ROM"); + break; + + case Solid::OpticalDisc::BluRayRecordable: + if (disc.isBlank()) + name =3D QObject::tr("Blank BD-R"); + else + name =3D QObject::tr("BD-R"); + break; + + case Solid::OpticalDisc::BluRayRewritable: + if (disc.isBlank()) + name =3D QObject::tr("Blank BD-RE"); + else + name =3D QObject::tr("BD-RE"); + break; + + case Solid::OpticalDisc::HdDvdRom: + name =3D QObject::tr("HD DVD-ROM"); + break; + + case Solid::OpticalDisc::HdDvdRecordable: + if (disc.isBlank()) + name =3D QObject::tr("Blank HD DVD-R"); + else + name =3D QObject::tr("HD DVD-R"); + break; + + case Solid::OpticalDisc::HdDvdRewritable: + if (disc.isBlank()) + name =3D QObject::tr("Blank HD DVD-RW"); + else + name =3D QObject::tr("HD DVD-RW"); + break; + } + + /* Special case for pure audio disc */ + if (disc.availableContent() =3D=3D Solid::OpticalDisc::Audio) { + name =3D QObject::tr("Audio CD"); + } + + return name; + } + + bool drive_is_removable =3D storageDrive.isRemovable(); + bool drive_is_hotpluggable =3D storageDrive.isHotpluggable(); + + QString size_str =3D formatByteSize(property("volume.size").toULongLon= g()); + if (drive_type =3D=3D Solid::StorageDrive::HardDisk && !drive_is_remov= able) { + if (!size_str.isEmpty()) { + if (drive_is_hotpluggable) { + name =3D QObject::tr("%1 External Hard Drive", "%1 is the = size").arg(size_str); + } else { + name =3D QObject::tr("%1 Hard Drive", "%1 is the size").ar= g(size_str); + } + } else { + if (drive_is_hotpluggable) + name =3D QObject::tr("External Hard Drive"); + else + name =3D QObject::tr("Hard Drive"); + } + } else { + if (drive_is_removable) { + name =3D QObject::tr("%1 Removable Media").arg(size_str); + } else { + name =3D QObject::tr("%1 Media").arg(size_str); + } + } + + return name; +} + #include "backends/hal/haldevice.moc" Index: backends/hal/haldevice.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- 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; =20 virtual QVariant property(const QString &key) const; =20 @@ -71,6 +72,9 @@ void slotCondition(const QString &condition, const QString &reason); =20 private: + QString storageVisibleName() const; + QString volumeVisibleName() const; + HalDevicePrivate *d; }; } Index: backends/fakehw/fakedevice.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- 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; =20 virtual QVariant property(const QString &key) const; virtual QMap allProperties() const; Index: backends/fakehw/fakedevice.cpp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- backends/fakehw/fakedevice.cpp (revision 951665) +++ backends/fakehw/fakedevice.cpp (working copy) @@ -123,6 +123,11 @@ } } =20 +QString FakeDevice::visibleName() const +{ + return product(); +} + QVariant FakeDevice::property(const QString &key) const { return d->propertyMap[key]; --Boundary-00=_M813JgG4XMtByz9 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Kde-hardware-devel mailing list Kde-hardware-devel@kde.org https://mail.kde.org/mailman/listinfo/kde-hardware-devel --Boundary-00=_M813JgG4XMtByz9--