mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-06-21 10:35:26 -04:00
calendar(dank): Add support for DankCalendar backend
- Add keyboard navigation to overview - Add edit events to overview - Add create events to overview - Add setting for auto/khal/dankcalendar backend selection
This commit is contained in:
@@ -0,0 +1,478 @@
|
||||
pragma ComponentBehavior: Bound
|
||||
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Common
|
||||
import qs.Services
|
||||
|
||||
Item {
|
||||
id: root
|
||||
readonly property var log: Log.scoped("CalendarDankBackend")
|
||||
|
||||
property bool enabled: false
|
||||
|
||||
property string socketPath: ""
|
||||
readonly property bool socketFound: socketPath.length > 0
|
||||
property bool connected: false
|
||||
property bool binaryExists: false
|
||||
property bool binaryChecked: false
|
||||
|
||||
property var calendars: []
|
||||
property var events: []
|
||||
property var eventsByDate: ({})
|
||||
property string lastError: ""
|
||||
property date focusDate: new Date()
|
||||
property var _loadedFrom: null
|
||||
property var _loadedTo: null
|
||||
|
||||
property var pendingRequests: ({})
|
||||
property int requestCounter: 0
|
||||
|
||||
readonly property var fallbackPalette: ["#7287fd", "#f38ba8", "#a6e3a1", "#fab387", "#cba6f7", "#94e2d5", "#f9e2af", "#89dceb"]
|
||||
|
||||
signal eventsUpdated
|
||||
|
||||
onEnabledChanged: {
|
||||
if (enabled) {
|
||||
if (!connected)
|
||||
discoverProcess.running = true;
|
||||
return;
|
||||
}
|
||||
requestSocket.connected = false;
|
||||
subscribeSocket.connected = false;
|
||||
socketPath = "";
|
||||
connected = false;
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
binaryCheck.running = true;
|
||||
discoverProcess.running = true;
|
||||
}
|
||||
|
||||
Process {
|
||||
id: binaryCheck
|
||||
command: ["sh", "-c", "command -v dankcal"]
|
||||
running: false
|
||||
onExited: code => {
|
||||
root.binaryExists = (code === 0);
|
||||
root.binaryChecked = true;
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: discoverProcess
|
||||
running: false
|
||||
command: ["sh", "-c", "s=\"${DANKCAL_SOCKET:-}\"; if [ -S \"$s\" ]; then echo \"$s\"; exit 0; fi; for f in \"${XDG_RUNTIME_DIR:-/tmp}\"/dankcal-*.sock /tmp/dankcal-*.sock; do [ -S \"$f\" ] || continue; p=$(basename \"$f\" .sock); p=${p#dankcal-}; if kill -0 \"$p\" 2>/dev/null; then echo \"$f\"; exit 0; fi; done"]
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const path = text.trim().split('\n')[0] || "";
|
||||
if (path.length > 0) {
|
||||
root._applySocketPath(path);
|
||||
return;
|
||||
}
|
||||
if (!root.connected) {
|
||||
if (root.socketPath !== "")
|
||||
root.log.info("dankcal socket gone, waiting for daemon");
|
||||
requestSocket.connected = false;
|
||||
subscribeSocket.connected = false;
|
||||
root.socketPath = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
id: rediscoverTimer
|
||||
interval: 3000
|
||||
repeat: true
|
||||
running: root.enabled && !root.connected
|
||||
onTriggered: {
|
||||
if (!discoverProcess.running)
|
||||
discoverProcess.running = true;
|
||||
}
|
||||
}
|
||||
|
||||
function launch() {
|
||||
if (!binaryExists)
|
||||
return;
|
||||
Quickshell.execDetached(["dankcal", "run", "-d", "--hidden"]);
|
||||
if (enabled && !connected)
|
||||
discoverProcess.running = true;
|
||||
}
|
||||
|
||||
function _applySocketPath(path) {
|
||||
if (path === socketPath) {
|
||||
if (socketFound && !connected)
|
||||
requestSocket.connected = true;
|
||||
return;
|
||||
}
|
||||
log.info("dankcal socket discovered:", path);
|
||||
requestSocket.connected = false;
|
||||
subscribeSocket.connected = false;
|
||||
socketPath = path;
|
||||
Qt.callLater(() => requestSocket.connected = true);
|
||||
}
|
||||
|
||||
DankSocket {
|
||||
id: requestSocket
|
||||
path: root.socketPath
|
||||
connected: false
|
||||
|
||||
onConnectionStateChanged: {
|
||||
if (linkUp) {
|
||||
root.connected = true;
|
||||
subscribeSocket.connected = true;
|
||||
root.log.info("connected to dankcal:", root.socketPath);
|
||||
root.refreshCalendars();
|
||||
root.reloadEvents();
|
||||
return;
|
||||
}
|
||||
if (!root.connected && !root.socketFound)
|
||||
return;
|
||||
root.connected = false;
|
||||
root._flushPending();
|
||||
requestSocket.connected = false;
|
||||
subscribeSocket.connected = false;
|
||||
root.log.info("dankcal disconnected, rediscovering");
|
||||
if (root.enabled)
|
||||
discoverProcess.running = true;
|
||||
}
|
||||
|
||||
parser: SplitParser {
|
||||
onRead: line => {
|
||||
if (!line || line.length === 0)
|
||||
return;
|
||||
let response;
|
||||
try {
|
||||
response = JSON.parse(line);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
root._handleResponse(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DankSocket {
|
||||
id: subscribeSocket
|
||||
path: root.socketPath
|
||||
connected: false
|
||||
|
||||
onConnectionStateChanged: {
|
||||
if (linkUp)
|
||||
root._sendSubscribe();
|
||||
}
|
||||
|
||||
parser: SplitParser {
|
||||
onRead: line => {
|
||||
if (!line || line.length === 0)
|
||||
return;
|
||||
let event;
|
||||
try {
|
||||
event = JSON.parse(line);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
root._handleEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
id: refreshDebounce
|
||||
interval: 400
|
||||
repeat: false
|
||||
onTriggered: {
|
||||
root.refreshCalendars();
|
||||
root.reloadEvents();
|
||||
}
|
||||
}
|
||||
|
||||
function _sendSubscribe() {
|
||||
subscribeSocket.send({
|
||||
"id": _nextId(),
|
||||
"method": "subscribe",
|
||||
"params": {
|
||||
"topics": ["accounts", "calendars", "events", "sync"]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function _nextId() {
|
||||
requestCounter++;
|
||||
return Date.now() + requestCounter;
|
||||
}
|
||||
|
||||
function _flushPending() {
|
||||
const ids = Object.keys(pendingRequests);
|
||||
for (const id of ids) {
|
||||
const cb = pendingRequests[id];
|
||||
delete pendingRequests[id];
|
||||
if (cb)
|
||||
cb({
|
||||
"error": "disconnected"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function _handleResponse(response) {
|
||||
if (response.event) {
|
||||
_handleEvent(response);
|
||||
return;
|
||||
}
|
||||
const id = response.id;
|
||||
if (!id)
|
||||
return;
|
||||
const cb = pendingRequests[id];
|
||||
if (cb) {
|
||||
delete pendingRequests[id];
|
||||
cb(response);
|
||||
}
|
||||
}
|
||||
|
||||
function _handleEvent(event) {
|
||||
switch (event.event) {
|
||||
case "accounts":
|
||||
case "calendars":
|
||||
refreshCalendars();
|
||||
refreshDebounce.restart();
|
||||
break;
|
||||
case "events":
|
||||
case "sync":
|
||||
refreshDebounce.restart();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function sendRequest(method, params, callback) {
|
||||
if (!connected) {
|
||||
if (callback)
|
||||
callback({
|
||||
"error": "not connected to dankcal socket"
|
||||
});
|
||||
return;
|
||||
}
|
||||
const id = _nextId();
|
||||
const req = {
|
||||
"id": id,
|
||||
"method": method
|
||||
};
|
||||
if (params)
|
||||
req.params = params;
|
||||
if (callback)
|
||||
pendingRequests[id] = callback;
|
||||
requestSocket.send(req);
|
||||
}
|
||||
|
||||
function refreshCalendars() {
|
||||
sendRequest("calendars.list", null, response => {
|
||||
if (response.error) {
|
||||
lastError = response.error;
|
||||
return;
|
||||
}
|
||||
const list = response.result || [];
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
if (!list[i].color)
|
||||
list[i].color = fallbackPalette[i % fallbackPalette.length];
|
||||
}
|
||||
calendars = list;
|
||||
_rebuildEventsByDate();
|
||||
});
|
||||
}
|
||||
|
||||
function calendarById(id) {
|
||||
for (let i = 0; i < calendars.length; i++) {
|
||||
if (calendars[i].id === id)
|
||||
return calendars[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function writableCalendars() {
|
||||
return calendars.filter(c => !c.readOnly);
|
||||
}
|
||||
|
||||
function defaultCalendar() {
|
||||
const writable = writableCalendars().filter(c => !c.hidden);
|
||||
return writable.length > 0 ? writable[0] : null;
|
||||
}
|
||||
|
||||
function loadEvents(startDate, endDate) {
|
||||
const mid = new Date((startDate.getTime() + endDate.getTime()) / 2);
|
||||
focusDate = mid;
|
||||
_ensureWindow();
|
||||
}
|
||||
|
||||
function _ensureWindow() {
|
||||
if (!connected)
|
||||
return;
|
||||
if (!_loadedFrom || !_loadedTo) {
|
||||
reloadEvents();
|
||||
return;
|
||||
}
|
||||
const margin = 14 * 86400000;
|
||||
const t = focusDate.getTime();
|
||||
if (t < _loadedFrom.getTime() + margin || t > _loadedTo.getTime() - margin)
|
||||
reloadEvents();
|
||||
else
|
||||
_rebuildEventsByDate();
|
||||
}
|
||||
|
||||
function reloadEvents() {
|
||||
if (!connected)
|
||||
return;
|
||||
const from = new Date(focusDate.getTime() - 60 * 86400000);
|
||||
const to = new Date(focusDate.getTime() + 90 * 86400000);
|
||||
sendRequest("events.list", {
|
||||
"from": from.toISOString(),
|
||||
"to": to.toISOString(),
|
||||
"limit": 5000
|
||||
}, response => {
|
||||
if (response.error) {
|
||||
lastError = response.error;
|
||||
return;
|
||||
}
|
||||
_loadedFrom = from;
|
||||
_loadedTo = to;
|
||||
const raw = (response.result || {}).events || [];
|
||||
events = raw.map(e => _normalizeEvent(e));
|
||||
_rebuildEventsByDate();
|
||||
});
|
||||
}
|
||||
|
||||
function _dayBoundary(iso) {
|
||||
const d = new Date(iso);
|
||||
return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
|
||||
}
|
||||
|
||||
function _normalizeEvent(e) {
|
||||
const allDay = !!e.allDay;
|
||||
const id = e.id || "";
|
||||
if (id.startsWith("task_"))
|
||||
log.warn("daemon event id collides with task prefix:", id);
|
||||
return {
|
||||
"id": id,
|
||||
"calendarId": e.calendarId || "",
|
||||
"title": e.summary || "(untitled)",
|
||||
"description": e.description || "",
|
||||
"location": e.location || "",
|
||||
"url": e.url || "",
|
||||
"start": allDay ? _dayBoundary(e.start) : new Date(e.start),
|
||||
"end": allDay ? _dayBoundary(e.end) : new Date(e.end),
|
||||
"allDay": allDay,
|
||||
"status": e.status || "confirmed",
|
||||
"recurringId": e.recurringId || "",
|
||||
"attendees": e.attendees || [],
|
||||
"organizer": e.organizer || null,
|
||||
"reminders": e.reminders || []
|
||||
};
|
||||
}
|
||||
|
||||
function decorateEvent(ev) {
|
||||
const cal = calendarById(ev.calendarId);
|
||||
const out = Object.assign({}, ev);
|
||||
out.color = cal ? cal.color : fallbackPalette[0];
|
||||
out.calendar = cal ? cal.name : "";
|
||||
out.account = cal ? (cal.accountName || cal.accountId || "") : "";
|
||||
out.readOnly = cal ? !!cal.readOnly : false;
|
||||
out.isMultiDay = ev.start.toDateString() !== ev.end.toDateString();
|
||||
return out;
|
||||
}
|
||||
|
||||
function _hiddenCalendarIds() {
|
||||
const hidden = {};
|
||||
for (let i = 0; i < calendars.length; i++) {
|
||||
if (calendars[i].hidden)
|
||||
hidden[calendars[i].id] = true;
|
||||
}
|
||||
return hidden;
|
||||
}
|
||||
|
||||
function _clampForDay(ev, cur, endDay) {
|
||||
const out = Object.assign({}, ev);
|
||||
const dayStart = new Date(cur.getFullYear(), cur.getMonth(), cur.getDate());
|
||||
const startDay = new Date(ev.start.getFullYear(), ev.start.getMonth(), ev.start.getDate());
|
||||
if (dayStart.getTime() === startDay.getTime()) {
|
||||
out.start = new Date(ev.start);
|
||||
} else {
|
||||
out.start = new Date(dayStart);
|
||||
if (!ev.allDay)
|
||||
out.start.setHours(0, 0, 0, 0);
|
||||
}
|
||||
if (dayStart.getTime() === endDay.getTime()) {
|
||||
out.end = new Date(ev.end);
|
||||
} else {
|
||||
out.end = new Date(dayStart);
|
||||
if (!ev.allDay)
|
||||
out.end.setHours(23, 59, 59, 999);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function _rebuildEventsByDate() {
|
||||
const hidden = _hiddenCalendarIds();
|
||||
const map = {};
|
||||
for (const raw of events) {
|
||||
if (raw.status === "cancelled")
|
||||
continue;
|
||||
if (hidden[raw.calendarId])
|
||||
continue;
|
||||
const ev = decorateEvent(raw);
|
||||
const lastInstant = ev.allDay ? new Date(ev.end.getTime() - 1) : ev.end;
|
||||
let cur = new Date(ev.start.getFullYear(), ev.start.getMonth(), ev.start.getDate());
|
||||
let endDay = new Date(lastInstant.getFullYear(), lastInstant.getMonth(), lastInstant.getDate());
|
||||
if (endDay < cur)
|
||||
endDay = new Date(cur);
|
||||
while (cur <= endDay) {
|
||||
const key = Qt.formatDate(cur, "yyyy-MM-dd");
|
||||
if (!map[key])
|
||||
map[key] = [];
|
||||
if (!map[key].some(e => e.id === ev.id))
|
||||
map[key].push(_clampForDay(ev, cur, endDay));
|
||||
cur.setDate(cur.getDate() + 1);
|
||||
}
|
||||
}
|
||||
eventsByDate = map;
|
||||
eventsUpdated();
|
||||
}
|
||||
|
||||
function createEvent(fields, callback) {
|
||||
sendRequest("events.create", fields, response => {
|
||||
if (response.error)
|
||||
lastError = response.error;
|
||||
else
|
||||
reloadEvents();
|
||||
if (callback)
|
||||
callback(response);
|
||||
});
|
||||
}
|
||||
|
||||
function updateEvent(id, fields, callback) {
|
||||
const params = Object.assign({
|
||||
"id": id
|
||||
}, fields);
|
||||
sendRequest("events.update", params, response => {
|
||||
if (response.error)
|
||||
lastError = response.error;
|
||||
else
|
||||
reloadEvents();
|
||||
if (callback)
|
||||
callback(response);
|
||||
});
|
||||
}
|
||||
|
||||
function deleteEvent(id, callback) {
|
||||
sendRequest("events.delete", {
|
||||
"id": id
|
||||
}, response => {
|
||||
if (response.error)
|
||||
lastError = response.error;
|
||||
else
|
||||
reloadEvents();
|
||||
if (callback)
|
||||
callback(response);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
pragma ComponentBehavior: Bound
|
||||
|
||||
import QtQuick
|
||||
import Quickshell.Io
|
||||
import qs.Common
|
||||
import qs.Services
|
||||
|
||||
Item {
|
||||
id: root
|
||||
readonly property var log: Log.scoped("CalendarKhalBackend")
|
||||
|
||||
property bool installed: false
|
||||
property var eventsByDate: ({})
|
||||
property bool isLoading: false
|
||||
property string lastError: ""
|
||||
property date lastStartDate
|
||||
property date lastEndDate
|
||||
property string dateFormat: "MM/dd/yyyy"
|
||||
|
||||
function checkAvailability() {
|
||||
if (!formatProcess.running)
|
||||
formatProcess.running = true;
|
||||
}
|
||||
|
||||
function loadCurrentMonth() {
|
||||
let today = new Date();
|
||||
let firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
|
||||
let lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
|
||||
let startDate = new Date(firstDay);
|
||||
startDate.setDate(startDate.getDate() - firstDay.getDay() - 7);
|
||||
let endDate = new Date(lastDay);
|
||||
endDate.setDate(endDate.getDate() + (6 - lastDay.getDay()) + 7);
|
||||
loadEvents(startDate, endDate);
|
||||
}
|
||||
|
||||
function loadEvents(startDate, endDate) {
|
||||
if (!installed)
|
||||
return;
|
||||
if (eventsProcess.running)
|
||||
return;
|
||||
root.lastStartDate = startDate;
|
||||
root.lastEndDate = endDate;
|
||||
root.isLoading = true;
|
||||
let startDateStr = Qt.formatDate(startDate, root.dateFormat);
|
||||
let endDateStr = Qt.formatDate(endDate, root.dateFormat);
|
||||
eventsProcess.requestStartDate = startDate;
|
||||
eventsProcess.requestEndDate = endDate;
|
||||
eventsProcess.command = ["khal", "list", "--json", "title", "--json", "description", "--json", "start-date", "--json", "start-time", "--json", "end-date", "--json", "end-time", "--json", "all-day", "--json", "location", "--json", "url", startDateStr, endDateStr];
|
||||
eventsProcess.running = true;
|
||||
}
|
||||
|
||||
function _parseDateFormat(formatExample) {
|
||||
return formatExample.replace("12", "MM").replace("21", "dd").replace("2013", "yyyy");
|
||||
}
|
||||
|
||||
Component.onCompleted: checkAvailability()
|
||||
|
||||
Process {
|
||||
id: formatProcess
|
||||
|
||||
command: ["khal", "printformats"]
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
if (exitCode !== 0)
|
||||
checkProcess.running = true;
|
||||
}
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
let lines = text.split('\n');
|
||||
for (let line of lines) {
|
||||
if (!line.startsWith('dateformat:'))
|
||||
continue;
|
||||
let formatExample = line.substring(line.indexOf(':') + 1).trim();
|
||||
root.dateFormat = root._parseDateFormat(formatExample);
|
||||
break;
|
||||
}
|
||||
checkProcess.running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: checkProcess
|
||||
|
||||
command: ["khal", "list", "today"]
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
root.installed = (exitCode === 0);
|
||||
if (root.installed)
|
||||
root.loadCurrentMonth();
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: eventsProcess
|
||||
|
||||
property date requestStartDate
|
||||
property date requestEndDate
|
||||
property string rawOutput: ""
|
||||
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
root.isLoading = false;
|
||||
if (exitCode !== 0) {
|
||||
root.lastError = "Failed to load events (exit code: " + exitCode + ")";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let newEventsByDate = {};
|
||||
let lines = eventsProcess.rawOutput.split('\n');
|
||||
for (let line of lines) {
|
||||
line = line.trim();
|
||||
if (!line || line === "[]")
|
||||
continue;
|
||||
|
||||
let dayEvents = JSON.parse(line);
|
||||
for (let event of dayEvents) {
|
||||
if (!event.title)
|
||||
continue;
|
||||
|
||||
let startDate, endDate;
|
||||
if (event['start-date'])
|
||||
startDate = Date.fromLocaleString(I18n.locale(), event['start-date'], root.dateFormat);
|
||||
else
|
||||
startDate = new Date();
|
||||
if (event['end-date'])
|
||||
endDate = Date.fromLocaleString(I18n.locale(), event['end-date'], root.dateFormat);
|
||||
else
|
||||
endDate = new Date(startDate);
|
||||
|
||||
let startTime = new Date(startDate);
|
||||
let endTime = new Date(endDate);
|
||||
if (event['start-time'] && event['all-day'] !== "True") {
|
||||
let timeStr = event['start-time'];
|
||||
if (timeStr) {
|
||||
let timeParts = timeStr.match(/(\d+):(\d+)(?::\d+)?\s*(AM|PM)?/i);
|
||||
if (timeParts) {
|
||||
let hours = parseInt(timeParts[1]);
|
||||
let minutes = parseInt(timeParts[2]);
|
||||
if (timeParts[3]) {
|
||||
let period = timeParts[3].toUpperCase();
|
||||
if (period === 'PM' && hours !== 12)
|
||||
hours += 12;
|
||||
else if (period === 'AM' && hours === 12)
|
||||
hours = 0;
|
||||
}
|
||||
startTime.setHours(hours, minutes);
|
||||
if (event['end-time']) {
|
||||
let endTimeParts = event['end-time'].match(/(\d+):(\d+)(?::\d+)?\s*(AM|PM)?/i);
|
||||
if (endTimeParts) {
|
||||
let endHours = parseInt(endTimeParts[1]);
|
||||
let endMinutes = parseInt(endTimeParts[2]);
|
||||
if (endTimeParts[3]) {
|
||||
let endPeriod = endTimeParts[3].toUpperCase();
|
||||
if (endPeriod === 'PM' && endHours !== 12)
|
||||
endHours += 12;
|
||||
else if (endPeriod === 'AM' && endHours === 12)
|
||||
endHours = 0;
|
||||
}
|
||||
endTime.setHours(endHours, endMinutes);
|
||||
}
|
||||
} else {
|
||||
endTime = new Date(startTime);
|
||||
endTime.setHours(startTime.getHours() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let eventId = event.title + "_" + event['start-date'] + "_" + (event['start-time'] || 'allday');
|
||||
let extractedUrl = "";
|
||||
if (!event.url && event.description) {
|
||||
let urlMatch = event.description.match(/https?:\/\/[^\s]+/);
|
||||
if (urlMatch)
|
||||
extractedUrl = urlMatch[0];
|
||||
}
|
||||
let eventTemplate = {
|
||||
"id": eventId,
|
||||
"title": event.title || "Untitled Event",
|
||||
"start": startTime,
|
||||
"end": endTime,
|
||||
"location": event.location || "",
|
||||
"description": event.description || "",
|
||||
"url": event.url || extractedUrl,
|
||||
"calendar": "",
|
||||
"color": "",
|
||||
"allDay": event['all-day'] === "True",
|
||||
"isMultiDay": startDate.toDateString() !== endDate.toDateString()
|
||||
};
|
||||
let currentDate = new Date(startDate);
|
||||
while (currentDate <= endDate) {
|
||||
let dateKey = Qt.formatDate(currentDate, "yyyy-MM-dd");
|
||||
if (!newEventsByDate[dateKey])
|
||||
newEventsByDate[dateKey] = [];
|
||||
|
||||
let existingEvent = newEventsByDate[dateKey].find(e => e.id === eventId);
|
||||
if (existingEvent) {
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
continue;
|
||||
}
|
||||
let dayEvent = Object.assign({}, eventTemplate);
|
||||
if (currentDate.getTime() === startDate.getTime()) {
|
||||
dayEvent.start = new Date(startTime);
|
||||
} else {
|
||||
dayEvent.start = new Date(currentDate);
|
||||
if (!dayEvent.allDay)
|
||||
dayEvent.start.setHours(0, 0, 0, 0);
|
||||
}
|
||||
if (currentDate.getTime() === endDate.getTime()) {
|
||||
dayEvent.end = new Date(endTime);
|
||||
} else {
|
||||
dayEvent.end = new Date(currentDate);
|
||||
if (!dayEvent.allDay)
|
||||
dayEvent.end.setHours(23, 59, 59, 999);
|
||||
}
|
||||
newEventsByDate[dateKey].push(dayEvent);
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
root.eventsByDate = newEventsByDate;
|
||||
root.lastError = "";
|
||||
} catch (error) {
|
||||
root.lastError = "Failed to parse events JSON: " + error.toString();
|
||||
root.eventsByDate = {};
|
||||
}
|
||||
eventsProcess.rawOutput = "";
|
||||
}
|
||||
|
||||
stdout: SplitParser {
|
||||
splitMarker: "\n"
|
||||
onRead: data => {
|
||||
eventsProcess.rawOutput += data + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,71 +11,87 @@ Singleton {
|
||||
id: root
|
||||
readonly property var log: Log.scoped("CalendarService")
|
||||
|
||||
property bool khalAvailable: true // Always true to enable DMS calendar card UI
|
||||
property bool khalInstalled: false // Tracks if khal is actually on the system
|
||||
readonly property string backendPref: SettingsData.calendarBackend
|
||||
readonly property string activeBackend: {
|
||||
switch (backendPref) {
|
||||
case "khal":
|
||||
return "khal";
|
||||
case "dankcal":
|
||||
return "dankcal";
|
||||
default:
|
||||
if (dankBackend.connected)
|
||||
return "dankcal";
|
||||
if (khalBackend.installed)
|
||||
return "khal";
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
readonly property bool calendarAvailable: activeBackend !== "none"
|
||||
readonly property bool isDankActive: activeBackend === "dankcal"
|
||||
readonly property bool canCreateEvents: isDankActive && dankBackend.connected
|
||||
property bool khalAvailable: true // compatibility alias - calendar card UI gate
|
||||
|
||||
readonly property bool dankConnected: dankBackend.connected
|
||||
readonly property bool dankBinaryExists: dankBackend.binaryExists
|
||||
readonly property bool dankNeedsLaunch: backendPref === "dankcal" && !dankBackend.connected
|
||||
|
||||
property var calendars: dankBackend.calendars
|
||||
property var eventsByDate: ({})
|
||||
property var khalEventsByDate: ({})
|
||||
property var taskEventsByDate: ({})
|
||||
property var localTasks: ({})
|
||||
property bool isLoading: false
|
||||
property bool isLoading: khalBackend.isLoading
|
||||
property string lastError: ""
|
||||
|
||||
property bool _rangeSet: false
|
||||
property date lastStartDate
|
||||
property date lastEndDate
|
||||
property string khalDateFormat: "MM/dd/yyyy"
|
||||
|
||||
onKhalEventsByDateChanged: mergeEvents()
|
||||
onTaskEventsByDateChanged: mergeEvents()
|
||||
|
||||
function checkKhalAvailability() {
|
||||
if (!khalCheckProcess.running)
|
||||
khalCheckProcess.running = true;
|
||||
onActiveBackendChanged: {
|
||||
mergeEvents();
|
||||
if (_rangeSet)
|
||||
loadEvents(lastStartDate, lastEndDate);
|
||||
}
|
||||
|
||||
function detectKhalDateFormat() {
|
||||
if (!khalFormatProcess.running)
|
||||
khalFormatProcess.running = true;
|
||||
CalendarKhalBackend {
|
||||
id: khalBackend
|
||||
onEventsByDateChanged: root.mergeEvents()
|
||||
}
|
||||
|
||||
function parseKhalDateFormat(formatExample) {
|
||||
let qtFormat = formatExample.replace("12", "MM").replace("21", "dd").replace("2013", "yyyy");
|
||||
return {
|
||||
format: qtFormat,
|
||||
parser: null
|
||||
};
|
||||
}
|
||||
|
||||
function loadCurrentMonth() {
|
||||
if (!root.khalAvailable)
|
||||
return;
|
||||
let today = new Date();
|
||||
let firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
|
||||
let lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
|
||||
// Add padding
|
||||
let startDate = new Date(firstDay);
|
||||
startDate.setDate(startDate.getDate() - firstDay.getDay() - 7);
|
||||
let endDate = new Date(lastDay);
|
||||
endDate.setDate(endDate.getDate() + (6 - lastDay.getDay()) + 7);
|
||||
loadEvents(startDate, endDate);
|
||||
CalendarDankBackend {
|
||||
id: dankBackend
|
||||
enabled: root.backendPref === "dankcal" || root.backendPref === "auto"
|
||||
onEventsByDateChanged: root.mergeEvents()
|
||||
onConnectedChanged: {
|
||||
if (connected && root._rangeSet)
|
||||
root.loadEvents(root.lastStartDate, root.lastEndDate);
|
||||
}
|
||||
}
|
||||
|
||||
function loadEvents(startDate, endDate) {
|
||||
if (!root.khalInstalled) {
|
||||
return;
|
||||
}
|
||||
if (eventsProcess.running) {
|
||||
return;
|
||||
}
|
||||
// Store last requested date range for refresh timer
|
||||
root.lastStartDate = startDate;
|
||||
root.lastEndDate = endDate;
|
||||
root.isLoading = true;
|
||||
// Format dates for khal using detected format
|
||||
let startDateStr = Qt.formatDate(startDate, root.khalDateFormat);
|
||||
let endDateStr = Qt.formatDate(endDate, root.khalDateFormat);
|
||||
eventsProcess.requestStartDate = startDate;
|
||||
eventsProcess.requestEndDate = endDate;
|
||||
eventsProcess.command = ["khal", "list", "--json", "title", "--json", "description", "--json", "start-date", "--json", "start-time", "--json", "end-date", "--json", "end-time", "--json", "all-day", "--json", "location", "--json", "url", startDateStr, endDateStr];
|
||||
eventsProcess.running = true;
|
||||
root._rangeSet = true;
|
||||
switch (activeBackend) {
|
||||
case "dankcal":
|
||||
dankBackend.loadEvents(startDate, endDate);
|
||||
break;
|
||||
case "khal":
|
||||
khalBackend.loadEvents(startDate, endDate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function _activeBackendEventsByDate() {
|
||||
switch (activeBackend) {
|
||||
case "dankcal":
|
||||
return dankBackend.eventsByDate;
|
||||
case "khal":
|
||||
return khalBackend.eventsByDate;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function getEventsForDate(date) {
|
||||
@@ -84,11 +100,54 @@ Singleton {
|
||||
}
|
||||
|
||||
function hasEventsForDate(date) {
|
||||
let events = getEventsForDate(date);
|
||||
return events.length > 0;
|
||||
return getEventsForDate(date).length > 0;
|
||||
}
|
||||
|
||||
function writableCalendars() {
|
||||
return isDankActive ? dankBackend.writableCalendars() : [];
|
||||
}
|
||||
|
||||
function defaultCalendar() {
|
||||
return isDankActive ? dankBackend.defaultCalendar() : null;
|
||||
}
|
||||
|
||||
function launchDankCalendar() {
|
||||
dankBackend.launch();
|
||||
}
|
||||
|
||||
function createEvent(fields, callback) {
|
||||
if (isDankActive) {
|
||||
dankBackend.createEvent(fields, callback);
|
||||
return;
|
||||
}
|
||||
if (callback)
|
||||
callback({
|
||||
"error": "read-only backend"
|
||||
});
|
||||
}
|
||||
|
||||
function updateEvent(id, fields, callback) {
|
||||
if (isDankActive) {
|
||||
dankBackend.updateEvent(id, fields, callback);
|
||||
return;
|
||||
}
|
||||
if (callback)
|
||||
callback({
|
||||
"error": "read-only backend"
|
||||
});
|
||||
}
|
||||
|
||||
function deleteEvent(id, callback) {
|
||||
if (isDankActive) {
|
||||
dankBackend.deleteEvent(id, callback);
|
||||
return;
|
||||
}
|
||||
if (callback)
|
||||
callback({
|
||||
"error": "read-only backend"
|
||||
});
|
||||
}
|
||||
|
||||
// In-memory Task CRUD methods
|
||||
function loadTasks(text) {
|
||||
if (!text || text.trim() === "") {
|
||||
root.localTasks = {};
|
||||
@@ -129,8 +188,7 @@ Singleton {
|
||||
"description": "Task from your Planner",
|
||||
"url": "",
|
||||
"calendar": "Todo Planner",
|
||||
"color": "#10B981" // Pastel Green
|
||||
,
|
||||
"color": "#10B981",
|
||||
"allDay": true,
|
||||
"isMultiDay": false
|
||||
});
|
||||
@@ -142,9 +200,8 @@ Singleton {
|
||||
function addTaskForDate(date, text) {
|
||||
let dateKey = Qt.formatDate(date, "yyyy-MM-dd");
|
||||
let tasks = Object.assign({}, root.localTasks);
|
||||
if (!tasks[dateKey]) {
|
||||
if (!tasks[dateKey])
|
||||
tasks[dateKey] = [];
|
||||
}
|
||||
let taskId = (new Date().getTime()) + "-dms";
|
||||
tasks[dateKey].push({
|
||||
"id": taskId,
|
||||
@@ -187,11 +244,10 @@ Singleton {
|
||||
let list = tasks[dateKey];
|
||||
let filtered = list.filter(item => item.id !== cleanId);
|
||||
if (filtered.length !== list.length) {
|
||||
if (filtered.length === 0) {
|
||||
if (filtered.length === 0)
|
||||
delete tasks[dateKey];
|
||||
} else {
|
||||
else
|
||||
tasks[dateKey] = filtered;
|
||||
}
|
||||
updated = true;
|
||||
break;
|
||||
}
|
||||
@@ -208,20 +264,17 @@ Singleton {
|
||||
let tasks = Object.assign({}, root.localTasks);
|
||||
let v = tasks[dateKey] || [];
|
||||
let idToItem = {};
|
||||
for (let item of v) {
|
||||
for (let item of v)
|
||||
idToItem[item.id] = item;
|
||||
}
|
||||
let newV = [];
|
||||
for (let tid of orderedIds) {
|
||||
if (idToItem[tid]) {
|
||||
if (idToItem[tid])
|
||||
newV.push(idToItem[tid]);
|
||||
}
|
||||
}
|
||||
let orderedSet = new Set(orderedIds);
|
||||
for (let item of v) {
|
||||
if (!orderedSet.has(item.id)) {
|
||||
if (!orderedSet.has(item.id))
|
||||
newV.push(item);
|
||||
}
|
||||
}
|
||||
tasks[dateKey] = newV;
|
||||
root.localTasks = tasks;
|
||||
@@ -254,30 +307,24 @@ Singleton {
|
||||
|
||||
function mergeEvents() {
|
||||
let merged = {};
|
||||
let backendEvents = _activeBackendEventsByDate();
|
||||
|
||||
// Merge khal events
|
||||
for (let dateKey in root.khalEventsByDate) {
|
||||
merged[dateKey] = [].concat(root.khalEventsByDate[dateKey]);
|
||||
}
|
||||
for (let dateKey in backendEvents)
|
||||
merged[dateKey] = [].concat(backendEvents[dateKey]);
|
||||
|
||||
// Merge task events
|
||||
for (let dateKey in root.taskEventsByDate) {
|
||||
if (!merged[dateKey]) {
|
||||
if (!merged[dateKey])
|
||||
merged[dateKey] = [];
|
||||
}
|
||||
for (let event of root.taskEventsByDate[dateKey]) {
|
||||
if (!merged[dateKey].some(e => e.id === event.id)) {
|
||||
if (!merged[dateKey].some(e => e.id === event.id))
|
||||
merged[dateKey].push(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort events within each date
|
||||
for (let dateKey in merged) {
|
||||
let list = merged[dateKey];
|
||||
for (let idx = 0; idx < list.length; idx++) {
|
||||
for (let idx = 0; idx < list.length; idx++)
|
||||
list[idx]._origIdx = idx;
|
||||
}
|
||||
list.sort((a, b) => {
|
||||
let diff = a.start.getTime() - b.start.getTime();
|
||||
if (diff !== 0)
|
||||
@@ -289,12 +336,6 @@ Singleton {
|
||||
root.eventsByDate = merged;
|
||||
}
|
||||
|
||||
// Initialize on component completion
|
||||
Component.onCompleted: {
|
||||
detectKhalDateFormat();
|
||||
}
|
||||
|
||||
// Atomic file view for tasks
|
||||
FileView {
|
||||
id: tasksFileView
|
||||
path: Quickshell.env("HOME") + "/.config/niri-calendar-todo/tasks.json"
|
||||
@@ -304,233 +345,11 @@ Singleton {
|
||||
watchChanges: true
|
||||
printErrors: false
|
||||
|
||||
onLoaded: {
|
||||
loadTasks(tasksFileView.text());
|
||||
}
|
||||
onLoaded: loadTasks(tasksFileView.text())
|
||||
|
||||
onLoadFailed: {
|
||||
root.localTasks = {};
|
||||
root.taskEventsByDate = {};
|
||||
}
|
||||
}
|
||||
|
||||
// Process for detecting khal date format
|
||||
Process {
|
||||
id: khalFormatProcess
|
||||
|
||||
command: ["khal", "printformats"]
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
if (exitCode !== 0) {
|
||||
checkKhalAvailability();
|
||||
}
|
||||
}
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
let lines = text.split('\n');
|
||||
for (let line of lines) {
|
||||
if (line.startsWith('dateformat:')) {
|
||||
let formatExample = line.substring(line.indexOf(':') + 1).trim();
|
||||
let formatInfo = parseKhalDateFormat(formatExample);
|
||||
root.khalDateFormat = formatInfo.format;
|
||||
break;
|
||||
}
|
||||
}
|
||||
checkKhalAvailability();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process for checking khal configuration
|
||||
Process {
|
||||
id: khalCheckProcess
|
||||
|
||||
command: ["khal", "list", "today"]
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
root.khalInstalled = (exitCode === 0);
|
||||
if (root.khalInstalled) {
|
||||
loadCurrentMonth();
|
||||
} else {
|
||||
loadEvents(root.lastStartDate || new Date(), root.lastEndDate || new Date());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process for loading events
|
||||
Process {
|
||||
id: eventsProcess
|
||||
|
||||
property date requestStartDate
|
||||
property date requestEndDate
|
||||
property string rawOutput: ""
|
||||
|
||||
running: false
|
||||
onExited: exitCode => {
|
||||
root.isLoading = false;
|
||||
if (exitCode !== 0) {
|
||||
root.lastError = "Failed to load events (exit code: " + exitCode + ")";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let newEventsByDate = {};
|
||||
let lines = eventsProcess.rawOutput.split('\n');
|
||||
for (let line of lines) {
|
||||
line = line.trim();
|
||||
if (!line || line === "[]")
|
||||
continue;
|
||||
|
||||
// Parse JSON line
|
||||
let dayEvents = JSON.parse(line);
|
||||
// Process each event in this day's array
|
||||
for (let event of dayEvents) {
|
||||
if (!event.title)
|
||||
continue;
|
||||
|
||||
// Parse start and end dates using detected format
|
||||
let startDate, endDate;
|
||||
if (event['start-date']) {
|
||||
startDate = Date.fromLocaleString(I18n.locale(), event['start-date'], root.khalDateFormat);
|
||||
} else {
|
||||
startDate = new Date();
|
||||
}
|
||||
if (event['end-date']) {
|
||||
endDate = Date.fromLocaleString(I18n.locale(), event['end-date'], root.khalDateFormat);
|
||||
} else {
|
||||
endDate = new Date(startDate);
|
||||
}
|
||||
// Create start/end times
|
||||
let startTime = new Date(startDate);
|
||||
let endTime = new Date(endDate);
|
||||
if (event['start-time'] && event['all-day'] !== "True") {
|
||||
// Parse time if available and not all-day
|
||||
let timeStr = event['start-time'];
|
||||
if (timeStr) {
|
||||
// Match time with optional seconds and AM/PM
|
||||
let timeParts = timeStr.match(/(\d+):(\d+)(?::\d+)?\s*(AM|PM)?/i);
|
||||
if (timeParts) {
|
||||
let hours = parseInt(timeParts[1]);
|
||||
let minutes = parseInt(timeParts[2]);
|
||||
|
||||
// Handle AM/PM conversion if present
|
||||
if (timeParts[3]) {
|
||||
let period = timeParts[3].toUpperCase();
|
||||
if (period === 'PM' && hours !== 12) {
|
||||
hours += 12;
|
||||
} else if (period === 'AM' && hours === 12) {
|
||||
hours = 0;
|
||||
}
|
||||
}
|
||||
|
||||
startTime.setHours(hours, minutes);
|
||||
if (event['end-time']) {
|
||||
let endTimeParts = event['end-time'].match(/(\d+):(\d+)(?::\d+)?\s*(AM|PM)?/i);
|
||||
if (endTimeParts) {
|
||||
let endHours = parseInt(endTimeParts[1]);
|
||||
let endMinutes = parseInt(endTimeParts[2]);
|
||||
|
||||
// Handle AM/PM conversion if present
|
||||
if (endTimeParts[3]) {
|
||||
let endPeriod = endTimeParts[3].toUpperCase();
|
||||
if (endPeriod === 'PM' && endHours !== 12) {
|
||||
endHours += 12;
|
||||
} else if (endPeriod === 'AM' && endHours === 12) {
|
||||
endHours = 0;
|
||||
}
|
||||
}
|
||||
|
||||
endTime.setHours(endHours, endMinutes);
|
||||
}
|
||||
} else {
|
||||
// Default to 1 hour duration on same day
|
||||
endTime = new Date(startTime);
|
||||
endTime.setHours(startTime.getHours() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Create unique ID for this event (to track multi-day events)
|
||||
let eventId = event.title + "_" + event['start-date'] + "_" + (event['start-time'] || 'allday');
|
||||
// Create event object template
|
||||
let extractedUrl = "";
|
||||
if (!event.url && event.description) {
|
||||
let urlMatch = event.description.match(/https?:\/\/[^\s]+/);
|
||||
if (urlMatch) {
|
||||
extractedUrl = urlMatch[0];
|
||||
}
|
||||
}
|
||||
let eventTemplate = {
|
||||
"id": eventId,
|
||||
"title": event.title || "Untitled Event",
|
||||
"start": startTime,
|
||||
"end": endTime,
|
||||
"location": event.location || "",
|
||||
"description": event.description || "",
|
||||
"url": event.url || extractedUrl,
|
||||
"calendar": "",
|
||||
"color": "",
|
||||
"allDay": event['all-day'] === "True",
|
||||
"isMultiDay": startDate.toDateString() !== endDate.toDateString()
|
||||
};
|
||||
// Add event to each day it spans
|
||||
let currentDate = new Date(startDate);
|
||||
while (currentDate <= endDate) {
|
||||
let dateKey = Qt.formatDate(currentDate, "yyyy-MM-dd");
|
||||
if (!newEventsByDate[dateKey])
|
||||
newEventsByDate[dateKey] = [];
|
||||
|
||||
// Check if this exact event is already added to this date (prevent duplicates)
|
||||
let existingEvent = newEventsByDate[dateKey].find(e => {
|
||||
return e.id === eventId;
|
||||
});
|
||||
if (existingEvent) {
|
||||
// Move to next day without adding duplicate
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
continue;
|
||||
}
|
||||
// Create a copy of the event for this date
|
||||
let dayEvent = Object.assign({}, eventTemplate);
|
||||
// For multi-day events, adjust the display time for this specific day
|
||||
if (currentDate.getTime() === startDate.getTime()) {
|
||||
// First day - use original start time
|
||||
dayEvent.start = new Date(startTime);
|
||||
} else {
|
||||
// Subsequent days - start at beginning of day for all-day events
|
||||
dayEvent.start = new Date(currentDate);
|
||||
if (!dayEvent.allDay)
|
||||
dayEvent.start.setHours(0, 0, 0, 0);
|
||||
}
|
||||
if (currentDate.getTime() === endDate.getTime()) {
|
||||
// Last day - use original end time
|
||||
dayEvent.end = new Date(endTime);
|
||||
} else {
|
||||
// Earlier days - end at end of day for all-day events
|
||||
dayEvent.end = new Date(currentDate);
|
||||
if (!dayEvent.allDay)
|
||||
dayEvent.end.setHours(23, 59, 59, 999);
|
||||
}
|
||||
newEventsByDate[dateKey].push(dayEvent);
|
||||
// Move to next day
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
root.khalEventsByDate = newEventsByDate;
|
||||
root.lastError = "";
|
||||
} catch (error) {
|
||||
root.lastError = "Failed to parse events JSON: " + error.toString();
|
||||
root.khalEventsByDate = {};
|
||||
}
|
||||
// Reset for next run
|
||||
eventsProcess.rawOutput = "";
|
||||
}
|
||||
|
||||
stdout: SplitParser {
|
||||
splitMarker: "\n"
|
||||
onRead: data => {
|
||||
eventsProcess.rawOutput += data + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user