+
+/*************************************************************************/
+#pragma mark -
+#pragma mark STATIC VARIABLES
+/*************************************************************************/
+
+static PreferencesController *prefs = nil;
+
+/*************************************************************************/
+#pragma mark -
+#pragma mark STATIC KEYCHAIN SUPPORT METHODS
+/*************************************************************************/
+
++ (SecKeychainItemRef)keychainItemForUser:(NSString *)user
+{
+ SecKeychainSearchRef search;
+ SecKeychainItemRef item;
+ OSStatus status;
+ SecKeychainAttribute attributes[3];
+ SecKeychainAttributeList list;
+
+ if ((user == nil) || ([user length] == 0)) {
+ return nil;
+ }
+
+ ITDebugLog(@"Audioscrobbler: Searching for keychain item for %@.", user);
+ attributes[0].tag = kSecAccountItemAttr;
+ attributes[0].data = (char *)[user UTF8String];
+ attributes[0].length = [user length];
+ attributes[1].tag = kSecDescriptionItemAttr;
+ attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
+ attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
+ attributes[2].tag = kSecLabelItemAttr;
+ attributes[2].data = (char *)AUDIOSCROBBLER_KEYCHAIN_SERVICE(user);
+ attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE(user));
+ list.count = 3;
+ list.attr = attributes;
+
+ status = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &list, &search);
+
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error searching for existing keychain item: %i", status);
+ }
+
+ status = SecKeychainSearchCopyNext(search, &item);
+
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error searching for existing keychain item: %i", status);
+ item = nil;
+ }
+
+ CFRelease(search);
+ return item;
+}
+
++ (BOOL)keychainItemExistsForUser:(NSString *)user
+{
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ BOOL exists = (item != nil);
+ if (item) {
+ CFRelease(item);
+ }
+ return exists;
+}
+
++ (BOOL)createKeychainItemForUser:(NSString *)user andPassword:(NSString *)password
+{
+ SecKeychainItemRef item;
+ OSStatus status;
+ SecKeychainAttribute attributes[3];
+ SecKeychainAttributeList list;
+
+ ITDebugLog(@"Audioscrobbler: Creating new keychain item for %@.", user);
+ attributes[0].tag = kSecAccountItemAttr;
+ attributes[0].data = (char *)[user UTF8String];
+ attributes[0].length = [user length];
+ attributes[1].tag = kSecDescriptionItemAttr;
+ attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
+ attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
+ attributes[2].tag = kSecLabelItemAttr;
+ attributes[2].data = (char *)AUDIOSCROBBLER_KEYCHAIN_SERVICE(user);
+ attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE(user));
+ list.count = 3;
+ list.attr = attributes;
+
+ status = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &list, [password length], [password UTF8String], NULL, NULL, &item);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error creating keychain item: %i", status);
+ }
+ return (status == noErr);
+}
+
++ (BOOL)deleteKeychainItemForUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ if (item != nil) {
+ status = SecKeychainItemDelete(item);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ CFRelease(item);
+ }
+ return (status == noErr);
+}
+
++ (BOOL)setKeychainItemPassword:(NSString *)password forUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ if (item != nil) {
+ status = SecKeychainItemModifyContent(item, NULL, [password length], [password UTF8String]);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ CFRelease(item);
+ }
+ return (status == noErr);
+}
+
++ (NSString *)getKeychainItemPasswordForUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ NSString *pass = nil;
+ if (item != nil) {
+ UInt32 length;
+ char *buffer;
+ status = SecKeychainItemCopyContent(item, NULL, NULL, &length, (void **)&buffer);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error getting keychain item password: %i", status);
+ } else {
+ pass = [[NSString alloc] initWithBytes:buffer length:length encoding:NSUTF8StringEncoding];
+ }
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ SecKeychainItemFreeContent(NULL, buffer);
+ CFRelease(item);
+ }
+ return [pass autorelease];
+}
+
+/*************************************************************************/
+#pragma mark -
+#pragma mark INITIALIZATION METHODS
+/*************************************************************************/
+
++ (PreferencesController *)sharedPrefs;
+{
+ if (! prefs) {
+ prefs = [[self alloc] init];
+ }
+ return prefs;
+}
+
+- (id)init