Import from SVN r48.
[sparkle2.git] / Framework / NDAlias / NDAlias.m
blob2e8c05c175224a147766d07decc15e352a319bec
1 /*
2  *  NDAlias.m
3  *  NDAliasProject
4  *
5  *  Created by Nathan Day on Thu Feb 07 2002.
6  *  Copyright (c) 2002 Nathan Day. All rights reserved.
7  */
9 #import "NDAlias.h"
10 #import "NSURL+NDCarbonUtilities.h"
12 @interface NDAlias (Private)
13 - (BOOL)createAliasRecordFor:(NSURL *)aURL fromURL:(NSURL *)aFromURL;
14 @end
16 @implementation NDAlias
19  * aliasWithURL:
20  */
21 + (id)aliasWithURL:(NSURL *)aURL
23         return [[[self alloc] initWithURL:aURL] autorelease];
27  * aliasWithURL:fromURL:
28  */
29 + (id)aliasWithURL:(NSURL *)aURL fromURL:(NSURL *)aFromURL
31         return [[[self alloc] initWithURL:aURL fromURL:aFromURL] autorelease];
35  * aliasWithPath:
36  */
37 + (id)aliasWithPath:(NSString *)aPath
39         return [[[self alloc] initWithPath:aPath] autorelease];
43  * aliasWithPath:fromPath:
44  */
45 + (id)aliasWithPath:(NSString *)aPath fromPath:(NSString *)aFromPath
47         return [[[self alloc] initWithPath:aPath fromPath:aFromPath] autorelease];
50 + (id)aliasWithData:(NSData *)aData
52         return [[[self alloc] initWithData:aData] autorelease];
56  * initWithPath:fromPath:
57  */
58 - (id)initWithPath:(NSString *)aPath
60         return [self initWithPath:aPath fromPath:nil];
64  * initWithPath:fromPath:
65  */
66 - (id)initWithPath:(NSString *)aPath fromPath:(NSString *)aFromPath
68         if( aPath && [[NSFileManager defaultManager] fileExistsAtPath:aPath] )
69         {
70                 if( aFromPath && [[NSFileManager defaultManager] fileExistsAtPath:aFromPath] )
71                         return [self initWithURL:[NSURL fileURLWithPath:aPath] fromURL:[NSURL fileURLWithPath:aFromPath]];
72                 else
73                         return [self initWithURL:[NSURL fileURLWithPath:aPath] fromURL:nil];
74         }
75         else
76         {
77                 [self release];
78                 return nil;
79         }
83  * initWithURL:
84  */
85 - (id)initWithURL:(NSURL *)aURL
87         return [self initWithURL:aURL fromURL:nil];
91  * initWithURL:fromURL:
92  */
93 - (id)initWithURL:(NSURL *)aURL fromURL:(NSURL *)aFromURL
95         if( (self = [self init]) != nil )
96         {
97                 if( aURL && [self createAliasRecordFor:aURL fromURL:aFromURL] )
98                 {
99                         changed = false;
100                 }
101                 else
102                 {
103                         [self release];
104                         self = nil;
105                 }
106         }
107         
108         return self;
112  * initWithCoder:
113  */
114 - (id)initWithCoder:(NSCoder *)aDecoder
116         return [self initWithData:[aDecoder decodeDataObject]];
120 - (id)initWithData:(NSData *)aData
122         if( (self = [self init]) != nil )
123         {
124                 if( aData && PtrToHand( [aData bytes], (Handle*)&aliasHandle, [aData length] ) == noErr )
125                 {
126                         changed = false;
127                 }
128                 else
129                 {
130                         [self release];
131                         self = nil;
132                 }
133         }
135         return self;
139  * encodeWithCoder:
140  */
141 - (void)encodeWithCoder:(NSCoder *)anEncoder
143         [anEncoder encodeDataObject:[self data]];       
147  * dealloc
148  */
149 - (void)dealloc
151         DisposeHandle( (Handle)aliasHandle );
152         [super dealloc];
156  * -setAllowUserInteraction:
157  */
158 - (void)setAllowUserInteraction:(BOOL)aFlag
160         mountFlags = aFlag ? (mountFlags & ~kResolveAliasFileNoUI) : (mountFlags | kResolveAliasFileNoUI);
164  * -allowUserInteraction
165  */
166 - (BOOL)allowUserInteraction
168         return mountFlags & kResolveAliasFileNoUI ? NO : YES;
172  * -setTryFileIDFirst:
173  */
174 - (void)setTryFileIDFirst:(BOOL)aFlag
176         mountFlags = aFlag ? (mountFlags | kResolveAliasTryFileIDFirst) : (mountFlags & ~kResolveAliasTryFileIDFirst);
180  * -tryFileIDFirst
181  */
182 - (BOOL)tryFileIDFirst
184         return mountFlags & kResolveAliasTryFileIDFirst ? YES : NO;
188  * url
189  */
190 - (NSURL *)url
192         id                                      theURL = nil;
193         FSRef                           theTarget;
194         OSErr                           theError;
195         if( (theError = FSResolveAliasWithMountFlags( NULL, aliasHandle, &theTarget, &changed, mountFlags )) == noErr )
196         {
197                 theURL = [NSURL URLWithFSRef:&theTarget];
198         }
199         return theURL;
203  * path
204  */
205 - (NSString *)path
207         return [[self url] path];
211  * changed
212  */
213 - (BOOL)changed
215         return changed != false;
219  * setURL:
220  */
221 - (BOOL)setURL:(NSURL *)aURL
223         return [self setURL:aURL fromURL:nil];
227  * setURL:
228  */
229 - (BOOL)setURL:(NSURL *)aURL fromURL:(NSURL *)aFromURL
231         OSErr                                   theError = !noErr;
232         FSRef                                   theReference,
233                                                         theFromReference;
234         
235         if( aURL != nil && [aURL isFileURL] && [aURL getFSRef:&theReference] )
236         {
237                 if( aFromURL != nil && [aFromURL isFileURL] && [aFromURL getFSRef:&theFromReference] )
238                         theError = FSUpdateAlias( &theFromReference, &theReference, aliasHandle, &changed );
239                 else
240                         theError = FSUpdateAlias( NULL, &theReference, aliasHandle, &changed );
241         }
243         return theError == noErr;
247  * setPath:
248  */
249 - (BOOL)setPath:(NSString *)aPath
251         return [self setPath:aPath fromPath:nil];
255  * setPath:fromPath:
256  */
257 - (BOOL)setPath:(NSString *)aPath fromPath:(NSString *)aFromPath
259         BOOL            theSuccess = NO;;
260         if( [[NSFileManager defaultManager] fileExistsAtPath:aPath] )
261         {
262                 if( [[NSFileManager defaultManager] fileExistsAtPath:aFromPath] )
263                         theSuccess = [self setURL:[NSURL fileURLWithPath:aPath] fromURL:[NSURL fileURLWithPath:aFromPath]];
264                 else
265                         theSuccess = [self setURL:[NSURL fileURLWithPath:aPath] fromURL:nil];
266         }
268         return theSuccess;
272  * description
273  */
274 - (NSString *)description
276         return [self path];
279 - (NSData *)data
281         NSData          * theData = nil;
282         if( aliasHandle != NULL )
283         {
284                 HLock((Handle)aliasHandle);
285                 theData = [NSData dataWithBytes:*aliasHandle length:GetHandleSize((Handle) aliasHandle)];
286                 HUnlock((Handle)aliasHandle);
287         }
289         return theData;
292 @end
294 @implementation NDAlias (Private)
297  * createAliasRecordFor:fromURL:
298  */
299 - (BOOL)createAliasRecordFor:(NSURL *)aURL fromURL:(NSURL *)aFromURL
301         OSErr                                   theError = noErr;
302         FSRef                                   theReference,
303                                                         theFromReference;
305         if( aURL != nil && [aURL isFileURL] && [aURL getFSRef:&theReference] )
306         {
307                 if( aFromURL != nil && [aFromURL isFileURL] && [aFromURL getFSRef:&theFromReference] )
308                 {
309                         theError = FSNewAlias( &theFromReference, &theReference, &aliasHandle );
310                 }
311                 else
312                 {
313                         theError = FSNewAliasMinimal( &theReference, &aliasHandle );
314                 }
315         }
317         return theError == noErr;
320 @end