Debian Codegraph @

src/util/ddesktopservices_linux.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "ddesktopservices.h"
6 #include <QtDBus/QtDBus>
7 #include <QDebug>
8 #include <QFile>
9
10 DGUI_BEGIN_NAMESPACE
11
12 #define EASY_CALL_DBUS(name) \
13 QDBusInterface *interface = fileManager1DBusInterface(); \
14 return interface && \
15 interface->call(#name, urls2uris(urls), startupId).type() != \
16 QDBusMessage::ErrorMessage;
17
18 static const QStringList SOUND_EFFECT_LIST {
19 "message",
20 "camera-shutter",
21 "trash-empty",
22 "x-deepin-app-sent-to-desktop",
23 "desktop-login",
24 "system-shutdown",
25 "desktop-logout",
26 "suspend-resume",
27 "audio-volume-change",
28 "power-unplug-battery-low",
29 "power-plug",
30 "power-unplug",
31 "device-added",
32 "device-removed",
33 "dialog-error",
34 };
35
36 static QDBusInterface *fileManager1DBusInterface()
37 {
38 static QDBusInterface interface(QStringLiteral("org.freedesktop.FileManager1"),
39 QStringLiteral("/org/freedesktop/FileManager1"),
40 QStringLiteral("org.freedesktop.FileManager1"));
41 return &interface;
42 }
43
44 static QStringList urls2uris(const QList<QUrl> &urls)
45 {
46 QStringList list;
47
48 list.reserve(urls.size());
49
50 for (const QUrl &url : urls) {
51 list << url.toString();
52 }
53
54 return list;
55 }
56
57 static QList<QUrl> path2urls(const QList<QString> &paths)
58 {
59 QList<QUrl> list;
60
61 list.reserve(paths.size());
62
63 for (const QString &path : paths) {
64 list << QUrl::fromLocalFile(path);
65 }
66
67 return list;
68 }
69
70 static QDBusInterface soundEffectInterface()
71 {
72 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
73 const auto& infc = QDBusConnection::sessionBus().interface();
74 QStringList activatableServiceNames = infc->activatableServiceNames();
75 bool isNewInterface = activatableServiceNames.contains(QLatin1String("org.deepin.dde.SoundEffect1"));
76 #else
77 bool isNewInterface = false; // Qt 5.14 以下就直接用旧的接口
78 #endif
79 const QLatin1String service(isNewInterface ? "org.deepin.dde.SoundEffect1" :"com.deepin.daemon.SoundEffect");
80 const QLatin1String path(isNewInterface ? "/org/deepin/dde/SoundEffect1" : "/com/deepin/daemon/SoundEffect");
81 const QLatin1String interface(isNewInterface ? "org.deepin.dde.SoundEffect1" :"com.deepin.daemon.SoundEffect");
82
83 // 使用后端 dbus 接口播放系统音频,音频存放目录: /usr/share/sounds/deepin/stereo/
84 return QDBusInterface(service, path, interface);
85 }
86
87 static bool systemSoundEffectEnabled(const QString &name)
88 {
89 auto soundEffect = soundEffectInterface();
90 if (!soundEffect.property("Enabled").toBool())
91 return false;
92
93 QDBusReply<bool> reply = soundEffect.call("IsSoundEnabled", name);
94
95 return reply.isValid() && reply.value();
96 }
97
98 bool DDesktopServices::showFolder(const QString &localFilePath, const QString &startupId)
99 {
100 return showFolder(QUrl::fromLocalFile(localFilePath), startupId);
101 }
102
103 bool DDesktopServices::showFolders(const QList<QString> &localFilePaths, const QString &startupId)
104 {
105 return showFolders(path2urls(localFilePaths), startupId);
106 }
107
108 bool DDesktopServices::showFolder(const QUrl &url, const QString &startupId)
109 {
110 return showFolders(QList<QUrl>() << url, startupId);
111 }
112
113 bool DDesktopServices::showFolders(const QList<QUrl> &urls, const QString &startupId)
114 {
115 EASY_CALL_DBUS(ShowFolders)
116 }
117
118 bool DDesktopServices::showFileItemProperty(const QString &localFilePath, const QString &startupId)
119 {
120 return showFileItemProperty(QUrl::fromLocalFile(localFilePath), startupId);
121 }
122
123 bool DDesktopServices::showFileItemProperties(const QList<QString> &localFilePaths, const QString &startupId)
124 {
125 return showFileItemProperties(path2urls(localFilePaths), startupId);
126 }
127
128 bool DDesktopServices::showFileItemProperty(const QUrl &url, const QString &startupId)
129 {
130 return showFileItemProperties(QList<QUrl>() << url, startupId);
131 }
132
133 bool DDesktopServices::showFileItemProperties(const QList<QUrl> &urls, const QString &startupId)
134 {
135 EASY_CALL_DBUS(ShowItemProperties)
136 }
137
138 bool DDesktopServices::showFileItem(const QString &localFilePath, const QString &startupId)
139 {
140 return showFileItem(QUrl::fromLocalFile(localFilePath), startupId);
141 }
142
143 bool DDesktopServices::showFileItems(const QList<QString> &localFilePaths, const QString &startupId)
144 {
145 return showFileItems(path2urls(localFilePaths), startupId);
146 }
147
148 bool DDesktopServices::showFileItem(const QUrl &url, const QString &startupId)
149 {
150 return showFileItems(QList<QUrl>() << url, startupId);
151 }
152
153 bool DDesktopServices::showFileItems(const QList<QUrl> &urls, const QString &startupId)
154 {
155 EASY_CALL_DBUS(ShowItems)
156 }
157
158 bool DDesktopServices::trash(const QString &localFilePath)
159 {
160 return trash(QUrl::fromLocalFile(localFilePath));
161 }
162
163 bool DDesktopServices::trash(const QList<QString> &localFilePaths)
164 {
165 return trash(path2urls(localFilePaths));
166 }
167
168 bool DDesktopServices::trash(const QUrl &url)
169 {
170 return trash(QList<QUrl>() << url);
171 }
172
173 bool DDesktopServices::trash(const QList<QUrl> &urls)
174 {
175 QDBusInterface *interface = fileManager1DBusInterface();
176 return interface && interface->call("Trash", urls2uris(urls)).type() != QDBusMessage::ErrorMessage;
177 }
178
179 bool DDesktopServices::playSystemSoundEffect(const DDesktopServices::SystemSoundEffect &effect)
180 {
181 return playSystemSoundEffect(SOUND_EFFECT_LIST.at(static_cast<int>(effect)));
182 }
183
184 bool DDesktopServices::playSystemSoundEffect(const QString &name)
185 {
186 if (!systemSoundEffectEnabled(name))
187 return false;
188
189 return previewSystemSoundEffect(name);
190 }
191
192 bool DDesktopServices::previewSystemSoundEffect(const DDesktopServices::SystemSoundEffect &effect)
193 {
194 return previewSystemSoundEffect(SOUND_EFFECT_LIST.at(static_cast<int>(effect)));
195 }
196
197 bool DDesktopServices::previewSystemSoundEffect(const QString &name)
198 {
199 if (name.isEmpty()) {
200 return false;
201 }
202
203 // 使用后端 dbus 接口播放系统音频,音频存放目录: /usr/share/sounds/deepin/stereo/
204 return soundEffectInterface().call("PlaySound", name).type() != QDBusMessage::ErrorMessage;
205 }
206
207 QString DDesktopServices::getNameByEffectType(const DDesktopServices::SystemSoundEffect &effect)
208 {
209 return SOUND_EFFECT_LIST.at(static_cast<int>(effect));
210 }
211
212 QString DDesktopServices::errorMessage()
213 {
214 return fileManager1DBusInterface()->lastError().message();
215 }
216
217 DGUI_END_NAMESPACE