Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added copyright and license headers on C++ source and header files in the carddav...
[xestiaab/.git] / source / carddav / carddav.cpp
1 // carddav.cpp - Main CardDAV Object file.\r
2 //\r
3 // (c) 2012-2015 Xestia Software Development.\r
4 //\r
5 // This file is part of Xestia Address Book.\r
6 //\r
7 // Xestia Address Book is free software: you can redistribute it and/or modify\r
8 // it under the terms of the GNU General Public License as published by the\r
9 // Free Software Foundation, version 3 of the license.\r
10 //\r
11 // Xestia Address Book is distributed in the hope that it will be useful,\r
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
14 // GNU General Public License for more details.\r
15 //\r
16 // You should have received a copy of the GNU General Public License along\r
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>\r
18 \r
19 #include "carddav.h"\r
20 #include "../version.h"\r
21 #include <wx/wx.h>\r
22 #include <wx/tokenzr.h>\r
23 #include <wx/ffile.h>\r
24 #include <libxml/parser.h>\r
25 #include <libxml/tree.h>\r
26 #include <map>\r
27 #include <thread>\r
28 #include "../vcard/vcard.h"\r
29 #include "../common/dirs.h"\r
30 \r
31 size_t WritebackFunc(char *ptr, size_t size, size_t nmemb, wxString *stream){\r
32         \r
33         wxString Data;\r
34         Data = wxString::FromUTF8((char *)ptr);\r
35         \r
36         stream->Append(Data);\r
37         \r
38         return size * nmemb;\r
39 \r
40 }\r
41 \r
42 int ProgressFunc(void *clientdata, double TTDown, double NDown, double TTUp, double NUp){\r
43 \r
44         int ProgressRet;\r
45         \r
46         CardDAV *IncCardDAV = static_cast<CardDAV*>(clientdata);\r
47         ProgressRet = IncCardDAV->ProgressFuncProc(clientdata, TTDown, NDown, TTUp, NUp);\r
48         if (ProgressRet != 0){\r
49                 return 1;\r
50         }\r
51         return 0;\r
52 \r
53 }\r
54 \r
55 wxString CardDAV::ServerAddress;\r
56 int CardDAV::ServerPort;\r
57 wxString CardDAV::ServerUser;\r
58 wxString CardDAV::ServerPass;\r
59 wxString CardDAV::ServerPrefix;\r
60 wxString CardDAV::ServerAccount;\r
61 bool CardDAV::ServerSSL;\r
62 bool *CardDAV::ServerResult;\r
63 bool *CardDAV::ServerMonitor;\r
64 bool CardDAV::SSLStatus;\r
65 bool CardDAV::SSLVerified;\r
66 bool CardDAV::ValidResponse;\r
67 bool CardDAV::AuthPassed;\r
68 bool CardDAV::HasCalDAVSupport;\r
69 bool CardDAV::AbortConnection;\r
70 wxString CardDAV::ServerResponse;\r
71 wxString CardDAV::ServerMethod;\r
72 wxString CardDAV::ServerFilenameLocation;\r
73 wxString CardDAV::ServerUploadData;\r
74 wxString CardDAV::ETagData;\r
75 wxString CardDAV::ETagResult;\r
76 bool CardDAV::UploadMode;\r
77 bool CardDAV::EditMode;\r
78 long CardDAV::ItemIndex;\r
79 std::map<int, int> *CardDAV::ActivityListPtr;\r
80 char CardDAV::curlerrbuffer[CURL_ERROR_SIZE];\r
81 SSLCertCollection CardDAV::SSLCertCol;\r
82 int CardDAV::SSLErrorCode;\r
83 int CardDAV::ConnectionErrorCode;\r
84 wxString CardDAV::PageHeader;\r
85 wxString CardDAV::PageData;\r
86 CURLcode CardDAV::claconncode;\r
87 int CardDAV::HTTPErrorCode;\r
88 wxString CardDAV::ErrorMessage;\r
89 SSLCertCollection CardDAV::VerifyCertCollection;\r
90 bool CardDAV::AllowSelfSign;\r
91 \r
92 CardDAV::CardDAV(){\r
93         ServerPort = 8080;\r
94         SSLStatus = FALSE;\r
95         SSLVerified = FALSE;\r
96         AuthPassed = FALSE;\r
97         ValidResponse = FALSE;\r
98         HasCalDAVSupport = FALSE;\r
99         SSLCertCol.SuccessCode = 0;\r
100         AllowSelfSign = FALSE;\r
101 \r
102         AbortConnection = FALSE;\r
103         UploadMode = FALSE;\r
104         EditMode = FALSE;\r
105         HTTPErrorCode = 0;\r
106 }\r
107 \r
108 CardDAV::~CardDAV(){\r
109 \r
110 }\r
111 \r
112 size_t UploadReadFunc(void *ptr, size_t size, size_t nmemb, void *userdata){\r
113 \r
114         struct UploadDataStruc *UploadPtr = (struct UploadDataStruc *)userdata;\r
115 \r
116         if (UploadPtr->sizeleft){\r
117 \r
118                 //MeepMoop->sizeleft--;\r
119                 //return 1;\r
120 \r
121                 UploadPtr->sizeleft--;\r
122                 wxString wxSChar;\r
123 \r
124                 wxSChar = UploadPtr->readptr->Mid(UploadPtr->seek,1);\r
125 \r
126                 //*(char *)ptr = (char)wxSChar.mb_str();\r
127 \r
128                 strncpy((char *)ptr, (const char*)wxSChar.mb_str(wxConvUTF8), 1);\r
129 \r
130                 UploadPtr->seek++;\r
131 \r
132                 return 1;\r
133 \r
134         }\r
135 \r
136         return 0;\r
137 \r
138 }\r
139 \r
140 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL){\r
141 \r
142         ServerAddress = SvrAddress;\r
143         ServerPort = SvrPort;\r
144         ServerUser = SvrUser;\r
145         ServerPass = SvrPass;\r
146         ServerSSL = SvrSSL;\r
147         \r
148         return TRUE;\r
149 \r
150 }\r
151 \r
152 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL, wxString SvrPrefix, wxString SvrAccount){\r
153 \r
154         ServerAddress = SvrAddress;\r
155         ServerPort = SvrPort;\r
156         ServerUser = SvrUser;\r
157         ServerPass = SvrPass;\r
158         ServerSSL = SvrSSL;\r
159         ServerPrefix = SvrPrefix;\r
160         ServerAccount = SvrAccount;\r
161 \r
162         return TRUE;\r
163 \r
164 }\r
165 \r
166 bool CardDAV::SetupResultBools(bool *SvrResult, bool *SvrMonitor){\r
167 \r
168         ServerResult = SvrResult;\r
169         ServerMonitor = SvrMonitor;\r
170 \r
171         return TRUE;\r
172 \r
173 }\r
174 \r
175 bool CardDAV::HasValidResponse(){\r
176 \r
177         return ValidResponse;\r
178 \r
179 }\r
180 \r
181 bool CardDAV::CanDoCardDAV(){\r
182 \r
183         return HasCalDAVSupport;\r
184 \r
185 }\r
186 \r
187 bool CardDAV::CanDoSSL(){\r
188 \r
189         return SSLStatus;\r
190 \r
191 }\r
192 \r
193 bool CardDAV::SSLVerify(){\r
194 \r
195         return SSLVerified;\r
196 \r
197 }\r
198 \r
199 bool CardDAV::AbleToLogin(){\r
200 \r
201         return AuthPassed;\r
202 \r
203 }\r
204 \r
205 bool CardDAV::IsSelfSigned(){\r
206 \r
207         return AllowSelfSign;\r
208 \r
209 }\r
210 \r
211 /*\r
212 \r
213 size_t CardDAV::WritebackFunc(char *ptr, size_t size, size_t nmemb, FILE *stream){\r
214 \r
215         wxString Data;\r
216         Data = wxString::FromUTF8((char *)ptr);\r
217         if ((bool)stream == TRUE){\r
218                 PageHeader.Append(Data);\r
219         } else {\r
220                 PageData.Append(Data);\r
221         }\r
222         return size * nmemb;\r
223 \r
224 }\r
225 \r
226 */\r
227 \r
228 int CardDAV::ProgressFuncProc(void *clientdata, double TTUp, double NUp, double TTDown, double NDown){\r
229 \r
230         if (AbortConnection == TRUE){\r
231         \r
232                 return -1;\r
233         \r
234         } else {\r
235         \r
236                 return 0;\r
237         \r
238         }\r
239 \r
240 }\r
241 \r
242 void CardDAV::Abort(){\r
243 \r
244         AbortConnection = TRUE;\r
245 \r
246 }\r
247 \r
248 SSLCertCollection CardDAV::GetSSLVerifyResults(){\r
249         return VerifyCertCollection;\r
250 }\r
251 \r
252 void CardDAV::AllowSelfSignTest(bool AllowSelfSignIn){\r
253         AllowSelfSign = AllowSelfSignIn;\r
254 }\r
255 \r
256 void CardDAV::GetSSLResults(){\r
257 \r
258         \r
259 \r
260 }\r
261 \r
262 void CardDAV::SetServerFilename(wxString Filename){\r
263 \r
264         ServerFilenameLocation = Filename;\r
265 \r
266 }\r
267 \r
268 wxString CardDAV::GetPageData()\r
269 {\r
270 \r
271         return PageData;\r
272 \r
273 }\r
274 \r
275 wxString CardDAV::ETagValueResult(){\r
276 \r
277         return ETagResult;\r
278 \r
279 }\r
280 \r
281 void CardDAV::SetupData(wxString Method, wxString FilenameLocation, wxString UploadData){\r
282 \r
283         ServerMethod = Method;\r
284         ServerFilenameLocation = FilenameLocation;\r
285         ServerUploadData = UploadData;\r
286 \r
287         // Check if ServerFilenameLocation has a / at \r
288         // the start and if not then append it.\r
289         \r
290         if (ServerFilenameLocation.Left(1) != wxT("/")){\r
291         \r
292                 // Not there so insert.\r
293                 \r
294                 ServerFilenameLocation = wxT("/") + ServerFilenameLocation;\r
295         \r
296         }\r
297 \r
298 }\r
299 \r
300 void CardDAV::SetupVariables(std::map<int, int> *actlist, int actindex){\r
301 \r
302         ActivityListPtr = actlist;\r
303         ItemIndex = actindex;\r
304 \r
305 }\r
306 \r
307 wxString CardDAV::GetETagData(){\r
308 \r
309         return ETagData;\r
310 \r
311 }\r
312 \r
313 void CardDAV::SetUploadMode(bool IncMode){\r
314 \r
315         UploadMode = IncMode;\r
316 \r
317 }\r
318 \r
319 void CardDAV::SetEditMode(bool EditModeInc){\r
320 \r
321         EditMode = EditModeInc;\r
322 \r
323 }\r
324 \r
325 int CardDAV::GetResultCode(){\r
326 \r
327         return (int)claconncode;\r
328 \r
329 }\r
330 \r
331 int CardDAV::GetHTTPCode(){\r
332 \r
333         return HTTPErrorCode;\r
334 \r
335 }\r
336 \r
337 wxString CardDAV::GetErrorBuffer(){\r
338 \r
339         wxString ErrorBuffer = wxString::FromUTF8(curlerrbuffer);\r
340 \r
341         return ErrorBuffer;\r
342 \r
343 }\r
344 \r
345 SSLCertCollection CardDAV::BuildSSLCollection(CURL *conn){\r
346 \r
347         SSLCertCollection SSLCertInfo;\r
348 \r
349         // Grab the certificate data.\r
350 \r
351         union {\r
352                 struct curl_slist *certdata;\r
353                 struct curl_certinfo *certinfo;\r
354         } certptr;\r
355 \r
356         certptr.certdata = NULL;\r
357         \r
358         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &certptr.certinfo);\r
359 \r
360         wxString CertPropName;\r
361         wxString CertPropValue;\r
362 \r
363         for (int i = 0; i < certptr.certinfo->num_of_certs; i++){\r
364 \r
365                 struct curl_slist *slist;\r
366                 SSLCertData SSLCertDataInc;\r
367                 \r
368                 for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){\r
369                         \r
370                         wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");\r
371                         \r
372                         // Get first token as the property name.\r
373                         \r
374                         CertPropName = CertDataInc.GetNextToken();\r
375                         \r
376                         // Get remaining tokens as the property value.\r
377                         \r
378                         while(CertDataInc.HasMoreTokens()){\r
379                         \r
380                                 CertPropValue.Append(CertDataInc.GetNextToken());\r
381                         \r
382                         }\r
383                         \r
384                         SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));\r
385                         CertPropName.clear();\r
386                         CertPropValue.clear();\r
387                         \r
388                 }\r
389         \r
390                 SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));\r
391         \r
392         }\r
393         \r
394         return SSLCertInfo;\r
395 \r
396 }\r
397 \r
398 SSLCertCollection CardDAV::GetCertificateData(){\r
399 \r
400         // Pass on the collected certificate data.\r
401 \r
402         return SSLCertCol;\r
403 \r
404 }\r
405 \r
406 wxString CardDAV::GetErrorMessage(){\r
407 \r
408         return ErrorMessage;\r
409 \r
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