1/*
2  Copyright (C) 2010-2012 Inverse
3
4  This file is part of SOGo
5
6  SOGo is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOGo; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#import <Foundation/NSCalendarDate.h>
23#import <Foundation/NSCharacterSet.h>
24
25#import <NGObjWeb/WOContext+SoObjects.h>
26#import <NGObjWeb/WOResponse.h>
27
28#import <NGCards/iCalEvent.h>
29#import <NGCards/iCalEventChanges.h>
30
31#import <SOGo/NSDictionary+Utilities.h>
32#import <SOGo/NSObject+Utilities.h>
33#import <SOGo/SOGoDateFormatter.h>
34#import <SOGo/SOGoUser.h>
35
36#import "SOGoAptMailNotification.h"
37
38@interface SOGoAptMailUpdate : SOGoAptMailNotification
39{
40  NSMutableDictionary *changes;
41  NSString *currentItem;
42}
43
44@end
45
46@implementation SOGoAptMailUpdate
47
48- (id) init
49{
50  self = [super init];
51
52  changes = [[NSMutableDictionary alloc] init];
53
54  return self;
55}
56
57- (void) dealloc
58{
59  RELEASE(currentItem);
60  RELEASE(changes);
61  [super dealloc];
62}
63
64- (NSString *) valueForProperty: (NSString *) property
65              withDateFormatter: (SOGoDateFormatter *) _dateFormatter
66{
67  static NSDictionary *valueTypes = nil;
68  NSString *valueType;
69  id value;
70
71  if (!valueTypes)
72    {
73      valueTypes = [NSDictionary dictionaryWithObjectsAndKeys:
74                                   @"date", @"startDate",
75                                 @"date", @"endDate",
76                                 @"date", @"due",
77                                 @"text", @"location",
78                                 @"text", @"summary",
79                                 @"text", @"comment",
80                                 nil];
81      [valueTypes retain];
82    }
83
84  valueType = [valueTypes objectForKey: property];
85  if (valueType)
86    {
87      value = [(iCalEvent *) apt propertyValue: property];
88      if ([valueType isEqualToString: @"date"])
89        {
90          [value setTimeZone: viewTZ];
91          if ([apt isAllDay])
92            value = [_dateFormatter formattedDate: value];
93          else
94            value = [_dateFormatter formattedDateAndTime: value];
95        }
96    }
97  else
98    value = nil;
99
100  return value;
101}
102
103- (void) _setupBodyContentWithFormatter: (SOGoDateFormatter *) _dateFormatter
104{
105  NSString *property, *label, *value;
106  NSArray *updatedProperties;
107  int count, max;
108
109  updatedProperties = [[iCalEventChanges changesFromEvent: previousApt
110                                                  toEvent: apt]
111                        updatedProperties];
112  max = [updatedProperties count];
113  for (count = 0; count < max; count++)
114    {
115      property = [updatedProperties objectAtIndex: count];
116      value = [self valueForProperty: property
117                   withDateFormatter: _dateFormatter];
118      /* Unhandled properties will return nil */
119      if (value)
120        {
121          label = [self labelForKey: [NSString stringWithFormat: @"%@_label",
122                                               property]
123                          inContext: context];
124	  [changes setObject: value  forKey: label];
125        }
126    }
127}
128
129- (NSArray *) allChangesList
130{
131  return [changes allKeys];
132}
133
134- (void) setCurrentItem: (NSString *) theItem
135{
136  ASSIGN(currentItem, theItem);
137}
138
139- (NSString *) currentItem
140{
141  return currentItem;
142}
143
144- (NSString *) valueForCurrentItem
145{
146  return [changes objectForKey: currentItem];
147}
148
149- (NSString *) bodyStartText
150{
151  NSString *bodyText;
152
153  bodyText = [self labelForKey: @"The following parameters have changed"
154                   @" in the \"%{Summary}\" meeting:"
155                     inContext: context];
156
157  return [values keysWithFormat: bodyText];
158}
159
160- (void) setupValues
161{
162  NSCalendarDate *date;
163  SOGoDateFormatter *localDateFormatter;
164
165  [super setupValues];
166
167  localDateFormatter = [[context activeUser] dateFormatterInContext: context];
168
169  date = [self oldStartDate];
170  [values setObject: [localDateFormatter shortFormattedDate: date]
171             forKey: @"OldStartDate"];
172
173  if (![apt isAllDay])
174    [values setObject: [localDateFormatter formattedTime: date]
175               forKey: @"OldStartTime"];
176
177  [self _setupBodyContentWithFormatter: localDateFormatter];
178}
179
180- (NSString *) getSubject
181{
182  NSString *subjectFormat;
183
184  if (!values)
185    [self setupValues];
186
187  if ([values objectForKey: @"OldStartTime"])
188    subjectFormat = [self labelForKey: (@"The appointment \"%{Summary}\" for the"
189                                        @" %{OldStartDate} at"
190                                        @" %{OldStartTime} has changed")
191                            inContext: context];
192  else
193    subjectFormat = [self labelForKey: (@"The appointment \"%{Summary}\" for the"
194                                        @" %{OldStartDate}"
195                                        @" has changed")
196                            inContext: context];
197
198  return [values keysWithFormat: subjectFormat];
199}
200
201- (NSString *) getBody
202{
203  NSString *body;
204
205  if (!values)
206    [self setupValues];
207
208  body = [[self generateResponse] contentAsString];
209
210  return [body stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
211}
212
213@end
214