Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
camelCase: Converted remaining code that was missed
[xestiacalendar/.git] / source / libraries / CalendarDataStorage / CalendarDataStorage.cpp
1 // CalendarDataStorage.cpp - CalendarDataStorage class
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "CalendarDataStorage.h"
21 #define CDS_RANDOMPOW 24
23 using namespace std;
25 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
26    return 0;
27 }
29 CalendarDataStorage::CalendarDataStorage(){
30         
31         // Initialise the SQLite database.
32         
33         sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, nullptr);
34         SetupTables();
35         
36 }
38 CalendarDataStorage::~CalendarDataStorage(){
39         
40         // Destory the SQLite database.
41         
42         sqlite3_close(db);
43         
44 }
46 void CalendarDataStorage::SetupTables(){
47         
48         // Setup the tables for the Calendar Data Storage.
49         
50         char *setupTablesErrMsg = nullptr;
51         int resultCode;
52         
53         // Setup the accounts table.
54         
55         const char *accountsTableString;
56         accountsTableString = "CREATE TABLE accounts(id INTEGER PRIMARY KEY AUTOINCREMENT"
57                 ", preferencesid INTEGER"
58                 ", name TEXT"
59                 ");";
60         
61         resultCode = sqlite3_exec(db, accountsTableString, callback, nullptr, &setupTablesErrMsg);
62         
63         if (resultCode != 0){
64                 DataStorageInitOK = false;
65                 return;
66         }
67         
68         // Setup the calendars table.
69         
70         const char *calendarTableString;
71         calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY AUTOINCREMENT"
72                 ", accountid INTEGER"
73                 ", name TEXT"
74                 ", calendarid TEXT"
75                 ", colour TEXT"
76                 ", description TEXT"
77                 ");";
78         
79         resultCode = sqlite3_exec(db, calendarTableString, callback, nullptr, &setupTablesErrMsg);
80         
81         if (resultCode != 0){
82                 DataStorageInitOK = false;
83                 return;
84         }
85         
86         // Setup the calendar entries table.
87         
88         const char *calendarentriesTableString;
89         calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY AUTOINCREMENT"
90                 ", calendarid INTEGER"
91                 ", entryname TEXT"
92                 ", entrydescription TEXT"
93                 ", entrystartyear INTEGER"
94                 ", entrystartmonth INTEGER"
95                 ", entrystartday INTEGER"
96                 ", entrystarthour INTEGER"
97                 ", entrystartminute INTEGER"
98                 ", entrystartsecond INTEGER"
99                 ", entryendyear INTEGER"
100                 ", entryendmonth INTEGER"
101                 ", entryendday INTEGER"
102                 ", entryendhour INTEGER"
103                 ", entryendminute INTEGER"
104                 ", entryendsecond INTEGER"
105                 ", entrydurationweek INTEGER"
106                 ", entrydurationday INTEGER"
107                 ", entrydurationhour INTEGER"
108                 ", entrydurationminute INTEGER"
109                 ", entrydurationsecond INTEGER"
110                 ", filename TEXT"
111                 ");";
112         
113         resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg);
114         
115         if (resultCode != 0){
116                 DataStorageInitOK = false;
117                 return;
118         }
120         // Setup the checksums table.
121         
122         const char *checksumsTableString;
123         checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
124                 ", checksumvalue TEXT);";
125         
126         resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
127         
128         if (resultCode != 0){
129                 DataStorageInitOK = false;
130                 return;
131         }
132         
133         // Setup internal checksums.
134         
135         CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
136         
137         if (addChecksum != CDSCHECKSUM_OK){
138                 DataStorageInitOK = false;
139                 return;
140         }
141         
142         DataStorageInitOK = true;
143         
146 bool CalendarDataStorage::DidInitOK()
148         
149         return DataStorageInitOK;
150         
153 CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int accountPreferencesID)
155         
156         CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL;
157         int resultCode;
158         
159         sqlite3_stmt *statementHandle;
160         
161         resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr);
162         
163         if (resultCode != 0){
164                 return CDSACCOUNT_FAILED;
165         }
166         
167         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
168         
169         if (resultCode != 0){
170                 return CDSACCOUNT_FAILED;
171         }
173         resultCode = sqlite3_bind_int(statementHandle, 2, accountPreferencesID);
174         
175         if (resultCode != 0){
176                 return CDSACCOUNT_FAILED;
177         }
178         
179         resultCode = sqlite3_step(statementHandle);
180         
181         if (resultCode != SQLITE_DONE){
182                 return CDSACCOUNT_FAILED;
183         }
184         
185         // Update the checksum.
186         
187         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
188         
189         addResult = CDSACCOUNT_OK;
190         
191         return addResult;
192         
195 CDSAccountList CalendarDataStorage::GetAccountList()
197         
198         CDSAccountList accountList;
199         
200         // Check if calendar exists first.
201         
202         int resultCode = 0;
203         
204         sqlite3_stmt *statementHandle;
205         
206         resultCode = sqlite3_prepare_v2(db, "SELECT id, name, preferencesid from accounts;", -1, &statementHandle, nullptr);
207         
208         if (resultCode != 0){
209                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
210                 return accountList;
211         }
212         
213         if (resultCode != 0){
214                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
215                 return accountList;
216         }
217         
218         resultCode = sqlite3_step(statementHandle);
219         
220         if (resultCode == SQLITE_ROW){
221                 
222         } else if (resultCode == SQLITE_DONE) {
223                 accountList.getAccountListResult = CDSACCOUNT_NOACCOUNT;
224                 return accountList;
225         } else {
226                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
227                 return accountList;
228         }
229         
230         while (resultCode == SQLITE_ROW){
231                 
232                 CDSGetAccountInfo accountInfo;
233                 
234                 accountInfo.accountID = sqlite3_column_int(statementHandle, 0);
235                 accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
236                 
237                 stringstream calendarStream;
238                 
239                 calendarStream << sqlite3_column_text(statementHandle, 1);
240                 
241                 accountInfo.accountName = calendarStream.str();
242                 accountInfo.accountInfoResult = CDSACCOUNT_OK;
243                 
244                 accountList.accountList.push_back(accountInfo);
245                 
246                 resultCode = sqlite3_step(statementHandle);
247                 
248         }       
249         
250         accountList.getAccountListResult = CDSACCOUNT_OK;
251         
252         return accountList;
253         
256 CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName)
258         
259         CDSGetAccountInfo accountInfo;
260         accountInfo.accountInfoResult = CDSACCOUNT_UNITTESTFAIL;
261         int resultCode;
262         
263         sqlite3_stmt *statementHandle;
264         
265         resultCode = sqlite3_prepare_v2(db, "SELECT name, id, preferencesid FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
267         if (resultCode != 0){
268                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
269                 return accountInfo;
270         }
271         
272         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
273         
274         if (resultCode != 0){
275                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
276                 return accountInfo;
277         }
278         
279         resultCode = sqlite3_step(statementHandle);
280         
281         if (resultCode == SQLITE_DONE){
282                 
283                 accountInfo.accountInfoResult = CDSACCOUNT_NOACCOUNT;
284                 return accountInfo;
285                 
286         } else if (resultCode == SQLITE_ROW){
287                 
288                 // Get the result data.
289                 
290                 stringstream accountNameStream;
291                 
292                 accountNameStream << sqlite3_column_text(statementHandle, 0);
293                 
294                 accountInfo.accountName = accountNameStream.str();
295                 accountInfo.accountID = sqlite3_column_int(statementHandle, 1);
296                 accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
297                 
298                 accountInfo.accountInfoResult = CDSACCOUNT_OK;
299                 return accountInfo;
300                 
301         } else {
302                 
303                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
304                 return accountInfo;
305                 
306         }
307         
308         return accountInfo;
309         
312 CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accountName)
314         
315         CDSAccountResult updateResult = CDSACCOUNT_UNITTESTFAIL;
316         int resultCode;
317         
318         sqlite3_stmt *findHandle;
319         sqlite3_stmt *statementHandle;
320         
321         // Check if account exists first.
322         
323         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
324         
325         if (resultCode != 0){
326                 return CDSACCOUNT_FAILED;
327         }
328         
329         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
330         
331         if (resultCode != 0){
332                 return CDSACCOUNT_FAILED;
333         }
334         
335         resultCode = sqlite3_step(findHandle);
336         
337         if (resultCode == SQLITE_ROW){
338                 
339         } else if (resultCode == SQLITE_DONE) {
340                 return CDSACCOUNT_NOACCOUNT;
341         } else {
342                 return CDSACCOUNT_FAILED;
343         }
344         
345         // Update the account.
346         
347         resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
348         
349         if (resultCode != 0){
350                 return CDSACCOUNT_FAILED;
351         }
352         
353         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
354         
355         if (resultCode != 0){
356                 return CDSACCOUNT_FAILED;
357         }
358         
359         resultCode = sqlite3_bind_int(statementHandle, 2, accountID);
360         
361         if (resultCode != 0){
362                 return CDSACCOUNT_FAILED;
363         }
364         
365         resultCode = sqlite3_step(statementHandle);
366         
367         if (resultCode != SQLITE_DONE){
368                 return CDSACCOUNT_FAILED;
369         }
370         
371         // Update the checksum.
372         
373         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
374         
375         updateResult = CDSACCOUNT_OK;
376         
377         return updateResult;
378         
381 CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
383         
384         CDSAccountResult deleteResult = CDSACCOUNT_UNITTESTFAIL;
385         
386         // Check if account exists first.
387         
388         sqlite3_stmt *findHandle;
389         sqlite3_stmt *statementHandle;
390         sqlite3_stmt *calendarHandle;
391         int resultCode;
392         
393         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
394         
395         if (resultCode != 0){
396                 return CDSACCOUNT_FAILED;
397         }
398         
399         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
400         
401         if (resultCode != 0){
402                 return CDSACCOUNT_FAILED;
403         }
404         
405         resultCode = sqlite3_step(findHandle);
406         
407         if (resultCode == SQLITE_ROW){
408                 
409         } else if (resultCode == SQLITE_DONE) {
410                 return CDSACCOUNT_NOACCOUNT;
411         } else {
412                 return CDSACCOUNT_FAILED;
413         }
414         
415         // Delete the account.
416         
417         resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts WHERE id=(?1);", -1, &statementHandle, nullptr);
418         
419         if (resultCode != 0){
420                 return CDSACCOUNT_FAILED;
421         }
422         
423         resultCode = sqlite3_bind_int(statementHandle, 1, accountID);
424         
425         if (resultCode != 0){
426                 return CDSACCOUNT_FAILED;
427         }
428         
429         resultCode = sqlite3_step(statementHandle);
430         
431         if (resultCode == SQLITE_DONE){
432                 //deleteResult = CDSACCOUNT_OK;
433         } else {
434                 return CDSACCOUNT_FAILED;
435         }
436         
437         // Get the calendar IDs and delete each calendar (and associated entries).
438         
439         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
440         
441         if (resultCode != 0){
442                 return CDSACCOUNT_FAILED;
443         }
444         
445         resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
446         
447         if (resultCode != 0){
448                 return CDSACCOUNT_FAILED;
449         }
450         
451         resultCode = sqlite3_step(calendarHandle);
452         
453         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
454                 deleteResult = CDSACCOUNT_OK;
455         } else {
456                 return CDSACCOUNT_FAILED;
457         }
458         
459         while (resultCode == SQLITE_ROW){
460                 
461                 int calendarDeleteID = sqlite3_column_int(calendarHandle, 0);
462                 
463                 DeleteCalendar(calendarDeleteID);
464                 
465                 resultCode = sqlite3_step(calendarHandle);
466                 
467         }
468         
469         // Update the checksum.
470         
471         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
472         
473         deleteResult = CDSACCOUNT_OK;
474         
475         return deleteResult;
476         
477         
480 CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour, string calendarDescription)
483         CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
484         int resultCode;
485         
486         sqlite3_stmt *statementHandle;
487         
488         resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour, description) VALUES(?1, ?2, ?3, ?4, ?5);", -1, &statementHandle, nullptr);
489         
490         if (resultCode != 0){
491                 return CDSCALENDAR_FAILED;
492         }
493         
494         resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
495         
496         if (resultCode != 0){
497                 return CDSCALENDAR_FAILED;
498         }
499         
500         resultCode = sqlite3_bind_text(statementHandle, 2, calendarID.c_str(), -1, SQLITE_STATIC);
502         if (resultCode != 0){
503                 return CDSCALENDAR_FAILED;
504         }
506         resultCode = sqlite3_bind_int(statementHandle, 3, accountID);
508         if (resultCode != 0){
509                 return CDSCALENDAR_FAILED;
510         }
511         
512         resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).c_str(), -1, SQLITE_STATIC);
514         if (resultCode != 0){
515                 return CDSCALENDAR_FAILED;
516         }
518         resultCode = sqlite3_bind_text(statementHandle, 5, calendarDescription.c_str(), -1, SQLITE_STATIC);
520         if (resultCode != 0){
521                 return CDSCALENDAR_FAILED;
522         }
523         
524         resultCode = sqlite3_step(statementHandle);
525         
526         if (resultCode != SQLITE_DONE){
527                 return CDSCALENDAR_FAILED;
528         }
529         
530         // Update the checksum.
531         
532         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
533         
534         addResult = CDSCALENDAR_OK;
535         
536         return addResult;
537         
540 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std::string calendarTextID)
542         
543         CDSGetAccountInfo accountResult;
544         CDSGetCalendarInfo calendarResult;
545         sqlite3_stmt *statementHandle;
546         int resultCode;
547         
548         // Check if the account exists.
549         
550         accountResult = GetAccount(accountName);
551         
552         switch (accountResult.accountInfoResult){
553                 
554                 case CDSACCOUNT_OK:
555                         calendarResult.accountName = accountResult.accountName;
556                         calendarResult.accountInfoResult = CDSACCOUNT_OK;
557                         break;
558                 case CDSACCOUNT_FAILED:
559                         calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
560                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
561                         return calendarResult;
562                 case CDSACCOUNT_NOACCOUNT:
563                         calendarResult.accountInfoResult = CDSACCOUNT_NOACCOUNT;
564                         calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
565                         return calendarResult;
566                 
567         }
568         
569         // Check if the calendar exists.
570         
571         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
572         
573         if (resultCode != 0){
574                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
575                 return calendarResult;
576         }
578         resultCode = sqlite3_bind_int(statementHandle, 1, accountResult.accountID);
579         
580         if (resultCode != 0){
581                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
582                 return calendarResult;
583         }
584         
585         resultCode = sqlite3_bind_text(statementHandle, 2, calendarTextID.c_str(), -1, SQLITE_STATIC);
586         
587         if (resultCode != 0){
588                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
589                 return calendarResult;
590         }
591         
592         resultCode = sqlite3_step(statementHandle);
593         
594         if (resultCode == SQLITE_DONE){
595                 
596                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
597                 return calendarResult;
598                 
599         } else if (resultCode == SQLITE_ROW){
600                 
601                 // Get the calendar name.
602                 
603                 stringstream calendarStream;
604                 
605                 calendarStream << sqlite3_column_text(statementHandle, 2);
606                 calendarResult.calendarName = calendarStream.str();
607                 
608                 calendarStream.str("");
609                 calendarStream << sqlite3_column_text(statementHandle, 3);
610                 calendarResult.calendarTextID = calendarStream.str();
612                 calendarStream.str("");
613                 calendarStream << sqlite3_column_text(statementHandle, 4);
614                 calendarResult.calendarColour = calendarStream.str();
615                 
616                 calendarStream.str("");
617                 calendarStream << sqlite3_column_text(statementHandle, 5);
618                 calendarResult.calendarDescription = calendarStream.str();
619                 
620                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
621                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
622                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
623                 return calendarResult;
624                 
625         } else {
626                 
627                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
628                 return calendarResult;
629                 
630         }
631         
632         return calendarResult;
633         
636 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
638         
639         CDSGetCalendarInfo calendarResult;
640         sqlite3_stmt *statementHandle;
641         sqlite3_stmt *accountHandle;
642         int resultCode;
643         
644         // Check if the calendar exists.
645         
646         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
647         
648         if (resultCode != 0){
649                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
650                 return calendarResult;
651         }
653         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
654         
655         if (resultCode != 0){
656                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
657                 return calendarResult;
658         }
659         
660         resultCode = sqlite3_step(statementHandle);
661         
662         if (resultCode == SQLITE_DONE){
663                 
664                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
665                 return calendarResult;
666                 
667         } else if (resultCode == SQLITE_ROW){
668                 
669                 // Get the calendar data.
670                 
671                 stringstream calendarStream;
672                 
673                 calendarStream << sqlite3_column_text(statementHandle, 2);
674                 calendarResult.calendarName = calendarStream.str();
675                 
676                 calendarStream.str("");
677                 calendarStream << sqlite3_column_text(statementHandle, 3);
678                 calendarResult.calendarTextID = calendarStream.str();
680                 calendarStream.str("");
681                 calendarStream << sqlite3_column_text(statementHandle, 4);
682                 calendarResult.calendarColour = calendarStream.str();
684                 calendarStream.str("");
685                 calendarStream << sqlite3_column_text(statementHandle, 5);
686                 calendarResult.calendarDescription = calendarStream.str();
687                 
688                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
689                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
690                 
691                 // Get the account name.
692                 
693                 resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE id=(?1);", -1, &accountHandle, nullptr);
694         
695                 if (resultCode != 0){
696                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
697                         return calendarResult;
698                 }
700                 resultCode = sqlite3_bind_int(accountHandle, 1, calendarResult.accountID);
701         
702                 if (resultCode != 0){
703                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
704                         return calendarResult;
705                 }
706         
707                 resultCode = sqlite3_step(accountHandle);
708                 
709                 if (resultCode == SQLITE_ROW){
710                         
711                         stringstream accountStream;
712                         accountStream << sqlite3_column_text(accountHandle, 0);
713                         calendarResult.accountName = accountStream.str();
714                         calendarResult.accountInfoResult = CDSACCOUNT_OK;
715                         
716                 } else {
718                         calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
719                         
720                 }
721                 
722                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
723                 return calendarResult;
724                 
725         } else {
726                 
727                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
728                 return calendarResult;
729                 
730         }
731         
732         return calendarResult;
733         
737 CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName, Colour calendarColour, std::string calendarDescription)
739         
740         CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
741         int resultCode;
742         
743         sqlite3_stmt *findHandle;
744         sqlite3_stmt *statementHandle;
745         
746         // Check if calendar exists first.
747         
748         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
749         
750         if (resultCode != 0){
751                 return CDSCALENDAR_FAILED;
752         }
753         
754         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
755         
756         if (resultCode != 0){
757                 return CDSCALENDAR_FAILED;
758         }
759         
760         resultCode = sqlite3_step(findHandle);
761         
762         if (resultCode == SQLITE_ROW){
763                 
764         } else if (resultCode == SQLITE_DONE) {
765                 return CDSCALENDAR_NOCALENDAR;
766         } else {
767                 return CDSCALENDAR_FAILED;
768         }
769         
770         // Update the account.
771         
772         resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1), colour=(?2), description=(?3) WHERE id=(?4);", -1, &statementHandle, nullptr);
773         
774         if (resultCode != 0){
775                 return CDSCALENDAR_FAILED;
776         }
777         
778         resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
779         
780         if (resultCode != 0){
781                 return CDSCALENDAR_FAILED;
782         }
784         resultCode = sqlite3_bind_text(statementHandle, 2, string(calendarColour).c_str(), -1, SQLITE_STATIC);
785         
786         if (resultCode != 0){
787                 return CDSCALENDAR_FAILED;
788         }
790         resultCode = sqlite3_bind_text(statementHandle, 3, calendarDescription.c_str(), -1, SQLITE_STATIC);
791         
792         if (resultCode != 0){
793                 return CDSCALENDAR_FAILED;
794         }
795         
796         resultCode = sqlite3_bind_int(statementHandle, 4, calendarID);
797         
798         if (resultCode != 0){
799                 return CDSCALENDAR_FAILED;
800         }
801         
802         resultCode = sqlite3_step(statementHandle);
803         
804         if (resultCode != SQLITE_DONE){
805                 return CDSCALENDAR_FAILED;
806         }
807         
808         // Update the checksum.
809         
810         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
811         
812         updateResult = CDSCALENDAR_OK;
813         
814         return updateResult;
815         
818 CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
820         
821         CDSCalendarResult deleteResult = CDSCALENDAR_UNITTESTFAIL;
822         
823         // Check if account exists first.
824         
825         sqlite3_stmt *findHandle;
826         sqlite3_stmt *statementHandle;
827         sqlite3_stmt *entriesHandle;
828         int resultCode;
829         
830         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
831         
832         if (resultCode != 0){
833                 return CDSCALENDAR_FAILED;
834         }
835         
836         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
837         
838         if (resultCode != 0){
839                 return CDSCALENDAR_FAILED;
840         }
841         
842         resultCode = sqlite3_step(findHandle);
843         
844         if (resultCode == SQLITE_ROW){
845                 
846         } else if (resultCode == SQLITE_DONE) {
847                 return CDSCALENDAR_NOCALENDAR;
848         } else {
849                 return CDSCALENDAR_FAILED;
850         }
851         
852         // Delete the calendar.
853         
854         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
855         
856         if (resultCode != 0){
857                 return CDSCALENDAR_FAILED;
858         }
859         
860         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
861         
862         if (resultCode != 0){
863                 return CDSCALENDAR_FAILED;
864         }
865         
866         resultCode = sqlite3_step(statementHandle);
867         
868         if (resultCode != SQLITE_DONE){
869                 return CDSCALENDAR_FAILED;
870         }
871         
872         // Delete the calendar entries.
873         
874         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE calendarid=(?1);", -1, &entriesHandle, nullptr);
875         
876         if (resultCode != 0){
877                 return CDSCALENDAR_FAILED;
878         }
879         
880         resultCode = sqlite3_bind_int(entriesHandle, 1, calendarID);
881         
882         if (resultCode != 0){
883                 return CDSCALENDAR_FAILED;
884         }
885         
886         resultCode = sqlite3_step(entriesHandle);
887         
888         if (resultCode == SQLITE_DONE){
889                 deleteResult = CDSCALENDAR_OK;
890         } else {
891                 return CDSCALENDAR_FAILED;
892         }
893         
894         // Update the checksum.
895         
896         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
897         
898         return deleteResult;
899         
902 CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string filename)
904         
905         CDSAddEntryResult addResult;
906         addResult.addEventResult = CDSENTRY_UNITTESTFAIL;
907         
908         // Load the event file.
909         
910         CalendarEventObject eventData;
911         CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
912         
913         // Check the result of the event file load.
914         
915         switch (eventLoadResult){
916                 
917                 case CALENDAROBJECTLOAD_OK:
918                         break;
919                 case CALENDAROBJECTLOAD_MISSING:
920                         addResult.addEventResult = CDSENTRY_MISSINGFILE;
921                         return addResult;
922                 case CALENDAROBJECTLOAD_INVALIDFORMAT:
923                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
924                         return addResult;
925                 case CALENDAROBJECTLOAD_CANNOTOPEN:
926                         addResult.addEventResult = CDSENTRY_CANNOTOPENFILE;
927                         return addResult;
928                 
929         }
930         
931         // Check if calendar exists first.
932         
933         int resultCode;
934         
935         sqlite3_stmt *findHandle;
936         sqlite3_stmt *statementHandle;
937         
938         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
939         
940         if (resultCode != 0){
941                 addResult.addEventResult = CDSENTRY_FAILED;
942                 return addResult;
943         }
944         
945         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
946         
947         if (resultCode != 0){
948                 addResult.addEventResult = CDSENTRY_FAILED;
949                 return addResult;
950         }
951         
952         resultCode = sqlite3_step(findHandle);
953         
954         if (resultCode == SQLITE_ROW){
955                 
956         } else if (resultCode == SQLITE_DONE) {
957                 addResult.addEventResult = CDSENTRY_NOCALENDAR;
958                 return addResult;
959         } else {
960                 addResult.addEventResult = CDSENTRY_FAILED;
961                 return addResult;
962         }
963         
964         // Get the required values from the event object.
965         
966         int eventStartYear = 0;
967         int eventStartMonth = 0;
968         int eventStartDay = 0;
969         int eventStartHour = 0;
970         int eventStartMinute = 0;
971         int eventStartSecond = 0;
972         int eventStartDuration = 0;
973         std::string eventString = "";
974         
975         // Start Date.
976         
977         if (eventData.dateTimeStartData.size() < 16){
978                 
979                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
980                 return addResult;
981                 
982         }
983         
984         eventString = eventData.dateTimeStartData.substr(0,4);
985         
986         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
987                 
988                 eventStartYear = atoi(eventString.c_str());
989                 
990         } else {
991                 
992                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
993                 return addResult;
994                 
995         }
997         eventString = eventData.dateTimeStartData.substr(4,2);
998         
999         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1000                 
1001                 eventStartMonth = atoi(eventString.c_str());
1002                 
1003         } else {
1004                 
1005                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1006                 return addResult;
1007                 
1008         }
1009         
1010         eventString = eventData.dateTimeStartData.substr(6,2);
1011         
1012         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1013                 
1014                 eventStartDay = atoi(eventString.c_str());
1015                 
1016         } else {
1017                 
1018                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1019                 return addResult;
1020                 
1021         }
1023         eventString = eventData.dateTimeStartData.substr(9,2);
1024         
1025         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1026                 
1027                 eventStartHour = atoi(eventString.c_str());
1028                 
1029         } else {
1030                 
1031                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1032                 return addResult;
1033                 
1034         }
1035         
1036         eventString = eventData.dateTimeStartData.substr(11,2);
1037         
1038         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1039                 
1040                 eventStartMinute = atoi(eventString.c_str());
1041                 
1042         } else {
1043                 
1044                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1045                 return addResult;
1046                 
1047         }
1048         
1049         eventString = eventData.dateTimeStartData.substr(13,2);
1050         
1051         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1052                 
1053                 eventStartSecond = atoi(eventString.c_str());
1054                 
1055         } else {
1056                 
1057                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1058                 return addResult;
1059                 
1060         }
1061         
1062         //eventYear = eventStartDate.substr(0, 4);
1063         
1064         // End Date.
1065         
1066         int eventEndYear = 0;
1067         int eventEndMonth = 0;
1068         int eventEndDay = 0;
1069         int eventEndHour = 0;
1070         int eventEndMinute = 0;
1071         int eventEndSecond = 0;
1072         int eventEndDuration = 0;
1073         
1074         if (eventData.dateTimeEndData != ""){
1075         
1076                 if (eventData.dateTimeEndData.size() < 16){
1077                 
1078                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1079                         return addResult;
1080                 
1081                 }
1082         
1083                 eventString = eventData.dateTimeEndData.substr(0,4);
1084         
1085                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1086                 
1087                         eventEndYear = atoi(eventString.c_str());
1088                 
1089                 } else {
1090                 
1091                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1092                         return addResult;
1093                 
1094                 }
1096                 eventString = eventData.dateTimeEndData.substr(4,2);
1097         
1098                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1099                 
1100                         eventEndMonth = atoi(eventString.c_str());
1101                 
1102                 } else {
1103                 
1104                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1105                         return addResult;
1106                 
1107                 }
1108         
1109                 eventString = eventData.dateTimeEndData.substr(6,2);
1110         
1111                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1112                 
1113                         eventEndDay = atoi(eventString.c_str());
1114                 
1115                 } else {
1116                 
1117                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1118                         return addResult;
1119                 
1120                 }
1122                 eventString = eventData.dateTimeEndData.substr(9,2);
1123         
1124                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1125                 
1126                         eventEndHour = atoi(eventString.c_str());
1127                 
1128                 } else {
1129                 
1130                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1131                         return addResult;
1132                 
1133                 }
1134         
1135                 eventString = eventData.dateTimeEndData.substr(11,2);
1136         
1137                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1138                 
1139                         eventEndMinute = atoi(eventString.c_str());
1140                 
1141                 } else {
1142                 
1143                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1144                         return addResult;
1145                 
1146                 }
1147         
1148                 eventString = eventData.dateTimeEndData.substr(13,2);
1149         
1150                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1151                 
1152                         eventEndSecond = atoi(eventString.c_str());
1153                 
1154                 } else {
1155                 
1156                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1157                         return addResult;
1158                 
1159                 }
1160                 
1161         }
1163         eventString = eventData.durationData;
1164         
1165         // Process the duration data.
1166         
1167         int eventDurationWeeks = 0;
1168         int eventDurationDays = 0;
1169         int eventDurationHours = 0;
1170         int eventDurationMinutes = 0;
1171         int eventDurationSeconds = 0;
1172         
1173         // Get the duration (if DTEND hasn't been specified).
1174         
1175         if (eventData.durationData.size() > 0){
1176                 
1177                 bool FoundP = false;
1178                 bool FoundW = false;
1179                 bool DateTimeMode = false;
1180                 
1181                 std::string::iterator eventDataChar = eventData.durationData.begin();
1182                 std::string currentValue = "";
1183                 
1184                 if (*eventDataChar != 'P'){
1185                         
1186                         eventDataChar = eventData.durationData.end();
1187                         
1188                 }
1189                 
1190                 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1191                         
1192                         // Check if value is a digit.
1193                         
1194                         if (isdigit(*eventDataChar)){
1195                                 
1196                                 currentValue += *eventDataChar;
1197                                 
1198                         } else {
1199                                 
1200                                 // Check that the value matches one of the letters.
1201                                 
1202                                 if (*eventDataChar == 'W' && DateTimeMode == false){
1203                                 
1204                                         eventDurationWeeks = atoi(currentValue.c_str());
1205                                         
1206                                 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1208                                         eventDurationDays = atoi(currentValue.c_str());
1209                                         
1210                                 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1211                                         
1212                                         DateTimeMode = true;
1213                                         
1214                                 } else if (*eventDataChar == 'H'){
1216                                         eventDurationHours = atoi(currentValue.c_str());
1217                                         
1218                                 } else if (*eventDataChar == 'M'){
1219                                         
1220                                         eventDurationMinutes = atoi(currentValue.c_str());
1221                                         
1222                                 } else if (*eventDataChar == 'S'){
1223                                 
1224                                         eventDurationSeconds = atoi(currentValue.c_str());
1225                                         
1226                                 }
1227                                         
1228                                 // Reset the current value.
1229                                 
1230                                 currentValue = "";
1231                                 
1232                         }
1233                         
1234                 }
1235                 
1236         }
1237         
1238         // Add the calendar entry.
1239         
1240         std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
1241         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1242         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1243         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1244         " filename)"
1245         " VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
1246         " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20), (?21))";
1247         
1248         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1250         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
1251         
1252         if (resultCode != 0){
1253                 addResult.addEventResult = CDSENTRY_FAILED;
1254                 return addResult;
1255         }
1256         
1257         // Process Entry Name.
1258         
1259         resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1260         
1261         if (resultCode != 0){
1262                 addResult.addEventResult = CDSENTRY_FAILED;
1263                 return addResult;
1264         }
1265         
1266         // Process Entry Description.
1267         
1268         string eventDescription;
1269         
1270         try {
1271                 eventDescription = eventData.descriptionList.at(0);
1272         }
1273         
1274         catch (out_of_range &err){
1275                 eventDescription = "";
1276         }
1277         
1278         resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1279         
1280         if (resultCode != 0){
1281                 addResult.addEventResult = CDSENTRY_FAILED;
1282                 return addResult;
1283         }
1284         
1285         // Process Entry Start Date information.
1286         
1287         resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1288         
1289         if (resultCode != 0){
1290                 addResult.addEventResult = CDSENTRY_FAILED;
1291                 return addResult;
1292         }
1294         resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1295         
1296         if (resultCode != 0){
1297                 addResult.addEventResult = CDSENTRY_FAILED;
1298                 return addResult;
1299         }
1301         resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1302         
1303         if (resultCode != 0){
1304                 addResult.addEventResult = CDSENTRY_FAILED;
1305                 return addResult;
1306         }
1308         resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1309         
1310         if (resultCode != 0){
1311                 addResult.addEventResult = CDSENTRY_FAILED;
1312                 return addResult;
1313         }
1315         resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1316         
1317         if (resultCode != 0){
1318                 addResult.addEventResult = CDSENTRY_FAILED;
1319                 return addResult;
1320         }
1321         
1322         resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1323         
1324         if (resultCode != 0){
1325                 addResult.addEventResult = CDSENTRY_FAILED;
1326                 return addResult;
1327         }
1328         
1329         // Process Entry Start End information.
1330         
1331         resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1332         
1333         if (resultCode != 0){
1334                 addResult.addEventResult = CDSENTRY_FAILED;
1335                 return addResult;
1336         }
1338         resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1339         
1340         if (resultCode != 0){
1341                 addResult.addEventResult = CDSENTRY_FAILED;
1342                 return addResult;
1343         }
1345         resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1346         
1347         if (resultCode != 0){
1348                 addResult.addEventResult = CDSENTRY_FAILED;
1349                 return addResult;
1350         }
1352         resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1353         
1354         if (resultCode != 0){
1355                 addResult.addEventResult = CDSENTRY_FAILED;
1356                 return addResult;
1357         }
1359         resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1360         
1361         if (resultCode != 0){
1362                 addResult.addEventResult = CDSENTRY_FAILED;
1363                 return addResult;
1364         }
1365         
1366         resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1367         
1368         if (resultCode != 0){
1369                 addResult.addEventResult = CDSENTRY_FAILED;
1370                 return addResult;
1371         }
1372         
1373         resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1374         
1375         if (resultCode != 0){
1376                 addResult.addEventResult = CDSENTRY_FAILED;
1377                 return addResult;
1378         }
1379         
1380         resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1381         
1382         if (resultCode != 0){
1383                 addResult.addEventResult = CDSENTRY_FAILED;
1384                 return addResult;
1385         }
1386         
1387         resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1388         
1389         if (resultCode != 0){
1390                 addResult.addEventResult = CDSENTRY_FAILED;
1391                 return addResult;
1392         }
1393         
1394         resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1395         
1396         if (resultCode != 0){
1397                 addResult.addEventResult = CDSENTRY_FAILED;
1398                 return addResult;
1399         }
1400         
1401         resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1402         
1403         if (resultCode != 0){
1404                 addResult.addEventResult = CDSENTRY_FAILED;
1405                 return addResult;
1406         }
1407         
1408         resultCode = sqlite3_bind_text(statementHandle, 21, filename.c_str(), -1, SQLITE_STATIC);
1409         
1410         if (resultCode != 0){
1411                 addResult.addEventResult = CDSENTRY_FAILED;
1412                 return addResult;
1413         }
1414         
1415         resultCode = sqlite3_step(statementHandle);
1416         
1417         if (resultCode != SQLITE_DONE){
1418                 addResult.addEventResult = CDSENTRY_FAILED;
1419                 return addResult;
1420         }
1421         
1422         addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1423         addResult.addEventResult = CDSENTRY_OK;
1424         
1425         // Update the checksum.
1426         
1427         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1428         
1429         return addResult;
1430         
1433 CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string filename)
1435         
1436         CDSEditEntryResult editResult;
1437         editResult.editEventResult = CDSENTRY_UNITTESTFAIL;
1438         
1439         // Load the event file.
1440         
1441         CalendarEventObject eventData;
1442         CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
1443         
1444         // Check the result of the event file load.
1445         
1446         switch (eventLoadResult){
1447                 
1448                 case CALENDAROBJECTLOAD_OK:
1449                         break;
1450                 case CALENDAROBJECTLOAD_MISSING:
1451                         editResult.editEventResult = CDSENTRY_MISSINGFILE;
1452                         return editResult;
1453                 case CALENDAROBJECTLOAD_INVALIDFORMAT:
1454                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1455                         return editResult;
1456                 case CALENDAROBJECTLOAD_CANNOTOPEN:
1457                         editResult.editEventResult = CDSENTRY_CANNOTOPENFILE;
1458                         return editResult;
1459                 
1460         }
1461         
1462         // Check if event exists first.
1463         
1464         int resultCode;
1465         
1466         sqlite3_stmt *findHandle;
1467         sqlite3_stmt *statementHandle;
1468         
1469         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1470         
1471         if (resultCode != 0){
1472                 editResult.editEventResult = CDSENTRY_FAILED;
1473                 return editResult;
1474         }
1475         
1476         resultCode = sqlite3_bind_int(findHandle, 1, eventID);
1477         
1478         if (resultCode != 0){
1479                 editResult.editEventResult = CDSENTRY_FAILED;
1480                 return editResult;
1481         }
1482         
1483         resultCode = sqlite3_step(findHandle);
1484         
1485         if (resultCode == SQLITE_ROW){
1486                 
1487         } else if (resultCode == SQLITE_DONE) {
1488                 editResult.editEventResult = CDSENTRY_NOENTRY;
1489                 return editResult;
1490         } else {
1491                 editResult.editEventResult = CDSENTRY_FAILED;
1492                 return editResult;
1493         }
1494         
1495         // Get the required values from the event object.
1496         
1497         int eventStartYear = 0;
1498         int eventStartMonth = 0;
1499         int eventStartDay = 0;
1500         int eventStartHour = 0;
1501         int eventStartMinute = 0;
1502         int eventStartSecond = 0;
1503         int eventStartDuration = 0;
1504         std::string eventString = "";
1505         
1506         // Start Date.
1507         
1508         if (eventData.dateTimeStartData.size() < 16){
1509                 
1510                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1511                 return editResult;
1512                 
1513         }
1514         
1515         eventString = eventData.dateTimeStartData.substr(0,4);
1516         
1517         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1518                 
1519                 eventStartYear = atoi(eventString.c_str());
1520                 
1521         } else {
1522                 
1523                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1524                 return editResult;
1525                 
1526         }
1528         eventString = eventData.dateTimeStartData.substr(4,2);
1529         
1530         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1531                 
1532                 eventStartMonth = atoi(eventString.c_str());
1533                 
1534         } else {
1535                 
1536                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1537                 return editResult;
1538                 
1539         }
1540         
1541         eventString = eventData.dateTimeStartData.substr(6,2);
1542         
1543         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1544                 
1545                 eventStartDay = atoi(eventString.c_str());
1546                 
1547         } else {
1548                 
1549                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1550                 return editResult;
1551                 
1552         }
1554         eventString = eventData.dateTimeStartData.substr(9,2);
1555         
1556         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1557                 
1558                 eventStartHour = atoi(eventString.c_str());
1559                 
1560         } else {
1561                 
1562                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1563                 return editResult;
1564                 
1565         }
1566         
1567         eventString = eventData.dateTimeStartData.substr(11,2);
1568         
1569         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1570                 
1571                 eventStartMinute = atoi(eventString.c_str());
1572                 
1573         } else {
1574                 
1575                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1576                 return editResult;
1577                 
1578         }
1579         
1580         eventString = eventData.dateTimeStartData.substr(13,2);
1581         
1582         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1583                 
1584                 eventStartSecond = atoi(eventString.c_str());
1585                 
1586         } else {
1587                 
1588                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1589                 return editResult;
1590                 
1591         }
1592         
1593         //eventYear = eventStartDate.substr(0, 4);
1594         
1595         // End Date.
1596         
1597         int eventEndYear = 0;
1598         int eventEndMonth = 0;
1599         int eventEndDay = 0;
1600         int eventEndHour = 0;
1601         int eventEndMinute = 0;
1602         int eventEndSecond = 0;
1603         int eventEndDuration = 0;
1604         
1605         if (eventData.dateTimeEndData != ""){
1606         
1607                 if (eventData.dateTimeEndData.size() < 16){
1608                 
1609                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1610                         return editResult;
1611                 
1612                 }
1613         
1614                 eventString = eventData.dateTimeEndData.substr(0,4);
1615         
1616                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1617                 
1618                         eventEndYear = atoi(eventString.c_str());
1619                 
1620                 } else {
1621                 
1622                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1623                         return editResult;
1624                 
1625                 }
1627                 eventString = eventData.dateTimeEndData.substr(4,2);
1628         
1629                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1630                 
1631                         eventEndMonth = atoi(eventString.c_str());
1632                 
1633                 } else {
1634                 
1635                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1636                         return editResult;
1637                 
1638                 }
1639         
1640                 eventString = eventData.dateTimeEndData.substr(6,2);
1641         
1642                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1643                 
1644                         eventEndDay = atoi(eventString.c_str());
1645                 
1646                 } else {
1647                 
1648                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1649                         return editResult;
1650                 
1651                 }
1653                 eventString = eventData.dateTimeEndData.substr(9,2);
1654         
1655                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1656                 
1657                         eventEndHour = atoi(eventString.c_str());
1658                 
1659                 } else {
1660                 
1661                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1662                         return editResult;
1663                 
1664                 }
1665         
1666                 eventString = eventData.dateTimeEndData.substr(11,2);
1667         
1668                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1669                 
1670                         eventEndMinute = atoi(eventString.c_str());
1671                 
1672                 } else {
1673                 
1674                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1675                         return editResult;
1676                 
1677                 }
1678         
1679                 eventString = eventData.dateTimeEndData.substr(13,2);
1680         
1681                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1682                 
1683                         eventEndSecond = atoi(eventString.c_str());
1684                 
1685                 } else {
1686                 
1687                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1688                         return editResult;
1689                 
1690                 }
1691                 
1692         }
1694         eventString = eventData.durationData;
1695         
1696         // Process the duration data.
1697         
1698         int eventDurationWeeks = 0;
1699         int eventDurationDays = 0;
1700         int eventDurationHours = 0;
1701         int eventDurationMinutes = 0;
1702         int eventDurationSeconds = 0;
1703         
1704         // Get the duration (if DTEND hasn't been specified).
1705         
1706         if (eventData.durationData.size() > 0){
1707                 
1708                 bool FoundP = false;
1709                 bool FoundW = false;
1710                 bool DateTimeMode = false;
1711                 
1712                 std::string::iterator eventDataChar = eventData.durationData.begin();
1713                 std::string currentValue = "";
1714                 
1715                 if (*eventDataChar != 'P'){
1716                         
1717                         eventDataChar = eventData.durationData.end();
1718                         
1719                 }
1720                 
1721                 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1722                         
1723                         // Check if value is a digit.
1724                         
1725                         if (isdigit(*eventDataChar)){
1726                                 
1727                                 currentValue += *eventDataChar;
1728                                 
1729                         } else {
1730                                 
1731                                 // Check that the value matches one of the letters.
1732                                 
1733                                 if (*eventDataChar == 'W' && DateTimeMode == false){
1734                                 
1735                                         eventDurationWeeks = atoi(currentValue.c_str());
1736                                         
1737                                 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1739                                         eventDurationDays = atoi(currentValue.c_str());
1740                                         
1741                                 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1742                                         
1743                                         DateTimeMode = true;
1744                                         
1745                                 } else if (*eventDataChar == 'H'){
1747                                         eventDurationHours = atoi(currentValue.c_str());
1748                                         
1749                                 } else if (*eventDataChar == 'M'){
1750                                         
1751                                         eventDurationMinutes = atoi(currentValue.c_str());
1752                                         
1753                                 } else if (*eventDataChar == 'S'){
1754                                 
1755                                         eventDurationSeconds = atoi(currentValue.c_str());
1756                                         
1757                                 }
1758                                         
1759                                 // Reset the current value.
1760                                 
1761                                 currentValue = "";
1762                                 
1763                         }
1764                         
1765                 }
1766                 
1767         }
1768         
1769         // Add the calendar entry.
1770         
1771         std::string sqlParameter = "UPDATE calendarentries SET entryname=(?2), entrydescription=(?3),"
1772         " entrystartyear=(?4), entrystartmonth=(?5), entrystartday=(?6), entrystarthour=(?7), entrystartminute=(?8), entrystartsecond=(?9),"
1773         " entryendyear=(?10), entryendmonth=(?11), entryendday=(?12), entryendhour=(?13), entryendminute=(?14), entryendsecond=(?15), "
1774         " entrydurationweek=(?16), entrydurationday=(?17), entrydurationhour=(?18), entrydurationminute=(?19), entrydurationsecond=(?20), "
1775         " WHERE id=(?1)";
1776         
1777         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1779         resultCode = sqlite3_bind_int(statementHandle, 1, eventID);
1780         
1781         if (resultCode != 0){
1782                 editResult.editEventResult = CDSENTRY_FAILED;
1783                 return editResult;
1784         }
1785         
1786         // Process Entry Name.
1787         
1788         resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1789         
1790         if (resultCode != 0){
1791                 editResult.editEventResult = CDSENTRY_FAILED;
1792                 return editResult;
1793         }
1794         
1795         // Process Entry Description.
1796         
1797         string eventDescription;
1798         
1799         try {
1800                 eventDescription = eventData.descriptionList.at(0);
1801         }
1802         
1803         catch (out_of_range &err){
1804                 eventDescription = "";
1805         }
1806         
1807         resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1808         
1809         if (resultCode != 0){
1810                 editResult.editEventResult = CDSENTRY_FAILED;
1811                 return editResult;
1812         }
1813         
1814         // Process Entry Start Date information.
1815         
1816         resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1817         
1818         if (resultCode != 0){
1819                 editResult.editEventResult = CDSENTRY_FAILED;
1820                 return editResult;
1821         }
1823         resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1824         
1825         if (resultCode != 0){
1826                 editResult.editEventResult = CDSENTRY_FAILED;
1827                 return editResult;
1828         }
1830         resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1831         
1832         if (resultCode != 0){
1833                 editResult.editEventResult = CDSENTRY_FAILED;
1834                 return editResult;
1835         }
1837         resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1838         
1839         if (resultCode != 0){
1840                 editResult.editEventResult = CDSENTRY_FAILED;
1841                 return editResult;
1842         }
1844         resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1845         
1846         if (resultCode != 0){
1847                 editResult.editEventResult = CDSENTRY_FAILED;
1848                 return editResult;
1849         }
1850         
1851         resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1852         
1853         if (resultCode != 0){
1854                 editResult.editEventResult = CDSENTRY_FAILED;
1855                 return editResult;
1856         }
1857         
1858         // Process Entry Start End information.
1859         
1860         resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1861         
1862         if (resultCode != 0){
1863                 editResult.editEventResult = CDSENTRY_FAILED;
1864                 return editResult;
1865         }
1867         resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1868         
1869         if (resultCode != 0){
1870                 editResult.editEventResult = CDSENTRY_FAILED;
1871                 return editResult;
1872         }
1874         resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1875         
1876         if (resultCode != 0){
1877                 editResult.editEventResult = CDSENTRY_FAILED;
1878                 return editResult;
1879         }
1881         resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1882         
1883         if (resultCode != 0){
1884                 editResult.editEventResult = CDSENTRY_FAILED;
1885                 return editResult;
1886         }
1888         resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1889         
1890         if (resultCode != 0){
1891                 editResult.editEventResult = CDSENTRY_FAILED;
1892                 return editResult;
1893         }
1894         
1895         resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1896         
1897         if (resultCode != 0){
1898                 editResult.editEventResult = CDSENTRY_FAILED;
1899                 return editResult;
1900         }
1901         
1902         resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1903         
1904         if (resultCode != 0){
1905                 editResult.editEventResult = CDSENTRY_FAILED;
1906                 return editResult;
1907         }
1908         
1909         resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1910         
1911         if (resultCode != 0){
1912                 editResult.editEventResult = CDSENTRY_FAILED;
1913                 return editResult;
1914         }
1915         
1916         resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1917         
1918         if (resultCode != 0){
1919                 editResult.editEventResult = CDSENTRY_FAILED;
1920                 return editResult;
1921         }
1922         
1923         resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1924         
1925         if (resultCode != 0){
1926                 editResult.editEventResult = CDSENTRY_FAILED;
1927                 return editResult;
1928         }
1929         
1930         resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1931         
1932         if (resultCode != 0){
1933                 editResult.editEventResult = CDSENTRY_FAILED;
1934                 return editResult;
1935         }
1936         
1937         resultCode = sqlite3_step(statementHandle);
1938         
1939         if (resultCode != SQLITE_DONE){
1940                 editResult.editEventResult = CDSENTRY_FAILED;
1941                 return editResult;
1942         }
1943         
1944         editResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1945         editResult.editEventResult = CDSENTRY_OK;
1946         
1947         // Update the checksum.
1948         
1949         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1950         
1951         return editResult;
1952         
1955 CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
1957         
1958         CDSGetCalendarEntryInfo entryResult;
1959         
1960         // Check if the calendar entry exists.
1961         
1962         int resultCode;
1963         
1964         sqlite3_stmt *findHandle;
1965         sqlite3_stmt *statementHandle;
1966         
1967         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1968         
1969         if (resultCode != 0){
1970                 entryResult.getEventResult = CDSENTRY_FAILED;
1971                 return entryResult;
1972         }
1973         
1974         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
1975         
1976         if (resultCode != 0){
1977                 entryResult.getEventResult = CDSENTRY_FAILED;
1978                 return entryResult;
1979         }
1980         
1981         resultCode = sqlite3_step(findHandle);
1982         
1983         if (resultCode == SQLITE_ROW){
1984                 
1985         } else if (resultCode == SQLITE_DONE) {
1986                 entryResult.getEventResult = CDSENTRY_NOENTRY;
1987                 return entryResult;
1988         } else {
1989                 entryResult.getEventResult = CDSENTRY_FAILED;
1990                 return entryResult;
1991         }
1992         
1993         // Get the calendar entry data.
1994         
1995         std::string sqlParameter = "SELECT entryname, entrydescription,"
1996         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1997         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1998         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1999         " calendarid, id, filename"
2000         " FROM calendarentries WHERE id=(?1)";
2001         
2002         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
2004         if (resultCode != 0){
2005                 entryResult.getEventResult = CDSENTRY_FAILED;
2006                 return entryResult;
2007         }
2008         
2009         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2010         
2011         if (resultCode != 0){
2012                 entryResult.getEventResult = CDSENTRY_FAILED;
2013                 return entryResult;
2014         }
2015         
2016         resultCode = sqlite3_step(statementHandle);
2017         
2018         if (resultCode == SQLITE_ROW){
2019                 
2020                 // Get the calendar entry name,
2021                 
2022                 stringstream entryStream;
2023                 
2024                 entryStream << sqlite3_column_text(statementHandle, 0);
2025                 entryResult.entryName = entryStream.str();
2026                 
2027                 entryStream.str("");
2028                 
2029                 // Get the calendar entry description.
2030                 
2031                 entryStream << sqlite3_column_text(statementHandle, 1);
2032                 entryResult.entryDescription = entryStream.str();               
2034                 entryStream.str("");
2035                 
2036                 // Get the calendar entry filename.
2037                 
2038                 entryStream << sqlite3_column_text(statementHandle, 21);
2039                 entryResult.entryFilename = entryStream.str();
2041                 entryStream.str("");
2042                 
2043                 entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
2044                 entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
2045                 entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
2046                 entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5);
2047                 entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6);
2048                 entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7);
2049                 entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8);
2050                 entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9);
2051                 entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10);
2052                 entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11);             
2053                 entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12);
2054                 entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13);
2055                 entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14);
2056                 entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15);
2057                 entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
2058                 entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
2059                 entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
2060                 entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
2061                 entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
2062                 
2063         } else if (resultCode == SQLITE_DONE) {
2064                 entryResult.getEventResult = CDSENTRY_NOCALENDAR;
2065                 return entryResult;
2066         } else {
2067                 entryResult.getEventResult = CDSENTRY_FAILED;
2068                 return entryResult;
2069         }
2070         
2071         entryResult.getEventResult = CDSENTRY_OK;
2072         
2073         return entryResult;
2074         
2077 CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
2079         
2080         CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL;
2081         
2082         // Check if the calendar entry exists.
2083         
2084         int resultCode;
2085         
2086         sqlite3_stmt *findHandle;
2087         sqlite3_stmt *statementHandle;
2088         
2089         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
2090         
2091         if (resultCode != 0){
2092                 return CDSENTRY_FAILED;
2093         }
2094         
2095         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
2096         
2097         if (resultCode != 0){
2098                 return CDSENTRY_FAILED;
2099         }
2100         
2101         resultCode = sqlite3_step(findHandle);
2102         
2103         if (resultCode == SQLITE_ROW){
2104                 
2105         } else if (resultCode == SQLITE_DONE) {
2106                 return CDSENTRY_NOENTRY;
2107         } else {
2108                 return CDSENTRY_FAILED;
2109         }
2111         // Delete the account.
2112         
2113         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr);
2114         
2115         if (resultCode != 0){
2116                 return CDSENTRY_FAILED;
2117         }
2118         
2119         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2120         
2121         if (resultCode != 0){
2122                 return CDSENTRY_FAILED;
2123         }
2124         
2125         resultCode = sqlite3_step(statementHandle);
2126         
2127         if (resultCode == SQLITE_DONE){
2128                 deleteResult = CDSENTRY_OK;
2129         } else {
2130                 return CDSENTRY_FAILED;
2131         }
2132         
2133         // Update the checksum.
2134         
2135         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2136         
2137         return deleteResult;    
2138         
2141 CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
2142         
2143         CDSEntryList entryList;
2144         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2145         
2146         // Check if calendar exists first.
2147         
2148         int resultCode;
2149         
2150         sqlite3_stmt *findHandle;
2151         sqlite3_stmt *calendarHandle;
2152         
2153         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
2154         
2155         if (resultCode != 0){
2156                 entryList.getEventListResult = CDSENTRY_FAILED;
2157                 return entryList;
2158         }
2159         
2160         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
2161         
2162         if (resultCode != 0){
2163                 entryList.getEventListResult = CDSENTRY_FAILED;
2164                 return entryList;
2165         }
2166         
2167         resultCode = sqlite3_step(findHandle);
2168         
2169         if (resultCode == SQLITE_ROW){
2170                 
2171         } else if (resultCode == SQLITE_DONE) {
2172                 entryList.getEventListResult = CDSENTRY_NOCALENDAR;
2173                 return entryList;
2174         } else {
2175                 entryList.getEventListResult = CDSENTRY_FAILED;
2176                 return entryList;
2177         }
2178         
2179         // Get the list of entry IDs.
2180         
2181         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE calendarid=(?1);", -1, &calendarHandle, nullptr);
2182         
2183         if (resultCode != 0){
2184                 entryList.getEventListResult = CDSENTRY_FAILED;
2185                 return entryList;
2186         }
2187         
2188         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarID);
2189         
2190         if (resultCode != 0){
2191                 entryList.getEventListResult = CDSENTRY_FAILED;
2192                 return entryList;
2193         }
2194         
2195         resultCode = sqlite3_step(calendarHandle);
2196         
2197         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2198         } else {
2199                 entryList.getEventListResult = CDSENTRY_FAILED;
2200                 return entryList;
2201         }
2202         
2203         while (resultCode == SQLITE_ROW){
2204                 
2205                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2206                 
2207                 entryList.entryList.push_back(calendarID);
2208                 
2209                 resultCode = sqlite3_step(calendarHandle);
2210                 
2211         }       
2212         
2213         entryList.getEventListResult = CDSENTRY_OK;
2214         
2215         return entryList;
2216         
2219 CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
2220         
2221         CDSEntryList entryList;
2222         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2223         
2224         // Check if calendar exists first.
2225         
2226         int resultCode;
2228         sqlite3_stmt *calendarHandle;
2229         
2230         // Get the list of entry IDs.
2232         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
2233         
2234         if (resultCode != 0){
2235                 entryList.getEventListResult = CDSENTRY_FAILED;
2236                 return entryList;
2237         }
2238         
2239         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
2240         
2241         if (resultCode != 0){
2242                 entryList.getEventListResult = CDSENTRY_FAILED;
2243                 return entryList;
2244         }
2246         resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
2247         
2248         if (resultCode != 0){
2249                 entryList.getEventListResult = CDSENTRY_FAILED;
2250                 return entryList;
2251         }
2252         
2253         resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
2254         
2255         if (resultCode != 0){
2256                 entryList.getEventListResult = CDSENTRY_FAILED;
2257                 return entryList;
2258         }
2259         
2260         resultCode = sqlite3_step(calendarHandle);
2261         
2262         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2263         } else {
2264                 entryList.getEventListResult = CDSENTRY_FAILED;
2265                 return entryList;
2266         }
2267         
2268         while (resultCode == SQLITE_ROW){
2269                 
2270                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2271                 
2272                 entryList.entryList.push_back(calendarID);
2273                 
2274                 resultCode = sqlite3_step(calendarHandle);
2275                 
2276         }       
2277         
2278         entryList.getEventListResult = CDSENTRY_OK;
2279         
2280         return entryList;
2281         
2284 CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
2285         
2286         CDSCalendarList calendarList;
2287         calendarList.getCalendarListResult = CDSCALENDAR_UNITTESTFAIL;
2288         
2289         // Check if calendar exists first.
2290         
2291         int resultCode;
2292         
2293         sqlite3_stmt *findHandle;
2294         sqlite3_stmt *calendarHandle;
2295         
2296         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
2297         
2298         if (resultCode != 0){
2299                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2300                 return calendarList;
2301         }
2302         
2303         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
2304         
2305         if (resultCode != 0){
2306                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2307                 return calendarList;
2308         }
2309         
2310         resultCode = sqlite3_step(findHandle);
2311         
2312         if (resultCode == SQLITE_ROW){
2313                 
2314         } else if (resultCode == SQLITE_DONE) {
2315                 calendarList.getCalendarListResult = CDSCALENDAR_NOCALENDAR;
2316                 return calendarList;
2317         } else {
2318                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2319                 return calendarList;
2320         }
2321         
2322         // Get the list of entry IDs.
2323         
2324         resultCode = sqlite3_prepare_v2(db, "SELECT id, calendarid FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
2325         
2326         if (resultCode != 0){
2327                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2328                 return calendarList;
2329         }
2330         
2331         resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
2332         
2333         if (resultCode != 0){
2334                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2335                 return calendarList;
2336         }
2337         
2338         resultCode = sqlite3_step(calendarHandle);
2339         
2340         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2341         } else {
2342                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2343                 return calendarList;
2344         }
2345         
2346         while (resultCode == SQLITE_ROW){
2347                 
2348                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2349                 
2350                 stringstream calendarStream;
2351                 
2352                 calendarStream << sqlite3_column_text(calendarHandle, 1);
2353                 
2354                 calendarList.calendarList.push_back(calendarID);
2355                 calendarList.calendarListTextID.push_back(calendarStream.str());
2356                 
2357                 calendarStream.str("");
2358                 
2359                 resultCode = sqlite3_step(calendarHandle);
2360                 
2361         }       
2362         
2363         calendarList.getCalendarListResult = CDSCALENDAR_OK;
2364         
2365         return calendarList;
2366         
2369 CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
2370         
2371         int resultCode;
2372         
2373         // Check if the checksum already exists.
2374         
2375         sqlite3_stmt *findHandle;
2376         sqlite3_stmt *checksumHandle;
2377         
2378         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
2379         
2380         if (resultCode != 0){
2381                 return CDSCHECKSUM_FAILED;
2382         }
2383         
2384         resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2385         
2386         if (resultCode != 0){
2387                 return CDSCHECKSUM_FAILED;
2388         }
2389         
2390         resultCode = sqlite3_step(findHandle);
2391         
2392         if (resultCode == SQLITE_ROW){
2393                 return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
2394         } else if (resultCode == SQLITE_DONE) {
2395                 
2396         } else {
2397                 return CDSCHECKSUM_FAILED;
2398         }
2399         
2400         // Add the checksum to the checksum table.
2401         
2402         resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
2403         
2404         if (resultCode != 0){
2405                 return CDSCHECKSUM_FAILED;
2406         }
2407         
2408         resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2409         
2410         if (resultCode != 0){
2411                 return CDSCHECKSUM_FAILED;
2412         }
2413         
2414         resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
2415         
2416         if (resultCode != 0){
2417                 return CDSCHECKSUM_FAILED;
2418         }
2419         
2420         resultCode = sqlite3_step(checksumHandle);
2421         
2422         if (resultCode != SQLITE_DONE){
2423                 return CDSCHECKSUM_FAILED;
2424         }       
2425         
2426         return CDSCHECKSUM_OK;
2427         
2430 CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
2431         
2432         CDSGetChecksumResult getChecksumResult;
2433         
2434         int resultCode;
2435         
2436         // Check if the checksum already exists.
2437         
2438         sqlite3_stmt *getHandle;
2439         
2440         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2441         
2442         if (resultCode != 0){
2443                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2444                 return getChecksumResult;
2445         }
2446         
2447         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2448         
2449         if (resultCode != 0){
2450                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2451                 return getChecksumResult;
2452         }
2453         
2454         resultCode = sqlite3_step(getHandle);
2455         
2456         if (resultCode == SQLITE_ROW){
2457         } else if (resultCode == SQLITE_DONE) {
2458                 getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
2459                 return getChecksumResult;
2460         } else {
2461                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2462                 return getChecksumResult;
2463         }
2464         
2465         stringstream checksumStream;
2466                 
2467         checksumStream << sqlite3_column_text(getHandle, 0);
2468                 
2469         getChecksumResult.checksumValue = checksumStream.str();
2470         
2471         getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
2472         
2473         return getChecksumResult;
2474         
2477 CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
2478         
2479         int resultCode;
2480         
2481         // Check if the checksum already exists.
2482         
2483         sqlite3_stmt *getHandle;
2484         sqlite3_stmt *statementHandle;
2485         
2486         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2487         
2488         if (resultCode != 0){
2489                 return CDSCHECKSUM_FAILED;
2490         }
2491         
2492         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2493         
2494         if (resultCode != 0){
2495                 return CDSCHECKSUM_FAILED;
2496         }
2497         
2498         resultCode = sqlite3_step(getHandle);
2499         
2500         if (resultCode == SQLITE_ROW){
2501         } else if (resultCode == SQLITE_DONE) {
2502                 return CDSCHECKSUM_NOHASH;
2503         } else {
2504                 return CDSCHECKSUM_FAILED;
2505         }
2506         
2507         // Update the checksum.
2508         
2509         resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
2510         
2511         if (resultCode != 0){
2512                 return CDSCHECKSUM_FAILED;
2513         }
2514         
2515         resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
2516         
2517         if (resultCode != 0){
2518                 return CDSCHECKSUM_FAILED;
2519         }
2520         
2521         resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
2522         
2523         if (resultCode != 0){
2524                 return CDSCHECKSUM_FAILED;
2525         }
2526         
2527         resultCode = sqlite3_step(statementHandle);
2528         
2529         if (resultCode != SQLITE_DONE){
2530                 return CDSCHECKSUM_FAILED;
2531         }
2532         
2533         return CDSCHECKSUM_OK;
2534         
2537 CDSCleanupResult CalendarDataStorage::Clear(){
2538         
2539         // Remove all data from the tables and reset the sequence numbers.
2541         int resultCode; 
2542         sqlite3_stmt *statementHandle;
2543         
2544         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries", -    1, &statementHandle, nullptr);
2545         
2546         if (resultCode != 0){
2547                 cout << "Fail 1" << endl;
2548                 return CDSCLEANUP_FAILED;
2549         }
2550         
2551         resultCode = sqlite3_step(statementHandle);
2552         
2553         if (resultCode != SQLITE_DONE){
2554                 cout << "Fail 2" << endl;
2555                 return CDSCLEANUP_FAILED;
2556         }
2557         
2558         resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendarentries';", -1, &statementHandle, nullptr);
2559         
2560         if (resultCode != 0){
2561                 cout << "Fail 3" << endl;
2562                 cout << sqlite3_errmsg(db) << endl;
2563                 return CDSCLEANUP_FAILED;
2564         }
2566         resultCode = sqlite3_step(statementHandle);
2567         
2568         if (resultCode != SQLITE_DONE){
2569                 cout << "Fail 4" << endl;
2570                 return CDSCLEANUP_FAILED;
2571         }
2573         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars", -1, &statementHandle, nullptr);
2574         
2575         if (resultCode != 0){
2576                 cout << "Fail 5" << endl;
2577                 return CDSCLEANUP_FAILED;
2578         }
2579         
2580         resultCode = sqlite3_step(statementHandle);
2581         
2582         if (resultCode != SQLITE_DONE){
2583                 cout << "Fail 6" << endl;
2584                 return CDSCLEANUP_FAILED;
2585         }
2586         
2587         resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendars';", -1, &statementHandle, nullptr);
2588         
2589         if (resultCode != 0){
2590                 cout << "Fail 7" << endl;
2591                 return CDSCLEANUP_FAILED;
2592         }
2594         resultCode = sqlite3_step(statementHandle);
2595         
2596         if (resultCode != SQLITE_DONE){
2597                 cout << "Fail 8" << endl;
2598                 return CDSCLEANUP_FAILED;
2599         }
2600         
2601         resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts", -1, &statementHandle, nullptr);
2602         
2603         if (resultCode != 0){
2604                 cout << "Fail 9" << endl;
2605                 return CDSCLEANUP_FAILED;
2606         }
2607         
2608         resultCode = sqlite3_step(statementHandle);
2609         
2610         if (resultCode != SQLITE_DONE){
2611                 cout << "Fail 10" << endl;
2612                 return CDSCLEANUP_FAILED;
2613         }
2614         
2615         resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='accounts'", -1, &statementHandle, nullptr);
2616         
2617         if (resultCode != 0){
2618                 cout << "Fail 11" << endl;
2619                 return CDSCLEANUP_FAILED;
2620         }
2622         resultCode = sqlite3_step(statementHandle);
2623         
2624         if (resultCode != SQLITE_DONE){
2625                 cout << "Fail 12" << endl;
2626                 return CDSCLEANUP_FAILED;
2627         }
2628         
2629         // Update the checksum.
2631         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2632         
2633         return CDSCLEANUP_OK;
2634         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy