Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added Timeout setting to CalDAVConnectionData/CalDAVStatus
[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 CalDAVOutput(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 CalDAV::CalDAV(){
37         // Setup the objects within the CalDAV connection 
38         // object.
39         
40         ConnectionHandle = curl_easy_init();
41         
42 }
44 CalDAV::~CalDAV(){
45         
46         // Destory the objects within the CalDAV connection
47         // object.
48         
49         curl_easy_cleanup(ConnectionHandle);
50         ConnectionHandle = nullptr;
51         
52 }
54 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
55         
56         // Check if ConnData is a nullptr, return if it is.
57         
58         if (ConnData == nullptr){
59                 return;
60         }
61         
62         // Set the connection settings to the values from ConnData.
64         ConnectionData = (*ConnData);
65         
66 }
68 CalDAVStatus CalDAV::GetConnectionData(){
69         
70         // Get the current connection settings for the CalDAV
71         // connection object and return a CalDAVStatus object.
72         
73         CalDAVStatus ConnectionStatus;
74         
75         ConnectionStatus.Hostname = ConnectionData.Hostname;
76         ConnectionStatus.Port = ConnectionData.Port;
77         ConnectionStatus.Username = ConnectionData.Username;
78         ConnectionStatus.Prefix = ConnectionData.Prefix;
79         ConnectionStatus.UseSSL = ConnectionData.UseSSL;
80         ConnectionStatus.Timeout = ConnectionData.Timeout;
81         
82         return ConnectionStatus;
83         
84 }
86 CalDAVServerResult CalDAV::Connect(){
88         CalDAVServerResult ServerResult;
90         string ServerAddress = "";
91         string ServerUserPass = "";
92         string ServerData = "";
93         string ServerHeader = "";
95         // Setup the server address.
96         
97         if (ConnectionData.UseSSL == true){
98                 ServerAddress += "https://";
99         } else {
100                 ServerAddress += "http://";
101         }
102         
103         ServerAddress += ConnectionData.Hostname;
104         
105         // Check if server port is 80, otherwise
106         // specifiy the port number in the address.
107         
108         if (ConnectionData.Port != 80){
109                 ServerAddress += ":";
110                 ServerAddress += to_string(ConnectionData.Port);
111         }
112         
113         ServerAddress += "/principals/";
115         // Setup the server password.
116         
117         ServerUserPass += ConnectionData.Username;
118         ServerUserPass += ":";
119         ServerUserPass += ConnectionData.Password;
120         
121         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, ServerAddress.c_str());
122         curl_easy_setopt(ConnectionHandle, CURLOPT_USERPWD, ServerUserPass.c_str());
123         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
124         curl_easy_setopt(ConnectionHandle, CURLOPT_FAILONERROR, 1L);
125         curl_easy_setopt(ConnectionHandle, CURLOPT_TIMEOUT, ConnectionData.Timeout);
126         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEFUNCTION, CalDAVOutput);
127         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEDATA, &ServerData);
128         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEHEADER, &ServerHeader);
129         
130         // Connect to the CalDAV server.
131         
132         ServerResult.Code = curl_easy_perform(ConnectionHandle);
134         // Process the result received from the server.
135         
136         if (ServerResult.Code != CURLE_OK){
137                 
138                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
139                 
140         } else {
141                 
142                 ServerResult.Result = CALDAVQUERYRESULT_OK;
143                 
144         }
145         
146         // Get the HTTP code.
147         
148         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
149         
150         return ServerResult;
151         
154 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
156         // Check if the passed CalDAV Connection Data is has
157         // an address set. Return false if nullptr is used.
159         if (ConnData == nullptr){
160         
161                 return false;
162         
163         }
164         
165         // Check the server hostname. Return false
166         // if no value has been set.
167         
168         if (ConnData->Hostname.size() == 0){
169         
170                 return false;
171         
172         }
173         
174         // Check the server port. Return false if
175         // no value has been set or the port number
176         // is less than 1 or higher than 65535.
177         
178         if (ConnData->Port < 1 || ConnData->Port > 65535){
179         
180                 return false;
181         
182         }
183         
184         // Check the server username. Return false
185         // if no value has been set.
186         
187         if (ConnData->Username.size() == 0){
188                 
189                 return false;
190                 
191         }       
192         
193         // Check the server password. Return false
194         // if no value has been set.
195         
196         if (ConnData->Password.size() == 0){
197                 
198                 return false;
199                 
200         }
202         // Cannot check UseSSL: It is either true
203         // or false.
204         
205         // Cannot check Prefix: The prefix may need
206         // to be worked out first.
208         // No errors were found whilst checking so
209         // return true.
210         
211         return true;
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