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