+// CalendarDataStorage.cpp - CalendarDataStorage class
+//
+// (c) 2016-2017 Xestia Software Development.
+//
+// This file is part of Xestia Calendar.
+//
+// Xestia Calendar is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Calendar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
+
#include "CalendarDataStorage.h"
-#include <iostream>
+#define CDS_RANDOMPOW 24
using namespace std;
// Setup the accounts table.
const char *accountsTableString;
- accountsTableString = "CREATE TABLE accounts(id INTEGER PRIMARY KEY "
+ accountsTableString = "CREATE TABLE accounts(id INTEGER PRIMARY KEY AUTOINCREMENT"
+ ", preferencesid INTEGER"
", name TEXT"
");";
// Setup the calendars table.
const char *calendarTableString;
- calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY "
+ calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY AUTOINCREMENT"
", accountid INTEGER"
", name TEXT"
", calendarid TEXT"
", colour TEXT"
+ ", description TEXT"
");";
resultCode = sqlite3_exec(db, calendarTableString, callback, nullptr, &setupTablesErrMsg);
// Setup the calendar entries table.
const char *calendarentriesTableString;
- calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY "
+ calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY AUTOINCREMENT"
", calendarid INTEGER"
", entryname TEXT"
", entrydescription TEXT"
", entrydurationhour INTEGER"
", entrydurationminute INTEGER"
", entrydurationsecond INTEGER"
+ ", filename TEXT"
");";
resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg);
// Setup the checksums table.
const char *checksumsTableString;
- checksumsTableString = "CREATE TABLE checksums(hashname TEXT PRIMARY KEY "
- ", hashvalue TEXT);";
+ checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
+ ", checksumvalue TEXT);";
resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
return;
}
+ // Setup internal checksums.
+
+ CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
+
+ if (addChecksum != CDSCHECKSUM_OK){
+ DataStorageInitOK = false;
+ return;
+ }
+
DataStorageInitOK = true;
}
}
-CDSAccountResult CalendarDataStorage::AddAccount(string accountName)
+CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int accountPreferencesID)
{
CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL;
int resultCode;
sqlite3_stmt *statementHandle;
+ sqlite3_stmt *findHandle;
+
+ if (accountName == ""){
+ return CDSACCOUNT_NONAME;
+ }
+
+ // Check if the account name already exsits.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE name=(?1);", -1, &findHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSACCOUNT_FAILED;
+ }
- resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name) VALUES(?1);", -1, &statementHandle, nullptr);
+ resultCode = sqlite3_bind_text(findHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSACCOUNT_FAILED;
+ }
+
+ resultCode = sqlite3_step(findHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSACCOUNT_FAILED;
+ } else if (resultCode == SQLITE_ROW){
+ return CDSACCOUNT_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr);
if (resultCode != 0){
return CDSACCOUNT_FAILED;
resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
+ if (resultCode != 0){
+ return CDSACCOUNT_FAILED;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 2, accountPreferencesID);
+
if (resultCode != 0){
return CDSACCOUNT_FAILED;
}
return CDSACCOUNT_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
addResult = CDSACCOUNT_OK;
return addResult;
sqlite3_stmt *statementHandle;
- resultCode = sqlite3_prepare_v2(db, "SELECT id, name from accounts;", -1, &statementHandle, nullptr);
+ resultCode = sqlite3_prepare_v2(db, "SELECT id, name, preferencesid from accounts;", -1, &statementHandle, nullptr);
if (resultCode != 0){
accountList.getAccountListResult = CDSACCOUNT_FAILED;
CDSGetAccountInfo accountInfo;
accountInfo.accountID = sqlite3_column_int(statementHandle, 0);
+ accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
stringstream calendarStream;
sqlite3_stmt *statementHandle;
- resultCode = sqlite3_prepare_v2(db, "SELECT name, id FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
+ resultCode = sqlite3_prepare_v2(db, "SELECT name, id, preferencesid FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
if (resultCode != 0){
accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
accountInfo.accountName = accountNameStream.str();
accountInfo.accountID = sqlite3_column_int(statementHandle, 1);
+ accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
accountInfo.accountInfoResult = CDSACCOUNT_OK;
return accountInfo;
int resultCode;
sqlite3_stmt *findHandle;
+ sqlite3_stmt *existingHandle;
sqlite3_stmt *statementHandle;
+ if (accountName == ""){
+ return CDSACCOUNT_NONAME;
+ }
+
// Check if account exists first.
resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
return CDSACCOUNT_FAILED;
}
+ // Check if account with the name given already exists before renaming.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT name from accounts WHERE name=(?1);", -1, &existingHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSACCOUNT_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(existingHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSACCOUNT_FAILED;
+ }
+
+ resultCode = sqlite3_step(existingHandle);
+
+ if (resultCode == SQLITE_ROW){
+ return CDSACCOUNT_FAILED;
+ } else if (resultCode == SQLITE_DONE) {
+
+ } else {
+ return CDSACCOUNT_FAILED;
+ }
+
// Update the account.
resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
return CDSACCOUNT_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
updateResult = CDSACCOUNT_OK;
return updateResult;
} else if (resultCode == SQLITE_DONE) {
return CDSACCOUNT_NOACCOUNT;
} else {
- return CDSACCOUNT_FAILED;
+
}
// Delete the account.
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
deleteResult = CDSACCOUNT_OK;
return deleteResult;
}
-CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour)
+CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour, string calendarDescription)
{
CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
int resultCode;
-
+
+ sqlite3_stmt *findHandle;
sqlite3_stmt *statementHandle;
- resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour) VALUES(?1, ?2, ?3, ?4);", -1, &statementHandle, nullptr);
+ // Check if the account exists first.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id FROM accounts WHERE id=(?1);", -1, &findHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCALENDAR_FAILED;
+ }
+
+ resultCode = sqlite3_bind_int(findHandle, 1, accountID);
+
+ if (resultCode != 0){
+ return CDSCALENDAR_FAILED;
+ }
+
+ resultCode = sqlite3_step(findHandle);
+
+ if (resultCode != SQLITE_ROW){
+ return CDSCALENDAR_NOACCOUNT;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour, description) VALUES(?1, ?2, ?3, ?4, ?5);", -1, &statementHandle, nullptr);
if (resultCode != 0){
return CDSCALENDAR_FAILED;
return CDSCALENDAR_FAILED;
}
- resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).c_str(), -1, SQLITE_STATIC);
+ string calendarColourString = (string)calendarColour;
+ resultCode = sqlite3_bind_text(statementHandle, 4, calendarColourString.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCALENDAR_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 5, calendarDescription.c_str(), -1, SQLITE_STATIC);
if (resultCode != 0){
return CDSCALENDAR_FAILED;
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
addResult = CDSCALENDAR_OK;
return addResult;
// Check if the calendar exists.
- resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
+ resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
if (resultCode != 0){
calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
calendarStream << sqlite3_column_text(statementHandle, 4);
calendarResult.calendarColour = calendarStream.str();
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 5);
+ calendarResult.calendarDescription = calendarStream.str();
+
+ calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
+ calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
+ calendarResult.calendarInfoResult = CDSCALENDAR_OK;
+ return calendarResult;
+
+ } else {
+
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+
+ }
+
+ return calendarResult;
+
+}
+
+CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
+{
+
+ CDSGetCalendarInfo calendarResult;
+ sqlite3_stmt *statementHandle;
+ sqlite3_stmt *accountHandle;
+ int resultCode;
+
+ // Check if the calendar exists.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode == SQLITE_DONE){
+
+ calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
+ return calendarResult;
+
+ } else if (resultCode == SQLITE_ROW){
+
+ // Get the calendar data.
+
+ stringstream calendarStream;
+
+ calendarStream << sqlite3_column_text(statementHandle, 2);
+ calendarResult.calendarName = calendarStream.str();
+
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 3);
+ calendarResult.calendarTextID = calendarStream.str();
+
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 4);
+ calendarResult.calendarColour = calendarStream.str();
+
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 5);
+ calendarResult.calendarDescription = calendarStream.str();
+
calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
+
+ // Get the account name.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE id=(?1);", -1, &accountHandle, nullptr);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_bind_int(accountHandle, 1, calendarResult.accountID);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_step(accountHandle);
+
+ if (resultCode == SQLITE_ROW){
+
+ stringstream accountStream;
+ accountStream << sqlite3_column_text(accountHandle, 0);
+ calendarResult.accountName = accountStream.str();
+ calendarResult.accountInfoResult = CDSACCOUNT_OK;
+
+ } else {
+
+ calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
+
+ }
+
calendarResult.calendarInfoResult = CDSCALENDAR_OK;
return calendarResult;
}
-CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName)
+
+CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName, Colour calendarColour, std::string calendarDescription)
{
CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
// Update the account.
- resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
+ resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1), colour=(?2), description=(?3) WHERE id=(?4);", -1, &statementHandle, nullptr);
if (resultCode != 0){
return CDSCALENDAR_FAILED;
resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
+ if (resultCode != 0){
+ return CDSCALENDAR_FAILED;
+ }
+
+ string calendarColourString = calendarColour;
+ resultCode = sqlite3_bind_text(statementHandle, 2, calendarColourString.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCALENDAR_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 3, calendarDescription.c_str(), -1, SQLITE_STATIC);
+
if (resultCode != 0){
return CDSCALENDAR_FAILED;
}
- resultCode = sqlite3_bind_int(statementHandle, 2, calendarID);
+ resultCode = sqlite3_bind_int(statementHandle, 4, calendarID);
if (resultCode != 0){
return CDSCALENDAR_FAILED;
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
updateResult = CDSCALENDAR_OK;
return updateResult;
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return deleteResult;
}
// Start Date.
- if (eventData.DateTimeStartData.size() < 16){
+ if (eventData.dateTimeStartData.size() < 16){
addResult.addEventResult = CDSENTRY_INVALIDFILE;
return addResult;
}
- eventString = eventData.DateTimeStartData.substr(0,4);
+ eventString = eventData.dateTimeStartData.substr(0,4);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeStartData.substr(4,2);
+ eventString = eventData.dateTimeStartData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeStartData.substr(6,2);
+ eventString = eventData.dateTimeStartData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeStartData.substr(9,2);
+ eventString = eventData.dateTimeStartData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeStartData.substr(11,2);
+ eventString = eventData.dateTimeStartData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeStartData.substr(13,2);
+ eventString = eventData.dateTimeStartData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
int eventEndSecond = 0;
int eventEndDuration = 0;
- if (eventData.DateTimeEndData != ""){
+ if (eventData.dateTimeEndData != ""){
- if (eventData.DateTimeEndData.size() < 16){
+ if (eventData.dateTimeEndData.size() < 16){
addResult.addEventResult = CDSENTRY_INVALIDFILE;
return addResult;
}
- eventString = eventData.DateTimeEndData.substr(0,4);
+ eventString = eventData.dateTimeEndData.substr(0,4);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeEndData.substr(4,2);
+ eventString = eventData.dateTimeEndData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeEndData.substr(6,2);
+ eventString = eventData.dateTimeEndData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeEndData.substr(9,2);
+ eventString = eventData.dateTimeEndData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeEndData.substr(11,2);
+ eventString = eventData.dateTimeEndData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DateTimeEndData.substr(13,2);
+ eventString = eventData.dateTimeEndData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
}
- eventString = eventData.DurationData;
+ eventString = eventData.durationData;
// Process the duration data.
// Get the duration (if DTEND hasn't been specified).
- if (eventData.DurationData.size() > 0){
+ if (eventData.durationData.size() > 0){
bool FoundP = false;
bool FoundW = false;
bool DateTimeMode = false;
- std::string::iterator eventDataChar = eventData.DurationData.begin();
+ std::string::iterator eventDataChar = eventData.durationData.begin();
std::string currentValue = "";
if (*eventDataChar != 'P'){
- eventDataChar = eventData.DurationData.end();
+ eventDataChar = eventData.durationData.end();
}
- for(eventDataChar; eventDataChar != eventData.DurationData.end(); eventDataChar++){
+ for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
// Check if value is a digit.
std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
" entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
" entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
- " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond)"
+ " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
+ " filename)"
" VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
- " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20))";
+ " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20), (?21))";
resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
// Process Entry Name.
- resultCode = sqlite3_bind_text(statementHandle, 2, eventData.SummaryData.c_str(), -1, SQLITE_STATIC);
+ resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
if (resultCode != 0){
addResult.addEventResult = CDSENTRY_FAILED;
string eventDescription;
try {
- eventDescription = eventData.DescriptionList.at(0);
+ eventDescription = eventData.descriptionList.at(0);
}
catch (out_of_range &err){
return addResult;
}
+ resultCode = sqlite3_bind_text(statementHandle, 21, filename.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ addResult.addEventResult = CDSENTRY_FAILED;
+ return addResult;
+ }
+
resultCode = sqlite3_step(statementHandle);
if (resultCode != SQLITE_DONE){
addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
addResult.addEventResult = CDSENTRY_OK;
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return addResult;
}
-CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
+CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string filename)
{
- CDSGetCalendarEntryInfo entryResult;
+ CDSEditEntryResult editResult;
+ editResult.editEventResult = CDSENTRY_UNITTESTFAIL;
- // Check if the calendar entry exists.
+ // Load the event file.
+
+ CalendarEventObject eventData;
+ CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
+
+ // Check the result of the event file load.
+
+ switch (eventLoadResult){
+
+ case CALENDAROBJECTLOAD_OK:
+ break;
+ case CALENDAROBJECTLOAD_MISSING:
+ editResult.editEventResult = CDSENTRY_MISSINGFILE;
+ return editResult;
+ case CALENDAROBJECTLOAD_INVALIDFORMAT:
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+ case CALENDAROBJECTLOAD_CANNOTOPEN:
+ editResult.editEventResult = CDSENTRY_CANNOTOPENFILE;
+ return editResult;
+
+ }
+
+ // Check if event exists first.
int resultCode;
sqlite3_stmt *findHandle;
sqlite3_stmt *statementHandle;
- resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
+ resultCode = sqlite3_prepare_v2(db, "SELECT id from calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
if (resultCode != 0){
- entryResult.getEventResult = CDSENTRY_FAILED;
- return entryResult;
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
}
- resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
+ resultCode = sqlite3_bind_int(findHandle, 1, eventID);
if (resultCode != 0){
- entryResult.getEventResult = CDSENTRY_FAILED;
- return entryResult;
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
}
resultCode = sqlite3_step(findHandle);
if (resultCode == SQLITE_ROW){
} else if (resultCode == SQLITE_DONE) {
- entryResult.getEventResult = CDSENTRY_NOENTRY;
- return entryResult;
+ editResult.editEventResult = CDSENTRY_NOENTRY;
+ return editResult;
} else {
- entryResult.getEventResult = CDSENTRY_FAILED;
- return entryResult;
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
}
- // Get the calendar entry data.
-
- std::string sqlParameter = "SELECT entryname, entrydescription,"
- " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
- " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
- " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond "
- " FROM calendarentries WHERE id=(?1)";
+ // Get the required values from the event object.
- resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
-
- if (resultCode != 0){
- entryResult.getEventResult = CDSENTRY_FAILED;
- return entryResult;
- }
+ int eventStartYear = 0;
+ int eventStartMonth = 0;
+ int eventStartDay = 0;
+ int eventStartHour = 0;
+ int eventStartMinute = 0;
+ int eventStartSecond = 0;
+ int eventStartDuration = 0;
+ std::string eventString = "";
- resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
+ // Start Date.
- if (resultCode != 0){
- entryResult.getEventResult = CDSENTRY_FAILED;
- return entryResult;
+ if (eventData.dateTimeStartData.size() < 16){
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(0,4);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartYear = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(4,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartMonth = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(6,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartDay = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(9,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartHour = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(11,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartMinute = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeStartData.substr(13,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventStartSecond = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ //eventYear = eventStartDate.substr(0, 4);
+
+ // End Date.
+
+ int eventEndYear = 0;
+ int eventEndMonth = 0;
+ int eventEndDay = 0;
+ int eventEndHour = 0;
+ int eventEndMinute = 0;
+ int eventEndSecond = 0;
+ int eventEndDuration = 0;
+
+ if (eventData.dateTimeEndData != ""){
+
+ if (eventData.dateTimeEndData.size() < 16){
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(0,4);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndYear = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(4,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndMonth = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(6,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndDay = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(9,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndHour = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(11,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndMinute = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ eventString = eventData.dateTimeEndData.substr(13,2);
+
+ if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
+
+ eventEndSecond = atoi(eventString.c_str());
+
+ } else {
+
+ editResult.editEventResult = CDSENTRY_INVALIDFILE;
+ return editResult;
+
+ }
+
+ }
+
+ eventString = eventData.durationData;
+
+ // Process the duration data.
+
+ int eventDurationWeeks = 0;
+ int eventDurationDays = 0;
+ int eventDurationHours = 0;
+ int eventDurationMinutes = 0;
+ int eventDurationSeconds = 0;
+
+ // Get the duration (if DTEND hasn't been specified).
+
+ if (eventData.durationData.size() > 0){
+
+ bool FoundP = false;
+ bool FoundW = false;
+ bool DateTimeMode = false;
+
+ std::string::iterator eventDataChar = eventData.durationData.begin();
+ std::string currentValue = "";
+
+ if (*eventDataChar != 'P'){
+
+ eventDataChar = eventData.durationData.end();
+
+ }
+
+ for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
+
+ // Check if value is a digit.
+
+ if (isdigit(*eventDataChar)){
+
+ currentValue += *eventDataChar;
+
+ } else {
+
+ // Check that the value matches one of the letters.
+
+ if (*eventDataChar == 'W' && DateTimeMode == false){
+
+ eventDurationWeeks = atoi(currentValue.c_str());
+
+ } else if (*eventDataChar == 'D' && DateTimeMode == false){
+
+ eventDurationDays = atoi(currentValue.c_str());
+
+ } else if (*eventDataChar == 'T' && DateTimeMode == false){
+
+ DateTimeMode = true;
+
+ } else if (*eventDataChar == 'H'){
+
+ eventDurationHours = atoi(currentValue.c_str());
+
+ } else if (*eventDataChar == 'M'){
+
+ eventDurationMinutes = atoi(currentValue.c_str());
+
+ } else if (*eventDataChar == 'S'){
+
+ eventDurationSeconds = atoi(currentValue.c_str());
+
+ }
+
+ // Reset the current value.
+
+ currentValue = "";
+
+ }
+
+ }
+
+ }
+
+ // Add the calendar entry.
+
+ std::string sqlParameter = "UPDATE calendarentries SET entryname=(?2), entrydescription=(?3),"
+ " entrystartyear=(?4), entrystartmonth=(?5), entrystartday=(?6), entrystarthour=(?7), entrystartminute=(?8), entrystartsecond=(?9),"
+ " entryendyear=(?10), entryendmonth=(?11), entryendday=(?12), entryendhour=(?13), entryendminute=(?14), entryendsecond=(?15), "
+ " entrydurationweek=(?16), entrydurationday=(?17), entrydurationhour=(?18), entrydurationminute=(?19), entrydurationsecond=(?20) "
+ " WHERE id=(?1)";
+
+ resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
+
+ resultCode = sqlite3_bind_int(statementHandle, 1, eventID);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ // Process Entry Name.
+
+ resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ // Process Entry Description.
+
+ string eventDescription;
+
+ try {
+ eventDescription = eventData.descriptionList.at(0);
+ }
+
+ catch (out_of_range &err){
+ eventDescription = "";
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ // Process Entry Start Date information.
+
+ resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ // Process Entry Start End information.
+
+ resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
+
+ if (resultCode != 0){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ editResult.editEventResult = CDSENTRY_FAILED;
+ return editResult;
+ }
+
+ editResult.calendarEntryID = sqlite3_last_insert_rowid(db);
+ editResult.editEventResult = CDSENTRY_OK;
+
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
+ return editResult;
+
+}
+
+CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
+{
+
+ CDSGetCalendarEntryInfo entryResult;
+
+ // Check if the calendar entry exists.
+
+ int resultCode;
+
+ sqlite3_stmt *findHandle;
+ sqlite3_stmt *statementHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
+
+ if (resultCode != 0){
+ entryResult.getEventResult = CDSENTRY_FAILED;
+ return entryResult;
+ }
+
+ resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
+
+ if (resultCode != 0){
+ entryResult.getEventResult = CDSENTRY_FAILED;
+ return entryResult;
+ }
+
+ resultCode = sqlite3_step(findHandle);
+
+ if (resultCode == SQLITE_ROW){
+
+ } else if (resultCode == SQLITE_DONE) {
+ entryResult.getEventResult = CDSENTRY_NOENTRY;
+ return entryResult;
+ } else {
+ entryResult.getEventResult = CDSENTRY_FAILED;
+ return entryResult;
+ }
+
+ // Get the calendar entry data.
+
+ std::string sqlParameter = "SELECT entryname, entrydescription,"
+ " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
+ " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
+ " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
+ " calendarid, id, filename"
+ " FROM calendarentries WHERE id=(?1)";
+
+ resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ entryResult.getEventResult = CDSENTRY_FAILED;
+ return entryResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
+
+ if (resultCode != 0){
+ entryResult.getEventResult = CDSENTRY_FAILED;
+ return entryResult;
}
resultCode = sqlite3_step(statementHandle);
entryStream.str("");
+ // Get the calendar entry filename.
+
+ entryStream << sqlite3_column_text(statementHandle, 21);
+ entryResult.entryFilename = entryStream.str();
+
+ entryStream.str("");
+
entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
+ entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
+ entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
} else if (resultCode == SQLITE_DONE) {
entryResult.getEventResult = CDSENTRY_NOCALENDAR;
return CDSENTRY_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return deleteResult;
}
}
+CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
+
+ CDSEntryList entryList;
+ entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
+
+ // Check if calendar exists first.
+
+ int resultCode;
+
+ sqlite3_stmt *calendarHandle;
+
+ // Get the list of entry IDs.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_step(calendarHandle);
+
+ if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
+ } else {
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ while (resultCode == SQLITE_ROW){
+
+ int calendarID = sqlite3_column_int(calendarHandle, 0);
+
+ entryList.entryList.push_back(calendarID);
+
+ resultCode = sqlite3_step(calendarHandle);
+
+ }
+
+ entryList.getEventListResult = CDSENTRY_OK;
+
+ return entryList;
+
+}
+
CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
CDSCalendarList calendarList;
return calendarList;
+}
+
+CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *findHandle;
+ sqlite3_stmt *checksumHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(findHandle);
+
+ if (resultCode == SQLITE_ROW){
+ return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
+ } else if (resultCode == SQLITE_DONE) {
+
+ } else {
+ return CDSCHECKSUM_FAILED;
+ }
+
+ // Add the checksum to the checksum table.
+
+ resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(checksumHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ return CDSCHECKSUM_OK;
+
+}
+
+CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
+
+ CDSGetChecksumResult getChecksumResult;
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *getHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
+
+ if (resultCode != 0){
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ resultCode = sqlite3_step(getHandle);
+
+ if (resultCode == SQLITE_ROW){
+ } else if (resultCode == SQLITE_DONE) {
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
+ return getChecksumResult;
+ } else {
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ stringstream checksumStream;
+
+ checksumStream << sqlite3_column_text(getHandle, 0);
+
+ getChecksumResult.checksumValue = checksumStream.str();
+
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
+
+ return getChecksumResult;
+
+}
+
+CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *getHandle;
+ sqlite3_stmt *statementHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(getHandle);
+
+ if (resultCode == SQLITE_ROW){
+ } else if (resultCode == SQLITE_DONE) {
+ return CDSCHECKSUM_NOHASH;
+ } else {
+ return CDSCHECKSUM_FAILED;
+ }
+
+ // Update the checksum.
+
+ resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ return CDSCHECKSUM_OK;
+
+}
+
+CDSCleanupResult CalendarDataStorage::Clear(){
+
+ // Remove all data from the tables and reset the sequence numbers.
+
+ int resultCode;
+ sqlite3_stmt *statementHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries", - 1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendarentries';", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendars';", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='accounts'", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCLEANUP_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCLEANUP_FAILED;
+ }
+
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
+ return CDSCLEANUP_OK;
+
}
\ No newline at end of file