Solid

fstabdevice.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 "fstabdevice.h"
8#include "fstab_debug.h"
9#include "fstabhandling.h"
10#include "fstabnetworkshare.h"
11#include "fstabservice.h"
12#include <QCoreApplication>
13#include <QDir>
14#include <QUrl>
15
16using namespace Solid::Backends::Fstab;
17
18FstabDevice::FstabDevice(QString uid)
19 : Solid::Ifaces::Device()
20 , m_uid(uid)
21{
22 m_device = m_uid;
23 m_device.remove(parentUdi() + "/");
24
25 const QString &fstype = FstabHandling::fstype(m_device);
26 qCDebug(FSTAB_LOG) << "Adding " << m_device << "type:" << fstype;
27
28 if (m_device.startsWith("//")) {
29 m_vendor = m_device.mid(2, m_device.indexOf("/", 2) - 2);
30 m_product = m_device.mid(m_device.indexOf("/", 2) + 1);
31 m_storageType = StorageType::NetworkShare;
32 } else if (fstype.startsWith("nfs")) {
33 m_vendor = m_device.left(m_device.indexOf(":/"));
34 m_product = m_device.mid(m_device.indexOf(":/") + 1);
35 m_storageType = StorageType::NetworkShare;
36 } else if (fstype.startsWith("fuse.") || fstype == QLatin1String("overlay")) {
37 m_vendor = fstype;
38 m_product = m_device.mid(m_device.indexOf(fstype) + fstype.length());
40 if (m_product.startsWith(home)) {
41 m_product = "~" + m_product.mid(home.length());
42 }
43 if ((fstype == QLatin1String("fuse.encfs")) || (fstype == QLatin1String("fuse.cryfs")) || (fstype == QLatin1String("fuse.gocryptfs"))) {
44 m_storageType = StorageType::Encrypted;
45 }
46 }
47
48 const QStringList &gvfsOptions = FstabHandling::options(m_device);
49 for (const QString &option : gvfsOptions) {
50 if (const auto tag = QLatin1String("x-gvfs-name="); option.startsWith(tag)) {
51 const QStringView encoded = QStringView(option).mid(tag.size());
52 m_displayName = QUrl::fromPercentEncoding(encoded.toLatin1());
53 } else if (const auto tag = QLatin1String("x-gvfs-icon="); option.startsWith(tag)) {
54 const QStringView encoded = QStringView(option).mid(tag.size());
55 m_iconName = QUrl::fromPercentEncoding(encoded.toLatin1());
56 }
57 }
58
59 if (m_storageType == StorageType::NetworkShare) {
60 m_description = QCoreApplication::translate("", "%1 on %2", "%1 is sharename, %2 is servername").arg(m_product, m_vendor);
61 } else {
62 m_description = QCoreApplication::translate("", "%1 (%2)", "%1 is mountpoint, %2 is fs type").arg(m_product, m_vendor);
63 }
64
65 if (m_displayName.isEmpty()) {
66 const QStringList currentMountPoints = FstabHandling::currentMountPoints(m_device);
67 if (currentMountPoints.isEmpty()) {
68 const QStringList mountPoints = FstabHandling::mountPoints(m_device);
69 m_displayName = mountPoints.isEmpty() ? m_description : mountPoints.first();
70 } else {
71 m_displayName = currentMountPoints.first();
72 }
73 }
74
75 if (m_iconName.isEmpty()) {
76 if (m_storageType == StorageType::NetworkShare) {
77 m_iconName = QLatin1String("network-server");
78 } else if (m_storageType == StorageType::Encrypted) {
79 m_iconName = QLatin1String("folder-decrypted");
80 } else {
81 const QStringList &mountPoints = FstabHandling::mountPoints(m_device);
82 const QString home = QDir::homePath();
83 if (mountPoints.contains("/")) {
84 m_iconName = QStringLiteral("drive-harddisk-root");
85 } else if (mountPoints.contains(home)) {
86 m_iconName = QStringLiteral("user-home");
87 } else {
88 m_iconName = QStringLiteral("folder");
89 }
90 }
91 }
92}
93
94FstabDevice::~FstabDevice()
95{
96}
97
98QString FstabDevice::udi() const
99{
100 return m_uid;
101}
102
103QString FstabDevice::parentUdi() const
104{
105 return QString::fromLatin1(FSTAB_UDI_PREFIX);
106}
107
108QString FstabDevice::vendor() const
109{
110 return m_vendor;
111}
112
113QString FstabDevice::product() const
114{
115 return m_product;
116}
117
118QString FstabDevice::icon() const
119{
120 return m_iconName;
121}
122
123QStringList FstabDevice::emblems() const
124{
125 QStringList res;
126 if (!m_storageAccess) {
127 FstabDevice *d = const_cast<FstabDevice *>(this);
128 d->m_storageAccess = new FstabStorageAccess(d);
129 }
130 if (m_storageAccess->isAccessible()) {
131 res << "emblem-mounted";
132 } else {
133 res << "emblem-unmounted";
134 }
135
136 return res;
137}
138
139QString FstabDevice::displayName() const
140{
141 return m_displayName;
142}
143
144QString FstabDevice::description() const
145{
146 return m_description;
147}
148
149bool FstabDevice::isEncrypted() const
150{
151 return m_storageType == FstabDevice::StorageType::Encrypted;
152}
153
154bool FstabDevice::queryDeviceInterface(const Solid::DeviceInterface::Type &interfaceType) const
155{
156 if (interfaceType == Solid::DeviceInterface::StorageAccess) {
157 return true;
158 }
159 if ((m_storageType == StorageType::NetworkShare) && (interfaceType == Solid::DeviceInterface::NetworkShare)) {
160 return true;
161 }
162 return false;
163}
164
165QObject *FstabDevice::createDeviceInterface(const Solid::DeviceInterface::Type &interfaceType)
166{
167 if (interfaceType == Solid::DeviceInterface::StorageAccess) {
168 if (!m_storageAccess) {
169 m_storageAccess = new FstabStorageAccess(this);
170 }
171 return m_storageAccess;
172 } else if ((m_storageType == StorageType::NetworkShare) && (interfaceType == Solid::DeviceInterface::NetworkShare)) {
173 return new FstabNetworkShare(this);
174 }
175 return nullptr;
176}
177
178QString FstabDevice::device() const
179{
180 return m_device;
181}
182
183void FstabDevice::onMtabChanged(const QString &device)
184{
185 if (m_device == device) {
186 Q_EMIT mtabChanged(device);
187 }
188}
189
190#include "moc_fstabdevice.cpp"
Type
This enum type defines the type of device interface that a Device can have.
This class allows applications to deal with devices available in the underlying system.
QAction * home(const QObject *recvr, const char *slot, QObject *parent)
The single responsibility of this class is to create arguments valid for logind Inhibit call.
Definition fakebattery.h:16
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QString homePath()
T & first()
bool isEmpty() const const
Q_EMITQ_EMIT
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
QString left(qsizetype n) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QStringView mid(qsizetype start, qsizetype length) const const
QByteArray toLatin1() const const
QString fromPercentEncoding(const QByteArray &input)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:56:28 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.