forked from ttscoff/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncSessionController.m
444 lines (366 loc) · 16.7 KB
/
SyncSessionController.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// SyncSessionController.m
// Notation
//
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
- Neither the name of Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "SyncSessionController.h"
#import "NotationPrefs.h"
#import "InvocationRecorder.h"
#import "SyncServiceSessionProtocol.h"
#import "NotationDirectoryManager.h"
#import "SimplenoteSession.h"
//#import <IOKit/IOMessage.h>
NSString *SyncSessionsChangedVisibleStatusNotification = @"SSCVSN";
@implementation SyncSessionController
static void SleepCallBack(void *refcon, io_service_t y, natural_t messageType, void * messageArgument);
- (id)initWithSyncDelegate:(id)aSyncDelegate notationPrefs:(NotationPrefs*)prefs {
if (self=[super init]) {
if (!(syncDelegate = aSyncDelegate)) {
NSLog(@"%@: need syncDelegate!", NSStringFromSelector(_cmd));
return nil;
}
if (!(notationPrefs = [prefs retain])) {
NSLog(@"%@: need notationPrefs!", NSStringFromSelector(_cmd));
return nil;
}
syncServiceTimers = [[NSMutableDictionary alloc] init];
return self;
}
return nil;
}
//these two methods must return parallel arrays:
+ (NSArray*)allServiceNames {
static NSArray *allNames = nil;
if (!allNames) allNames = [[NSArray alloc] initWithObjects:SimplenoteServiceName, nil];
return allNames;
}
+ (NSArray*)allServiceClasses {
static NSArray *allClasses = nil;
if (!allClasses) allClasses = [[NSArray alloc] initWithObjects:NSClassFromString(@"SimplenoteSession"), nil];
return allClasses;
}
- (void)setSyncDelegate:(id)aDelegate {
syncDelegate = aDelegate;
}
- (id)syncDelegate {
return syncDelegate;
}
static void SleepCallBack(void *refcon, io_service_t y, natural_t messageType, void * messageArgument) {
SyncSessionController *self = (SyncSessionController*)refcon;
InvocationRecorder *invRecorder = nil;
switch (messageType) {
case kIOMessageSystemWillSleep:
[[(invRecorder = [InvocationRecorder invocationRecorder]) prepareWithInvocationTarget:self] endDelayingSleepWithMessage:messageArgument];
if (![self waitForUncommitedChangesWithInvocation:[invRecorder invocation]]) {
//if we don't have to wait, then do not delay sleep
[self endDelayingSleepWithMessage:messageArgument];
} else {
NSLog(@"delaying sleep for uncommitted changes");
}
break;
case kIOMessageCanSystemSleep:
//pevent idle sleep if a session is currently running
if ([self hasRunningSessions]) {
IOCancelPowerChange(self->fRootPort, (long)messageArgument);
} else {
IOAllowPowerChange(self->fRootPort, (long)messageArgument);
}
break;
case kIOMessageSystemHasPoweredOn:
//after waking from sleep, probably don't need to do anything as the services' network reachability check(s) ought to run later, anyway
break;
}
}
- (void)endDelayingSleepWithMessage:(void*)messageArgument {
//NSLog(@"allow powerchange under port %X for '%d'", fRootPort, (long)messageArgument);
IOAllowPowerChange(fRootPort, (long)messageArgument);
}
- (void)_registerPowerChangeCallbackIfNecessary {
if (!notifyPortRef) {
if ((fRootPort = IORegisterForSystemPower((void*)self, ¬ifyPortRef, SleepCallBack, &deregisteringNotifier))) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes);
//NSLog(@"registered for power change under port %X", fRootPort);
} else {
NSLog(@"error: IORegisterForSystemPower");
}
}
}
- (void)unregisterPowerChangeCallback {
if (notifyPortRef) {
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes);
IODeregisterForSystemPower(&deregisteringNotifier);
IOServiceClose(fRootPort);
IONotificationPortDestroy(notifyPortRef);
//NSLog(@"unregistered for power change under port %X", fRootPort);
fRootPort = 0;
notifyPortRef = NULL;
}
}
- (id<SyncServiceSession>)_sessionForSyncService:(NSString*)serviceName {
//map names to sync service sessions, creating them if necessary
NSAssert(serviceName != nil, @"servicename is required");
if (!syncServiceSessions) syncServiceSessions = [[NSMutableDictionary alloc] initWithCapacity:1];
id<SyncServiceSession> session = [syncServiceSessions objectForKey:serviceName];
if (!session) {
if ([serviceName isEqualToString:SimplenoteServiceName]) {
if (![notationPrefs syncServiceIsEnabled:SimplenoteServiceName]) return nil;
SimplenoteSession *snSession = [[SimplenoteSession alloc] initWithNotationPrefs:notationPrefs];
if (snSession) {
[syncServiceSessions setObject:snSession forKey:serviceName];
[snSession setDelegate:syncDelegate];
[snSession release]; //owned by syncServiceSessions
}
return snSession;
} /* else if ([serviceName isEqualToString:SimpletextServiceName]) {
//init and return other services here
} */ else {
NSLog(@"%@: unknown service named '%@'", NSStringFromSelector(_cmd), serviceName);
}
}
return session;
}
- (void)invalidateSyncService:(NSString*)serviceName {
id<SyncServiceSession> session = [[[syncServiceSessions objectForKey:serviceName] retain] autorelease];
//ensure that reachability is unscheduled if dealloc of session does not occur here
if ([session respondsToSelector:@selector(invalidateReachabilityRefs)])
[session performSelector:@selector(invalidateReachabilityRefs)];
[session stop];
[session setDelegate:nil];
[syncServiceSessions removeObjectForKey:serviceName];
[[syncServiceTimers objectForKey:serviceName] invalidate];
[syncServiceTimers removeObjectForKey:serviceName];
//can't unregister power-change-callback here because network interruptions could extend sleep via dissociating the notifier
}
- (void)initializeService:(NSString*)serviceName {
[self queueStatusNotification];
id <SyncServiceSession> session = [[[self _sessionForSyncService:serviceName] retain] autorelease];
if (session) {
if (![syncServiceTimers objectForKey:serviceName]) {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:[notationPrefs syncFrequencyInMinutesForServiceName:serviceName] * 60.0
target:self selector:@selector(handleSyncServiceTimer:) userInfo:session repeats:YES];
[syncServiceTimers setObject:timer forKey:serviceName];
}
[self _registerPowerChangeCallbackIfNecessary];
//start syncing now
[session startFetchingListForFullSync];
}
}
- (void)handleSyncServiceTimer:(NSTimer*)aTimer {
id <SyncServiceSession> session = [aTimer userInfo];
NSAssert([session conformsToProtocol:@protocol(SyncServiceSession)], @"incorrect userinfo object from sync timer");
//file notifications are not always caught without user activity; let's make sure the directory is always in sync
//this will have the side effect of showing the deletion-warning sheet at potentially unexpected times
if ([syncDelegate respondsToSelector:@selector(synchronizeNotesFromDirectory)])
[syncDelegate synchronizeNotesFromDirectory];
[session startFetchingListForFullSync];
}
- (void)disableService:(NSString*)serviceName {
//stops the service, turns it off, and removes the password
[self invalidateSyncService:serviceName];
[notationPrefs setSyncEnabled:NO forService:serviceName];
[notationPrefs removeSyncPasswordForService:serviceName];
//remove password to prevent instant reactivation of whatever alert ultimately prompted this action, if the user re-enables the service
//should not need this; this class should control sync prefs directly, or at least syncprefs objs
[[NSNotificationCenter defaultCenter] postNotificationName:NotationPrefsDidChangeNotification object:nil];
}
- (void)invalidateAllServices {
NSArray *svcs = [[[syncServiceSessions allKeys] copy] autorelease];
NSUInteger i = 0;
for (i=0; i<[svcs count]; i++) [self invalidateSyncService:[svcs objectAtIndex:i]];
}
- (void)initializeAllServices {
NSArray *svcs = [[self class] allServiceNames];
NSUInteger i = 0;
for (i=0; i<[svcs count]; i++) [self initializeService:[svcs objectAtIndex:i]];
}
- (void)schedulePushToAllInitializedSessionsForNote:(id <SynchronizedNote>)aNote {
[[syncServiceSessions allValues] makeObjectsPerformSelector:@selector(schedulePushForNote:) withObject:aNote];
}
- (NSArray*)activeSessions {
return [syncServiceSessions allValues];
}
- (void)menuNeedsUpdate:(NSMenu *)menu {
[self _updateMenuWithCurrentStatus:menu];
}
- (void)_updateMenuWithCurrentStatus:(NSMenu*)aMenu {
if (IsSnowLeopardOrLater) {
[aMenu performSelector:@selector(removeAllItems)];
} else {
while ([aMenu numberOfItems])
[aMenu removeItemAtIndex:0];
}
//BUG: on Tiger this creates an extra item that appears at the _bottom_ of the pulldown,
//but on Leopard and above the first item is rightly used as the "title" of the button
[aMenu addItem:[NSMenuItem separatorItem]];
//for each service that NV can handle, add a section to the menu with information about its current session, if one exists
NSArray *svcs = [[self class] allServiceNames];
NSUInteger i = 0;
for (i=0; i<[svcs count]; i++) {
Class class = [[[self class] allServiceClasses] objectAtIndex:i];
NSString *serviceName = [svcs objectAtIndex:i];
BOOL isEnabled = [notationPrefs syncServiceIsEnabled:serviceName];
//"<Name>" (if disabled, "<Name>: Disabled")
NSMenuItem *serviceHeaderItem = [[[NSMenuItem alloc] initWithTitle: isEnabled ? [class localizedServiceTitle] :
[NSString stringWithFormat:NSLocalizedString(@"%@: Disabled", @"<Sync Service Name>: Disabled"), [class localizedServiceTitle]]
action:nil keyEquivalent:@""] autorelease];
[serviceHeaderItem setEnabled:NO];
[aMenu addItem:serviceHeaderItem];
id <SyncServiceSession> session = [syncServiceSessions objectForKey:serviceName];
if (session) {
NSArray *tasks = [[session activeTasks] allObjects];
if ([tasks count]) {
//one item per task
NSUInteger j = 0;
for (j=0; j<[tasks count]; j++) {
NSMenuItem *taskItem = [[[NSMenuItem alloc] initWithTitle:[[tasks objectAtIndex:j] statusText] action:NULL keyEquivalent:@""] autorelease];
[taskItem setEnabled:NO];
[aMenu addItem:taskItem];
}
} else {
//use the session-level status
NSMenuItem *sessionStatusItem = [[[NSMenuItem alloc] initWithTitle:[session statusText] action:NULL keyEquivalent:@""] autorelease];
[sessionStatusItem setEnabled:NO];
[aMenu addItem:sessionStatusItem];
}
//now for the ACTION items:
[aMenu addItem:[NSMenuItem separatorItem]];
//if running "stop"; otherwise, "sync":
if ([session isRunning]) {
NSMenuItem *stopItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Stop Synchronizing", nil) action:@selector(stop) keyEquivalent:@""] autorelease];
[stopItem setEnabled:YES];
[stopItem setTarget:session];
[aMenu addItem:stopItem];
} else {
NSMenuItem *syncItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Synchronize Now", nil)
action:@selector(startFetchingListForFullSyncManual) keyEquivalent:@""] autorelease];
[syncItem setEnabled:YES];
[syncItem setTarget:session];
[aMenu addItem:syncItem];
}
} else {
//can't provide any information other than enabled/disabled
//if enabled, a message that the user or password is missing
//if neither is missing, a generic error that ought never appear
NSDictionary *acctDict = [notationPrefs syncAccountForServiceName:serviceName];
NSMenuItem *badItem = nil;
if (![acctDict objectForKey:@"username"] || ![acctDict objectForKey:@"password"]) {
badItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Incorrect login and password", @"sync status menu msg")
action:nil keyEquivalent:@""] autorelease];
} else if (isEnabled) {
badItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Session could not be created", nil) action:nil keyEquivalent:@""] autorelease];
}
[badItem setEnabled:NO];
if (badItem) [aMenu addItem:badItem];
}
if (i < [svcs count] - 1) {
[aMenu addItem:[NSMenuItem separatorItem]];
}
}
}
- (NSMenu*)syncStatusMenu {
if (!statusMenu) {
statusMenu = [[NSMenu alloc] initWithTitle:@"Sync Status"];
[statusMenu setAutoenablesItems:NO];
[statusMenu setDelegate:self];
}
return statusMenu;
}
- (BOOL)hasRunningSessions {
NSArray *sessions = [syncServiceSessions allValues];
NSUInteger i = 0;
for (i=0; i<[sessions count]; i++) {
if ([[sessions objectAtIndex:i] isRunning]) return YES;
}
return NO;
}
- (BOOL)hasErrors {
NSArray *svcs = [[self class] allServiceNames];
NSUInteger i = 0;
for (i=0; i<[svcs count]; i++) {
NSString *serviceName = [svcs objectAtIndex:i];
if ([notationPrefs syncServiceIsEnabled:serviceName]) {
//only report errors for those services with which the user is expecting (or hoping) to sync
id <SyncServiceSession> session = [syncServiceSessions objectForKey:serviceName];
if (!session) return YES;
if (![session isRunning]) {
//report errors for only stopped sessions
if ([session lastError]) return YES;
}
}
}
return NO;
}
- (void)queueStatusNotification {
//send an alert telling people to check our -hasErrors and -hasRunningSessions methods
NSNotification *aNote = [NSNotification notificationWithName:SyncSessionsChangedVisibleStatusNotification object:self];
[[NSNotificationQueue defaultQueue] enqueueNotification:aNote postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
}
- (NSString*)changeCommittingErrorMessage {
return lastUncomittedChangeResultMessage;
}
- (void)invokeUncommmitedWaitCallbackIfNecessaryReturningError:(NSString*)errString {
if ([uncommittedWaitInvocations count]) {
[lastUncomittedChangeResultMessage autorelease];
lastUncomittedChangeResultMessage = [errString copy];
if ([errString length] || ![self hasRunningSessions]) {
//fail on the first occur that occurs; currently doesn't provide an opportunity for continuing to sync with other non-failed svcs
[uncommittedWaitInvocations makeObjectsPerformSelector:@selector(invoke)];
[uncommittedWaitInvocations removeAllObjects];
}
}
}
- (BOOL)waitForUncommitedChangesWithInvocation:(NSInvocation*)anInvocation {
// push any uncommitted notes for all sessions, so that those will then be running
// if we didn't have to push for any of the sessions AND none of the sessions are running, then return right away; there are no changes left to send
// syncDelegate invokes anInvocation when any currently running sessions have stopped and no sessions have any more uncommited notes
// it must call invokeUncommmitedWaitCallbackIfNecessary from -syncSession:didStopWithError:
NSAssert(anInvocation != nil, @"cannot wait without an ending invocation");
ComparableInvocation *cInvocation = [[[ComparableInvocation alloc] initWithInvocation: anInvocation] autorelease];
if ([uncommittedWaitInvocations containsObject:cInvocation]) {
NSLog(@"%@: already waiting for %@", NSStringFromSelector(_cmd), anInvocation);
return YES; //we're already waiting for this invocation
}
[lastUncomittedChangeResultMessage release];
lastUncomittedChangeResultMessage = nil;
BOOL willNeedToWait = NO;
NSArray *sessions = [syncServiceSessions allValues];
NSUInteger i = 0;
for (i=0; i<[sessions count]; i++) {
id <SyncServiceSession> session = [sessions objectAtIndex:i];
if ([session hasUnsyncedChanges]) {
//if the session has an error, the last reachability status is bad, and nothing is currently in progress, then skip pushing for it
if ([session reachabilityFailed] && [session lastError] && ![session isRunning]) {
NSLog(@"%@: skipped %@ due to assumed reachability status", NSStringFromSelector(_cmd), session);
continue;
}
if (!(![session pushSyncServiceChanges] && ![session isRunning])) {
willNeedToWait = YES;
if (!uncommittedWaitInvocations) uncommittedWaitInvocations = [[NSMutableSet alloc] initWithCapacity:1];
[uncommittedWaitInvocations addObject:cInvocation];
}
}
}
return willNeedToWait;
}
- (void)dealloc {
[self unregisterPowerChangeCallback];
[statusMenu release];
[syncServiceTimers release];
[syncServiceSessions release];
syncServiceSessions = nil;
[notationPrefs release];
[uncommittedWaitInvocations release];
uncommittedWaitInvocations = nil;
[lastUncomittedChangeResultMessage release];
[super dealloc];
}
@end