]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/agenor_defaults.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1065 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / agenor_defaults.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "AgenorUserManager.h"
23 #include "common.h"
24
25 static void usage(NSArray *args) {
26   fprintf(stderr, "usage: %s <uid> read|write|info [<key>] [<value>]\n",
27           [[args objectAtIndex:0] cString]);
28 }
29
30 static void doInfo(NSUserDefaults *defaults) {
31   printf("defaults for: '%s'\n", [[defaults valueForKey:@"uid"] cString]);
32   printf("  profile table: '%s'\n",
33          [[[defaults valueForKey:@"tableURL"] absoluteString] cString]);
34 }
35
36 static void doRead(NSUserDefaults *defaults, NSString *key) {
37   id value;
38
39   if (key == nil) {
40     NSArray  *defNames;
41     unsigned i, count;
42     
43     defNames = [defaults valueForKey:@"primaryDefaultNames"];
44     if ((count = [defNames count]) == 0) {
45       fprintf(stderr, "There are no keys in the Agenor profile!\n");
46       return;
47     }
48     
49     for (i = 0; i < count; i++) {
50       printf("%s: %s\n",
51              [[defNames objectAtIndex:i] cString],
52              [[[defaults objectForKey:[defNames objectAtIndex:i]]
53                 description] cString]);
54     }
55   }
56   else if ((value = [defaults objectForKey:key]) == nil) {
57     fprintf(stderr, "There is no key '%s' in the Agenor profile!\n", 
58             [key cString]);
59   }
60   else
61     printf("%s\n", [[value description] cString]);
62 }
63
64 static void doWrite(NSUserDefaults *defaults, NSString *key, NSString *value) {
65   [defaults setObject:value forKey:key];
66   
67   if (![defaults synchronize]) {
68     fprintf(stderr, "Failed to synchronize defaults with profile!\n");
69     return;
70   }
71 }
72
73 static void doIt(NSArray *args) {
74   AgenorUserManager *userManager;
75   NSUserDefaults    *defaults;
76   NSString *uid, *op, *key, *value;
77
78   /* extract arguments */
79   
80   if ([args count] < 3) {
81     usage(args);
82     return;
83   }
84
85   uid   = [args objectAtIndex:1];
86   op    = [args objectAtIndex:2];
87   key   = nil;
88   value = nil;
89   
90   if ([args count] > 3)
91     key = [args objectAtIndex:3];
92   
93   if ([op isEqualToString:@"write"]) {
94     if ([args count] < 5) {
95       usage(args);
96       return;
97     }
98     value = [args objectAtIndex:4];
99   }
100   
101   /* run */
102   
103   userManager = [AgenorUserManager sharedUserManager];
104   defaults    = [userManager getUserDefaultsForUID:uid];
105   
106   if (![defaults isNotNull]) {
107     fprintf(stderr, "Error: found no userdefaults for UID: '%s'\n", 
108             [uid cString]);
109     exit(1);
110   }
111   
112   if ([op isEqualToString:@"read"])
113     doRead(defaults, key);
114   else if ([op isEqualToString:@"write"])
115     doWrite(defaults, key, value);
116   else if ([op isEqualToString:@"info"])
117     doInfo(defaults);
118   else
119     usage(args);
120 }
121
122 int main(int argc, char **argv, char **env) {
123   NSAutoreleasePool *pool;
124   
125   pool = [[NSAutoreleasePool alloc] init];
126 #if LIB_FOUNDATION_LIBRARY
127   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
128 #endif
129   
130   doIt([[NSProcessInfo processInfo] argumentsWithoutDefaults]);
131   
132   [pool release];
133   return 0;
134 }