File indexing completed on 2024-05-19 16:31:57
0001 /* 0002 * SPDX-FileCopyrightText: 2009 David Hubner <hubnerd@ntlworld.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 * 0006 */ 0007 0008 #include "soldevice.h" 0009 0010 #include <solid/deviceinterface.h> 0011 #include <solid/processor.h> 0012 0013 #include <QTreeWidget> 0014 0015 #include <KLocalizedString> 0016 0017 // Local 0018 #include "qvlistlayout.h" 0019 0020 SolDevice::SolDevice(const Solid::DeviceInterface::Type &type) 0021 : QTreeWidgetItem() 0022 , deviceSet(false) 0023 , deviceTypeHolder(type) 0024 { 0025 setText(0, Solid::DeviceInterface::typeToString(type)); 0026 } 0027 0028 SolDevice::SolDevice(QTreeWidgetItem *parent) 0029 : QTreeWidgetItem(parent) 0030 , deviceSet(false) 0031 , deviceTypeHolder(Solid::DeviceInterface::Unknown) 0032 { 0033 } 0034 0035 SolDevice::SolDevice(const Solid::DeviceInterface::Type &type, const QString &typeName) 0036 : QTreeWidgetItem() 0037 , deviceSet(false) 0038 , deviceTypeHolder(type) 0039 { 0040 setText(0, typeName); 0041 0042 setDefaultListing(type); 0043 } 0044 0045 SolDevice::SolDevice(QTreeWidgetItem *parent, const Solid::Device &device) 0046 : QTreeWidgetItem(parent) 0047 , deviceTypeHolder(Solid::DeviceInterface::Unknown) 0048 , tiedDevice(device) 0049 { 0050 deviceSet = device.isValid(); 0051 setDefaultDeviceText(); 0052 setDefaultDeviceIcon(); 0053 setDefaultDeviceToolTip(); 0054 } 0055 0056 // Sets 0057 0058 void SolDevice::setDefaultListing(const Solid::DeviceInterface::Type &type) 0059 { 0060 createDeviceChildren<SolDevice>(this, QString(), type); 0061 } 0062 0063 void SolDevice::setDefaultDeviceText() 0064 { 0065 QString ddtString = i18nc("unknown device", "Unknown"); 0066 0067 if (deviceSet) { 0068 ddtString = tiedDevice.product(); 0069 if (tiedDevice.isDeviceInterface(Solid::DeviceInterface::StorageVolume) || tiedDevice.isDeviceInterface(Solid::DeviceInterface::Battery)) { 0070 QString label = SolDevice::udi().section(QStringLiteral("/"), -1, -1); 0071 if (!label.isEmpty()) { 0072 ddtString = label; 0073 } 0074 } 0075 } 0076 setText(0, ddtString); 0077 } 0078 0079 void SolDevice::setDefaultDeviceIcon() 0080 { 0081 QIcon ddiString = QIcon::fromTheme(QStringLiteral("kde")); 0082 0083 if (deviceSet) { 0084 ddiString = QIcon(tiedDevice.icon()); 0085 } 0086 setDeviceIcon(ddiString); 0087 } 0088 0089 void SolDevice::setDefaultDeviceToolTip() 0090 { 0091 QString ddttString = i18nc("Default device tooltip", "A Device"); 0092 0093 if (deviceSet) { 0094 ddttString = tiedDevice.description(); 0095 } 0096 setDeviceToolTip(ddttString); 0097 } 0098 0099 void SolDevice::setDeviceIcon(const QIcon &icon) 0100 { 0101 setIcon(0, icon); 0102 } 0103 0104 void SolDevice::setDeviceText(const QString &text) 0105 { 0106 setText(0, text); 0107 } 0108 0109 void SolDevice::setDeviceToolTip(const QString &toolTipText) 0110 { 0111 setToolTip(0, toolTipText); 0112 } 0113 0114 // Gets 0115 0116 QVListLayout *SolDevice::infoPanelLayout() 0117 { 0118 deviceInfoLayout = new QVListLayout(); 0119 return deviceInfoLayout; 0120 } 0121 0122 QIcon SolDevice::deviceIcon() const 0123 { 0124 return icon(0); 0125 } 0126 0127 Solid::DeviceInterface::Type SolDevice::deviceType() const 0128 { 0129 return deviceTypeHolder; 0130 } 0131 0132 Solid::Device *SolDevice::device() 0133 { 0134 return &tiedDevice; 0135 } 0136 0137 QString SolDevice::udi() const 0138 { 0139 return tiedDevice.udi(); 0140 } 0141 0142 // Is 0143 0144 bool SolDevice::isDeviceSet() 0145 { 0146 return deviceSet; 0147 } 0148 0149 bool SolDevice::operator<(const QTreeWidgetItem &other) const 0150 { 0151 const SolDevice *otherDevice = dynamic_cast<const SolDevice *>(&other); 0152 if (otherDevice) { 0153 if (deviceType() != otherDevice->deviceType()) { 0154 return deviceType() < otherDevice->deviceType(); 0155 } 0156 switch (deviceType()) { 0157 case Solid::DeviceInterface::Processor: { 0158 const Solid::Processor *left = tiedDevice.as<const Solid::Processor>(); 0159 const Solid::Processor *right = otherDevice->tiedDevice.as<const Solid::Processor>(); 0160 // Processors are sorted in ascending order, so this is reversed 0161 return left->number() > right->number(); 0162 } 0163 case Solid::DeviceInterface::StorageVolume: { 0164 // Storage volumes are sorted in ascending order (i.e. sda, sda1, sda2...) 0165 return text(0) > other.text(0); 0166 } 0167 default: 0168 break; 0169 } 0170 } 0171 return text(0) < other.text(0); 0172 }