Fixed a small memory leak in PreferencesController and keychain access.
[MenuTunes.git] / PreferencesController.m
index f7598cb..0a796dc 100755 (executable)
 #import <sys/types.h>
 #import <sys/stat.h>
 
 #import <sys/types.h>
 #import <sys/stat.h>
 
-#import <ITKit/ITLoginItem.h>
-
-#import <ITKit/ITHotKeyCenter.h>
-#import <ITKit/ITKeyCombo.h>
-#import <ITKit/ITKeyComboPanel.h>
-#import <ITKit/ITWindowPositioning.h>
-#import <ITKit/ITKeyBroadcaster.h>
-
+#import <ITKit/ITKit.h>
 #import <ITKit/ITTSWBackgroundView.h>
 #import <ITKit/ITTSWBackgroundView.h>
-#import <ITKit/ITWindowEffect.h>
-#import <ITKit/ITCutWindowEffect.h>
-#import <ITKit/ITDissolveWindowEffect.h>
-#import <ITKit/ITSlideHorizontallyWindowEffect.h>
-#import <ITKit/ITSlideVerticallyWindowEffect.h>
-#import <ITKit/ITPivotWindowEffect.h>
-
 
 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
 
 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
-#define AUDIOSCROBBLER_KEYCHAIN_SERVICE "MenuTunes: Audioscrobbler"
+#define AUDIOSCROBBLER_KEYCHAIN_SERVICE(user) [[NSString stringWithFormat:@"Audioscrobbler: %@", user] UTF8String]
 #define AUDIOSCROBBLER_KEYCHAIN_KIND "application password"
 
 /*************************************************************************/
 #define AUDIOSCROBBLER_KEYCHAIN_KIND "application password"
 
 /*************************************************************************/
@@ -98,8 +84,8 @@ static PreferencesController *prefs = nil;
        attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
        attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
        attributes[2].tag = kSecLabelItemAttr;
        attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
        attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
        attributes[2].tag = kSecLabelItemAttr;
-       attributes[2].data = AUDIOSCROBBLER_KEYCHAIN_SERVICE;
-       attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE);
+       attributes[2].data = (char *)AUDIOSCROBBLER_KEYCHAIN_SERVICE(user);
+       attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE(user));
        list.count = 3;
        list.attr = attributes;
 
        list.count = 3;
        list.attr = attributes;
 
@@ -145,8 +131,8 @@ static PreferencesController *prefs = nil;
        attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
        attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
        attributes[2].tag = kSecLabelItemAttr;
        attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
        attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
        attributes[2].tag = kSecLabelItemAttr;
-       attributes[2].data = AUDIOSCROBBLER_KEYCHAIN_SERVICE;
-       attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE);
+       attributes[2].data = (char *)AUDIOSCROBBLER_KEYCHAIN_SERVICE(user);
+       attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE(user));
        list.count = 3;
        list.attr = attributes;
 
        list.count = 3;
        list.attr = attributes;
 
@@ -176,7 +162,7 @@ static PreferencesController *prefs = nil;
        OSStatus status = errSecNotAvailable;
        SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
        if (item != nil) {
        OSStatus status = errSecNotAvailable;
        SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
        if (item != nil) {
-               status = SecKeychainItemModifyContent(item, NULL, [password length], [password cString]);
+               status = SecKeychainItemModifyContent(item, NULL, [password length], [password UTF8String]);
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
                }
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
                }
@@ -197,19 +183,17 @@ static PreferencesController *prefs = nil;
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error getting keychain item password: %i", status);
                } else {
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error getting keychain item password: %i", status);
                } else {
-                       if ([NSString respondsToSelector:@selector(stringWithCString:encoding:)]) {
-                               pass = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];
-                       } else {
-                               pass = [NSString stringWithCString:buffer];
-                       }
+                       NSLog(@"Audioscrobbler: password buffer: \"%s\" \"Length: %i\"", buffer, length);
+                       pass = [[NSString alloc] initWithBytes:buffer length:length encoding:NSUTF8StringEncoding];
                }
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
                }
                }
                if (status != noErr) {
                        ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
                }
+               SecKeychainItemFreeContent(NULL, buffer);
                CFRelease(item);
        }
                CFRelease(item);
        }
-       NSLog(@"Audioscrobbler: Retrieved password: %@", pass);
-       return pass;
+       NSLog(@"Audioscrobbler: Retrieved password: \"%@\"", pass);
+       return [pass autorelease];
 }
 
 /*************************************************************************/
 }
 
 /*************************************************************************/
@@ -275,12 +259,12 @@ static PreferencesController *prefs = nil;
                                                        @"Toggle Loop",
                                                                                                           @"Toggle Song Included In Shuffle",
                                                        @"Pop-up status menu",
                                                        @"Toggle Loop",
                                                                                                           @"Toggle Song Included In Shuffle",
                                                        @"Pop-up status menu",
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9c"],
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9c"],
-                                                       [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\86â\98\86â\98\86â\98\86â\98\86"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\86â\98\86â\98\86â\98\86"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\86â\98\86â\98\86"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\86â\98\86"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\85â\98\86"],
+                                                       [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\85"],
                                                        nil];
         hotKeysDictionary = [[NSMutableDictionary alloc] init];
         controller = nil;
                                                        nil];
         hotKeysDictionary = [[NSMutableDictionary alloc] init];
         controller = nil;
@@ -421,7 +405,7 @@ static PreferencesController *prefs = nil;
             [showScriptsButton setEnabled:NO];
         }
     } else if ( [sender tag] == 1120) {
             [showScriptsButton setEnabled:NO];
         }
     } else if ( [sender tag] == 1120) {
-        mkdir([[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] cString], 0744);
+        mkdir([[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] UTF8String], 0744);
         [[NSWorkspace sharedWorkspace] openFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
     } else if ( [sender tag] == 6010) {
                //Toggle the other Audioscrobbler options
         [[NSWorkspace sharedWorkspace] openFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
     } else if ( [sender tag] == 6010) {
                //Toggle the other Audioscrobbler options