Plugins/Displays/SMS/GrowlSMSPrefs.m
author Rudy Richter
Sat Aug 01 20:43:39 2009 -0400 (2009-08-01)
changeset 4259 0e9b6b0b1e25
parent 4246 4f52d1d98978
child 4666 59b81a267426
child 5032 ed4241cc32ed
permissions -rw-r--r--
Plugins: clang warnings
     1 //
     2 //  GrowlSMSPrefs.m
     3 //  Display Plugins
     4 //
     5 //  Created by Diggory Laycock
     6 //  Copyright 2005-2006 The Growl Project All rights reserved.
     7 //
     8 
     9 #import "GrowlSMSPrefs.h"
    10 #import "GrowlDefinesInternal.h"
    11 #import "NSStringAdditions.h"
    12 #import <Security/SecKeychain.h>
    13 #import <Security/SecKeychainItem.h>
    14 
    15 #define GrowlSMSPrefDomain		@"com.Growl.SMS"
    16 #define accountNameKey			@"SMS - Account Name"
    17 #define accountAPIIDKey			@"SMS - Account API ID"
    18 #define destinationNumberKey	@"SMS - Destination Number"
    19 
    20 #define keychainServiceName "GrowlSMS"
    21 #define keychainAccountName "SMSWebServicePassword"
    22 
    23 
    24 @implementation GrowlSMSPrefs
    25 
    26 - (NSString *) mainNibName {
    27 	return @"GrowlSMSPrefs";
    28 }
    29 
    30 - (void) didSelect {
    31 	SYNCHRONIZE_GROWL_PREFS();
    32 }
    33 
    34 #pragma mark -
    35 
    36 - (NSString *) getAccountName {
    37 	NSString *value = nil;
    38 	READ_GROWL_PREF_VALUE(accountNameKey, GrowlSMSPrefDomain, NSString *, &value);
    39 	if(value)
    40 		CFMakeCollectable(value);
    41 	return [value autorelease];
    42 }
    43 
    44 - (void) setAccountName:(NSString *)value {
    45 	if (!value)
    46 		value = @"";
    47 	WRITE_GROWL_PREF_VALUE(accountNameKey, value, GrowlSMSPrefDomain);
    48 	UPDATE_GROWL_PREFS();
    49 }
    50 
    51 
    52 - (NSString *) getAccountAPIID {
    53 	NSString *value = nil;
    54 	READ_GROWL_PREF_VALUE(accountAPIIDKey, GrowlSMSPrefDomain, NSString *, &value);
    55 	if(value)
    56 		CFMakeCollectable(value);
    57 	return [value autorelease];
    58 }
    59 
    60 - (void) setAccountAPIID:(NSString *)value {
    61 	if (!value)
    62 		value = @"";
    63 	WRITE_GROWL_PREF_VALUE(accountAPIIDKey, value, GrowlSMSPrefDomain);
    64 	UPDATE_GROWL_PREFS();
    65 }
    66 
    67 
    68 - (NSString *) getDestinationNumber {
    69 	NSString *value = nil;
    70 	READ_GROWL_PREF_VALUE(destinationNumberKey, GrowlSMSPrefDomain, NSString *, &value);
    71 	if(value)
    72 		CFMakeCollectable(value);
    73 	return [value autorelease];
    74 }
    75 
    76 - (void) setDestinationNumber:(NSString *)value {
    77 	if (!value)
    78 		value = @"";
    79 	WRITE_GROWL_PREF_VALUE(destinationNumberKey, value, GrowlSMSPrefDomain);
    80 	UPDATE_GROWL_PREFS();
    81 }
    82 
    83 
    84 - (NSString *) accountPassword {
    85 	unsigned char *password;
    86 	UInt32 passwordLength;
    87 	OSStatus status;
    88 	status = SecKeychainFindGenericPassword( NULL,
    89 											 (UInt32)strlen(keychainServiceName), keychainServiceName,
    90 											 (UInt32)strlen(keychainAccountName), keychainAccountName,
    91 											 &passwordLength, (void **)&password, NULL );
    92 
    93 	NSString *passwordString;
    94 	if (status == noErr) {
    95 		passwordString = (NSString *)CFStringCreateWithBytes(kCFAllocatorDefault, password, passwordLength, kCFStringEncodingUTF8, false);
    96 		if(passwordString)
    97 			CFMakeCollectable(passwordString);		
    98 		[passwordString autorelease];
    99 		SecKeychainItemFreeContent(NULL, password);
   100 	} else {
   101 		if (status != errSecItemNotFound)
   102 			NSLog(@"Failed to retrieve SMS Account password from keychain. Error: %d", status);
   103 		passwordString = @"";
   104 	}
   105 
   106 	return passwordString;
   107 }
   108 
   109 - (void) setAccountPassword:(NSString *)value {
   110 	const char *password = value ? [value UTF8String] : "";
   111 	UInt32 length = (UInt32)strlen(password);
   112 	OSStatus status;
   113 	SecKeychainItemRef itemRef = nil;
   114 	status = SecKeychainFindGenericPassword( NULL,
   115 											 (UInt32)strlen(keychainServiceName), keychainServiceName,
   116 											 (UInt32)strlen(keychainAccountName), keychainAccountName,
   117 											 NULL, NULL, &itemRef );
   118 	if (status == errSecItemNotFound) {
   119 		// add new item
   120 		status = SecKeychainAddGenericPassword( NULL,
   121 												(UInt32)strlen(keychainServiceName), keychainServiceName,
   122 												(UInt32)strlen(keychainAccountName), keychainAccountName,
   123 												length, password, NULL );
   124 		if (status)
   125 			NSLog(@"Failed to add SMS Account password to keychain.");
   126 	} else {
   127 		// change existing password
   128 		SecKeychainAttribute attrs[] = {
   129 			{ kSecAccountItemAttr, (UInt32)strlen(keychainAccountName), (char *)keychainAccountName },
   130 			{ kSecServiceItemAttr, (UInt32)strlen(keychainServiceName), (char *)keychainServiceName }
   131 		};
   132 		const SecKeychainAttributeList attributes = { (UInt32)sizeof(attrs) / (UInt32)sizeof(attrs[0]), attrs };
   133 		status = SecKeychainItemModifyAttributesAndData( itemRef,		// the item reference
   134 														 &attributes,	// no change to attributes
   135 														 length,		// length of password
   136 														 password		// pointer to password data
   137 														 );
   138 		if (itemRef)
   139 			CFRelease(itemRef);
   140 		if (status)
   141 			NSLog(@"Failed to change SMS password in keychain.");
   142 	}
   143 }
   144 
   145 @end