| 1 | /* ============================================================================= |
|---|
| 2 | FILE: UKKQueue.h |
|---|
| 3 | PROJECT: Filie |
|---|
| 4 | |
|---|
| 5 | COPYRIGHT: (c) 2003 M. Uli Kusterer, all rights reserved. |
|---|
| 6 | |
|---|
| 7 | AUTHORS: M. Uli Kusterer - UK |
|---|
| 8 | |
|---|
| 9 | LICENSES: MIT License |
|---|
| 10 | |
|---|
| 11 | REVISIONS: |
|---|
| 12 | 2006-03-13 UK Clarified license, streamlined UKFileWatcher stuff, |
|---|
| 13 | Changed notifications to be useful and turned off by |
|---|
| 14 | default some deprecated stuff. |
|---|
| 15 | 2003-12-21 UK Created. |
|---|
| 16 | ========================================================================== */ |
|---|
| 17 | |
|---|
| 18 | // ----------------------------------------------------------------------------- |
|---|
| 19 | // Headers: |
|---|
| 20 | // ----------------------------------------------------------------------------- |
|---|
| 21 | |
|---|
| 22 | #import <Foundation/Foundation.h> |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <sys/event.h> |
|---|
| 25 | #import "UKFileWatcher.h" |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | // ----------------------------------------------------------------------------- |
|---|
| 29 | // Constants: |
|---|
| 30 | // ----------------------------------------------------------------------------- |
|---|
| 31 | |
|---|
| 32 | // Backwards compatibility constants. Don't rely on code commented out with these constants, because it may be deleted in a future version. |
|---|
| 33 | #ifndef UKKQUEUE_BACKWARDS_COMPATIBLE |
|---|
| 34 | #define UKKQUEUE_BACKWARDS_COMPATIBLE 0 // 1 to send old-style kqueue:receivedNotification:forFile: messages to objects that accept them. |
|---|
| 35 | #endif |
|---|
| 36 | |
|---|
| 37 | #ifndef UKKQUEUE_SEND_STUPID_NOTIFICATIONS |
|---|
| 38 | #define UKKQUEUE_SEND_STUPID_NOTIFICATIONS 0 // 1 to send old-style notifications that have the path as the object and no userInfo dictionary. |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #ifndef UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME |
|---|
| 42 | #define UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME 0 // 1 to allow use of sharedQueue instead of sharedFileWatcher. |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | #ifndef UKKQUEUE_OLD_NOTIFICATION_NAMES |
|---|
| 46 | #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. |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | // Flags for notifyingAbout: |
|---|
| 50 | #define UKKQueueNotifyAboutRename NOTE_RENAME // Item was renamed. |
|---|
| 51 | #define UKKQueueNotifyAboutWrite NOTE_WRITE // Item contents changed (also folder contents changed). |
|---|
| 52 | #define UKKQueueNotifyAboutDelete NOTE_DELETE // item was removed. |
|---|
| 53 | #define UKKQueueNotifyAboutAttributeChange NOTE_ATTRIB // Item attributes changed. |
|---|
| 54 | #define UKKQueueNotifyAboutSizeIncrease NOTE_EXTEND // Item size increased. |
|---|
| 55 | #define UKKQueueNotifyAboutLinkCountChanged NOTE_LINK // Item's link count changed. |
|---|
| 56 | #define UKKQueueNotifyAboutAccessRevocation NOTE_REVOKE // Access to item was revoked. |
|---|
| 57 | |
|---|
| 58 | // Notifications this sends: |
|---|
| 59 | // (see UKFileWatcher) |
|---|
| 60 | // Old names: *deprecated* |
|---|
| 61 | #if UKKQUEUE_OLD_NOTIFICATION_NAMES |
|---|
| 62 | #define UKKQueueFileRenamedNotification UKFileWatcherRenameNotification |
|---|
| 63 | #define UKKQueueFileWrittenToNotification UKFileWatcherWriteNotification |
|---|
| 64 | #define UKKQueueFileDeletedNotification UKFileWatcherDeleteNotification |
|---|
| 65 | #define UKKQueueFileAttributesChangedNotification UKFileWatcherAttributeChangeNotification |
|---|
| 66 | #define UKKQueueFileSizeIncreasedNotification UKFileWatcherSizeIncreaseNotification |
|---|
| 67 | #define UKKQueueFileLinkCountChangedNotification UKFileWatcherLinkCountChangeNotification |
|---|
| 68 | #define UKKQueueFileAccessRevocationNotification UKFileWatcherAccessRevocationNotification |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | // ----------------------------------------------------------------------------- |
|---|
| 73 | // UKKQueue: |
|---|
| 74 | // ----------------------------------------------------------------------------- |
|---|
| 75 | |
|---|
| 76 | @interface UKKQueue : NSObject <UKFileWatcher> |
|---|
| 77 | { |
|---|
| 78 | int queueFD; // The actual queue ID (Unix file descriptor). |
|---|
| 79 | NSMutableArray* watchedPaths; // List of NSStrings containing the paths we're watching. |
|---|
| 80 | NSMutableArray* watchedFDs; // List of NSNumbers containing the file descriptors we're watching. |
|---|
| 81 | id delegate; // Gets messages about changes instead of notification center, if specified. |
|---|
| 82 | id delegateProxy; // Proxy object to which we send messages so they reach delegate on the main thread. |
|---|
| 83 | BOOL alwaysNotify; // Send notifications even if we have a delegate? Defaults to NO. |
|---|
| 84 | BOOL keepThreadRunning; // Termination criterion of our thread. |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | +(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. |
|---|
| 88 | |
|---|
| 89 | -(int) queueFD; // I know you unix geeks want this... |
|---|
| 90 | |
|---|
| 91 | // High-level file watching: (use UKFileWatcher protocol methods instead, where possible!) |
|---|
| 92 | -(void) addPathToQueue: (NSString*)path; |
|---|
| 93 | -(void) addPathToQueue: (NSString*)path notifyingAbout: (u_int)fflags; |
|---|
| 94 | -(void) removePathFromQueue: (NSString*)path; |
|---|
| 95 | |
|---|
| 96 | -(id) delegate; |
|---|
| 97 | -(void) setDelegate: (id)newDelegate; |
|---|
| 98 | |
|---|
| 99 | -(BOOL) alwaysNotify; |
|---|
| 100 | -(void) setAlwaysNotify: (BOOL)n; |
|---|
| 101 | |
|---|
| 102 | #if UKKQUEUE_OLD_SINGLETON_ACCESSOR_NAME |
|---|
| 103 | +(UKKQueue*) sharedQueue; |
|---|
| 104 | #endif |
|---|
| 105 | |
|---|
| 106 | // private: |
|---|
| 107 | -(void) watcherThread: (id)sender; |
|---|
| 108 | -(void) postNotification: (NSString*)nm forFile: (NSString*)fp; // Message-posting bottleneck. |
|---|
| 109 | |
|---|
| 110 | @end |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | // ----------------------------------------------------------------------------- |
|---|
| 114 | // Methods delegates need to provide: |
|---|
| 115 | // * DEPRECATED * use UKFileWatcher delegate methods instead! |
|---|
| 116 | // ----------------------------------------------------------------------------- |
|---|
| 117 | |
|---|
| 118 | @interface NSObject (UKKQueueDelegate) |
|---|
| 119 | |
|---|
| 120 | -(void) kqueue: (UKKQueue*)kq receivedNotification: (NSString*)nm forFile: (NSString*)fpath; |
|---|
| 121 | |
|---|
| 122 | @end |
|---|