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 #ifndef SOLDEVICE
0009 #define SOLDEVICE
0010 
0011 // QT
0012 #include <QDebug>
0013 #include <QTreeWidgetItem>
0014 
0015 // Solid
0016 #include <solid/device.h>
0017 // KDE
0018 
0019 class QVListLayout;
0020 
0021 class SolDevice : public QTreeWidgetItem
0022 {
0023 public:
0024     SolDevice(const Solid::DeviceInterface::Type &);
0025     SolDevice(const Solid::DeviceInterface::Type &, const QString &);
0026     SolDevice(QTreeWidgetItem *);
0027     SolDevice(QTreeWidgetItem *, const Solid::Device &);
0028 
0029     QIcon deviceIcon() const;
0030     Solid::Device *device();
0031     Solid::DeviceInterface::Type deviceType() const;
0032 
0033     template<class IFace>
0034     const IFace *interface()
0035     {
0036         if (deviceSet) {
0037             IFace *dev = tiedDevice.as<const IFace>();
0038             if (!dev) {
0039                 qDebug() << "Device unable to be cast to correct device";
0040             }
0041             return dev;
0042         } else {
0043             return nullptr;
0044         }
0045     }
0046 
0047     template<class IFace>
0048     const IFace *interface(const Solid::Device &device)
0049     {
0050         IFace *dev = device.as<const IFace>();
0051         if (!dev) {
0052             qDebug() << "Device unable to be cast to correct device";
0053         }
0054         return dev;
0055     }
0056 
0057     template<class IFace>
0058     void createDeviceChildren(QTreeWidgetItem *treeParent, const QString &parentUid, const Solid::DeviceInterface::Type &type)
0059     {
0060         const QList<Solid::Device> list = Solid::Device::listFromType(type, parentUid);
0061 
0062         foreach (const Solid::Device &dev, list) {
0063             new IFace(treeParent, dev);
0064         }
0065     }
0066 
0067     void setDeviceIcon(const QIcon &);
0068     void setDeviceToolTip(const QString &);
0069 
0070     virtual QVListLayout *infoPanelLayout();
0071     virtual void addItem(const Solid::Device &dev)
0072     {
0073         new SolDevice(this, dev);
0074     }
0075 
0076     virtual void refreshName()
0077     {
0078         setDefaultDeviceText();
0079     }
0080 
0081     QString udi() const;
0082     bool isDeviceSet();
0083 
0084     bool operator<(const QTreeWidgetItem &other) const override;
0085 
0086 protected:
0087     void setDeviceText(const QString &);
0088 
0089     virtual void setDefaultDeviceToolTip();
0090     virtual void setDefaultDeviceText();
0091     virtual void setDefaultDeviceIcon();
0092     virtual void setDefaultListing(const Solid::DeviceInterface::Type &);
0093 
0094     bool deviceSet;
0095     QVListLayout *deviceInfoLayout;
0096     Solid::DeviceInterface::Type deviceTypeHolder;
0097     Solid::Device tiedDevice;
0098 };
0099 
0100 #endif // SOLDEVICE