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 "devicelisting.h"
0009 
0010 // Solid
0011 #include <solid/devicenotifier.h>
0012 
0013 #include <QContextMenuEvent>
0014 #include <QMenu>
0015 
0016 // Local
0017 #include "devinfo.h"
0018 #include "infopanel.h"
0019 #include "soldevicetypes.h"
0020 #include "solidhelper.h"
0021 //#include "nicsignals.h"
0022 
0023 DeviceListing::DeviceListing(QWidget *parent, InfoPanel *info, DevInfoPlugin *stat)
0024     : QTreeWidget(parent)
0025     , iPanel(info)
0026     , status(stat)
0027 {
0028     //     // Check nic changes
0029     //    nicSig = new NicSignals();
0030     //    connect(nicSig,SIGNAL(nicActivatedOrDisconnected()),this,SLOT(networkingChangedSlot()));
0031     //
0032     // Check if selection changed
0033     connect(this, &DeviceListing::currentItemChanged, this, &DeviceListing::currentItemChangedSlot);
0034 
0035     // Check if item is added
0036     connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceListing::deviceAddedSlot);
0037 
0038     // Check if item is removed
0039     connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DeviceListing::deviceRemovedSlot);
0040 
0041     setWhatsThis(i18nc("Device Listing Whats This", "Shows all the devices that are currently listed."));
0042 
0043     createMenuActions();
0044     setHeaderLabels(QStringList(i18n("Devices")));
0045     populateListing();
0046     setSortingEnabled(true);
0047 }
0048 
0049 DeviceListing::~DeviceListing()
0050 {
0051     // delete nicSig;
0052     clear();
0053 }
0054 
0055 void DeviceListing::createMenuActions()
0056 {
0057     colAct = new QAction(i18n("Collapse All"), this);
0058     connect(colAct, &QAction::triggered, this, &DeviceListing::collapseAllDevicesSlot);
0059 
0060     expAct = new QAction(i18n("Expand All"), this);
0061     connect(expAct, &QAction::triggered, this, &DeviceListing::expandAllDevicesSlot);
0062 
0063     allAct = new QAction(i18n("Show All Devices"), this);
0064     connect(allAct, &QAction::triggered, this, &DeviceListing::showAllDevicesSlot);
0065 
0066     relAct = new QAction(i18n("Show Relevant Devices"), this);
0067     connect(relAct, &QAction::triggered, this, &DeviceListing::showRelevantDevicesSlot);
0068 }
0069 
0070 void DeviceListing::contextMenuEvent(QContextMenuEvent *event)
0071 {
0072     QMenu menu(this);
0073 
0074     menu.addAction(colAct);
0075     menu.addAction(expAct);
0076     menu.addAction(allAct);
0077     menu.addAction(relAct);
0078     menu.exec(event->globalPos());
0079 }
0080 
0081 QTreeWidgetItem *DeviceListing::createListItems(const Solid::DeviceInterface::Type &type)
0082 {
0083     switch (type) {
0084     case Solid::DeviceInterface::Processor:
0085         return new SolProcessorDevice(type);
0086     case Solid::DeviceInterface::StorageDrive:
0087         return new SolStorageDevice(type);
0088     case Solid::DeviceInterface::Camera:
0089         return new SolCameraDevice(type);
0090     case Solid::DeviceInterface::PortableMediaPlayer:
0091         return new SolMediaPlayerDevice(type);
0092     case Solid::DeviceInterface::Battery:
0093         return new SolBatteryDevice(type);
0094     default:
0095         return new SolDevice(type, i18nc("unknown device type", "Unknown"));
0096     }
0097 }
0098 
0099 void DeviceListing::populateListing(const show showStatus)
0100 {
0101     const Solid::DeviceInterface::Type needHardware[] = {
0102         Solid::DeviceInterface::Processor,
0103         Solid::DeviceInterface::StorageDrive,
0104         Solid::DeviceInterface::Battery,
0105         Solid::DeviceInterface::PortableMediaPlayer,
0106         Solid::DeviceInterface::Camera,
0107     };
0108 
0109     clear();
0110 
0111     for (unsigned int i = 0; i < (sizeof(needHardware) / sizeof(Solid::DeviceInterface::Type)); i++) {
0112         QTreeWidgetItem *tmpDevice = createListItems(needHardware[i]);
0113         deviceMap[needHardware[i]] = static_cast<SolDevice *>(tmpDevice);
0114 
0115         if ((tmpDevice->childCount() > 0) || (showStatus == ALL)) {
0116             addTopLevelItem(tmpDevice);
0117         }
0118     }
0119 }
0120 
0121 void DeviceListing::currentItemChangedSlot(QTreeWidgetItem *listItemIn, QTreeWidgetItem *previous)
0122 {
0123     Q_UNUSED(previous);
0124 
0125     SolDevice *listItem = static_cast<SolDevice *>(listItemIn);
0126     if (listItem && listItem->isDeviceSet()) {
0127         iPanel->setTopInfo(listItem->deviceIcon(), listItem->device());
0128 
0129         QVListLayout *bottomLay = listItem->infoPanelLayout();
0130         if (!bottomLay) {
0131             return;
0132         }
0133 
0134         iPanel->setBottomInfo(bottomLay);
0135     } else {
0136         status->updateStatus(i18nc("no device UDI", "None"));
0137     }
0138 }
0139 
0140 void DeviceListing::deviceAddedSlot(const QString &udi)
0141 {
0142     SolidHelper *solhelp = new SolidHelper();
0143 
0144     Solid::Device dev(udi);
0145     if (!dev.isValid()) {
0146         // Probably the device already disappeared again.
0147         return;
0148     }
0149 
0150     Solid::DeviceInterface::Type deviceType = solhelp->deviceType(&dev);
0151     QTreeWidgetItem *parent = getTreeWidgetItemFromUdi(this, dev.parentUdi());
0152 
0153     // Incase of bad index
0154     if (deviceMap[deviceType] == nullptr) {
0155         QTreeWidgetItem *topItem = topLevelItem(0);
0156         if (!topItem) {
0157             delete solhelp;
0158             return;
0159         }
0160         deviceMap[deviceType] = static_cast<SolDevice *>(topItem);
0161     }
0162 
0163     switch (deviceType) {
0164     case Solid::DeviceInterface::Processor:
0165         new SolProcessorDevice(deviceMap[deviceType], dev);
0166         break;
0167     case Solid::DeviceInterface::Camera:
0168         new SolCameraDevice(deviceMap[deviceType], dev);
0169         break;
0170     case Solid::DeviceInterface::PortableMediaPlayer:
0171         new SolMediaPlayerDevice(deviceMap[deviceType], dev);
0172         break;
0173     case Solid::DeviceInterface::Battery:
0174         new SolBatteryDevice(deviceMap[deviceType], dev);
0175         break;
0176     case Solid::DeviceInterface::StorageDrive:
0177         new SolStorageDevice(deviceMap[deviceType], dev, SolStorageDevice::NOCHILDREN);
0178         break;
0179     case Solid::DeviceInterface::StorageVolume:
0180         if (parent == nullptr) {
0181             break;
0182         }
0183         new SolVolumeDevice(parent, dev);
0184         break;
0185     default:
0186         break;
0187     }
0188     delete solhelp;
0189 }
0190 
0191 void DeviceListing::deviceRemovedSlot(const QString &udi)
0192 {
0193     const QTreeWidgetItem *item = getTreeWidgetItemFromUdi(this, udi);
0194     if (item == nullptr) {
0195         return;
0196     }
0197 
0198     delete item;
0199 }
0200 
0201 void DeviceListing::collapseAllDevicesSlot()
0202 {
0203     collapseAll();
0204 }
0205 
0206 void DeviceListing::expandAllDevicesSlot()
0207 {
0208     expandAll();
0209 }
0210 
0211 void DeviceListing::showAllDevicesSlot()
0212 {
0213     populateListing(ALL);
0214 }
0215 
0216 void DeviceListing::showRelevantDevicesSlot()
0217 {
0218     populateListing(RELEVANT);
0219 }