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