diff --git a/core/Services/CalDAV/CalDAVClient.vala b/core/Services/CalDAV/CalDAVClient.vala index 7ff90660a..afa6b56db 100644 --- a/core/Services/CalDAV/CalDAVClient.vala +++ b/core/Services/CalDAV/CalDAVClient.vala @@ -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; } @@ -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; diff --git a/core/Services/CalDAV/Core.vala b/core/Services/CalDAV/Core.vala index ff2d25887..2c52beff4 100644 --- a/core/Services/CalDAV/Core.vala +++ b/core/Services/CalDAV/Core.vala @@ -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 (); } } diff --git a/core/Services/CalDAV/WebDAVClient.vala b/core/Services/CalDAV/WebDAVClient.vala index 2005c8319..380e7aa75 100644 --- a/core/Services/CalDAV/WebDAVClient.vala +++ b/core/Services/CalDAV/WebDAVClient.vala @@ -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 = ""; + } + + 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 (""); + if (last_close == -1) { + last_close = trimmed.last_index_of (""); + } + + if (last_close > 0) { + var tag_len = trimmed.has_suffix ("") ? 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 () {