File: StoreManager.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 (372 lines) | stat: -rw-r--r-- 10,818 bytes parent folder | download | duplicates (2)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import "AgendaStore.h"
#import "StoreManager.h"
#import "ConfigManager.h"
#import "defines.h"
#import "Event.h"

NSString * const SADataChangedInStoreManager = @"SADataDidChangedInStoreManager";
static NSString * const PERSONAL_AGENDA = @"Personal Agenda";

static NSMutableDictionary *backends;
static StoreManager *singleton;

@implementation StoreManager
- (void)initStores
{
  NSArray *storeArray;
  NSString *defaultStore;
  NSEnumerator *enumerator;
  NSString *stname;
  Class backendClass;
  ConfigManager *config = [ConfigManager globalConfig];
  id store;

  /* Create user defined stores */
  storeArray = [config objectForKey:STORES];
  defaultStore = [config objectForKey:ST_DEFAULT];
  enumerator = [storeArray objectEnumerator];
  while ((stname = [enumerator nextObject]))
    [self addStoreNamed:stname];

  /* Create automatic stores */
  enumerator = [backends objectEnumerator];
  while ((backendClass = [enumerator nextObject])) {
    if (![backendClass isUserInstanciable] && [backendClass storeName]) {
      store = [backendClass storeNamed:[backendClass storeName]];
      [_stores setObject:store forKey:[backendClass storeName]];
      NSLog(@"Added %@ to StoreManager", [backendClass storeName]);
    }
  }
  [self setDefaultStore:defaultStore];
}

+ (void)initialize
{
  NSArray *classes;
  NSEnumerator *enumerator;
  Class backendClass;

  if (self == [StoreManager class]) {
    classes = GSObjCAllSubclassesOfClass([MemoryStore class]);
    enumerator = [classes objectEnumerator];
    backends = [[NSMutableDictionary alloc] initWithCapacity:[classes count]];
    while ((backendClass = [enumerator nextObject])) {
      if ([backendClass conformsToProtocol:@protocol(MemoryStore)])
	[backends setObject:backendClass forKey:[backendClass storeTypeName]];
      else
	NSLog(@"Can't register %@ as a store backend", [backendClass description]);
    }
    singleton = [StoreManager new];
    /*
     * Stores have to be loaded after the singleton is fully initialized 
     * as they might call methods depending on the singleton like
     * [StoreManager globalManager]
     */
    [singleton initStores];
  }
}

+ (NSArray *)backends
{
  return [backends allValues];
}

+ (Class)backendForName:(NSString *)name
{
  return [backends valueForKey:name];
}

+ (StoreManager *)globalManager
{
  return singleton;
}

- (NSDictionary *)defaults
{
  NSDictionary *local = [NSDictionary
			  dictionaryWithObjects:[NSArray arrayWithObjects:@"LocalStore", @"Personal", nil]
			  forKeys:[NSArray arrayWithObjects:ST_CLASS, ST_FILE, nil]];
  NSDictionary *dict = [NSDictionary 
			 dictionaryWithObjects:[NSArray arrayWithObjects: [NSArray arrayWithObject:PERSONAL_AGENDA], local, PERSONAL_AGENDA, nil]
			 forKeys:[NSArray arrayWithObjects: STORES, PERSONAL_AGENDA, ST_DEFAULT, nil]];
  return dict;
}

- (id)init
{
  if (!(self = [super init]))
    return nil;
  
  [[ConfigManager globalConfig] registerDefaults:[self defaults]];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(dataChanged:)
					       name:SADataChangedInStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(dataChanged:)
					       name:SAElementAddedToStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(dataChanged:)
					       name:SAElementRemovedFromStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(dataChanged:)
					       name:SAElementUpdatedInStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(dataChanged:)
					       name:SAEnabledStatusChangedForStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(handleError:)
					       name:SAErrorReadingStore
					     object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self 
					   selector:@selector(handleError:)
					       name:SAErrorWritingStore
					     object:nil];
  _stores = [[NSMutableDictionary alloc] initWithCapacity:1];
  _dayEventsCache = [[NSMutableDictionary alloc] initWithCapacity:256];
  _eventCache = [[NSMutableArray alloc] initWithCapacity:512];
  _opqueue = [NSOperationQueue new];
  return self;
}

- (void)dealloc
{
  // FIXME : this isn't called on exit, probably a dangling reference
  NSDebugLLog(@"SimpleAgenda", @"Releasing StoreManager");
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [self synchronise];
  RELEASE(_defaultStore);
  RELEASE(_stores);
  RELEASE(_dayEventsCache);
  RELEASE(_eventCache);
  RELEASE(_opqueue);
  [super dealloc];
}

- (NSOperationQueue *)operationQueue
{
  return _opqueue;
}

- (void)dataChanged:(NSNotification *)not
{
  [_dayEventsCache removeAllObjects];
  [_eventCache removeAllObjects];
  [[NSNotificationCenter defaultCenter] postNotificationName:SADataChangedInStoreManager object:self];
}

- (void)displayPanelFromDictionary:(NSDictionary *)dict
{
  NSRunAlertPanel([dict objectForKey:@"title"], [dict objectForKey:@"message"], _(@"Ok"), nil, nil);
}

- (void)handleError:(NSNotification *)not
{
  id <AgendaStore> store = [not object];
  NSNumber *errorCode = [[not userInfo] objectForKey:@"errorCode"];
  NSString *title = [NSString stringWithFormat:_(@"Error on calendar %@"), [store description]];

  if ([[not name] isEqualToString:SAErrorReadingStore]) {
    [store setEnabled:NO];
    [self performSelectorOnMainThread:@selector(displayPanelFromDictionary:) 
			   withObject:[NSDictionary dictionaryWithObjectsAndKeys:title, @"title", _(@"We're unable to read the calendar, it will be disabled."), @"message", nil]
			waitUntilDone:YES];
  }
  if ([[not name] isEqualToString:SAErrorWritingStore]) {
    if ([errorCode intValue] == 412) {      
      [self performSelectorOnMainThread:@selector(displayPanelFromDictionary:) 
			     withObject:[NSDictionary dictionaryWithObjectsAndKeys:title, @"title", _(@"The calendar was modified. To prevent losing other modifications, it will be updated."), @"message", nil]
			  waitUntilDone:YES];
    } else {
      [store setWritable:NO];
      [self performSelectorOnMainThread:@selector(displayPanelFromDictionary:) 
			     withObject:[NSDictionary dictionaryWithObjectsAndKeys:title, @"title", _(@"We're unable to save modifications, this calendar will be marked as read only and reread."), @"message", nil]
			  waitUntilDone:YES];
    }
    [store read];
  }
}

- (void)addStoreNamed:(NSString *)name
{
  Class storeClass;
  id <AgendaStore> store;
  NSDictionary *dict;

  dict = [[ConfigManager globalConfig] objectForKey:name];
  if (dict) {
    if ([dict objectForKey:ST_CLASS] != nil)
      storeClass = NSClassFromString([dict objectForKey:ST_CLASS]);
    else
      storeClass = NSClassFromString(@"LocalStore");
    store = [storeClass storeNamed:name];
    if (store) {
      [_stores setObject:store forKey:name];
      NSLog(@"Added %@ to StoreManager", name);
    } else {
      NSLog(@"Unable to initialize store %@", name);
    }
  }
}

- (void)removeStoreNamed:(NSString *)name
{
  [_stores removeObjectForKey:name];
  NSLog(@"Removed %@ from StoreManager", name);
  [self dataChanged:nil];
}

- (id <AgendaStore>)storeForName:(NSString *)name
{
  return [_stores objectForKey:name];
}

- (void)setDefaultStore:(NSString *)name
{
  id st = [self storeForName:name];
  if (st != nil) {
    ASSIGN(_defaultStore, st);
    [[ConfigManager globalConfig] setObject:name forKey:ST_DEFAULT];
  }
}

- (id <AgendaStore>)defaultStore
{
  return _defaultStore;
}

- (NSEnumerator *)storeEnumerator
{
  return [_stores objectEnumerator];
}

- (void)synchronise
{
  NSEnumerator *enumerator = [_stores objectEnumerator];
  id <AgendaStore> store;

  while ((store = [enumerator nextObject]))
    if ([store conformsToProtocol:@protocol(StoreBackend)])
      [store write];
}

- (void)refresh
{
  NSEnumerator *enumerator = [_stores objectEnumerator];
  id <AgendaStore> store;

  while ((store = [enumerator nextObject]))
    if ([store conformsToProtocol:@protocol(StoreBackend)])
      [store read];
}

- (id <AgendaStore>)storeContainingElement:(Element *)elt
{
  NSEnumerator *enumerator = [_stores objectEnumerator];
  id <AgendaStore> store;

  while ((store = [enumerator nextObject]))
    if ([store contains:elt])
      return store;
  return nil;
}

- (BOOL)moveElement:(Element *)elt toStore:(id <MemoryStore>)store
{
  id <MemoryStore> origin = [elt store];

  if (origin && ![origin writable])
    return NO;
  if (![store writable])
    return NO;
  if (!origin)
    [store add:elt];
  else if (origin == store)
    [store update:elt];
  else {
    [elt retain];
    [origin remove:elt];
    [store add:elt];
    [elt release];
  }
  return YES;
}

- (NSArray *)allEvents
{
  NSEnumerator *enumerator;
  id <AgendaStore> store;

  if ([_eventCache count])
    return _eventCache;
  enumerator = [_stores objectEnumerator];
  while ((store = [enumerator nextObject]))
    if ([store enabled])
      [_eventCache addObjectsFromArray:[store events]];
  return _eventCache;
}

- (NSArray *)allTasks
{
  NSMutableArray *all = [NSMutableArray arrayWithCapacity:32];
  NSEnumerator *enumerator = [_stores objectEnumerator];
  id <AgendaStore> store;

  while ((store = [enumerator nextObject]))
    if ([store enabled])
      [all addObjectsFromArray:[store tasks]];
  return all;
}

- (NSArray *)visibleTasks
{
  NSMutableArray *all = [NSMutableArray arrayWithCapacity:32];
  NSEnumerator *enumerator = [_stores objectEnumerator];
  id <AgendaStore> store;

  while ((store = [enumerator nextObject]))
    if ([store enabled] && [store displayed])
      [all addObjectsFromArray:[store tasks]];
  return all;
}

- (NSSet *)scheduledAppointmentsForDay:(Date *)date
{
  NSMutableSet *dayEvents;
  NSEnumerator *enumerator;
  Event *event;

  NSAssert(date != nil, @"No date specified, am I supposed to guess ?");
  dayEvents = [_dayEventsCache objectForKey:(id)date];
  if (dayEvents)
    return dayEvents;

  dayEvents = [NSMutableSet setWithCapacity:8];
  enumerator = [[self allEvents] objectEnumerator];
  while ((event = [enumerator nextObject]))
    if ([event isScheduledForDay:date])
      [dayEvents addObject:event];
  [_dayEventsCache setObject:dayEvents forKey:(id)date];
  return dayEvents;
}

- (NSSet *)visibleAppointmentsForDay:(Date *)date
{
  NSMutableSet *visible = [NSMutableSet setWithCapacity:4];
  NSEnumerator *enumerator;
  Event *event;

  enumerator = [[self scheduledAppointmentsForDay:date] objectEnumerator];
  while ((event = [enumerator nextObject])) {
    if ([[event store] displayed])
      [visible addObject:event];
  }
  return visible;
}
@end