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