4 * Apcupsd monitoring applet for Mac OS X
8 * Copyright (C) 2009 Adam Kropelin
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General
12 * Public License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 #import "InstanceConfig.h"
26 #import "NSString+uuid.h"
28 #define DEFAULT_HOSTNAME @"127.0.0.1"
29 #define DEFAULT_PORT 3551
30 #define DEFAULT_REFRESH 5
31 #define DEFAULT_POPUPS YES
33 @implementation InstanceConfig
35 + (InstanceConfig*) configWithDictionary:(NSDictionary*)dict
37 InstanceConfig *cfg = [[[InstanceConfig alloc] init] autorelease];
38 cfg->config = [[NSMutableDictionary dictionaryWithDictionary:dict] retain];
42 + (InstanceConfig*) configWithDefaults
44 InstanceConfig *cfg = [[[InstanceConfig alloc] init] autorelease];
45 cfg->config = [[NSMutableDictionary dictionary] retain];
48 [cfg->config setObject:[NSString stringWithUUID] forKey:ID_PREF_KEY];
49 [cfg->config setObject:DEFAULT_HOSTNAME forKey:HOSTNAME_PREF_KEY];
50 [cfg->config setObject:[NSNumber numberWithInt:DEFAULT_PORT] forKey:PORT_PREF_KEY];
51 [cfg->config setObject:[NSNumber numberWithInt:DEFAULT_REFRESH] forKey:REFRESH_PREF_KEY];
52 [cfg->config setObject:[NSNumber numberWithBool:DEFAULT_POPUPS] forKey:POPUPS_PREF_KEY];
57 + (void) removeConfigWithId:(NSString*)id
59 // Fetch instances array from preferences and make a mutable copy
60 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
61 NSArray *tmp = [prefs arrayForKey:INSTANCES_PREF_KEY];
62 if (!tmp) tmp = [NSArray array];
63 NSMutableArray *instances = [NSMutableArray arrayWithArray:tmp];
65 // Remove existing config from array
66 for (unsigned int i = 0; i < [instances count]; i++)
68 InstanceConfig *tmpconfig =
69 [InstanceConfig configWithDictionary:[instances objectAtIndex:i]];
71 // If id was found, remove that config
72 if ([id isEqualToString:[tmpconfig id]])
73 [instances removeObjectAtIndex:i];
76 // Write array back to prefs
77 [prefs setObject:instances forKey:INSTANCES_PREF_KEY];
89 return [[[config objectForKey:HOSTNAME_PREF_KEY] retain] autorelease];
94 return [(NSNumber*)[config objectForKey:PORT_PREF_KEY] intValue];
99 return [(NSNumber*)[config objectForKey:REFRESH_PREF_KEY] intValue];
104 return [(NSNumber*)[config objectForKey:POPUPS_PREF_KEY] boolValue];
109 return [[[config objectForKey:ID_PREF_KEY] retain] autorelease];
112 - (void)setHost:(NSString *)host
114 [config setObject:host forKey:HOSTNAME_PREF_KEY];
117 - (void)setPort:(int)port;
119 [config setObject:[NSNumber numberWithInt:port] forKey:PORT_PREF_KEY];
122 - (void)setRefresh:(int)refresh;
124 [config setObject:[NSNumber numberWithInt:refresh] forKey:REFRESH_PREF_KEY];
127 - (void)setPopups:(BOOL)popupsEnabled;
129 [config setObject:[NSNumber numberWithBool:popupsEnabled] forKey:POPUPS_PREF_KEY];
134 // Fetch instances array from preferences and make a mutable copy
135 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
136 NSArray *tmp = [prefs arrayForKey:INSTANCES_PREF_KEY];
137 if (!tmp) tmp = [NSArray array];
138 NSMutableArray *instances = [NSMutableArray arrayWithArray:tmp];
140 // Search for matching config
142 for (i = 0; i < [instances count]; i++)
144 InstanceConfig *tmpconfig =
145 [InstanceConfig configWithDictionary:[instances objectAtIndex:i]];
147 // If id was found replace with new config
148 if ([[self id] isEqualToString:[tmpconfig id]])
150 [instances replaceObjectAtIndex:i withObject:config];
155 // If config did not yet exist in array, add it at the end
156 if (i == [instances count])
157 [instances addObject:config];
159 // Write array back to prefs
160 [prefs setObject:instances forKey:INSTANCES_PREF_KEY];