15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* ***** BEGIN LICENSE BLOCK *****
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Version: MPL 1.1/GPL 2.0/LGPL 2.1
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The contents of this file are subject to the Mozilla Public License Version
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1.1 (the "License"); you may not use this file except in compliance with
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the License. You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * http://www.mozilla.org/MPL/
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Software distributed under the License is distributed on an "AS IS" basis,
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * for the specific language governing rights and limitations under the
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * License.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The Original Code is Camino code.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The Initial Developer of the Original Code is
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Netscape Communications Corporation.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Portions created by the Initial Developer are Copyright (C) 2002
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the Initial Developer. All Rights Reserved.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Contributor(s):
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   Nate Weaver (Wevah) - wevah@derailer.org
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Alternatively, the contents of this file may be used under the terms of
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * either the GNU General Public License Version 2 or later (the "GPL"), or
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in which case the provisions of the GPL or the LGPL are applicable instead
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * of those above. If you wish to allow use of your version of this file only
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * under the terms of either the GPL or the LGPL, and not to allow others to
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * use your version of this file under the terms of the MPL, indicate your
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * decision by deleting the provisions above and replace them with the notice
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * and other provisions required by the GPL or the LGPL. If you do not delete
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the provisions above, a recipient may use your version of this file under
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the terms of any one of the MPL, the GPL or the LGPL.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ***** END LICENSE BLOCK ***** */
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#import "NSURL+Utils.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)@implementation NSURL (CaminoExtensions)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)+ (NSURL*)decodeLocalFileURL:(NSURL*)url
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSString* urlPathString = [url path];
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSString* ext = [[urlPathString pathExtension] lowercaseString];
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OSType fileType = NSHFSTypeCodeFromFileType(NSHFSTypeOfFile(urlPathString));
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ([ext isEqualToString:@"url"] || fileType == 'LINK') {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url = [NSURL URLFromIEURLFile:urlPathString];
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else if ([ext isEqualToString:@"webloc"] || [ext isEqualToString:@"ftploc"] ||
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           fileType == 'ilht' || fileType == 'ilft')
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url = [NSURL URLFromInetloc:urlPathString];
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return url;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the URL from a .webloc/.ftploc file.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the URL, or nil on failure.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)+(NSURL*)URLFromInetloc:(NSString*)inFile
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FSRef ref;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSURL *ret = nil;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (inFile && FSPathMakeRef((UInt8 *)[inFile fileSystemRepresentation], &ref, NULL) == noErr) {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    short resRef;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    resRef = FSOpenResFile(&ref, fsRdPerm);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (resRef != -1) { // Has resouce fork.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Handle urlResHandle;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if ((urlResHandle = Get1Resource('url ', 256))) { // Has 'url ' resource with ID 256.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        long size;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        size = GetMaxResourceSize(urlResHandle);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Begin Google Modified
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//        ret = [NSURL URLWithString:[NSString stringWithCString:(char *)*urlResHandle length:size]];
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NSString *urlString = [[[NSString alloc] initWithBytes:(void *)*urlResHandle
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                        length:size
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                      encoding:NSMacOSRomanStringEncoding]  // best guess here
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               autorelease];
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ret = [NSURL URLWithString:urlString];
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// End Google Modified
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CloseResFile(resRef);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!ret) { // Look for valid plist data.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NSDictionary *plist;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if ((plist = [[NSDictionary alloc] initWithContentsOfFile:inFile])) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ret = [NSURL URLWithString:[plist objectForKey:@"URL"]];
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        [plist release];
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ret;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the URL from a .url file.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the URL or nil on failure.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)+(NSURL*)URLFromIEURLFile:(NSString*)inFile
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NSURL *ret = nil;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Is this really an IE .url file?
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (inFile) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NSCharacterSet *newlines = [NSCharacterSet characterSetWithCharactersInString:@"\r\n"];
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Begin Google Modified
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    NSScanner *scanner = [NSScanner scannerWithString:[NSString stringWithContentsOfFile:inFile]];
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NSString *fileString = [NSString stringWithContentsOfFile:inFile
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                     encoding:NSWindowsCP1252StringEncoding  // best guess here
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                        error:nil];
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NSScanner *scanner = [NSScanner scannerWithString:fileString];
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // End Google Modified
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    [scanner scanUpToString:@"[InternetShortcut]" intoString:nil];
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ([scanner scanString:@"[InternetShortcut]" intoString:nil]) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Scan each non-empty line in this section. We don't need to explicitly scan the newlines or
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // whitespace because NSScanner ignores these by default.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NSString *line;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      while ([scanner scanUpToCharactersFromSet:newlines intoString:&line]) {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if ([line hasPrefix:@"URL="]) {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ret = [NSURL URLWithString:[line substringFromIndex:4]];
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        else if ([line hasPrefix:@"["]) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // This is the start of a new section, so if we haven't found an URL yet, we should bail.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ret;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)@end
149