summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/dms/Services/PortalService.qml
blob: 73c0a9f9c324ac77ca31bee7324a4c81397ff00f (plain)
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
pragma Singleton
pragma ComponentBehavior: Bound

import QtQuick
import Quickshell
import Quickshell.Io
import qs.Common

Singleton {
    id: root

    property bool accountsServiceAvailable: false
    property string systemProfileImage: ""
    property string profileImage: ""
    property bool settingsPortalAvailable: false
    property int systemColorScheme: 0

    property bool freedeskAvailable: false
    property string colorSchemeCommand: ""
    property string pendingProfileImage: ""

    readonly property string socketPath: Quickshell.env("DMS_SOCKET")

    function init() {
    }

    function getSystemProfileImage() {
        if (!freedeskAvailable)
            return;
        const username = Quickshell.env("USER");
        if (!username)
            return;
        DMSService.sendRequest("freedesktop.accounts.getUserIconFile", {
            "username": username
        }, response => {
            if (response.result && response.result.success) {
                const iconFile = response.result.value || "";
                if (iconFile && iconFile !== "" && iconFile !== "/var/lib/AccountsService/icons/") {
                    systemProfileImage = iconFile;
                    if (!profileImage || profileImage === "") {
                        profileImage = iconFile;
                    }
                }
            }
        });
    }

    function getUserProfileImage(username) {
        if (!username) {
            profileImage = "";
            return;
        }
        if (Quickshell.env("DMS_RUN_GREETER") === "1" || Quickshell.env("DMS_RUN_GREETER") === "true") {
            profileImage = "";
            return;
        }

        if (!freedeskAvailable) {
            profileImage = "";
            return;
        }

        DMSService.sendRequest("freedesktop.accounts.getUserIconFile", {
            "username": username
        }, response => {
            if (response.result && response.result.success) {
                const icon = response.result.value || "";
                if (icon && icon !== "" && icon !== "/var/lib/AccountsService/icons/") {
                    profileImage = icon;
                } else {
                    profileImage = "";
                }
            } else {
                profileImage = "";
            }
        });
    }

    function setProfileImage(imagePath) {
        if (accountsServiceAvailable) {
            pendingProfileImage = imagePath;
            setSystemProfileImage(imagePath || "");
        } else {
            profileImage = imagePath;
        }
    }

    function getSystemColorScheme() {
        if (typeof SettingsData !== "undefined" && SettingsData.syncModeWithPortal === false) {
            return;
        }
        if (!freedeskAvailable)
            return;
        DMSService.sendRequest("freedesktop.settings.getColorScheme", null, response => {
            if (response.result) {
                systemColorScheme = response.result.value || 0;
            }
        });
    }

    function setLightMode(isLightMode) {
        if (typeof SettingsData !== "undefined" && SettingsData.syncModeWithPortal === false) {
            return;
        }
        setSystemColorScheme(isLightMode);
    }

    function setSystemColorScheme(isLightMode) {
        if (typeof SettingsData !== "undefined" && SettingsData.syncModeWithPortal === false) {
            return;
        }

        const targetScheme = isLightMode ? "default" : "prefer-dark";

        if (colorSchemeCommand === "gsettings") {
            Quickshell.execDetached(["gsettings", "set", "org.gnome.desktop.interface", "color-scheme", targetScheme]);
        }
        if (colorSchemeCommand === "dconf") {
            Quickshell.execDetached(["dconf", "write", "/org/gnome/desktop/interface/color-scheme", `'${targetScheme}'`]);
        }
    }

    function setSystemIconTheme(themeName) {
        if (!settingsPortalAvailable || !freedeskAvailable)
            return;
        DMSService.sendRequest("freedesktop.settings.setIconTheme", {
            "iconTheme": themeName
        }, response => {
            if (response.error) {
                console.warn("PortalService: Failed to set icon theme:", response.error);
            }
        });
    }

    function setSystemProfileImage(imagePath) {
        if (!accountsServiceAvailable || !freedeskAvailable)
            return;
        DMSService.sendRequest("freedesktop.accounts.setIconFile", {
            "path": imagePath || ""
        }, response => {
            if (response.error) {
                console.warn("PortalService: Failed to set icon file:", response.error);

                const errorMsg = response.error.toString();
                let userMessage = I18n.tr("Failed to set profile image");

                if (errorMsg.includes("too large")) {
                    userMessage = I18n.tr("Profile image is too large. Please use a smaller image.");
                } else if (errorMsg.includes("permission")) {
                    userMessage = I18n.tr("Permission denied to set profile image.");
                } else if (errorMsg.includes("not found") || errorMsg.includes("does not exist")) {
                    userMessage = I18n.tr("Selected image file not found.");
                } else {
                    userMessage = I18n.tr("Failed to set profile image: %1").arg(errorMsg.split(":").pop().trim());
                }

                Quickshell.execDetached(["notify-send", "-u", "normal", "-a", "DMS", "-i", "error", I18n.tr("Profile Image Error"), userMessage]);

                pendingProfileImage = "";
            } else {
                profileImage = pendingProfileImage;
                pendingProfileImage = "";
                Qt.callLater(() => getSystemProfileImage());
            }
        });
    }

    Component.onCompleted: {
        if (socketPath && socketPath.length > 0) {
            checkDMSCapabilities();
        } else {
            console.info("PortalService: DMS_SOCKET not set");
        }
        colorSchemeDetector.running = true;
    }

    Connections {
        target: DMSService

        function onConnectionStateChanged() {
            if (DMSService.isConnected) {
                checkDMSCapabilities();
            }
        }
    }

    Connections {
        target: DMSService
        enabled: DMSService.isConnected

        function onCapabilitiesChanged() {
            checkDMSCapabilities();
        }
    }

    function checkDMSCapabilities() {
        if (!DMSService.isConnected) {
            return;
        }

        if (DMSService.capabilities.length === 0) {
            return;
        }

        freedeskAvailable = DMSService.capabilities.includes("freedesktop");
        if (freedeskAvailable) {
            checkAccountsService();
            checkSettingsPortal();
        } else {
            console.info("PortalService: freedesktop capability not available in DMS");
        }
    }

    function checkAccountsService() {
        if (!freedeskAvailable)
            return;
        DMSService.sendRequest("freedesktop.getState", null, response => {
            if (response.result && response.result.accounts) {
                accountsServiceAvailable = response.result.accounts.available || false;
                if (accountsServiceAvailable) {
                    getSystemProfileImage();
                }
            }
        });
    }

    function checkSettingsPortal() {
        if (!freedeskAvailable)
            return;
        DMSService.sendRequest("freedesktop.getState", null, response => {
            if (response.result && response.result.settings) {
                settingsPortalAvailable = response.result.settings.available || false;
                if (settingsPortalAvailable && SettingsData.syncModeWithPortal) {
                    getSystemColorScheme();
                }
            }
        });
    }

    function getGreeterUserProfileImage(username) {
        if (!username) {
            profileImage = "";
            return;
        }
        userProfileCheckProcess.command = ["bash", "-c", `uid=$(id -u ${username} 2>/dev/null) && [ -n "$uid" ] && dbus-send --system --print-reply --dest=org.freedesktop.Accounts /org/freedesktop/Accounts/User$uid org.freedesktop.DBus.Properties.Get string:org.freedesktop.Accounts.User string:IconFile 2>/dev/null | grep -oP 'string "\\K[^"]+' || echo ""`];
        userProfileCheckProcess.running = true;
    }

    Process {
        id: userProfileCheckProcess
        command: []
        running: false

        stdout: StdioCollector {
            onStreamFinished: {
                const trimmed = text.trim();
                if (trimmed && trimmed !== "" && !trimmed.includes("Error") && trimmed !== "/var/lib/AccountsService/icons/") {
                    root.profileImage = trimmed;
                } else {
                    root.profileImage = "";
                }
            }
        }

        onExited: exitCode => {
            if (exitCode !== 0) {
                root.profileImage = "";
            }
        }
    }

    Process {
        id: colorSchemeDetector
        command: ["bash", "-c", "command -v gsettings || command -v dconf"]
        running: false

        stdout: StdioCollector {
            onStreamFinished: {
                const cmd = text.trim();
                if (cmd.includes("gsettings")) {
                    root.colorSchemeCommand = "gsettings";
                } else if (cmd.includes("dconf")) {
                    root.colorSchemeCommand = "dconf";
                }
            }
        }
    }

    IpcHandler {
        target: "profile"

        function getImage(): string {
            return root.profileImage;
        }

        function setImage(path: string): string {
            if (!path) {
                return "ERROR: No path provided";
            }

            const absolutePath = path.startsWith("/") ? path : `${StandardPaths.writableLocation(StandardPaths.HomeLocation)}/${path}`;

            try {
                root.setProfileImage(absolutePath);
                return "SUCCESS: Profile image set to " + absolutePath;
            } catch (e) {
                return "ERROR: Failed to set profile image: " + e.toString();
            }
        }

        function clearImage(): string {
            root.setProfileImage("");
            return "SUCCESS: Profile image cleared";
        }
    }
}