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