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
|
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Pipewire
import qs.Commons
import qs.Services.UI
Item {
id: root
property var pluginApi: null
// --- Logic extracted from BarWidget.qml ---
property bool micActive: false
property bool camActive: false
property bool scrActive: false
property var micApps: []
property var camApps: []
property var scrApps: []
property var accessHistory: []
// Previous states for history tracking
property var _prevMicApps: []
property var _prevCamApps: []
property var _prevScrApps: []
// Get active color from settings or default
property var cfg: pluginApi?.pluginSettings || ({})
property var defaults: pluginApi?.manifest?.metadata?.defaultSettings || ({})
property bool enableToast: cfg.enableToast ?? defaults.enableToast ?? true
property string activeColorKey: cfg.activeColor ?? defaults.activeColor ?? "primary"
property string micFilterRegex: cfg.micFilterRegex ?? defaults.micFilterRegex ?? ""
property string camFilterRegex: cfg.camFilterRegex ?? defaults.camFilterRegex ?? ""
PwObjectTracker {
objects: Pipewire.ready ? Pipewire.nodes.values : []
}
Process {
id: cameraDetectionProcess
running: false
command: ["sh", "-c", "for dev in /sys/class/video4linux/video*; do [ -e \"$dev/name\" ] && grep -qv 'Metadata' \"$dev/name\" && dev_name=$(basename \"$dev\") && find /proc/[0-9]*/fd -lname \"/dev/$dev_name\" 2>/dev/null; done | cut -d/ -f3 | xargs -r ps -o comm= -p | sort -u | tr '\\n' ',' | sed 's/,$//'"]
stdout: StdioCollector {
onStreamFinished: {
var appsString = this.text.trim();
var apps = appsString.length > 0 ? appsString.split(',') : [];
var filterRegex = null;
if (root.camFilterRegex && root.camFilterRegex.length > 0) {
try {
filterRegex = new RegExp(root.camFilterRegex);
} catch (e) {
Logger.w("PrivacyIndicator: Invalid camFilterRegex:", root.camFilterRegex);
}
}
var appNames = [];
for (var i = 0; i < apps.length; i++) {
var appName = apps[i];
if (filterRegex && appName && filterRegex.test(appName)) continue;
if (appName && appNames.indexOf(appName) === -1) appNames.push(appName);
}
root.camApps = appNames;
root.camActive = appNames.length > 0;
}
}
}
Timer {
interval: 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: updatePrivacyState()
}
function hasNodeLinks(node, links) {
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link && (link.source === node || link.target === node)) return true;
}
return false;
}
function getAppName(node) {
return node.properties["application.name"] || node.nickname || node.name || "";
}
function updateMicrophoneState(nodes, links) {
var appNames = [];
var isActive = false;
var filterRegex = null;
if (root.micFilterRegex && root.micFilterRegex.length > 0) {
try {
filterRegex = new RegExp(root.micFilterRegex);
} catch (e) {
Logger.w("PrivacyIndicator: Invalid micFilterRegex:", root.micFilterRegex);
}
}
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (!node || !node.isStream || !node.audio || node.isSink) continue;
if (!hasNodeLinks(node, links) || !node.properties) continue;
var mediaClass = node.properties["media.class"] || "";
if (mediaClass === "Stream/Input/Audio") {
if (node.properties["stream.capture.sink"] === "true") continue;
var appName = getAppName(node);
if (filterRegex && appName && filterRegex.test(appName)) continue;
isActive = true;
if (appName && appNames.indexOf(appName) === -1) appNames.push(appName);
}
}
root.micActive = isActive;
root.micApps = appNames;
}
function updateCameraState() {
cameraDetectionProcess.running = true;
}
function isScreenShareNode(node) {
if (!node.properties) return false;
var mediaClass = node.properties["media.class"] || "";
if (mediaClass.indexOf("Audio") >= 0) return false;
if (mediaClass.indexOf("Video") === -1) return false;
var mediaName = (node.properties["media.name"] || "").toLowerCase();
if (mediaName.match(/^(xdph-streaming|gsr-default|game capture|screen|desktop|display|cast|webrtc|v4l2)/) ||
mediaName === "gsr-default_output" ||
mediaName.match(/screen-cast|screen-capture|desktop-capture|monitor-capture|window-capture|game-capture/i)) {
return true;
}
return false;
}
function updateScreenShareState(nodes, links) {
var appNames = [];
var isActive = false;
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (!node || !hasNodeLinks(node, links) || !node.properties) continue;
if (isScreenShareNode(node)) {
isActive = true;
var appName = getAppName(node);
if (appName && appNames.indexOf(appName) === -1) appNames.push(appName);
}
}
root.scrActive = isActive;
root.scrApps = appNames;
}
function updatePrivacyState() {
if (!Pipewire.ready) return;
var nodes = Pipewire.nodes.values || [];
var links = Pipewire.links.values || [];
updateMicrophoneState(nodes, links);
updateCameraState();
updateScreenShareState(nodes, links);
}
// --- History Persistence ---
property string stateFile: ""
property bool isLoaded: false
Component.onCompleted: {
// Setup state file path
Qt.callLater(() => {
if (typeof Settings !== 'undefined' && Settings.cacheDir) {
stateFile = Settings.cacheDir + "privacy-history.json";
historyFileView.path = stateFile;
}
});
}
FileView {
id: historyFileView
printErrors: false
watchChanges: false
adapter: JsonAdapter {
id: adapter
property var history: []
}
onLoaded: {
root.isLoaded = true;
if (adapter.history) {
// Restore history
root.accessHistory = adapter.history;
}
}
onLoadFailed: error => {
// If file doesn't exist (error 2), we are ready to save new data
if (error === 2) {
root.isLoaded = true;
} else {
console.error("PrivacyIndicator: Failed to load history file:", error);
root.isLoaded = true; // Try to continue anyway
}
}
}
function saveHistory() {
if (!stateFile || !isLoaded) return;
adapter.history = root.accessHistory;
// Ensure cache directory exists and save
try {
Quickshell.execDetached(["mkdir", "-p", Settings.cacheDir]);
Qt.callLater(() => {
try {
historyFileView.writeAdapter();
} catch (e) {
console.error("PrivacyIndicator: Failed to save history", e);
}
});
} catch (e) {
console.error("PrivacyIndicator: Failed to save history", e);
}
}
function addToHistory(app, type, icon, colorKey, action) {
var time = new Date().toLocaleTimeString(Qt.locale(), Locale.ShortFormat);
var entry = {
"appName": app,
"type": type,
"icon": icon,
"colorKey": colorKey,
"time": time,
"timestamp": Date.now(),
"action": action // "started" or "stopped"
};
var newHistory = [entry].concat(accessHistory);
if (newHistory.length > 50) newHistory = newHistory.slice(0, 50); // Increased limit as we have more entries now
accessHistory = newHistory;
saveHistory();
}
function clearHistory() {
accessHistory = [];
saveHistory();
}
function checkAppChanges(newApps, oldApps, type, icon, colorKey) {
if (!newApps && !oldApps) return;
// Check for new apps (Started)
if (newApps) {
for (var i = 0; i < newApps.length; i++) {
var app = newApps[i];
if (!oldApps || oldApps.indexOf(app) === -1) {
addToHistory(app, type, icon, colorKey, "started");
}
}
}
// Check for removed apps (Stopped)
if (oldApps) {
for (var j = 0; j < oldApps.length; j++) {
var oldApp = oldApps[j];
if (!newApps || newApps.indexOf(oldApp) === -1) {
addToHistory(oldApp, type, icon, colorKey, "stopped");
}
}
}
}
onMicAppsChanged: {
checkAppChanges(micApps, _prevMicApps, "Microphone", "microphone", activeColorKey);
_prevMicApps = micApps;
}
// Helper to detect activation edge
property bool oldMicActive: false
onMicActiveChanged: {
if (enableToast && micActive && !oldMicActive) {
ToastService.showNotice(pluginApi?.tr("toast.mic-on"), "", "microphone");
}
oldMicActive = micActive
}
property bool oldCamActive: false
onCamActiveChanged: {
if (enableToast && camActive && !oldCamActive) {
ToastService.showNotice(pluginApi?.tr("toast.cam-on"), "", "camera");
}
oldCamActive = camActive
}
onCamAppsChanged: {
checkAppChanges(camApps, _prevCamApps, "Camera", "camera", activeColorKey);
_prevCamApps = camApps;
}
property bool oldScrActive: false
onScrActiveChanged: {
if (enableToast && scrActive && !oldScrActive) {
ToastService.showNotice(pluginApi?.tr("toast.screen-on"), "", "screen-share");
}
oldScrActive = scrActive
}
onScrAppsChanged: {
checkAppChanges(scrApps, _prevScrApps, "Screen", "screen-share", activeColorKey);
_prevScrApps = scrApps;
}
}
|