<commit_before>//
//  VolumeNotifier.c
//  HardwareGrowler
//
//  Created by <NAME> on 10/02/2005.
//  Copyright 2005 The Growl Project. All rights reserved.
//

#include "VolumeNotifier.h"
#include "AppController.h"
#include <DiskArbitration/DiskArbitration.h>

extern void NSLog(CFStringRef format, ...);

static DASessionRef session;

static void diskDescriptionChanged(DADiskRef disk, CFArrayRef keys, void *userInfo) {
#pragma(userInfo)
	CFDictionaryRef description = DADiskCopyDescription(disk);
	CFStringRef name = CFDictionaryGetValue(description, kDADiskDescriptionVolumeNameKey);
	CFURLRef pathURL = CFDictionaryGetValue(description, kDADiskDescriptionVolumePathKey);
	if (pathURL) {
		CFStringRef path = CFURLCopyFileSystemPath(pathURL, kCFURLPOSIXPathStyle);
		AppController_volumeDidMount(name, path);
		CFRelease(path);
	} else
		AppController_volumeDidUnmount(name);
	CFRelease(description);
}

void VolumeNotifier_init(void) {
	session = DASessionCreate(kCFAllocatorDefault);
	DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

	/* We use the disk description changed callback and not the disk
	 * appeared callback because we need volume path to find the icon.
	 * The volume path is not available at the time the disk appears, it
	 * is set at a later point in time and the disk description changed
	 * callback is fired.
	 */
	DARegisterDiskDescriptionChangedCallback(session, kDADiskDescriptionMatchVolumeMountable, kDADiskDescriptionWatchVolumePath, diskDescriptionChanged, NULL);
}

void VolumeNotifier_dealloc(void) {
	DAUnregisterCallback(session, diskDescriptionChanged, NULL);
	DASessionUnscheduleFromRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	CFRelease(session);
}<commit_msg>this is a first attempt at addressing Diggory's issue with HWGrowler not notifying when usb/network drives are mounted. the method that was here before i looked at it only notified when the user manually changed the name of the drive. strange.<commit_after>//
//  VolumeNotifier.c
//  HardwareGrowler
//
//  Created by <NAME> on 10/02/2005.
//  Copyright 2005 The Growl Project. All rights reserved.
//

#include "VolumeNotifier.h"
#include "AppController.h"
#include <DiskArbitration/DiskArbitration.h>

extern void NSLog(CFStringRef format, ...);

static DASessionRef appearSession;
static DASessionRef disappearSession;

static void diskMounted(DADiskRef disk, CFArrayRef keys, void *userInfo) {
#pragma(userInfo)
	CFDictionaryRef description = DADiskCopyDescription(disk);
	CFStringRef name = CFDictionaryGetValue(description, kDADiskDescriptionVolumeNameKey);
	CFURLRef pathURL = CFDictionaryGetValue(description, kDADiskDescriptionVolumePathKey);
	if (pathURL) {
		CFStringRef path = CFURLCopyFileSystemPath(pathURL, kCFURLPOSIXPathStyle);
		AppController_volumeDidMount(name, path);
		CFRelease(path);
	} else
		AppController_volumeDidUnmount(name);
	CFRelease(description);
}

static void diskUnMounted(DADiskRef disk, CFArrayRef keys, void *userInfo) {
#pragma(userInfo)
	CFDictionaryRef description = DADiskCopyDescription(disk);
	CFStringRef name = CFDictionaryGetValue(description, kDADiskDescriptionVolumeNameKey);
	AppController_volumeDidUnmount(name);
	CFRelease(description);
}


void VolumeNotifier_init(void) {
	
	appearSession = DASessionCreate(kCFAllocatorDefault);
	disappearSession = DASessionCreate(kCFAllocatorDefault);
	
	DASessionScheduleWithRunLoop(appearSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	DASessionScheduleWithRunLoop(disappearSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	
	DARegisterDiskAppearedCallback(appearSession, kDADiskDescriptionMatchVolumeMountable, diskMounted, NULL);
	DARegisterDiskDisappearedCallback(disappearSession, kDADiskDescriptionMatchVolumeMountable, diskUnMounted, NULL);
}

void VolumeNotifier_dealloc(void) {
	DAUnregisterCallback(appearSession, diskMounted, NULL);
	DAUnregisterCallback(disappearSession, diskUnMounted, NULL);
	DASessionUnscheduleFromRunLoop(appearSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	DASessionUnscheduleFromRunLoop(disappearSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	CFRelease(appearSession);
	CFRelease(disappearSession);
}
{'fraction_non_alphanumeric': 0.08025568181818182, 'fraction_numerical': 0.005681818181818182, 'mean_word_length': 7.569979716024341, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5202312', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1267, 'n_words': 291}