File: smb4khardwareinterface.cpp

package info (click to toggle)
smb4k 4.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,320 kB
  • sloc: cpp: 19,431; xml: 967; makefile: 7; sh: 5
file content (318 lines) | stat: -rw-r--r-- 10,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
    Provides an interface to the computer's hardware

    SPDX-FileCopyrightText: 2015-2025 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

// application specific includes
#include "smb4khardwareinterface.h"

// system includes
#include <unistd.h>

// Qt includes
#if (QT_VERSION >= QT_VERSION_CHECK(6, 8, 0))
#include <QApplicationStatic>
#else
#include <qapplicationstatic.h>
#endif
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusUnixFileDescriptor>
#include <QDebug>
#include <QNetworkInterface>
#include <QString>
#include <QStringList>
#include <QTimer>

// KDE includes
#include <Solid/Device>
#include <Solid/DeviceInterface>
#include <Solid/DeviceNotifier>
#include <Solid/NetworkShare>
#include <solid_version.h>
#if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
#include <KMountPoint>
#endif

class Smb4KHardwareInterfacePrivate
{
public:
#if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
    QStringList mountPoints;
#endif
    QScopedPointer<QDBusInterface> dbusInterface;
    QDBusUnixFileDescriptor fileDescriptor;
    bool systemOnline;
    bool systemSleep;
    bool initialImportDone;
#if defined(Q_OS_LINUX)
    QStringList udis;
#endif
    int timerId;
};

class Smb4KHardwareInterfaceStatic
{
public:
    Smb4KHardwareInterface instance;
};

Q_APPLICATION_STATIC(Smb4KHardwareInterfaceStatic, p);

Smb4KHardwareInterface::Smb4KHardwareInterface(QObject *parent)
    : QObject(parent)
    , d(new Smb4KHardwareInterfacePrivate)
{
    d->systemOnline = false;
    d->systemSleep = false;
    d->initialImportDone = false;
    d->fileDescriptor.setFileDescriptor(-1);
    d->timerId = -1;

    //
    // Set up the DBUS interface
    //
    d->dbusInterface.reset(new QDBusInterface(QStringLiteral("org.freedesktop.login1"),
                                              QStringLiteral("/org/freedesktop/login1"),
                                              QStringLiteral("org.freedesktop.login1.Manager"),
                                              QDBusConnection::systemBus(),
                                              this));

    if (!d->dbusInterface->isValid()) {
        d->dbusInterface.reset(new QDBusInterface(QStringLiteral("org.freedesktop.ConsoleKit"),
                                                  QStringLiteral("/org/freedesktop/ConsoleKit/Manager"),
                                                  QStringLiteral("org.freedesktop.ConsoleKit.Manager"),
                                                  QDBusConnection::systemBus(),
                                                  this));
    }

    QDBusConnection::systemBus()
        .connect(QString(), QString(), QStringLiteral("org.freedesktop.login1.Manager"), QStringLiteral("PrepareForSleep"), this, SLOT(slotSystemSleep(bool)));

    //
    // Check the online state
    //
    checkOnlineState(false);

    //
    // Get the initial list of CIFS/SMB3/SMBFS shares mounted
    // on the system and then start the timer.
    //
    QTimer::singleShot(0, [&]() {
#if defined(Q_OS_LINUX)
        QList<Solid::Device> allDevices = Solid::Device::allDevices();

        for (const Solid::Device &device : std::as_const(allDevices)) {
            const Solid::DeviceInterface *iface = device.asDeviceInterface(Solid::DeviceInterface::NetworkShare);
            const Solid::NetworkShare *networkShare = qobject_cast<const Solid::NetworkShare *>(iface);

            if (networkShare && (networkShare->type() == Solid::NetworkShare::Cifs || networkShare->type() == Solid::NetworkShare::Smb3)) {
                d->udis << device.udi();
                QString mountpoint = device.udi().section(QStringLiteral(":"), -1, -1).trimmed();
                Q_EMIT networkShareAdded(mountpoint);
            }
        }
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
        KMountPoint::List mountPoints = KMountPoint::currentMountPoints(KMountPoint::BasicInfoNeeded | KMountPoint::NeedMountOptions);

        for (const QExplicitlySharedDataPointer<KMountPoint> &mountPoint : mountPoints) {
            if (mountPoint->mountType() == QStringLiteral("smbfs")) {
                d->mountPoints.append(mountPoint->mountPoint());
                Q_EMIT networkShareAdded(mountPoint->mountPoint());
            }
        }
#endif
        d->initialImportDone = true;
        d->timerId = startTimer(1000);
    });

#if defined(Q_OS_LINUX)
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &Smb4KHardwareInterface::slotDeviceAdded);
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &Smb4KHardwareInterface::slotDeviceRemoved);
#endif
}

Smb4KHardwareInterface::~Smb4KHardwareInterface()
{
}

Smb4KHardwareInterface *Smb4KHardwareInterface::self()
{
    return &p->instance;
}

bool Smb4KHardwareInterface::isOnline() const
{
    return d->systemOnline;
}

void Smb4KHardwareInterface::inhibit()
{
    if (d->fileDescriptor.isValid()) {
        return;
    }

    if (d->dbusInterface->isValid()) {
        QVariantList args;
        args << QStringLiteral("shutdown:sleep");
        args << QStringLiteral("Smb4K");
        args << QStringLiteral("Mounting or unmounting in progress");
        args << QStringLiteral("delay");

        QDBusReply<QDBusUnixFileDescriptor> descriptor = d->dbusInterface->callWithArgumentList(QDBus::Block, QStringLiteral("Inhibit"), args);

        if (descriptor.isValid()) {
            d->fileDescriptor = descriptor.value();
        }
    }
}

void Smb4KHardwareInterface::uninhibit()
{
    if (!d->fileDescriptor.isValid()) {
        return;
    }

    if (d->dbusInterface->isValid()) {
        close(d->fileDescriptor.fileDescriptor());
        d->fileDescriptor.setFileDescriptor(-1);
    }
}

void Smb4KHardwareInterface::checkOnlineState(bool emitSignal)
{
    bool online = false;
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();

    for (const QNetworkInterface &interface : std::as_const(interfaces)) {
        if (interface.isValid() && interface.type() != QNetworkInterface::Loopback && interface.flags() & QNetworkInterface::IsRunning) {
            online = true;
            break;
        }
    }

    if (online != d->systemOnline) {
        d->systemOnline = online;

        if (emitSignal) {
            Q_EMIT onlineStateChanged(d->systemOnline);
        }
    }
}

bool Smb4KHardwareInterface::initialImportDone() const
{
    return d->initialImportDone;
}

QStringList Smb4KHardwareInterface::allMountPoints() const
{
    QStringList mountPoints;
#if defined(Q_OS_LINUX)
    for (const QString &udi : std::as_const(d->udis)) {
        mountPoints << udi.section(QStringLiteral(":"), -1, -1).trimmed();
    }
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
    mountPoints = d->mountPoints;
#endif
    return mountPoints;
}

void Smb4KHardwareInterface::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);

    checkOnlineState();

#if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
    // NOTE: Using FreeBSD 11 with KF 5.27, Solid is not able to detect mounted shares.
    // Thus, we check here whether shares have been mounted or unmounted.
    // This is a hack and should be removed as soon as possible.
    KMountPoint::List mountPoints = KMountPoint::currentMountPoints(KMountPoint::BasicInfoNeeded | KMountPoint::NeedMountOptions);
    QStringList mountPointList, alreadyMounted;

    for (const QExplicitlySharedDataPointer<KMountPoint> &mountPoint : mountPoints) {
        if (mountPoint->mountType() == QStringLiteral("smbfs")) {
            mountPointList.append(mountPoint->mountPoint());
        }
    }

    QMutableStringListIterator it(mountPointList);

    while (it.hasNext()) {
        QString mountPoint = it.next();
        int index = -1;

        if ((index = d->mountPoints.indexOf(mountPoint)) != -1) {
            d->mountPoints.removeAt(index);
            alreadyMounted.append(mountPoint);
            it.remove();
        }
    }

    if (!d->mountPoints.isEmpty()) {
        for (const QString &mountPoint : std::as_const(d->mountPoints)) {
            Q_EMIT networkShareRemoved(mountPoint);
        }
    }

    if (!mountPointList.isEmpty()) {
        for (const QString &mountPoint : std::as_const(mountPointList)) {
            Q_EMIT networkShareAdded(mountPoint);
        }
    }

    d->mountPoints.clear();
    d->mountPoints.append(alreadyMounted);
    d->mountPoints.append(mountPointList);
#endif
}

#if defined(Q_OS_LINUX)
void Smb4KHardwareInterface::slotDeviceAdded(const QString &udi)
{
    Solid::Device device(udi);

    const Solid::DeviceInterface *iface = device.asDeviceInterface(Solid::DeviceInterface::NetworkShare);
    const Solid::NetworkShare *networkShare = qobject_cast<const Solid::NetworkShare *>(iface);

    if (networkShare && (networkShare->type() == Solid::NetworkShare::Cifs || networkShare->type() == Solid::NetworkShare::Smb3)) {
        d->udis << udi;
        QString mountpoint = udi.section(QStringLiteral(":"), -1, -1).trimmed();
        Q_EMIT networkShareAdded(mountpoint);
    }
}

void Smb4KHardwareInterface::slotDeviceRemoved(const QString &udi)
{
    if (d->udis.contains(udi)) {
        QString mountpoint = udi.section(QStringLiteral(":"), -1, -1).trimmed();
        Q_EMIT networkShareRemoved(mountpoint);
        d->udis.removeOne(udi);
    }
}
#endif

void Smb4KHardwareInterface::slotSystemSleep(bool sleep)
{
    inhibit();
    d->systemSleep = sleep;

    if (d->systemSleep) {
        killTimer(d->timerId);
        d->timerId = -1;
        // The system will recover after a shutdown completely, so we
        // do not have the trigger any unmounts by emitting a signal
        // here. However, we will awake from a sleep later, so some things
        // should be triggered by the emission of the onlineStateChanged
        // signal.
        d->systemOnline = false;
    } else {
        d->timerId = startTimer(1000);
    }

    uninhibit();
}