]> err.no Git - sope/blob - sope-core/samples/bmlookup.m
deleted moved files (not sure what went wrong before ...)
[sope] / sope-core / samples / bmlookup.m
1 /*
2   Copyright (C) 2000-2004 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 // $Id$
22
23 #include "common.h"
24 #include <EOControl/EOControl.h>
25 #include <NGExtensions/NSFileManager+Extensions.h>
26 #include <NGExtensions/NGFileFolderInfoDataSource.h>
27 #include <NGExtensions/NGExtensions.h>
28
29 @interface BMLookupTool : NSObject
30 {
31   NGBundleManager *bm;
32 }
33
34 - (int)runWithArguments:(NSArray *)_args;
35
36 @end
37
38 @implementation BMLookupTool
39
40 - (id)init {
41   if ((self = [super init])) {
42     self->bm = [[NGBundleManager defaultBundleManager] retain];
43   }
44   return self;
45 }
46 - (void)dealloc {
47   [self->bm release];
48   [super dealloc];
49 }
50
51 - (void)listResourcesOfType:(NSString *)btype {
52   NSEnumerator *resources;
53   id resource;
54   
55   printf("lookup resources of type: '%s'\n", [btype cString]);
56
57   resources = [[bm providedResourcesOfType:btype] objectEnumerator];
58   while ((resource = [resources nextObject])) {
59     NSString *rname;
60       
61     if ((rname  = [resource objectForKey:@"name"]) != nil) {
62       NSBundle *bundle;
63       
64       bundle = [bm bundleProvidingResource:rname ofType:btype];
65       printf("  resource  '%s'\n",  [rname cString]);
66       printf("    bundle: '%s'\n", [[bundle bundlePath] cString]);
67       printf("    info:   %s\n", [[resource description] cString]);
68     }
69     else
70       printf("  resource info: %s\n", [[resource description] cString]);
71   }
72 }
73
74 - (void)lookupResourceWithName:(NSString *)bname ofType:(NSString *)btype {
75   NSBundle *bundle;
76     
77   printf("lookup resource '%s' of type: '%s'\n", 
78          [bname cString], [btype cString]);
79     
80   bundle = [self->bm bundleProvidingResource:bname ofType:btype];
81   printf("  bundle: '%s'\n", [[bundle bundlePath] cString]);
82   
83   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"load"]) {
84     if (![bundle load])
85       NSLog(@"Could not load bundle: %@", bundle);
86     else
87       printf("  did load bundle: %s\n", [[bundle description] cString]);
88   }
89 }
90
91 - (int)runWithArguments:(NSArray *)_args {
92   if ([_args count] < 2) {
93     NSLog(@"usage: %@ type name", [[_args objectAtIndex:0] lastPathComponent]);
94     return 1;
95   }
96   
97   if ([_args count] == 2) {
98     [self listResourcesOfType:[_args objectAtIndex:1]];
99     return 0;
100   }
101   
102   [self lookupResourceWithName:[_args objectAtIndex:2]
103         ofType:[_args objectAtIndex:1]];
104   return 0;
105 }
106
107 @end /* BMLookupTool */
108
109 int main(int argc, char **argv, char **env) {
110   NSAutoreleasePool *pool;
111   int rc;
112   
113   pool = [[NSAutoreleasePool alloc] init];
114 #if LIB_FOUNDATION_LIBRARY
115   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
116 #endif
117   
118   rc = [[[[BMLookupTool alloc] init] autorelease]
119          runWithArguments:
120            [[NSProcessInfo processInfo] argumentsWithoutDefaults]];
121   
122   [pool release];
123   
124   exit(rc);
125   return rc;
126 }