API  0.9.10
CPCib.j
Go to the documentation of this file.
1 /*
2  * CPCib.j
3  * AppKit
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 
25 CPCibOwner = @"CPCibOwner";
26 CPCibTopLevelObjects = @"CPCibTopLevelObjects";
27 CPCibReplacementClasses = @"CPCibReplacementClasses";
28 CPCibExternalObjects = @"CPCibExternalObjects";
29 
30 var CPCibObjectDataKey = @"CPCibObjectDataKey";
31 
36 @implementation CPCib : CPObject
37 {
38  BOOL _awakenCustomResources;
39 
40  CPBundle _bundle;
41  CPData _data;
42  CPString _cibName;
43  id _loadDelegate;
44 }
45 
46 - (id)_initWithData:(CPData)data bundle:(CPBundle)aBundle cibName:(CPString)aCibName
47 {
48  self = [super init];
49 
50  _data = data;
51  _cibName = aCibName;
52  _bundle = aBundle;
53  _awakenCustomResources = YES;
54 
55  return self;
56 }
57 
58 - (id)initWithContentsOfURL:(CPURL)aURL
59 {
60  self = [super init];
61 
62  if (self)
63  {
64  _cibName = [aURL lastPathComponent];
65 
67 
68  if (!_data)
69  return nil;
70 
71  _awakenCustomResources = YES;
72  }
73 
74  return self;
75 }
76 
77 - (id)initWithContentsOfURL:(CPURL)aURL loadDelegate:(id)aLoadDelegate
78 {
79  self = [super init];
80 
81  if (self)
82  {
83  _cibName = [aURL lastPathComponent];
84 
86 
87  _awakenCustomResources = YES;
88 
89  _loadDelegate = aLoadDelegate;
90  }
91 
92  return self;
93 }
94 
95 - (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle
96 {
97  if (![aName hasSuffix:@".cib"])
98  aName = [aName stringByAppendingString:@".cib"];
99 
100  // If aBundle is nil, use mainBundle, but ONLY for searching for the nib, not for resources later.
101  var bundle = aBundle || [CPBundle mainBundle];
102 
103  self = [self initWithContentsOfURL:[bundle _cibPathForResource:aName]];
104 
105  if (self)
106  _bundle = aBundle;
107 
108  return self;
109 }
110 
111 - (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle loadDelegate:(id)aLoadDelegate
112 {
113  if (![aName hasSuffix:@".cib"])
114  aName = [aName stringByAppendingString:@".cib"];
115 
116  // If aBundle is nil, use mainBundle, but ONLY for searching for the nib, not for resources later.
117  var bundle = aBundle || [CPBundle mainBundle];
118 
119  self = [self initWithContentsOfURL:[bundle _cibPathForResource:aName] loadDelegate:aLoadDelegate];
120 
121  if (self)
122  _bundle = aBundle;
123 
124  return self;
125 }
126 
127 - (BOOL)instantiateCibWithExternalNameTable:(CPDictionary)anExternalNameTable
128 {
129  var bundle = _bundle,
130  owner = [anExternalNameTable objectForKey:CPCibOwner];
131 
132  if (!bundle && owner)
133  bundle = [CPBundle bundleForClass:[owner class]];
134 
135  var unarchiver = [[_CPCibKeyedUnarchiver alloc] initForReadingWithData:_data bundle:bundle awakenCustomResources:_awakenCustomResources cibName:_cibName],
136  replacementClasses = [anExternalNameTable objectForKey:CPCibReplacementClasses];
137 
138  if (replacementClasses)
139  {
140  var key = nil,
141  keyEnumerator = [replacementClasses keyEnumerator];
142 
143  while ((key = [keyEnumerator nextObject]) !== nil)
144  [unarchiver setClass:[replacementClasses objectForKey:key] forClassName:key];
145  }
146 
147  [unarchiver setExternalObjectsForProxyIdentifiers:[anExternalNameTable objectForKey:CPCibExternalObjects]];
148 
149  var objectData = [unarchiver decodeObjectForKey:CPCibObjectDataKey];
150 
151  if (!objectData || ![objectData isKindOfClass:[_CPCibObjectData class]])
152  return NO;
153 
154  var topLevelObjects = [anExternalNameTable objectForKey:CPCibTopLevelObjects];
155 
156  [objectData instantiateWithOwner:owner topLevelObjects:topLevelObjects];
157  [objectData establishConnectionsWithOwner:owner topLevelObjects:topLevelObjects];
158  [objectData awakeWithOwner:owner topLevelObjects:topLevelObjects];
159 
160  // Display Visible Windows.
161  [objectData displayVisibleWindows];
162 
163  return YES;
164 }
165 
166 - (BOOL)instantiateCibWithOwner:(id)anOwner topLevelObjects:(CPArray)topLevelObjects
167 {
168  // anOwner can be nil, and we can't store nil in a dictionary. If we leave it out,
169  // anyone who asks for CPCibOwner will get nil back.
170  var nameTable = @{ CPCibTopLevelObjects: topLevelObjects };
171 
172  if (anOwner)
173  [nameTable setObject:anOwner forKey:CPCibOwner];
174 
175  return [self instantiateCibWithExternalNameTable:nameTable];
176 }
177 
178 @end
179 
181 
182 - (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
183 {
184  // FIXME: Why aren't we getting connection:didFailWithError:
185  if (!data)
186  return [self connection:aConnection didFailWithError:nil];
187 
188  _data = [CPData dataWithRawString:data];
189 }
190 
191 - (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
192 {
193  if ([_loadDelegate respondsToSelector:@selector(cibDidFailToLoad:)])
194  [_loadDelegate cibDidFailToLoad:self];
195 
196  _loadDelegate = nil;
197 }
198 
199 - (void)connectionDidFinishLoading:(CPURLConnection)aConnection
200 {
201  if ([_loadDelegate respondsToSelector:@selector(cibDidFinishLoading:)])
202  [_loadDelegate cibDidFinishLoading:self];
203 
204  _loadDelegate = nil;
205 }
206 
207 @end
208 
209 var CPCibDataFileKey = @"CPCibDataFileKey",
210  CPCibBundleIdentifierKey = @"CPCibBundleIdentifierKey";
211 
212 @implementation CPCib (CPCoding)
213 
214 - (id)initWithCoder:(CPCoder)aCoder
215 {
216  self = [super init];
217 
218  var base64 = [aCoder decodeObjectForKey:CPCibDataFileKey];
219  _data = [CPData dataWithBase64:base64];
220 
221  return self;
222 }
223 
224 - (void)encodeWithCoder:(CPCoder)aCoder
225 {
226  [aCoder encodeObject:[_data base64] forKey:CPCibDataFileKey];
227 }
228 
229 @end
230 
232 
236 - (BOOL)_awakenCustomResources
237 {
238  return _awakenCustomResources;
239 }
240 
244 - (void)set_awakenCustomResources:(BOOL)aValue
245 {
246  _awakenCustomResources = aValue;
247 }
248 
249 @end
void connection:didFailWithError:(CPURLConnection aConnection, [didFailWithError] CPError anError)
Definition: CPCib.j:191
Definition: CPCib.h:2
A Cappuccino wrapper for any data type.
Definition: CPData.h:2
Provides loading of a URL request.
CPData sendSynchronousRequest:returningResponse:(CPURLRequest aRequest, [returningResponse]/*{ */CPURLResponse/*} */aURLResponse)
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPCibReplacementClasses
Definition: CPCib.j:27
BOOL instantiateCibWithExternalNameTable:(CPDictionary anExternalNameTable)
Definition: CPCib.j:127
id requestWithURL:(CPURL aURL)
Definition: CPURLRequest.j:56
var CPCibObjectDataKey
Definition: CPCib.j:30
An immutable string (collection of characters).
Definition: CPString.h:2
CPBundle mainBundle()
Definition: CPBundle.j:82
var CPCibDataFileKey
Definition: CPCib.j:209
id objectForKey:(id aKey)
Definition: CPDictionary.j:515
CPData dataWithBase64:(CPString aString)
Definition: CPData.j:73
id initWithContentsOfURL:loadDelegate:(CPURL aURL, [loadDelegate] id aLoadDelegate)
Definition: CPCib.j:77
id initWithContentsOfURL:(CPURL aURL)
Definition: CPCib.j:58
CPCibTopLevelObjects
Definition: CPCib.j:26
CPData dataWithRawString:(CPString aString)
Definition: CPData.j:45
var CPCibBundleIdentifierKey
Definition: CPCib.j:210
Used for encapsulating, presenting, and recovery from errors.
Definition: CPError.h:2
CPCibOwner
Definition: CPCib.j:25
CPString lastPathComponent()
Definition: CPURL.j:193
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
CPURLConnection connectionWithRequest:delegate:(CPURLRequest aRequest, [delegate] id aDelegate)
id init()
Definition: CPObject.j:145
CPBundle bundleForClass:(Class aClass)
Definition: CPBundle.j:77
Contains data obtained during a request made with CPURLConnection.
Definition: CPURLRequest.h:2
CPString stringByAppendingString:(CPString aString)
Definition: CPString.j:221
CPCibExternalObjects
Definition: CPCib.j:28
Definition: CPURL.h:2