Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code and unit tests for editing calendars 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         // Restore the original settings.
515         
516         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals");
517         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
518         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
519         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
520         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
521         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
522         
523         // Process the received XML data into a list of calendars
524         // and locations.
525         
526         if (ServerResult != CURLE_OK){
527                 
528                 return ServerList;
529                 
530         }
531         
532         ServerList = ProcessXMLCalendarList();
533         
534         return ServerList;
535         
538 CalDAVServerResult CalDAV::AddCalendar(string CalendarName){
539         
540         CalDAVServerResult ServerResult;
541         CalDAVSendData CalendarAddSendData;
542         
543         // Build the server address.
544         
545         string UserPrincipalURI = "";
546         UserPrincipalURI = GetUserPrincipal();
547         
548         if (UserPrincipalURI.size() == 0){
549                 
550                 return ServerResult;
551                 
552         }
553         
554         string CalendarHomeURI = "";
555         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
556         
557         // Generate the UUID.
558         
559         string UUIDValue = GenerateUUID();
560         UUIDValue.erase(UUIDValue.end()-1);
561         
562         string CalendarHomeURL = CalendarHomeURI;
563         CalendarHomeURL.append(UUIDValue);
564         CalendarHomeURL.append("/");
565         
566         // Build the calendar list address.
567         
568         string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURL);
569         
570         string CalendarAddRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
571         "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">\n"
572         " <d:set>\n"
573         "  <d:prop>\n"
574         "   <d:displayname>";
575         CalendarAddRequest += CalendarName;
576         CalendarAddRequest += "</d:displayname>\n"
577         "   <c:supported-calendar-component-set>\n"
578         "    <c:comp name=\"VTODO\"/>\n"
579         "    <c:comp name=\"VEVENT\"/>\n"
580         "   </c:supported-calendar-component-set>\n"
581         "  </d:prop>\n"
582         " </d:set>\n"
583         "</c:mkcalendar>";
584         
585         CalendarAddSendData.readptr = &CalendarAddRequest;
586         CalendarAddSendData.sizeleft = CalendarAddRequest.size();
587         
588         // Setup the header.
589         
590         struct curl_slist *CalendarRequestHeader = NULL;
591         
592         //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
593         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str());
594         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "MKCALENDAR");
595         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
596         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarAddSendData);
597         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
598         
599         // Process the data.
600         
601         ServerData.clear();
602         ServerHeader.clear();
603         
604         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
605         
606         if (ServerConnectionResult == CURLE_OK){
607                 ServerResult.Result = CALDAVQUERYRESULT_OK;
608         } else {
609                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
610         }
611         ServerResult.Code = ServerConnectionResult;
612         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
613         
614         // Restore the original settings.
615         
616         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
617         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
618         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
619         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
620         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
621         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
622         
623         return ServerResult;
624         
627 CalDAVServerResult CalDAV::EditCalendarProcess(string *CalendarHREF,
628                         string *CalendarName,
629                         Colour *CalendarColour,
630                         string *CalendarDescription,
631                         int *CalendarOrder){
633         CalDAVServerResult ServerResult;
634         CalDAVSendData CalendarEditSendData;
635         
636         // Build the server address.
637         
638         string UserPrincipalURI = "";
639         UserPrincipalURI = GetUserPrincipal();
640         
641         if (UserPrincipalURI.size() == 0){
642                 
643                 return ServerResult;
644                 
645         }
646         
647         string CalendarHomeURI = "";
648         CalendarHomeURI = GetCalendarHome(UserPrincipalURI);
649         
650         // Generate the UUID.
651         
652         string UUIDValue = GenerateUUID();
653         UUIDValue.erase(UUIDValue.end()-1);
654         
655         string CalendarHomeURL = CalendarHomeURI;
656         CalendarHomeURL.append(UUIDValue);
657         CalendarHomeURL.append("/");
658         
659         // Build the calendar list address.
660         
661         string CalendarEditURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF));
662         
663         string CalendarEditRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
664         "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"\n"
665         "       xmlns:x0=\"http://apple.com/ns/ical/\">\n"
666         " <d:set>\n"
667         "  <d:prop>\n";
668         
669         // Update the calendar name.
670         
671         if (CalendarName != nullptr){
672         
673                 CalendarEditRequest += "<d:displayname>";
674                 CalendarEditRequest += (*CalendarName);
675                 CalendarEditRequest += "</d:displayname>\n";
676                 
677         }
678         
679         // Update the calendar colour.
680         
681         if (CalendarColour != nullptr){
682                 
683                 CalendarEditRequest += "<x0:calendar-color>";
684                 CalendarEditRequest += (*CalendarColour);
685                 CalendarEditRequest += "</x0:calendar-color>\n";
686                 
687         }
688         
689         // Update the calendar description.
690         
691         if (CalendarDescription != nullptr){
692                 
693                 CalendarEditRequest += "<c:calendar-description>";
694                 CalendarEditRequest += (*CalendarDescription);
695                 CalendarEditRequest += "</c:calendar-description>\n";           
696                 
697         }
698         
699         // Update the calendar order.
700         
701         if (CalendarOrder != nullptr){
702                 
703                 CalendarEditRequest += "<x0:calendar-order>";
704                 CalendarEditRequest += to_string((*CalendarOrder));
705                 CalendarEditRequest += "</x0:calendar-order>\n";
706                 
707         }
708         
709         CalendarEditRequest += "  </d:prop>\n"
710         " </d:set>\n"
711         "</d:propertyupdate>";
712         
713         CalendarEditSendData.readptr = &CalendarEditRequest;
714         CalendarEditSendData.sizeleft = CalendarEditRequest.size();
715         
716         // Setup the header.
717         
718         struct curl_slist *CalendarRequestHeader = NULL;
719         
720         //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
721         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarEditURLAddress.c_str());
722         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
723         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L);
724         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarEditSendData);
725         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
726         
727         // Process the data.
728         
729         ServerData.clear();
730         ServerHeader.clear();
731         
732         CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle);
733         
734         if (ServerConnectionResult == CURLE_OK){
735                 ServerResult.Result = CALDAVQUERYRESULT_OK;
736         } else {
737                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;            
738         }
739         ServerResult.Code = ServerConnectionResult;
740         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
741         
742         // Restore the original settings.
743         
744         string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
745         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str());
746         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
747         curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L);
748         curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL);
749         curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL);
750         
751         return ServerResult;
755 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
756                         string *CalendarName,
757                         Colour *CalendarColour,
758                         string *CalendarDescription,
759                         int *CalendarOrder){
760         
761         CalDAVServerResult ServerResult;
762         
763         ServerResult = EditCalendarProcess(CalendarHREF,
764                 CalendarName,
765                 CalendarColour,
766                 CalendarDescription,
767                 CalendarOrder);
768                                 
769         return ServerResult;
770         
773 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
774                         Colour *CalendarColour){
775         
777         CalDAVServerResult ServerResult;
779         ServerResult = EditCalendarProcess(CalendarHREF,
780                 nullptr,
781                 CalendarColour,
782                 nullptr,
783                 nullptr);
784                                 
785         return ServerResult;    
786         
789 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
790                         string *CalendarName){
791         
792         CalDAVServerResult ServerResult;
793         
794         ServerResult = EditCalendarProcess(CalendarHREF,
795                 CalendarName,
796                 nullptr,
797                 nullptr,
798                 nullptr);       
799         
800         return ServerResult;
801         
804 CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF,
805                         int *CalendarOrder){
806         
807         CalDAVServerResult ServerResult;
808         
809         ServerResult = EditCalendarProcess(CalendarHREF,
810                 nullptr,
811                 nullptr,
812                 nullptr,
813                 CalendarOrder);
814         
815         return ServerResult;
816         
819 CalDAVServerResult CalDAV::EditCalendarDescription(string *CalendarHREF,
820                         string *CalendarDescription){
821         
822         CalDAVServerResult ServerResult;
823         
824         ServerResult = EditCalendarProcess(CalendarHREF,
825                 nullptr,
826                 nullptr,
827                 CalendarDescription,
828                 nullptr);
829         
830         return ServerResult;
831         
833 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
835         // Check if the passed CalDAV Connection Data is has
836         // an address set. Return false if nullptr is used.
838         if (ConnData == nullptr){
839         
840                 return false;
841         
842         }
843         
844         // Check the server hostname. Return false
845         // if no value has been set.
846         
847         if (ConnData->Hostname.size() == 0){
848         
849                 return false;
850         
851         }
852         
853         // Check the server port. Return false if
854         // no value has been set or the port number
855         // is less than 1 or higher than 65535.
856         
857         if (ConnData->Port < 1 || ConnData->Port > 65535){
858         
859                 return false;
860         
861         }
862         
863         // Check the server username. Return false
864         // if no value has been set.
865         
866         if (ConnData->Username.size() == 0){
867                 
868                 return false;
869                 
870         }       
871         
872         // Check the server password. Return false
873         // if no value has been set.
874         
875         if (ConnData->Password.size() == 0){
876                 
877                 return false;
878                 
879         }
881         // Cannot check UseSSL: It is either true
882         // or false.
883         
884         // Cannot check Prefix: The prefix may need
885         // to be worked out first.
887         // No errors were found whilst checking so
888         // return true.
889         
890         return true;
894 string BuildServerAddress(CalDAVConnectionData *ConnData, string URIAddress){
895         
896         string ServerAddress;
897         
898         // Setup the server address.
899         
900         if (ConnData->UseSSL == true){
901                 ServerAddress += "https://";
902         } else {
903                 ServerAddress += "http://";
904         }
905         
906         ServerAddress += ConnData->Hostname;
907         
908         // Check if server port is 80, otherwise
909         // specifiy the port number in the address.
910         
911         if (ConnData->Port != 80){
912                 ServerAddress += ":";
913                 ServerAddress += to_string(ConnData->Port);
914         }
915         
916         ServerAddress += URIAddress;
917         
918         return ServerAddress;
919         
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