2013年5月16日木曜日

ディレクトリの更新を通知してもらう簡単な方法


全てのディレクトリで更新があった時に呼ばれる方法。
特定のディレクトリじゃないので起動時に一回登録してやればいい。


void fsevents_callback(ConstFSEventStreamRef streamRef, void *userData, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[])
{
    int i;
    char **paths = eventPaths;
    DFAppDelegate *appDelegate = (DFAppDelegate*)[[NSApplication sharedApplication] delegate];

    for (i=0; i
        [appDelegate updateWindowPath:[NSString stringWithCString:paths[i] encoding:NSUTF8StringEncoding]];
    }
}

- (void)checkDirectory:(NSString*)path
{
FSEventStreamEventId lastEventId = kFSEventStreamEventIdSinceNow;
    NSArray* pathsToWatch = [NSArray arrayWithObjects:path, nil];
    void *selfPointer = (__bridge void*)self;
    FSEventStreamContext context = {0, selfPointer, NULL, NULL, NULL};
    FSEventStreamRef stream;
    NSTimeInterval latency = 3.0;
    stream = FSEventStreamCreate(NULL, &fsevents_callback, &context, (__bridge CFArrayRef)pathsToWatch, lastEventId, latency, kFSEventStreamCreateFlagNone);
    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);
}


0 件のコメント: