]> err.no Git - scalable-opengroupware.org/blob - Main/SOGoProductLoader.m
Install libs to /usr/lib
[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   NSEnumerator *libraryPaths;
84   NSString *directory;
85
86   libraryPaths = [NSStandardLibraryPaths() objectEnumerator];
87   while ((directory = [libraryPaths nextObject]))
88     [ma addObject: [directory stringByAppendingPathComponent:self->productDirectoryName]];
89 }
90
91 - (void)_addFHSPathesToArray:(NSMutableArray *)ma {
92   NSString *s;
93
94   s = @"sogod-0.9";
95   [ma addObject:[@"/usr/local/lib/" stringByAppendingString:s]];
96   [ma addObject:[@"/usr/lib/"       stringByAppendingString:s]];
97 }
98
99 - (NSArray *)productSearchPathes {
100   NSMutableArray *ma;
101   BOOL hasGNUstepEnv;
102   
103   if (self->searchPathes != nil)
104     return self->searchPathes;
105
106   hasGNUstepEnv = [[[[NSProcessInfo processInfo] environment]
107                      objectForKey:@"GNUSTEP_USER_ROOT"] length] > 0 ? YES : NO;
108   
109   ma  = [NSMutableArray arrayWithCapacity:6];
110   
111   if (hasGNUstepEnv)
112     [self _addGNUstepSearchPathesToArray:ma];
113 #if COCOA_Foundation_LIBRARY
114   else
115     [self _addCocoaSearchPathesToArray:ma];
116 #endif
117
118   [self _addFHSPathesToArray:ma];
119   
120   self->searchPathes = [ma copy];
121   
122   if ([self->searchPathes count] == 0) {
123     [self logWithFormat:@"%s: no search pathes were found !", 
124           __PRETTY_FUNCTION__];
125   }
126   
127   return self->searchPathes;
128 }
129
130 - (void)loadProducts {
131   SoProductRegistry *registry = nil;
132   NSFileManager *fm;
133   NSEnumerator *pathes;
134   NSString *lpath, *bpath, *extension;
135   NSEnumerator *productNames;
136   NSString *productName;
137
138   registry = [SoProductRegistry sharedProductRegistry];
139   fm       = [NSFileManager defaultManager];
140  
141   pathes = [[self productSearchPathes] objectEnumerator];
142   lpath = [pathes nextObject];
143   while (lpath)
144     {
145       [self logWithFormat:@"scanning SOGo products in: %@", lpath];
146
147       productNames = [[fm directoryContentsAtPath: lpath] objectEnumerator];
148
149       productName = [productNames nextObject];
150       while (productName)
151         {
152           extension = [productName pathExtension];
153           if ([extension length] > 0
154               && [extension isEqualToString: @"SOGo"])
155             {
156               bpath = [lpath stringByAppendingPathComponent: productName];
157               [self logWithFormat:@"  register SOGo product: %@",
158                     productName];
159               [registry registerProductAtPath: bpath];
160             }
161           productName = [productNames nextObject];
162         }
163
164       lpath = [pathes nextObject];
165     }
166
167   if (![registry loadAllProducts])
168     [self warnWithFormat:@"could not load all products !"];
169 }
170
171 @end /* SOGoProductLoader */