X-Git-Url: http://Server1/repobrowser/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibraries%2FCalendarDataStorage%2FCalendarDataStorage.cpp;h=5842631dfd32d08d4aa3ce5f2afc04ff72afc018;hb=a191ad71a0d6823b9710067c7cccf8905290dc9c;hp=b1d2c5b52a85f16d7deca31a193087cf3711bdfc;hpb=e302a149a147c693cff6ca8bed0a0d8cb856e5b5;p=xestiacalendar%2F.git
diff --git a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp
index b1d2c5b..5842631 100644
--- a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp
+++ b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp
@@ -1,9 +1,25 @@
+// 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
+
#include "CalendarDataStorage.h"
#define CDS_RANDOMPOW 24
-#include
-
using namespace std;
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
@@ -141,6 +157,33 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int account
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_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);
@@ -300,8 +343,13 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun
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);
@@ -326,6 +374,30 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun
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);
@@ -393,7 +465,7 @@ CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
} else if (resultCode == SQLITE_DONE) {
return CDSACCOUNT_NOACCOUNT;
} else {
- return CDSACCOUNT_FAILED;
+
}
// Delete the account.
@@ -466,9 +538,30 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda
CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
int resultCode;
-
+
+ sqlite3_stmt *findHandle;
sqlite3_stmt *statementHandle;
+ // 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){
@@ -493,7 +586,8 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda
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;
@@ -765,7 +859,8 @@ CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string cal
return CDSCALENDAR_FAILED;
}
- resultCode = sqlite3_bind_text(statementHandle, 2, string(calendarColour).c_str(), -1, SQLITE_STATIC);
+ string calendarColourString = calendarColour;
+ resultCode = sqlite3_bind_text(statementHandle, 2, calendarColourString.c_str(), -1, SQLITE_STATIC);
if (resultCode != 0){
return CDSCALENDAR_FAILED;
@@ -958,14 +1053,14 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
// 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)){
@@ -978,7 +1073,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeStartData.substr(4,2);
+ eventString = eventData.dateTimeStartData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -991,7 +1086,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeStartData.substr(6,2);
+ eventString = eventData.dateTimeStartData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1004,7 +1099,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeStartData.substr(9,2);
+ eventString = eventData.dateTimeStartData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1017,7 +1112,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeStartData.substr(11,2);
+ eventString = eventData.dateTimeStartData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1030,7 +1125,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeStartData.substr(13,2);
+ eventString = eventData.dateTimeStartData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1055,16 +1150,16 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
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)){
@@ -1077,7 +1172,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeEndData.substr(4,2);
+ eventString = eventData.dateTimeEndData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1090,7 +1185,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeEndData.substr(6,2);
+ eventString = eventData.dateTimeEndData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1103,7 +1198,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeEndData.substr(9,2);
+ eventString = eventData.dateTimeEndData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1116,7 +1211,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeEndData.substr(11,2);
+ eventString = eventData.dateTimeEndData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1129,7 +1224,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DateTimeEndData.substr(13,2);
+ eventString = eventData.dateTimeEndData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1144,7 +1239,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
}
- eventString = eventData.DurationData;
+ eventString = eventData.durationData;
// Process the duration data.
@@ -1156,22 +1251,22 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
// 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.
@@ -1240,7 +1335,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
// 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;
@@ -1252,7 +1347,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
string eventDescription;
try {
- eventDescription = eventData.DescriptionList.at(0);
+ eventDescription = eventData.descriptionList.at(0);
}
catch (out_of_range &err){
@@ -1489,14 +1584,14 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
// Start Date.
- if (eventData.DateTimeStartData.size() < 16){
+ if (eventData.dateTimeStartData.size() < 16){
editResult.editEventResult = CDSENTRY_INVALIDFILE;
return editResult;
}
- eventString = eventData.DateTimeStartData.substr(0,4);
+ eventString = eventData.dateTimeStartData.substr(0,4);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1509,7 +1604,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeStartData.substr(4,2);
+ eventString = eventData.dateTimeStartData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1522,7 +1617,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeStartData.substr(6,2);
+ eventString = eventData.dateTimeStartData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1535,7 +1630,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeStartData.substr(9,2);
+ eventString = eventData.dateTimeStartData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1548,7 +1643,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeStartData.substr(11,2);
+ eventString = eventData.dateTimeStartData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1561,7 +1656,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeStartData.substr(13,2);
+ eventString = eventData.dateTimeStartData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1586,16 +1681,16 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
int eventEndSecond = 0;
int eventEndDuration = 0;
- if (eventData.DateTimeEndData != ""){
+ if (eventData.dateTimeEndData != ""){
- if (eventData.DateTimeEndData.size() < 16){
+ if (eventData.dateTimeEndData.size() < 16){
editResult.editEventResult = CDSENTRY_INVALIDFILE;
return editResult;
}
- eventString = eventData.DateTimeEndData.substr(0,4);
+ eventString = eventData.dateTimeEndData.substr(0,4);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1608,7 +1703,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeEndData.substr(4,2);
+ eventString = eventData.dateTimeEndData.substr(4,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1621,7 +1716,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeEndData.substr(6,2);
+ eventString = eventData.dateTimeEndData.substr(6,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1634,7 +1729,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeEndData.substr(9,2);
+ eventString = eventData.dateTimeEndData.substr(9,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1647,7 +1742,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeEndData.substr(11,2);
+ eventString = eventData.dateTimeEndData.substr(11,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1660,7 +1755,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DateTimeEndData.substr(13,2);
+ eventString = eventData.dateTimeEndData.substr(13,2);
if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
@@ -1675,7 +1770,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
}
- eventString = eventData.DurationData;
+ eventString = eventData.durationData;
// Process the duration data.
@@ -1687,22 +1782,22 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
// 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.
@@ -1755,11 +1850,11 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
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), "
+ " 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){
@@ -1769,7 +1864,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
// 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){
editResult.editEventResult = CDSENTRY_FAILED;
@@ -1781,7 +1876,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
string eventDescription;
try {
- eventDescription = eventData.DescriptionList.at(0);
+ eventDescription = eventData.descriptionList.at(0);
}
catch (out_of_range &err){
@@ -2528,85 +2623,72 @@ CDSCleanupResult CalendarDataStorage::Clear(){
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;
}