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