Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/Services/CalDAV/CalDAVClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ public class Services.CalDAV.CalDAVClient : Services.CalDAV.WebDAVClient {
}

public async void sync_tasklist (Objects.Project project, GLib.Cancellable cancellable) throws GLib.Error {
if (project.is_deck) {
debug ("[CalDAV] sync_tasklist called for: %s", project.name);

if (project.is_deck || project.is_archived) {
debug ("[CalDAV] Skipping project (is_deck: %s, is_archived: %s)", project.is_deck.to_string(), project.is_archived.to_string());
return;
}

Expand All @@ -362,14 +365,18 @@ public class Services.CalDAV.CalDAVClient : Services.CalDAV.WebDAVClient {
project.loading = true;
project.sync_started ();

debug ("[CalDAV] Fetching project details");
yield fetch_project_details (project, cancellable);

if (project.sync_id == "") {
debug ("[CalDAV] Project has no sync_id, skipping sync");
project.loading = false;
return;
}

debug ("[CalDAV] Sending sync-collection report");
var multi_status = yield report (project.calendar_url, xml, "1", cancellable);
debug ("[CalDAV] Received %d responses", multi_status.responses().size);

foreach (WebDAVResponse response in multi_status.responses ()) {
string? href = response.href;
Expand Down
15 changes: 13 additions & 2 deletions core/Services/CalDAV/Core.vala
Original file line number Diff line number Diff line change
Expand Up @@ -263,22 +263,33 @@ public class Services.CalDAV.Core : GLib.Object {


public async void sync (Objects.Source source) {
debug ("[CalDAV] Starting sync for source: %s (type: %s)", source.display_name, source.source_type.to_string());
debug ("[CalDAV] Server URL: %s", source.caldav_data.server_url);
debug ("[CalDAV] Username: %s", source.caldav_data.username);
debug ("[CalDAV] Password length: %d", source.caldav_data.password != null ? source.caldav_data.password.length : 0);

var caldav_client = get_client (source);

source.sync_started ();

try {
var cancellable = new GLib.Cancellable ();
debug ("[CalDAV] Syncing source metadata");
yield caldav_client.sync (source, cancellable);

foreach (Objects.Project project in Services.Store.instance ().get_projects_by_source (source.id)) {
var projects = Services.Store.instance ().get_projects_by_source (source.id);
debug ("[CalDAV] Found %d projects to sync", projects.size);

foreach (Objects.Project project in projects) {
debug ("[CalDAV] Syncing project: %s (url: %s, sync_id: %s)", project.name, project.calendar_url, project.sync_id);
yield caldav_client.sync_tasklist (project, cancellable);
}

source.sync_finished ();
source.last_sync = new GLib.DateTime.now_local ().to_string ();
debug ("[CalDAV] Sync completed successfully");
} catch (Error e) {
warning ("Failed to sync: %s", e.message);
warning ("[CalDAV] Sync failed: %s (code: %d)", e.message, e.code);
source.sync_failed ();
}
}
Expand Down
38 changes: 37 additions & 1 deletion core/Services/CalDAV/WebDAVClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,43 @@ public class Services.CalDAV.WebDAVMultiStatus : Object {

public WebDAVMultiStatus.from_string (string xml) throws GLib.Error {
this.xml_content = xml;
this.root = new GXml.XDocument.from_string (xml).document_element;
debug ("[CalDAV] Parsing XML, length: %d", xml != null ? xml.length : 0);

if (xml == null || xml.strip () == "") {
warning ("[CalDAV] Empty XML");
xml = "<?xml version=\"1.0\"?><multistatus xmlns=\"DAV:\"/>";
}

var cleaned = clean_xml (xml);

try {
this.root = new GXml.XDocument.from_string (cleaned).document_element;
} catch (Error e) {
warning ("[CalDAV] XML parse error: %s", e.message);
warning ("[CalDAV] First 1000 chars: %s", cleaned.substring(0, int.min(1000, cleaned.length)));
warning ("[CalDAV] Last 200 chars: %s", cleaned.substring(int.max(0, cleaned.length - 200)));
throw e;
}
}

private string clean_xml (string xml) {
var trimmed = xml.strip ();

var last_close = trimmed.last_index_of ("</d:multistatus>");
if (last_close == -1) {
last_close = trimmed.last_index_of ("</multistatus>");
}

if (last_close > 0) {
var tag_len = trimmed.has_suffix ("</d:multistatus>") ? 16 : 14;
var end_pos = last_close + tag_len;
if (end_pos < trimmed.length) {
debug ("[CalDAV] Removing %d extra bytes", trimmed.length - end_pos);
return trimmed.substring (0, end_pos);
}
}

return trimmed;
}

public void debug_print () {
Expand Down