]> err.no Git - scalable-opengroupware.org/blob - Main/SOGoProductLoader.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1166 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / Main / SOGoProductLoader.m
1 /*
2   Copyright (C) 2004-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 #import <Foundation/NSArray.h>
23 #import <Foundation/NSDictionary.h>
24 #import <Foundation/NSFileManager.h>
25 #import <Foundation/NSPathUtilities.h>
26 #import <Foundation/NSProcessInfo.h>
27
28 #import <NGObjWeb/SoProductRegistry.h>
29 #import <NGExtensions/NSObject+Logs.h>
30
31 #import "SOGoProductLoader.h"
32
33 @implementation SOGoProductLoader
34
35 + (int)sogoMajorVersion {
36   return SOGO_MAJOR_VERSION;
37 }
38 + (int)sogoMinorVersion {
39   return SOGO_MINOR_VERSION;
40 }
41
42 + (id)productLoader {
43   return [[[self alloc] init] autorelease];
44 }
45
46 - (id)init {
47   if ((self = [super init])) {
48     self->productDirectoryName =
49       [[NSString alloc] initWithFormat:@"SOGo-%i.%i", 
50         [[self class] sogoMajorVersion],
51         [[self class] sogoMinorVersion]];
52   }
53   return self;
54 }
55
56 - (void)dealloc {
57   [self->productDirectoryName release];
58   [self->searchPathes release];
59   [super dealloc];
60 }
61
62 /* loading */
63
64 - (void)_addCocoaSearchPathesToArray:(NSMutableArray *)ma {
65   id tmp;
66
67   tmp = NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory,
68                                             NSAllDomainsMask,
69                                             YES);
70   if ([tmp count] > 0) {
71     NSEnumerator *e;
72       
73     e = [tmp objectEnumerator];
74     while ((tmp = [e nextObject])) {
75       tmp = [tmp stringByAppendingPathComponent:self->productDirectoryName];
76       if (![ma containsObject:tmp])
77         [ma addObject:tmp];
78     }
79   }
80 }
81
82 - (void)_addGNUstepSearchPathesToArray:(NSMutableArray *)ma {
83   NSDictionary *env;
84   id tmp;
85   
86   env = [[NSProcessInfo processInfo] environment];
87   if ((tmp = [env objectForKey:@"GNUSTEP_PATHPREFIX_LIST"]) == nil)
88     tmp = [env objectForKey:@"GNUSTEP_PATHLIST"];
89   
90   tmp = [tmp componentsSeparatedByString:@":"];
91   if ([tmp count] > 0) {
92     NSEnumerator *e;
93       
94     e = [tmp objectEnumerator];
95     while ((tmp = [e nextObject])) {
96       tmp = [tmp stringByAppendingPathComponent:@"Library"];
97       tmp = [tmp stringByAppendingPathComponent:self->productDirectoryName];
98       if (![ma containsObject:tmp])
99         [ma addObject:tmp];
100     }
101   }
102   else {
103     NSLog(@"%s: empty library search path !", __PRETTY_FUNCTION__);
104   }
105 }
106
107 - (void)_addFHSPathesToArray:(NSMutableArray *)ma {
108   NSString *s;
109
110   s = @"sogod-0.9";
111   [ma addObject:[@"/usr/local/lib/" stringByAppendingString:s]];
112   [ma addObject:[@"/usr/lib/"       stringByAppendingString:s]];
113 }
114
115 - (NSArray *)productSearchPathes {
116   NSMutableArray *ma;
117   BOOL hasGNUstepEnv;
118   
119   if (self->searchPathes != nil)
120     return self->searchPathes;
121
122   hasGNUstepEnv = [[[[NSProcessInfo processInfo] environment]
123                      objectForKey:@"GNUSTEP_USER_ROOT"] length] > 0 ? YES : NO;
124   
125   ma  = [NSMutableArray arrayWithCapacity:6];
126   
127   if (hasGNUstepEnv)
128     [self _addGNUstepSearchPathesToArray:ma];
129 #if COCOA_Foundation_LIBRARY
130   else
131     [self _addCocoaSearchPathesToArray:ma];
132 #endif
133
134   [self _addFHSPathesToArray:ma];
135   
136   self->searchPathes = [ma copy];
137   
138   if ([self->searchPathes count] == 0) {
139     [self logWithFormat:@"%s: no search pathes were found !", 
140           __PRETTY_FUNCTION__];
141   }
142   
143   return self->searchPathes;
144 }
145
146 - (void)loadProducts {
147   SoProductRegistry *registry = nil;
148   NSFileManager *fm;
149   NSEnumerator *pathes;
150   NSString *lpath, *bpath, *extension;
151   NSEnumerator *productNames;
152   NSString *productName;
153
154   registry = [SoProductRegistry sharedProductRegistry];
155   fm       = [NSFileManager defaultManager];
156  
157   pathes = [[self productSearchPathes] objectEnumerator];
158   lpath = [pathes nextObject];
159   while (lpath)
160     {
161       [self logWithFormat:@"scanning SOGo products in: %@", lpath];
162
163       productNames = [[fm directoryContentsAtPath: lpath] objectEnumerator];
164
165       productName = [productNames nextObject];
166       while (productName)
167         {
168           extension = [productName pathExtension];
169           if ([extension length] > 0
170               && [extension isEqualToString: @"SOGo"])
171             {
172               bpath = [lpath stringByAppendingPathComponent: productName];
173               [self logWithFormat:@"  register SOGo product: %@",
174                     productName];
175               [registry registerProductAtPath: bpath];
176             }
177           productName = [productNames nextObject];
178         }
179
180       lpath = [pathes nextObject];
181     }
182
183   if (![registry loadAllProducts])
184     [self warnWithFormat:@"could not load all products !"];
185 }
186
187 @end /* SOGoProductLoader */