1 // CalDAV.cpp - CalDAV Connection Object.
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
23 size_t CalDAVReceive(char *ReceivedBuffer, size_t Size, size_t NewMemoryBytes, string *StringPointer)
26 string ReceivedBufferString = "";
27 ReceivedBufferString.append(ReceivedBuffer, NewMemoryBytes);
29 StringPointer->append(ReceivedBufferString);
31 return Size * NewMemoryBytes;
35 size_t CalDAVSend(char *SendBuffer, size_t Size, size_t NewMemoryBytes, void *DataStruct){
37 struct CalDAVSendData *UploadPtr = (struct CalDAVSendData *)DataStruct;
39 if (UploadPtr->sizeleft){
41 UploadPtr->sizeleft--;
44 CharSend = (*UploadPtr->readptr)[UploadPtr->seek];
46 *SendBuffer = CharSend;
60 // Setup the objects within the CalDAV connection
63 ConnectionHandle = curl_easy_init();
69 // Destory the objects within the CalDAV connection
72 curl_easy_cleanup(ConnectionHandle);
73 ConnectionHandle = nullptr;
77 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
79 // Check if ConnData is a nullptr, return if it is.
81 if (ConnData == nullptr){
85 // Set the connection settings to the values from ConnData.
87 ConnectionData = (*ConnData);
91 CalDAVStatus CalDAV::GetConnectionData(){
93 // Get the current connection settings for the CalDAV
94 // connection object and return a CalDAVStatus object.
96 CalDAVStatus ConnectionStatus;
98 ConnectionStatus.Hostname = ConnectionData.Hostname;
99 ConnectionStatus.Port = ConnectionData.Port;
100 ConnectionStatus.Username = ConnectionData.Username;
101 ConnectionStatus.Prefix = ConnectionData.Prefix;
102 ConnectionStatus.UseSSL = ConnectionData.UseSSL;
103 ConnectionStatus.Timeout = ConnectionData.Timeout;
105 return ConnectionStatus;
109 CalDAVServerResult CalDAV::Connect(){
111 CalDAVServerResult ServerResult;
113 string ServerAddress = "";
114 string ServerUserPass = "";
116 // Setup the server address.
118 ServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
120 // Setup the server password.
122 ServerUserPass += ConnectionData.Username;
123 ServerUserPass += ":";
124 ServerUserPass += ConnectionData.Password;
126 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, ServerAddress.c_str());
127 curl_easy_setopt(ConnectionHandle, CURLOPT_USERPWD, ServerUserPass.c_str());
128 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
129 curl_easy_setopt(ConnectionHandle, CURLOPT_FAILONERROR, 1L);
130 curl_easy_setopt(ConnectionHandle, CURLOPT_TIMEOUT, ConnectionData.Timeout);
131 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEFUNCTION, CalDAVReceive);
132 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEDATA, &ServerData);
133 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEHEADER, &ServerHeader);
135 // Connect to the CalDAV server.
137 ServerResult.Code = curl_easy_perform(ConnectionHandle);
139 // Process the result received from the server.
141 if (ServerResult.Code != CURLE_OK){
143 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
147 ServerResult.Result = CALDAVQUERYRESULT_OK;
151 // Get the HTTP code.
153 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
159 CalDAVServerResult CalDAV::GetServerResult(){
161 return ConnectionServerResult;
165 CalDAVServerSupport CalDAV::GetServerSupport(){
167 CalDAVServerSupport ServerStatus;
169 // Setup the server connection.
171 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "OPTIONS");
173 CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
177 if (ServerResult == CURLE_OK){
178 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
180 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
182 ConnectionServerResult.Code = ServerResult;
183 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
185 if (ServerResult != CURLE_OK){
189 // Check that the server header has data in,
190 // otherwise return an "empty" CalDAVServerSupport.
192 if (ServerHeader.size() == 0){
196 // Process each line looking for the first DAV header
199 bool NewlineMode = true;
203 for (int CharSeek = 0; CharSeek < ServerHeader.size(); CharSeek++){
205 if (NewlineMode == true){
207 // Check if we have reached the end of the string.
209 if (CharSeek >= ServerHeader.size()){
215 // Check the first four letters to make sure
218 string DAVHeaderCheck = "";
221 DAVHeaderCheck = ServerHeader.substr(CharSeek, 4);
224 catch (out_of_range &oor){
228 if (DAVHeaderCheck == "DAV:"){
232 for (; CharSeek < ServerHeader.size(); CharSeek++){
234 if (ServerHeader[CharSeek] == '\n'){
240 DAVLine.push_back(ServerHeader[CharSeek]);
252 if (ServerHeader[CharSeek] == '\n'){
260 // Process the DAV line.
262 vector<string> DAVLineData;
263 string DAVSegmentString;
265 for (int CharSeek = 0; CharSeek < DAVLine.size(); CharSeek++){
267 if (DAVLine[CharSeek] == ' '){
271 if (DAVLine[CharSeek] == ','){
273 DAVLineData.push_back(DAVSegmentString);
274 DAVSegmentString.clear();
279 DAVSegmentString += DAVLine[CharSeek];
283 // Process the DAV values and set each value
284 // to true as required.
286 for (int DAVItemSeek = 0;
287 DAVItemSeek < DAVLineData.size();
290 if (DAVLineData.at(DAVItemSeek) == "calendar-access"){
292 ServerStatus.BasicSupport = true;
298 // Reset the connection status.
300 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
306 string CalDAV::GetUserPrincipal(){
308 string CurrentUserPrincipal = "";
309 string UserPrincipalRequest = "";
310 CalDAVSendData UserPrincipalSendData;
312 UserPrincipalRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
313 "<d:propfind xmlns:d=\"DAV:\">\n"
315 " <d:current-user-principal />\n"
319 UserPrincipalSendData.readptr = &UserPrincipalRequest;
320 UserPrincipalSendData.sizeleft = UserPrincipalRequest.size();
324 struct curl_slist *UserPrincipalRequestHeader = NULL;
326 UserPrincipalRequestHeader = curl_slist_append(UserPrincipalRequestHeader, "Depth: 0");
327 UserPrincipalRequestHeader = curl_slist_append(UserPrincipalRequestHeader, "Prefer: return-minimal");
328 UserPrincipalRequestHeader = curl_slist_append(UserPrincipalRequestHeader, "Content-Type: application/xml; charset=utf-8");
330 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, UserPrincipalRequestHeader);
332 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
333 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
334 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &UserPrincipalSendData);
335 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
340 ServerHeader.clear();
342 CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
346 if (ServerResult == CURLE_OK){
347 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
349 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
351 ConnectionServerResult.Code = ServerResult;
352 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
354 if (ServerResult != CURLE_OK){
356 return CurrentUserPrincipal;
360 // Process the User Principal from the ServerData.
362 CurrentUserPrincipal = ProcessXMLUserPrincipal();
364 // Reset the changed settings.
366 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
367 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
368 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
370 return CurrentUserPrincipal;
374 string CalDAV::GetCalendarHome(string UserPrincipalURI){
376 string CalendarHomeURI = "";
378 // Build the Calendar Home URL address.
380 string CalendarHomeURL = BuildServerAddress(&ConnectionData, UserPrincipalURI);
382 // Setup the header request.
384 CalDAVSendData CalendarHomeSendData;
386 string CalendarHomeRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
387 "<d:propfind xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
389 " <c:calendar-home-set />\n"
393 CalendarHomeSendData.readptr = &CalendarHomeRequest;
394 CalendarHomeSendData.sizeleft = CalendarHomeRequest.size();
398 struct curl_slist *CalendarRequestHeader = NULL;
400 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Depth: 0");
401 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Prefer: return-minimal");
402 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Content-Type: application/xml; charset=utf-8");
404 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
405 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarHomeURL.c_str());
406 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
407 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
408 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarHomeSendData);
409 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
414 ServerHeader.clear();
416 CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
420 if (ServerResult == CURLE_OK){
421 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
423 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
425 ConnectionServerResult.Code = ServerResult;
426 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
428 if (ServerResult != CURLE_OK){
430 return CalendarHomeURI;
434 // Process the User Principal from the ServerData.
436 CalendarHomeURI = ProcessXMLCalendarHome();
438 // Reset the changed settings.
440 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
441 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
443 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
444 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
445 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
446 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
448 return CalendarHomeURI;
452 CalDAVCalendarList CalDAV::GetCalendars(){
454 CalDAVCalendarList ServerList;
455 CalDAVSendData CalendarListSendData;
457 // Build the server address.
459 string UserPrincipalURI = "";
460 UserPrincipalURI = GetUserPrincipal();
462 if (UserPrincipalURI.size() == 0){
468 string CalendarHomeURI = "";
469 CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
471 string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURI);
473 string CalendarListRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
474 "<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\""
475 " xmlns:c=\"urn:ietf:params:xml:ns:caldav\" xmlns:x0=\"http://apple.com/ns/ical/\">\n"
477 " <d:resourcetype />\n"
478 " <d:displayname />\n"
479 " <d:sync-token />\n"
480 " <x0:calendar-color />\n"
481 " <x0:calendar-order />\n"
483 " <c:supported-calendar-component-set />\n"
484 " <c:calendar-description />\n"
488 CalendarListSendData.readptr = &CalendarListRequest;
489 CalendarListSendData.sizeleft = CalendarListRequest.size();
493 struct curl_slist *CalendarListRequestHeader = NULL;
495 CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Depth: 1");
496 CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Prefer: return-minimal");
497 CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Content-Type: application/xml; charset=utf-8");
499 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarListRequestHeader);
500 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str());
501 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
502 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
503 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarListSendData);
504 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
509 ServerHeader.clear();
511 CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
513 //ServerList = ProcessXMLCalendarList();
515 if (ServerResult == CURLE_OK){
516 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
518 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
520 ConnectionServerResult.Code = ServerResult;
521 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
523 if (ServerResult != CURLE_OK){
529 // Process the received XML data into a list of calendars
532 ServerList = ProcessXMLCalendarList();
534 // Restore the original settings.
536 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
538 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
539 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
540 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
541 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
542 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
548 CalDAVServerResult CalDAV::AddCalendar(string CalendarName){
550 CalDAVServerResult ServerResult;
551 CalDAVSendData CalendarAddSendData;
553 // Build the server address.
555 string UserPrincipalURI = "";
556 UserPrincipalURI = GetUserPrincipal();
558 if (UserPrincipalURI.size() == 0){
564 string CalendarHomeURI = "";
565 CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
567 // Generate the UUID.
569 string UUIDValue = GenerateUUID();
570 UUIDValue.erase(UUIDValue.end()-1);
572 string CalendarHomeURL = CalendarHomeURI;
573 CalendarHomeURL.append(UUIDValue);
574 CalendarHomeURL.append("/");
576 // Build the calendar list address.
578 string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURL);
580 string CalendarAddRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
581 "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
585 CalendarAddRequest += CalendarName;
586 CalendarAddRequest += "</d:displayname>\n"
587 " <c:supported-calendar-component-set>\n"
588 " <c:comp name=\"VTODO\"/>\n"
589 " <c:comp name=\"VEVENT\"/>\n"
590 " </c:supported-calendar-component-set>\n"
595 CalendarAddSendData.readptr = &CalendarAddRequest;
596 CalendarAddSendData.sizeleft = CalendarAddRequest.size();
600 struct curl_slist *CalendarRequestHeader = NULL;
602 //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
603 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str());
604 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "MKCALENDAR");
605 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
606 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarAddSendData);
607 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
612 ServerHeader.clear();
614 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
616 if (ServerConnectionResult == CURLE_OK){
617 ServerResult.Result = CALDAVQUERYRESULT_OK;
619 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
621 ServerResult.Code = ServerConnectionResult;
622 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
624 // Restore the original settings.
626 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
627 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
628 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
629 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
630 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
631 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
637 CalDAVServerResult CalDAV::EditCalendarProcess(string *CalendarHREF,
638 string *CalendarName,
639 Colour *CalendarColour,
640 string *CalendarDescription,
643 CalDAVServerResult ServerResult;
644 CalDAVSendData CalendarEditSendData;
646 // Build the server address.
648 string UserPrincipalURI = "";
649 UserPrincipalURI = GetUserPrincipal();
651 if (UserPrincipalURI.size() == 0){
657 string CalendarHomeURI = "";
658 CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
660 // Generate the UUID.
662 string UUIDValue = GenerateUUID();
663 UUIDValue.erase(UUIDValue.end()-1);
665 string CalendarHomeURL = CalendarHomeURI;
666 CalendarHomeURL.append(UUIDValue);
667 CalendarHomeURL.append("/");
669 // Build the calendar list address.
671 string CalendarEditURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF));
673 string CalendarEditRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
674 "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"\n"
675 " xmlns:x0=\"http://apple.com/ns/ical/\">\n"
679 // Update the calendar name.
681 if (CalendarName != nullptr){
683 CalendarEditRequest += "<d:displayname>";
684 CalendarEditRequest += (*CalendarName);
685 CalendarEditRequest += "</d:displayname>\n";
689 // Update the calendar colour.
691 if (CalendarColour != nullptr){
693 CalendarEditRequest += "<x0:calendar-color>";
694 CalendarEditRequest += (*CalendarColour);
695 CalendarEditRequest += "</x0:calendar-color>\n";
699 // Update the calendar description.
701 if (CalendarDescription != nullptr){
703 CalendarEditRequest += "<c:calendar-description>";
704 CalendarEditRequest += (*CalendarDescription);
705 CalendarEditRequest += "</c:calendar-description>\n";
709 // Update the calendar order.
711 if (CalendarOrder != nullptr){
713 CalendarEditRequest += "<x0:calendar-order>";
714 CalendarEditRequest += to_string((*CalendarOrder));
715 CalendarEditRequest += "</x0:calendar-order>\n";
719 CalendarEditRequest += " </d:prop>\n"
721 "</d:propertyupdate>";
723 CalendarEditSendData.readptr = &CalendarEditRequest;
724 CalendarEditSendData.sizeleft = CalendarEditRequest.size();
728 struct curl_slist *CalendarRequestHeader = NULL;
730 //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
731 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarEditURLAddress.c_str());
732 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
733 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
734 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarEditSendData);
735 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
740 ServerHeader.clear();
742 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
744 if (ServerConnectionResult == CURLE_OK){
745 ServerResult.Result = CALDAVQUERYRESULT_OK;
747 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
749 ServerResult.Code = ServerConnectionResult;
750 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
752 // Restore the original settings.
754 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
755 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
756 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
757 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
758 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
759 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
765 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
766 string *CalendarName,
767 Colour *CalendarColour,
768 string *CalendarDescription,
771 CalDAVServerResult ServerResult;
773 ServerResult = EditCalendarProcess(CalendarHREF,
783 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
784 Colour *CalendarColour){
787 CalDAVServerResult ServerResult;
789 ServerResult = EditCalendarProcess(CalendarHREF,
799 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
800 string *CalendarName){
802 CalDAVServerResult ServerResult;
804 ServerResult = EditCalendarProcess(CalendarHREF,
814 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
817 CalDAVServerResult ServerResult;
819 ServerResult = EditCalendarProcess(CalendarHREF,
829 CalDAVServerResult CalDAV::EditCalendarDescription(string *CalendarHREF,
830 string *CalendarDescription){
832 CalDAVServerResult ServerResult;
834 ServerResult = EditCalendarProcess(CalendarHREF,
844 CalDAVServerResult CalDAV::DeleteCalendar(string *CalendarHREF){
846 CalDAVServerResult ServerResult;
848 // Build the server address.
850 string UserPrincipalURI = "";
851 UserPrincipalURI = GetUserPrincipal();
853 if (UserPrincipalURI.size() == 0){
859 string CalendarHomeURI = "";
860 CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
862 // Generate the UUID.
864 string UUIDValue = GenerateUUID();
865 UUIDValue.erase(UUIDValue.end()-1);
867 string CalendarHomeURL = CalendarHomeURI;
868 CalendarHomeURL.append(UUIDValue);
869 CalendarHomeURL.append("/");
871 // Build the calendar list address.
873 struct curl_slist *DeleteRequestHeader = NULL;
875 DeleteRequestHeader = curl_slist_append(DeleteRequestHeader, "Depth: infinity");
877 string CalendarDeleteURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF));
879 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, DeleteRequestHeader);
880 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarDeleteURLAddress.c_str());
881 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "DELETE");
883 // Delete the calendar.
886 ServerHeader.clear();
888 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
890 if (ServerConnectionResult == CURLE_OK){
891 ServerResult.Result = CALDAVQUERYRESULT_OK;
893 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
895 ServerResult.Code = ServerConnectionResult;
896 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
898 // Restore the original settings.
900 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
901 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
902 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
903 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
904 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
905 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
906 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
912 CalDAVServerResult CalDAV::GetEntryETag(string *CalendarEntryHREF, string *ETagValue){
914 CalDAVServerResult ServerResult;
915 CalDAVSendData EntryETagGetData;
917 // Build the server address.
919 string UserPrincipalURI = "";
920 UserPrincipalURI = GetUserPrincipal();
922 if (UserPrincipalURI.size() == 0){
928 string CalendarHomeURI = "";
929 CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
931 // Split the path and filename.
934 string EntryFilename;
936 SplitPathFilename(CalendarEntryHREF, &EntryURIPath, &EntryFilename);
938 // Build the request for the server.
940 string EntryETagRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
941 "<c:calendar-multiget xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
946 EntryETagRequest += (*CalendarEntryHREF);
947 EntryETagRequest += "</d:href>\n"
948 "</c:calendar-multiget>";
950 EntryETagGetData.readptr = &EntryETagRequest;
951 EntryETagGetData.sizeleft = EntryETagRequest.size();
953 // Build the calendar list address.
955 struct curl_slist *GetETagRequestHeader = NULL;
957 GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Depth: 1");
958 GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Prefer: return-minimal");
959 GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Content-Type: application/xml; charset=utf-8");
961 string GetETagURLAddress = BuildServerAddress(&ConnectionData, EntryURIPath);
963 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, GetETagRequestHeader);
964 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, GetETagURLAddress.c_str());
965 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
966 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
967 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryETagGetData);
968 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
970 // Attempt to get the entity tag.
973 ServerHeader.clear();
975 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
977 if (ServerConnectionResult == CURLE_OK){
978 ServerResult.Result = CALDAVQUERYRESULT_OK;
980 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
982 ServerResult.Code = ServerConnectionResult;
983 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
985 if (ServerConnectionResult != CURLE_OK){
989 // Get the entity tag from the result.
991 *ETagValue = ProcessXMLEntryETag();
993 // Restore the original settings.
995 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
996 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
997 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
998 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
999 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1000 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1001 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1003 return ServerResult;
1007 CalDAVServerResult CalDAV::AddEntry(string *CalendarEntryHREF, string *EntryData){
1009 // Add an entry to the calendar collection.
1011 CalDAVServerResult ServerResult;
1012 CalDAVSendData EntryAddSendData;
1014 // Build the calendar list address.
1016 string EntryAddURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1018 EntryAddSendData.readptr = EntryData;
1019 EntryAddSendData.sizeleft = EntryData->size();
1021 struct curl_slist *CalendarRequestHeader = NULL;
1023 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Content-Type: text/calendar; charset=utf-8");
1025 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
1026 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryAddURLAddress.c_str());
1027 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
1028 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
1029 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryAddSendData);
1030 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
1032 // Process the data.
1035 ServerHeader.clear();
1037 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1039 if (ServerConnectionResult == CURLE_OK){
1040 ServerResult.Result = CALDAVQUERYRESULT_OK;
1042 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
1044 ServerResult.Code = ServerConnectionResult;
1045 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1047 // Restore the original settings.
1049 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1050 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1051 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
1052 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1053 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1054 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1055 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1057 return ServerResult;
1061 CalDAVServerResult CalDAV::EditEntry(string *CalendarEntryHREF, string *EntryData, string *EntryETag){
1063 // Edit an entry in the calendar collection.
1065 // Add an entry to the calendar collection.
1067 CalDAVServerResult ServerResult;
1068 CalDAVSendData EntryAddSendData;
1070 // Build the calendar list address.
1072 string EntryAddURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1074 EntryAddSendData.readptr = EntryData;
1075 EntryAddSendData.sizeleft = EntryData->size();
1077 string IfMatchHeader = "If-Match: \"";
1078 IfMatchHeader.append(*EntryETag);
1079 IfMatchHeader.append("\"");
1081 struct curl_slist *CalendarRequestHeader = NULL;
1083 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Content-Type: text/calendar; charset=utf-8");
1084 CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, IfMatchHeader.c_str());
1086 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
1087 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryAddURLAddress.c_str());
1088 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
1089 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
1090 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryAddSendData);
1091 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
1093 // Process the data.
1096 ServerHeader.clear();
1098 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1100 if (ServerConnectionResult == CURLE_OK){
1101 ServerResult.Result = CALDAVQUERYRESULT_OK;
1103 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
1105 ServerResult.Code = ServerConnectionResult;
1106 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1108 // Restore the original settings.
1110 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1111 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1112 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
1113 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1114 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1115 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1116 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1118 return ServerResult;
1122 CalDAVServerResult CalDAV::DeleteEntry(string *CalendarEntryHREF){
1124 // Delete an entry in the calendar collection.
1126 CalDAVServerResult ServerResult;
1128 // Build the calendar list address.
1130 string EntryDeleteURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1132 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1133 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryDeleteURLAddress.c_str());
1134 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "DELETE");
1136 // Delete the calendar.
1139 ServerHeader.clear();
1141 CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1143 if (ServerConnectionResult == CURLE_OK){
1144 ServerResult.Result = CALDAVQUERYRESULT_OK;
1146 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
1148 ServerResult.Code = ServerConnectionResult;
1149 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1151 // Restore the original settings.
1153 string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1154 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1155 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
1156 curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1157 curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1158 curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1159 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1161 return ServerResult;
1165 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
1167 // Check if the passed CalDAV Connection Data is has
1168 // an address set. Return false if nullptr is used.
1170 if (ConnData == nullptr){
1176 // Check the server hostname. Return false
1177 // if no value has been set.
1179 if (ConnData->Hostname.size() == 0){
1185 // Check the server port. Return false if
1186 // no value has been set or the port number
1187 // is less than 1 or higher than 65535.
1189 if (ConnData->Port < 1 || ConnData->Port > 65535){
1195 // Check the server username. Return false
1196 // if no value has been set.
1198 if (ConnData->Username.size() == 0){
1204 // Check the server password. Return false
1205 // if no value has been set.
1207 if (ConnData->Password.size() == 0){
1213 // Cannot check UseSSL: It is either true
1216 // Cannot check Prefix: The prefix may need
1217 // to be worked out first.
1219 // No errors were found whilst checking so
1226 string BuildServerAddress(CalDAVConnectionData *ConnData, string URIAddress){
1228 string ServerAddress;
1230 // Setup the server address.
1232 if (ConnData->UseSSL == true){
1233 ServerAddress += "https://";
1235 ServerAddress += "http://";
1238 ServerAddress += ConnData->Hostname;
1240 // Check if server port is 80, otherwise
1241 // specifiy the port number in the address.
1243 if (ConnData->Port != 80){
1244 ServerAddress += ":";
1245 ServerAddress += to_string(ConnData->Port);
1248 ServerAddress += URIAddress;
1250 return ServerAddress;