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/>
26 // Setup the objects within the CalDAV connection
29 ConnectionHandle = curl_easy_init();
35 // Destory the objects within the CalDAV connection
38 curl_easy_cleanup(ConnectionHandle);
39 ConnectionHandle = nullptr;
43 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
45 // Check if ConnData is a nullptr, return if it is.
47 if (ConnData == nullptr){
51 // Set the connection settings to the values from ConnData.
53 ConnectionData = (*ConnData);
57 CalDAVStatus CalDAV::GetConnectionData(){
59 // Get the current connection settings for the CalDAV
60 // connection object and return a CalDAVStatus object.
62 CalDAVStatus ConnectionStatus;
64 ConnectionStatus.Hostname = ConnectionData.Hostname;
65 ConnectionStatus.Port = ConnectionData.Port;
66 ConnectionStatus.Username = ConnectionData.Username;
67 ConnectionStatus.Prefix = ConnectionData.Prefix;
68 ConnectionStatus.UseSSL = ConnectionData.UseSSL;
70 return ConnectionStatus;
74 CalDAVServerResult CalDAV::Connect(){
76 CalDAVServerResult ServerResult;
78 string ServerAddress = "";
79 string ServerUserPass = "";
81 // Setup the server address.
83 if (ConnectionData.UseSSL == true){
84 ServerAddress += "https://";
86 ServerAddress += "http://";
89 ServerAddress += ConnectionData.Hostname;
91 // Check if server port is 80, otherwise
92 // specifiy the port number in the address.
94 if (ConnectionData.Port != 80){
96 ServerAddress += to_string(ConnectionData.Port);
99 ServerAddress += "/principals/";
101 // Setup the server password.
103 ServerUserPass += ConnectionData.Username;
104 ServerUserPass += ":";
105 ServerUserPass += ConnectionData.Password;
107 curl_easy_setopt(ConnectionHandle, CURLOPT_URL, ServerAddress.c_str());
108 curl_easy_setopt(ConnectionHandle, CURLOPT_USERPWD, ServerUserPass.c_str());
109 curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
110 curl_easy_setopt(ConnectionHandle, CURLOPT_FAILONERROR, 1L);
112 // Connect to the CalDAV server.
114 ServerResult.Code = curl_easy_perform(ConnectionHandle);
116 // Process the result received from the server.
118 if (ServerResult.Code != CURLE_OK){
120 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
124 ServerResult.Result = CALDAVQUERYRESULT_OK;
128 // Get the HTTP code.
130 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
136 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
138 // Check if the passed CalDAV Connection Data is has
139 // an address set. Return false if nullptr is used.
141 if (ConnData == nullptr){
147 // Check the server hostname. Return false
148 // if no value has been set.
150 if (ConnData->Hostname.size() == 0){
156 // Check the server port. Return false if
157 // no value has been set or the port number
158 // is less than 1 or higher than 65535.
160 if (ConnData->Port < 1 || ConnData->Port > 65535){
166 // Check the server username. Return false
167 // if no value has been set.
169 if (ConnData->Username.size() == 0){
175 // Check the server password. Return false
176 // if no value has been set.
178 if (ConnData->Password.size() == 0){
184 // Cannot check UseSSL: It is either true
187 // Cannot check Prefix: The prefix may need
188 // to be worked out first.
190 // No errors were found whilst checking so