Re: mounting idisk?
Re: mounting idisk?
- Subject: Re: mounting idisk?
- From: Steve Gehrman <email@hidden>
- Date: Tue, 29 Apr 2003 23:59:30 -0700
>
Can anyone explain how to programatically mount an iDisk using webdav
>
(and here's the kicker) without requiring user input (i.e. without
>
asking the user for a password)?
call like this:
[NTVolumeMounter mountVolumeWithScheme:@"http" host:@"idisk.mac.com"
path:userName user:userName password:password openInNewWindow:YES];
here's some code (it's in there somewhere)
- (id)init;
{
self = [super init];
FSCreateVolumeOperation(&_volumeOp);
return self;
}
- (void)dealloc;
{
FSDisposeVolumeOperation(_volumeOp);
if (_mountUPP)
DisposeFSVolumeMountUPP(_mountUPP);
if (_unmountUPP)
DisposeFSVolumeUnmountUPP(_unmountUPP);
if (_ejectUPP)
DisposeFSVolumeEjectUPP(_ejectUPP);
[_url release];
[_desc release];
[super dealloc];
}
+ (void)eject:(NTFileDesc*)desc
{
if ([desc isEjectable])
[NTVolumeMounter ejectVolume:desc];
else if ([desc isVolume])
[NTVolumeMounter unmountVolume:desc];
}
+ (void)mountVolumeWithURL:(NSURL*)url user:(NSString*)user
password:(NSString*)password openInNewWindow:(BOOL)openInNewWindow;
{
NTVolumeMounter* result = [[NTVolumeMounter alloc] init];
// this object autoreleases itself when done
[result doMountVolumeWithURL:url user:user password:password
openInNewWindow:openInNewWindow];
}
+ (void)unmountVolume:(NTFileDesc*)desc;
{
NTVolumeMounter* result = [[NTVolumeMounter alloc] init];
// this object autoreleases itself when done
[result doUnmountVolumeWithDesc:desc];
}
+ (void)ejectVolume:(NTFileDesc*)desc;
{
NTVolumeMounter* result = [[NTVolumeMounter alloc] init];
// this object autoreleases itself when done
[result doEjectVolumeWithDesc:desc];
}
+ (void)mountVolumeWithScheme:(NSString*)scheme host:(NSString*)host
path:(NSString*)path user:(NSString*)user password:(NSString*)password
openInNewWindow:(BOOL)openInNewWindow;
{
NSString* urlString;
if (path && [path length])
urlString = [NSString stringWithFormat:@"%@://%@/%@", scheme,
host, path];
else
urlString = [NSString stringWithFormat:@"%@://%@", scheme,
host];
NS_DURING
[NTVolumeMounter mountVolumeWithURL:[[[NSURL alloc]
initWithString:urlString] autorelease] user:user password:password
openInNewWindow:openInNewWindow];
NS_HANDLER
;
NS_ENDHANDLER
}
@end
@implementation NTVolumeMounter (Private)
void volumeMountCallback(FSVolumeOperation volumeOp, void *clientData,
OSStatus err, FSVolumeRefNum mountedVolumeRefNum)
{
NTVolumeMounter* mounter = (NTVolumeMounter*)clientData;
NTFileDesc *volumeDesc=nil;
if (err != noErr)
{
// this error comes up when the volume is already mounted
if (err == fBsyErr)
{
volumeDesc = [mounter volumeForURL:[mounter url]];
if (volumeDesc && [volumeDesc isValid])
[NTRevealer revealItem:volumeDesc inNewWindow:YES];
}
else
{
NSString *messageString = NSLocalizedString(@"An error
occurred while trying to mount \"%@\".", @"");
NSString *errorString = NSLocalizedString(@"Error: %d",
@"");
messageString = [NSString stringWithFormat:messageString,
[mounter url]];
errorString = [NSString stringWithFormat:errorString, err];
[NTSimpleAlert alertPanel:messageString
subMessage:errorString];
}
}
else if ([mounter openInNewWindow] && mountedVolumeRefNum != 0)
{
volumeDesc = [NTFileDesc descVolumeRefNum:mountedVolumeRefNum];
if (volumeDesc && [volumeDesc isValid])
{
[mounter setVolume:volumeDesc forURL:[mounter url]];
[NTRevealer revealItem:volumeDesc inNewWindow:YES];
}
}
[mounter autorelease];
}
void volumeUnmountCallback(FSVolumeOperation volumeOp, void
*clientData, OSStatus err, FSVolumeRefNum volumeRefNum, pid_t dissenter)
{
NTVolumeMounter* mounter = (NTVolumeMounter*)clientData;
if (err != noErr)
{
NTFileDesc* volumeDesc = [mounter desc];
NSString *messageString = NSLocalizedString(@"An error occurred
while trying to unmount \"%@\".", @"");
NSString *errorString = NSLocalizedString(@"Error: %d", @"");
messageString = [NSString stringWithFormat:messageString,
[volumeDesc volumeName]];
errorString = [NSString stringWithFormat:errorString, err];
[NTSimpleAlert alertPanel:messageString subMessage:errorString];
}
[mounter autorelease];
}
void volumeEjectCallback(FSVolumeOperation volumeOp, void *clientData,
OSStatus err, FSVolumeRefNum volumeRefNum, pid_t dissenter)
{
NTVolumeMounter* mounter = (NTVolumeMounter*)clientData;
if (err != noErr)
{
NTFileDesc* volumeDesc = [mounter desc];
NSString *messageString = NSLocalizedString(@"An error occurred
while trying to eject \"%@\".", @"");
NSString *errorString = NSLocalizedString(@"Error: %d", @"");
messageString = [NSString stringWithFormat:messageString,
[volumeDesc volumeName]];
errorString = [NSString stringWithFormat:errorString, err];
[NTSimpleAlert alertPanel:messageString subMessage:errorString];
}
[mounter autorelease];
}
- (void)doMountVolumeWithURL:(NSURL*)url user:(NSString*)user
password:(NSString*)password openInNewWindow:(BOOL)openInNewWindow;
{
OSStatus err;
CFStringRef userRef=nil, passRef=nil;
_url = [url retain];
_openInNewWindow = openInNewWindow;
// if no user name given, user the login name
if (!user || ![user length])
user = NSUserName();
if (![_url user])
{
if (user && [user length])
userRef = (CFStringRef)user;
}
if (![_url password])
{
if (password && [password length])
passRef = (CFStringRef)password;
}
_mountUPP = NewFSVolumeMountUPP(volumeMountCallback);
err = FSMountServerVolumeAsync((CFURLRef)_url, (CFURLRef)NULL,
userRef, passRef, _volumeOp, self, 0, _mountUPP, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
}
- (void)doUnmountVolumeWithDesc:(NTFileDesc*)desc;
{
OSStatus err;
_desc = [desc retain];
_unmountUPP = NewFSVolumeUnmountUPP(volumeUnmountCallback);
// SNG the Async calls are fucked!!
if (NO)
err = FSUnmountVolumeAsync([desc volumeRefNum], 0, _volumeOp,
self, _unmountUPP, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
else
{
err = FSUnmountVolumeSync([desc volumeRefNum], 0, NULL);
InvokeFSVolumeEjectUPP(_volumeOp, self, err, [desc
volumeRefNum], 0, _unmountUPP);
}
}
- (void)doEjectVolumeWithDesc:(NTFileDesc*)desc;
{
OSStatus err;
_desc = [desc retain];
_ejectUPP = NewFSVolumeEjectUPP(volumeEjectCallback);
// SNG the Async calls are fucked!!
if (NO)
err = FSEjectVolumeAsync([desc volumeRefNum], 0, _volumeOp,
self, _ejectUPP, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
else
{
err = FSEjectVolumeSync([desc volumeRefNum], 0, NULL);
InvokeFSVolumeEjectUPP(_volumeOp, self, err, [desc
volumeRefNum], 0, _ejectUPP);
}
}
- (NSURL*)url;
{
return _url;
}
- (NTFileDesc*)desc;
{
return _desc;
}
static NSMutableDictionary *sVolumeURLDictionary=nil;
- (NTFileDesc*)volumeForURL:(NSURL*)url;
{
NTFileDesc* result = nil;
if (sVolumeURLDictionary)
{
NSString* path = [sVolumeURLDictionary objectForKey:url];
if (path)
{
result = [NTFileDesc descNoResolve:path];
if (![result isValid])
result = nil; // it's autoreleased, so nil is fine
}
}
return result;
}
- (void)setVolume:(NTFileDesc*)volumeDesc forURL:(NSURL*)url;
{
if (!sVolumeURLDictionary)
sVolumeURLDictionary = [[NSMutableDictionary alloc] init];
[sVolumeURLDictionary setObject:[volumeDesc path] forKey:url];
}
- (BOOL)openInNewWindow;
{
return _openInNewWindow;
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.