UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / apcagent / InstanceConfig.m
blob58d5e5b6c0db12f71772c7b6b195c3a3f57210cf
1 /*
2  * InstanceConfig.m
3  *
4  * Apcupsd monitoring applet for Mac OS X
5  */
7 /*
8  * Copyright (C) 2009 Adam Kropelin
9  *
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.
13  *
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.
18  *
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,
22  * MA 02111-1307, USA.
23  */
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];
39    return cfg;
42 + (InstanceConfig*) configWithDefaults
44    InstanceConfig *cfg = [[[InstanceConfig alloc] init] autorelease];
45    cfg->config = [[NSMutableDictionary dictionary] retain];
47    // Establish defaults
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];
54    return cfg;
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++)
67    {
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];
74    }
76    // Write array back to prefs
77    [prefs setObject:instances forKey:INSTANCES_PREF_KEY];
78    [prefs synchronize];
81 - (void)dealloc
83    [config release];
84    [super dealloc];
87 - (NSString *)host
89    return [[[config objectForKey:HOSTNAME_PREF_KEY] retain] autorelease];
92 - (int)port
94    return [(NSNumber*)[config objectForKey:PORT_PREF_KEY] intValue];
97 - (int)refresh
99    return [(NSNumber*)[config objectForKey:REFRESH_PREF_KEY] intValue];
102 - (BOOL)popups
104    return [(NSNumber*)[config objectForKey:POPUPS_PREF_KEY] boolValue];
107 - (NSString *)id
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];
132 - (void) save
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
141    unsigned int i;
142    for (i = 0; i < [instances count]; i++)
143    {
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]])
149       {
150          [instances replaceObjectAtIndex:i withObject:config];
151          break;
152       }
153    }
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];
161    [prefs synchronize];
164 @end