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