1 // CalDAV.cpp - CalDAV Connection Object.
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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/>
23 size_t CalDAVOutput(char *ReceivedBuffer, size_t Size, size_t NewMemoryBytes, string *StringPointer)
26 string ReceivedBufferString = "";
27 ReceivedBufferString.append(ReceivedBuffer, NewMemoryBytes);
29 StringPointer->append(ReceivedBufferString);
31 return Size * NewMemoryBytes;
37 // Setup the objects within the CalDAV connection
40 ConnectionHandle = curl_easy_init();
46 // Destory the objects within the CalDAV connection
49 curl_easy_cleanup(ConnectionHandle);
50 ConnectionHandle = nullptr;
54 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
56 // Check if ConnData is a nullptr, return if it is.
58 if (ConnData == nullptr){
62 // Set the connection settings to the values from ConnData.
64 ConnectionData = (*ConnData);
68 CalDAVStatus CalDAV::GetConnectionData(){
70 // Get the current connection settings for the CalDAV
71 // connection object and return a CalDAVStatus object.
73 CalDAVStatus ConnectionStatus;
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;
82 return ConnectionStatus;
86 CalDAVServerResult CalDAV::Connect(){
88 CalDAVServerResult ServerResult;
90 string ServerAddress = "";
91 string ServerUserPass = "";
93 // Setup the server address.
95 if (ConnectionData.UseSSL == true){
96 ServerAddress += "https://";
98 ServerAddress += "http://";
101 ServerAddress += ConnectionData.Hostname;
103 // Check if server port is 80, otherwise
104 // specifiy the port number in the address.
106 if (ConnectionData.Port != 80){
107 ServerAddress += ":";
108 ServerAddress += to_string(ConnectionData.Port);
111 ServerAddress += "/principals/";
113 // Setup the server password.
115 ServerUserPass += ConnectionData.Username;
116 ServerUserPass += ":";
117 ServerUserPass += ConnectionData.Password;
119 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, ServerAddress.c_str());
120 curl_easy_setopt(ConnectionHandle, CURLOPT_USERPWD, ServerUserPass.c_str());
121 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
122 curl_easy_setopt(ConnectionHandle, CURLOPT_FAILONERROR, 1L);
123 curl_easy_setopt(ConnectionHandle, CURLOPT_TIMEOUT, ConnectionData.Timeout);
124 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEFUNCTION, CalDAVOutput);
125 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEDATA, &ServerData);
126 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEHEADER, &ServerHeader);
128 // Connect to the CalDAV server.
130 ServerResult.Code = curl_easy_perform(ConnectionHandle);
132 // Process the result received from the server.
134 if (ServerResult.Code != CURLE_OK){
136 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
140 ServerResult.Result = CALDAVQUERYRESULT_OK;
144 // Get the HTTP code.
146 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
152 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
154 // Check if the passed CalDAV Connection Data is has
155 // an address set. Return false if nullptr is used.
157 if (ConnData == nullptr){
163 // Check the server hostname. Return false
164 // if no value has been set.
166 if (ConnData->Hostname.size() == 0){
172 // Check the server port. Return false if
173 // no value has been set or the port number
174 // is less than 1 or higher than 65535.
176 if (ConnData->Port < 1 || ConnData->Port > 65535){
182 // Check the server username. Return false
183 // if no value has been set.
185 if (ConnData->Username.size() == 0){
191 // Check the server password. Return false
192 // if no value has been set.
194 if (ConnData->Password.size() == 0){
200 // Cannot check UseSSL: It is either true
203 // Cannot check Prefix: The prefix may need
204 // to be worked out first.
206 // No errors were found whilst checking so