Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Perform checks if XML Node(s) have been found or not.
[xestiacalendar/.git] / source / objects / CalDAV / CalDAV-XMLProcessing.cpp
1 // CalDAV-XMLProcessing.cpp - CalDAV Connection Object - XML Processing.
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;
22  
23 string CalDAV::ProcessXMLUserPrincipal(){
25         string UserPrincipalURI;
26         
27         xmlDocPtr xmlCalDAVDoc;
28         xmlCalDAVDoc = xmlReadMemory(ServerData.c_str(), (int)ServerData.size(), "noname.xml", NULL, 0);
30         xmlNodePtr NodeSeek;
31         bool NodeFound = false;
32         
33         // Start with the first node, look for multistatus.
34         
35         for (NodeSeek = xmlCalDAVDoc->children;
36                 NodeSeek != NULL;
37                 NodeSeek = NodeSeek->next)
38         {
39         
40                 if (!xmlStrcmp(NodeSeek->name, (const xmlChar *)"multistatus") ||
41                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"d:multistatus") ||
42                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"D:multistatus")
43                 ){
44                         
45                         NodeFound = true;
46                         break;
47                         
48                 }
49                 
50         }
51         
52         if (NodeFound == false){
53         
54                 return UserPrincipalURI;
56         }
57         
58         // Look for response.
60         if (NodeFound == false){ return UserPrincipalURI; } else { NodeFound = false; }
61         NodeFound = MatchXMLNameTransverse(&NodeSeek, "response");
63         // Look for propstat.
64         
65         if (NodeFound == false){ return UserPrincipalURI; } else { NodeFound = false; }
66         NodeFound = MatchXMLNameTransverse(&NodeSeek, "propstat");
68         // Look for prop.
69         
70         if (NodeFound == false){ return UserPrincipalURI; } else { NodeFound = false; }
71         NodeFound = MatchXMLNameTransverse(&NodeSeek, "prop");
72         
73         // Look for current-user-principal.
74         
75         if (NodeFound == false){ return UserPrincipalURI; } else { NodeFound = false; }
76         NodeFound = MatchXMLNameTransverse(&NodeSeek, "current-user-principal");
77         
78         // Look for href.       
79         
80         if (NodeFound == false){ return UserPrincipalURI; } else { NodeFound = false; }
81         NodeFound = MatchXMLNameTransverse(&NodeSeek, "href");
83         // Get the data from href.
84         
85         UserPrincipalURI = FetchXMLData(&NodeSeek);
86         
87         xmlFreeDoc(xmlCalDAVDoc);
88         
89         return UserPrincipalURI;
90         
91 }
93 string CalDAV::ProcessXMLCalendarHome(){
95         string CalendarHomeURI;
96         
97         xmlDocPtr xmlCalDAVDoc;
98         xmlCalDAVDoc = xmlReadMemory(ServerData.c_str(), (int)ServerData.size(), "noname.xml", NULL, 0);
100         xmlNodePtr NodeSeek;
101         bool NodeFound = false;
102         
103         // Start with the first node, look for multistatus.
104         
105         for (NodeSeek = xmlCalDAVDoc->children;
106                 NodeSeek != NULL;
107                 NodeSeek = NodeSeek->next)
108         {
109         
110                 if (!xmlStrcmp(NodeSeek->name, (const xmlChar *)"multistatus") ||
111                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"d:multistatus") ||
112                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"D:multistatus")
113                 ){
114                         
115                         NodeFound = true;
116                         break;
117                         
118                 }
119                 
120         }
121         
122         if (NodeFound == false){
123         
124                 return CalendarHomeURI;
126         }
127         
128         // Look for response.
130         if (NodeFound == false){ return CalendarHomeURI; } else { NodeFound = false; }
131         NodeFound = MatchXMLNameTransverse(&NodeSeek, "response");
133         // Look for propstat.
134         
135         if (NodeFound == false){ return CalendarHomeURI; } else { NodeFound = false; }
136         NodeFound = MatchXMLNameTransverse(&NodeSeek, "propstat");
138         // Look for prop.
139         
140         if (NodeFound == false){ return CalendarHomeURI; } else { NodeFound = false; }
141         NodeFound = MatchXMLNameTransverse(&NodeSeek, "prop");
142         
143         // Look for calendar-home-set.
144         
145         if (NodeFound == false){ return CalendarHomeURI; } else { NodeFound = false; }
146         NodeFound = MatchXMLNameTransverse(&NodeSeek, "calendar-home-set");
147         
148         // Look for href.       
149         
150         if (NodeFound == false){ return CalendarHomeURI; } else { NodeFound = false; }
151         NodeFound = MatchXMLNameTransverse(&NodeSeek, "href");
153         // Get the data from href.
154         
155         CalendarHomeURI = FetchXMLData(&NodeSeek);
156         
157         xmlFreeDoc(xmlCalDAVDoc);
158         
159         return CalendarHomeURI;
160         
163 CalDAVCalendarList CalDAV::ProcessXMLCalendarList(){
164         
165         CalDAVCalendarList CalendarList;
166         
167         xmlDocPtr xmlCalDAVDoc;
168         xmlCalDAVDoc = xmlReadMemory(ServerData.c_str(), (int)ServerData.size(), "noname.xml", NULL, 0);
170         xmlNodePtr NodeSeek = NULL;
171         xmlNodePtr NodeResponse = NULL;
172         xmlNodePtr NodeMatch = NULL;
173         xmlNodePtr NodeData = NULL;
174         bool NodeFound = false;
175         int ResponseCount = 0;
176         
177         // Start with the first node, look for multistatus.
178         
179         for (NodeSeek = xmlCalDAVDoc->children;
180                 NodeSeek != NULL;
181                 NodeSeek = NodeSeek->next)
182         {
183         
184                 if (!xmlStrcmp(NodeSeek->name, (const xmlChar *)"multistatus") ||
185                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"d:multistatus") ||
186                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"D:multistatus")
187                 ){
188                 
189                         NodeResponse = NodeSeek->children;      
190                         NodeFound = true;
191                         break;
192                         
193                 }
194                 
195         }
196         
197         if (NodeFound == false){
198         
199                 return CalendarList;
201         }
202         
203         for (NodeResponse = NodeSeek->children;
204                 NodeResponse != nullptr;
205                 NodeResponse = NodeResponse->next)
206         {
207         
208                 // Go through each of the responses and find the calendars.
210                 NodeMatch = xmlCopyNode(NodeResponse, 1);
211                 
212                 if (MatchXMLName(&NodeMatch, "response")){
213 \r                       NodeData = xmlCopyNode(NodeMatch, 1);
214                         
215                         // Check the resource type is a calendar.
216                         
217                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
218                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
219                         if (!MatchXMLNameTransverse(&NodeData, "resourcetype")){ continue; }                    
220                         if (!MatchXMLNameTransverse(&NodeData, "calendar")){ continue; }
221                         
222                         // Get the HREF.
223                         
224                         NodeData = xmlCopyNode(NodeMatch, 1);
225                         
226                         if (!MatchXMLNameTransverse(&NodeData, "href")){ continue; }
227                                                 
228                         string HREFAddress = FetchXMLData(&NodeData);
229                         
230                         // Get the calendar name.
232                         NodeData = xmlCopyNode(NodeMatch, 1);
233                         
234                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
235                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
236                         if (!MatchXMLNameTransverse(&NodeData, "displayname")){ continue; }
237                         
238                         string CalendarName = FetchXMLData(&NodeData);
239                         
240                         // Get the calendar description.
241                         
242                         NodeData = xmlCopyNode(NodeMatch, 1);
243                         
244                         string CalendarDescription = "";
245                         
246                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
247                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
248                         if (MatchXMLNameTransverse(&NodeData, "calendar-description")){ 
249                         
250                                 CalendarDescription = FetchXMLData(&NodeData);
251                                 
252                         }
253                         
254                         // Get the calendar colour.
255                         
256                         NodeData = xmlCopyNode(NodeMatch, 1);
258                         Colour CalendarColour;
259                         bool ColourResult = false;
260                         
261                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
262                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
263                         if (MatchXMLNameTransverse(&NodeData, "calendar-color")){ 
264                         
265                                 string CalendarColourString = "";
266                                 string CalendarColourHexValue = "";
267                                 int ColourNumber;
268                                 bool KeepRunning = true;
269                                 
270                                 CalendarColourString = FetchXMLData(&NodeData);
271                                 
272                                 while(KeepRunning == true){
273                                 
274                                         if (CalendarColourString.substr(0,1) == "#" && CalendarColourString.length() == 9){
275                                                 
276                                                 // Get the red colour.
277                                                 
278                                                 CalendarColourHexValue = CalendarColourString.substr(1,2);
279                                                 if (!HexToInt(&CalendarColourHexValue, &ColourNumber)){ break; }
280                                                 CalendarColour.red = ColourNumber;
281                                                 
282                                                 // Get the green colour.
284                                                 CalendarColourHexValue = CalendarColourString.substr(3,2);
285                                                 if (!HexToInt(&CalendarColourHexValue, &ColourNumber)){ break; }
286                                                 CalendarColour.green = ColourNumber;
287                                                 
288                                                 // Get the blue colour.
289                                                 
290                                                 CalendarColourHexValue = CalendarColourString.substr(5,2);
291                                                 if (!HexToInt(&CalendarColourHexValue, &ColourNumber)){ break; };
292                                                 CalendarColour.blue = ColourNumber;
293                                                 
294                                                 // Get the alpha.
296                                                 CalendarColourHexValue = CalendarColourString.substr(7,2);
297                                                 if (!HexToInt(&CalendarColourHexValue, &ColourNumber)){ break; };
298                                                 CalendarColour.alpha = ColourNumber;
299                                                 
300                                                 ColourResult = true;
301                                                 
302                                         } else {
304                                                 ColourResult = false;
305                                         
306                                         }
307                                         
308                                         break;
309                                         
310                                 }
311                                 
312                         }
313                         
314                         if (ColourResult == false){
315                                         
316                                         CalendarColour.red = 0;
317                                         CalendarColour.blue = 0;
318                                         CalendarColour.green = 0;
319                                         CalendarColour.alpha = 0;
320                                         
321                         }
322                         
323                         // Get the calendar order.
325                         NodeData = xmlCopyNode(NodeMatch, 1);
326                         
327                         int CalendarOrder = 0;
328                         
329                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
330                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
331                         if (MatchXMLNameTransverse(&NodeData, "calendar-order")){
332                                 
333                                 string CalendarOrderString = FetchXMLData(&NodeData);
334                                 if (!HexToInt(&CalendarOrderString, &CalendarOrder)){
335                                         CalendarOrder = 0;
336                                 }
337                                 
338                         }
339                         
340                         // Get the calendar tag.
341                         
342                         NodeData = xmlCopyNode(NodeMatch, 1);
343                         
344                         string CalendarTag = "";
345                         
346                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
347                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
348                         if (MatchXMLNameTransverse(&NodeData, "getctag")){ 
349                                 
350                                 CalendarTag = FetchXMLData(&NodeData);
351                                 
352                         }
353                         
354                         // Get the calendar tag URL.
355                         
356                         NodeData = xmlCopyNode(NodeMatch, 1);
357                         
358                         string CalendarTagURL = "";
359                         
360                         if (!MatchXMLNameTransverse(&NodeData, "propstat")){ continue; }
361                         if (!MatchXMLNameTransverse(&NodeData, "prop")){ continue; }
362                         if (MatchXMLNameTransverse(&NodeData, "sync-token")){ 
363                                 
364                                 CalendarTagURL = FetchXMLData(&NodeData);
365                                 
366                         }
367                         
368                         // Insert the calendar information into the
369                         // list if all the information is there.
370                         
371                         CalendarList.Name.insert(make_pair(ResponseCount, CalendarName));
372                         CalendarList.Description.insert(make_pair(ResponseCount, CalendarDescription));
373                         CalendarList.HREF.insert(make_pair(ResponseCount, HREFAddress));
374                         CalendarList.CalColour.insert(make_pair(ResponseCount, CalendarColour));
375                         CalendarList.Order.insert(make_pair(ResponseCount, CalendarOrder));
376                         CalendarList.Tag.insert(make_pair(ResponseCount, CalendarTag));
377                         CalendarList.TagURL.insert(make_pair(ResponseCount, CalendarTagURL));
378                         
379                         ResponseCount++;
380                         
381                 }
382                 
383         }
384         
385         xmlFreeDoc(xmlCalDAVDoc);
386         
387         return CalendarList;
388         
391 string CalDAV::ProcessXMLEntryETag(){
392         
393         string EntryETag;
394         
395         xmlDocPtr xmlCalDAVDoc;
396         xmlCalDAVDoc = xmlReadMemory(ServerData.c_str(), (int)ServerData.size(), "noname.xml", NULL, 0);
398         xmlNodePtr NodeSeek;
399         bool NodeFound = false;
400         
401         // Start with the first node, look for multistatus.
402         
403         for (NodeSeek = xmlCalDAVDoc->children;
404                 NodeSeek != NULL;
405                 NodeSeek = NodeSeek->next)
406         {
407         
408                 if (!xmlStrcmp(NodeSeek->name, (const xmlChar *)"multistatus") ||
409                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"d:multistatus") ||
410                         !xmlStrcmp(NodeSeek->name, (const xmlChar *)"D:multistatus")
411                 ){
412                         
413                         NodeFound = true;
414                         break;
415                         
416                 }
417                 
418         }
419         
420         if (NodeFound == false){
421         
422                 return EntryETag;
424         }
425         
426         // Look for response.
427         
428         if (NodeFound == false){ return EntryETag; } else { NodeFound = false; }
429         NodeFound = MatchXMLNameTransverse(&NodeSeek, "response");
431         // Look for propstat.
432         
433         if (NodeFound == false){ return EntryETag; } else { NodeFound = false; }
434         NodeFound = MatchXMLNameTransverse(&NodeSeek, "propstat");
436         // Look for prop.
437         
438         if (NodeFound == false){ return EntryETag; } else { NodeFound = false; }
439         NodeFound = MatchXMLNameTransverse(&NodeSeek, "prop");
440         
441         // Look for calendar-home-set.
442         
443         if (NodeFound == false){ return EntryETag; } else { NodeFound = false; }
444         NodeFound = MatchXMLNameTransverse(&NodeSeek, "getetag");
446         // Get the data from href.
447         
448         EntryETag = FetchXMLData(&NodeSeek);    
449         
450         xmlFreeDoc(xmlCalDAVDoc);       
451         
452         // Check if the entity tag contains quote marks
453         // at the start and end and remove them (if needed).
454         
455         if (EntryETag.substr(0,1) == "\"" && 
456                 EntryETag.substr(EntryETag.size()-1, 1) == "\"" && EntryETag.size() > 2){
457                 
458                 EntryETag.erase(EntryETag.begin());
459                 EntryETag.erase(EntryETag.end()-1);
460                         
461         }
462         
463         return EntryETag;
464         
467 bool CalDAV::MatchXMLNameTransverse(xmlNodePtr *NodePtr, string NodeName){
469         string NodeNameSmallD = "d:" + NodeName;
470         string NodeNameLargeD = "D:" + NodeName;
471         
472         for ((*NodePtr) = (*NodePtr)->children;
473                 (*NodePtr) != NULL;
474                 (*NodePtr) = (*NodePtr)->next)
475         {
476         
477                 if (!xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeName.c_str()) ||
478                         !xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeNameSmallD.c_str()) ||
479                         !xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeNameLargeD.c_str())
480                 ){
481                         
482                         return true;
483                         
484                 }
485                 
486         }
487         
488         return false;
489         
492 bool CalDAV::MatchXMLName(xmlNodePtr *NodePtrOriginal, string NodeName){
494         if (NodePtrOriginal == nullptr){
495                 
496                 return false;
497                 
498         }
499         
500         string NodeNameSmallD = "d:" + NodeName;
501         string NodeNameLargeD = "D:" + NodeName;
502         
503         xmlNodePtr *NodePtr = NodePtrOriginal;
504         
505         if (!xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeName.c_str()) ||
506                 !xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeNameSmallD.c_str()) ||
507                 !xmlStrcmp((*NodePtr)->name, (const xmlChar *)NodeNameLargeD.c_str())
508         ){
509                         
510                 return true;
511                         
512         } else {
513                         
514                 return false;
515                 
516         }
517         
518         return false;
519         
522 string CalDAV::FetchXMLData(xmlNodePtr *NodePtr){
524         for ((*NodePtr) = (*NodePtr)->children;
525                 (*NodePtr) != NULL;
526                 (*NodePtr) = (*NodePtr)->next)
527         {
528                                                                 
529                 return (const char*)(*NodePtr)->content;
530                         
531         }
532         
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