]> err.no Git - scalable-opengroupware.org/blob - UI/SOGoUI/SOGoFolderAdvisory.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1188 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / SOGoUI / SOGoFolderAdvisory.m
1 /* SOGoFolderAdvisory.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Ludovic Marcotte <ludovic@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSURL.h>
24
25 #import <NGObjWeb/WOResponse.h>
26 #import <NGExtensions/NGHashMap.h>
27 #import <NGMail/NGMimeMessage.h>
28 #import <NGMime/NGMimeBodyPart.h>
29 #import <NGMime/NGMimeMultipartBody.h>
30
31 #import <SoObjects/SOGo/SOGoMailer.h>
32 #import <SoObjects/SOGo/SOGoUser.h>
33 #import <SoObjects/SOGo/SOGoObject.h>
34 #import <SoObjects/SOGo/LDAPUserManager.h>
35 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
36 #import <SoObjects/SOGo/NSString+Utilities.h>
37
38 #import "SOGoFolderAdvisory.h"
39
40 @implementation SOGoFolderAdvisory
41
42 - (id) init
43 {
44   if ((self = [super init]))
45     {
46       recipientUID = nil;
47       folderObject = nil;
48       isSubject = NO;
49       isBody = NO;
50     }
51
52   return self;
53 }
54
55 - (void) dealloc
56 {
57   [recipientUID release];
58   [folderObject release];
59   [super dealloc];
60 }
61
62 - (void) setFolderObject: (SOGoFolder *) theFolder
63 {
64   ASSIGN(folderObject, theFolder);
65 }
66
67 - (void) setRecipientUID: (NSString *) newRecipientUID
68 {
69   ASSIGN (recipientUID, newRecipientUID);
70 }
71
72 - (BOOL) isSubject
73 {
74   return isSubject;
75 }
76
77 - (BOOL) isBody
78 {
79   return isBody;
80 }
81
82 - (NSString *) displayName
83 {
84   return [folderObject displayName];
85 }
86
87 - (NSString *) httpFolderURL
88 {
89   NSString *absoluteString;
90   NSMutableString *url;
91
92 #warning the url returned by SOGoMail may be empty, we need to handle that
93   absoluteString = [[folderObject soURL] absoluteString];
94   url = [NSMutableString stringWithString: absoluteString];
95
96   if (![url hasSuffix: @"/"])
97     [url appendString: @"/"];
98
99   return url;
100 }
101
102 - (NSString *) subject
103 {
104   NSString *subject;
105
106   isSubject = YES;
107   subject = [[self generateResponse] contentAsString];
108   isSubject = NO;
109
110   return [[subject stringByTrimmingSpaces] asQPSubjectString: @"utf-8"];
111 }
112
113 - (NSString *) body
114 {
115   NSString *body;
116
117   isBody = YES;
118   body = [[self generateResponse] contentAsString];
119   isBody = NO;
120
121   return [body stringByTrimmingSpaces];
122 }
123
124 - (NSString *) folderMethod
125 {
126   [self subclassResponsibility: _cmd];
127   
128   return nil;
129 }
130
131 - (NGMimeBodyPart *) _textPart
132 {
133   NGMutableHashMap *headerMap;
134   NGMimeBodyPart *part;
135   NSData *body;
136
137   headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
138   [headerMap setObject: @"text/plain; charset=utf-8" forKey: @"content-type"];
139
140   part = [NGMimeBodyPart bodyPartWithHeader: headerMap];
141   body = [[self body] dataUsingEncoding: NSUTF8StringEncoding];
142   [part setBody: [self body]];
143
144   return part;
145 }
146
147 - (NGMimeBodyPart *) _sogoNotificationPart
148 {
149   NGMutableHashMap *headerMap;
150   NGMimeBodyPart *part;
151   NSData *body;
152
153   /* calendar part */
154   headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
155   [headerMap setObject: [NSString stringWithFormat:
156                                     @"%@; method=%@; type=%@; charset=%@",
157                                   @"application/x-sogo-notification",
158                                   [self folderMethod], [folderObject folderType],
159                                   @"utf-8"]
160              forKey: @"content-type"];
161
162   part = [NGMimeBodyPart bodyPartWithHeader: headerMap];
163   body = [[self httpFolderURL] dataUsingEncoding: NSUTF8StringEncoding];
164   [part setBody: body];
165
166   return part;
167 }
168
169 - (void) send
170 {
171   NSString *recipient, *date;
172   NGMutableHashMap *headerMap;
173   NGMimeMessage *message;
174   NGMimeMultipartBody *body;
175   SOGoUser *activeUser;
176   NSDictionary *identity;
177   NSString *from, *fullMail;
178
179   activeUser = [context activeUser];
180   identity = [activeUser primaryIdentity];
181   from = [identity objectForKey: @"email"];
182   fullMail = [NSString stringWithFormat: @"%@ <%@>",
183                        [identity objectForKey: @"fullName"], from];
184
185   recipient = [[LDAPUserManager sharedUserManager]
186                 getFullEmailForUID: recipientUID];
187
188   headerMap = [NGMutableHashMap hashMapWithCapacity: 5];
189   [headerMap setObject: @"multipart/alternative" forKey: @"content-type"];
190   [headerMap setObject: fullMail forKey: @"From"];
191   [headerMap setObject: recipient forKey: @"To"];
192   date = [[NSCalendarDate date] rfc822DateString];
193   [headerMap setObject: date forKey: @"Date"];
194   [headerMap setObject: [self subject] forKey: @"Subject"];
195   message = [NGMimeMessage messageWithHeader: headerMap];
196
197   body = [[NGMimeMultipartBody alloc] initWithPart: message];
198   [body addBodyPart: [self _textPart]];
199   [body addBodyPart: [self _sogoNotificationPart]];
200   [message setBody: body];
201   [body release];
202
203   [[SOGoMailer sharedMailer] sendMimePart: message
204                              toRecipients: [NSArray arrayWithObject: recipient]
205                              sender: from];
206 }
207
208 @end
209
210 @implementation SOGoFolderEnglishAdditionAdvisory
211 - (NSString *) folderMethod { return @"add"; }
212 @end
213
214 @implementation SOGoFolderEnglishRemovalAdvisory
215 - (NSString *) folderMethod { return @"remove"; }
216 @end
217
218 @implementation SOGoFolderFrenchAdditionAdvisory
219 - (NSString *) folderMethod { return @"add"; }
220 @end
221
222 @implementation SOGoFolderFrenchRemovalAdvisory
223 - (NSString *) folderMethod { return @"remove"; }
224 @end
225
226 @implementation SOGoFolderGermanAdditionAdvisory
227 - (NSString *) folderMethod { return @"add"; }
228 @end
229
230 @implementation SOGoFolderGermanRemovalAdvisory
231 - (NSString *) folderMethod { return @"remove"; }
232 @end