-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.h
159 lines (123 loc) · 3.73 KB
/
db.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef CALENDARI__DB_H
#define CALENDARI__DB_H 1
#include "event.h"
#include "recur.h"
#include <map>
#include <sqlite3.h>
#include <string>
#include <sstream>
struct icalcomponent_impl;
typedef struct icalcomponent_impl icalcomponent;
namespace calendari {
// Forward reference
namespace sql { class Statement; }
struct Version
{
/** CALENDAR, indexed by CALNUM. */
std::map<int,Calendar*> _calendar;
std::map<std::string,Event*> _event;
std::map<Occurrence::key_type,Occurrence*> _occurrence;
/** Clear away all events and occurrences for the given calender. */
void purge(int calnum);
/** Clear away all calendars, events and occurrences. */
void destroy(void);
};
class Db
{
public:
explicit Db(const char* dbname);
~Db(void);
/** Creates tables and indices in the database. */
void create_db(void);
void refresh_cal(int calnum, int from_version, int to_version=1);
/** Initial load of all calendar information. */
void load_calendars(int version=1);
/** Initial load of one calendar's information. */
Calendar* load_calendar(int calnum, int version=1);
/** Find all occurrences between the specified (begin,end] times. */
std::multimap<time_t,Occurrence*> find(time_t begin,time_t end,int version=1);
/** Look up the calnum of the given calid, or generate a new unique number. */
int calnum(const char* calid);
Calendar* calendar(int calnum, int version=1)
{
const std::map<int,Calendar*>& m( calendars(version) );
std::map<int,Calendar*>::const_iterator it = m.find(calnum);
return( it==m.end()? NULL: it->second );
}
const std::map<int,Calendar*>& calendars(int version=1)
{ return _ver[version]._calendar; }
Calendar* create_calendar(
const char* calid,
const char* calname,
const char* path,
bool readonly,
const char* colour,
bool show,
int version=1
);
void erase_calendar(Calendar* cal);
Occurrence* create_event(
const char* uid,
time_t dtstart,
time_t dtend,
const char* summary,
bool all_day,
int calnum,
int version=1
);
/** Read a VEVENT from the database, or create a new, empty one. */
icalcomponent* vevent(const char* uid, int version=1);
void moved(Occurrence* occ, int version=1);
void erase(Occurrence* occ, int version=1);
template<class T>
T setting(const char* key, const T& dflt) const;
template<class T>
void set_setting(const char* key, const T& val) const;
operator sqlite3* (void) const
{ return _sdb; }
private:
sqlite3* _sdb;
std::map<int,Version> _ver;
/** Helper, loads calendars from 'select_stmt'. */
void _load_calendars(sql::Statement& select_stmt, int version);
Occurrence* make_occurrence(
int calnum,
const char* uid,
const char* summary,
int sequence,
bool all_day,
RecurType evt_recurs,
time_t dtstart,
time_t dtend,
RecurType occ_recurs,
int version
);
/** Returns TRUE and sets value if key exists, else returns FALSE. */
bool _setting(const char* key, std::string& val) const;
void _set_setting(const char* key, const char* val) const;
};
template<class T>
T Db::setting(const char* key, const T& dflt) const
{
std::string valstr;
if(_setting(key,valstr))
{
T result;
std::istringstream is(valstr);
is>>result;
return result;
}
else
{
return dflt;
}
}
template<class T>
void Db::set_setting(const char* key, const T& val) const
{
std::ostringstream os;
os<<val;
_set_setting(key,os.str().c_str());
}
} // end namespace
#endif // CALENDARI__DB_H