Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code and unit tests for editing entries in the CalDAV object
[xestiacalendar/.git] / source / objects / CalDAV / CalDAV.cpp
1 // CalDAV.cpp - CalDAV Connection Object.
2 //
3 // (c) 2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
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.
10 //
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.
15 //
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/>
19 #include "CalDAV.h"
21 using namespace std;
23 size_t CalDAVReceive(char *ReceivedBuffer, size_t Size, size_t NewMemoryBytes, string *StringPointer)
24 {
25         
26         string ReceivedBufferString = "";
27         ReceivedBufferString.append(ReceivedBuffer, NewMemoryBytes);
28         
29         StringPointer->append(ReceivedBufferString);
30         
31         return Size * NewMemoryBytes;
32         
33 }
35 size_t CalDAVSend(char *SendBuffer, size_t Size, size_t NewMemoryBytes, void *DataStruct){
37         struct CalDAVSendData *UploadPtr = (struct CalDAVSendData *)DataStruct;
38         
39         if (UploadPtr->sizeleft){
41                 UploadPtr->sizeleft--;
42                 char CharSend;
44                 CharSend = (*UploadPtr->readptr)[UploadPtr->seek];
45                 
46                 *SendBuffer = CharSend;
47                 
48                 UploadPtr->seek++;
50                 return 1;
52         }
54         return 0;
56 }
58 CalDAV::CalDAV(){
60         // Setup the objects within the CalDAV connection 
61         // object.
62         
63         ConnectionHandle = curl_easy_init();
64         
65 }
67 CalDAV::~CalDAV(){
68         
69         // Destory the objects within the CalDAV connection
70         // object.
71         
72         curl_easy_cleanup(ConnectionHandle);
73         ConnectionHandle = nullptr;
74         
75 }
77 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
78         
79         // Check if ConnData is a nullptr, return if it is.
80         
81         if (ConnData == nullptr){
82                 return;
83         }
84         
85         // Set the connection settings to the values from ConnData.
87         ConnectionData = (*ConnData);
88         
89 }
91 CalDAVStatus CalDAV::GetConnectionData(){
92         
93         // Get the current connection settings for the CalDAV
94         // connection object and return a CalDAVStatus object.
95         
96         CalDAVStatus ConnectionStatus;
97         
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;
104         
105         return ConnectionStatus;
106         
109 CalDAVServerResult CalDAV::Connect(){
111         CalDAVServerResult ServerResult;
113         string ServerAddress = "";
114         string ServerUserPass = "";
116         // Setup the server address.
117         
118         ServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
120         // Setup the server password.
121         
122         ServerUserPass += ConnectionData.Username;
123         ServerUserPass += ":";
124         ServerUserPass += ConnectionData.Password;
125         
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);
134         
135         // Connect to the CalDAV server.
136         
137         ServerResult.Code = curl_easy_perform(ConnectionHandle);
139         // Process the result received from the server.
140         
141         if (ServerResult.Code != CURLE_OK){
142                 
143                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
144                 
145         } else {
146                 
147                 ServerResult.Result = CALDAVQUERYRESULT_OK;
148                 
149         }
150         
151         // Get the HTTP code.
152         
153         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
154         
155         return ServerResult;
156         
159 CalDAVServerResult CalDAV::GetServerResult(){
160         
161         return ConnectionServerResult;
162         
165 CalDAVServerSupport CalDAV::GetServerSupport(){
167         CalDAVServerSupport ServerStatus;
168         
169         // Setup the server connection.
170         
171         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "OPTIONS");
172         
173         CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
174         
175         // Set the results.
176         
177         if (ServerResult == CURLE_OK){
178                 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
179         } else {
180                 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;          
181         }
182         ConnectionServerResult.Code = ServerResult;
183         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
184         
185         if (ServerResult != CURLE_OK){
186                 return ServerStatus;
187         }
188         
189         // Check that the server header has data in,
190         // otherwise return an "empty" CalDAVServerSupport.
191         
192         if (ServerHeader.size() == 0){
193                 return ServerStatus;
194         }
195         
196         // Process each line looking for the first DAV header 
197         // line.
198         
199         bool NewlineMode = true;
200         
201         string DAVLine;
202         
203         for (int CharSeek = 0; CharSeek < ServerHeader.size(); CharSeek++){
204                 
205                 if (NewlineMode == true){
206                         
207                         // Check if we have reached the end of the string.
208                         
209                         if (CharSeek >= ServerHeader.size()){
210                                 
211                                 break;
212                                 
213                         }
214                         
215                         // Check the first four letters to make sure
216                         // they are 'DAV:'.
217                         
218                         string DAVHeaderCheck = "";
219                         
220                         try {
221                                 DAVHeaderCheck = ServerHeader.substr(CharSeek, 4);
222                         }
223                         
224                         catch (out_of_range &oor){
225                                 break;
226                         }
227                         
228                         if (DAVHeaderCheck == "DAV:"){
229                                 
230                                 CharSeek += 5;
231                                 
232                                 for (; CharSeek < ServerHeader.size(); CharSeek++){
233                                         
234                                         if (ServerHeader[CharSeek] == '\n'){
235                                         
236                                                 break;
237                                                 
238                                         }
239                                         
240                                         DAVLine.push_back(ServerHeader[CharSeek]);
241                                         
242                                 }
243                                 
244                                 break;
245                                 
246                         }
247                         
248                         NewlineMode = false;
249                         
250                 }
251                 
252                 if (ServerHeader[CharSeek] == '\n'){
253                         
254                         NewlineMode = true;
255                         
256                 }
257                 
258         }
259         
260         // Process the DAV line.
261         
262         vector<string> DAVLineData;
263         string DAVSegmentString;
264         
265         for (int CharSeek = 0; CharSeek < DAVLine.size(); CharSeek++){
266                 
267                 if (DAVLine[CharSeek] == ' '){
268                         continue;
269                 }
270                 
271                 if (DAVLine[CharSeek] == ','){
272                         
273                         DAVLineData.push_back(DAVSegmentString);
274                         DAVSegmentString.clear();
275                         continue;
276                         
277                 }
278                 
279                 DAVSegmentString += DAVLine[CharSeek];
280                 
281         }
282         
283         // Process the DAV values and set each value
284         // to true as required.
285         
286         for (int DAVItemSeek = 0; 
287                 DAVItemSeek < DAVLineData.size();
288                 DAVItemSeek++){
289                         
290                 if (DAVLineData.at(DAVItemSeek) == "calendar-access"){
291                         
292                         ServerStatus.BasicSupport = true;
293                 
294                 }
295                         
296         }
297         
298         // Reset the connection status.
299         
300         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);
301         
302         return ServerStatus;
303         
306 string CalDAV::GetUserPrincipal(){
307         
308         string CurrentUserPrincipal = "";
309         string UserPrincipalRequest = "";
310         CalDAVSendData UserPrincipalSendData;
311         
312         UserPrincipalRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
313         "<d:propfind xmlns:d=\"DAV:\">\n"
314         " <d:prop>\n"
315         "  <d:current-user-principal />\n"
316         " </d:prop>\n"
317         "</d:propfind>";
318         
319         UserPrincipalSendData.readptr = &UserPrincipalRequest;
320         UserPrincipalSendData.sizeleft = UserPrincipalRequest.size();
321         
322         // Setup the header.
323         
324         struct curl_slist *UserPrincipalRequestHeader = NULL;
325         
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");
329         
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);
336         
337         // Process the data.
338         
339         ServerData.clear();
340         ServerHeader.clear();
341         
342         CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
343         
344         // Set the results.
345         
346         if (ServerResult == CURLE_OK){
347                 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
348         } else {
349                 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;          
350         }
351         ConnectionServerResult.Code = ServerResult;
352         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
353         
354         if (ServerResult != CURLE_OK){
355                 
356                 return CurrentUserPrincipal;
357                 
358         }
359         
360         // Process the User Principal from the ServerData.
362         CurrentUserPrincipal = ProcessXMLUserPrincipal();
363         
364         // Reset the changed settings.
365         
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){
375         
376         string CalendarHomeURI = "";
378         // Build the Calendar Home URL address.
379         
380         string CalendarHomeURL = BuildServerAddress(&ConnectionData, UserPrincipalURI);
381         
382         // Setup the header request.
383         
384         CalDAVSendData CalendarHomeSendData;
385         
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"
388         " <d:prop>\n"
389         "  <c:calendar-home-set />\n"
390         " </d:prop>\n"
391         "</d:propfind>";
392         
393         CalendarHomeSendData.readptr = &CalendarHomeRequest;
394         CalendarHomeSendData.sizeleft = CalendarHomeRequest.size();
395         
396         // Setup the header.
397         
398         struct curl_slist *CalendarRequestHeader = NULL;
399         
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");
403         
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);
410         
411         // Process the data.
412         
413         ServerData.clear();
414         ServerHeader.clear();
415         
416         CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
417         
418         // Set the results.
419         
420         if (ServerResult == CURLE_OK){
421                 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
422         } else {
423                 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;          
424         }
425         ConnectionServerResult.Code = ServerResult;
426         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
427         
428         if (ServerResult != CURLE_OK){
429                 
430                 return CalendarHomeURI;
431                 
432         }
433         
434         // Process the User Principal from the ServerData.
436         CalendarHomeURI = ProcessXMLCalendarHome();
437         
438         // Reset the changed settings.
439         
440         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
441         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
442         
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);
447         
448         return CalendarHomeURI;
449         
452 CalDAVCalendarList CalDAV::GetCalendars(){
453         
454         CalDAVCalendarList ServerList;
455         CalDAVSendData CalendarListSendData;
456         
457         // Build the server address.
458         
459         string UserPrincipalURI = "";
460         UserPrincipalURI = GetUserPrincipal();
461         
462         if (UserPrincipalURI.size() == 0){
463                 
464                 return ServerList;
465                 
466         }
467         
468         string CalendarHomeURI = "";
469         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
470         
471         string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURI);
472         
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"
476         " <d:prop>\n"
477         "  <d:resourcetype />\n"
478         "  <d:displayname />\n"
479         "  <x0:calendar-color />\n"
480         "  <x0:calendar-order />\n"
481         "  <cs:getctag />\n"
482         "  <c:supported-calendar-component-set />\n"
483         "  <c:calendar-description />\n"
484         " </d:prop>\n"
485         "</d:propfind>";
486         
487         CalendarListSendData.readptr = &CalendarListRequest;
488         CalendarListSendData.sizeleft = CalendarListRequest.size();
489         
490         // Setup the header.
491         
492         struct curl_slist *CalendarListRequestHeader = NULL;
493         
494         CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Depth: 1");
495         CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Prefer: return-minimal");
496         CalendarListRequestHeader = curl_slist_append(CalendarListRequestHeader, "Content-Type: application/xml; charset=utf-8");
497         
498         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarListRequestHeader);
499         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str());
500         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
501         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
502         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarListSendData);
503         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
504         
505         // Process the data.
506         
507         ServerData.clear();
508         ServerHeader.clear();
509         
510         CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
511         
512         //ServerList = ProcessXMLCalendarList();
513         
514         if (ServerResult == CURLE_OK){
515                 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
516         } else {
517                 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;          
518         }
519         ConnectionServerResult.Code = ServerResult;
520         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
521         
522         if (ServerResult != CURLE_OK){
523                 
524                 return ServerList;
525                 
526         }
527         
528         // Process the received XML data into a list of calendars
529         // and locations.
530         
531         ServerList = ProcessXMLCalendarList();
532         
533         // Restore the original settings.
534         
535         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
536         
537         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
538         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
539         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
540         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
541         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
542         
543         return ServerList;
544         
547 CalDAVServerResult CalDAV::AddCalendar(string CalendarName){
548         
549         CalDAVServerResult ServerResult;
550         CalDAVSendData CalendarAddSendData;
551         
552         // Build the server address.
553         
554         string UserPrincipalURI = "";
555         UserPrincipalURI = GetUserPrincipal();
556         
557         if (UserPrincipalURI.size() == 0){
558                 
559                 return ServerResult;
560                 
561         }
562         
563         string CalendarHomeURI = "";
564         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
565         
566         // Generate the UUID.
567         
568         string UUIDValue = GenerateUUID();
569         UUIDValue.erase(UUIDValue.end()-1);
570         
571         string CalendarHomeURL = CalendarHomeURI;
572         CalendarHomeURL.append(UUIDValue);
573         CalendarHomeURL.append("/");
574         
575         // Build the calendar list address.
576         
577         string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURL);
578         
579         string CalendarAddRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
580         "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
581         " <d:set>\n"
582         "  <d:prop>\n"
583         "   <d:displayname>";
584         CalendarAddRequest += CalendarName;
585         CalendarAddRequest += "</d:displayname>\n"
586         "   <c:supported-calendar-component-set>\n"
587         "    <c:comp name=\"VTODO\"/>\n"
588         "    <c:comp name=\"VEVENT\"/>\n"
589         "   </c:supported-calendar-component-set>\n"
590         "  </d:prop>\n"
591         " </d:set>\n"
592         "</c:mkcalendar>";
593         
594         CalendarAddSendData.readptr = &CalendarAddRequest;
595         CalendarAddSendData.sizeleft = CalendarAddRequest.size();
596         
597         // Setup the header.
598         
599         struct curl_slist *CalendarRequestHeader = NULL;
600         
601         //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
602         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str());
603         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "MKCALENDAR");
604         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
605         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarAddSendData);
606         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
607         
608         // Process the data.
609         
610         ServerData.clear();
611         ServerHeader.clear();
612         
613         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
614         
615         if (ServerConnectionResult == CURLE_OK){
616                 ServerResult.Result = CALDAVQUERYRESULT_OK;
617         } else {
618                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
619         }
620         ServerResult.Code = ServerConnectionResult;
621         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
622         
623         // Restore the original settings.
624         
625         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
626         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
627         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
628         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
629         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
630         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
631         
632         return ServerResult;
633         
636 CalDAVServerResult CalDAV::EditCalendarProcess(string *CalendarHREF,
637                         string *CalendarName,
638                         Colour *CalendarColour,
639                         string *CalendarDescription,
640                         int *CalendarOrder){
642         CalDAVServerResult ServerResult;
643         CalDAVSendData CalendarEditSendData;
644         
645         // Build the server address.
646         
647         string UserPrincipalURI = "";
648         UserPrincipalURI = GetUserPrincipal();
649         
650         if (UserPrincipalURI.size() == 0){
651                 
652                 return ServerResult;
653                 
654         }
655         
656         string CalendarHomeURI = "";
657         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
658         
659         // Generate the UUID.
660         
661         string UUIDValue = GenerateUUID();
662         UUIDValue.erase(UUIDValue.end()-1);
663         
664         string CalendarHomeURL = CalendarHomeURI;
665         CalendarHomeURL.append(UUIDValue);
666         CalendarHomeURL.append("/");
667         
668         // Build the calendar list address.
669         
670         string CalendarEditURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF));
671         
672         string CalendarEditRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
673         "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"\n"
674         "       xmlns:x0=\"http://apple.com/ns/ical/\">\n"
675         " <d:set>\n"
676         "  <d:prop>\n";
677         
678         // Update the calendar name.
679         
680         if (CalendarName != nullptr){
681         
682                 CalendarEditRequest += "<d:displayname>";
683                 CalendarEditRequest += (*CalendarName);
684                 CalendarEditRequest += "</d:displayname>\n";
685                 
686         }
687         
688         // Update the calendar colour.
689         
690         if (CalendarColour != nullptr){
691                 
692                 CalendarEditRequest += "<x0:calendar-color>";
693                 CalendarEditRequest += (*CalendarColour);
694                 CalendarEditRequest += "</x0:calendar-color>\n";
695                 
696         }
697         
698         // Update the calendar description.
699         
700         if (CalendarDescription != nullptr){
701                 
702                 CalendarEditRequest += "<c:calendar-description>";
703                 CalendarEditRequest += (*CalendarDescription);
704                 CalendarEditRequest += "</c:calendar-description>\n";           
705                 
706         }
707         
708         // Update the calendar order.
709         
710         if (CalendarOrder != nullptr){
711                 
712                 CalendarEditRequest += "<x0:calendar-order>";
713                 CalendarEditRequest += to_string((*CalendarOrder));
714                 CalendarEditRequest += "</x0:calendar-order>\n";
715                 
716         }
717         
718         CalendarEditRequest += "  </d:prop>\n"
719         " </d:set>\n"
720         "</d:propertyupdate>";
721         
722         CalendarEditSendData.readptr = &CalendarEditRequest;
723         CalendarEditSendData.sizeleft = CalendarEditRequest.size();
724         
725         // Setup the header.
726         
727         struct curl_slist *CalendarRequestHeader = NULL;
728         
729         //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
730         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarEditURLAddress.c_str());
731         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
732         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
733         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarEditSendData);
734         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
735         
736         // Process the data.
737         
738         ServerData.clear();
739         ServerHeader.clear();
740         
741         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
742         
743         if (ServerConnectionResult == CURLE_OK){
744                 ServerResult.Result = CALDAVQUERYRESULT_OK;
745         } else {
746                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
747         }
748         ServerResult.Code = ServerConnectionResult;
749         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
750         
751         // Restore the original settings.
752         
753         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
754         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
755         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
756         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
757         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
758         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
759         
760         return ServerResult;
764 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
765                         string *CalendarName,
766                         Colour *CalendarColour,
767                         string *CalendarDescription,
768                         int *CalendarOrder){
769         
770         CalDAVServerResult ServerResult;
771         
772         ServerResult = EditCalendarProcess(CalendarHREF,
773                 CalendarName,
774                 CalendarColour,
775                 CalendarDescription,
776                 CalendarOrder);
777                                 
778         return ServerResult;
779         
782 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
783                         Colour *CalendarColour){
784         
786         CalDAVServerResult ServerResult;
788         ServerResult = EditCalendarProcess(CalendarHREF,
789                 nullptr,
790                 CalendarColour,
791                 nullptr,
792                 nullptr);
793                                 
794         return ServerResult;    
795         
798 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
799                         string *CalendarName){
800         
801         CalDAVServerResult ServerResult;
802         
803         ServerResult = EditCalendarProcess(CalendarHREF,
804                 CalendarName,
805                 nullptr,
806                 nullptr,
807                 nullptr);       
808         
809         return ServerResult;
810         
813 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
814                         int *CalendarOrder){
815         
816         CalDAVServerResult ServerResult;
817         
818         ServerResult = EditCalendarProcess(CalendarHREF,
819                 nullptr,
820                 nullptr,
821                 nullptr,
822                 CalendarOrder);
823         
824         return ServerResult;
825         
828 CalDAVServerResult CalDAV::EditCalendarDescription(string *CalendarHREF,
829                         string *CalendarDescription){
830         
831         CalDAVServerResult ServerResult;
832         
833         ServerResult = EditCalendarProcess(CalendarHREF,
834                 nullptr,
835                 nullptr,
836                 CalendarDescription,
837                 nullptr);
838         
839         return ServerResult;
840         
843 CalDAVServerResult CalDAV::DeleteCalendar(string *CalendarHREF){
845         CalDAVServerResult ServerResult;
846         
847         // Build the server address.
848         
849         string UserPrincipalURI = "";
850         UserPrincipalURI = GetUserPrincipal();
851         
852         if (UserPrincipalURI.size() == 0){
853                 
854                 return ServerResult;
855                 
856         }
857         
858         string CalendarHomeURI = "";
859         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
860         
861         // Generate the UUID.
862         
863         string UUIDValue = GenerateUUID();
864         UUIDValue.erase(UUIDValue.end()-1);
865         
866         string CalendarHomeURL = CalendarHomeURI;
867         CalendarHomeURL.append(UUIDValue);
868         CalendarHomeURL.append("/");
869         
870         // Build the calendar list address.
871         
872         struct curl_slist *DeleteRequestHeader = NULL;
873         
874         DeleteRequestHeader = curl_slist_append(DeleteRequestHeader, "Depth: infinity");
875         
876         string CalendarDeleteURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF));
877         
878         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, DeleteRequestHeader);
879         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarDeleteURLAddress.c_str());
880         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "DELETE");
881         
882         // Delete the calendar.
883         
884         ServerData.clear();
885         ServerHeader.clear();
886         
887         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
888         
889         if (ServerConnectionResult == CURLE_OK){
890                 ServerResult.Result = CALDAVQUERYRESULT_OK;
891         } else {
892                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
893         }
894         ServerResult.Code = ServerConnectionResult;
895         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
896         
897         // Restore the original settings.
898         
899         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
900         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
901         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
902         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
903         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
904         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
905         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
906         
907         return ServerResult;
908         
911 CalDAVServerResult CalDAV::GetEntryETag(string *CalendarEntryHREF, string *ETagValue){
912         
913         CalDAVServerResult ServerResult;
914         CalDAVSendData EntryETagGetData;
915         
916         // Build the server address.
917         
918         string UserPrincipalURI = "";
919         UserPrincipalURI = GetUserPrincipal();
920         
921         if (UserPrincipalURI.size() == 0){
922                 
923                 return ServerResult;
924                 
925         }
926         
927         string CalendarHomeURI = "";
928         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
930         // Split the path and filename.
931         
932         string EntryURIPath;
933         string EntryFilename;
934         
935         SplitPathFilename(CalendarEntryHREF, &EntryURIPath, &EntryFilename);
936         
937         // Build the request for the server.
939         string EntryETagRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
940         "<c:calendar-multiget xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
941         " <d:prop>\n"
942         "  <d:getetag />\n"
943         " </d:prop>\n"
944         " <d:href>";
945         EntryETagRequest += (*CalendarEntryHREF);
946         EntryETagRequest += "</d:href>\n"
947         "</c:calendar-multiget>";
948         
949         EntryETagGetData.readptr = &EntryETagRequest;
950         EntryETagGetData.sizeleft = EntryETagRequest.size();
951         
952         // Build the calendar list address.
953         
954         struct curl_slist *GetETagRequestHeader = NULL;
956         GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Depth: 1");     
957         GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Prefer: return-minimal");
958         GetETagRequestHeader = curl_slist_append(GetETagRequestHeader, "Content-Type: application/xml; charset=utf-8");
959         
960         string GetETagURLAddress = BuildServerAddress(&ConnectionData, EntryURIPath);
961         
962         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, GetETagRequestHeader);
963         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, GetETagURLAddress.c_str());
964         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
965         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
966         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryETagGetData);
967         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
968         
969         // Attempt to get the entity tag.
970         
971         ServerData.clear();
972         ServerHeader.clear();
973         
974         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
975         
976         if (ServerConnectionResult == CURLE_OK){
977                 ServerResult.Result = CALDAVQUERYRESULT_OK;
978         } else {
979                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
980         }
981         ServerResult.Code = ServerConnectionResult;
982         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
983         
984         if (ServerConnectionResult != CURLE_OK){
985                 return ServerResult;
986         }
987         
988         // Get the entity tag from the result.
989         
990         *ETagValue = ProcessXMLEntryETag();
991         
992         // Restore the original settings.
993         
994         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
995         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
996         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
997         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
998         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
999         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1000         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1001         
1002         return ServerResult;
1003         
1006 CalDAVServerResult CalDAV::AddEntry(string *CalendarEntryHREF, string *EntryData){
1007         
1008         // Add an entry to the calendar collection.
1009         
1010         CalDAVServerResult ServerResult;
1011         CalDAVSendData EntryAddSendData;
1012         
1013         // Build the calendar list address.
1014         
1015         string EntryAddURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1016         
1017         EntryAddSendData.readptr = EntryData;
1018         EntryAddSendData.sizeleft = EntryData->size();
1019         
1020         struct curl_slist *CalendarRequestHeader = NULL;
1021         
1022         CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Content-Type: text/calendar; charset=utf-8");
1023         
1024         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
1025         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryAddURLAddress.c_str());
1026         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
1027         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
1028         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryAddSendData);
1029         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
1030         
1031         // Process the data.
1032         
1033         ServerData.clear();
1034         ServerHeader.clear();
1035         
1036         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1037         
1038         if (ServerConnectionResult == CURLE_OK){
1039                 ServerResult.Result = CALDAVQUERYRESULT_OK;
1040         } else {
1041                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
1042         }
1043         ServerResult.Code = ServerConnectionResult;
1044         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1045         
1046         // Restore the original settings.
1047         
1048         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1049         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1050         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
1051         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1052         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1053         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1054         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1055         
1056         return ServerResult;
1057         
1060 CalDAVServerResult CalDAV::EditEntry(string *CalendarEntryHREF, string *EntryData, string *EntryETag){
1061         
1062         // Edit an entry in the calendar collection.
1064         // Add an entry to the calendar collection.
1065         
1066         CalDAVServerResult ServerResult;
1067         CalDAVSendData EntryAddSendData;
1068         
1069         // Build the calendar list address.
1070         
1071         string EntryAddURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1072         
1073         EntryAddSendData.readptr = EntryData;
1074         EntryAddSendData.sizeleft = EntryData->size();
1075         
1076         string IfMatchHeader = "If-Match: \"";
1077         IfMatchHeader.append(*EntryETag);
1078         IfMatchHeader.append("\"");
1079         
1080         struct curl_slist *CalendarRequestHeader = NULL;
1081         
1082         CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, "Content-Type: text/calendar; charset=utf-8");
1083         CalendarRequestHeader = curl_slist_append(CalendarRequestHeader, IfMatchHeader.c_str());        
1084         
1085         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
1086         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryAddURLAddress.c_str());
1087         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
1088         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
1089         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &EntryAddSendData);
1090         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
1091         
1092         // Process the data.
1093         
1094         ServerData.clear();
1095         ServerHeader.clear();
1096         
1097         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1098         
1099         if (ServerConnectionResult == CURLE_OK){
1100                 ServerResult.Result = CALDAVQUERYRESULT_OK;
1101         } else {
1102                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
1103         }
1104         ServerResult.Code = ServerConnectionResult;
1105         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1106         
1107         // Restore the original settings.
1108         
1109         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1110         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1111         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
1112         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1113         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1114         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1115         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1116         
1117         return ServerResult;
1118         
1121 CalDAVServerResult CalDAV::DeleteEntry(string *CalendarEntryHREF){
1122         
1123         // Delete an entry in the calendar collection.
1124         
1125         CalDAVServerResult ServerResult;
1126         
1127         // Build the calendar list address.
1128         
1129         string EntryDeleteURLAddress = BuildServerAddress(&ConnectionData, (*CalendarEntryHREF));
1130         
1131         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1132         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, EntryDeleteURLAddress.c_str());
1133         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "DELETE");
1134         
1135         // Delete the calendar.
1136         
1137         ServerData.clear();
1138         ServerHeader.clear();
1139         
1140         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
1141         
1142         if (ServerConnectionResult == CURLE_OK){
1143                 ServerResult.Result = CALDAVQUERYRESULT_OK;
1144         } else {
1145                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
1146         }
1147         ServerResult.Code = ServerConnectionResult;
1148         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
1149         
1150         // Restore the original settings.
1151         
1152         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
1153         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
1154         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
1155         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
1156         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
1157         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
1158         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, NULL);
1159         
1160         return ServerResult;
1161         
1164 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
1166         // Check if the passed CalDAV Connection Data is has
1167         // an address set. Return false if nullptr is used.
1169         if (ConnData == nullptr){
1170         
1171                 return false;
1172         
1173         }
1174         
1175         // Check the server hostname. Return false
1176         // if no value has been set.
1177         
1178         if (ConnData->Hostname.size() == 0){
1179         
1180                 return false;
1181         
1182         }
1183         
1184         // Check the server port. Return false if
1185         // no value has been set or the port number
1186         // is less than 1 or higher than 65535.
1187         
1188         if (ConnData->Port < 1 || ConnData->Port > 65535){
1189         
1190                 return false;
1191         
1192         }
1193         
1194         // Check the server username. Return false
1195         // if no value has been set.
1196         
1197         if (ConnData->Username.size() == 0){
1198                 
1199                 return false;
1200                 
1201         }       
1202         
1203         // Check the server password. Return false
1204         // if no value has been set.
1205         
1206         if (ConnData->Password.size() == 0){
1207                 
1208                 return false;
1209                 
1210         }
1212         // Cannot check UseSSL: It is either true
1213         // or false.
1214         
1215         // Cannot check Prefix: The prefix may need
1216         // to be worked out first.
1218         // No errors were found whilst checking so
1219         // return true.
1220         
1221         return true;
1225 string BuildServerAddress(CalDAVConnectionData *ConnData, string URIAddress){
1226         
1227         string ServerAddress;
1228         
1229         // Setup the server address.
1230         
1231         if (ConnData->UseSSL == true){
1232                 ServerAddress += "https://";
1233         } else {
1234                 ServerAddress += "http://";
1235         }
1236         
1237         ServerAddress += ConnData->Hostname;
1238         
1239         // Check if server port is 80, otherwise
1240         // specifiy the port number in the address.
1241         
1242         if (ConnData->Port != 80){
1243                 ServerAddress += ":";
1244                 ServerAddress += to_string(ConnData->Port);
1245         }
1246         
1247         ServerAddress += URIAddress;
1248         
1249         return ServerAddress;
1250         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy