Solid

fstabmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Mario Bensi <mbensi@ipsquad.net>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "fstabmanager.h"
8#include "../shared/rootdevice.h"
9#include "fstab_debug.h"
10#include "fstabdevice.h"
11#include "fstabhandling.h"
12#include "fstabservice.h"
13#include "fstabwatcher.h"
14
15using namespace Solid::Backends::Fstab;
16using namespace Solid::Backends::Shared;
17
18FstabManager::FstabManager(QObject *parent)
19 : Solid::Ifaces::DeviceManager(parent)
20 , m_deviceList(FstabHandling::deviceList())
21{
22 m_supportedInterfaces << Solid::DeviceInterface::StorageAccess;
23 m_supportedInterfaces << Solid::DeviceInterface::NetworkShare;
24
25 connect(FstabWatcher::instance(), &FstabWatcher::fstabChanged, this, &FstabManager::onFstabChanged);
26 connect(FstabWatcher::instance(), &FstabWatcher::mtabChanged, this, &FstabManager::onMtabChanged);
27}
28
29QString FstabManager::udiPrefix() const
30{
31 return QStringLiteral(FSTAB_UDI_PREFIX);
32}
33
34QSet<Solid::DeviceInterface::Type> FstabManager::supportedInterfaces() const
35{
36 return m_supportedInterfaces;
37}
38
39QStringList FstabManager::allDevices()
40{
41 QStringList result;
42
43 result << udiPrefix();
44 for (const QString &device : std::as_const(m_deviceList)) {
45 result << udiPrefix() + QStringLiteral("/") + device;
46 }
47
48 return result;
49}
50
51QStringList FstabManager::devicesFromQuery(const QString &parentUdi, Solid::DeviceInterface::Type type)
52{
53 if ((parentUdi == udiPrefix()) || parentUdi.isEmpty()) {
54 QStringList result;
55 if (type == Solid::DeviceInterface::StorageAccess) {
56 for (const QString &device : std::as_const(m_deviceList)) {
57 result << udiPrefix() + QStringLiteral("/") + device;
58 }
59 return result;
60 } else if (type == Solid::DeviceInterface::NetworkShare) {
61 for (const QString &device : std::as_const(m_deviceList)) {
62 result << udiPrefix() + QStringLiteral("/") + device;
63 }
64 return result;
65 }
66 } else {
67 if (type == Solid::DeviceInterface::StorageAccess || type == Solid::DeviceInterface::NetworkShare) {
68 return QStringList{parentUdi};
69 }
70 }
71
72 return QStringList();
73}
74
75QObject *FstabManager::createDevice(const QString &udi)
76{
77 if (udi == udiPrefix()) {
78 RootDevice *root = new RootDevice(udi);
79
80 root->setProduct(tr("Filesystem Volumes"));
81 root->setDescription(tr("Mountable filesystems declared in your system"));
82 root->setIcon(QStringLiteral("folder"));
83
84 return root;
85
86 } else {
87 // global device manager makes sure udi starts with udi prefix + '/'
88 QString internalName = udi.mid(udiPrefix().length() + 1, -1);
89 if (!m_deviceList.contains(internalName)) {
90 return nullptr;
91 }
92
93 FstabDevice *device = new FstabDevice(udi);
94 connect(this, &FstabManager::mtabChanged, device, &FstabDevice::onMtabChanged);
95 return device;
96 }
97}
98
99void FstabManager::onFstabChanged()
100{
101 FstabHandling::flushFstabCache();
102 _k_updateDeviceList();
103}
104
105void FstabManager::_k_updateDeviceList()
106{
107 const QStringList deviceList = FstabHandling::deviceList();
108 const QSet<QString> newlist(deviceList.begin(), deviceList.end());
109 const QSet<QString> oldlist(m_deviceList.begin(), m_deviceList.end());
110
111 m_deviceList = deviceList;
112
113 qCDebug(FSTAB_LOG) << oldlist << "->" << newlist;
114
115 for (const QString &device : newlist) {
116 if (!oldlist.contains(device)) {
117 Q_EMIT deviceAdded(udiPrefix() + QStringLiteral("/") + device);
118 }
119 }
120
121 for (const QString &device : oldlist) {
122 if (!newlist.contains(device)) {
123 Q_EMIT deviceRemoved(udiPrefix() + QStringLiteral("/") + device);
124 }
125 }
126}
127
128void FstabManager::onMtabChanged()
129{
130 FstabHandling::flushMtabCache();
131
132 _k_updateDeviceList(); // devicelist is union of mtab and fstab
133
134 for (const QString &device : std::as_const(m_deviceList)) {
135 // notify storageaccess objects via device ...
136 Q_EMIT mtabChanged(device);
137 }
138}
139
140FstabManager::~FstabManager()
141{
142}
143
144#include "moc_fstabmanager.cpp"
Type
This enum type defines the type of device interface that a Device can have.
void deviceAdded(const QString &udi)
This signal is emitted when a new device appears in the system.
void deviceRemoved(const QString &udi)
This signal is emitted when a device disappears from the system.
iterator begin()
iterator end()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) 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 Fri Nov 22 2024 12:01:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.