/* * Copyright(c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1(the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * Modification History * * June 1, 2001 Allan Nathanson * - public API conversion * * November 16, 2000 Allan Nathanson * - initial revision */ #include #include static CFArrayRef normalizePath(CFStringRef path) { CFArrayRef tmpElements; CFMutableArrayRef elements; CFIndex nElements; CFIndex i; if (!CFStringHasPrefix(path, CFSTR("/"))) { /* if no root separator */ return NULL; } tmpElements = CFStringCreateArrayBySeparatingStrings(NULL, path, CFSTR("/")); elements = CFArrayCreateMutableCopy(NULL, 0, tmpElements); CFRelease(tmpElements); /* remove empty path components */ nElements = CFArrayGetCount(elements); for (i=nElements; i>0; i--) { CFStringRef pathElement; pathElement = CFArrayGetValueAtIndex(elements, i-1); if (CFStringGetLength(pathElement) == 0) { CFArrayRemoveValueAtIndex(elements, i-1); nElements--; } } if (nElements < 1) { CFRelease(elements); return NULL; } return elements; } static int getPath(SCPreferencesRef session, CFStringRef path, CFMutableDictionaryRef *entity) { CFArrayRef elements; CFIndex i; CFIndex nElements; int status = kSCStatusFailed; CFMutableDictionaryRef value = NULL; elements = normalizePath(path); if (elements == NULL) { return kSCStatusNoKey; } /* get preferences key */ value = (CFMutableDictionaryRef)SCPreferencesGetValue(session, CFArrayGetValueAtIndex(elements, 0)); if (!value) { status = kSCStatusNoKey; goto done; } if (CFGetTypeID(value) != CFDictionaryGetTypeID()) { status = kSCStatusNoKey; goto done; } nElements = CFArrayGetCount(elements); for (i=1; i 1) { CFDictionarySetValue(element, CFArrayGetValueAtIndex(elements, nElements-1), value); } ok = SCPreferencesSetValue(session, CFArrayGetValueAtIndex(elements, 0), root); if (newRoot) CFRelease(root); CFRelease(elements); return ok; } Boolean SCPreferencesPathSetLink(SCPreferencesRef session, CFStringRef path, CFStringRef link) { CFMutableDictionaryRef dict; Boolean ok; SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesPathSetLink:")); SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" path = %@"), path); SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" link = %@"), link); dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionaryAddValue(dict, kSCResvLink, link); ok = SCPreferencesPathSetValue(session, path, dict); CFRelease(dict); return ok; } Boolean SCPreferencesPathRemoveValue(SCPreferencesRef session, CFStringRef path) { CFMutableDictionaryRef element; CFArrayRef elements = NULL; CFIndex i; CFIndex nElements; Boolean ok = FALSE; CFMutableDictionaryRef root = NULL; SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesPathRemoveValue:")); SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" path = %@"), path); elements = normalizePath(path); if (elements == NULL) { _SCErrorSet(kSCStatusNoKey); return FALSE; } /* get preferences key */ root = (CFMutableDictionaryRef)SCPreferencesGetValue(session, CFArrayGetValueAtIndex(elements, 0)); if (!root) { goto done; } nElements = CFArrayGetCount(elements); if (nElements == 1) { /* if we are removing the data associated with the preference key */ ok = SCPreferencesRemoveValue(session, CFArrayGetValueAtIndex(elements, 0)); goto done; } element = root; for (i=1; i