Baloo

storagedevices.cpp
1 /*
2  This file is part of the KDE Baloo project.
3  SPDX-FileCopyrightText: 2011 Sebastian Trueg <[email protected]>
4  SPDX-FileCopyrightText: 2014 Vishesh Handa <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "storagedevices.h"
10 #include "baloodebug.h"
11 
12 #include <Solid/DeviceNotifier>
13 #include <Solid/DeviceInterface>
14 #include <Solid/Block>
15 #include <Solid/StorageDrive>
16 #include <Solid/StorageVolume>
17 #include <Solid/StorageAccess>
18 #include <Solid/NetworkShare>
19 #include <Solid/OpticalDisc>
20 #include <Solid/Predicate>
21 
22 using namespace Baloo;
23 
24 StorageDevices::StorageDevices(QObject* parent)
25  : QObject(parent)
26 {
27  connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded,
28  this, &StorageDevices::slotSolidDeviceAdded);
29  connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved,
30  this, &StorageDevices::slotSolidDeviceRemoved);
31 
32  initCacheEntries();
33 }
34 
35 StorageDevices::~StorageDevices()
36 {
37 }
38 
39 void StorageDevices::initCacheEntries()
40 {
41  const QList<Solid::Device> devices
42  = Solid::Device::listFromQuery(QStringLiteral("StorageVolume.usage=='FileSystem'"))
43  + Solid::Device::listFromType(Solid::DeviceInterface::NetworkShare);
44  for (const Solid::Device& dev : devices) {
45  createCacheEntry(dev);
46  }
47 }
48 
49 QList<StorageDevices::Entry> StorageDevices::allMedia() const
50 {
51  return m_metadataCache.values();
52 }
53 
54 StorageDevices::Entry* StorageDevices::createCacheEntry(const Solid::Device& dev)
55 {
56  if (dev.udi().isEmpty()) {
57  return nullptr;
58  }
59 
60  const Solid::StorageAccess* storage = dev.as<Solid::StorageAccess>();
61  if (!storage) {
62  return nullptr;
63  }
64 
65  Entry entry(dev);
66  auto it = m_metadataCache.insert(dev.udi(), entry);
67 
69  this, &StorageDevices::slotAccessibilityChanged);
70  //connect(storage, SIGNAL(teardownRequested(QString)),
71  // this, SLOT(slotTeardownRequested(QString)));
72  return &it.value();
73 }
74 
75 bool StorageDevices::isEmpty() const
76 {
77  return m_metadataCache.isEmpty();
78 }
79 
80 void StorageDevices::slotSolidDeviceAdded(const QString& udi)
81 {
82  qCDebug(BALOO) << udi;
83  Entry* e = createCacheEntry(Solid::Device(udi));
84  if (e) {
85  Q_EMIT deviceAdded(e);
86  }
87 }
88 
89 void StorageDevices::slotSolidDeviceRemoved(const QString& udi)
90 {
91  QHash< QString, Entry >::iterator it = m_metadataCache.find(udi);
92  if (it != m_metadataCache.end()) {
93  qCDebug(BALOO) << "Found removable storage volume for Baloo undocking:" << udi;
94  Q_EMIT deviceRemoved(&it.value());
95  m_metadataCache.erase(it);
96  }
97 }
98 
99 void StorageDevices::slotAccessibilityChanged(bool accessible, const QString& udi)
100 {
101  qCDebug(BALOO) << accessible << udi;
102  Q_UNUSED(accessible);
103 
104  //
105  // cache new mount path
106  //
107  Entry* entry = &m_metadataCache[udi];
108  Q_ASSERT(entry != nullptr);
109  Q_EMIT deviceAccessibilityChanged(entry);
110 }
111 
112 StorageDevices::Entry::Entry()
113 {
114 }
115 
116 StorageDevices::Entry::Entry(const Solid::Device& device)
117  : m_device(device)
118 {
119 }
120 
121 QString StorageDevices::Entry::mountPath() const
122 {
123  if (const Solid::StorageAccess* sa = m_device.as<Solid::StorageAccess>()) {
124  return sa->filePath();
125  } else {
126  return QString();
127  }
128 }
129 
130 bool StorageDevices::Entry::isMounted() const
131 {
132  if (const Solid::StorageAccess* sa = m_device.as<Solid::StorageAccess>()) {
133  return sa->isAccessible();
134  } else {
135  return false;
136  }
137 }
138 
139 bool StorageDevices::Entry::isUsable() const
140 {
141  if (mountPath().isEmpty()) {
142  return false;
143  }
144 
145  bool usable = true;
146 
147  const Solid::Device& dev = m_device;
148  if (dev.is<Solid::StorageVolume>() && dev.parent().is<Solid::StorageDrive>()) {
149  auto parent = dev.parent().as<Solid::StorageDrive>();
150  if (parent->isRemovable() || parent->isHotpluggable()) {
151  usable = false;
152  }
153 
155  if (volume->isIgnored() || volume->usage() != Solid::StorageVolume::FileSystem) {
156  usable = false;
157  }
158  }
159 
160  if (dev.is<Solid::NetworkShare>()) {
161  usable = false;
162  } else if (dev.is<Solid::OpticalDisc>()) {
163  usable = false;
164  }
165 
166  if (usable) {
167  if (const Solid::StorageAccess* sa = dev.as<Solid::StorageAccess>()) {
168  usable = sa->isAccessible();
169  }
170  }
171 
172  return usable;
173 }
174 
175 #include "moc_storagedevices.cpp"
void deviceRemoved(const QString &udi)
const T value(const Key &key) const const
bool is() const
void deviceAdded(const QString &udi)
Q_EMITQ_EMIT
QHash::iterator erase(QHash::iterator pos)
QHash::iterator find(const Key &key)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QHash::iterator insert(const Key &key, const T &value)
QString udi() const
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
bool isEmpty() const const
DevIface * as()
static QList< Device > listFromType(const DeviceInterface::Type &type, const QString &parentUdi=QString())
void accessibilityChanged(bool accessible, const QString &udi)
static QList< Device > listFromQuery(const Predicate &predicate, const QString &parentUdi=QString())
QList< T > values() const const
Device parent() const
bool isEmpty() const const
QHash::iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.