Changeset View
Changeset View
Standalone View
Standalone View
kioslave/desktop/desktopnotifier.cpp
- This file was added.
1 | /* This file is part of the KDE Project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2008, 2009 Fredrik Höglund <fredrik@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License version 2 as published by the Free Software Foundation. | ||||
7 | | ||||
8 | This library is distributed in the hope that it will be useful, | ||||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
11 | Library General Public License for more details. | ||||
12 | | ||||
13 | You should have received a copy of the GNU Library General Public License | ||||
14 | along with this library; see the file COPYING.LIB. If not, write to | ||||
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
16 | Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "desktopnotifier.h" | ||||
20 | | ||||
21 | #include <KDirWatch> | ||||
22 | #include <KPluginFactory> | ||||
23 | #include <KPluginLoader> | ||||
24 | | ||||
25 | #include <kdirnotify.h> | ||||
26 | | ||||
27 | #include <QStandardPaths> | ||||
28 | #include <QUrl> | ||||
29 | | ||||
30 | K_PLUGIN_FACTORY_WITH_JSON(DesktopNotifierFactory, | ||||
31 | "desktopnotifier.json", | ||||
32 | registerPlugin<DesktopNotifier>();) | ||||
33 | | ||||
34 | DesktopNotifier::DesktopNotifier(QObject *parent, const QList<QVariant> &) | ||||
35 | : KDEDModule(parent) | ||||
36 | { | ||||
37 | m_desktopLocation = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); | ||||
38 | | ||||
39 | dirWatch = new KDirWatch(this); | ||||
40 | dirWatch->addDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); | ||||
41 | dirWatch->addDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + '/' + "Trash/files"); | ||||
42 | dirWatch->addFile(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")); | ||||
43 | | ||||
44 | connect(dirWatch, &KDirWatch::created, this, &DesktopNotifier::created); | ||||
45 | connect(dirWatch, &KDirWatch::dirty, this, &DesktopNotifier::dirty); | ||||
46 | } | ||||
47 | | ||||
48 | void DesktopNotifier::watchDir(const QString &path) | ||||
49 | { | ||||
50 | dirWatch->addDir(path); | ||||
51 | } | ||||
52 | | ||||
53 | void DesktopNotifier::created(const QString &path) | ||||
54 | { | ||||
55 | if (path == QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")){ | ||||
56 | checkDesktopLocation(); | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | void DesktopNotifier::dirty(const QString &path) | ||||
61 | { | ||||
62 | Q_UNUSED(path) | ||||
63 | | ||||
64 | if (path.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + '/' + "Trash/files")) { | ||||
65 | // Trigger an update of the trash icon | ||||
66 | if (QFile::exists(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/trash.desktop")) | ||||
67 | org::kde::KDirNotify::emitFilesChanged(QList<QUrl>() << QUrl(QStringLiteral("desktop:/trash.desktop"))); | ||||
68 | } else if (path == QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")){ | ||||
69 | checkDesktopLocation(); | ||||
70 | } else { | ||||
71 | // Emitting FilesAdded forces a re-read of the dir | ||||
72 | QUrl url; | ||||
73 | url.setScheme(QStringLiteral("desktop")); | ||||
74 | const auto relativePath = QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).relativeFilePath(path); | ||||
75 | url.setPath(QStringLiteral("%1/%2").arg(url.path(), relativePath)); | ||||
76 | url.setPath(QDir::cleanPath(url.path())); | ||||
77 | org::kde::KDirNotify::emitFilesAdded(url); | ||||
78 | } | ||||
79 | } | ||||
80 | | ||||
81 | void DesktopNotifier::checkDesktopLocation() | ||||
82 | { | ||||
83 | const QUrl ¤tLocation = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); | ||||
84 | | ||||
85 | if (m_desktopLocation != currentLocation) { | ||||
86 | m_desktopLocation = currentLocation; | ||||
87 | org::kde::KDirNotify::emitFilesChanged(QList<QUrl>() << QUrl(QStringLiteral("desktop:/"))); | ||||
88 | } | ||||
89 | } | ||||
90 | | ||||
91 | #include <desktopnotifier.moc> |