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.mid(parentUdi().length() + 1);
23
24 const QString &fstype = FstabHandling::fstype(m_device);
25 qCDebug(FSTAB_LOG) << "Adding " << m_device << "type:" << fstype;
26
27 if (m_device.startsWith(QLatin1String("//"))) {
28 m_vendor = m_device.mid(2, m_device.indexOf(QLatin1String("/"), 2) - 2);
29 m_product = m_device.mid(m_device.indexOf(QLatin1String("/"), 2) + 1);
30 m_storageType = StorageType::NetworkShare;
31 } else if (fstype.startsWith(QLatin1String("nfs"))) {
32 m_vendor = m_device.left(m_device.indexOf(QLatin1String(":/")));
33 m_product = m_device.mid(m_device.indexOf(QLatin1String(":/")) + 1);
34 m_storageType = StorageType::NetworkShare;
35 } else if (fstype.startsWith(QLatin1String("fuse.")) || fstype == QLatin1String("overlay")) {
36 m_vendor = fstype;
37 m_product = m_device.mid(m_device.indexOf(fstype) + fstype.length());
39 if (m_product.startsWith(home)) {
40 m_product = QStringLiteral("~") + m_product.mid(home.length());
41 }
42 if ((fstype == QLatin1String("fuse.encfs")) || (fstype == QLatin1String("fuse.cryfs")) || (fstype == QLatin1String("fuse.gocryptfs"))) {
43 m_storageType = StorageType::Encrypted;
44 } else if (fstype == QLatin1String("fuse.sshfs") || fstype == QLatin1String("fuse.rclone")) {
45 m_storageType = StorageType::NetworkShare;
46 }
47 }
48
49 const auto &options = FstabHandling::options(m_device);
50
51 const auto gvfsName = options.value(QLatin1String("x-gvfs-name"));
52 if (!gvfsName.isEmpty()) {
53 m_displayName = QUrl::fromPercentEncoding(gvfsName.toUtf8());
54 }
55 const auto gvfsIcon = options.value(QLatin1String("x-gvfs-icon"));
56 if (!gvfsIcon.isEmpty()) {
57 m_iconName = QUrl::fromPercentEncoding(gvfsIcon.toUtf8());
58 }
59
60 if (m_storageType == StorageType::NetworkShare) {
61 m_description = QCoreApplication::translate("", "%1 on %2", "%1 is sharename, %2 is servername").arg(m_product, m_vendor);
62 } else {
63 m_description = QCoreApplication::translate("", "%1 (%2)", "%1 is mountpoint, %2 is fs type").arg(m_product, m_vendor);
64 }
65
66 if (m_displayName.isEmpty()) {
67 const QStringList currentMountPoints = FstabHandling::currentMountPoints(m_device);
68 if (currentMountPoints.isEmpty()) {
69 const QStringList mountPoints = FstabHandling::mountPoints(m_device);
70 m_displayName = mountPoints.isEmpty() ? m_description : mountPoints.first();
71 } else {
72 m_displayName = currentMountPoints.first();
73 }
74 }
75
76 if (m_iconName.isEmpty()) {
77 if (m_storageType == StorageType::NetworkShare) {
78 m_iconName = QStringLiteral("network-server");
79 } else if (m_storageType == StorageType::Encrypted) {
80 m_iconName = QStringLiteral("folder-decrypted");
81 } else {
82 const QStringList &mountPoints = FstabHandling::mountPoints(m_device);
83 const QString home = QDir::homePath();
84 if (mountPoints.contains(QLatin1String("/"))) {
85 m_iconName = QStringLiteral("drive-harddisk-root");
86 } else if (mountPoints.contains(home)) {
87 m_iconName = QStringLiteral("user-home");
88 } else {
89 m_iconName = QStringLiteral("folder");
90 }
91 }
92 }
93}
94
95FstabDevice::~FstabDevice()
96{
97}
98
99QString FstabDevice::udi() const
100{
101 return m_uid;
102}
103
104QString FstabDevice::parentUdi() const
105{
106 return QString::fromLatin1(FSTAB_UDI_PREFIX);
107}
108
109QString FstabDevice::vendor() const
110{
111 return m_vendor;
112}
113
114QString FstabDevice::product() const
115{
116 return m_product;
117}
118
119QString FstabDevice::icon() const
120{
121 return m_iconName;
122}
123
124QStringList FstabDevice::emblems() const
125{
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 return {QStringLiteral("emblem-mounted")};
132 } else {
133 return {QStringLiteral("emblem-unmounted")};
134 }
135}
136
137QString FstabDevice::displayName() const
138{
139 return m_displayName;
140}
141
142QString FstabDevice::description() const
143{
144 return m_description;
145}
146
147bool FstabDevice::isEncrypted() const
148{
149 return m_storageType == FstabDevice::StorageType::Encrypted;
150}
151
152bool FstabDevice::queryDeviceInterface(const Solid::DeviceInterface::Type &interfaceType) const
153{
154 if (interfaceType == Solid::DeviceInterface::StorageAccess) {
155 return true;
156 }
157 if ((m_storageType == StorageType::NetworkShare) && (interfaceType == Solid::DeviceInterface::NetworkShare)) {
158 return true;
159 }
160 return false;
161}
162
163QObject *FstabDevice::createDeviceInterface(const Solid::DeviceInterface::Type &interfaceType)
164{
165 if (interfaceType == Solid::DeviceInterface::StorageAccess) {
166 if (!m_storageAccess) {
167 m_storageAccess = new FstabStorageAccess(this);
168 }
169 return m_storageAccess;
170 } else if ((m_storageType == StorageType::NetworkShare) && (interfaceType == Solid::DeviceInterface::NetworkShare)) {
171 return new FstabNetworkShare(this);
172 }
173 return nullptr;
174}
175
176QString FstabDevice::device() const
177{
178 return m_device;
179}
180
181void FstabDevice::onMtabChanged(const QString &device)
182{
183 if (m_device == device) {
184 Q_EMIT mtabChanged(device);
185 }
186}
187
188#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)
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)
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
QString fromPercentEncoding(const QByteArray &input)
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.