Baloo

storagedevices.cpp
1/*
2 This file is part of the KDE Baloo project.
3 SPDX-FileCopyrightText: 2011 Sebastian Trueg <trueg@kde.org>
4 SPDX-FileCopyrightText: 2014 Vishesh Handa <vhanda@kde.org>
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
22using namespace Baloo;
23
24StorageDevices::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
35StorageDevices::~StorageDevices()
36{
37}
38
39void 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
49QList<StorageDevices::Entry> StorageDevices::allMedia() const
50{
51 return m_metadataCache.values();
52}
53
54StorageDevices::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
75bool StorageDevices::isEmpty() const
76{
77 return m_metadataCache.isEmpty();
78}
79
80void 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
89void 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
99void 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
112StorageDevices::Entry::Entry()
113{
114}
115
116StorageDevices::Entry::Entry(const Solid::Device& device)
117 : m_device(device)
118{
119}
120
121QString 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
130bool 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
139bool 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)
void deviceAdded(const QString &udi)
static QList< Device > listFromType(const DeviceInterface::Type &type, const QString &parentUdi=QString())
static QList< Device > listFromQuery(const Predicate &predicate, const QString &parentUdi=QString())
DevIface * as()
void accessibilityChanged(bool accessible, const QString &udi)
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
iterator end()
iterator erase(const_iterator pos)
iterator find(const Key &key)
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
QList< T > values() const const
bool isEmpty() const const
T value(qsizetype i) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.