Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Win32: implement further UTF8 support
[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_ROW){
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         string calendarColourString = (string)calendarColour;
590         resultCode = sqlite3_bind_text(statementHandle, 4, calendarColourString.c_str(), -1, SQLITE_STATIC);
592         if (resultCode != 0){
593                 return CDSCALENDAR_FAILED;
594         }
596         resultCode = sqlite3_bind_text(statementHandle, 5, calendarDescription.c_str(), -1, SQLITE_STATIC);
598         if (resultCode != 0){
599                 return CDSCALENDAR_FAILED;
600         }
601         
602         resultCode = sqlite3_step(statementHandle);
603         
604         if (resultCode != SQLITE_DONE){
605                 return CDSCALENDAR_FAILED;
606         }
607         
608         // Update the checksum.
609         
610         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
611         
612         addResult = CDSCALENDAR_OK;
613         
614         return addResult;
615         
618 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std::string calendarTextID)
620         
621         CDSGetAccountInfo accountResult;
622         CDSGetCalendarInfo calendarResult;
623         sqlite3_stmt *statementHandle;
624         int resultCode;
625         
626         // Check if the account exists.
627         
628         accountResult = GetAccount(accountName);
629         
630         switch (accountResult.accountInfoResult){
631                 
632                 case CDSACCOUNT_OK:
633                         calendarResult.accountName = accountResult.accountName;
634                         calendarResult.accountInfoResult = CDSACCOUNT_OK;
635                         break;
636                 case CDSACCOUNT_FAILED:
637                         calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
638                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
639                         return calendarResult;
640                 case CDSACCOUNT_NOACCOUNT:
641                         calendarResult.accountInfoResult = CDSACCOUNT_NOACCOUNT;
642                         calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
643                         return calendarResult;
644                 
645         }
646         
647         // Check if the calendar exists.
648         
649         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
650         
651         if (resultCode != 0){
652                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
653                 return calendarResult;
654         }
656         resultCode = sqlite3_bind_int(statementHandle, 1, accountResult.accountID);
657         
658         if (resultCode != 0){
659                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
660                 return calendarResult;
661         }
662         
663         resultCode = sqlite3_bind_text(statementHandle, 2, calendarTextID.c_str(), -1, SQLITE_STATIC);
664         
665         if (resultCode != 0){
666                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
667                 return calendarResult;
668         }
669         
670         resultCode = sqlite3_step(statementHandle);
671         
672         if (resultCode == SQLITE_DONE){
673                 
674                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
675                 return calendarResult;
676                 
677         } else if (resultCode == SQLITE_ROW){
678                 
679                 // Get the calendar name.
680                 
681                 stringstream calendarStream;
682                 
683                 calendarStream << sqlite3_column_text(statementHandle, 2);
684                 calendarResult.calendarName = calendarStream.str();
685                 
686                 calendarStream.str("");
687                 calendarStream << sqlite3_column_text(statementHandle, 3);
688                 calendarResult.calendarTextID = calendarStream.str();
690                 calendarStream.str("");
691                 calendarStream << sqlite3_column_text(statementHandle, 4);
692                 calendarResult.calendarColour = calendarStream.str();
693                 
694                 calendarStream.str("");
695                 calendarStream << sqlite3_column_text(statementHandle, 5);
696                 calendarResult.calendarDescription = calendarStream.str();
697                 
698                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
699                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
700                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
701                 return calendarResult;
702                 
703         } else {
704                 
705                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
706                 return calendarResult;
707                 
708         }
709         
710         return calendarResult;
711         
714 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
716         
717         CDSGetCalendarInfo calendarResult;
718         sqlite3_stmt *statementHandle;
719         sqlite3_stmt *accountHandle;
720         int resultCode;
721         
722         // Check if the calendar exists.
723         
724         resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
725         
726         if (resultCode != 0){
727                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
728                 return calendarResult;
729         }
731         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
732         
733         if (resultCode != 0){
734                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
735                 return calendarResult;
736         }
737         
738         resultCode = sqlite3_step(statementHandle);
739         
740         if (resultCode == SQLITE_DONE){
741                 
742                 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
743                 return calendarResult;
744                 
745         } else if (resultCode == SQLITE_ROW){
746                 
747                 // Get the calendar data.
748                 
749                 stringstream calendarStream;
750                 
751                 calendarStream << sqlite3_column_text(statementHandle, 2);
752                 calendarResult.calendarName = calendarStream.str();
753                 
754                 calendarStream.str("");
755                 calendarStream << sqlite3_column_text(statementHandle, 3);
756                 calendarResult.calendarTextID = calendarStream.str();
758                 calendarStream.str("");
759                 calendarStream << sqlite3_column_text(statementHandle, 4);
760                 calendarResult.calendarColour = calendarStream.str();
762                 calendarStream.str("");
763                 calendarStream << sqlite3_column_text(statementHandle, 5);
764                 calendarResult.calendarDescription = calendarStream.str();
765                 
766                 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
767                 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
768                 
769                 // Get the account name.
770                 
771                 resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE id=(?1);", -1, &accountHandle, nullptr);
772         
773                 if (resultCode != 0){
774                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
775                         return calendarResult;
776                 }
778                 resultCode = sqlite3_bind_int(accountHandle, 1, calendarResult.accountID);
779         
780                 if (resultCode != 0){
781                         calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
782                         return calendarResult;
783                 }
784         
785                 resultCode = sqlite3_step(accountHandle);
786                 
787                 if (resultCode == SQLITE_ROW){
788                         
789                         stringstream accountStream;
790                         accountStream << sqlite3_column_text(accountHandle, 0);
791                         calendarResult.accountName = accountStream.str();
792                         calendarResult.accountInfoResult = CDSACCOUNT_OK;
793                         
794                 } else {
796                         calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
797                         
798                 }
799                 
800                 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
801                 return calendarResult;
802                 
803         } else {
804                 
805                 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
806                 return calendarResult;
807                 
808         }
809         
810         return calendarResult;
811         
815 CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName, Colour calendarColour, std::string calendarDescription)
817         
818         CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
819         int resultCode;
820         
821         sqlite3_stmt *findHandle;
822         sqlite3_stmt *statementHandle;
823         
824         // Check if calendar exists first.
825         
826         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
827         
828         if (resultCode != 0){
829                 return CDSCALENDAR_FAILED;
830         }
831         
832         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
833         
834         if (resultCode != 0){
835                 return CDSCALENDAR_FAILED;
836         }
837         
838         resultCode = sqlite3_step(findHandle);
839         
840         if (resultCode == SQLITE_ROW){
841                 
842         } else if (resultCode == SQLITE_DONE) {
843                 return CDSCALENDAR_NOCALENDAR;
844         } else {
845                 return CDSCALENDAR_FAILED;
846         }
847         
848         // Update the account.
849         
850         resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1), colour=(?2), description=(?3) WHERE id=(?4);", -1, &statementHandle, nullptr);
851         
852         if (resultCode != 0){
853                 return CDSCALENDAR_FAILED;
854         }
855         
856         resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
857         
858         if (resultCode != 0){
859                 return CDSCALENDAR_FAILED;
860         }
862         string calendarColourString = calendarColour;
863         resultCode = sqlite3_bind_text(statementHandle, 2, calendarColourString.c_str(), -1, SQLITE_STATIC);
864         
865         if (resultCode != 0){
866                 return CDSCALENDAR_FAILED;
867         }
869         resultCode = sqlite3_bind_text(statementHandle, 3, calendarDescription.c_str(), -1, SQLITE_STATIC);
870         
871         if (resultCode != 0){
872                 return CDSCALENDAR_FAILED;
873         }
874         
875         resultCode = sqlite3_bind_int(statementHandle, 4, calendarID);
876         
877         if (resultCode != 0){
878                 return CDSCALENDAR_FAILED;
879         }
880         
881         resultCode = sqlite3_step(statementHandle);
882         
883         if (resultCode != SQLITE_DONE){
884                 return CDSCALENDAR_FAILED;
885         }
886         
887         // Update the checksum.
888         
889         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
890         
891         updateResult = CDSCALENDAR_OK;
892         
893         return updateResult;
894         
897 CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
899         
900         CDSCalendarResult deleteResult = CDSCALENDAR_UNITTESTFAIL;
901         
902         // Check if account exists first.
903         
904         sqlite3_stmt *findHandle;
905         sqlite3_stmt *statementHandle;
906         sqlite3_stmt *entriesHandle;
907         int resultCode;
908         
909         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
910         
911         if (resultCode != 0){
912                 return CDSCALENDAR_FAILED;
913         }
914         
915         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
916         
917         if (resultCode != 0){
918                 return CDSCALENDAR_FAILED;
919         }
920         
921         resultCode = sqlite3_step(findHandle);
922         
923         if (resultCode == SQLITE_ROW){
924                 
925         } else if (resultCode == SQLITE_DONE) {
926                 return CDSCALENDAR_NOCALENDAR;
927         } else {
928                 return CDSCALENDAR_FAILED;
929         }
930         
931         // Delete the calendar.
932         
933         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
934         
935         if (resultCode != 0){
936                 return CDSCALENDAR_FAILED;
937         }
938         
939         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
940         
941         if (resultCode != 0){
942                 return CDSCALENDAR_FAILED;
943         }
944         
945         resultCode = sqlite3_step(statementHandle);
946         
947         if (resultCode != SQLITE_DONE){
948                 return CDSCALENDAR_FAILED;
949         }
950         
951         // Delete the calendar entries.
952         
953         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE calendarid=(?1);", -1, &entriesHandle, nullptr);
954         
955         if (resultCode != 0){
956                 return CDSCALENDAR_FAILED;
957         }
958         
959         resultCode = sqlite3_bind_int(entriesHandle, 1, calendarID);
960         
961         if (resultCode != 0){
962                 return CDSCALENDAR_FAILED;
963         }
964         
965         resultCode = sqlite3_step(entriesHandle);
966         
967         if (resultCode == SQLITE_DONE){
968                 deleteResult = CDSCALENDAR_OK;
969         } else {
970                 return CDSCALENDAR_FAILED;
971         }
972         
973         // Update the checksum.
974         
975         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
976         
977         return deleteResult;
978         
981 CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string filename)
983         
984         CDSAddEntryResult addResult;
985         addResult.addEventResult = CDSENTRY_UNITTESTFAIL;
986         
987         // Load the event file.
988         
989         CalendarEventObject eventData;  
990         CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
992         // Check the result of the event file load.
993         
994         switch (eventLoadResult){
995                 
996                 case CALENDAROBJECTLOAD_OK:
997                         break;
998                 case CALENDAROBJECTLOAD_MISSING:
999                         addResult.addEventResult = CDSENTRY_MISSINGFILE;
1000                         return addResult;
1001                 case CALENDAROBJECTLOAD_INVALIDFORMAT:
1002                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1003                         return addResult;
1004                 case CALENDAROBJECTLOAD_CANNOTOPEN:
1005                         addResult.addEventResult = CDSENTRY_CANNOTOPENFILE;
1006                         return addResult;
1007                 
1008         }
1009         
1010         // Check if calendar exists first.
1011         
1012         int resultCode;
1013         
1014         sqlite3_stmt *findHandle;
1015         sqlite3_stmt *statementHandle;
1016         
1017         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
1018         
1019         if (resultCode != 0){
1020                 addResult.addEventResult = CDSENTRY_FAILED;
1021                 return addResult;
1022         }
1023         
1024         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
1025         
1026         if (resultCode != 0){
1027                 addResult.addEventResult = CDSENTRY_FAILED;
1028                 return addResult;
1029         }
1030         
1031         resultCode = sqlite3_step(findHandle);
1032         
1033         if (resultCode == SQLITE_ROW){
1034                 
1035         } else if (resultCode == SQLITE_DONE) {
1036                 addResult.addEventResult = CDSENTRY_NOCALENDAR;
1037                 return addResult;
1038         } else {
1039                 addResult.addEventResult = CDSENTRY_FAILED;
1040                 return addResult;
1041         }
1042         
1043         // Get the required values from the event object.
1044         
1045         int eventStartYear = 0;
1046         int eventStartMonth = 0;
1047         int eventStartDay = 0;
1048         int eventStartHour = 0;
1049         int eventStartMinute = 0;
1050         int eventStartSecond = 0;
1051         int eventStartDuration = 0;
1052         std::string eventString = "";
1053         
1054         // Start Date.
1055         
1056         if (eventData.dateTimeStartData.size() < 16){
1057                 
1058                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1059                 return addResult;
1060                 
1061         }
1062         
1063         eventString = eventData.dateTimeStartData.substr(0,4);
1064         
1065         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1066                 
1067                 eventStartYear = atoi(eventString.c_str());
1068                 
1069         } else {
1070                 
1071                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1072                 return addResult;
1073                 
1074         }
1076         eventString = eventData.dateTimeStartData.substr(4,2);
1077         
1078         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1079                 
1080                 eventStartMonth = atoi(eventString.c_str());
1081                 
1082         } else {
1083                 
1084                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1085                 return addResult;
1086                 
1087         }
1088         
1089         eventString = eventData.dateTimeStartData.substr(6,2);
1090         
1091         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1092                 
1093                 eventStartDay = atoi(eventString.c_str());
1094                 
1095         } else {
1096                 
1097                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1098                 return addResult;
1099                 
1100         }
1102         eventString = eventData.dateTimeStartData.substr(9,2);
1103         
1104         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1105                 
1106                 eventStartHour = atoi(eventString.c_str());
1107                 
1108         } else {
1109                 
1110                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1111                 return addResult;
1112                 
1113         }
1114         
1115         eventString = eventData.dateTimeStartData.substr(11,2);
1116         
1117         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1118                 
1119                 eventStartMinute = atoi(eventString.c_str());
1120                 
1121         } else {
1122                 
1123                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1124                 return addResult;
1125                 
1126         }
1127         
1128         eventString = eventData.dateTimeStartData.substr(13,2);
1129         
1130         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1131                 
1132                 eventStartSecond = atoi(eventString.c_str());
1133                 
1134         } else {
1135                 
1136                 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1137                 return addResult;
1138                 
1139         }
1140         
1141         //eventYear = eventStartDate.substr(0, 4);
1142         
1143         // End Date.
1144         
1145         int eventEndYear = 0;
1146         int eventEndMonth = 0;
1147         int eventEndDay = 0;
1148         int eventEndHour = 0;
1149         int eventEndMinute = 0;
1150         int eventEndSecond = 0;
1151         int eventEndDuration = 0;
1152         
1153         if (eventData.dateTimeEndData != ""){
1154         
1155                 if (eventData.dateTimeEndData.size() < 16){
1156                 
1157                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1158                         return addResult;
1159                 
1160                 }
1161         
1162                 eventString = eventData.dateTimeEndData.substr(0,4);
1163         
1164                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1165                 
1166                         eventEndYear = atoi(eventString.c_str());
1167                 
1168                 } else {
1169                 
1170                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1171                         return addResult;
1172                 
1173                 }
1175                 eventString = eventData.dateTimeEndData.substr(4,2);
1176         
1177                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1178                 
1179                         eventEndMonth = atoi(eventString.c_str());
1180                 
1181                 } else {
1182                 
1183                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1184                         return addResult;
1185                 
1186                 }
1187         
1188                 eventString = eventData.dateTimeEndData.substr(6,2);
1189         
1190                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1191                 
1192                         eventEndDay = atoi(eventString.c_str());
1193                 
1194                 } else {
1195                 
1196                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1197                         return addResult;
1198                 
1199                 }
1201                 eventString = eventData.dateTimeEndData.substr(9,2);
1202         
1203                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1204                 
1205                         eventEndHour = atoi(eventString.c_str());
1206                 
1207                 } else {
1208                 
1209                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1210                         return addResult;
1211                 
1212                 }
1213         
1214                 eventString = eventData.dateTimeEndData.substr(11,2);
1215         
1216                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1217                 
1218                         eventEndMinute = atoi(eventString.c_str());
1219                 
1220                 } else {
1221                 
1222                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1223                         return addResult;
1224                 
1225                 }
1226         
1227                 eventString = eventData.dateTimeEndData.substr(13,2);
1228         
1229                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1230                 
1231                         eventEndSecond = atoi(eventString.c_str());
1232                 
1233                 } else {
1234                 
1235                         addResult.addEventResult = CDSENTRY_INVALIDFILE;
1236                         return addResult;
1237                 
1238                 }
1239                 
1240         }
1242         eventString = eventData.durationData;
1243         
1244         // Process the duration data.
1245         
1246         int eventDurationWeeks = 0;
1247         int eventDurationDays = 0;
1248         int eventDurationHours = 0;
1249         int eventDurationMinutes = 0;
1250         int eventDurationSeconds = 0;
1251         
1252         // Get the duration (if DTEND hasn't been specified).
1253         
1254         if (eventData.durationData.size() > 0){
1255                 
1256                 bool FoundP = false;
1257                 bool FoundW = false;
1258                 bool DateTimeMode = false;
1259                 
1260                 std::string::iterator eventDataChar = eventData.durationData.begin();
1261                 std::string currentValue = "";
1262                 
1263                 if (*eventDataChar != 'P'){
1264                         
1265                         eventDataChar = eventData.durationData.end();
1266                         
1267                 }
1268                 
1269                 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1270                         
1271                         // Check if value is a digit.
1272                         
1273                         if (isdigit(*eventDataChar)){
1274                                 
1275                                 currentValue += *eventDataChar;
1276                                 
1277                         } else {
1278                                 
1279                                 // Check that the value matches one of the letters.
1280                                 
1281                                 if (*eventDataChar == 'W' && DateTimeMode == false){
1282                                 
1283                                         eventDurationWeeks = atoi(currentValue.c_str());
1284                                         
1285                                 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1287                                         eventDurationDays = atoi(currentValue.c_str());
1288                                         
1289                                 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1290                                         
1291                                         DateTimeMode = true;
1292                                         
1293                                 } else if (*eventDataChar == 'H'){
1295                                         eventDurationHours = atoi(currentValue.c_str());
1296                                         
1297                                 } else if (*eventDataChar == 'M'){
1298                                         
1299                                         eventDurationMinutes = atoi(currentValue.c_str());
1300                                         
1301                                 } else if (*eventDataChar == 'S'){
1302                                 
1303                                         eventDurationSeconds = atoi(currentValue.c_str());
1304                                         
1305                                 }
1306                                         
1307                                 // Reset the current value.
1308                                 
1309                                 currentValue = "";
1310                                 
1311                         }
1312                         
1313                 }
1314                 
1315         }
1316         
1317         // Add the calendar entry.
1318         
1319         std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
1320         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1321         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1322         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1323         " filename)"
1324         " VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
1325         " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20), (?21))";
1326         
1327         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1329         resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
1330         
1331         if (resultCode != 0){
1332                 addResult.addEventResult = CDSENTRY_FAILED;
1333                 return addResult;
1334         }
1335         
1336         // Process Entry Name.
1337         
1338         resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1339         
1340         if (resultCode != 0){
1341                 addResult.addEventResult = CDSENTRY_FAILED;
1342                 return addResult;
1343         }
1344         
1345         // Process Entry Description.
1346         
1347         string eventDescription;
1348         
1349         try {
1350                 eventDescription = eventData.descriptionList.at(0);
1351         }
1352         
1353         catch (out_of_range &err){
1354                 eventDescription = "";
1355         }
1356         
1357         resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1358         
1359         if (resultCode != 0){
1360                 addResult.addEventResult = CDSENTRY_FAILED;
1361                 return addResult;
1362         }
1363         
1364         // Process Entry Start Date information.
1365         
1366         resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1367         
1368         if (resultCode != 0){
1369                 addResult.addEventResult = CDSENTRY_FAILED;
1370                 return addResult;
1371         }
1373         resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1374         
1375         if (resultCode != 0){
1376                 addResult.addEventResult = CDSENTRY_FAILED;
1377                 return addResult;
1378         }
1380         resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1381         
1382         if (resultCode != 0){
1383                 addResult.addEventResult = CDSENTRY_FAILED;
1384                 return addResult;
1385         }
1387         resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1388         
1389         if (resultCode != 0){
1390                 addResult.addEventResult = CDSENTRY_FAILED;
1391                 return addResult;
1392         }
1394         resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1395         
1396         if (resultCode != 0){
1397                 addResult.addEventResult = CDSENTRY_FAILED;
1398                 return addResult;
1399         }
1400         
1401         resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1402         
1403         if (resultCode != 0){
1404                 addResult.addEventResult = CDSENTRY_FAILED;
1405                 return addResult;
1406         }
1407         
1408         // Process Entry Start End information.
1409         
1410         resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1411         
1412         if (resultCode != 0){
1413                 addResult.addEventResult = CDSENTRY_FAILED;
1414                 return addResult;
1415         }
1417         resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1418         
1419         if (resultCode != 0){
1420                 addResult.addEventResult = CDSENTRY_FAILED;
1421                 return addResult;
1422         }
1424         resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1425         
1426         if (resultCode != 0){
1427                 addResult.addEventResult = CDSENTRY_FAILED;
1428                 return addResult;
1429         }
1431         resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1432         
1433         if (resultCode != 0){
1434                 addResult.addEventResult = CDSENTRY_FAILED;
1435                 return addResult;
1436         }
1438         resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1439         
1440         if (resultCode != 0){
1441                 addResult.addEventResult = CDSENTRY_FAILED;
1442                 return addResult;
1443         }
1444         
1445         resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1446         
1447         if (resultCode != 0){
1448                 addResult.addEventResult = CDSENTRY_FAILED;
1449                 return addResult;
1450         }
1451         
1452         resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1453         
1454         if (resultCode != 0){
1455                 addResult.addEventResult = CDSENTRY_FAILED;
1456                 return addResult;
1457         }
1458         
1459         resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1460         
1461         if (resultCode != 0){
1462                 addResult.addEventResult = CDSENTRY_FAILED;
1463                 return addResult;
1464         }
1465         
1466         resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1467         
1468         if (resultCode != 0){
1469                 addResult.addEventResult = CDSENTRY_FAILED;
1470                 return addResult;
1471         }
1472         
1473         resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1474         
1475         if (resultCode != 0){
1476                 addResult.addEventResult = CDSENTRY_FAILED;
1477                 return addResult;
1478         }
1479         
1480         resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1481         
1482         if (resultCode != 0){
1483                 addResult.addEventResult = CDSENTRY_FAILED;
1484                 return addResult;
1485         }
1486         
1487         resultCode = sqlite3_bind_text(statementHandle, 21, filename.c_str(), -1, SQLITE_STATIC);
1488         
1489         if (resultCode != 0){
1490                 addResult.addEventResult = CDSENTRY_FAILED;
1491                 return addResult;
1492         }
1493         
1494         resultCode = sqlite3_step(statementHandle);
1495         
1496         if (resultCode != SQLITE_DONE){
1497                 addResult.addEventResult = CDSENTRY_FAILED;
1498                 return addResult;
1499         }
1500         
1501         addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1502         addResult.addEventResult = CDSENTRY_OK;
1503         
1504         // Update the checksum.
1505         
1506         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1507         
1508         return addResult;
1509         
1512 CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string filename)
1514         
1515         CDSEditEntryResult editResult;
1516         editResult.editEventResult = CDSENTRY_UNITTESTFAIL;
1517         
1518         // Load the event file.
1519         
1520         CalendarEventObject eventData;
1521         CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
1522         
1523         // Check the result of the event file load.
1524         
1525         switch (eventLoadResult){
1526                 
1527                 case CALENDAROBJECTLOAD_OK:
1528                         break;
1529                 case CALENDAROBJECTLOAD_MISSING:
1530                         editResult.editEventResult = CDSENTRY_MISSINGFILE;
1531                         return editResult;
1532                 case CALENDAROBJECTLOAD_INVALIDFORMAT:
1533                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1534                         return editResult;
1535                 case CALENDAROBJECTLOAD_CANNOTOPEN:
1536                         editResult.editEventResult = CDSENTRY_CANNOTOPENFILE;
1537                         return editResult;
1538                 
1539         }
1540         
1541         // Check if event exists first.
1542         
1543         int resultCode;
1544         
1545         sqlite3_stmt *findHandle;
1546         sqlite3_stmt *statementHandle;
1547         
1548         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1549         
1550         if (resultCode != 0){
1551                 editResult.editEventResult = CDSENTRY_FAILED;
1552                 return editResult;
1553         }
1554         
1555         resultCode = sqlite3_bind_int(findHandle, 1, eventID);
1556         
1557         if (resultCode != 0){
1558                 editResult.editEventResult = CDSENTRY_FAILED;
1559                 return editResult;
1560         }
1561         
1562         resultCode = sqlite3_step(findHandle);
1563         
1564         if (resultCode == SQLITE_ROW){
1565                 
1566         } else if (resultCode == SQLITE_DONE) {
1567                 editResult.editEventResult = CDSENTRY_NOENTRY;
1568                 return editResult;
1569         } else {
1570                 editResult.editEventResult = CDSENTRY_FAILED;
1571                 return editResult;
1572         }
1573         
1574         // Get the required values from the event object.
1575         
1576         int eventStartYear = 0;
1577         int eventStartMonth = 0;
1578         int eventStartDay = 0;
1579         int eventStartHour = 0;
1580         int eventStartMinute = 0;
1581         int eventStartSecond = 0;
1582         int eventStartDuration = 0;
1583         std::string eventString = "";
1584         
1585         // Start Date.
1586         
1587         if (eventData.dateTimeStartData.size() < 16){
1588                 
1589                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1590                 return editResult;
1591                 
1592         }
1593         
1594         eventString = eventData.dateTimeStartData.substr(0,4);
1595         
1596         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1597                 
1598                 eventStartYear = atoi(eventString.c_str());
1599                 
1600         } else {
1601                 
1602                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1603                 return editResult;
1604                 
1605         }
1607         eventString = eventData.dateTimeStartData.substr(4,2);
1608         
1609         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1610                 
1611                 eventStartMonth = atoi(eventString.c_str());
1612                 
1613         } else {
1614                 
1615                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1616                 return editResult;
1617                 
1618         }
1619         
1620         eventString = eventData.dateTimeStartData.substr(6,2);
1621         
1622         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1623                 
1624                 eventStartDay = atoi(eventString.c_str());
1625                 
1626         } else {
1627                 
1628                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1629                 return editResult;
1630                 
1631         }
1633         eventString = eventData.dateTimeStartData.substr(9,2);
1634         
1635         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1636                 
1637                 eventStartHour = atoi(eventString.c_str());
1638                 
1639         } else {
1640                 
1641                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1642                 return editResult;
1643                 
1644         }
1645         
1646         eventString = eventData.dateTimeStartData.substr(11,2);
1647         
1648         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1649                 
1650                 eventStartMinute = atoi(eventString.c_str());
1651                 
1652         } else {
1653                 
1654                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1655                 return editResult;
1656                 
1657         }
1658         
1659         eventString = eventData.dateTimeStartData.substr(13,2);
1660         
1661         if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1662                 
1663                 eventStartSecond = atoi(eventString.c_str());
1664                 
1665         } else {
1666                 
1667                 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1668                 return editResult;
1669                 
1670         }
1671         
1672         //eventYear = eventStartDate.substr(0, 4);
1673         
1674         // End Date.
1675         
1676         int eventEndYear = 0;
1677         int eventEndMonth = 0;
1678         int eventEndDay = 0;
1679         int eventEndHour = 0;
1680         int eventEndMinute = 0;
1681         int eventEndSecond = 0;
1682         int eventEndDuration = 0;
1683         
1684         if (eventData.dateTimeEndData != ""){
1685         
1686                 if (eventData.dateTimeEndData.size() < 16){
1687                 
1688                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1689                         return editResult;
1690                 
1691                 }
1692         
1693                 eventString = eventData.dateTimeEndData.substr(0,4);
1694         
1695                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1696                 
1697                         eventEndYear = atoi(eventString.c_str());
1698                 
1699                 } else {
1700                 
1701                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1702                         return editResult;
1703                 
1704                 }
1706                 eventString = eventData.dateTimeEndData.substr(4,2);
1707         
1708                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1709                 
1710                         eventEndMonth = atoi(eventString.c_str());
1711                 
1712                 } else {
1713                 
1714                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1715                         return editResult;
1716                 
1717                 }
1718         
1719                 eventString = eventData.dateTimeEndData.substr(6,2);
1720         
1721                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1722                 
1723                         eventEndDay = atoi(eventString.c_str());
1724                 
1725                 } else {
1726                 
1727                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1728                         return editResult;
1729                 
1730                 }
1732                 eventString = eventData.dateTimeEndData.substr(9,2);
1733         
1734                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1735                 
1736                         eventEndHour = atoi(eventString.c_str());
1737                 
1738                 } else {
1739                 
1740                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1741                         return editResult;
1742                 
1743                 }
1744         
1745                 eventString = eventData.dateTimeEndData.substr(11,2);
1746         
1747                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1748                 
1749                         eventEndMinute = atoi(eventString.c_str());
1750                 
1751                 } else {
1752                 
1753                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1754                         return editResult;
1755                 
1756                 }
1757         
1758                 eventString = eventData.dateTimeEndData.substr(13,2);
1759         
1760                 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1761                 
1762                         eventEndSecond = atoi(eventString.c_str());
1763                 
1764                 } else {
1765                 
1766                         editResult.editEventResult = CDSENTRY_INVALIDFILE;
1767                         return editResult;
1768                 
1769                 }
1770                 
1771         }
1773         eventString = eventData.durationData;
1774         
1775         // Process the duration data.
1776         
1777         int eventDurationWeeks = 0;
1778         int eventDurationDays = 0;
1779         int eventDurationHours = 0;
1780         int eventDurationMinutes = 0;
1781         int eventDurationSeconds = 0;
1782         
1783         // Get the duration (if DTEND hasn't been specified).
1784         
1785         if (eventData.durationData.size() > 0){
1786                 
1787                 bool FoundP = false;
1788                 bool FoundW = false;
1789                 bool DateTimeMode = false;
1790                 
1791                 std::string::iterator eventDataChar = eventData.durationData.begin();
1792                 std::string currentValue = "";
1793                 
1794                 if (*eventDataChar != 'P'){
1795                         
1796                         eventDataChar = eventData.durationData.end();
1797                         
1798                 }
1799                 
1800                 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1801                         
1802                         // Check if value is a digit.
1803                         
1804                         if (isdigit(*eventDataChar)){
1805                                 
1806                                 currentValue += *eventDataChar;
1807                                 
1808                         } else {
1809                                 
1810                                 // Check that the value matches one of the letters.
1811                                 
1812                                 if (*eventDataChar == 'W' && DateTimeMode == false){
1813                                 
1814                                         eventDurationWeeks = atoi(currentValue.c_str());
1815                                         
1816                                 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1818                                         eventDurationDays = atoi(currentValue.c_str());
1819                                         
1820                                 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1821                                         
1822                                         DateTimeMode = true;
1823                                         
1824                                 } else if (*eventDataChar == 'H'){
1826                                         eventDurationHours = atoi(currentValue.c_str());
1827                                         
1828                                 } else if (*eventDataChar == 'M'){
1829                                         
1830                                         eventDurationMinutes = atoi(currentValue.c_str());
1831                                         
1832                                 } else if (*eventDataChar == 'S'){
1833                                 
1834                                         eventDurationSeconds = atoi(currentValue.c_str());
1835                                         
1836                                 }
1837                                         
1838                                 // Reset the current value.
1839                                 
1840                                 currentValue = "";
1841                                 
1842                         }
1843                         
1844                 }
1845                 
1846         }
1847         
1848         // Add the calendar entry.
1849         
1850         std::string sqlParameter = "UPDATE calendarentries SET entryname=(?2), entrydescription=(?3),"
1851         " entrystartyear=(?4), entrystartmonth=(?5), entrystartday=(?6), entrystarthour=(?7), entrystartminute=(?8), entrystartsecond=(?9),"
1852         " entryendyear=(?10), entryendmonth=(?11), entryendday=(?12), entryendhour=(?13), entryendminute=(?14), entryendsecond=(?15), "
1853         " entrydurationweek=(?16), entrydurationday=(?17), entrydurationhour=(?18), entrydurationminute=(?19), entrydurationsecond=(?20) "
1854         " WHERE id=(?1)";
1855         
1856         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1857         
1858         resultCode = sqlite3_bind_int(statementHandle, 1, eventID);
1859         
1860         if (resultCode != 0){
1861                 editResult.editEventResult = CDSENTRY_FAILED;
1862                 return editResult;
1863         }
1864         
1865         // Process Entry Name.
1866         
1867         resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1868         
1869         if (resultCode != 0){
1870                 editResult.editEventResult = CDSENTRY_FAILED;
1871                 return editResult;
1872         }
1873         
1874         // Process Entry Description.
1875         
1876         string eventDescription;
1877         
1878         try {
1879                 eventDescription = eventData.descriptionList.at(0);
1880         }
1881         
1882         catch (out_of_range &err){
1883                 eventDescription = "";
1884         }
1885         
1886         resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1887         
1888         if (resultCode != 0){
1889                 editResult.editEventResult = CDSENTRY_FAILED;
1890                 return editResult;
1891         }
1892         
1893         // Process Entry Start Date information.
1894         
1895         resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1896         
1897         if (resultCode != 0){
1898                 editResult.editEventResult = CDSENTRY_FAILED;
1899                 return editResult;
1900         }
1902         resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1903         
1904         if (resultCode != 0){
1905                 editResult.editEventResult = CDSENTRY_FAILED;
1906                 return editResult;
1907         }
1909         resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1910         
1911         if (resultCode != 0){
1912                 editResult.editEventResult = CDSENTRY_FAILED;
1913                 return editResult;
1914         }
1916         resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1917         
1918         if (resultCode != 0){
1919                 editResult.editEventResult = CDSENTRY_FAILED;
1920                 return editResult;
1921         }
1923         resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1924         
1925         if (resultCode != 0){
1926                 editResult.editEventResult = CDSENTRY_FAILED;
1927                 return editResult;
1928         }
1929         
1930         resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1931         
1932         if (resultCode != 0){
1933                 editResult.editEventResult = CDSENTRY_FAILED;
1934                 return editResult;
1935         }
1936         
1937         // Process Entry Start End information.
1938         
1939         resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1940         
1941         if (resultCode != 0){
1942                 editResult.editEventResult = CDSENTRY_FAILED;
1943                 return editResult;
1944         }
1946         resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1947         
1948         if (resultCode != 0){
1949                 editResult.editEventResult = CDSENTRY_FAILED;
1950                 return editResult;
1951         }
1953         resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1954         
1955         if (resultCode != 0){
1956                 editResult.editEventResult = CDSENTRY_FAILED;
1957                 return editResult;
1958         }
1960         resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1961         
1962         if (resultCode != 0){
1963                 editResult.editEventResult = CDSENTRY_FAILED;
1964                 return editResult;
1965         }
1967         resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1968         
1969         if (resultCode != 0){
1970                 editResult.editEventResult = CDSENTRY_FAILED;
1971                 return editResult;
1972         }
1973         
1974         resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1975         
1976         if (resultCode != 0){
1977                 editResult.editEventResult = CDSENTRY_FAILED;
1978                 return editResult;
1979         }
1980         
1981         resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1982         
1983         if (resultCode != 0){
1984                 editResult.editEventResult = CDSENTRY_FAILED;
1985                 return editResult;
1986         }
1987         
1988         resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1989         
1990         if (resultCode != 0){
1991                 editResult.editEventResult = CDSENTRY_FAILED;
1992                 return editResult;
1993         }
1994         
1995         resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1996         
1997         if (resultCode != 0){
1998                 editResult.editEventResult = CDSENTRY_FAILED;
1999                 return editResult;
2000         }
2001         
2002         resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
2003         
2004         if (resultCode != 0){
2005                 editResult.editEventResult = CDSENTRY_FAILED;
2006                 return editResult;
2007         }
2008         
2009         resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
2010         
2011         if (resultCode != 0){
2012                 editResult.editEventResult = CDSENTRY_FAILED;
2013                 return editResult;
2014         }
2015         
2016         resultCode = sqlite3_step(statementHandle);
2017         
2018         if (resultCode != SQLITE_DONE){
2019                 editResult.editEventResult = CDSENTRY_FAILED;
2020                 return editResult;
2021         }
2022         
2023         editResult.calendarEntryID = sqlite3_last_insert_rowid(db);
2024         editResult.editEventResult = CDSENTRY_OK;
2025         
2026         // Update the checksum.
2027         
2028         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2029         
2030         return editResult;
2031         
2034 CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
2036         
2037         CDSGetCalendarEntryInfo entryResult;
2038         
2039         // Check if the calendar entry exists.
2040         
2041         int resultCode;
2042         
2043         sqlite3_stmt *findHandle;
2044         sqlite3_stmt *statementHandle;
2045         
2046         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
2047         
2048         if (resultCode != 0){
2049                 entryResult.getEventResult = CDSENTRY_FAILED;
2050                 return entryResult;
2051         }
2052         
2053         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
2054         
2055         if (resultCode != 0){
2056                 entryResult.getEventResult = CDSENTRY_FAILED;
2057                 return entryResult;
2058         }
2059         
2060         resultCode = sqlite3_step(findHandle);
2061         
2062         if (resultCode == SQLITE_ROW){
2063                 
2064         } else if (resultCode == SQLITE_DONE) {
2065                 entryResult.getEventResult = CDSENTRY_NOENTRY;
2066                 return entryResult;
2067         } else {
2068                 entryResult.getEventResult = CDSENTRY_FAILED;
2069                 return entryResult;
2070         }
2071         
2072         // Get the calendar entry data.
2073         
2074         std::string sqlParameter = "SELECT entryname, entrydescription,"
2075         " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
2076         " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
2077         " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
2078         " calendarid, id, filename"
2079         " FROM calendarentries WHERE id=(?1)";
2080         
2081         resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
2083         if (resultCode != 0){
2084                 entryResult.getEventResult = CDSENTRY_FAILED;
2085                 return entryResult;
2086         }
2087         
2088         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2089         
2090         if (resultCode != 0){
2091                 entryResult.getEventResult = CDSENTRY_FAILED;
2092                 return entryResult;
2093         }
2094         
2095         resultCode = sqlite3_step(statementHandle);
2096         
2097         if (resultCode == SQLITE_ROW){
2098                 
2099                 // Get the calendar entry name,
2100                 
2101                 stringstream entryStream;
2102                 
2103                 entryStream << sqlite3_column_text(statementHandle, 0);
2104                 entryResult.entryName = entryStream.str();
2105                 
2106                 entryStream.str("");
2107                 
2108                 // Get the calendar entry description.
2109                 
2110                 entryStream << sqlite3_column_text(statementHandle, 1);
2111                 entryResult.entryDescription = entryStream.str();               
2113                 entryStream.str("");
2114                 
2115                 // Get the calendar entry filename.
2116                 
2117                 entryStream << sqlite3_column_text(statementHandle, 21);
2118                 entryResult.entryFilename = entryStream.str();
2120                 entryStream.str("");
2121                 
2122                 entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
2123                 entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
2124                 entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
2125                 entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5);
2126                 entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6);
2127                 entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7);
2128                 entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8);
2129                 entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9);
2130                 entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10);
2131                 entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11);             
2132                 entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12);
2133                 entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13);
2134                 entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14);
2135                 entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15);
2136                 entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
2137                 entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
2138                 entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
2139                 entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
2140                 entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
2141                 
2142         } else if (resultCode == SQLITE_DONE) {
2143                 entryResult.getEventResult = CDSENTRY_NOCALENDAR;
2144                 return entryResult;
2145         } else {
2146                 entryResult.getEventResult = CDSENTRY_FAILED;
2147                 return entryResult;
2148         }
2149         
2150         entryResult.getEventResult = CDSENTRY_OK;
2151         
2152         return entryResult;
2153         
2156 CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
2158         
2159         CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL;
2160         
2161         // Check if the calendar entry exists.
2162         
2163         int resultCode;
2164         
2165         sqlite3_stmt *findHandle;
2166         sqlite3_stmt *statementHandle;
2167         
2168         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
2169         
2170         if (resultCode != 0){
2171                 return CDSENTRY_FAILED;
2172         }
2173         
2174         resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
2175         
2176         if (resultCode != 0){
2177                 return CDSENTRY_FAILED;
2178         }
2179         
2180         resultCode = sqlite3_step(findHandle);
2181         
2182         if (resultCode == SQLITE_ROW){
2183                 
2184         } else if (resultCode == SQLITE_DONE) {
2185                 return CDSENTRY_NOENTRY;
2186         } else {
2187                 return CDSENTRY_FAILED;
2188         }
2190         // Delete the account.
2191         
2192         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr);
2193         
2194         if (resultCode != 0){
2195                 return CDSENTRY_FAILED;
2196         }
2197         
2198         resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2199         
2200         if (resultCode != 0){
2201                 return CDSENTRY_FAILED;
2202         }
2203         
2204         resultCode = sqlite3_step(statementHandle);
2205         
2206         if (resultCode == SQLITE_DONE){
2207                 deleteResult = CDSENTRY_OK;
2208         } else {
2209                 return CDSENTRY_FAILED;
2210         }
2211         
2212         // Update the checksum.
2213         
2214         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2215         
2216         return deleteResult;    
2217         
2220 CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
2221         
2222         CDSEntryList entryList;
2223         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2224         
2225         // Check if calendar exists first.
2226         
2227         int resultCode;
2228         
2229         sqlite3_stmt *findHandle;
2230         sqlite3_stmt *calendarHandle;
2231         
2232         resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
2233         
2234         if (resultCode != 0){
2235                 entryList.getEventListResult = CDSENTRY_FAILED;
2236                 return entryList;
2237         }
2238         
2239         resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
2240         
2241         if (resultCode != 0){
2242                 entryList.getEventListResult = CDSENTRY_FAILED;
2243                 return entryList;
2244         }
2245         
2246         resultCode = sqlite3_step(findHandle);
2247         
2248         if (resultCode == SQLITE_ROW){
2249                 
2250         } else if (resultCode == SQLITE_DONE) {
2251                 entryList.getEventListResult = CDSENTRY_NOCALENDAR;
2252                 return entryList;
2253         } else {
2254                 entryList.getEventListResult = CDSENTRY_FAILED;
2255                 return entryList;
2256         }
2257         
2258         // Get the list of entry IDs.
2259         
2260         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE calendarid=(?1);", -1, &calendarHandle, nullptr);
2261         
2262         if (resultCode != 0){
2263                 entryList.getEventListResult = CDSENTRY_FAILED;
2264                 return entryList;
2265         }
2266         
2267         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarID);
2268         
2269         if (resultCode != 0){
2270                 entryList.getEventListResult = CDSENTRY_FAILED;
2271                 return entryList;
2272         }
2273         
2274         resultCode = sqlite3_step(calendarHandle);
2275         
2276         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2277         } else {
2278                 entryList.getEventListResult = CDSENTRY_FAILED;
2279                 return entryList;
2280         }
2281         
2282         while (resultCode == SQLITE_ROW){
2283                 
2284                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2285                 
2286                 entryList.entryList.push_back(calendarID);
2287                 
2288                 resultCode = sqlite3_step(calendarHandle);
2289                 
2290         }       
2291         
2292         entryList.getEventListResult = CDSENTRY_OK;
2293         
2294         return entryList;
2295         
2298 CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
2299         
2300         CDSEntryList entryList;
2301         entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2302         
2303         // Check if calendar exists first.
2304         
2305         int resultCode;
2307         sqlite3_stmt *calendarHandle;
2308         
2309         // Get the list of entry IDs.
2311         resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
2312         
2313         if (resultCode != 0){
2314                 entryList.getEventListResult = CDSENTRY_FAILED;
2315                 return entryList;
2316         }
2317         
2318         resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
2319         
2320         if (resultCode != 0){
2321                 entryList.getEventListResult = CDSENTRY_FAILED;
2322                 return entryList;
2323         }
2325         resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
2326         
2327         if (resultCode != 0){
2328                 entryList.getEventListResult = CDSENTRY_FAILED;
2329                 return entryList;
2330         }
2331         
2332         resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
2333         
2334         if (resultCode != 0){
2335                 entryList.getEventListResult = CDSENTRY_FAILED;
2336                 return entryList;
2337         }
2338         
2339         resultCode = sqlite3_step(calendarHandle);
2340         
2341         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2342         } else {
2343                 entryList.getEventListResult = CDSENTRY_FAILED;
2344                 return entryList;
2345         }
2346         
2347         while (resultCode == SQLITE_ROW){
2348                 
2349                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2350                 
2351                 entryList.entryList.push_back(calendarID);
2352                 
2353                 resultCode = sqlite3_step(calendarHandle);
2354                 
2355         }       
2356         
2357         entryList.getEventListResult = CDSENTRY_OK;
2358         
2359         return entryList;
2360         
2363 CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
2364         
2365         CDSCalendarList calendarList;
2366         calendarList.getCalendarListResult = CDSCALENDAR_UNITTESTFAIL;
2367         
2368         // Check if calendar exists first.
2369         
2370         int resultCode;
2371         
2372         sqlite3_stmt *findHandle;
2373         sqlite3_stmt *calendarHandle;
2374         
2375         resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
2376         
2377         if (resultCode != 0){
2378                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2379                 return calendarList;
2380         }
2381         
2382         resultCode = sqlite3_bind_int(findHandle, 1, accountID);
2383         
2384         if (resultCode != 0){
2385                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2386                 return calendarList;
2387         }
2388         
2389         resultCode = sqlite3_step(findHandle);
2390         
2391         if (resultCode == SQLITE_ROW){
2392                 
2393         } else if (resultCode == SQLITE_DONE) {
2394                 calendarList.getCalendarListResult = CDSCALENDAR_NOCALENDAR;
2395                 return calendarList;
2396         } else {
2397                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2398                 return calendarList;
2399         }
2400         
2401         // Get the list of entry IDs.
2402         
2403         resultCode = sqlite3_prepare_v2(db, "SELECT id, calendarid FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
2404         
2405         if (resultCode != 0){
2406                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2407                 return calendarList;
2408         }
2409         
2410         resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
2411         
2412         if (resultCode != 0){
2413                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2414                 return calendarList;
2415         }
2416         
2417         resultCode = sqlite3_step(calendarHandle);
2418         
2419         if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2420         } else {
2421                 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2422                 return calendarList;
2423         }
2424         
2425         while (resultCode == SQLITE_ROW){
2426                 
2427                 int calendarID = sqlite3_column_int(calendarHandle, 0);
2428                 
2429                 stringstream calendarStream;
2430                 
2431                 calendarStream << sqlite3_column_text(calendarHandle, 1);
2432                 
2433                 calendarList.calendarList.push_back(calendarID);
2434                 calendarList.calendarListTextID.push_back(calendarStream.str());
2435                 
2436                 calendarStream.str("");
2437                 
2438                 resultCode = sqlite3_step(calendarHandle);
2439                 
2440         }       
2441         
2442         calendarList.getCalendarListResult = CDSCALENDAR_OK;
2443         
2444         return calendarList;
2445         
2448 CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
2449         
2450         int resultCode;
2451         
2452         // Check if the checksum already exists.
2453         
2454         sqlite3_stmt *findHandle;
2455         sqlite3_stmt *checksumHandle;
2456         
2457         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
2458         
2459         if (resultCode != 0){
2460                 return CDSCHECKSUM_FAILED;
2461         }
2462         
2463         resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2464         
2465         if (resultCode != 0){
2466                 return CDSCHECKSUM_FAILED;
2467         }
2468         
2469         resultCode = sqlite3_step(findHandle);
2470         
2471         if (resultCode == SQLITE_ROW){
2472                 return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
2473         } else if (resultCode == SQLITE_DONE) {
2474                 
2475         } else {
2476                 return CDSCHECKSUM_FAILED;
2477         }
2478         
2479         // Add the checksum to the checksum table.
2480         
2481         resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
2482         
2483         if (resultCode != 0){
2484                 return CDSCHECKSUM_FAILED;
2485         }
2486         
2487         resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2488         
2489         if (resultCode != 0){
2490                 return CDSCHECKSUM_FAILED;
2491         }
2492         
2493         resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
2494         
2495         if (resultCode != 0){
2496                 return CDSCHECKSUM_FAILED;
2497         }
2498         
2499         resultCode = sqlite3_step(checksumHandle);
2500         
2501         if (resultCode != SQLITE_DONE){
2502                 return CDSCHECKSUM_FAILED;
2503         }       
2504         
2505         return CDSCHECKSUM_OK;
2506         
2509 CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
2510         
2511         CDSGetChecksumResult getChecksumResult;
2512         
2513         int resultCode;
2514         
2515         // Check if the checksum already exists.
2516         
2517         sqlite3_stmt *getHandle;
2518         
2519         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2520         
2521         if (resultCode != 0){
2522                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2523                 return getChecksumResult;
2524         }
2525         
2526         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2527         
2528         if (resultCode != 0){
2529                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2530                 return getChecksumResult;
2531         }
2532         
2533         resultCode = sqlite3_step(getHandle);
2534         
2535         if (resultCode == SQLITE_ROW){
2536         } else if (resultCode == SQLITE_DONE) {
2537                 getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
2538                 return getChecksumResult;
2539         } else {
2540                 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2541                 return getChecksumResult;
2542         }
2543         
2544         stringstream checksumStream;
2545                 
2546         checksumStream << sqlite3_column_text(getHandle, 0);
2547                 
2548         getChecksumResult.checksumValue = checksumStream.str();
2549         
2550         getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
2551         
2552         return getChecksumResult;
2553         
2556 CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
2557         
2558         int resultCode;
2559         
2560         // Check if the checksum already exists.
2561         
2562         sqlite3_stmt *getHandle;
2563         sqlite3_stmt *statementHandle;
2564         
2565         resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2566         
2567         if (resultCode != 0){
2568                 return CDSCHECKSUM_FAILED;
2569         }
2570         
2571         resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2572         
2573         if (resultCode != 0){
2574                 return CDSCHECKSUM_FAILED;
2575         }
2576         
2577         resultCode = sqlite3_step(getHandle);
2578         
2579         if (resultCode == SQLITE_ROW){
2580         } else if (resultCode == SQLITE_DONE) {
2581                 return CDSCHECKSUM_NOHASH;
2582         } else {
2583                 return CDSCHECKSUM_FAILED;
2584         }
2585         
2586         // Update the checksum.
2587         
2588         resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
2589         
2590         if (resultCode != 0){
2591                 return CDSCHECKSUM_FAILED;
2592         }
2593         
2594         resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
2595         
2596         if (resultCode != 0){
2597                 return CDSCHECKSUM_FAILED;
2598         }
2599         
2600         resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
2601         
2602         if (resultCode != 0){
2603                 return CDSCHECKSUM_FAILED;
2604         }
2605         
2606         resultCode = sqlite3_step(statementHandle);
2607         
2608         if (resultCode != SQLITE_DONE){
2609                 return CDSCHECKSUM_FAILED;
2610         }
2611         
2612         return CDSCHECKSUM_OK;
2613         
2616 CDSCleanupResult CalendarDataStorage::Clear(){
2617         
2618         // Remove all data from the tables and reset the sequence numbers.
2620         int resultCode; 
2621         sqlite3_stmt *statementHandle;
2622         
2623         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries", -    1, &statementHandle, nullptr);
2624         
2625         if (resultCode != 0){
2626                 return CDSCLEANUP_FAILED;
2627         }
2628         
2629         resultCode = sqlite3_step(statementHandle);
2630         
2631         if (resultCode != SQLITE_DONE){
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                 return CDSCLEANUP_FAILED;
2639         }
2641         resultCode = sqlite3_step(statementHandle);
2642         
2643         if (resultCode != SQLITE_DONE){
2644                 return CDSCLEANUP_FAILED;
2645         }
2647         resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars", -1, &statementHandle, nullptr);
2648         
2649         if (resultCode != 0){
2650                 return CDSCLEANUP_FAILED;
2651         }
2652         
2653         resultCode = sqlite3_step(statementHandle);
2654         
2655         if (resultCode != SQLITE_DONE){
2656                 return CDSCLEANUP_FAILED;
2657         }
2658         
2659         resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendars';", -1, &statementHandle, nullptr);
2660         
2661         if (resultCode != 0){
2662                 return CDSCLEANUP_FAILED;
2663         }
2665         resultCode = sqlite3_step(statementHandle);
2666         
2667         if (resultCode != SQLITE_DONE){
2668                 return CDSCLEANUP_FAILED;
2669         }
2670         
2671         resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts", -1, &statementHandle, nullptr);
2672         
2673         if (resultCode != 0){
2674                 return CDSCLEANUP_FAILED;
2675         }
2676         
2677         resultCode = sqlite3_step(statementHandle);
2678         
2679         if (resultCode != SQLITE_DONE){
2680                 return CDSCLEANUP_FAILED;
2681         }
2682         
2683         resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='accounts'", -1, &statementHandle, nullptr);
2684         
2685         if (resultCode != 0){
2686                 return CDSCLEANUP_FAILED;
2687         }
2689         resultCode = sqlite3_step(statementHandle);
2690         
2691         if (resultCode != SQLITE_DONE){
2692                 return CDSCLEANUP_FAILED;
2693         }
2694         
2695         // Update the checksum.
2697         UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2698         
2699         return CDSCLEANUP_OK;
2700         
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