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{
42 for (const Solid::Device& dev : devices) {
43 createCacheEntry(dev);
44 }
45}
46
47QList<StorageDevices::Entry> StorageDevices::allMedia() const
48{
49 return m_metadataCache.values();
50}
51
52StorageDevices::Entry* StorageDevices::createCacheEntry(const Solid::Device& dev)
53{
54 if (dev.udi().isEmpty()) {
55 return nullptr;
56 }
57
58 const Solid::StorageAccess* storage = dev.as<Solid::StorageAccess>();
59 if (!storage) {
60 return nullptr;
61 }
62
63 Entry entry(dev);
64 auto it = m_metadataCache.insert(dev.udi(), entry);
65
67 this, &StorageDevices::slotAccessibilityChanged);
68 //connect(storage, SIGNAL(teardownRequested(QString)),
69 // this, SLOT(slotTeardownRequested(QString)));
70 return &it.value();
71}
72
73bool StorageDevices::isEmpty() const
74{
75 return m_metadataCache.isEmpty();
76}
77
78void StorageDevices::slotSolidDeviceAdded(const QString& udi)
79{
80 qCDebug(BALOO) << udi;
81 Entry* e = createCacheEntry(Solid::Device(udi));
82 if (e) {
83 Q_EMIT deviceAdded(e);
84 }
85}
86
87void StorageDevices::slotSolidDeviceRemoved(const QString& udi)
88{
89 QHash< QString, Entry >::iterator it = m_metadataCache.find(udi);
90 if (it != m_metadataCache.end()) {
91 qCDebug(BALOO) << "Found removable storage volume for Baloo undocking:" << udi;
92 Q_EMIT deviceRemoved(&it.value());
93 m_metadataCache.erase(it);
94 }
95}
96
97void StorageDevices::slotAccessibilityChanged(bool accessible, const QString& udi)
98{
99 qCDebug(BALOO) << accessible << udi;
100 Q_UNUSED(accessible);
101
102 //
103 // cache new mount path
104 //
105 Entry* entry = &m_metadataCache[udi];
106 Q_ASSERT(entry != nullptr);
107 Q_EMIT deviceAccessibilityChanged(entry);
108}
109
110StorageDevices::Entry::Entry()
111{
112}
113
114StorageDevices::Entry::Entry(const Solid::Device& device)
115 : m_device(device)
116{
117}
118
119QString StorageDevices::Entry::mountPath() const
120{
121 if (const Solid::StorageAccess* sa = m_device.as<Solid::StorageAccess>()) {
122 return sa->filePath();
123 } else {
124 return QString();
125 }
126}
127
128bool StorageDevices::Entry::isMounted() const
129{
130 if (const Solid::StorageAccess* sa = m_device.as<Solid::StorageAccess>()) {
131 return sa->isAccessible();
132 } else {
133 return false;
134 }
135}
136
137bool StorageDevices::Entry::isUsable() const
138{
139 if (mountPath().isEmpty()) {
140 return false;
141 }
142
143 bool usable = true;
144
145 const Solid::Device& dev = m_device;
146 if (dev.is<Solid::StorageVolume>() && dev.parent().is<Solid::StorageDrive>()) {
147 auto parent = dev.parent().as<Solid::StorageDrive>();
148 if (parent->isRemovable() || parent->isHotpluggable()) {
149 usable = false;
150 }
151
153 if (volume->isIgnored() || volume->usage() != Solid::StorageVolume::FileSystem) {
154 usable = false;
155 }
156 }
157
158 if (dev.is<Solid::NetworkShare>()) {
159 usable = false;
160 } else if (dev.is<Solid::OpticalDisc>()) {
161 usable = false;
162 } else if (dev.is<Solid::StorageAccess>() && dev.parentUdi() == QStringLiteral("/org/kde/fstab")) {
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)
QString udi() const
QString parentUdi() const
static QList< Device > allDevices()
Device parent() const
bool is() const
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
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isEmpty() 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 Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.