Use UKKQueue for auto import which gets rid of polling and makes the process instantaneous.

This commit is contained in:
Mitchell Livingston 2006-09-05 04:28:07 +00:00
parent 4fdc2f4c91
commit bc14e9e56c
13 changed files with 1264 additions and 46 deletions

View File

@ -127,6 +127,14 @@
A28DBADC0A33C1D800F4B4A7 /* ActionButton.png in Resources */ = {isa = PBXBuildFile; fileRef = A28DBADB0A33C1D800F4B4A7 /* ActionButton.png */; };
A2912C540A2956E80097A0CA /* PrefsWindow.nib in Resources */ = {isa = PBXBuildFile; fileRef = A2912C520A2956E80097A0CA /* PrefsWindow.nib */; };
A29597350A72A9E10057248B /* StatusBarBackground.png in Resources */ = {isa = PBXBuildFile; fileRef = A29597340A72A9E10057248B /* StatusBarBackground.png */; };
A2A3065B0AAD24A80049E2AC /* UKFileWatcher.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = A2A306530AAD24A80049E2AC /* UKFileWatcher.h */; };
A2A3065C0AAD24A80049E2AC /* UKFileWatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = A2A306540AAD24A80049E2AC /* UKFileWatcher.m */; };
A2A3065D0AAD24A80049E2AC /* UKFNSubscribeFileWatcher.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = A2A306550AAD24A80049E2AC /* UKFNSubscribeFileWatcher.h */; };
A2A3065E0AAD24A80049E2AC /* UKFNSubscribeFileWatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = A2A306560AAD24A80049E2AC /* UKFNSubscribeFileWatcher.m */; };
A2A3065F0AAD24A80049E2AC /* UKKQueue.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = A2A306570AAD24A80049E2AC /* UKKQueue.h */; };
A2A306600AAD24A80049E2AC /* UKKQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = A2A306580AAD24A80049E2AC /* UKKQueue.m */; };
A2A306610AAD24A80049E2AC /* UKMainThreadProxy.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = A2A306590AAD24A80049E2AC /* UKMainThreadProxy.h */; };
A2A306620AAD24A80049E2AC /* UKMainThreadProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A2A3065A0AAD24A80049E2AC /* UKMainThreadProxy.m */; };
A2AF1C390A3D0F6200F1575D /* FileTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = A2AF1C370A3D0F6200F1575D /* FileTableView.m */; };
A2BD40070A09BBEA008CCE96 /* bencode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D1838B709DEC0430047D688 /* bencode.h */; };
A2C655650A04FEDC00E9FD82 /* BottomBorder.png in Resources */ = {isa = PBXBuildFile; fileRef = A2C655640A04FEDC00E9FD82 /* BottomBorder.png */; };
@ -165,6 +173,10 @@
A261F1E40A69A1B10002815A /* Growl.framework in CopyFiles */,
A24F19210A3A796800C9C145 /* Sparkle.framework in CopyFiles */,
A256588C0A9A695400E8A03B /* MessageWindowController.h in CopyFiles */,
A2A3065B0AAD24A80049E2AC /* UKFileWatcher.h in CopyFiles */,
A2A3065D0AAD24A80049E2AC /* UKFNSubscribeFileWatcher.h in CopyFiles */,
A2A3065F0AAD24A80049E2AC /* UKKQueue.h in CopyFiles */,
A2A306610AAD24A80049E2AC /* UKMainThreadProxy.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -336,6 +348,14 @@
A28DBADB0A33C1D800F4B4A7 /* ActionButton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ActionButton.png; path = macosx/Images/ActionButton.png; sourceTree = "<group>"; };
A2912C530A2956E80097A0CA /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = macosx/English.lproj/PrefsWindow.nib; sourceTree = "<group>"; };
A29597340A72A9E10057248B /* StatusBarBackground.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = StatusBarBackground.png; path = macosx/Images/StatusBarBackground.png; sourceTree = "<group>"; };
A2A306530AAD24A80049E2AC /* UKFileWatcher.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = UKFileWatcher.h; path = macosx/UKKQueue/UKFileWatcher.h; sourceTree = "<group>"; };
A2A306540AAD24A80049E2AC /* UKFileWatcher.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = UKFileWatcher.m; path = macosx/UKKQueue/UKFileWatcher.m; sourceTree = "<group>"; };
A2A306550AAD24A80049E2AC /* UKFNSubscribeFileWatcher.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = UKFNSubscribeFileWatcher.h; path = macosx/UKKQueue/UKFNSubscribeFileWatcher.h; sourceTree = "<group>"; };
A2A306560AAD24A80049E2AC /* UKFNSubscribeFileWatcher.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = UKFNSubscribeFileWatcher.m; path = macosx/UKKQueue/UKFNSubscribeFileWatcher.m; sourceTree = "<group>"; };
A2A306570AAD24A80049E2AC /* UKKQueue.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = UKKQueue.h; path = macosx/UKKQueue/UKKQueue.h; sourceTree = "<group>"; };
A2A306580AAD24A80049E2AC /* UKKQueue.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = UKKQueue.m; path = macosx/UKKQueue/UKKQueue.m; sourceTree = "<group>"; };
A2A306590AAD24A80049E2AC /* UKMainThreadProxy.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = UKMainThreadProxy.h; path = macosx/UKKQueue/UKMainThreadProxy.h; sourceTree = "<group>"; };
A2A3065A0AAD24A80049E2AC /* UKMainThreadProxy.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = UKMainThreadProxy.m; path = macosx/UKKQueue/UKMainThreadProxy.m; sourceTree = "<group>"; };
A2A84AD20A04FCDC00C898D4 /* BottomBorder.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = BottomBorder.png; path = macosx/Images/BottomBorder.png; sourceTree = "<group>"; };
A2AF1C360A3D0F6200F1575D /* FileTableView.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = FileTableView.h; path = macosx/FileTableView.h; sourceTree = "<group>"; };
A2AF1C370A3D0F6200F1575D /* FileTableView.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = FileTableView.m; path = macosx/FileTableView.m; sourceTree = "<group>"; };
@ -383,6 +403,14 @@
080E96DDFE201D6D7F000001 /* Sources */ = {
isa = PBXGroup;
children = (
A2A306530AAD24A80049E2AC /* UKFileWatcher.h */,
A2A306540AAD24A80049E2AC /* UKFileWatcher.m */,
A2A306550AAD24A80049E2AC /* UKFNSubscribeFileWatcher.h */,
A2A306560AAD24A80049E2AC /* UKFNSubscribeFileWatcher.m */,
A2A306570AAD24A80049E2AC /* UKKQueue.h */,
A2A306580AAD24A80049E2AC /* UKKQueue.m */,
A2A306590AAD24A80049E2AC /* UKMainThreadProxy.h */,
A2A3065A0AAD24A80049E2AC /* UKMainThreadProxy.m */,
A256588A0A9A695400E8A03B /* MessageWindowController.h */,
A256588B0A9A695400E8A03B /* MessageWindowController.m */,
A200B8390A2263BA007BBB1E /* InfoWindowController.h */,
@ -836,6 +864,10 @@
A2710E770A86796000CE4F7D /* PrefsWindow.m in Sources */,
A256588D0A9A695400E8A03B /* MessageWindowController.m in Sources */,
A23BBEB70A9BBE77003F7274 /* BarButton.m in Sources */,
A2A3065C0AAD24A80049E2AC /* UKFileWatcher.m in Sources */,
A2A3065E0AAD24A80049E2AC /* UKFNSubscribeFileWatcher.m in Sources */,
A2A306600AAD24A80049E2AC /* UKKQueue.m in Sources */,
A2A306620AAD24A80049E2AC /* UKMainThreadProxy.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -89,9 +89,6 @@
io_connect_t fRootPort;
NSTimer * fTimer;
NSTimer * fAutoImportTimer;
NSMutableArray * fAutoImportedNames;
BOOL fUpdateInProgress;
Badger * fBadger;
}
@ -175,8 +172,7 @@
- (void) reloadInspectorSettings: (NSNotification *) notification;
- (void) checkAutoImportDirectory: (NSTimer *) timer;
- (void) autoImportChange: (NSNotification *) notification;
- (void) checkAutoImportDirectory;
- (void) sleepCallBack: (natural_t) messageType argument: (void *) messageArgument;

View File

@ -29,6 +29,7 @@
#import "TorrentCell.h"
#import "TorrentTableView.h"
#import "StringAdditions.h"
#import "UKKQueue.h"
#import <Sparkle/Sparkle.h>
@ -90,9 +91,9 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
fBadger = [[Badger alloc] init];
fAutoImportedNames = [[NSMutableArray alloc] init];
[GrowlApplicationBridge setGrowlDelegate: self];
[[UKKQueue sharedFileWatcher] setDelegate: self];
}
return self;
}
@ -111,7 +112,6 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
[fSortType release];
[fFilterType release];
[fAutoImportedNames release];
tr_close(fLib);
[super dealloc];
@ -317,7 +317,7 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
[nc addObserver: self selector: @selector(autoSpeedLimitChange:)
name: @"AutoSpeedLimitChange" object: nil];
[nc addObserver: self selector: @selector(autoImportChange:)
[nc addObserver: self selector: @selector(checkAutoImportDirectory)
name: @"AutoImportSettingChange" object: nil];
[nc addObserver: self selector: @selector(setWindowSizeToFit)
@ -366,11 +366,6 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
[self autoSpeedLimitChange: nil];
fSpeedLimitTimer = [NSTimer scheduledTimerWithTimeInterval: AUTO_SPEED_LIMIT_SECONDS target: self
selector: @selector(autoSpeedLimit:) userInfo: nil repeats: YES];
//timer to check for auto import every 15 seconds, must do after everything else is set up
fAutoImportTimer = [NSTimer scheduledTimerWithTimeInterval: AUTO_IMPORT_SECONDS target: self
selector: @selector(checkAutoImportDirectory:) userInfo: nil repeats: YES];
[[NSRunLoop currentRunLoop] addTimer: fAutoImportTimer forMode: NSDefaultRunLoopMode];
}
- (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows
@ -419,7 +414,6 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
- (void) applicationWillTerminate: (NSNotification *) notification
{
//stop timers
[fAutoImportTimer invalidate];
[fSpeedLimitTimer invalidate];
[fTimer invalidate];
@ -1533,27 +1527,24 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
[fInfoController updateInfoStatsAndSettings];
}
- (void) checkAutoImportDirectory: (NSTimer *) timer
-(void) watcher: (id<UKFileWatcher>) kq receivedNotification: (NSString*) nm forPath: (NSString*) fpath
{
if ([nm isEqualToString: UKFileWatcherWriteNotification])
[self checkAutoImportDirectory];
}
- (void) checkAutoImportDirectory
{
if (![fDefaults boolForKey: @"AutoImport"])
return;
NSString * path = [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath];
//if folder cannot be found or the contents hasn't changed simply give up
NSArray * allFileNames;
if (!(allFileNames = [[NSFileManager defaultManager] directoryContentsAtPath: path])
|| [allFileNames isEqualToArray: fAutoImportedNames])
NSArray * importedNames;
if (!(importedNames = [[NSFileManager defaultManager] directoryContentsAtPath: path]))
return;
//try to add files that haven't already been added
NSMutableArray * newFileNames = [NSMutableArray arrayWithArray: allFileNames];
[newFileNames removeObjectsInArray: fAutoImportedNames];
//save the current list of files
[fAutoImportedNames setArray: allFileNames];
NSEnumerator * enumerator = [newFileNames objectEnumerator];
NSEnumerator * enumerator = [importedNames objectEnumerator];
NSString * file;
unsigned oldCount;
while ((file = [enumerator nextObject]))
@ -1569,12 +1560,6 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy
}
}
- (void) autoImportChange: (NSNotification *) notification
{
[fAutoImportedNames removeAllObjects];
[self checkAutoImportDirectory: nil];
}
- (int) numberOfRowsInTableView: (NSTableView *) t
{
return [fDisplayedTorrents count];

View File

@ -24,6 +24,7 @@
#import "PrefsController.h"
#import "StringAdditions.h"
#import "UKKQueue.h"
#define MIN_PORT 1
#define MAX_PORT 65535
@ -113,6 +114,9 @@
[fAutoImportCheck setState: autoImport];
[fImportFolderPopUp setEnabled: autoImport];
if (autoImport)
[[UKKQueue sharedFileWatcher] addPath: fImportFolder];
//set auto size
[fAutoSizeCheck setState: [fDefaults boolForKey: @"AutoSize"]];
@ -750,6 +754,11 @@
[fDefaults setBool: state forKey: @"AutoImport"];
[fImportFolderPopUp setEnabled: state];
if (state == NSOnState)
[[UKKQueue sharedFileWatcher] addPath: fImportFolder];
else
[[UKKQueue sharedFileWatcher] removePathFromQueue: fImportFolder];
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
}
@ -866,18 +875,23 @@
- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
if (code == NSOKButton)
{
[fImportFolder release];
fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
[fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
[self updateImportPopUp];
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
}
[fImportFolderPopUp selectItemAtIndex: 0];
if (code == NSOKButton)
{
UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
[sharedQueue removePathFromQueue: fImportFolder];
[fImportFolder release];
fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
[fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
[self updateImportPopUp];
[sharedQueue addPath: fImportFolder];
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
}
[fImportFolderPopUp selectItemAtIndex: 0];
}
- (void) updateImportPopUp

View File

@ -0,0 +1,49 @@
/* =============================================================================
FILE: UKFNSubscribeFileWatcher.m
PROJECT: Filie
COPYRIGHT: (c) 2005 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Commented, added singleton.
2005-03-02 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
#import "UKFileWatcher.h"
#import <Carbon/Carbon.h>
/*
NOTE: FNSubscribe has a built-in delay: If your application is in the
background while the changes happen, all notifications will be queued up
and sent to your app at once the moment it is brought to front again. If
your app really needs to do live updates in the background, use a KQueue
instead.
*/
// -----------------------------------------------------------------------------
// Class declaration:
// -----------------------------------------------------------------------------
@interface UKFNSubscribeFileWatcher : NSObject <UKFileWatcher>
{
id delegate; // Delegate must respond to UKFileWatcherDelegate protocol.
NSMutableDictionary* subscriptions; // List of FNSubscription pointers in NSValues, with the pathnames as their keys.
}
+(id) sharedFileWatcher;
// UKFileWatcher defines the methods: addPath: removePath: and delegate accessors.
// Private:
-(void) sendDelegateMessage: (FNMessage)message forSubscription: (FNSubscriptionRef)subscription;
@end

View File

@ -0,0 +1,201 @@
/* =============================================================================
FILE: UKFNSubscribeFileWatcher.m
PROJECT: Filie
COPYRIGHT: (c) 2005 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Commented, added singleton, added notifications.
2005-03-02 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKFNSubscribeFileWatcher.h"
#import <Carbon/Carbon.h>
// -----------------------------------------------------------------------------
// Prototypes:
// -----------------------------------------------------------------------------
void UKFileSubscriptionProc(FNMessage message, OptionBits flags, void *refcon, FNSubscriptionRef subscription);
@implementation UKFNSubscribeFileWatcher
// -----------------------------------------------------------------------------
// sharedFileWatcher:
// Singleton accessor.
// -----------------------------------------------------------------------------
+(id) sharedFileWatcher
{
static UKFNSubscribeFileWatcher* sSharedFileWatcher = nil;
if( !sSharedFileWatcher )
sSharedFileWatcher = [[UKFNSubscribeFileWatcher alloc] init]; // This is a singleton, and thus an intentional "leak".
return sSharedFileWatcher;
}
// -----------------------------------------------------------------------------
// * CONSTRUCTOR:
// -----------------------------------------------------------------------------
-(id) init
{
self = [super init];
if( !self )
return nil;
subscriptions = [[NSMutableDictionary alloc] init];
return self;
}
// -----------------------------------------------------------------------------
// * DESTRUCTOR:
// -----------------------------------------------------------------------------
-(void) dealloc
{
NSEnumerator* enny = [subscriptions objectEnumerator];
NSValue* subValue = nil;
while( (subValue = [enny nextObject]) )
{
FNSubscriptionRef subscription = [subValue pointerValue];
FNUnsubscribe( subscription );
}
[subscriptions release];
[super dealloc];
}
// -----------------------------------------------------------------------------
// addPath:
// Start watching the object at the specified path. This only sends write
// notifications for all changes, as FNSubscribe doesn't tell what actually
// changed about our folder.
// -----------------------------------------------------------------------------
-(void) addPath: (NSString*)path
{
OSStatus err = noErr;
static FNSubscriptionUPP subscriptionUPP = NULL;
FNSubscriptionRef subscription = NULL;
if( !subscriptionUPP )
subscriptionUPP = NewFNSubscriptionUPP( UKFileSubscriptionProc );
err = FNSubscribeByPath( (UInt8*) [path fileSystemRepresentation], subscriptionUPP, (void*)self,
kNilOptions, &subscription );
if( err != noErr )
{
NSLog( @"UKFNSubscribeFileWatcher addPath: %@ failed due to error ID=%ld.", path, err );
return;
}
[subscriptions setObject: [NSValue valueWithPointer: subscription] forKey: path];
}
// -----------------------------------------------------------------------------
// removePath:
// Stop watching the object at the specified path.
// -----------------------------------------------------------------------------
-(void) removePath: (NSString*)path
{
NSValue* subValue = nil;
@synchronized( self )
{
subValue = [[[subscriptions objectForKey: path] retain] autorelease];
[subscriptions removeObjectForKey: path];
}
if( subValue )
{
FNSubscriptionRef subscription = [subValue pointerValue];
FNUnsubscribe( subscription );
}
}
// -----------------------------------------------------------------------------
// sendDelegateMessage:forSubscription:
// Bottleneck for change notifications. This is called by our callback
// function to actually inform the delegate and send out notifications.
//
// This *only* sends out write notifications, as FNSubscribe doesn't tell
// what changed about our folder.
// -----------------------------------------------------------------------------
-(void) sendDelegateMessage: (FNMessage)message forSubscription: (FNSubscriptionRef)subscription
{
NSValue* subValue = [NSValue valueWithPointer: subscription];
NSString* path = [[subscriptions allKeysForObject: subValue] objectAtIndex: 0];
[[[NSWorkspace sharedWorkspace] notificationCenter] postNotificationName: UKFileWatcherWriteNotification
object: self
userInfo: [NSDictionary dictionaryWithObjectsAndKeys: path, @"path", nil]];
[delegate watcher: self receivedNotification: UKFileWatcherWriteNotification forPath: path];
//NSLog( @"UKFNSubscribeFileWatcher noticed change to %@", path ); // DEBUG ONLY!
}
// -----------------------------------------------------------------------------
// delegate:
// Accessor for file watcher delegate.
// -----------------------------------------------------------------------------
-(id) delegate
{
return delegate;
}
// -----------------------------------------------------------------------------
// setDelegate:
// Mutator for file watcher delegate.
// -----------------------------------------------------------------------------
-(void) setDelegate: (id)newDelegate
{
delegate = newDelegate;
}
@end
// -----------------------------------------------------------------------------
// UKFileSubscriptionProc:
// Callback function we hand to Carbon so it can tell us when something
// changed about our watched folders. We set the refcon to a pointer to
// our object. This simply extracts the object and hands the info off to
// sendDelegateMessage:forSubscription: which does the actual work.
// -----------------------------------------------------------------------------
void UKFileSubscriptionProc( FNMessage message, OptionBits flags, void *refcon, FNSubscriptionRef subscription )
{
UKFNSubscribeFileWatcher* obj = (UKFNSubscribeFileWatcher*) refcon;
if( message == kFNDirectoryModifiedMessage ) // No others exist as of 10.4
[obj sendDelegateMessage: message forSubscription: subscription];
else
NSLog( @"UKFileSubscriptionProc: Unknown message %d", message );
}

View File

@ -0,0 +1,62 @@
/* =============================================================================
FILE: UKFileWatcher.h
PROJECT: Filie
COPYRIGHT: (c) 2005 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Moved notification constants to .m file.
2005-02-25 UK Created.
========================================================================== */
/*
This is a protocol that file change notification classes should adopt.
That way, no matter whether you use Carbon's FNNotify/FNSubscribe, BSD's
kqueue or whatever, the object being notified can react to change
notifications the same way, and you can easily swap one out for the other
to cater to different OS versions, target volumes etc.
*/
// -----------------------------------------------------------------------------
// Protocol:
// -----------------------------------------------------------------------------
@protocol UKFileWatcher
// +(id) sharedFileWatcher; // Singleton accessor. Not officially part of the protocol, but use this name if you provide a singleton.
-(void) addPath: (NSString*)path;
-(void) removePath: (NSString*)path;
-(id) delegate;
-(void) setDelegate: (id)newDelegate;
@end
// -----------------------------------------------------------------------------
// Methods delegates need to provide:
// -----------------------------------------------------------------------------
@interface NSObject (UKFileWatcherDelegate)
-(void) watcher: (id<UKFileWatcher>)kq receivedNotification: (NSString*)nm forPath: (NSString*)fpath;
@end
// Notifications this sends:
/* object = the file watcher object
userInfo.path = file path watched
These notifications are sent via the NSWorkspace notification center */
extern NSString* UKFileWatcherRenameNotification;
extern NSString* UKFileWatcherWriteNotification;
extern NSString* UKFileWatcherDeleteNotification;
extern NSString* UKFileWatcherAttributeChangeNotification;
extern NSString* UKFileWatcherSizeIncreaseNotification;
extern NSString* UKFileWatcherLinkCountChangeNotification;
extern NSString* UKFileWatcherAccessRevocationNotification;

View File

@ -0,0 +1,38 @@
/* =============================================================================
FILE: UKKQueue.m
PROJECT: Filie
COPYRIGHT: (c) 2005-06 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Created, moved notification constants here as exportable
symbols.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
#import "UKFileWatcher.h"
// -----------------------------------------------------------------------------
// Constants:
// -----------------------------------------------------------------------------
// Do not rely on the actual contents of these constants. They will eventually
// be changed to be more generic and less KQueue-specific.
NSString* UKFileWatcherRenameNotification = @"UKKQueueFileRenamedNotification";
NSString* UKFileWatcherWriteNotification = @"UKKQueueFileWrittenToNotification";
NSString* UKFileWatcherDeleteNotification = @"UKKQueueFileDeletedNotification";
NSString* UKFileWatcherAttributeChangeNotification = @"UKKQueueFileAttributesChangedNotification";
NSString* UKFileWatcherSizeIncreaseNotification = @"UKKQueueFileSizeIncreasedNotification";
NSString* UKFileWatcherLinkCountChangeNotification = @"UKKQueueFileLinkCountChangedNotification";
NSString* UKFileWatcherAccessRevocationNotification = @"UKKQueueFileAccessRevocationNotification";

View File

@ -0,0 +1,32 @@
UKKQUEUE
--------
A wrapper class around the kqueue file change notification mechanism.
Simply create a UKKQueue (or use the singleton), add a few paths to it and listen to the change notifications via NSWorkspace's notification center.
LICENSE:
(c) 2003-06 by M. Uli Kusterer. You may redistribute, modify, use in
commercial products free of charge, however distributing modified copies
requires that you clearly mark them as having been modified by you, while
maintaining the original markings and copyrights. I don't like getting bug
reports about code I wasn't involved in.
I'd also appreciate if you gave credit in your app's about screen or a similar
place. A simple "Thanks to M. Uli Kusterer" is quite sufficient.
Also, I rarely turn down any postcards, gifts, complementary copies of
applications etc.
REVISION HISTORY:
0.1 - Initial release.
0.2 - Now calls delegate on main thread using UKMainThreadProxy, and checks retain count to make sure the object is released even when the thread is still holding on to it. Equivalent to SVN revision 79.
0.3 - Now adopts UKFileWatcher protocol to allow swapping out a kqueue for another scheme easily. Uses O_EVONLY instead of O_RDONLY to open the file without preventing it from being deleted or its drive ejected.
0.4 - Now includes UKFNSubscribeFileWatcher, and closes the kqueue file descriptor in a separate thread (thanks to Dominic Yu for the suggestion!) so you don't have to wait for close() to time out.
0.5 - Turns off all deprecated features. Changes the notifications to make it possible to subscribe to them more selectively. Changes notification constants to be safer for apps that expose KQueue to their plugins. FNSubscribeFileWatcher now also sends notifications (sorry, "write" only).
CONTACT:
Get the newest version at http://www.zathras.de
E-Mail me at witness (at) zathras (dot) de or witness (dot) of (dot) teachtext (at) gmx (dot) net

122
macosx/UKKQueue/UKKQueue.h Normal file
View File

@ -0,0 +1,122 @@
/* =============================================================================
FILE: UKKQueue.h
PROJECT: Filie
COPYRIGHT: (c) 2003 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Clarified license, streamlined UKFileWatcher stuff,
Changed notifications to be useful and turned off by
default some deprecated stuff.
2003-12-21 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import <Foundation/Foundation.h>
#include <sys/types.h>
#include <sys/event.h>
#import "UKFileWatcher.h"
// -----------------------------------------------------------------------------
// Constants:
// -----------------------------------------------------------------------------
// Backwards compatibility constants. Don't rely on code commented out with these constants, because it may be deleted in a future version.
#ifndef UKKQUEUE_BACKWARDS_COMPATIBLE
#define UKKQUEUE_BACKWARDS_COMPATIBLE 0 // 1 to send old-style kqueue:receivedNotification:forFile: messages to objects that accept them.
#endif
#ifndef UKKQUEUE_SEND_STUPID_NOTIFICATIONS
#define UKKQUEUE_SEND_STUPID_NOTIFICATIONS 0 // 1 to send old-style notifications that have the path as the object and no userInfo dictionary.
#endif
#ifndef UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME
#define UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME 0 // 1 to allow use of sharedQueue instead of sharedFileWatcher.
#endif
#ifndef UKKQUEUE_OLD_NOTIFICATION_NAMES
#define UKKQUEUE_OLD_NOTIFICATION_NAMES 0 // 1 to allow use of old KQueue-style notification names instead of the new more generic ones in UKFileWatcher.
#endif
// Flags for notifyingAbout:
#define UKKQueueNotifyAboutRename NOTE_RENAME // Item was renamed.
#define UKKQueueNotifyAboutWrite NOTE_WRITE // Item contents changed (also folder contents changed).
#define UKKQueueNotifyAboutDelete NOTE_DELETE // item was removed.
#define UKKQueueNotifyAboutAttributeChange NOTE_ATTRIB // Item attributes changed.
#define UKKQueueNotifyAboutSizeIncrease NOTE_EXTEND // Item size increased.
#define UKKQueueNotifyAboutLinkCountChanged NOTE_LINK // Item's link count changed.
#define UKKQueueNotifyAboutAccessRevocation NOTE_REVOKE // Access to item was revoked.
// Notifications this sends:
// (see UKFileWatcher)
// Old names: *deprecated*
#if UKKQUEUE_OLD_NOTIFICATION_NAMES
#define UKKQueueFileRenamedNotification UKFileWatcherRenameNotification
#define UKKQueueFileWrittenToNotification UKFileWatcherWriteNotification
#define UKKQueueFileDeletedNotification UKFileWatcherDeleteNotification
#define UKKQueueFileAttributesChangedNotification UKFileWatcherAttributeChangeNotification
#define UKKQueueFileSizeIncreasedNotification UKFileWatcherSizeIncreaseNotification
#define UKKQueueFileLinkCountChangedNotification UKFileWatcherLinkCountChangeNotification
#define UKKQueueFileAccessRevocationNotification UKFileWatcherAccessRevocationNotification
#endif
// -----------------------------------------------------------------------------
// UKKQueue:
// -----------------------------------------------------------------------------
@interface UKKQueue : NSObject <UKFileWatcher>
{
int queueFD; // The actual queue ID (Unix file descriptor).
NSMutableArray* watchedPaths; // List of NSStrings containing the paths we're watching.
NSMutableArray* watchedFDs; // List of NSNumbers containing the file descriptors we're watching.
id delegate; // Gets messages about changes instead of notification center, if specified.
id delegateProxy; // Proxy object to which we send messages so they reach delegate on the main thread.
BOOL alwaysNotify; // Send notifications even if we have a delegate? Defaults to NO.
BOOL keepThreadRunning; // Termination criterion of our thread.
}
+(id) sharedFileWatcher; // Returns a singleton, a shared kqueue object Handy if you're subscribing to the notifications. Use this, or just create separate objects using alloc/init. Whatever floats your boat.
-(int) queueFD; // I know you unix geeks want this...
// High-level file watching: (use UKFileWatcher protocol methods instead, where possible!)
-(void) addPathToQueue: (NSString*)path;
-(void) addPathToQueue: (NSString*)path notifyingAbout: (u_int)fflags;
-(void) removePathFromQueue: (NSString*)path;
-(id) delegate;
-(void) setDelegate: (id)newDelegate;
-(BOOL) alwaysNotify;
-(void) setAlwaysNotify: (BOOL)n;
#if UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME
+(UKKQueue*) sharedQueue;
#endif
// private:
-(void) watcherThread: (id)sender;
-(void) postNotification: (NSString*)nm forFile: (NSString*)fp; // Message-posting bottleneck.
@end
// -----------------------------------------------------------------------------
// Methods delegates need to provide:
// * DEPRECATED * use UKFileWatcher delegate methods instead!
// -----------------------------------------------------------------------------
@interface NSObject (UKKQueueDelegate)
-(void) kqueue: (UKKQueue*)kq receivedNotification: (NSString*)nm forFile: (NSString*)fpath;
@end

480
macosx/UKKQueue/UKKQueue.m Normal file
View File

@ -0,0 +1,480 @@
/* =============================================================================
FILE: UKKQueue.m
PROJECT: Filie
COPYRIGHT: (c) 2003 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Clarified license, streamlined UKFileWatcher stuff,
Changed notifications to be useful and turned off by
default some deprecated stuff.
2004-12-28 UK Several threading fixes.
2003-12-21 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKKQueue.h"
#import "UKMainThreadProxy.h"
#import <unistd.h>
#import <fcntl.h>
// -----------------------------------------------------------------------------
// Macros:
// -----------------------------------------------------------------------------
// @synchronized isn't available prior to 10.3, so we use a typedef so
// this class is thread-safe on Panther but still compiles on older OSs.
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
#define AT_SYNCHRONIZED(n) @synchronized(n)
#else
#define AT_SYNCHRONIZED(n)
#endif
// -----------------------------------------------------------------------------
// Globals:
// -----------------------------------------------------------------------------
static UKKQueue * gUKKQueueSharedQueueSingleton = nil;
@implementation UKKQueue
// Deprecated:
#if UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME
+(UKKQueue*) sharedQueue
{
return [self sharedFileWatcher];
}
#endif
// -----------------------------------------------------------------------------
// sharedQueue:
// Returns a singleton queue object. In many apps (especially those that
// subscribe to the notifications) there will only be one kqueue instance,
// and in that case you can use this.
//
// For all other cases, feel free to create additional instances to use
// independently.
//
// REVISIONS:
// 2006-03-13 UK Renamed from sharedQueue.
// 2005-07-02 UK Created.
// -----------------------------------------------------------------------------
+(id) sharedFileWatcher
{
AT_SYNCHRONIZED( self )
{
if( !gUKKQueueSharedQueueSingleton )
gUKKQueueSharedQueueSingleton = [[UKKQueue alloc] init]; // This is a singleton, and thus an intentional "leak".
}
return gUKKQueueSharedQueueSingleton;
}
// -----------------------------------------------------------------------------
// * CONSTRUCTOR:
// Creates a new KQueue and starts that thread we use for our
// notifications.
//
// REVISIONS:
// 2004-11-12 UK Doesn't pass self as parameter to watcherThread anymore,
// because detachNewThreadSelector retains target and args,
// which would cause us to never be released.
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(id) init
{
self = [super init];
if( self )
{
queueFD = kqueue();
if( queueFD == -1 )
{
[self release];
return nil;
}
watchedPaths = [[NSMutableArray alloc] init];
watchedFDs = [[NSMutableArray alloc] init];
// Start new thread that fetches and processes our events:
keepThreadRunning = YES;
[NSThread detachNewThreadSelector:@selector(watcherThread:) toTarget:self withObject:nil];
}
return self;
}
// -----------------------------------------------------------------------------
// release:
// Since NSThread retains its target, we need this method to terminate the
// thread when we reach a retain-count of two. The thread is terminated by
// setting keepThreadRunning to NO.
//
// REVISIONS:
// 2004-11-12 UK Created.
// -----------------------------------------------------------------------------
-(oneway void) release
{
AT_SYNCHRONIZED(self)
{
//NSLog(@"%@ (%d)", self, [self retainCount]);
if( [self retainCount] == 2 && keepThreadRunning )
keepThreadRunning = NO;
}
[super release];
}
// -----------------------------------------------------------------------------
// * DESTRUCTOR:
// Releases the kqueue again.
//
// REVISIONS:
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) dealloc
{
delegate = nil;
[delegateProxy release];
if( keepThreadRunning )
keepThreadRunning = NO;
// Close all our file descriptors so the files can be deleted:
NSEnumerator* enny = [watchedFDs objectEnumerator];
NSNumber* fdNum;
while( (fdNum = [enny nextObject]) )
{
if( close( [fdNum intValue] ) == -1 )
NSLog(@"dealloc: Couldn't close file descriptor (%d)", errno);
}
[watchedPaths release];
watchedPaths = nil;
[watchedFDs release];
watchedFDs = nil;
[super dealloc];
//NSLog(@"kqueue released.");
}
// -----------------------------------------------------------------------------
// queueFD:
// Returns a Unix file descriptor for the KQueue this uses. The descriptor
// is owned by this object. Do not close it!
//
// REVISIONS:
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(int) queueFD
{
return queueFD;
}
// -----------------------------------------------------------------------------
// addPathToQueue:
// Tell this queue to listen for all interesting notifications sent for
// the object at the specified path. If you want more control, use the
// addPathToQueue:notifyingAbout: variant instead.
//
// REVISIONS:
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) addPathToQueue: (NSString*)path
{
[self addPath: path];
}
-(void) addPath: (NSString*)path
{
[self addPathToQueue: path notifyingAbout: UKKQueueNotifyAboutRename
| UKKQueueNotifyAboutWrite
| UKKQueueNotifyAboutDelete
| UKKQueueNotifyAboutAttributeChange];
}
// -----------------------------------------------------------------------------
// addPathToQueue:notfyingAbout:
// Tell this queue to listen for the specified notifications sent for
// the object at the specified path.
//
// REVISIONS:
// 2005-06-29 UK Files are now opened using O_EVTONLY instead of O_RDONLY
// which allows ejecting or deleting watched files/folders.
// Thanks to Phil Hargett for finding this flag in the docs.
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) addPathToQueue: (NSString*)path notifyingAbout: (u_int)fflags
{
struct timespec nullts = { 0, 0 };
struct kevent ev;
int fd = open( [path fileSystemRepresentation], O_EVTONLY, 0 );
if( fd >= 0 )
{
EV_SET( &ev, fd, EVFILT_VNODE,
EV_ADD | EV_ENABLE | EV_CLEAR,
fflags, 0, (void*)path );
AT_SYNCHRONIZED( self )
{
[watchedPaths addObject: path];
[watchedFDs addObject: [NSNumber numberWithInt: fd]];
kevent( queueFD, &ev, 1, NULL, 0, &nullts );
}
}
}
-(void) removePath: (NSString*)path
{
[self removePathFromQueue: path];
}
// -----------------------------------------------------------------------------
// removePathFromQueue:
// Stop listening for changes to the specified path. This removes all
// notifications. Use this to balance both addPathToQueue:notfyingAbout:
// as well as addPathToQueue:.
//
// REVISIONS:
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) removePathFromQueue: (NSString*)path
{
int index = 0;
int fd = -1;
AT_SYNCHRONIZED( self )
{
index = [watchedPaths indexOfObject: path];
if( index == NSNotFound )
return;
fd = [[watchedFDs objectAtIndex: index] intValue];
[watchedFDs removeObjectAtIndex: index];
[watchedPaths removeObjectAtIndex: index];
}
if( close( fd ) == -1 )
NSLog(@"removePathFromQueue: Couldn't close file descriptor (%d)", errno);
}
// -----------------------------------------------------------------------------
// removeAllPathsFromQueue:
// Stop listening for changes to all paths. This removes all
// notifications.
//
// REVISIONS:
// 2004-12-28 UK Added as suggested by bbum.
// -----------------------------------------------------------------------------
-(void) removeAllPathsFromQueue;
{
AT_SYNCHRONIZED( self )
{
NSEnumerator * fdEnumerator = [watchedFDs objectEnumerator];
NSNumber * anFD;
while( (anFD = [fdEnumerator nextObject]) != nil )
close( [anFD intValue] );
[watchedFDs removeAllObjects];
[watchedPaths removeAllObjects];
}
}
// -----------------------------------------------------------------------------
// watcherThread:
// This method is called by our NSThread to loop and poll for any file
// changes that our kqueue wants to tell us about. This sends separate
// notifications for the different kinds of changes that can happen.
// All messages are sent via the postNotification:forFile: main bottleneck.
//
// This also calls sharedWorkspace's noteFileSystemChanged.
//
// To terminate this method (and its thread), set keepThreadRunning to NO.
//
// REVISIONS:
// 2005-08-27 UK Changed to use keepThreadRunning instead of kqueueFD
// being -1 as termination criterion, and to close the
// queue in this thread so the main thread isn't blocked.
// 2004-11-12 UK Fixed docs to include termination criterion, added
// timeout to make sure the bugger gets disposed.
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) watcherThread: (id)sender
{
int n;
struct kevent ev;
struct timespec timeout = { 5, 0 }; // 5 seconds timeout.
int theFD = queueFD; // So we don't have to risk accessing iVars when the thread is terminated.
while( keepThreadRunning )
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NS_DURING
n = kevent( queueFD, NULL, 0, &ev, 1, &timeout );
if( n > 0 )
{
if( ev.filter == EVFILT_VNODE )
{
if( ev.fflags )
{
NSString* fpath = [[(NSString *)ev.udata retain] autorelease]; // In case one of the notified folks removes the path.
//NSLog(@"UKKQueue: Detected file change: %@", fpath);
[[NSWorkspace sharedWorkspace] noteFileSystemChanged: fpath];
//NSLog(@"ev.flags = %u",ev.fflags); // DEBUG ONLY!
if( (ev.fflags & NOTE_RENAME) == NOTE_RENAME )
[self postNotification: UKFileWatcherRenameNotification forFile: fpath];
if( (ev.fflags & NOTE_WRITE) == NOTE_WRITE )
[self postNotification: UKFileWatcherWriteNotification forFile: fpath];
if( (ev.fflags & NOTE_DELETE) == NOTE_DELETE )
[self postNotification: UKFileWatcherDeleteNotification forFile: fpath];
if( (ev.fflags & NOTE_ATTRIB) == NOTE_ATTRIB )
[self postNotification: UKFileWatcherAttributeChangeNotification forFile: fpath];
if( (ev.fflags & NOTE_EXTEND) == NOTE_EXTEND )
[self postNotification: UKFileWatcherSizeIncreaseNotification forFile: fpath];
if( (ev.fflags & NOTE_LINK) == NOTE_LINK )
[self postNotification: UKFileWatcherLinkCountChangeNotification forFile: fpath];
if( (ev.fflags & NOTE_REVOKE) == NOTE_REVOKE )
[self postNotification: UKFileWatcherAccessRevocationNotification forFile: fpath];
}
}
}
NS_HANDLER
NSLog(@"Error in UKKQueue watcherThread: %@",localException);
NS_ENDHANDLER
[pool release];
}
// Close our kqueue's file descriptor:
if( close( theFD ) == -1 )
NSLog(@"release: Couldn't close main kqueue (%d)", errno);
//NSLog(@"exiting kqueue watcher thread.");
}
// -----------------------------------------------------------------------------
// postNotification:forFile:
// This is the main bottleneck for posting notifications. If you don't want
// the notifications to go through NSWorkspace, override this method and
// send them elsewhere.
//
// REVISIONS:
// 2004-02-27 UK Changed this to send new notification, and the old one
// only to objects that respond to it. The old category on
// NSObject could cause problems with the proxy itself.
// 2004-10-31 UK Helloween fun: Make this use a mainThreadProxy and
// allow sending the notification even if we have a
// delegate.
// 2004-03-13 UK Documented.
// -----------------------------------------------------------------------------
-(void) postNotification: (NSString*)nm forFile: (NSString*)fp
{
if( delegateProxy )
{
#if UKKQUEUE_BACKWARDS_COMPATIBLE
if( ![delegateProxy respondsToSelector: @selector(watcher:receivedNotification:forPath:)] )
[delegateProxy kqueue: self receivedNotification: nm forFile: fp];
else
#endif
[delegateProxy watcher: self receivedNotification: nm forPath: fp];
}
if( !delegateProxy || alwaysNotify )
{
#if UKKQUEUE_SEND_STUPID_NOTIFICATIONS
[[[NSWorkspace sharedWorkspace] notificationCenter] postNotificationName: nm object: fp];
#else
[[[NSWorkspace sharedWorkspace] notificationCenter] postNotificationName: nm object: self
userInfo: [NSDictionary dictionaryWithObjectsAndKeys: fp, @"path", nil]];
#endif
}
}
-(id) delegate
{
return delegate;
}
-(void) setDelegate: (id)newDelegate
{
id oldProxy = delegateProxy;
delegate = newDelegate;
delegateProxy = [delegate copyMainThreadProxy];
[oldProxy release];
}
// -----------------------------------------------------------------------------
// Flag to send a notification even if we have a delegate:
// -----------------------------------------------------------------------------
-(BOOL) alwaysNotify
{
return alwaysNotify;
}
-(void) setAlwaysNotify: (BOOL)n
{
alwaysNotify = n;
}
// -----------------------------------------------------------------------------
// description:
// This method can be used to help in debugging. It provides the value
// used by NSLog & co. when you request to print this object using the
// %@ format specifier.
//
// REVISIONS:
// 2004-11-12 UK Created.
// -----------------------------------------------------------------------------
-(NSString*) description
{
return [NSString stringWithFormat: @"%@ { watchedPaths = %@, alwaysNotify = %@ }", NSStringFromClass([self class]), watchedPaths, (alwaysNotify? @"YES" : @"NO") ];
}
@end

View File

@ -0,0 +1,56 @@
/* =============================================================================
FILE: UKMainThreadProxy.h
PROJECT: UKMainThreadProxy
PURPOSE: Send a message to object theObject to [theObject mainThreadProxy]
instead and the message will be received on the main thread by
theObject.
COPYRIGHT: (c) 2004 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT License
REVISIONS:
2006-03-13 UK Clarified license.
2004-10-14 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
// -----------------------------------------------------------------------------
// Categories:
// -----------------------------------------------------------------------------
@interface NSObject (UKMainThreadProxy)
-(id) mainThreadProxy; // You can't init or release this object.
-(id) copyMainThreadProxy; // Gives you a retained version.
@end
// -----------------------------------------------------------------------------
// Classes:
// -----------------------------------------------------------------------------
/*
This object is created as a proxy in a second thread for an existing object.
All messages you send to this object will automatically be sent to the other
object on the main thread, except NSObject methods like retain/release etc.
*/
@interface UKMainThreadProxy : NSObject
{
IBOutlet id target;
}
-(id) initWithTarget: (id)targ;
@end

View File

@ -0,0 +1,151 @@
/* =============================================================================
FILE: UKMainThreadProxy.h
PROJECT: UKMainThreadProxy
PURPOSE: Send a message to object theObject to [theObject mainThreadProxy]
instead and the message will be received on the main thread by
theObject.
COPYRIGHT: (c) 2004 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: MIT Licenseâ
REVISIONS:
2006-03-13 UK Clarified license.
2004-10-14 UK Created.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKMainThreadProxy.h"
@implementation UKMainThreadProxy
-(id) initWithTarget: (id)targ
{
self = [super init];
if( self )
target = targ;
return self;
}
// -----------------------------------------------------------------------------
// Introspection overrides:
// -----------------------------------------------------------------------------
-(BOOL) respondsToSelector: (SEL)itemAction
{
BOOL does = [super respondsToSelector: itemAction];
return( does || [target respondsToSelector: itemAction] );
}
-(id) performSelector: (SEL)itemAction
{
BOOL does = [super respondsToSelector: itemAction];
if( does )
return [super performSelector: itemAction];
if( ![target respondsToSelector: itemAction] )
[self doesNotRecognizeSelector: itemAction];
[target retain];
[target performSelectorOnMainThread: itemAction withObject: nil waitUntilDone: YES];
[target release];
return nil;
}
-(id) performSelector: (SEL)itemAction withObject: (id)obj
{
BOOL does = [super respondsToSelector: itemAction];
if( does )
return [super performSelector: itemAction withObject: obj];
if( ![target respondsToSelector: itemAction] )
[self doesNotRecognizeSelector: itemAction];
[target retain];
[obj retain];
[target performSelectorOnMainThread: itemAction withObject: obj waitUntilDone: YES];
[obj release];
[target release];
return nil;
}
// -----------------------------------------------------------------------------
// Forwarding unknown methods to the target:
// -----------------------------------------------------------------------------
-(NSMethodSignature*) methodSignatureForSelector: (SEL)itemAction
{
NSMethodSignature* sig = [super methodSignatureForSelector: itemAction];
if( sig )
return sig;
return [target methodSignatureForSelector: itemAction];
}
-(void) forwardInvocation: (NSInvocation*)invocation
{
SEL itemAction = [invocation selector];
if( [target respondsToSelector: itemAction] )
{
[invocation retainArguments];
[target retain];
[invocation performSelectorOnMainThread: @selector(invokeWithTarget:) withObject: target waitUntilDone: YES];
[target release];
}
else
[self doesNotRecognizeSelector: itemAction];
}
// -----------------------------------------------------------------------------
// Safety net:
// -----------------------------------------------------------------------------
-(id) mainThreadProxy // Just in case someone accidentally sends this message to a main thread proxy.
{
return self;
}
-(id) copyMainThreadProxy // Just in case someone accidentally sends this message to a main thread proxy.
{
return [self retain];
}
@end
// -----------------------------------------------------------------------------
// Shorthand notation for getting a main thread proxy:
// -----------------------------------------------------------------------------
@implementation NSObject (UKMainThreadProxy)
-(id) mainThreadProxy
{
return [[[UKMainThreadProxy alloc] initWithTarget: self] autorelease];
}
-(id) copyMainThreadProxy
{
return [[UKMainThreadProxy alloc] initWithTarget: self];
}
@end