From 1ee0816458ce63e6bd689ade74295096b037c116 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 17 Apr 2016 21:36:22 +0100 Subject: [PATCH] Added the AddCalendar function. Added the AddCalendar function to the CalDAV object. Unit tests also done for the function. --- source/objects/CalDAV/CalDAV.cpp | 91 ++++++++++++++++++++++++++++ source/objects/CalDAV/CalDAV.h | 2 + source/tests/xestiacalendar_caldav.h | 50 +++++++++++++++ 3 files changed, 143 insertions(+) diff --git a/source/objects/CalDAV/CalDAV.cpp b/source/objects/CalDAV/CalDAV.cpp index 51a1ec6..e2a787d 100644 --- a/source/objects/CalDAV/CalDAV.cpp +++ b/source/objects/CalDAV/CalDAV.cpp @@ -534,6 +534,97 @@ CalDAVCalendarList CalDAV::GetCalendars(){ } +CalDAVServerResult CalDAV::AddCalendar(string CalendarName){ + + CalDAVServerResult ServerResult; + CalDAVSendData CalendarAddSendData; + + // Build the server address. + + string UserPrincipalURI = ""; + UserPrincipalURI = GetUserPrincipal(); + + if (UserPrincipalURI.size() == 0){ + + return ServerResult; + + } + + string CalendarHomeURI = ""; + CalendarHomeURI = GetCalendarHome(UserPrincipalURI); + + // Generate the UUID. + + string UUIDValue = GenerateUUID(); + UUIDValue.erase(UUIDValue.end()-1); + + string CalendarHomeURL = CalendarHomeURI; + CalendarHomeURL.append(UUIDValue); + CalendarHomeURL.append("/"); + + // Build the calendar list address. + + string CalendarListURLAddress = BuildServerAddress(&ConnectionData, CalendarHomeURL); + + string CalendarAddRequest = "\n" + "\n" + " \n" + " \n" + " "; + CalendarAddRequest += CalendarName; + CalendarAddRequest += "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + ""; + + CalendarAddSendData.readptr = &CalendarAddRequest; + CalendarAddSendData.sizeleft = CalendarAddRequest.size(); + + // Setup the header. + + struct curl_slist *CalendarRequestHeader = NULL; + + cout << CalendarListURLAddress << endl; + + //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader); + curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarListURLAddress.c_str()); + curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "MKCALENDAR"); + curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarAddSendData); + curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend); + + // Process the data. + + ServerData.clear(); + ServerHeader.clear(); + + CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle); + + if (ServerConnectionResult == CURLE_OK){ + ServerResult.Result = CALDAVQUERYRESULT_OK; + } else { + ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR; + } + ServerResult.Code = ServerConnectionResult; + curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode); + + // Restore the original settings. + + string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals"); + curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str()); + curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL); + curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L); + curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL); + curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL); + + return ServerResult; + +} + bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){ // Check if the passed CalDAV Connection Data is has diff --git a/source/objects/CalDAV/CalDAV.h b/source/objects/CalDAV/CalDAV.h index aee045a..cbe065a 100644 --- a/source/objects/CalDAV/CalDAV.h +++ b/source/objects/CalDAV/CalDAV.h @@ -29,6 +29,7 @@ #include #include "../../common/colour.h" #include "../../common/text.h" +#include "../../common/uuid.h" using namespace std; @@ -141,6 +142,7 @@ class CalDAV{ CalDAVServerResult GetServerResult(); CalDAVServerSupport GetServerSupport(); CalDAVCalendarList GetCalendars(); + CalDAVServerResult AddCalendar(string CalendarName); string GetUserPrincipal(); string GetCalendarHome(string UserPrincipalURI); diff --git a/source/tests/xestiacalendar_caldav.h b/source/tests/xestiacalendar_caldav.h index 8c9860b..2e5d9cb 100644 --- a/source/tests/xestiacalendar_caldav.h +++ b/source/tests/xestiacalendar_caldav.h @@ -501,4 +501,54 @@ TEST(CalDAV, ListCalendars){ ASSERT_EQ(207, ConnResult.HTTPCode); ASSERT_EQ(CURLE_OK, ConnResult.Code); +} + +TEST(CalDAV, AddCalendar){ + + CalDAVConnectionData ConnNormal; + string CurrentUserPrincipal; + + bool ValidDataNormal = false; + + // Attempt to read the caldavtest.auth file. + + ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal); + if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){ + ValidDataNormal = true; + } + + ASSERT_EQ(true, ValidDataNormal); + + // Setup the connection. + + CalDAV ServerConnection; + + ServerConnection.SetupConnectionData(&ConnNormal); + + // Verify the connection settings. + + CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData(); + + ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname); + ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username); + ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port); + ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix); + ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL); + + // Connect to the server. + + CalDAVServerResult ConnResult = ServerConnection.Connect(); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(200, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Add a calendar to the server. + + ConnResult = ServerConnection.AddCalendar("New Calendar"); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(201, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + } \ No newline at end of file -- 2.39.2