pragma Singleton import QtQuick import QtQml.Models import Quickshell import Quickshell.Io import Quickshell.Services.Notifications import "NotificationPolicy.js" as Policy Singleton { id: root readonly property bool dnd: Preferences.data.notificationDnd === true || Preferences.error !== "" readonly property bool saveHistory: Preferences.data.notificationHistory !== false readonly property bool enabled: Quickshell.env("ADAPTIVE_NOTIFICATIONS") !== "0" readonly property var items: serverLoader.item ? serverLoader.item.trackedNotifications.values : [] readonly property bool ready: enabled && historyReady && serverLoader.item !== null property var history: [] property var keys: ({}) property var deadlines: ({}) property int sequence: 0 property bool historyReady: false property bool historyWritable: false property string error: "" readonly property string historyDirectory: Quickshell.statePath("notification-history") readonly property string historyPath: historyDirectory + "/notifications.json" property bool storagePrepared: false signal present(var notification) signal removed(int id) function setDnd(value) { Preferences.setValue("notificationDnd", value); } function setSaveHistory(value) { Preferences.setValue("notificationHistory", value); if (!value && Preferences.data.notificationHistory === false) clearHistory(); } function persist() { if (historyReady && historyWritable) historyFile.setText(JSON.stringify({version: 1, entries: history})); } function previous(n) { return history.find(e => e.session === String(Quickshell.processId) && e.notificationId === n.id); } function restoredDeadline(n) { return previous(n)?.deadline; } function received(n, resumed) { if (!keys[n.id]) { const next = Object.assign({}, keys); const old = resumed ? previous(n) : null; next[n.id] = old ? old.key : Date.now().toString(36) + "-" + (++sequence); keys = next; } if (!n.transient && saveHistory && historyReady) { const old = resumed ? history.find(e => e.key === keys[n.id]) : null; history = Policy.upsert(history, {key: keys[n.id], appName: n.appName.slice(0, 512), desktopEntry: n.desktopEntry.slice(0, 512), summary: n.summary.slice(0, 2048), body: n.body.slice(0, 8192), urgency: n.urgency, time: old ? old.time : Date.now(), state: "received", session: String(Quickshell.processId), notificationId: n.id, deadline: deadlines[n.id] || 0}); persist(); } else if (n.transient && history.some(e => e.key === keys[n.id])) { history = history.filter(e => e.key !== keys[n.id]); persist(); } if (!dnd && !resumed) present(n); } function recordClose(id, reason) { const key = keys[id]; history = history.map(e => e.key === key ? Object.assign({}, e, {state: String(reason)}) : e); persist(); const next = Object.assign({}, keys); delete next[id]; keys = next; const timers = Object.assign({}, deadlines); delete timers[id]; deadlines = timers; removed(id); } function setDeadline(id, deadline) { const next = Object.assign({}, deadlines); next[id] = deadline; deadlines = next; } function dismiss(id) { const n = items.find(n => n.id === id); if (n) n.dismiss(); } function invoke(id, action) { const n = items.find(n => n.id === id); const a = n?.actions.find(a => a.identifier === action); if (a) a.invoke(); } function clear() { items.slice().forEach(n => n.dismiss()); clearHistory(); } function clearHistory() { history = []; persist(); } function removeHistory(key) { history = history.filter(e => e.key !== key); persist(); } readonly property var archived: history.filter(e => !Object.values(keys).includes(e.key)) readonly property var groups: { const result = []; const add = (entry, notification) => { const key = entry.desktopEntry || entry.appName || "Приложение"; let group = result.find(g => g.key === key); if (!group) { group = {key: key, name: entry.appName || key, entries: []}; result.push(group); } group.entries.push(notification ? {notification: entry} : {record: entry}); }; items.slice().reverse().filter(n => !n.transient).forEach(n => add(n, true)); archived.forEach(e => add(e, false)); return result; } // The history file is bounded. Images and client actions are intentionally not serialized. Process { command: ["install", "-d", "-m", "700", root.historyDirectory] running: root.enabled onExited: (code, status) => { if (code === 0) root.storagePrepared = true; else { root.error = "Не удалось подготовить приватное хранилище истории"; root.historyReady = true; } } } FileView { id: historyFile path: root.enabled && root.storagePrepared ? root.historyPath : "" atomicWrites: true printErrors: false onLoaded: { if (!root.storagePrepared) return; try { root.history = Policy.validate(JSON.parse(text())); root.historyWritable = true; } catch (e) { root.error = "История не загружена: " + String(e) + ". Файл сохранён: " + root.historyPath; } root.historyReady = true; } onLoadFailed: error => { if (!root.storagePrepared) return; root.historyReady = true; root.historyWritable = error === FileViewError.FileNotFound; if (!root.historyWritable) root.error = "Не удалось прочитать историю: " + root.historyPath; } onSaveFailed: root.error = "Не удалось сохранить историю: " + root.historyPath } Instantiator { model: root.items delegate: NotificationEntry { required property var modelData; notification: modelData } } LazyLoader { id: serverLoader active: root.enabled === true && root.historyReady === true && Preferences.initialized === true component: NotificationServer { bodySupported: true actionsSupported: true imageSupported: true keepOnReload: true onNotification: n => { n.tracked = true; if (trackedNotifications.values.length >= 100) trackedNotifications.values[0].expire(); } } } }