File: Alarm.m

package info (click to toggle)
agenda.app 0.47-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: objc: 8,103; makefile: 16; sh: 5
file content (304 lines) | stat: -rw-r--r-- 7,761 bytes parent folder | download | duplicates (3)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* emacs buffer mode hint -*- objc -*- */

#import "Alarm.h"
#import "Date.h"
#import "Element.h"
#import "HourFormatter.h"

@implementation Alarm
- (void)deleteProperty:(icalproperty_kind)kind fromComponent:(icalcomponent *)ic
{
  icalproperty *prop = icalcomponent_get_first_property(ic, kind);
  if (prop)
      icalcomponent_remove_property(ic, prop);
}

- (void)encodeWithCoder:(NSCoder *)coder
{
  [coder encodeObject:[NSString stringWithCString:icalcomponent_as_ical_string(_ic)] forKey:@"alarmComponent"];
}

- (id)initWithCoder:(NSCoder *)coder
{
  _ic = icalcomponent_new_from_string([[coder decodeObjectForKey:@"alarmComponent"] cString]);
  return self;
}

- (void)dealloc
{
  icalcomponent_free(_ic);
  [super dealloc];
}

- (id)init
{
  self = [super init];
  if (self) {
    _element = nil;
    _ic = icalcomponent_new([self iCalComponentType]);
    if (!_ic) {
      NSLog(@"Error while creating an VALARM component");
      DESTROY(self);
    }
  }
  return self;
}

+ (id)alarm
{
  return AUTORELEASE([[Alarm alloc] init]);
}

- (NSAttributedString *)desc
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_DESCRIPTION_PROPERTY);
  if (prop)
    return AUTORELEASE([[NSAttributedString alloc] initWithString:[NSString stringWithUTF8String:icalproperty_get_description(prop)]]);
  return nil;
}

- (void)setDesc:(NSAttributedString *)desc
{
  [self deleteProperty:ICAL_DESCRIPTION_PROPERTY fromComponent:_ic];
  if (desc)
    icalcomponent_add_property(_ic, icalproperty_new_description([[desc string] UTF8String]));
}

- (NSString *)summary
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_SUMMARY_PROPERTY);
  if (!prop) {
    NSLog(@"Error : no summary property");
    return nil;
  }
  return [NSString stringWithUTF8String:icalproperty_get_summary(prop)];    
}

- (void)setSummary:(NSString *)summary
{
  [self deleteProperty:ICAL_SUMMARY_PROPERTY fromComponent:_ic];
  if (summary)
    icalcomponent_add_property(_ic, icalproperty_new_summary([summary UTF8String]));
}

- (BOOL)isAbsoluteTrigger
{
  struct icaltriggertype trigger;
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_TRIGGER_PROPERTY);

  if (!prop) {
    NSLog(@"Error : no trigger property");
    return NO;
  }
  trigger = icalproperty_get_trigger(prop);
  if (icaltime_is_null_time(trigger.time))
    return NO;
  return YES;
}

- (Date *)absoluteTrigger
{
  struct icaltriggertype trigger;
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_TRIGGER_PROPERTY);

  if (!prop) {
    NSLog(@"Error : no trigger property");
    return nil;
  }
  trigger = icalproperty_get_trigger(prop);
  return AUTORELEASE([[Date alloc] initWithICalTime:trigger.time]);
}

- (void)setAbsoluteTrigger:(Date *)date
{
  struct icaltriggertype trigger;

  [self deleteProperty:ICAL_TRIGGER_PROPERTY fromComponent:_ic];
  memset(&trigger, 0, sizeof(trigger));
  trigger.time = [date UTCICalTime];
  icalcomponent_add_property(_ic, icalproperty_new_trigger(trigger));
}

- (NSTimeInterval)relativeTrigger
{
  struct icaltriggertype trigger;
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_TRIGGER_PROPERTY);

  if (!prop) {
    NSLog(@"Error : no trigger property");
    return -1;
  }
  trigger = icalproperty_get_trigger(prop);
  return icaldurationtype_as_int(trigger.duration);
}

- (void)setRelativeTrigger:(NSTimeInterval)duration
{
  struct icaltriggertype trigger;

  [self deleteProperty:ICAL_TRIGGER_PROPERTY fromComponent:_ic];
  memset(&trigger, 0, sizeof(trigger));
  trigger.duration = icaldurationtype_from_int(duration);
  icalcomponent_add_property(_ic, icalproperty_new_trigger(trigger));
}

- (enum icalproperty_action)action
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_ACTION_PROPERTY);

  if (!prop) {
    NSLog(@"Error : no ACTION property");
    return -1;
  }
  return icalproperty_get_action(prop);
}

- (void)setAction:(enum icalproperty_action)action
{
  [self deleteProperty:ICAL_ACTION_PROPERTY fromComponent:_ic];
  icalcomponent_add_property(_ic, icalproperty_new_action(action));
}

- (NSString *)emailAddress
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_ATTENDEE_PROPERTY);

  if (prop)
    return [NSString stringWithUTF8String:icalproperty_get_attendee(prop)];
  return nil;
}

- (void)setEmailAddress:(NSString *)emailAddress
{
  [self deleteProperty:ICAL_ATTENDEE_PROPERTY fromComponent:_ic];
  if (emailAddress) {
    icalcomponent_add_property(_ic, icalproperty_new_attendee([emailAddress UTF8String]));
    [self setAction:ICAL_ACTION_EMAIL];
  }
}

- (NSString *)sound
{
  /* FIXME */
  return nil;
}

- (void)setSound:(NSString *)sound
{
  /* FIXME */
  if (sound) {
    [self setAction:ICAL_ACTION_AUDIO];
  }
}

- (NSURL *)url
{
  /* FIXME */
  return nil;
}

- (void)setUrl:(NSURL *)url
{
  /* FIXME */
  if (url) {
    [self setAction:ICAL_ACTION_PROCEDURE];
  }
}

- (int)repeatCount
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_REPEAT_PROPERTY);

  if (prop)
    return icalproperty_get_repeat(prop);
  return 0;
}

- (void)setRepeatCount:(int)count
{
  [self deleteProperty:ICAL_REPEAT_PROPERTY fromComponent:_ic];
  if (count > 0)
    icalcomponent_add_property(_ic, icalproperty_new_repeat(count));
}

- (NSTimeInterval)repeatInterval
{
  icalproperty *prop = icalcomponent_get_first_property(_ic, ICAL_DURATION_PROPERTY);

  if (prop)
    return icaldurationtype_as_int(icalproperty_get_duration(prop));
  return 0;
}

- (void)setRepeatInterval:(NSTimeInterval)interval
{
  [self deleteProperty:ICAL_DURATION_PROPERTY fromComponent:_ic];
  if (interval > 0)
    icalcomponent_add_property(_ic, icalproperty_new_duration(icaldurationtype_from_int(interval)));
}

- (Element *)element
{
  return _element;
}

- (void)setElement:(Element *)element
{
  _element = element;
  NSDebugLLog(@"SimpleAgenda", @"Added %@ to element %@", [self description], [element UID]);
}

- (Date *)triggerDateRelativeTo:(Date *)date
{
  return [Date dateWithTimeInterval:[self relativeTrigger] sinceDate:date];
}

- (NSString *)description
{
  if ([self isAbsoluteTrigger])
    return [NSString stringWithFormat:@"Absolute trigger set to %@ repeat %d interval %f description <%@>", [[self absoluteTrigger] description], [self repeatCount], [self repeatInterval], [self desc]];
  return [NSString stringWithFormat:@"Relative trigger delay %f repeat %d interval %f description <%@>", [self relativeTrigger], [self repeatCount], [self repeatInterval], [self desc]];
}

- (NSString *)shortDescription
{
  NSTimeInterval trigger;

  if ([self isAbsoluteTrigger])
    return [NSString stringWithFormat:_(@"Trigger on %@"), [[[self absoluteTrigger] calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] objectForKey:NSShortTimeDateFormatString]]];
  trigger = [self relativeTrigger];
  if (trigger >= 0)
    return [NSString stringWithFormat:_(@"Trigger %@ after the event"), [HourFormatter stringForObjectValue:[NSNumber numberWithInt:trigger]]];
  return [NSString stringWithFormat:_(@"Trigger %@ before the event"), [HourFormatter stringForObjectValue:[NSNumber numberWithInt:-trigger]]];
}

- (id)copyWithZone:(NSZone *)zone
{
  return [[Alarm allocWithZone:zone] initWithICalComponent:[self asICalComponent]];
}


- (id)initWithICalComponent:(icalcomponent *)ic
{
  if ((self = [super init])) {
    _element = nil;
    _ic = icalcomponent_new_clone(ic);
    if (!_ic) {
      NSLog(@"Error creating Alarm from iCal component");
      NSLog(@"\n\n%s", icalcomponent_as_ical_string(ic));
      DESTROY(self);
    }
  }
  return self;
}

- (icalcomponent *)asICalComponent
{
  return icalcomponent_new_clone(_ic);
}

- (int)iCalComponentType
{
  return ICAL_VALARM_COMPONENT;
}
@end