home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2008 March / MAC_easy_03_08.iso / Software / Shareware / Isolator-3.3.dmg / Docs / Source Code / StartupItemController.m < prev    next >
Encoding:
Text File  |  2008-06-04  |  1.9 KB  |  93 lines

  1. //
  2. //  StartupItemController.m
  3. //  Isolator
  4. //
  5. //  Created by Ben Willmore on 12/02/2007.
  6. //  Copyright 2007 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "StartupItemController.h"
  10.  
  11. @implementation StartupItemController
  12.  
  13. -(id) init
  14. {
  15.     [super init];
  16.     
  17.     return self;
  18. }
  19.  
  20. -(BOOL) enabled
  21. {
  22.     OSStatus err;
  23.     NSArray* itemArray = NULL;
  24.     
  25.     err = LIAECopyLoginItems((CFArrayRef*)&itemArray);
  26.     
  27.     if (err != noErr)
  28.         return NO;
  29.     
  30.     BOOL found = NO;
  31.     
  32.     NSArray* pathComponents = [[[NSBundle mainBundle] bundlePath] pathComponents];
  33.     NSString* name = [pathComponents objectAtIndex:[pathComponents count]-1];
  34.     NSString* nameComparison = [[@"*" stringByAppendingString:name] stringByAppendingString:@"*"];
  35.  
  36.     NSEnumerator* enumerator = [itemArray objectEnumerator];
  37.     NSDictionary* item;
  38.         
  39.     while( item = [enumerator nextObject] ) {
  40.         if ([[[item objectForKey:@"URL"] absoluteString] isLike:nameComparison])
  41.             found = YES;
  42.     }
  43.  
  44.     [itemArray release]; // added by BW
  45.     
  46.     return found;
  47. }
  48.  
  49. -(void) setEnabled:(BOOL)value
  50. {
  51.     if ([self enabled])
  52.         [self removeStartupItem];
  53.     
  54.     if (value)
  55.         [self addStartupItem];
  56. }
  57.  
  58.  
  59. -(void) removeStartupItem
  60. {
  61.     OSStatus err;
  62.     NSArray* itemArray = NULL;
  63.         
  64.     err = LIAECopyLoginItems((CFArrayRef*)&itemArray);
  65.     
  66.     if (err != noErr)
  67.         return;
  68.     
  69.     NSArray* pathComponents = [[[NSBundle mainBundle] bundlePath] pathComponents];
  70.     NSString* name = [pathComponents objectAtIndex:[pathComponents count]-1];
  71.     NSString* nameComparison = [[@"*" stringByAppendingString:name] stringByAppendingString:@"*"];
  72.  
  73.     NSEnumerator* enumerator = [itemArray objectEnumerator];
  74.     NSDictionary* item;
  75.  
  76.     CFIndex idx = 0;
  77.     BOOL removed = NO;
  78.     while( (item = [enumerator nextObject]) && (!removed) ) {
  79.         if ([[[item objectForKey:@"URL"] absoluteString] isLike:nameComparison]){
  80.             LIAERemove(idx);
  81.             removed = YES;
  82.         }
  83.         idx = idx + 1;
  84.     }
  85. }
  86.  
  87. -(void) addStartupItem
  88. {
  89.     LIAEAddURLAtEnd((CFURLRef)[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]],NO);
  90. }
  91.         
  92. @end
  93.