From: Steve Brokenshire Date: Wed, 11 Jan 2017 19:58:14 +0000 (+0000) Subject: CalendarDataStorage: Update code X-Git-Tag: release-0.02~24 X-Git-Url: http://Server1/repobrowser/?a=commitdiff_plain;h=e302a149a147c693cff6ca8bed0a0d8cb856e5b5;p=xestiacalendar%2F.git CalendarDataStorage: Update code --- diff --git a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp index a47a354..b1d2c5b 100644 --- a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp +++ b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp @@ -37,7 +37,8 @@ void CalendarDataStorage::SetupTables(){ // 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" ");"; @@ -51,11 +52,12 @@ void CalendarDataStorage::SetupTables(){ // 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); @@ -68,7 +70,7 @@ void CalendarDataStorage::SetupTables(){ // 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" @@ -89,6 +91,7 @@ void CalendarDataStorage::SetupTables(){ ", entrydurationhour INTEGER" ", entrydurationminute INTEGER" ", entrydurationsecond INTEGER" + ", filename TEXT" ");"; resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg); @@ -131,7 +134,7 @@ bool CalendarDataStorage::DidInitOK() } -CDSAccountResult CalendarDataStorage::AddAccount(string accountName) +CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int accountPreferencesID) { CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL; @@ -139,7 +142,7 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName) sqlite3_stmt *statementHandle; - resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name) VALUES(?1);", -1, &statementHandle, nullptr); + resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr); if (resultCode != 0){ return CDSACCOUNT_FAILED; @@ -147,6 +150,12 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName) 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; } @@ -178,7 +187,7 @@ CDSAccountList CalendarDataStorage::GetAccountList() 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; @@ -207,6 +216,7 @@ CDSAccountList CalendarDataStorage::GetAccountList() CDSGetAccountInfo accountInfo; accountInfo.accountID = sqlite3_column_int(statementHandle, 0); + accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2); stringstream calendarStream; @@ -236,7 +246,7 @@ CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName) 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; @@ -267,6 +277,7 @@ CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName) accountInfo.accountName = accountNameStream.str(); accountInfo.accountID = sqlite3_column_int(statementHandle, 1); + accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2); accountInfo.accountInfoResult = CDSACCOUNT_OK; return accountInfo; @@ -450,7 +461,7 @@ CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID) } -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; @@ -458,7 +469,7 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda sqlite3_stmt *statementHandle; - resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour) VALUES(?1, ?2, ?3, ?4);", -1, &statementHandle, nullptr); + 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; @@ -484,6 +495,12 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).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; } @@ -535,7 +552,7 @@ CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std // 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; @@ -580,6 +597,10 @@ CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std 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; @@ -601,11 +622,12 @@ 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 FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr); + 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; @@ -642,9 +664,45 @@ CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID) 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; @@ -660,7 +718,7 @@ CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID) } -CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName) +CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName, Colour calendarColour, std::string calendarDescription) { CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL; @@ -695,7 +753,7 @@ CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string cal // 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; @@ -703,11 +761,23 @@ CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string cal resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC); + if (resultCode != 0){ + return CDSCALENDAR_FAILED; + } + + resultCode = sqlite3_bind_text(statementHandle, 2, string(calendarColour).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; @@ -1154,9 +1224,10 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file 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); @@ -1318,6 +1389,13 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file 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){ @@ -1336,30 +1414,54 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file } -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); @@ -1367,158 +1469,663 @@ CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID) 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, " - " calendarid, id" - " 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; + } - resultCode = sqlite3_step(statementHandle); + eventString = eventData.DateTimeStartData.substr(0,4); - if (resultCode == SQLITE_ROW){ - - // Get the calendar entry name, - - stringstream entryStream; + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ - entryStream << sqlite3_column_text(statementHandle, 0); - entryResult.entryName = entryStream.str(); + eventStartYear = atoi(eventString.c_str()); - entryStream.str(""); + } else { - // Get the calendar entry description. + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; - entryStream << sqlite3_column_text(statementHandle, 1); - entryResult.entryDescription = entryStream.str(); + } - entryStream.str(""); + eventString = eventData.DateTimeStartData.substr(4,2); + + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ - entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2); - entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3); - entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4); - entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5); - entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6); - entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7); - entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8); - entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9); - entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10); - entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11); - entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12); - entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13); - entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14); - entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15); - 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); + eventStartMonth = atoi(eventString.c_str()); - } else if (resultCode == SQLITE_DONE) { - entryResult.getEventResult = CDSENTRY_NOCALENDAR; - return entryResult; } else { - entryResult.getEventResult = CDSENTRY_FAILED; - return entryResult; - } - - entryResult.getEventResult = CDSENTRY_OK; - - return entryResult; - -} - -CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID) -{ - - CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL; - - // 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){ - return CDSENTRY_FAILED; - } - - resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID); - - if (resultCode != 0){ - return CDSENTRY_FAILED; + + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; + } - resultCode = sqlite3_step(findHandle); + eventString = eventData.DateTimeStartData.substr(6,2); - if (resultCode == SQLITE_ROW){ + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ + + eventStartDay = atoi(eventString.c_str()); - } else if (resultCode == SQLITE_DONE) { - return CDSENTRY_NOENTRY; } else { - return CDSENTRY_FAILED; + + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; + } - // Delete the account. - - resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr); + eventString = eventData.DateTimeStartData.substr(9,2); - if (resultCode != 0){ - return CDSENTRY_FAILED; + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ + + eventStartHour = atoi(eventString.c_str()); + + } else { + + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; + } - resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID); + eventString = eventData.DateTimeStartData.substr(11,2); - if (resultCode != 0){ - return CDSENTRY_FAILED; + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ + + eventStartMinute = atoi(eventString.c_str()); + + } else { + + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; + } - resultCode = sqlite3_step(statementHandle); + eventString = eventData.DateTimeStartData.substr(13,2); - if (resultCode == SQLITE_DONE){ - deleteResult = CDSENTRY_OK; + if (all_of(eventString.begin(), eventString.end(), ::isdigit)){ + + eventStartSecond = atoi(eventString.c_str()); + } else { - return CDSENTRY_FAILED; + + editResult.editEventResult = CDSENTRY_INVALIDFILE; + return editResult; + } - // Update the checksum. - - UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW))); + //eventYear = eventStartDate.substr(0, 4); - return deleteResult; + // End Date. -} - -CDSEntryList CalendarDataStorage::GetEventList(int calendarID){ + int eventEndYear = 0; + int eventEndMonth = 0; + int eventEndDay = 0; + int eventEndHour = 0; + int eventEndMinute = 0; + int eventEndSecond = 0; + int eventEndDuration = 0; - CDSEntryList entryList; - entryList.getEventListResult = CDSENTRY_UNITTESTFAIL; + 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); + + if (resultCode == SQLITE_ROW){ + + // Get the calendar entry name, + + stringstream entryStream; + + entryStream << sqlite3_column_text(statementHandle, 0); + entryResult.entryName = entryStream.str(); + + entryStream.str(""); + + // Get the calendar entry description. + + entryStream << sqlite3_column_text(statementHandle, 1); + entryResult.entryDescription = entryStream.str(); + + 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.entryStartHour = sqlite3_column_int(statementHandle, 5); + entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6); + entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7); + entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8); + entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9); + entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10); + entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11); + entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12); + entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13); + entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14); + entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15); + 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 entryResult; + } else { + entryResult.getEventResult = CDSENTRY_FAILED; + return entryResult; + } + + entryResult.getEventResult = CDSENTRY_OK; + + return entryResult; + +} + +CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID) +{ + + CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL; + + // 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){ + return CDSENTRY_FAILED; + } + + resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID); + + if (resultCode != 0){ + return CDSENTRY_FAILED; + } + + resultCode = sqlite3_step(findHandle); + + if (resultCode == SQLITE_ROW){ + + } else if (resultCode == SQLITE_DONE) { + return CDSENTRY_NOENTRY; + } else { + return CDSENTRY_FAILED; + } + + // Delete the account. + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + return CDSENTRY_FAILED; + } + + resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID); + + if (resultCode != 0){ + return CDSENTRY_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode == SQLITE_DONE){ + deleteResult = CDSENTRY_OK; + } else { + return CDSENTRY_FAILED; + } + + // Update the checksum. + + UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW))); + + return deleteResult; + +} + +CDSEntryList CalendarDataStorage::GetEventList(int calendarID){ + + CDSEntryList entryList; + entryList.getEventListResult = CDSENTRY_UNITTESTFAIL; // Check if calendar exists first. @@ -1909,4 +2516,104 @@ CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, 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){ + cout << "Fail 1" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 2" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendarentries';", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + cout << "Fail 3" << endl; + cout << sqlite3_errmsg(db) << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 4" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + cout << "Fail 5" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 6" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendars';", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + cout << "Fail 7" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 8" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + cout << "Fail 9" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 10" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='accounts'", -1, &statementHandle, nullptr); + + if (resultCode != 0){ + cout << "Fail 11" << endl; + return CDSCLEANUP_FAILED; + } + + resultCode = sqlite3_step(statementHandle); + + if (resultCode != SQLITE_DONE){ + cout << "Fail 12" << endl; + return CDSCLEANUP_FAILED; + } + + // Update the checksum. + + UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW))); + + return CDSCLEANUP_OK; + } \ No newline at end of file diff --git a/source/libraries/CalendarDataStorage/CalendarDataStorage.h b/source/libraries/CalendarDataStorage/CalendarDataStorage.h index 4b41b90..47c70b5 100644 --- a/source/libraries/CalendarDataStorage/CalendarDataStorage.h +++ b/source/libraries/CalendarDataStorage/CalendarDataStorage.h @@ -49,6 +49,7 @@ enum CDSEntryResult{ struct CDSGetAccountInfo{ int accountID = 0; + int accountPreferencesID = 0; std::string accountName = ""; CDSAccountResult accountInfoResult = CDSACCOUNT_NOACTION; }; @@ -59,6 +60,7 @@ struct CDSGetCalendarInfo{ std::string accountName = ""; std::string calendarName = ""; std::string calendarTextID = ""; + std::string calendarDescription = ""; Colour calendarColour; CDSAccountResult accountInfoResult = CDSACCOUNT_NOACTION; CDSCalendarResult calendarInfoResult = CDSCALENDAR_NOACTION; @@ -69,12 +71,18 @@ struct CDSAddEntryResult{ CDSEntryResult addEventResult = CDSENTRY_NOACTION; }; +struct CDSEditEntryResult{ + int calendarEntryID = 0; + CDSEntryResult editEventResult = CDSENTRY_NOACTION; +}; + struct CDSGetCalendarEntryInfo{ int accountID = 0; int calendarID = 0; int calendarEntryID = 0; std::string entryName = ""; std::string entryDescription = ""; + std::string entryFilename = ""; int entryStartYear = 0; int entryStartMonth = 0; int entryStartDay = 0; @@ -125,6 +133,12 @@ struct CDSGetChecksumResult{ std::string checksumValue = ""; }; +enum CDSCleanupResult{ + CDSCLEANUP_UNITTESTFAIL = -1, + CDSCLEANUP_OK, + CDSCLEANUP_FAILED +}; + class CalendarDataStorage{ private: @@ -138,7 +152,7 @@ class CalendarDataStorage{ // Account functions. - CDSAccountResult AddAccount(std::string accountName); + CDSAccountResult AddAccount(std::string accountName, int accountPreferencesID); CDSGetAccountInfo GetAccount(std::string accountName); CDSAccountResult UpdateAccount(int accountID, std::string accountName); CDSAccountResult DeleteAccount(int accountID); @@ -146,11 +160,11 @@ class CalendarDataStorage{ // Calendar functions. - CDSCalendarResult AddCalendar(int accountID, std::string calendarName, std::string calendarID, Colour calendarColour); + CDSCalendarResult AddCalendar(int accountID, std::string calendarName, std::string calendarID, Colour calendarColour, std::string calendarDescription); CDSGetCalendarInfo GetCalendar(std::string accountName, std::string calendarTextID); CDSGetCalendarInfo GetCalendar(int calendarID); CDSCalendarList GetCalendarList(int accountID); - CDSCalendarResult UpdateCalendar(int calendarID, std::string calendarName); + CDSCalendarResult UpdateCalendar(int calendarID, std::string calendarName, Colour calendarColour, std::string calendarDescription); CDSCalendarResult DeleteCalendar(int calendarID); // Entry functions @@ -159,6 +173,7 @@ class CalendarDataStorage{ CDSGetCalendarEntryInfo GetEvent(int calendarEntryID); CDSEntryList GetEventList(int calendarID); CDSEntryList GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay); + CDSEditEntryResult UpdateEvent(int eventID, std::string filename); CDSEntryResult DeleteEvent(int calendarEntryID); // Checksum functions. @@ -166,6 +181,10 @@ class CalendarDataStorage{ CDSChecksumResult AddChecksum(std::string checksumName, std::string checksumValue); CDSGetChecksumResult GetChecksum(std::string checksumName); CDSChecksumResult UpdateChecksum(std::string checksumName, std::string checksumValue); + + // Cleanup functions. + + CDSCleanupResult Clear(); };