Libical API Documentation 4.0 UNRELEASED Go to the stable 3.0 documentation
Loading...
Searching...
No Matches
icalcalendar.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: icalcalendar.c
3 CREATOR: eric 23 December 1999
4
5 SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>
6 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7======================================================================*/
8
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "icalcalendar.h"
23#include "icaldirset.h"
24#include "icalerror_p.h"
25#include "icalfileset.h"
26
27#include <errno.h>
28#include <stdlib.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31
32#define BOOKED_DIR "booked"
33#define INCOMING_FILE "incoming.ics"
34#define PROP_FILE "properties.ics"
35#define FBLIST_FILE "freebusy.ics"
36
37struct icalcalendar_impl {
38 char *dir;
39 icalset *freebusy;
40 icalset *properties;
41 icalset *booked;
42 icalset *incoming;
43};
44
45struct icalcalendar_impl *icalcalendar_new_impl(void)
46{
47 struct icalcalendar_impl *impl;
48
49 if ((impl = (struct icalcalendar_impl *)malloc(sizeof(struct icalcalendar_impl))) == 0) {
51 return 0;
52 }
53
54 return impl;
55}
56
57static icalerrorenum icalcalendar_create(const struct icalcalendar_impl *impl)
58{
59 char path[MAXPATHLEN];
60 struct stat sbuf;
61 int r;
62
63 icalerror_check_arg_re((impl != 0), "impl", ICAL_BADARG_ERROR);
64
65 path[0] = '\0';
66 strncpy(path, impl->dir, MAXPATHLEN - 1);
67 strncat(path, "/", MAXPATHLEN - strlen(path) - 1);
68 strncat(path, BOOKED_DIR, MAXPATHLEN - strlen(path) - 1);
69 path[MAXPATHLEN - 1] = '\0';
70
71 /* coverity[fs_check_call] */
72 r = stat(path, &sbuf);
73
74 if (r != 0 && errno == ENOENT) {
75 if (mkdir(path, 0777) != 0) {
77 return ICAL_FILE_ERROR;
78 }
79 }
80
81 return ICAL_NO_ERROR;
82}
83
84icalcalendar *icalcalendar_new(const char *dir)
85{
86 struct icalcalendar_impl *impl;
87
88 icalerror_check_arg_rz((dir != 0), "dir");
89
90 impl = icalcalendar_new_impl();
91
92 if (impl == 0) {
93 return 0;
94 }
95
96 impl->dir = (char *)strdup(dir);
97 impl->freebusy = 0;
98 impl->properties = 0;
99 impl->booked = 0;
100 impl->incoming = 0;
101
102 if (icalcalendar_create(impl) != ICAL_NO_ERROR) {
103 free(impl->dir);
104 free(impl);
105 return 0;
106 }
107
108 return impl;
109}
110
111void icalcalendar_free(icalcalendar *calendar)
112{
113 if (calendar->dir != 0) {
114 free(calendar->dir);
115 }
116
117 if (calendar->freebusy != 0) {
118 icalset_free(calendar->freebusy);
119 }
120
121 if (calendar->properties != 0) {
122 icalset_free(calendar->properties);
123 }
124
125 if (calendar->booked != 0) {
126 icalset_free(calendar->booked);
127 }
128
129 if (calendar->incoming != 0) {
130 icalset_free(calendar->incoming);
131 }
132
133 calendar->dir = 0;
134 calendar->freebusy = 0;
135 calendar->properties = 0;
136 calendar->booked = 0;
137 calendar->incoming = 0;
138
139 free(calendar);
140}
141
142int icalcalendar_lock(const icalcalendar *calendar)
143{
144 icalerror_check_arg_rz((calendar != 0), "calendar");
145 return 0;
146}
147
148int icalcalendar_unlock(const icalcalendar *calendar)
149{
150 icalerror_check_arg_rz((calendar != 0), "calendar");
151 return 0;
152}
153
154int icalcalendar_islocked(const icalcalendar *calendar)
155{
156 icalerror_check_arg_rz((calendar != 0), "calendar");
157 return 0;
158}
159
160int icalcalendar_ownlock(const icalcalendar *calendar)
161{
162 icalerror_check_arg_rz((calendar != 0), "calendar");
163 return 0;
164}
165
166icalset *icalcalendar_get_booked(icalcalendar *calendar)
167{
168 char dir[MAXPATHLEN];
169
170 icalerror_check_arg_rz((calendar != 0), "calendar");
171
172 dir[0] = '\0';
173 strncpy(dir, calendar->dir, MAXPATHLEN - 1);
174 strncat(dir, "/", MAXPATHLEN - strlen(dir) - 1);
175 strncat(dir, BOOKED_DIR, MAXPATHLEN - strlen(dir) - 1);
176 dir[MAXPATHLEN - 1] = '\0';
177
178 if (calendar->booked == 0) {
180 calendar->booked = icaldirset_new(dir);
181 assert(icalerrno == ICAL_NO_ERROR);
182 }
183
184 return calendar->booked;
185}
186
187icalset *icalcalendar_get_incoming(icalcalendar *calendar)
188{
189 char path[MAXPATHLEN];
190
191 icalerror_check_arg_rz((calendar != 0), "calendar");
192
193 path[0] = '\0';
194 strncpy(path, calendar->dir, MAXPATHLEN - 1);
195 strncat(path, "/", MAXPATHLEN - strlen(path) - 1);
196 strncat(path, INCOMING_FILE, MAXPATHLEN - strlen(path) - 1);
197 path[MAXPATHLEN - 1] = '\0';
198
199 if (calendar->properties == 0) {
200 calendar->properties = icalfileset_new(path);
201 }
202
203 return calendar->properties;
204}
205
206icalset *icalcalendar_get_properties(icalcalendar *calendar)
207{
208 char path[MAXPATHLEN];
209
210 icalerror_check_arg_rz((calendar != 0), "calendar");
211
212 path[0] = '\0';
213 strncpy(path, calendar->dir, MAXPATHLEN - 1);
214 strncat(path, "/", MAXPATHLEN - strlen(path) - 1);
215 strncat(path, PROP_FILE, MAXPATHLEN - strlen(path) - 1);
216 path[MAXPATHLEN - 1] = '\0';
217
218 if (calendar->properties == 0) {
219 calendar->properties = icalfileset_new(path);
220 }
221
222 return calendar->properties;
223}
224
225icalset *icalcalendar_get_freebusy(icalcalendar *calendar)
226{
227 char path[MAXPATHLEN];
228
229 icalerror_check_arg_rz((calendar != 0), "calendar");
230
231 path[0] = '\0';
232 strncpy(path, calendar->dir, MAXPATHLEN - 1);
233 strncat(path, "/", MAXPATHLEN - strlen(path) - 1);
234 strncat(path, FBLIST_FILE, MAXPATHLEN - strlen(path) - 1);
235 path[MAXPATHLEN - 1] = '\0';
236
237 if (calendar->freebusy == 0) {
238 calendar->freebusy = icalfileset_new(path);
239 }
240
241 return calendar->freebusy;
242}
icalset * icalcalendar_get_freebusy(icalcalendar *calendar)
icalset * icalcalendar_get_properties(icalcalendar *calendar)
Routines for storing calendar data in a file system.
Manages a database of ical components and offers interfaces for reading, writing and searching for co...
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
void icalerror_clear_errno(void)
Resets icalerrno to ICAL_NO_ERROR.
Definition icalerror.c:85
icalerrorenum
Represents the different types of errors that can be triggered in libical.
Definition icalerror.h:42
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
@ ICAL_BADARG_ERROR
Definition icalerror.h:47
@ ICAL_FILE_ERROR
Definition icalerror.h:65
@ ICAL_NO_ERROR
Definition icalerror.h:44
#define icalerrno
Access the current icalerrno value.
Definition icalerror.h:130
Manages a database of ical components and offers interfaces for reading, writing and searching for co...
void icalset_free(icalset *set)
Definition icalset.c:325