API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CPBundle.j
Go to the documentation of this file.
1 /*
2  * CPBundle.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 CPBundleDidLoadNotification = @"CPBundleDidLoadNotification";
25 
33 
34 @implementation CPBundle : CPObject
35 {
36  CFBundle _bundle;
37  id _delegate;
38 }
39 
40 + (CPBundle)bundleWithURL:(CPURL)aURL
41 {
42  return [[self alloc] initWithURL:aURL];
43 }
44 
45 + (CPBundle)bundleWithPath:(CPString)aPath
46 {
47  return [self bundleWithURL:aPath];
48 }
49 
50 + (CPBundle)bundleForClass:(Class)aClass
51 {
52  return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()];
53 }
54 
55 + (CPBundle)mainBundle
56 {
57  return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()];
58 }
59 
60 - (id)initWithURL:(CPURL)aURL
61 {
62  aURL = new CFURL(aURL);
63 
64  var URLString = aURL.absoluteString(),
65  existingBundle = CPBundlesForURLStrings[URLString];
66 
67  if (existingBundle)
68  return existingBundle;
69 
70  self = [super init];
71 
72  if (self)
73  {
74  _bundle = new CFBundle(aURL);
75  CPBundlesForURLStrings[URLString] = self;
76  }
77 
78  return self;
79 }
80 
81 - (id)initWithPath:(CPString)aPath
82 {
83  return [self initWithURL:aPath];
84 }
85 
86 - (Class)classNamed:(CPString)aString
87 {
88  // ???
89 }
90 
91 - (CPURL)bundleURL
92 {
93  return _bundle.bundleURL();
94 }
95 
96 - (CPString)bundlePath
97 {
98  return [[self bundleURL] path];
99 }
100 
101 - (CPString)resourcePath
102 {
103  return [[self resourceURL] path];
104 }
105 
106 - (CPURL)resourceURL
107 {
108  return _bundle.resourcesDirectoryURL();
109 }
110 
111 - (Class)principalClass
112 {
113  var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
114 
115  //[self load];
116 
117  return className ? CPClassFromString(className) : Nil;
118 }
119 
120 - (CPString)bundleIdentifier
121 {
122  return [self objectForInfoDictionaryKey:@"CPBundleIdentifier"];
123 }
124 
125 - (BOOL)isLoaded
126 {
127  return _bundle.isLoaded();
128 }
129 
130 - (CPString)pathForResource:(CPString)aFilename
131 {
132  return _bundle.pathForResource(aFilename);
133 }
134 
135 - (CPDictionary)infoDictionary
136 {
137  return _bundle.infoDictionary();
138 }
139 
140 - (id)objectForInfoDictionaryKey:(CPString)aKey
141 {
142  return _bundle.valueForInfoDictionaryKey(aKey);
143 }
144 
145 - (void)loadWithDelegate:(id)aDelegate
146 {
147  _delegate = aDelegate;
148 
149  _bundle.addEventListener("load", function()
150  {
151  [_delegate bundleDidFinishLoading:self];
152  // userInfo should contain a list of all classes loaded from this bundle. When writing this there
153  // seems to be no efficient way to get it though.
154  [[CPNotificationCenter defaultCenter] postNotificationName:CPBundleDidLoadNotification object:self userInfo:nil];
155  });
156 
157  _bundle.addEventListener("error", function()
158  {
159  CPLog.error("Could not find bundle: " + self);
160  });
161 
162  _bundle.load(YES);
163 }
164 
165 - (CPArray)staticResourceURLs
166 {
167  var staticResourceURLs = [],
168  staticResources = _bundle.staticResources(),
169  index = 0,
170  count = [staticResources count];
171 
172  for (; index < count; ++index)
173  [staticResourceURLs addObject:staticResources[index].URL()];
174 
175  return staticResourceURLs;
176 }
177 
178 - (CPArray)environments
179 {
180  return _bundle.environments();
181 }
182 
183 - (CPString)mostEligibleEnvironment
184 {
185  return _bundle.mostEligibleEnvironment();
186 }
187 
188 - (CPString)description
189 {
190  return [super description] + "(" + [self bundlePath] + ")";
191 }
192 
193 @end