![]() |
API 0.9.5
|
00001 /* 00002 * CPBundle.j 00003 * Foundation 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00024 CPBundleDidLoadNotification = @"CPBundleDidLoadNotification"; 00025 00032 var CPBundlesForURLStrings = { }; 00033 00034 @implementation CPBundle : CPObject 00035 { 00036 CFBundle _bundle; 00037 id _delegate; 00038 } 00039 00040 + (CPBundle)bundleWithURL:(CPURL)aURL 00041 { 00042 return [[self alloc] initWithURL:aURL]; 00043 } 00044 00045 + (CPBundle)bundleWithPath:(CPString)aPath 00046 { 00047 return [self bundleWithURL:aPath]; 00048 } 00049 00050 + (CPBundle)bundleForClass:(Class)aClass 00051 { 00052 return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()]; 00053 } 00054 00055 + (CPBundle)mainBundle 00056 { 00057 return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()]; 00058 } 00059 00060 - (id)initWithURL:(CPURL)aURL 00061 { 00062 aURL = new CFURL(aURL); 00063 00064 var URLString = aURL.absoluteString(), 00065 existingBundle = CPBundlesForURLStrings[URLString]; 00066 00067 if (existingBundle) 00068 return existingBundle; 00069 00070 self = [super init]; 00071 00072 if (self) 00073 { 00074 _bundle = new CFBundle(aURL); 00075 CPBundlesForURLStrings[URLString] = self; 00076 } 00077 00078 return self; 00079 } 00080 00081 - (id)initWithPath:(CPString)aPath 00082 { 00083 return [self initWithURL:aPath]; 00084 } 00085 00086 - (Class)classNamed:(CPString)aString 00087 { 00088 // ??? 00089 } 00090 00091 - (CPURL)bundleURL 00092 { 00093 return _bundle.bundleURL(); 00094 } 00095 00096 - (CPString)bundlePath 00097 { 00098 return [[self bundleURL] path]; 00099 } 00100 00101 - (CPString)resourcePath 00102 { 00103 return [[self resourceURL] path]; 00104 } 00105 00106 - (CPURL)resourceURL 00107 { 00108 return _bundle.resourcesDirectoryURL(); 00109 } 00110 00111 - (Class)principalClass 00112 { 00113 var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"]; 00114 00115 //[self load]; 00116 00117 return className ? CPClassFromString(className) : Nil; 00118 } 00119 00120 - (CPString)bundleIdentifier 00121 { 00122 return [self objectForInfoDictionaryKey:@"CPBundleIdentifier"]; 00123 } 00124 00125 - (BOOL)isLoaded 00126 { 00127 return _bundle.isLoaded(); 00128 } 00129 00130 - (CPString)pathForResource:(CPString)aFilename 00131 { 00132 return _bundle.pathForResource(aFilename); 00133 } 00134 00135 - (CPDictionary)infoDictionary 00136 { 00137 return _bundle.infoDictionary(); 00138 } 00139 00140 - (id)objectForInfoDictionaryKey:(CPString)aKey 00141 { 00142 return _bundle.valueForInfoDictionaryKey(aKey); 00143 } 00144 00145 - (void)loadWithDelegate:(id)aDelegate 00146 { 00147 _delegate = aDelegate; 00148 00149 _bundle.addEventListener("load", function() 00150 { 00151 [_delegate bundleDidFinishLoading:self]; 00152 // userInfo should contain a list of all classes loaded from this bundle. When writing this there 00153 // seems to be no efficient way to get it though. 00154 [[CPNotificationCenter defaultCenter] postNotificationName:CPBundleDidLoadNotification object:self userInfo:nil]; 00155 }); 00156 00157 _bundle.addEventListener("error", function() 00158 { 00159 CPLog.error("Could not find bundle: " + self); 00160 }); 00161 00162 _bundle.load(YES); 00163 } 00164 00165 - (CPArray)staticResourceURLs 00166 { 00167 var staticResourceURLs = [], 00168 staticResources = _bundle.staticResources(), 00169 index = 0, 00170 count = [staticResources count]; 00171 00172 for (; index < count; ++index) 00173 [staticResourceURLs addObject:staticResources[index].URL()]; 00174 00175 return staticResourceURLs; 00176 } 00177 00178 - (CPArray)environments 00179 { 00180 return _bundle.environments(); 00181 } 00182 00183 - (CPString)mostEligibleEnvironment 00184 { 00185 return _bundle.mostEligibleEnvironment(); 00186 } 00187 00188 - (CPString)description 00189 { 00190 return [super description] + "(" + [self bundlePath] + ")"; 00191 } 00192 00193 @end