/theremin/trunk/Source/Preferences/ProfileController.m

http://github.com/Lichtlos/Theremin_c3ks · Objective C · 232 lines · 176 code · 49 blank · 7 comment · 38 complexity · ef2827ce86e1020d0ee103e1b95faf7f MD5 · raw file

  1. //
  2. // ProfileController.m
  3. // Theremin
  4. //
  5. // Created by Patrik Weiskircher on 13.08.08.
  6. // Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "ProfileController.h"
  9. #import "PreferencesController.h"
  10. #import "Profile.h"
  11. #import "ProfileRepository.h"
  12. #import "ImageTextCell.h"
  13. const NSString *newProfileNameTemplate = @"Profile %d";
  14. @interface ProfileController (PrivateMethods)
  15. - (NSString *) newProfileName;
  16. - (BOOL) profileNameIsUnique:(NSString *)aProfileName;
  17. - (void) saveUIToProfile:(Profile *)aProfile;
  18. - (void) loadProfileToUI:(Profile *)aProfile;
  19. - (void) enableUIs:(BOOL)aValue;
  20. - (void) loadPortToUI:(int)port usingMode:(ProfileMode)mode;
  21. - (Profile *) defaultProfile;
  22. - (Profile *) selectedProfile;
  23. - (ProfileMode) profileModeFromType;
  24. @end
  25. @implementation ProfileController
  26. - (void) dealloc
  27. {
  28. [_lastProfile release];
  29. [super dealloc];
  30. }
  31. - (void) enableUIs:(BOOL)aValue {
  32. [_description setEnabled:aValue];
  33. [_type setEnabled:aValue];
  34. [_hostname setEnabled:aValue];
  35. [_port setEnabled:aValue];
  36. [_password setEnabled:aValue];
  37. [_autoreconnect setEnabled:aValue];
  38. }
  39. - (void) saveUIToProfile:(Profile *)aProfile {
  40. [aProfile setDescription:[_description stringValue]];
  41. [aProfile setMode:[self profileModeFromType]];
  42. [aProfile setHostname:[_hostname stringValue]];
  43. [aProfile setPort:[_port intValue]];
  44. [aProfile setPassword:[_password stringValue]];
  45. [aProfile setAutoreconnect:[_autoreconnect state] == NSOnState];
  46. if ([aProfile mode] == eModeSqueezeCenter)
  47. [aProfile setUser:[_user stringValue]];
  48. }
  49. - (void) loadProfileToUI:(Profile *)aProfile {
  50. if ([aProfile description])
  51. [_description setStringValue:[aProfile description]];
  52. else
  53. [_description setStringValue:@""];
  54. [_type selectItemWithTag:[aProfile mode] == eModeMPD ? 0 : 1];
  55. if ([aProfile hostname])
  56. [_hostname setStringValue:[aProfile hostname]];
  57. else
  58. [_hostname setStringValue:@""];
  59. [self loadPortToUI:[aProfile port] usingMode:[aProfile mode]];
  60. if ([aProfile password])
  61. [_password setStringValue:[aProfile password]];
  62. else
  63. [_password setStringValue:@""];
  64. [_autoreconnect setState:[aProfile autoreconnect] ? NSOnState : NSOffState];
  65. [_user setEnabled:[aProfile mode] == eModeSqueezeCenter];
  66. if ([aProfile mode] == eModeSqueezeCenter && [aProfile user] != nil)
  67. [_user setStringValue:[aProfile user]];
  68. else
  69. [_user setStringValue:@""];
  70. }
  71. - (void) loadPortToUI:(int)port usingMode:(ProfileMode)mode {
  72. if (port == -1) {
  73. if (mode == eModeMPD)
  74. port = 6600;
  75. else if (mode == eModeSqueezeCenter)
  76. port = 9090;
  77. }
  78. [_port setStringValue:[NSString stringWithFormat:@"%d", port]];
  79. }
  80. - (IBAction) typeChanged:(id)sender {
  81. [self loadPortToUI:[[self selectedProfile] port] usingMode:[self profileModeFromType]];
  82. }
  83. - (id) initWithCoder:(id)coder {
  84. self = [super initWithCoder:coder];
  85. NSArray *profiles = [ProfileRepository profiles];
  86. [self setContent:profiles];
  87. return self;
  88. }
  89. - (void) awakeFromNib {
  90. ImageTextCell *cell = [[[ImageTextCell alloc] init] autorelease];
  91. [cell setDataDelegate:self];
  92. [[[_tableView tableColumns] objectAtIndex:0] setDataCell:cell];
  93. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"squeezeboxSupportEnabled"] boolValue] == NO)
  94. [_type removeItemAtIndex:1];
  95. }
  96. - (id) newObject {
  97. Profile *profile = [[Profile alloc] initWithDescription:[self newProfileName]];
  98. if ([[self content] count] == 0)
  99. [profile setDefault:YES];
  100. return profile;
  101. }
  102. - (NSString *) newProfileName {
  103. NSString *profile;
  104. int count = 1;
  105. do {
  106. profile = [NSString stringWithFormat:(NSString *)newProfileNameTemplate, count];
  107. count++;
  108. } while (![self profileNameIsUnique:profile]);
  109. return profile;
  110. }
  111. - (BOOL) profileNameIsUnique:(NSString *)aProfileName {
  112. NSArray *profiles = [self content];
  113. for (int i = 0; i < [profiles count]; i++)
  114. if ([[[profiles objectAtIndex:i] description] isEqualToString:aProfileName])
  115. return NO;
  116. return YES;
  117. }
  118. - (void) saveProfiles {
  119. if ([self selectedProfile])
  120. [self saveUIToProfile:[self selectedProfile]];
  121. [ProfileRepository saveProfiles:[self content]];
  122. }
  123. - (void) opened {
  124. [self tableViewSelectionDidChange:nil];
  125. }
  126. - (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
  127. if (_lastProfile != nil)
  128. [self saveUIToProfile:_lastProfile];
  129. [self enableUIs:YES];
  130. Profile *profile = [self selectedProfile];
  131. [self loadProfileToUI:profile];
  132. [_lastProfile release];
  133. _lastProfile = [profile retain];
  134. [_defaultButton setEnabled:![self currentSelectionIsDefault]];
  135. }
  136. - (Profile *) selectedProfile {
  137. if ([_tableView selectedRow] == -1)
  138. return nil;
  139. return [[self content] objectAtIndex:[_tableView selectedRow]];
  140. }
  141. - (ProfileMode) profileModeFromType {
  142. return [[_type selectedItem] tag] == 0 ? eModeMPD : eModeSqueezeCenter;
  143. }
  144. - (Profile *) defaultProfile {
  145. NSArray *profiles = [self content];
  146. for (int i = 0; i < [profiles count]; i++) {
  147. if ([[profiles objectAtIndex:i] default])
  148. return [profiles objectAtIndex:i];
  149. }
  150. return nil;
  151. }
  152. - (IBAction) setCurrentSelectionAsDefault:(id)sender {
  153. Profile *profile = [self selectedProfile];
  154. [[self defaultProfile] setDefault:NO];
  155. [profile setDefault:YES];
  156. [_defaultButton setEnabled:NO];
  157. }
  158. - (BOOL) currentSelectionIsDefault {
  159. return [[self selectedProfile] default];
  160. }
  161. - (BOOL) canRemove {
  162. if ([[self content] count] == 1)
  163. return NO;
  164. return [super canRemove];
  165. }
  166. - (IBAction) removeCurrentlySelectedProfile:(id)sender {
  167. [self removeObject:[self selectedProfile]];
  168. [self tableViewSelectionDidChange:nil];
  169. }
  170. - (NSImage*) iconForCell: (ImageTextCell*) cell data: (NSObject*) data {
  171. return nil;
  172. }
  173. - (NSString*) primaryTextForCell: (ImageTextCell*) cell data: (NSObject*) data {
  174. return [data description];
  175. }
  176. - (NSString*) secondaryTextForCell: (ImageTextCell*) cell data: (NSObject*) data {
  177. Profile *profile = (Profile *)data;
  178. NSString *mode = [profile mode] == eModeMPD ? @"MPD" : @"SqueezeCenter";
  179. if ([profile default])
  180. return [NSString stringWithFormat:@"%@ (Default)", mode];
  181. return mode;
  182. }
  183. @end