Updated the path subscriber as Mountain Lion seemed to have deprecated and broke FNSubscribeByPath. The new update checks if you are on 10.5 or later and if you are it will use the new FileSystem Events.
This commit is contained in:
parent
4cca0cf0ec
commit
0c07b8c44c
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
@interface MGMPathSubscriber (MGMPrivate)
|
@interface MGMPathSubscriber (MGMPrivate)
|
||||||
- (void)subscriptionChanged:(FNSubscriptionRef)theSubscription;
|
- (void)subscriptionChanged:(FNSubscriptionRef)theSubscription;
|
||||||
|
- (void)subscriptionFSChanged:(ConstFSEventStreamRef)theSubscription;
|
||||||
- (void)sendNotificationForPath:(NSString *)thePath;
|
- (void)sendNotificationForPath:(NSString *)thePath;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -17,10 +18,19 @@ static MGMPathSubscriber *MGMSharedPathSubscriber;
|
|||||||
NSString * const MGMSubscribedPathChangedNotification = @"MGMSubscribedPathChangedNotification";
|
NSString * const MGMSubscribedPathChangedNotification = @"MGMSubscribedPathChangedNotification";
|
||||||
|
|
||||||
void MGMPathSubscriptionChange(FNMessage theMessage, OptionBits theFlags, void *thePathSubscription, FNSubscriptionRef theSubscription) {
|
void MGMPathSubscriptionChange(FNMessage theMessage, OptionBits theFlags, void *thePathSubscription, FNSubscriptionRef theSubscription) {
|
||||||
if (theMessage==kFNDirectoryModifiedMessage)
|
if (theMessage==kFNDirectoryModifiedMessage)
|
||||||
[(MGMPathSubscriber *)thePathSubscription subscriptionChanged:theSubscription];
|
[(MGMPathSubscriber *)thePathSubscription subscriptionChanged:theSubscription];
|
||||||
else
|
else
|
||||||
NSLog(@"MGMPathSubscription: Received Unknown message: %d", (int)theMessage);
|
NSLog(@"MGMPathSubscription: Received Unknown message: %u", theMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MGMPathSubscriptionFSChange(ConstFSEventStreamRef streamRef, void *thePathSubscription, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
|
||||||
|
for (size_t i=0; i<numEvents; i++) {
|
||||||
|
if (eventFlags[i] & (kFSEventStreamEventFlagItemCreated | kFSEventStreamEventFlagItemRemoved | kFSEventStreamEventFlagItemRenamed)) {
|
||||||
|
[(MGMPathSubscriber *)thePathSubscription subscriptionFSChanged:streamRef];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation MGMPathSubscriber
|
@implementation MGMPathSubscriber
|
||||||
@ -52,23 +62,59 @@ void MGMPathSubscriptionChange(FNMessage theMessage, OptionBits theFlags, void *
|
|||||||
delegate = theDelegate;
|
delegate = theDelegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (int)OSMajorVersion {
|
||||||
|
SInt32 majorVersion;
|
||||||
|
if (Gestalt(gestaltSystemVersionMajor, &majorVersion)==noErr) {
|
||||||
|
return (int)majorVersion;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
- (int)OSMinorVersion {
|
||||||
|
SInt32 minorVersion;
|
||||||
|
if (Gestalt(gestaltSystemVersionMinor, &minorVersion)==noErr) {
|
||||||
|
return (int)minorVersion;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)addPath:(NSString *)thePath {
|
- (void)addPath:(NSString *)thePath {
|
||||||
NSValue *value = [subscriptions objectForKey:thePath];
|
NSValue *value = [subscriptions objectForKey:thePath];
|
||||||
if (value!=nil)
|
if (value!=nil)
|
||||||
return;
|
return;
|
||||||
FNSubscriptionRef subscription = NULL;
|
FSEventStreamContext context = {0, self, NULL, NULL, NULL};
|
||||||
OSStatus error = FNSubscribeByPath((UInt8 *)[thePath fileSystemRepresentation], subscriptionUPP, self, kFNNotifyInBackground, &subscription);
|
if ([self OSMajorVersion]==10 && [self OSMinorVersion]>=5) {
|
||||||
if (error!=noErr) {
|
FSEventStreamRef stream = FSEventStreamCreate(NULL, &MGMPathSubscriptionFSChange, &context, (CFArrayRef)[NSArray arrayWithObject:thePath], kFSEventStreamEventIdSinceNow, 0.5, kFSEventStreamCreateFlagNone);
|
||||||
NSLog(@"MGMPathSubscription: Unable to subscribe to %@ due to the error %ld", thePath, (long)error);
|
if (stream==NULL) {
|
||||||
return;
|
NSLog(@"MGMPathSubscription: Unable to subscribe to %@", thePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||||
|
FSEventStreamStart(stream);
|
||||||
|
[subscriptions setObject:[NSValue valueWithPointer:stream] forKey:thePath];
|
||||||
|
} else {
|
||||||
|
FNSubscriptionRef subscription = NULL;
|
||||||
|
OSStatus error = FNSubscribeByPath((UInt8 *)[thePath fileSystemRepresentation], subscriptionUPP, self, kFNNotifyInBackground, &subscription);
|
||||||
|
if (error!=noErr) {
|
||||||
|
NSLog(@"MGMPathSubscription: Unable to subscribe to %@ due to the error %ld", thePath, (long)error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[subscriptions setObject:[NSValue valueWithPointer:subscription] forKey:thePath];
|
||||||
}
|
}
|
||||||
[subscriptions setObject:[NSValue valueWithPointer:subscription] forKey:thePath];
|
|
||||||
}
|
}
|
||||||
- (void)removePath:(NSString *)thePath {
|
- (void)removePath:(NSString *)thePath {
|
||||||
NSValue *value = [subscriptions objectForKey:thePath];
|
NSValue *value = [subscriptions objectForKey:thePath];
|
||||||
if (value!=nil) {
|
if (value!=nil) {
|
||||||
FNUnsubscribe([value pointerValue]);
|
if ([self OSMajorVersion]==10 && [self OSMinorVersion]>=5) {
|
||||||
[subscriptions removeObjectForKey:thePath];
|
FSEventStreamRef stream = [value pointerValue];
|
||||||
|
FSEventStreamStop(stream);
|
||||||
|
FSEventStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||||
|
FSEventStreamInvalidate(stream);
|
||||||
|
FSEventStreamRelease(stream);
|
||||||
|
[subscriptions removeObjectForKey:thePath];
|
||||||
|
} else {
|
||||||
|
FNUnsubscribe([value pointerValue]);
|
||||||
|
[subscriptions removeObjectForKey:thePath];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
- (void)removeAllPaths {
|
- (void)removeAllPaths {
|
||||||
@ -93,6 +139,16 @@ void MGMPathSubscriptionChange(FNMessage theMessage, OptionBits theFlags, void *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
- (void)subscriptionFSChanged:(ConstFSEventStreamRef)theSubscription {
|
||||||
|
NSArray *keys = [subscriptions allKeysForObject:[NSValue valueWithPointer:theSubscription]];
|
||||||
|
if ([keys count]>=1) {
|
||||||
|
NSString *path = [keys objectAtIndex:0];
|
||||||
|
if (![notificationsSending containsObject:path]) {
|
||||||
|
[notificationsSending addObject:path];
|
||||||
|
[self performSelector:@selector(sendNotificationForPath:) withObject:path afterDelay:0.5];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
- (void)sendNotificationForPath:(NSString *)thePath {
|
- (void)sendNotificationForPath:(NSString *)thePath {
|
||||||
[[NSNotificationCenter defaultCenter] postNotificationName:MGMSubscribedPathChangedNotification object:thePath];
|
[[NSNotificationCenter defaultCenter] postNotificationName:MGMSubscribedPathChangedNotification object:thePath];
|
||||||
if ([delegate respondsToSelector:@selector(subscribedPathChanged:)]) [delegate subscribedPathChanged:thePath];
|
if ([delegate respondsToSelector:@selector(subscribedPathChanged:)]) [delegate subscribedPathChanged:thePath];
|
||||||
|
Loading…
Reference in New Issue
Block a user