// Copyright 2003 - 2004 Nathan Hamblen
// This file based in part on InternetLocationCM.cp from Abracode's Contextual Menu Workshop. Its license appears below
/* Contextual Menu Workshop Copyright (C) 2002-2003 Abracode Inc This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Some parts of this software is based on preexisting code for which Abracode Inc does not claim copyright. Look individual files for detailed information. You must respect the copyright of original authors and not remove the notices from these files. */


#import "WebLocationWrapper.h"
#import "NSString+CarbonFSSpecCreation.h"

ResType urlResourceType = 'url ';

// This subclass of NSFileWrapper ja; read and write capability for internet location resources.
@implementation WebLocationWrapper

// Inits as normal with the superclass, then uses carbon methods to retrieve any url resources that are present.
- (WebLocationWrapper *)initWithPath:(NSString *)path
{
  FSSpec fileSpec;
  short refNum;

  if (!(self = [super initWithPath:path]))
    return nil;

  if (![path getFSSpec:&fileSpec createFileIfNecessary:NO])
    return self;

  refNum = FSpOpenResFile( &fileSpec, fsRdPerm );
  if( ResError() == noErr )
  {
    short iCnt = Count1Resources(urlResourceType);
    if(iCnt > 0)
    {
      Handle hRsrc = Get1IndResource( urlResourceType, 1);
      if(ResError() == noErr && GetHandleSize(hRsrc) > 0 )
      {
        mURL = [[NSString alloc] initWithCString: (char *) *hRsrc
                                         length:GetHandleSize(hRsrc)];
      }
      else
        NSLog(@"No url resources found");
    }
    CloseResFile(refNum);
  }
  else
    NSLog(@"Unable to open resource.");
  return self;
}

- (WebLocationWrapper *)initRegularFileWithContents:(NSData *)data
{
  return (self = [super initRegularFileWithContents:data]);
}

- (NSString *)url
{
  return mURL;
}

- (void) setUrl:(NSString *)newUrl
{
  if (mURL)
    [mURL release];
  mURL = [newUrl retain];
}

// This method expects the file to already be a proper internet location file. Errors will result and it will return NO if the correct resources can not be found.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)atomicFlag
    updateFilenames:(BOOL)updateNamesFlag
{
  FSSpec fileSpec;
  Handle hUrl, hText;
  short refNum;
  BOOL status;

  if (![super writeToFile:path atomically:atomicFlag updateFilenames:updateNamesFlag])
  {
    NSLog(@"Unable to write standard wrapper.");
    return NO;
  }

  if (![path getFSSpec:&fileSpec createFileIfNecessary:NO])
  {
    NSLog(@"Unable to retrieve carbon file spec.");
    return NO;
  }
  refNum = FSpOpenResFile( &fileSpec, fsRdWrPerm );
  if( ResError() != noErr )
  {
    NSLog(@"Unable to open resource.");
    return NO;
  }
  hUrl = Get1IndResource(urlResourceType, 1);
  hText = Get1IndResource(kScrapFlavorTypeText, 1);

  PtrToXHand((Ptr) [mURL cString], hUrl, [mURL cStringLength]);
  PtrToXHand((Ptr) [mURL cString], hText, [mURL cStringLength]);

  ChangedResource(hUrl);
  status = ResError() == noErr;
  ChangedResource(hText);
  status = status && (ResError() == noErr);
  if(!status)
    NSLog(@"Unable to change resource.");
  CloseResFile(refNum);
  if (ResError() != noErr)
  {
    NSLog(@"Error closing resource file.");
    return NO;
  }
  return status;
}

- (void)dealloc {
  if (mURL)
    [mURL release];
}

@end