File indexing completed on 2024-05-12 17:09:48

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2009 Fredrik Höglund <fredrik@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "desktopnotifier.h"
0008 
0009 #include <KDesktopFile>
0010 #include <KDirWatch>
0011 #include <KPluginFactory>
0012 
0013 #include <kdirnotify.h>
0014 
0015 #include <QDir>
0016 #include <QFile>
0017 #include <QStandardPaths>
0018 
0019 K_PLUGIN_CLASS_WITH_JSON(DesktopNotifier, "desktopnotifier.json")
0020 
0021 DesktopNotifier::DesktopNotifier(QObject *parent, const QList<QVariant> &)
0022     : KDEDModule(parent)
0023 {
0024     m_desktopLocation = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
0025 
0026     dirWatch = new KDirWatch(this);
0027     dirWatch->addDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
0028     dirWatch->addDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + '/' + "Trash/files");
0029     dirWatch->addFile(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs"));
0030 
0031     connect(dirWatch, &KDirWatch::created, this, &DesktopNotifier::created);
0032     connect(dirWatch, &KDirWatch::dirty, this, &DesktopNotifier::dirty);
0033 }
0034 
0035 void DesktopNotifier::watchDir(const QString &path)
0036 {
0037     dirWatch->addDir(path);
0038 }
0039 
0040 void DesktopNotifier::created(const QString &path)
0041 {
0042     if (path == QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")) {
0043         checkDesktopLocation();
0044     }
0045 }
0046 
0047 void DesktopNotifier::dirty(const QString &path)
0048 {
0049     Q_UNUSED(path)
0050 
0051     if (path.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + '/' + "Trash/files")) {
0052         QList<QUrl> trashUrls;
0053 
0054         // Check for any .desktop file linking to trash:/ to update its icon
0055         const auto desktopFiles = QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).entryInfoList({QStringLiteral("*.desktop")});
0056         for (const auto &fi : desktopFiles) {
0057             KDesktopFile df(fi.absoluteFilePath());
0058             if (df.hasLinkType() && df.readUrl() == QLatin1String("trash:/")) {
0059                 trashUrls << QUrl(QStringLiteral("desktop:/") + fi.fileName());
0060             }
0061         }
0062 
0063         if (!trashUrls.isEmpty()) {
0064             org::kde::KDirNotify::emitFilesChanged(trashUrls);
0065         }
0066     } else if (path == QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")) {
0067         checkDesktopLocation();
0068     } else {
0069         // Emitting FilesAdded forces a re-read of the dir
0070         QUrl url;
0071         url.setScheme(QStringLiteral("desktop"));
0072         const auto relativePath = QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).relativeFilePath(path);
0073         url.setPath(QStringLiteral("%1/%2").arg(url.path(), relativePath));
0074         url.setPath(QDir::cleanPath(url.path()));
0075         org::kde::KDirNotify::emitFilesAdded(url);
0076     }
0077 }
0078 
0079 void DesktopNotifier::checkDesktopLocation()
0080 {
0081     const QUrl &currentLocation = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
0082 
0083     if (m_desktopLocation != currentLocation) {
0084         m_desktopLocation = currentLocation;
0085         org::kde::KDirNotify::emitFilesChanged(QList<QUrl>() << QUrl(QStringLiteral("desktop:/")));
0086     }
0087 }
0088 
0089 #include <desktopnotifier.moc>