File indexing completed on 2024-04-28 15:27:20

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kautomount.h"
0009 
0010 #include "kio_widgets_debug.h"
0011 #include <KIO/JobUiDelegate>
0012 #include <KIO/OpenUrlJob>
0013 #include <jobuidelegatefactory.h>
0014 #include <kdirnotify.h>
0015 #include <kio/simplejob.h>
0016 #include <kmountpoint.h>
0017 
0018 #include <KDirWatch>
0019 #include <KJobUiDelegate>
0020 
0021 #include <QDebug>
0022 
0023 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 88)
0024 
0025 /***********************************************************************
0026  *
0027  * Utility classes
0028  *
0029  ***********************************************************************/
0030 
0031 class KAutoMountPrivate
0032 {
0033 public:
0034     KAutoMountPrivate(KAutoMount *qq, const QString &device, const QString &mountPoint, const QString &desktopFile, bool showFileManagerWindow)
0035         : q(qq)
0036         , m_strDevice(device)
0037         , m_desktopFile(desktopFile)
0038         , m_mountPoint(mountPoint)
0039         , m_bShowFilemanagerWindow(showFileManagerWindow)
0040     {
0041     }
0042 
0043     KAutoMount *q;
0044     QString m_strDevice;
0045     QString m_desktopFile;
0046     QString m_mountPoint;
0047     bool m_bShowFilemanagerWindow;
0048 
0049     void slotResult(KJob *);
0050 };
0051 
0052 KAutoMount::KAutoMount(bool _readonly,
0053                        const QByteArray &_format,
0054                        const QString &_device,
0055                        const QString &_mountpoint,
0056                        const QString &_desktopFile,
0057                        bool _show_filemanager_window)
0058     : d(new KAutoMountPrivate(this, _device, _mountpoint, _desktopFile, _show_filemanager_window))
0059 {
0060     KIO::Job *job = KIO::mount(_readonly, _format, _device, _mountpoint);
0061     connect(job, &KJob::result, this, [this, job]() {
0062         d->slotResult(job);
0063     });
0064 }
0065 
0066 KAutoMount::~KAutoMount() = default;
0067 
0068 void KAutoMountPrivate::slotResult(KJob *job)
0069 {
0070     if (job->error()) {
0071         Q_EMIT q->error();
0072         job->uiDelegate()->showErrorMessage();
0073     } else {
0074         const KMountPoint::List mountPoints(KMountPoint::currentMountPoints());
0075         KMountPoint::Ptr mp = mountPoints.findByDevice(m_strDevice);
0076         // Mounting devices using "LABEL=" or "UUID=" will fail if we look for
0077         // the device using only its real name since /etc/mtab will never contain
0078         // the LABEL or UUID entries. Hence, we check using the mount point below
0079         // when device name lookup fails. #247235
0080         if (!mp) {
0081             mp = mountPoints.findByPath(m_mountPoint);
0082         }
0083 
0084         if (!mp) {
0085             qCWarning(KIO_WIDGETS)
0086                 << m_strDevice << "was correctly mounted, but findByDevice() didn't find it."
0087                 << "This looks like a bug, please report it on https://bugs.kde.org, together with your /etc/fstab and /etc/mtab lines for this device";
0088         } else {
0089             const QUrl url = QUrl::fromLocalFile(mp->mountPoint());
0090             // qDebug() << "KAutoMount: m_strDevice=" << m_strDevice << " -> mountpoint=" << mountpoint;
0091             if (m_bShowFilemanagerWindow) {
0092                 KIO::OpenUrlJob *job = new KIO::OpenUrlJob(url, QStringLiteral("inode/directory"));
0093                 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr /*TODO - window*/));
0094                 job->setRunExecutables(true);
0095                 job->start();
0096             }
0097             // Notify about the new stuff in that dir, in case of opened windows showing it
0098             org::kde::KDirNotify::emitFilesAdded(url);
0099         }
0100 
0101         // Update the desktop file which is used for mount/unmount (icon change)
0102         // qDebug() << " mount finished : updating " << m_desktopFile;
0103         org::kde::KDirNotify::emitFilesChanged(QList<QUrl>() << QUrl::fromLocalFile(m_desktopFile));
0104         // KDirWatch::self()->setFileDirty( m_desktopFile );
0105 
0106         Q_EMIT q->finished();
0107     }
0108     q->deleteLater();
0109 }
0110 
0111 class KAutoUnmountPrivate
0112 {
0113 public:
0114     KAutoUnmountPrivate(KAutoUnmount *qq, const QString &_mountpoint, const QString &_desktopFile)
0115         : q(qq)
0116         , m_desktopFile(_desktopFile)
0117         , m_mountpoint(_mountpoint)
0118     {
0119     }
0120     KAutoUnmount *const q;
0121     QString m_desktopFile;
0122     QString m_mountpoint;
0123 
0124     void slotResult(KJob *job);
0125 };
0126 
0127 KAutoUnmount::KAutoUnmount(const QString &_mountpoint, const QString &_desktopFile)
0128     : d(new KAutoUnmountPrivate(this, _mountpoint, _desktopFile))
0129 {
0130     KIO::Job *job = KIO::unmount(d->m_mountpoint);
0131     connect(job, &KJob::result, this, [this, job]() {
0132         d->slotResult(job);
0133     });
0134 }
0135 
0136 void KAutoUnmountPrivate::slotResult(KJob *job)
0137 {
0138     if (job->error()) {
0139         Q_EMIT q->error();
0140         job->uiDelegate()->showErrorMessage();
0141     } else {
0142         // Update the desktop file which is used for mount/unmount (icon change)
0143         // qDebug() << "unmount finished : updating " << m_desktopFile;
0144         org::kde::KDirNotify::emitFilesChanged(QList<QUrl>() << QUrl::fromLocalFile(m_desktopFile));
0145         // KDirWatch::self()->setFileDirty( m_desktopFile );
0146 
0147         // Notify about the new stuff in that dir, in case of opened windows showing it
0148         // You may think we removed files, but this may have also re-added some
0149         // (if the mountpoint wasn't empty). The only possible behavior on FilesAdded
0150         // is to relist the directory anyway.
0151         const QUrl mp = QUrl::fromLocalFile(m_mountpoint);
0152         org::kde::KDirNotify::emitFilesAdded(mp);
0153 
0154         Q_EMIT q->finished();
0155     }
0156 
0157     q->deleteLater();
0158 }
0159 
0160 KAutoUnmount::~KAutoUnmount() = default;
0161 
0162 #include "moc_kautomount.cpp"
0163 
0164 #endif