Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmMain: Implemented code to hide/show calendar entries
[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 "
41                 ", name TEXT"
42                 ");";
43         
44         resultCode = sqlite3_exec(db, accountsTableString, callback, nullptr, &setupTablesErrMsg);
45         
46         if (resultCode != 0){
47                 DataStorageInitOK = false;
48                 return;
49         }
50         
51         // Setup the calendars table.
52         
53         const char *calendarTableString;
54         calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY "
55                 ", accountid INTEGER"
56                 ", name TEXT"
57                 ", calendarid TEXT"
58                 ", colour TEXT"
59                 ");";
60         
61         resultCode = sqlite3_exec(db, calendarTableString, callback, nullptr, &setupTablesErrMsg);
62         
63         if (resultCode != 0){
64                 DataStorageInitOK = false;
65                 return;
66         }
67         
68         // Setup the calendar entries table.
69         
70         const char *calendarentriesTableString;
71         calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY "
72                 ", calendarid INTEGER"
73                 ", entryname TEXT"
74                 ", entrydescription TEXT"
75                 ", entrystartyear INTEGER"
76                 ", entrystartmonth INTEGER"
77                 ", entrystartday INTEGER"
78                 ", entrystarthour INTEGER"
79                 ", entrystartminute INTEGER"
80                 ", entrystartsecond INTEGER"
81                 ", entryendyear INTEGER"
82                 ", entryendmonth INTEGER"
83                 ", entryendday INTEGER"
84                 ", entryendhour INTEGER"
85                 ", entryendminute INTEGER"
86                 ", entryendsecond INTEGER"
87                 ", entrydurationweek INTEGER"
88                 ", entrydurationday INTEGER"
89                 ", entrydurationhour INTEGER"
90                 ", entrydurationminute INTEGER"
91                 ", entrydurationsecond INTEGER"
92                 ");";
93         
94         resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg);
95         
96         if (resultCode != 0){
97                 DataStorageInitOK = false;
98                 return;
99         }
101         // Setup the checksums table.
102         
103         const char *checksumsTableString;
104         checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
105                 ", checksumvalue TEXT);";
106         
107         resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
108         
109         if (resultCode != 0){
110                 DataStorageInitOK = false;
111                 return;
112         }
113         
114         // Setup internal checksums.
115         
116         CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
117         
118         if (addChecksum != CDSCHECKSUM_OK){
119                 DataStorageInitOK = false;
120                 return;
121         }
122         
123         DataStorageInitOK = true;
124         
127 bool CalendarDataStorage::DidInitOK()
129         
130         return DataStorageInitOK;
131         
134 CDSAccountResult CalendarDataStorage::AddAccount(string accountName)
136         
137         CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL;
138         int resultCode;
139         
140         sqlite3_stmt *statementHandle;
141         
142         resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name) VALUES(?1);", -1, &statementHandle, nullptr);
143         
144         if (resultCode != 0){
145                 return CDSACCOUNT_FAILED;
146         }
147         
148         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
149         
150         if (resultCode != 0){
151                 return CDSACCOUNT_FAILED;
152         }
153         
154         resultCode = sqlite3_step(statementHandle);
155         
156         if (resultCode != SQLITE_DONE){
157                 return CDSACCOUNT_FAILED;
158         }
159         
160         // Update the checksum.
161         
162         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
163         
164         addResult = CDSACCOUNT_OK;
165         
166         return addResult;
167         
170 CDSAccountList CalendarDataStorage::GetAccountList()
172         
173         CDSAccountList accountList;
174         
175         // Check if calendar exists first.
176         
177         int resultCode = 0;
178         
179         sqlite3_stmt *statementHandle;
180         
181         resultCode = sqlite3_prepare_v2(db, "SELECT id, name from accounts;", -1, &statementHandle, nullptr);
182         
183         if (resultCode != 0){
184                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
185                 return accountList;
186         }
187         
188         if (resultCode != 0){
189                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
190                 return accountList;
191         }
192         
193         resultCode = sqlite3_step(statementHandle);
194         
195         if (resultCode == SQLITE_ROW){
196                 
197         } else if (resultCode == SQLITE_DONE) {
198                 accountList.getAccountListResult = CDSACCOUNT_NOACCOUNT;
199                 return accountList;
200         } else {
201                 accountList.getAccountListResult = CDSACCOUNT_FAILED;
202                 return accountList;
203         }
204         
205         while (resultCode == SQLITE_ROW){
206                 
207                 CDSGetAccountInfo accountInfo;
208                 
209                 accountInfo.accountID = sqlite3_column_int(statementHandle, 0);
210                 
211                 stringstream calendarStream;
212                 
213                 calendarStream << sqlite3_column_text(statementHandle, 1);
214                 
215                 accountInfo.accountName = calendarStream.str();
216                 accountInfo.accountInfoResult = CDSACCOUNT_OK;
217                 
218                 accountList.accountList.push_back(accountInfo);
219                 
220                 resultCode = sqlite3_step(statementHandle);
221                 
222         }       
223         
224         accountList.getAccountListResult = CDSACCOUNT_OK;
225         
226         return accountList;
227         
230 CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName)
232         
233         CDSGetAccountInfo accountInfo;
234         accountInfo.accountInfoResult = CDSACCOUNT_UNITTESTFAIL;
235         int resultCode;
236         
237         sqlite3_stmt *statementHandle;
238         
239         resultCode = sqlite3_prepare_v2(db, "SELECT name, id FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
241         if (resultCode != 0){
242                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
243                 return accountInfo;
244         }
245         
246         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
247         
248         if (resultCode != 0){
249                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
250                 return accountInfo;
251         }
252         
253         resultCode = sqlite3_step(statementHandle);
254         
255         if (resultCode == SQLITE_DONE){
256                 
257                 accountInfo.accountInfoResult = CDSACCOUNT_NOACCOUNT;
258                 return accountInfo;
259                 
260         } else if (resultCode == SQLITE_ROW){
261                 
262                 // Get the result data.
263                 
264                 stringstream accountNameStream;
265                 
266                 accountNameStream << sqlite3_column_text(statementHandle, 0);
267                 
268                 accountInfo.accountName = accountNameStream.str();
269                 accountInfo.accountID = sqlite3_column_int(statementHandle, 1);
270                 
271                 accountInfo.accountInfoResult = CDSACCOUNT_OK;
272                 return accountInfo;
273                 
274         } else {
275                 
276                 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
277                 return accountInfo;
278                 
279         }
280         
281         return accountInfo;
282         
285 CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accountName)
287         
288         CDSAccountResult updateResult = CDSACCOUNT_UNITTESTFAIL;
289         int resultCode;
290         
291         sqlite3_stmt *findHandle;
292         sqlite3_stmt *statementHandle;
293         
294         // Check if account exists first.
295         
296         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
297         
298         if (resultCode != 0){
299                 return CDSACCOUNT_FAILED;
300         }
301         
302         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
303         
304         if (resultCode != 0){
305                 return CDSACCOUNT_FAILED;
306         }
307         
308         resultCode = sqlite3_step(findHandle);
309         
310         if (resultCode == SQLITE_ROW){
311                 
312         } else if (resultCode == SQLITE_DONE) {
313                 return CDSACCOUNT_NOACCOUNT;
314         } else {
315                 return CDSACCOUNT_FAILED;
316         }
317         
318         // Update the account.
319         
320         resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
321         
322         if (resultCode != 0){
323                 return CDSACCOUNT_FAILED;
324         }
325         
326         resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
327         
328         if (resultCode != 0){
329                 return CDSACCOUNT_FAILED;
330         }
331         
332         resultCode = sqlite3_bind_int(statementHandle, 2, accountID);
333         
334         if (resultCode != 0){
335                 return CDSACCOUNT_FAILED;
336         }
337         
338         resultCode = sqlite3_step(statementHandle);
339         
340         if (resultCode != SQLITE_DONE){
341                 return CDSACCOUNT_FAILED;
342         }
343         
344         // Update the checksum.
345         
346         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
347         
348         updateResult = CDSACCOUNT_OK;
349         
350         return updateResult;
351         
354 CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
356         
357         CDSAccountResult deleteResult = CDSACCOUNT_UNITTESTFAIL;
358         
359         // Check if account exists first.
360         
361         sqlite3_stmt *findHandle;
362         sqlite3_stmt *statementHandle;
363         sqlite3_stmt *calendarHandle;
364         int resultCode;
365         
366         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
367         
368         if (resultCode != 0){
369                 return CDSACCOUNT_FAILED;
370         }
371         
372         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
373         
374         if (resultCode != 0){
375                 return CDSACCOUNT_FAILED;
376         }
377         
378         resultCode = sqlite3_step(findHandle);
379         
380         if (resultCode == SQLITE_ROW){
381                 
382         } else if (resultCode == SQLITE_DONE) {
383                 return CDSACCOUNT_NOACCOUNT;
384         } else {
385                 return CDSACCOUNT_FAILED;
386         }
387         
388         // Delete the account.
389         
390         resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts WHERE id=(?1);", -1, &statementHandle, nullptr);
391         
392         if (resultCode != 0){
393                 return CDSACCOUNT_FAILED;
394         }
395         
396         resultCode = sqlite3_bind_int(statementHandle, 1, accountID);
397         
398         if (resultCode != 0){
399                 return CDSACCOUNT_FAILED;
400         }
401         
402         resultCode = sqlite3_step(statementHandle);
403         
404         if (resultCode == SQLITE_DONE){
405                 //deleteResult = CDSACCOUNT_OK;
406         } else {
407                 return CDSACCOUNT_FAILED;
408         }
409         
410         // Get the calendar IDs and delete each calendar (and associated entries).
411         
412         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
413         
414         if (resultCode != 0){
415                 return CDSACCOUNT_FAILED;
416         }
417         
418         resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
419         
420         if (resultCode != 0){
421                 return CDSACCOUNT_FAILED;
422         }
423         
424         resultCode = sqlite3_step(calendarHandle);
425         
426         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
427                 deleteResult = CDSACCOUNT_OK;
428         } else {
429                 return CDSACCOUNT_FAILED;
430         }
431         
432         while (resultCode == SQLITE_ROW){
433                 
434                 int calendarDeleteID = sqlite3_column_int(calendarHandle, 0);
435                 
436                 DeleteCalendar(calendarDeleteID);
437                 
438                 resultCode = sqlite3_step(calendarHandle);
439                 
440         }
441         
442         // Update the checksum.
443         
444         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
445         
446         deleteResult = CDSACCOUNT_OK;
447         
448         return deleteResult;
449         
450         
453 CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour)
456         CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
457         int resultCode;
458         
459         sqlite3_stmt *statementHandle;
460         
461         resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour) VALUES(?1, ?2, ?3, ?4);", -1, &statementHandle, nullptr);
462         
463         if (resultCode != 0){
464                 return CDSCALENDAR_FAILED;
465         }
466         
467         resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
468         
469         if (resultCode != 0){
470                 return CDSCALENDAR_FAILED;
471         }
472         
473         resultCode = sqlite3_bind_text(statementHandle, 2, calendarID.c_str(), -1, SQLITE_STATIC);
475         if (resultCode != 0){
476                 return CDSCALENDAR_FAILED;
477         }
479         resultCode = sqlite3_bind_int(statementHandle, 3, accountID);
481         if (resultCode != 0){
482                 return CDSCALENDAR_FAILED;
483         }
484         
485         resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).c_str(), -1, SQLITE_STATIC);
487         if (resultCode != 0){
488                 return CDSCALENDAR_FAILED;
489         }
490         
491         resultCode = sqlite3_step(statementHandle);
492         
493         if (resultCode != SQLITE_DONE){
494                 return CDSCALENDAR_FAILED;
495         }
496         
497         // Update the checksum.
498         
499         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
500         
501         addResult = CDSCALENDAR_OK;
502         
503         return addResult;
504         
507 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std::string calendarTextID)
509         
510         CDSGetAccountInfo accountResult;
511         CDSGetCalendarInfo calendarResult;
512         sqlite3_stmt *statementHandle;
513         int resultCode;
514         
515         // Check if the account exists.
516         
517         accountResult = GetAccount(accountName);
518         
519         switch (accountResult.accountInfoResult){
520                 
521                 case CDSACCOUNT_OK:
522                         calendarResult.accountName = accountResult.accountName;
523                         calendarResult.accountInfoResult = CDSACCOUNT_OK;
524                         break;
525                 case CDSACCOUNT_FAILED:
526                         calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
527                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
528                         return calendarResult;
529                 case CDSACCOUNT_NOACCOUNT:
530                         calendarResult.accountInfoResult = CDSACCOUNT_NOACCOUNT;
531                         calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
532                         return calendarResult;
533                 
534         }
535         
536         // Check if the calendar exists.
537         
538         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
539         
540         if (resultCode != 0){
541                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
542                 return calendarResult;
543         }
545         resultCode = sqlite3_bind_int(statementHandle, 1, accountResult.accountID);
546         
547         if (resultCode != 0){
548                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
549                 return calendarResult;
550         }
551         
552         resultCode = sqlite3_bind_text(statementHandle, 2, calendarTextID.c_str(), -1, SQLITE_STATIC);
553         
554         if (resultCode != 0){
555                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
556                 return calendarResult;
557         }
558         
559         resultCode = sqlite3_step(statementHandle);
560         
561         if (resultCode == SQLITE_DONE){
562                 
563                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
564                 return calendarResult;
565                 
566         } else if (resultCode == SQLITE_ROW){
567                 
568                 // Get the calendar name.
569                 
570                 stringstream calendarStream;
571                 
572                 calendarStream << sqlite3_column_text(statementHandle, 2);
573                 calendarResult.calendarName = calendarStream.str();
574                 
575                 calendarStream.str("");
576                 calendarStream << sqlite3_column_text(statementHandle, 3);
577                 calendarResult.calendarTextID = calendarStream.str();
579                 calendarStream.str("");
580                 calendarStream << sqlite3_column_text(statementHandle, 4);
581                 calendarResult.calendarColour = calendarStream.str();
582                 
583                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
584                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
585                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
586                 return calendarResult;
587                 
588         } else {
589                 
590                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
591                 return calendarResult;
592                 
593         }
594         
595         return calendarResult;
596         
599 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
601         
602         CDSGetCalendarInfo calendarResult;
603         sqlite3_stmt *statementHandle;
604         int resultCode;
605         
606         // Check if the calendar exists.
607         
608         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
609         
610         if (resultCode != 0){
611                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
612                 return calendarResult;
613         }
615         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
616         
617         if (resultCode != 0){
618                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
619                 return calendarResult;
620         }
621         
622         resultCode = sqlite3_step(statementHandle);
623         
624         if (resultCode == SQLITE_DONE){
625                 
626                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
627                 return calendarResult;
628                 
629         } else if (resultCode == SQLITE_ROW){
630                 
631                 // Get the calendar data.
632                 
633                 stringstream calendarStream;
634                 
635                 calendarStream << sqlite3_column_text(statementHandle, 2);
636                 calendarResult.calendarName = calendarStream.str();
637                 
638                 calendarStream.str("");
639                 calendarStream << sqlite3_column_text(statementHandle, 3);
640                 calendarResult.calendarTextID = calendarStream.str();
642                 calendarStream.str("");
643                 calendarStream << sqlite3_column_text(statementHandle, 4);
644                 calendarResult.calendarColour = calendarStream.str();
645                 
646                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
647                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
648                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
649                 return calendarResult;
650                 
651         } else {
652                 
653                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
654                 return calendarResult;
655                 
656         }
657         
658         return calendarResult;
659         
663 CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName)
665         
666         CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
667         int resultCode;
668         
669         sqlite3_stmt *findHandle;
670         sqlite3_stmt *statementHandle;
671         
672         // Check if calendar exists first.
673         
674         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
675         
676         if (resultCode != 0){
677                 return CDSCALENDAR_FAILED;
678         }
679         
680         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
681         
682         if (resultCode != 0){
683                 return CDSCALENDAR_FAILED;
684         }
685         
686         resultCode = sqlite3_step(findHandle);
687         
688         if (resultCode == SQLITE_ROW){
689                 
690         } else if (resultCode == SQLITE_DONE) {
691                 return CDSCALENDAR_NOCALENDAR;
692         } else {
693                 return CDSCALENDAR_FAILED;
694         }
695         
696         // Update the account.
697         
698         resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
699         
700         if (resultCode != 0){
701                 return CDSCALENDAR_FAILED;
702         }
703         
704         resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
705         
706         if (resultCode != 0){
707                 return CDSCALENDAR_FAILED;
708         }
709         
710         resultCode = sqlite3_bind_int(statementHandle, 2, calendarID);
711         
712         if (resultCode != 0){
713                 return CDSCALENDAR_FAILED;
714         }
715         
716         resultCode = sqlite3_step(statementHandle);
717         
718         if (resultCode != SQLITE_DONE){
719                 return CDSCALENDAR_FAILED;
720         }
721         
722         // Update the checksum.
723         
724         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
725         
726         updateResult = CDSCALENDAR_OK;
727         
728         return updateResult;
729         
732 CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
734         
735         CDSCalendarResult deleteResult = CDSCALENDAR_UNITTESTFAIL;
736         
737         // Check if account exists first.
738         
739         sqlite3_stmt *findHandle;
740         sqlite3_stmt *statementHandle;
741         sqlite3_stmt *entriesHandle;
742         int resultCode;
743         
744         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
745         
746         if (resultCode != 0){
747                 return CDSCALENDAR_FAILED;
748         }
749         
750         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
751         
752         if (resultCode != 0){
753                 return CDSCALENDAR_FAILED;
754         }
755         
756         resultCode = sqlite3_step(findHandle);
757         
758         if (resultCode == SQLITE_ROW){
759                 
760         } else if (resultCode == SQLITE_DONE) {
761                 return CDSCALENDAR_NOCALENDAR;
762         } else {
763                 return CDSCALENDAR_FAILED;
764         }
765         
766         // Delete the calendar.
767         
768         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
769         
770         if (resultCode != 0){
771                 return CDSCALENDAR_FAILED;
772         }
773         
774         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
775         
776         if (resultCode != 0){
777                 return CDSCALENDAR_FAILED;
778         }
779         
780         resultCode = sqlite3_step(statementHandle);
781         
782         if (resultCode != SQLITE_DONE){
783                 return CDSCALENDAR_FAILED;
784         }
785         
786         // Delete the calendar entries.
787         
788         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE calendarid=(?1);", -1, &entriesHandle, nullptr);
789         
790         if (resultCode != 0){
791                 return CDSCALENDAR_FAILED;
792         }
793         
794         resultCode = sqlite3_bind_int(entriesHandle, 1, calendarID);
795         
796         if (resultCode != 0){
797                 return CDSCALENDAR_FAILED;
798         }
799         
800         resultCode = sqlite3_step(entriesHandle);
801         
802         if (resultCode == SQLITE_DONE){
803                 deleteResult = CDSCALENDAR_OK;
804         } else {
805                 return CDSCALENDAR_FAILED;
806         }
807         
808         // Update the checksum.
809         
810         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
811         
812         return deleteResult;
813         
816 CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string filename)
818         
819         CDSAddEntryResult addResult;
820         addResult.addEventResult = CDSENTRY_UNITTESTFAIL;
821         
822         // Load the event file.
823         
824         CalendarEventObject eventData;
825         CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
826         
827         // Check the result of the event file load.
828         
829         switch (eventLoadResult){
830                 
831                 case CALENDAROBJECTLOAD_OK:
832                         break;
833                 case CALENDAROBJECTLOAD_MISSING:
834                         addResult.addEventResult = CDSENTRY_MISSINGFILE;
835                         return addResult;
836                 case CALENDAROBJECTLOAD_INVALIDFORMAT:
837                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
838                         return addResult;
839                 case CALENDAROBJECTLOAD_CANNOTOPEN:
840                         addResult.addEventResult = CDSENTRY_CANNOTOPENFILE;
841                         return addResult;
842                 
843         }
844         
845         // Check if calendar exists first.
846         
847         int resultCode;
848         
849         sqlite3_stmt *findHandle;
850         sqlite3_stmt *statementHandle;
851         
852         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
853         
854         if (resultCode != 0){
855                 addResult.addEventResult = CDSENTRY_FAILED;
856                 return addResult;
857         }
858         
859         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
860         
861         if (resultCode != 0){
862                 addResult.addEventResult = CDSENTRY_FAILED;
863                 return addResult;
864         }
865         
866         resultCode = sqlite3_step(findHandle);
867         
868         if (resultCode == SQLITE_ROW){
869                 
870         } else if (resultCode == SQLITE_DONE) {
871                 addResult.addEventResult = CDSENTRY_NOCALENDAR;
872                 return addResult;
873         } else {
874                 addResult.addEventResult = CDSENTRY_FAILED;
875                 return addResult;
876         }
877         
878         // Get the required values from the event object.
879         
880         int eventStartYear = 0;
881         int eventStartMonth = 0;
882         int eventStartDay = 0;
883         int eventStartHour = 0;
884         int eventStartMinute = 0;
885         int eventStartSecond = 0;
886         int eventStartDuration = 0;
887         std::string eventString = "";
888         
889         // Start Date.
890         
891         if (eventData.DateTimeStartData.size() < 16){
892                 
893                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
894                 return addResult;
895                 
896         }
897         
898         eventString = eventData.DateTimeStartData.substr(0,4);
899         
900         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
901                 
902                 eventStartYear = atoi(eventString.c_str());
903                 
904         } else {
905                 
906                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
907                 return addResult;
908                 
909         }
911         eventString = eventData.DateTimeStartData.substr(4,2);
912         
913         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
914                 
915                 eventStartMonth = atoi(eventString.c_str());
916                 
917         } else {
918                 
919                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
920                 return addResult;
921                 
922         }
923         
924         eventString = eventData.DateTimeStartData.substr(6,2);
925         
926         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
927                 
928                 eventStartDay = atoi(eventString.c_str());
929                 
930         } else {
931                 
932                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
933                 return addResult;
934                 
935         }
937         eventString = eventData.DateTimeStartData.substr(9,2);
938         
939         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
940                 
941                 eventStartHour = atoi(eventString.c_str());
942                 
943         } else {
944                 
945                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
946                 return addResult;
947                 
948         }
949         
950         eventString = eventData.DateTimeStartData.substr(11,2);
951         
952         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
953                 
954                 eventStartMinute = atoi(eventString.c_str());
955                 
956         } else {
957                 
958                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
959                 return addResult;
960                 
961         }
962         
963         eventString = eventData.DateTimeStartData.substr(13,2);
964         
965         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
966                 
967                 eventStartSecond = atoi(eventString.c_str());
968                 
969         } else {
970                 
971                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
972                 return addResult;
973                 
974         }
975         
976         //eventYear = eventStartDate.substr(0, 4);
977         
978         // End Date.
979         
980         int eventEndYear = 0;
981         int eventEndMonth = 0;
982         int eventEndDay = 0;
983         int eventEndHour = 0;
984         int eventEndMinute = 0;
985         int eventEndSecond = 0;
986         int eventEndDuration = 0;
987         
988         if (eventData.DateTimeEndData != ""){
989         
990                 if (eventData.DateTimeEndData.size() < 16){
991                 
992                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
993                         return addResult;
994                 
995                 }
996         
997                 eventString = eventData.DateTimeEndData.substr(0,4);
998         
999                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1000                 
1001                         eventEndYear = atoi(eventString.c_str());
1002                 
1003                 } else {
1004                 
1005                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1006                         return addResult;
1007                 
1008                 }
1010                 eventString = eventData.DateTimeEndData.substr(4,2);
1011         
1012                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1013                 
1014                         eventEndMonth = atoi(eventString.c_str());
1015                 
1016                 } else {
1017                 
1018                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1019                         return addResult;
1020                 
1021                 }
1022         
1023                 eventString = eventData.DateTimeEndData.substr(6,2);
1024         
1025                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1026                 
1027                         eventEndDay = atoi(eventString.c_str());
1028                 
1029                 } else {
1030                 
1031                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1032                         return addResult;
1033                 
1034                 }
1036                 eventString = eventData.DateTimeEndData.substr(9,2);
1037         
1038                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1039                 
1040                         eventEndHour = atoi(eventString.c_str());
1041                 
1042                 } else {
1043                 
1044                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1045                         return addResult;
1046                 
1047                 }
1048         
1049                 eventString = eventData.DateTimeEndData.substr(11,2);
1050         
1051                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1052                 
1053                         eventEndMinute = atoi(eventString.c_str());
1054                 
1055                 } else {
1056                 
1057                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1058                         return addResult;
1059                 
1060                 }
1061         
1062                 eventString = eventData.DateTimeEndData.substr(13,2);
1063         
1064                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1065                 
1066                         eventEndSecond = atoi(eventString.c_str());
1067                 
1068                 } else {
1069                 
1070                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1071                         return addResult;
1072                 
1073                 }
1074                 
1075         }
1077         eventString = eventData.DurationData;
1078         
1079         // Process the duration data.
1080         
1081         int eventDurationWeeks = 0;
1082         int eventDurationDays = 0;
1083         int eventDurationHours = 0;
1084         int eventDurationMinutes = 0;
1085         int eventDurationSeconds = 0;
1086         
1087         // Get the duration (if DTEND hasn't been specified).
1088         
1089         if (eventData.DurationData.size() > 0){
1090                 
1091                 bool FoundP = false;
1092                 bool FoundW = false;
1093                 bool DateTimeMode = false;
1094                 
1095                 std::string::iterator eventDataChar = eventData.DurationData.begin();
1096                 std::string currentValue = "";
1097                 
1098                 if (*eventDataChar != 'P'){
1099                         
1100                         eventDataChar = eventData.DurationData.end();
1101                         
1102                 }
1103                 
1104                 for(eventDataChar; eventDataChar != eventData.DurationData.end(); eventDataChar++){
1105                         
1106                         // Check if value is a digit.
1107                         
1108                         if (isdigit(*eventDataChar)){
1109                                 
1110                                 currentValue += *eventDataChar;
1111                                 
1112                         } else {
1113                                 
1114                                 // Check that the value matches one of the letters.
1115                                 
1116                                 if (*eventDataChar == 'W' && DateTimeMode == false){
1117                                 
1118                                         eventDurationWeeks = atoi(currentValue.c_str());
1119                                         
1120                                 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1122                                         eventDurationDays = atoi(currentValue.c_str());
1123                                         
1124                                 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1125                                         
1126                                         DateTimeMode = true;
1127                                         
1128                                 } else if (*eventDataChar == 'H'){
1130                                         eventDurationHours = atoi(currentValue.c_str());
1131                                         
1132                                 } else if (*eventDataChar == 'M'){
1133                                         
1134                                         eventDurationMinutes = atoi(currentValue.c_str());
1135                                         
1136                                 } else if (*eventDataChar == 'S'){
1137                                 
1138                                         eventDurationSeconds = atoi(currentValue.c_str());
1139                                         
1140                                 }
1141                                         
1142                                 // Reset the current value.
1143                                 
1144                                 currentValue = "";
1145                                 
1146                         }
1147                         
1148                 }
1149                 
1150         }
1151         
1152         // Add the calendar entry.
1153         
1154         std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
1155         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1156         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1157         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond)"
1158         " VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
1159         " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20))";
1160         
1161         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1163         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
1164         
1165         if (resultCode != 0){
1166                 addResult.addEventResult = CDSENTRY_FAILED;
1167                 return addResult;
1168         }
1169         
1170         // Process Entry Name.
1171         
1172         resultCode = sqlite3_bind_text(statementHandle, 2, eventData.SummaryData.c_str(), -1, SQLITE_STATIC);
1173         
1174         if (resultCode != 0){
1175                 addResult.addEventResult = CDSENTRY_FAILED;
1176                 return addResult;
1177         }
1178         
1179         // Process Entry Description.
1180         
1181         string eventDescription;
1182         
1183         try {
1184                 eventDescription = eventData.DescriptionList.at(0);
1185         }
1186         
1187         catch (out_of_range &err){
1188                 eventDescription = "";
1189         }
1190         
1191         resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1192         
1193         if (resultCode != 0){
1194                 addResult.addEventResult = CDSENTRY_FAILED;
1195                 return addResult;
1196         }
1197         
1198         // Process Entry Start Date information.
1199         
1200         resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1201         
1202         if (resultCode != 0){
1203                 addResult.addEventResult = CDSENTRY_FAILED;
1204                 return addResult;
1205         }
1207         resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1208         
1209         if (resultCode != 0){
1210                 addResult.addEventResult = CDSENTRY_FAILED;
1211                 return addResult;
1212         }
1214         resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1215         
1216         if (resultCode != 0){
1217                 addResult.addEventResult = CDSENTRY_FAILED;
1218                 return addResult;
1219         }
1221         resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1222         
1223         if (resultCode != 0){
1224                 addResult.addEventResult = CDSENTRY_FAILED;
1225                 return addResult;
1226         }
1228         resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1229         
1230         if (resultCode != 0){
1231                 addResult.addEventResult = CDSENTRY_FAILED;
1232                 return addResult;
1233         }
1234         
1235         resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1236         
1237         if (resultCode != 0){
1238                 addResult.addEventResult = CDSENTRY_FAILED;
1239                 return addResult;
1240         }
1241         
1242         // Process Entry Start End information.
1243         
1244         resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1245         
1246         if (resultCode != 0){
1247                 addResult.addEventResult = CDSENTRY_FAILED;
1248                 return addResult;
1249         }
1251         resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1252         
1253         if (resultCode != 0){
1254                 addResult.addEventResult = CDSENTRY_FAILED;
1255                 return addResult;
1256         }
1258         resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1259         
1260         if (resultCode != 0){
1261                 addResult.addEventResult = CDSENTRY_FAILED;
1262                 return addResult;
1263         }
1265         resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1266         
1267         if (resultCode != 0){
1268                 addResult.addEventResult = CDSENTRY_FAILED;
1269                 return addResult;
1270         }
1272         resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1273         
1274         if (resultCode != 0){
1275                 addResult.addEventResult = CDSENTRY_FAILED;
1276                 return addResult;
1277         }
1278         
1279         resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1280         
1281         if (resultCode != 0){
1282                 addResult.addEventResult = CDSENTRY_FAILED;
1283                 return addResult;
1284         }
1285         
1286         resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1287         
1288         if (resultCode != 0){
1289                 addResult.addEventResult = CDSENTRY_FAILED;
1290                 return addResult;
1291         }
1292         
1293         resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1294         
1295         if (resultCode != 0){
1296                 addResult.addEventResult = CDSENTRY_FAILED;
1297                 return addResult;
1298         }
1299         
1300         resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1301         
1302         if (resultCode != 0){
1303                 addResult.addEventResult = CDSENTRY_FAILED;
1304                 return addResult;
1305         }
1306         
1307         resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1308         
1309         if (resultCode != 0){
1310                 addResult.addEventResult = CDSENTRY_FAILED;
1311                 return addResult;
1312         }
1313         
1314         resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1315         
1316         if (resultCode != 0){
1317                 addResult.addEventResult = CDSENTRY_FAILED;
1318                 return addResult;
1319         }
1320         
1321         resultCode = sqlite3_step(statementHandle);
1322         
1323         if (resultCode != SQLITE_DONE){
1324                 addResult.addEventResult = CDSENTRY_FAILED;
1325                 return addResult;
1326         }
1327         
1328         addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1329         addResult.addEventResult = CDSENTRY_OK;
1330         
1331         // Update the checksum.
1332         
1333         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1334         
1335         return addResult;
1336         
1339 CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
1341         
1342         CDSGetCalendarEntryInfo entryResult;
1343         
1344         // Check if the calendar entry exists.
1345         
1346         int resultCode;
1347         
1348         sqlite3_stmt *findHandle;
1349         sqlite3_stmt *statementHandle;
1350         
1351         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1352         
1353         if (resultCode != 0){
1354                 entryResult.getEventResult = CDSENTRY_FAILED;
1355                 return entryResult;
1356         }
1357         
1358         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
1359         
1360         if (resultCode != 0){
1361                 entryResult.getEventResult = CDSENTRY_FAILED;
1362                 return entryResult;
1363         }
1364         
1365         resultCode = sqlite3_step(findHandle);
1366         
1367         if (resultCode == SQLITE_ROW){
1368                 
1369         } else if (resultCode == SQLITE_DONE) {
1370                 entryResult.getEventResult = CDSENTRY_NOENTRY;
1371                 return entryResult;
1372         } else {
1373                 entryResult.getEventResult = CDSENTRY_FAILED;
1374                 return entryResult;
1375         }
1376         
1377         // Get the calendar entry data.
1378         
1379         std::string sqlParameter = "SELECT entryname, entrydescription,"
1380         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1381         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1382         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1383         " calendarid, id"
1384         " FROM calendarentries WHERE id=(?1)";
1385         
1386         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1388         if (resultCode != 0){
1389                 entryResult.getEventResult = CDSENTRY_FAILED;
1390                 return entryResult;
1391         }
1392         
1393         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
1394         
1395         if (resultCode != 0){
1396                 entryResult.getEventResult = CDSENTRY_FAILED;
1397                 return entryResult;
1398         }
1399         
1400         resultCode = sqlite3_step(statementHandle);
1401         
1402         if (resultCode == SQLITE_ROW){
1403                 
1404                 // Get the calendar entry name,
1405                 
1406                 stringstream entryStream;
1407                 
1408                 entryStream << sqlite3_column_text(statementHandle, 0);
1409                 entryResult.entryName = entryStream.str();
1410                 
1411                 entryStream.str("");
1412                 
1413                 // Get the calendar entry description.
1414                 
1415                 entryStream << sqlite3_column_text(statementHandle, 1);
1416                 entryResult.entryDescription = entryStream.str();               
1418                 entryStream.str("");
1419                 
1420                 entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
1421                 entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
1422                 entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
1423                 entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5);
1424                 entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6);
1425                 entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7);
1426                 entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8);
1427                 entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9);
1428                 entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10);
1429                 entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11);             
1430                 entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12);
1431                 entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13);
1432                 entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14);
1433                 entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15);
1434                 entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
1435                 entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
1436                 entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
1437                 entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
1438                 entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
1439                 
1440         } else if (resultCode == SQLITE_DONE) {
1441                 entryResult.getEventResult = CDSENTRY_NOCALENDAR;
1442                 return entryResult;
1443         } else {
1444                 entryResult.getEventResult = CDSENTRY_FAILED;
1445                 return entryResult;
1446         }
1447         
1448         entryResult.getEventResult = CDSENTRY_OK;
1449         
1450         return entryResult;
1451         
1454 CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
1456         
1457         CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL;
1458         
1459         // Check if the calendar entry exists.
1460         
1461         int resultCode;
1462         
1463         sqlite3_stmt *findHandle;
1464         sqlite3_stmt *statementHandle;
1465         
1466         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1467         
1468         if (resultCode != 0){
1469                 return CDSENTRY_FAILED;
1470         }
1471         
1472         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
1473         
1474         if (resultCode != 0){
1475                 return CDSENTRY_FAILED;
1476         }
1477         
1478         resultCode = sqlite3_step(findHandle);
1479         
1480         if (resultCode == SQLITE_ROW){
1481                 
1482         } else if (resultCode == SQLITE_DONE) {
1483                 return CDSENTRY_NOENTRY;
1484         } else {
1485                 return CDSENTRY_FAILED;
1486         }
1488         // Delete the account.
1489         
1490         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr);
1491         
1492         if (resultCode != 0){
1493                 return CDSENTRY_FAILED;
1494         }
1495         
1496         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
1497         
1498         if (resultCode != 0){
1499                 return CDSENTRY_FAILED;
1500         }
1501         
1502         resultCode = sqlite3_step(statementHandle);
1503         
1504         if (resultCode == SQLITE_DONE){
1505                 deleteResult = CDSENTRY_OK;
1506         } else {
1507                 return CDSENTRY_FAILED;
1508         }
1509         
1510         // Update the checksum.
1511         
1512         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1513         
1514         return deleteResult;    
1515         
1518 CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
1519         
1520         CDSEntryList entryList;
1521         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
1522         
1523         // Check if calendar exists first.
1524         
1525         int resultCode;
1526         
1527         sqlite3_stmt *findHandle;
1528         sqlite3_stmt *calendarHandle;
1529         
1530         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
1531         
1532         if (resultCode != 0){
1533                 entryList.getEventListResult = CDSENTRY_FAILED;
1534                 return entryList;
1535         }
1536         
1537         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
1538         
1539         if (resultCode != 0){
1540                 entryList.getEventListResult = CDSENTRY_FAILED;
1541                 return entryList;
1542         }
1543         
1544         resultCode = sqlite3_step(findHandle);
1545         
1546         if (resultCode == SQLITE_ROW){
1547                 
1548         } else if (resultCode == SQLITE_DONE) {
1549                 entryList.getEventListResult = CDSENTRY_NOCALENDAR;
1550                 return entryList;
1551         } else {
1552                 entryList.getEventListResult = CDSENTRY_FAILED;
1553                 return entryList;
1554         }
1555         
1556         // Get the list of entry IDs.
1557         
1558         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE calendarid=(?1);", -1, &calendarHandle, nullptr);
1559         
1560         if (resultCode != 0){
1561                 entryList.getEventListResult = CDSENTRY_FAILED;
1562                 return entryList;
1563         }
1564         
1565         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarID);
1566         
1567         if (resultCode != 0){
1568                 entryList.getEventListResult = CDSENTRY_FAILED;
1569                 return entryList;
1570         }
1571         
1572         resultCode = sqlite3_step(calendarHandle);
1573         
1574         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1575         } else {
1576                 entryList.getEventListResult = CDSENTRY_FAILED;
1577                 return entryList;
1578         }
1579         
1580         while (resultCode == SQLITE_ROW){
1581                 
1582                 int calendarID = sqlite3_column_int(calendarHandle, 0);
1583                 
1584                 entryList.entryList.push_back(calendarID);
1585                 
1586                 resultCode = sqlite3_step(calendarHandle);
1587                 
1588         }       
1589         
1590         entryList.getEventListResult = CDSENTRY_OK;
1591         
1592         return entryList;
1593         
1596 CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
1597         
1598         CDSEntryList entryList;
1599         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
1600         
1601         // Check if calendar exists first.
1602         
1603         int resultCode;
1605         sqlite3_stmt *calendarHandle;
1606         
1607         // Get the list of entry IDs.
1609         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
1610         
1611         if (resultCode != 0){
1612                 entryList.getEventListResult = CDSENTRY_FAILED;
1613                 return entryList;
1614         }
1615         
1616         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
1617         
1618         if (resultCode != 0){
1619                 entryList.getEventListResult = CDSENTRY_FAILED;
1620                 return entryList;
1621         }
1623         resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
1624         
1625         if (resultCode != 0){
1626                 entryList.getEventListResult = CDSENTRY_FAILED;
1627                 return entryList;
1628         }
1629         
1630         resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
1631         
1632         if (resultCode != 0){
1633                 entryList.getEventListResult = CDSENTRY_FAILED;
1634                 return entryList;
1635         }
1636         
1637         resultCode = sqlite3_step(calendarHandle);
1638         
1639         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1640         } else {
1641                 entryList.getEventListResult = CDSENTRY_FAILED;
1642                 return entryList;
1643         }
1644         
1645         while (resultCode == SQLITE_ROW){
1646                 
1647                 int calendarID = sqlite3_column_int(calendarHandle, 0);
1648                 
1649                 entryList.entryList.push_back(calendarID);
1650                 
1651                 resultCode = sqlite3_step(calendarHandle);
1652                 
1653         }       
1654         
1655         entryList.getEventListResult = CDSENTRY_OK;
1656         
1657         return entryList;
1658         
1661 CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
1662         
1663         CDSCalendarList calendarList;
1664         calendarList.getCalendarListResult = CDSCALENDAR_UNITTESTFAIL;
1665         
1666         // Check if calendar exists first.
1667         
1668         int resultCode;
1669         
1670         sqlite3_stmt *findHandle;
1671         sqlite3_stmt *calendarHandle;
1672         
1673         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
1674         
1675         if (resultCode != 0){
1676                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1677                 return calendarList;
1678         }
1679         
1680         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
1681         
1682         if (resultCode != 0){
1683                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1684                 return calendarList;
1685         }
1686         
1687         resultCode = sqlite3_step(findHandle);
1688         
1689         if (resultCode == SQLITE_ROW){
1690                 
1691         } else if (resultCode == SQLITE_DONE) {
1692                 calendarList.getCalendarListResult = CDSCALENDAR_NOCALENDAR;
1693                 return calendarList;
1694         } else {
1695                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1696                 return calendarList;
1697         }
1698         
1699         // Get the list of entry IDs.
1700         
1701         resultCode = sqlite3_prepare_v2(db, "SELECT id, calendarid FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
1702         
1703         if (resultCode != 0){
1704                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1705                 return calendarList;
1706         }
1707         
1708         resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
1709         
1710         if (resultCode != 0){
1711                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1712                 return calendarList;
1713         }
1714         
1715         resultCode = sqlite3_step(calendarHandle);
1716         
1717         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1718         } else {
1719                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1720                 return calendarList;
1721         }
1722         
1723         while (resultCode == SQLITE_ROW){
1724                 
1725                 int calendarID = sqlite3_column_int(calendarHandle, 0);
1726                 
1727                 stringstream calendarStream;
1728                 
1729                 calendarStream << sqlite3_column_text(calendarHandle, 1);
1730                 
1731                 calendarList.calendarList.push_back(calendarID);
1732                 calendarList.calendarListTextID.push_back(calendarStream.str());
1733                 
1734                 calendarStream.str("");
1735                 
1736                 resultCode = sqlite3_step(calendarHandle);
1737                 
1738         }       
1739         
1740         calendarList.getCalendarListResult = CDSCALENDAR_OK;
1741         
1742         return calendarList;
1743         
1746 CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
1747         
1748         int resultCode;
1749         
1750         // Check if the checksum already exists.
1751         
1752         sqlite3_stmt *findHandle;
1753         sqlite3_stmt *checksumHandle;
1754         
1755         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
1756         
1757         if (resultCode != 0){
1758                 return CDSCHECKSUM_FAILED;
1759         }
1760         
1761         resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1762         
1763         if (resultCode != 0){
1764                 return CDSCHECKSUM_FAILED;
1765         }
1766         
1767         resultCode = sqlite3_step(findHandle);
1768         
1769         if (resultCode == SQLITE_ROW){
1770                 return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
1771         } else if (resultCode == SQLITE_DONE) {
1772                 
1773         } else {
1774                 return CDSCHECKSUM_FAILED;
1775         }
1776         
1777         // Add the checksum to the checksum table.
1778         
1779         resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
1780         
1781         if (resultCode != 0){
1782                 return CDSCHECKSUM_FAILED;
1783         }
1784         
1785         resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1786         
1787         if (resultCode != 0){
1788                 return CDSCHECKSUM_FAILED;
1789         }
1790         
1791         resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
1792         
1793         if (resultCode != 0){
1794                 return CDSCHECKSUM_FAILED;
1795         }
1796         
1797         resultCode = sqlite3_step(checksumHandle);
1798         
1799         if (resultCode != SQLITE_DONE){
1800                 return CDSCHECKSUM_FAILED;
1801         }       
1802         
1803         return CDSCHECKSUM_OK;
1804         
1807 CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
1808         
1809         CDSGetChecksumResult getChecksumResult;
1810         
1811         int resultCode;
1812         
1813         // Check if the checksum already exists.
1814         
1815         sqlite3_stmt *getHandle;
1816         
1817         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
1818         
1819         if (resultCode != 0){
1820                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1821                 return getChecksumResult;
1822         }
1823         
1824         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1825         
1826         if (resultCode != 0){
1827                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1828                 return getChecksumResult;
1829         }
1830         
1831         resultCode = sqlite3_step(getHandle);
1832         
1833         if (resultCode == SQLITE_ROW){
1834         } else if (resultCode == SQLITE_DONE) {
1835                 getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
1836                 return getChecksumResult;
1837         } else {
1838                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1839                 return getChecksumResult;
1840         }
1841         
1842         stringstream checksumStream;
1843                 
1844         checksumStream << sqlite3_column_text(getHandle, 0);
1845                 
1846         getChecksumResult.checksumValue = checksumStream.str();
1847         
1848         getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
1849         
1850         return getChecksumResult;
1851         
1854 CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
1855         
1856         int resultCode;
1857         
1858         // Check if the checksum already exists.
1859         
1860         sqlite3_stmt *getHandle;
1861         sqlite3_stmt *statementHandle;
1862         
1863         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
1864         
1865         if (resultCode != 0){
1866                 return CDSCHECKSUM_FAILED;
1867         }
1868         
1869         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1870         
1871         if (resultCode != 0){
1872                 return CDSCHECKSUM_FAILED;
1873         }
1874         
1875         resultCode = sqlite3_step(getHandle);
1876         
1877         if (resultCode == SQLITE_ROW){
1878         } else if (resultCode == SQLITE_DONE) {
1879                 return CDSCHECKSUM_NOHASH;
1880         } else {
1881                 return CDSCHECKSUM_FAILED;
1882         }
1883         
1884         // Update the checksum.
1885         
1886         resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
1887         
1888         if (resultCode != 0){
1889                 return CDSCHECKSUM_FAILED;
1890         }
1891         
1892         resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
1893         
1894         if (resultCode != 0){
1895                 return CDSCHECKSUM_FAILED;
1896         }
1897         
1898         resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
1899         
1900         if (resultCode != 0){
1901                 return CDSCHECKSUM_FAILED;
1902         }
1903         
1904         resultCode = sqlite3_step(statementHandle);
1905         
1906         if (resultCode != SQLITE_DONE){
1907                 return CDSCHECKSUM_FAILED;
1908         }
1909         
1910         return CDSCHECKSUM_OK;
1911         
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