aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libicalss/icalcalendar.c
blob: 0933df1e31bfbd5e22ee868abf5e5829d6105fc6 (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- Mode: C -*-
  ======================================================================
  FILE: icalcalendar.c
  CREATOR: eric 23 December 1999
  
  $Id$
  $Locker$
    
 (C) COPYRIGHT 1999 Eric Busboom
 http://www.softwarestudio.org

 The contents of this file are subject to the Mozilla Public License
 Version 1.0 (the "License"); you may not use this file except in
 compliance with the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and
 limitations under the License.
 
 The Original Code is eric. The Initial Developer of the Original
 Code is Eric Busboom


 ======================================================================*/


#include "icalcalendar.h"
#include "icalcluster.h"
#include <limits.h> 
#include <sys/stat.h> /* For mkdir, stat */
#include <sys/types.h> /* For mkdir */
#include <fcntl.h> /* For mkdir */
#include <unistd.h>  /* For mkdir, stat */    
#include <stdlib.h> /* for malloc */
#include <string.h> /* for strcat */
#include <errno.h>

#define BOOKED_DIR "booked"
#define INCOMING_FILE "incoming.ics"
#define PROP_FILE "properties.ics"
#define FBLIST_FILE "freebusy.ics"

struct icalcalendar_impl 
{
    char* dir;
    icalcomponent* freebusy;
    icalcomponent* properties;
    icalstore* booked;
    icalstore* incoming;
};

struct icalcalendar_impl* icalcalendar_new_impl()
{
    struct icalcalendar_impl* impl;

    if ( ( impl = (struct icalcalendar_impl*)
       malloc(sizeof(struct icalcalendar_impl))) == 0) {
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    return 0;
    }

    return impl;
}


icalerrorenum icalcalendar_create(struct icalcalendar_impl* impl)
{
    char path[PATH_MAX];
    struct stat sbuf;
    int r;
    
    icalerror_check_arg_re((impl != 0),"impl",ICAL_BADARG_ERROR);

    path[0] = '\0';
    strcpy(path,impl->dir);
    strcat(path,"/");
    strcat(path,BOOKED_DIR);

    r = stat(path,&sbuf);

    if( r != 0 && errno == ENOENT){

    if(mkdir(path,0777)!=0){
        icalerror_set_errno(ICAL_FILE_ERROR);
        return ICAL_FILE_ERROR;
    }
    }

    return ICAL_NO_ERROR;
}

icalcalendar* icalcalendar_new(char* dir)
{
    struct icalcalendar_impl* impl;

    icalerror_check_arg_rz((dir != 0),"dir");
    
    impl = icalcalendar_new_impl();

    if (impl == 0){
    return 0;
    }

    impl->dir = (char*)strdup(dir);
    impl->freebusy = 0;
    impl->properties = 0;
    impl->booked = 0;
    impl->incoming = 0;

    if (icalcalendar_create(impl) != ICAL_NO_ERROR){
    free(impl);
    return 0;
    }

    return impl;
}

void icalcalendar_free(icalcalendar* calendar)
{

    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
        
    if (impl->dir !=0){
    free(impl->dir);
    }

    if (impl->freebusy !=0){
    icalcluster_free(impl->freebusy);
    }

    if (impl->properties !=0){
    icalcluster_free(impl->properties);
    }

    if (impl->booked !=0){
    icalstore_free(impl->booked);
    }

    if (impl->incoming !=0){
    icalstore_free(impl->incoming);
    }

    impl->dir = 0;
    impl->freebusy = 0;
    impl->properties = 0;
    impl->booked = 0;
    impl->incoming = 0;


    free(impl);
}


int icalcalendar_lock(icalcalendar* calendar)
{
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");
    return 0;
}

int icalcalendar_unlock(icalcalendar* calendar)
{
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");
    return 0;
}

int icalcalendar_islocked(icalcalendar* calendar)
{
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");
    return 0;
}

int icalcalendar_ownlock(icalcalendar* calendar)
{
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");
    return 0;
}

icalstore* icalcalendar_get_booked(icalcalendar* calendar)
{
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    char dir[PATH_MAX];

    icalerror_check_arg_rz((impl != 0),"impl");
    
    dir[0] = '\0';
    strcpy(dir,impl->dir);
    strcat(dir,"/");
    strcat(dir,BOOKED_DIR);

    if (impl->booked == 0){
    icalerror_clear_errno();
    impl->booked = icalstore_new(dir);
    assert(icalerrno == ICAL_NO_ERROR);
    }

    return impl->booked;

}

icalcluster* icalcalendar_get_incoming(icalcalendar* calendar)
{
    char path[PATH_MAX];
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");

    path[0] = '\0';
    strcpy(path,impl->dir);
    strcat(path,"/");
    strcat(path,INCOMING_FILE);

    if (impl->properties == 0){
    impl->properties = icalcluster_new(path);
    }

    return impl->properties;
}

icalcluster* icalcalendar_get_properties(icalcalendar* calendar)
{
    char path[PATH_MAX];
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");

    path[0] = '\0';
    strcpy(path,impl->dir);
    strcat(path,"/");
    strcat(path,PROP_FILE);

    if (impl->properties == 0){
    impl->properties = icalcluster_new(path);
    }

    return impl->properties;
}

icalcluster* icalcalendar_get_freebusy(icalcalendar* calendar)
{
    char path[PATH_MAX];
    struct icalcalendar_impl *impl = (struct icalcalendar_impl*)calendar;
    icalerror_check_arg_rz((impl != 0),"impl");

    path[0] = '\0';
    strcpy(path,impl->dir);
    strcat(path,"/");
    strcat(path,FBLIST_FILE);


    if (impl->freebusy == 0){
    impl->freebusy = icalcluster_new(path);
    }

    return impl->freebusy;
}