/
UIxCalFolderActions.m
178 lines (144 loc) · 5.55 KB
/
UIxCalFolderActions.m
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
/*
Copyright (C) 2006-2016 Inverse inc.
This file is part of SOGo
SOGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with SOGo; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#import <Foundation/NSValue.h>
#import <NGHttp/NGHttpRequest.h>
#import <NGObjWeb/NSException+HTTP.h>
#define COMPILING_NGOBJWEB 1 /* httpRequest is needed in
importAction */
#import <NGObjWeb/WORequest.h>
#undef COMPILING_NGOBJWEB
#import <NGObjWeb/WOResponse.h>
#import <NGMime/NGMimeMultipartBody.h>
#import <NGCards/iCalCalendar.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <Appointments/SOGoWebAppointmentFolder.h>
#import <Appointments/SOGoAppointmentFolderICS.h>
#import "UIxCalFolderActions.h"
@implementation UIxCalFolderActions
- (WOResponse *) exportAction
{
WOResponse *response;
SOGoAppointmentFolderICS *folderICS;
NSString *disposition;
folderICS = [self clientObject];
response = [self responseWithStatus: 200
andString: [folderICS contentAsString]];
[response setHeader: @"text/calendar; charset=utf-8"
forKey: @"content-type"];
disposition = [NSString stringWithFormat: @"attachment; filename=\"%@.ics\"",
[[folderICS displayName] asQPSubjectString: @"utf-8"]];
[response setHeader: disposition forKey: @"Content-Disposition"];
return response;
}
- (WOResponse *) importAction
{
SOGoAppointmentFolder *folder;
NSMutableDictionary *rc;
iCalCalendar *additions;
NSString *fileContent;
WOResponse *response;
WORequest *request;
NSArray *cals;
id data;
int i, imported;
imported = 0;
rc = [NSMutableDictionary dictionary];
request = [context request];
folder = [self clientObject];
data = [[request httpRequest] body];
// We got an exception, that means the file upload limit
// has been reached.
if ([data isKindOfClass: [NSException class]])
{
response = [self responseWithStatus: 507];
return response;
}
data = [[[data parts] lastObject] body];
fileContent = [[NSString alloc] initWithData: (NSData *) data
encoding: NSUTF8StringEncoding];
if (fileContent == nil)
fileContent = [[NSString alloc] initWithData: (NSData *) data
encoding: NSISOLatin1StringEncoding];
[fileContent autorelease];
if (fileContent && [fileContent length]
&& [fileContent hasPrefix: @"BEGIN:"])
{
cals = [iCalCalendar parseFromSource: fileContent];
for (i = 0; i < [cals count]; i++)
{
additions = [cals objectAtIndex: i];
imported += [folder importCalendar: additions];
}
}
[rc setObject: [NSNumber numberWithInt: imported]
forKey: @"imported"];
response = [self responseWithStatus: 200];
[response setHeader: @"text/html"
forKey: @"content-type"];
[(WOResponse*)response appendContentString: [rc jsonRepresentation]];
return response;
}
/* These methods are only available on instance of SOGoWebAppointmentFolder. */
/**
* @api {get} /so/:username/Scheduler/:calendarId/reload Load Web calendar
* @apiVersion 1.0.0
* @apiName PostReloadWebCalendar
* @apiGroup Calendar
* @apiExample {curl} Example usage:
* curl -i http://localhost/SOGo/so/sogo1/Calendar/5B30-55419180-7-6B687280/reload
*
* @apiDescription Load and parse the events from a remote Web calendar (.ics)
*
* @apiSuccess (Success 200) {Number} status The HTTP code received when accessing the remote URL
* @apiSuccess (Success 200) {String} [imported] The number of imported events in case of success
* @apiError (Error 500) {String} [error] The error type in case of a failure
*/
- (WOResponse *) reloadAction
{
NSDictionary *results;
unsigned int httpCode;
httpCode = 200;
results = [[self clientObject] loadWebCalendar];
if ([results objectForKey: @"status"])
httpCode = [[results objectForKey: @"status"] intValue];
return [self responseWithStatus: httpCode andJSONRepresentation: results];
}
- (WOResponse *) setCredentialsAction
{
NSDictionary *params;
NSString *username, *password;
WORequest *request;
WOResponse *response;
request = [context request];
params = [[request contentAsString] objectFromJSONString];
username = [[params objectForKey: @"username"] stringByTrimmingSpaces];
password = [[params objectForKey: @"password"] stringByTrimmingSpaces];
if ([username length] > 0 && [password length] > 0)
{
[[self clientObject] setUsername: username
andPassword: password];
response = [self responseWith204];
}
else
response = [self responseWithStatus: 400
andJSONRepresentation: [NSDictionary dictionaryWithObject: @"missing 'username' and/or"
@" 'password' parameters"
forKey: @"message"]];
return response;
}
@end /* UIxCalFolderActions */