1/*
2  Copyright (C) 2004 SKYRIX Software AG
3  Copyright (C) 2005-2016 Inverse inc.
4
5  This file is part of SOGo
6
7  SOGo is free software; you can redistribute it and/or modify it under
8  the terms of the GNU Lesser General Public License as published by the
9  Free Software Foundation; either version 2, or (at your option) any
10  later version.
11
12  SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
13  WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15  License for more details.
16
17  You should have received a copy of the GNU Lesser General Public
18  License along with OGo; see the file COPYING.  If not, write to the
19  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  02111-1307, USA.
21*/
22
23#import <Foundation/NSArray.h>
24#import <Foundation/NSAutoreleasePool.h>
25#import <Foundation/NSDictionary.h>
26#import <Foundation/NSFileManager.h>
27#import <Foundation/NSPathUtilities.h>
28#import <Foundation/NSProcessInfo.h>
29
30#import <NGObjWeb/SoProductRegistry.h>
31#import <NGExtensions/NSObject+Logs.h>
32
33#import "SOGoProductLoader.h"
34
35static NSString *productDirectoryName = @"SOGo";
36
37@implementation SOGoProductLoader
38
39+ (id) productLoader
40{
41  return [[self new] autorelease];
42}
43
44- (void) dealloc
45{
46  [searchPathes release];
47  [super dealloc];
48}
49
50/* loading */
51
52- (void) _addCocoaSearchPathesToArray: (NSMutableArray *) ma
53{
54  id tmp;
55  NSEnumerator *e;
56
57  tmp = NSSearchPathForDirectoriesInDomains (NSAllLibrariesDirectory,
58					     NSAllDomainsMask,
59					     YES);
60  if ([tmp count] > 0)
61    {
62      e = [tmp objectEnumerator];
63      while ((tmp = [e nextObject]))
64	{
65	  tmp = [tmp stringByAppendingPathComponent: productDirectoryName];
66	  if (![ma containsObject: tmp])
67	    [ma addObject: tmp];
68	}
69    }
70}
71
72- (void) _addGNUstepSearchPathesToArray: (NSMutableArray *) ma
73{
74  NSEnumerator *libraryPaths;
75  NSString *directory;
76
77  libraryPaths = [NSStandardLibraryPaths() objectEnumerator];
78  while ((directory = [libraryPaths nextObject]))
79    [ma addObject:
80	  [directory stringByAppendingPathComponent: productDirectoryName]];
81}
82
83- (NSArray *) productSearchPathes
84{
85  NSMutableArray *ma;
86
87  if (!searchPathes)
88    {
89#warning we should work on searchPathes directly instead of ma
90      ma = [NSMutableArray arrayWithCapacity: 6];
91
92      [self _addGNUstepSearchPathesToArray: ma];
93#if COCOA_Foundation_LIBRARY
94      [self _addCocoaSearchPathesToArray: ma];
95#endif
96
97      searchPathes = [ma copy];
98
99      if ([searchPathes count] == 0)
100	[self logWithFormat: @"%s: no search pathes were found !",
101	      __PRETTY_FUNCTION__];
102    }
103
104  return searchPathes;
105}
106
107- (void) loadAllProducts: (BOOL) verbose
108{
109  SoProductRegistry *registry = nil;
110  NSFileManager *fm;
111  NSMutableArray *loadedProducts;
112  NSEnumerator *pathes;
113  NSString *lpath, *bpath;
114  NSEnumerator *productNames;
115  NSString *productName;
116  NSAutoreleasePool *pool;
117
118  pool = [NSAutoreleasePool new];
119
120  registry = [SoProductRegistry sharedProductRegistry];
121  fm = [NSFileManager defaultManager];
122
123  loadedProducts = [NSMutableArray array];
124
125  pathes = [[self productSearchPathes] objectEnumerator];
126  while ((lpath = [pathes nextObject]))
127    {
128      productNames = [[fm directoryContentsAtPath: lpath] objectEnumerator];
129      while ((productName = [productNames nextObject]))
130	{
131          if ([[productName pathExtension] isEqualToString: @"SOGo"])
132            {
133	      bpath = [lpath stringByAppendingPathComponent: productName];
134	      [registry registerProductAtPath: bpath];
135              [loadedProducts addObject: productName];
136	    }
137	}
138      if ([loadedProducts count])
139        {
140	  if (verbose)
141	    {
142	      [self logWithFormat: @"SOGo products loaded from '%@':", lpath];
143	      [self logWithFormat: @"  %@",
144		    [loadedProducts componentsJoinedByString: @", "]];
145	    }
146          [loadedProducts removeAllObjects];
147        }
148    }
149
150  if (![registry loadAllProducts] && verbose)
151    [self warnWithFormat: @"could not load all products !"];
152  [pool release];
153}
154
155- (void) loadProducts: (NSArray *) products
156{
157  SoProductRegistry *registry = nil;
158  NSFileManager *fm;
159  NSEnumerator *pathes;
160  NSString *lpath, *bpath;
161  NSEnumerator *productNames;
162  NSString *productName;
163  NSAutoreleasePool *pool;
164
165  pool = [NSAutoreleasePool new];
166
167  registry = [SoProductRegistry sharedProductRegistry];
168  fm = [NSFileManager defaultManager];
169
170  pathes = [[self productSearchPathes] objectEnumerator];
171  while ((lpath = [pathes nextObject]))
172    {
173      productNames = [[fm directoryContentsAtPath: lpath] objectEnumerator];
174      while ((productName = [productNames nextObject]))
175	{
176          if ([products containsObject: productName])
177            {
178	      bpath = [lpath stringByAppendingPathComponent: productName];
179	      [registry registerProductAtPath: bpath];
180	    }
181	}
182    }
183
184  if (![registry loadAllProducts])
185    [self warnWithFormat: @"could not load all products !"];
186  [pool release];
187}
188
189@end /* SOGoProductLoader */
190