API  0.9.8
 All Classes Files Functions Variables Typedefs 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 
32 
33 @implementation CPBundle : CPObject
34 {
35  CFBundle _bundle;
36  id _delegate;
37 }
38 
39 + (CPBundle)bundleWithURL:(CPURL)aURL
40 {
41  return [[self alloc] initWithURL:aURL];
42 }
43 
44 + (CPBundle)bundleWithPath:(CPString)aPath
45 {
46  return [self bundleWithURL:aPath];
47 }
48 
49 + (CPBundle)bundleWithIdentifier:(CPString)anIdentifier
50 {
51  var bundle = CFBundle.bundleWithIdentifier(anIdentifier);
52 
53  if (bundle)
54  {
55  var url = bundle.bundleURL(),
56  cpBundle = CPBundlesForURLStrings[url.absoluteString()];
57 
58  if (!cpBundle)
59  cpBundle = [self bundleWithURL:url];
60 
61  return cpBundle;
62  }
63 
64  return nil;
65 }
66 
67 + (CPBundle)bundleForClass:(Class)aClass
68 {
69  return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()];
70 }
71 
72 + (CPBundle)mainBundle
73 {
74  return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()];
75 }
76 
77 - (id)initWithURL:(CPURL)aURL
78 {
79  aURL = new CFURL(aURL);
80 
81  var URLString = aURL.absoluteString(),
82  existingBundle = CPBundlesForURLStrings[URLString];
83 
84  if (existingBundle)
85  return existingBundle;
86 
87  self = [super init];
88 
89  if (self)
90  {
91  _bundle = new CFBundle(aURL);
92  CPBundlesForURLStrings[URLString] = self;
93  }
94 
95  return self;
96 }
97 
98 - (id)initWithPath:(CPString)aPath
99 {
100  return [self initWithURL:aPath];
101 }
102 
103 - (Class)classNamed:(CPString)aString
104 {
105  // ???
106 }
107 
108 - (CPURL)bundleURL
109 {
110  return _bundle.bundleURL();
111 }
112 
113 - (CPString)bundlePath
114 {
115  return [[self bundleURL] path];
116 }
117 
118 - (CPString)resourcePath
119 {
120  return [[self resourceURL] path];
121 }
122 
123 - (CPURL)resourceURL
124 {
125  return _bundle.resourcesDirectoryURL();
126 }
127 
128 - (Class)principalClass
129 {
130  var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
131 
132  //[self load];
133 
134  return className ? CPClassFromString(className) : nil;
135 }
136 
137 - (CPString)bundleIdentifier
138 {
139  return _bundle.identifier();
140 }
141 
142 - (BOOL)isLoaded
143 {
144  return _bundle.isLoaded();
145 }
146 
147 - (CPString)pathForResource:(CPString)aFilename
148 {
149  return _bundle.pathForResource(aFilename);
150 }
151 
152 - (CPDictionary)infoDictionary
153 {
154  return _bundle.infoDictionary();
155 }
156 
157 - (id)objectForInfoDictionaryKey:(CPString)aKey
158 {
159  return _bundle.valueForInfoDictionaryKey(aKey);
160 }
161 
162 - (void)loadWithDelegate:(id)aDelegate
163 {
164  _delegate = aDelegate;
165 
166  _bundle.addEventListener("load", function()
167  {
168  [_delegate bundleDidFinishLoading:self];
169  // userInfo should contain a list of all classes loaded from this bundle. When writing this there
170  // seems to be no efficient way to get it though.
171  [[CPNotificationCenter defaultCenter] postNotificationName:CPBundleDidLoadNotification object:self userInfo:nil];
172  });
173 
174  _bundle.addEventListener("error", function()
175  {
176  CPLog.error("Could not find bundle: " + self);
177  });
178 
179  _bundle.load(YES);
180 }
181 
182 - (CPArray)staticResourceURLs
183 {
184  var staticResourceURLs = [],
185  staticResources = _bundle.staticResources(),
186  index = 0,
187  count = [staticResources count];
188 
189  for (; index < count; ++index)
190  [staticResourceURLs addObject:staticResources[index].URL()];
191 
192  return staticResourceURLs;
193 }
194 
195 - (CPArray)environments
196 {
197  return _bundle.environments();
198 }
199 
200 - (CPString)mostEligibleEnvironment
201 {
202  return _bundle.mostEligibleEnvironment();
203 }
204 
205 - (CPString)description
206 {
207  return [super description] + "(" + [self bundlePath] + ")";
208 }
209 
210 @end