Commit bd02a66d authored by Ivan.Shulga's avatar Ivan.Shulga Committed by Alexander Trofimov

added for macos version

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@56735 954022d7-b5bf-4e40-9824-e11837661b57
parent 479b07ef
/*
* Copyright (C) 2009 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Modified by Josh Aas of Mozilla Corporation.
*/
#ifndef ComplexTextInputPanel_h_
#define ComplexTextInputPanel_h_
#import <Cocoa/Cocoa.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
@class NSTextInputContext;
#endif
@interface ComplexTextInputPanel : NSPanel {
NSTextView *mInputTextView;
}
+ (ComplexTextInputPanel*)sharedComplexTextInputPanel;
- (NSTextInputContext*)inputContext;
- (BOOL)interpretKeyEvent:(NSEvent*)event string:(NSString**)string;
- (void)cancelComposition;
- (BOOL)inComposition;
@end
#endif // ComplexTextInputPanel_h_
/*
* Copyright (C) 2009 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Modified by Josh Aas of Mozilla Corporation.
*/
#import "ComplexTextInputPanel.h"
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
@interface NSView (SnowLeopardMethods)
- (NSTextInputContext *)inputContext;
@end
// This is actually an NSTextInputContext method, but we can't declare
// that since the whole class is 10.6+.
@interface NSObject (SnowLeopardMethods)
- (BOOL)handleEvent:(NSEvent *)theEvent;
@end
static NSString* const NSTextInputContextKeyboardSelectionDidChangeNotification =
@"NSTextInputContextKeyboardSelectionDidChangeNotification";
#endif
#define kInputWindowHeight 20
@implementation ComplexTextInputPanel
+ (ComplexTextInputPanel*)sharedComplexTextInputPanel
{
static ComplexTextInputPanel *sComplexTextInputPanel;
if (!sComplexTextInputPanel)
sComplexTextInputPanel = [[ComplexTextInputPanel alloc] init];
return sComplexTextInputPanel;
}
- (id)init
{
// In the original Apple code the style mask is given by a function which is not open source.
// What could possibly be worth hiding in that function, I do not know.
// Courtesy of gdb: stylemask: 011000011111, 0x61f
self = [super initWithContentRect:NSZeroRect styleMask:0x61f backing:NSBackingStoreBuffered defer:YES];
if (!self)
return nil;
// Set the frame size.
NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect frame = NSMakeRect(visibleFrame.origin.x, visibleFrame.origin.y, visibleFrame.size.width, kInputWindowHeight);
[self setFrame:frame display:NO];
mInputTextView = [[NSTextView alloc] initWithFrame:[self.contentView frame]];
mInputTextView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable | NSViewMaxXMargin | NSViewMinXMargin | NSViewMaxYMargin | NSViewMinYMargin;
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[self.contentView frame]];
scrollView.documentView = mInputTextView;
self.contentView = scrollView;
[scrollView release];
[self setFloatingPanel:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardInputSourceChanged:)
name:NSTextInputContextKeyboardSelectionDidChangeNotification
object:nil];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[mInputTextView release];
[super dealloc];
}
- (void)keyboardInputSourceChanged:(NSNotification *)notification
{
[self cancelComposition];
}
- (BOOL)interpretKeyEvent:(NSEvent*)event string:(NSString**)string
{
BOOL hadMarkedText = [mInputTextView hasMarkedText];
*string = nil;
if (![[mInputTextView inputContext] handleEvent:event])
return NO;
if ([mInputTextView hasMarkedText]) {
// Don't show the input method window for dead keys
if ([[event characters] length] > 0)
[self orderFront:nil];
return YES;
} else {
[self orderOut:nil];
NSString *text = [[mInputTextView textStorage] string];
if ([text length] > 0)
*string = [[text copy] autorelease];
}
[mInputTextView setString:@""];
return hadMarkedText;
}
- (NSTextInputContext*)inputContext
{
return [mInputTextView inputContext];
}
- (void)cancelComposition
{
[mInputTextView setString:@""];
[self orderOut:nil];
}
- (BOOL)inComposition
{
return [mInputTextView hasMarkedText];
}
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Chimera code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
* Bruce Davidson <Bruce.Davidson@ipl.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <AppKit/AppKit.h>
extern NSString* const kCorePasteboardFlavorType_url;
extern NSString* const kCorePasteboardFlavorType_urln;
extern NSString* const kCorePasteboardFlavorType_urld;
extern NSString* const kCaminoBookmarkListPBoardType;
extern NSString* const kWebURLsWithTitlesPboardType;
@interface NSPasteboard(ChimeraPasteboardURLUtils)
- (int) declareURLPasteboardWithAdditionalTypes:(NSArray*)additionalTypes owner:(id)newOwner;
- (void) setDataForURL:(NSString*)url title:(NSString*)title;
- (void) setURLs:(NSArray*)inUrls withTitles:(NSArray*)inTitles;
- (void) getURLs:(NSArray**)outUrls
andTitles:(NSArray**)outTitles
convertingFilenames:(BOOL)convertFilenames;
- (BOOL) containsURLData;
@end
@interface NSPasteboard(ChromiumHTMLUtils)
// Returns the HTML converted from RTF data on the pasteboard. If there is
// none, returns an empty string.
- (NSString*)htmlFromRtf;
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Chimera code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
* Bruce Davidson <Bruce.Davidson@ipl.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import "NSPasteboard+Utils.h"
#import "NSURL+Utils.h"
#import "NSString+Utils.h"
NSString* const kCorePasteboardFlavorType_url = @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url
NSString* const kCorePasteboardFlavorType_urln = @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title
NSString* const kCorePasteboardFlavorType_urld = @"CorePasteboardFlavorType 0x75726C64"; // 'urld' URL description
NSString* const kCaminoBookmarkListPBoardType = @"MozBookmarkType"; // list of Camino bookmark UIDs
NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; // Safari-compatible URL + title arrays
@interface NSPasteboard(ChimeraPasteboardURLUtilsPrivate)
- (NSString*)cleanedStringWithPasteboardString:(NSString*)aString;
@end
@implementation NSPasteboard(ChimeraPasteboardURLUtilsPrivate)
//
// Utility method to ensure strings we're using in |containsURLData|
// and |getURLs:andTitles| are free of internal control characters
// and leading/trailing whitespace
//
- (NSString*)cleanedStringWithPasteboardString:(NSString*)aString
{
NSString* cleanString = [aString stringByRemovingCharactersInSet:[NSCharacterSet controlCharacterSet]];
return [cleanString stringByTrimmingWhitespace];
}
@end
@implementation NSPasteboard(ChimeraPasteboardURLUtils)
- (int)declareURLPasteboardWithAdditionalTypes:(NSArray*)additionalTypes owner:(id)newOwner
{
NSArray* allTypes = [additionalTypes arrayByAddingObjectsFromArray:
[NSArray arrayWithObjects:
kWebURLsWithTitlesPboardType,
NSURLPboardType,
NSStringPboardType,
kCorePasteboardFlavorType_url,
kCorePasteboardFlavorType_urln,
nil]];
return [self declareTypes:allTypes owner:newOwner];
}
//
// Copy a single URL (with an optional title) to the clipboard in all relevant
// formats. Convenience method for clients that can only ever deal with one
// URL and shouldn't have to build up the arrays for setURLs:withTitles:.
//
- (void)setDataForURL:(NSString*)url title:(NSString*)title
{
NSArray* urlList = [NSArray arrayWithObject:url];
NSArray* titleList = nil;
if (title)
titleList = [NSArray arrayWithObject:title];
[self setURLs:urlList withTitles:titleList];
}
//
// Copy a set of URLs, each of which may have a title, to the pasteboard
// using all the available formats.
// The title array should be nil, or must have the same length as the URL array.
//
- (void)setURLs:(NSArray*)inUrls withTitles:(NSArray*)inTitles
{
unsigned int urlCount = [inUrls count];
// Best format that we know about is Safari's URL + title arrays - build these up
if (!inTitles) {
NSMutableArray* tmpTitleArray = [NSMutableArray arrayWithCapacity:urlCount];
for (unsigned int i = 0; i < urlCount; ++i)
[tmpTitleArray addObject:[inUrls objectAtIndex:i]];
inTitles = tmpTitleArray;
}
NSMutableArray* filePaths = [NSMutableArray array];
for (unsigned int i = 0; i < urlCount; ++i) {
NSURL* url = [NSURL URLWithString:[inUrls objectAtIndex:i]];
if ([url isFileURL] && [[NSFileManager defaultManager] fileExistsAtPath:[url path]])
[filePaths addObject:[url path]];
}
if ([filePaths count] > 0) {
[self addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[self setPropertyList:filePaths forType:NSFilenamesPboardType];
}
NSMutableArray* clipboardData = [NSMutableArray array];
[clipboardData addObject:[NSArray arrayWithArray:inUrls]];
[clipboardData addObject:inTitles];
[self setPropertyList:clipboardData forType:kWebURLsWithTitlesPboardType];
if (urlCount == 1) {
NSString* url = [inUrls objectAtIndex:0];
NSString* title = [inTitles objectAtIndex:0];
[[NSURL URLWithString:url] writeToPasteboard:self];
[self setString:url forType:NSStringPboardType];
const char* tempCString = [url UTF8String];
[self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_url];
if (inTitles)
tempCString = [title UTF8String];
[self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_urln];
}
else if (urlCount > 1)
{
// With multiple URLs there aren't many other formats we can use
// Just write a string of each URL (ignoring titles) on a separate line
[self setString:[inUrls componentsJoinedByString:@"\n"] forType:NSStringPboardType];
// but we have to put something in the carbon style flavors, otherwise apps will think
// there is data there, but get nothing
NSString* firstURL = [inUrls objectAtIndex:0];
NSString* firstTitle = [inTitles objectAtIndex:0];
const char* tempCString = [firstURL UTF8String];
[self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_url];
tempCString = [firstTitle UTF8String]; // not i18n friendly
[self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_urln];
}
}
// Get the set of URLs and their corresponding titles from the pasteboard.
// If there are no URLs in a format we understand on the pasteboard empty
// arrays will be returned. The two arrays will always be the same size.
// The arrays returned are on the auto release pool. If |convertFilenames|
// is YES, then the function will attempt to convert filenames in the drag
// to file URLs.
- (void) getURLs:(NSArray**)outUrls
andTitles:(NSArray**)outTitles
convertingFilenames:(BOOL)convertFilenames
{
NSArray* types = [self types];
NSURL* urlFromNSURL = nil; // Used below in getting an URL from the NSURLPboardType.
if ([types containsObject:kWebURLsWithTitlesPboardType]) {
NSArray* urlAndTitleContainer = [self propertyListForType:kWebURLsWithTitlesPboardType];
*outUrls = [urlAndTitleContainer objectAtIndex:0];
*outTitles = [urlAndTitleContainer objectAtIndex:1];
} else if ([types containsObject:NSFilenamesPboardType]) {
NSArray *files = [self propertyListForType:NSFilenamesPboardType];
*outUrls = [NSMutableArray arrayWithCapacity:[files count]];
*outTitles = [NSMutableArray arrayWithCapacity:[files count]];
for ( unsigned int i = 0; i < [files count]; ++i ) {
NSString *file = [files objectAtIndex:i];
NSString *ext = [[file pathExtension] lowercaseString];
NSString *urlString = nil;
NSString *title = @"";
OSType fileType = NSHFSTypeCodeFromFileType(NSHFSTypeOfFile(file));
// Check whether the file is a .webloc, a .ftploc, a .url, or some other kind of file.
if ([ext isEqualToString:@"webloc"] || [ext isEqualToString:@"ftploc"] || fileType == 'ilht' || fileType == 'ilft') {
NSURL* urlFromInetloc = [NSURL URLFromInetloc:file];
if (urlFromInetloc) {
urlString = [urlFromInetloc absoluteString];
title = [[file lastPathComponent] stringByDeletingPathExtension];
}
} else if ([ext isEqualToString:@"url"] || fileType == 'LINK') {
NSURL* urlFromIEURLFile = [NSURL URLFromIEURLFile:file];
if (urlFromIEURLFile) {
urlString = [urlFromIEURLFile absoluteString];
title = [[file lastPathComponent] stringByDeletingPathExtension];
}
}
if (!urlString) {
if (!convertFilenames) {
continue;
}
// Use the filename if not a .webloc or .url file, or if either of the
// functions returns nil.
urlString = [[NSURL fileURLWithPath:file] absoluteString];
title = [file lastPathComponent];
}
[(NSMutableArray*) *outUrls addObject:urlString];
[(NSMutableArray*) *outTitles addObject:title];
}
} else if ([types containsObject:NSURLPboardType] && (urlFromNSURL = [NSURL URLFromPasteboard:self])) {
*outUrls = [NSArray arrayWithObject:[urlFromNSURL absoluteString]];
NSString* title = nil;
if ([types containsObject:kCorePasteboardFlavorType_urld])
title = [self stringForType:kCorePasteboardFlavorType_urld];
if (!title && [types containsObject:kCorePasteboardFlavorType_urln])
title = [self stringForType:kCorePasteboardFlavorType_urln];
if (!title && [types containsObject:NSStringPboardType])
title = [self stringForType:NSStringPboardType];
*outTitles = [NSArray arrayWithObject:(title ? title : @"")];
} else if ([types containsObject:NSStringPboardType]) {
NSString* potentialURLString = [self cleanedStringWithPasteboardString:[self stringForType:NSStringPboardType]];
if ([potentialURLString isValidURI]) {
*outUrls = [NSArray arrayWithObject:potentialURLString];
NSString* title = nil;
if ([types containsObject:kCorePasteboardFlavorType_urld])
title = [self stringForType:kCorePasteboardFlavorType_urld];
if (!title && [types containsObject:kCorePasteboardFlavorType_urln])
title = [self stringForType:kCorePasteboardFlavorType_urln];
*outTitles = [NSArray arrayWithObject:(title ? title : @"")];
} else {
// The string doesn't look like a URL - return empty arrays
*outUrls = [NSArray array];
*outTitles = [NSArray array];
}
} else {
// We don't recognise any of these formats - return empty arrays
*outUrls = [NSArray array];
*outTitles = [NSArray array];
}
}
//
// Indicates if this pasteboard contains URL data that we understand
// Deals with all our URL formats. Only strings that are valid URLs count.
// If this returns YES it is safe to use getURLs:andTitles: to retrieve the data.
//
// NB: Does not consider our internal bookmark list format, because callers
// usually need to deal with this separately because it can include folders etc.
//
- (BOOL) containsURLData
{
NSArray* types = [self types];
if ( [types containsObject:kWebURLsWithTitlesPboardType]
|| [types containsObject:NSURLPboardType]
|| [types containsObject:NSFilenamesPboardType] )
return YES;
if ([types containsObject:NSStringPboardType]) {
// Trim whitespace off the ends and newlines out of the middle so we don't reject otherwise-valid URLs;
// we'll do another cleaning when we set the URLs and titles later, so this is safe.
NSString* potentialURLString = [self cleanedStringWithPasteboardString:[self stringForType:NSStringPboardType]];
return [potentialURLString isValidURI];
}
return NO;
}
@end
@implementation NSPasteboard(ChromiumHTMLUtils)
// Convert the RTF to HTML via an NSAttributedString.
- (NSString*)htmlFromRtf {
if (![[self types] containsObject:NSRTFPboardType])
return @"";
NSAttributedString* attributed =
[[[NSAttributedString alloc]
initWithRTF:[self dataForType:NSRTFPboardType]
documentAttributes:nil] autorelease];
NSDictionary* attributeDict =
[NSDictionary dictionaryWithObject:NSHTMLTextDocumentType
forKey:NSDocumentTypeDocumentAttribute];
NSData* htmlData =
[attributed dataFromRange:NSMakeRange(0, [attributed length])
documentAttributes:attributeDict
error:nil];
// According to the docs, NSHTMLTextDocumentType is UTF8.
return [[[NSString alloc]
initWithData:htmlData encoding:NSUTF8StringEncoding] autorelease];
}
@end
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <Cocoa/Cocoa.h>
@interface NSScreen (CHScreenAdditions)
+ (NSScreen*)screenForPoint:(NSPoint)point;
@end
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import "NSScreen+Utils.h"
@implementation NSScreen (CHScreenAdditions)
+ (NSScreen*)screenForPoint:(NSPoint)point
{
NSArray* screens = [NSScreen screens];
NSEnumerator* screenEnum = [screens objectEnumerator];
NSScreen* screen;
while ( (screen = [screenEnum nextObject]) ) {
NSRect frame = [screen frame];
if (NSPointInRect(point, frame))
break;
}
return screen;
}
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Chimera code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <Foundation/Foundation.h>
typedef enum
{
kTruncateAtStart,
kTruncateAtMiddle,
kTruncateAtEnd
} ETruncationType;
// a category to extend NSString
@interface NSString (ChimeraStringUtils)
+ (id)ellipsisString;
+ (NSString*)stringWithUUID;
- (BOOL)isEqualToStringIgnoringCase:(NSString*)inString;
- (BOOL)hasCaseInsensitivePrefix:(NSString*)inString;
// Some URIs can contain spaces and still work, even though they aren't strictly valid
// per RFC2396. This method allows us to account for those URIs.
- (BOOL)isLooselyValidatedURI;
// Utility method to identify URIs that can be run in the context of the current page.
// These URIs could be used as attack vectors via AppleScript, for example.
- (BOOL)isPotentiallyDangerousURI;
// Utility method to ensure validity of URI strings. NSURL is used to validate
// most of them, but the NSURL test may fail for |javascript:| and |data:| URIs
// because they often contain invalid (per RFC2396) characters such as spaces.
- (BOOL)isValidURI;
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
- (NSString *)stringByTruncatingTo:(unsigned int)maxCharacters at:(ETruncationType)truncationType;
- (NSString *)stringByTruncatingToWidth:(float)inWidth at:(ETruncationType)truncationType withAttributes:(NSDictionary *)attributes;
- (NSString *)stringByTrimmingWhitespace;
- (NSString *)stringByRemovingAmpEscapes;
- (NSString *)stringByAddingAmpEscapes;
@end
@interface NSMutableString (ChimeraMutableStringUtils)
- (void)truncateTo:(unsigned)maxCharacters at:(ETruncationType)truncationType;
- (void)truncateToWidth:(float)maxWidth at:(ETruncationType)truncationType withAttributes:(NSDictionary *)attributes;
@end
@interface NSString (ChimeraFilePathStringUtils)
- (NSString*)volumeNamePathComponent;
- (NSString*)displayNameOfLastPathComponent;
@end
@interface NSString (CaminoURLStringUtils)
// Returns true if the string represents a "blank" URL ("" or "about:blank")
- (BOOL)isBlankURL;
// Returns a URI that looks good in a location field
- (NSString *)unescapedURI;
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Chimera code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
* David Haas <haasd@cae.wisc.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <AppKit/AppKit.h> // for NSStringDrawing.h
#import "NSString+Utils.h"
#include "googleurl/src/gurl.h"
@implementation NSString (ChimeraStringUtils)
+ (id)ellipsisString
{
static NSString* sEllipsisString = nil;
if (!sEllipsisString) {
unichar ellipsisChar = 0x2026;
sEllipsisString = [[NSString alloc] initWithCharacters:&ellipsisChar length:1];
}
return sEllipsisString;
}
+ (NSString*)stringWithUUID
{
NSString* uuidString = nil;
CFUUIDRef newUUID = CFUUIDCreate(kCFAllocatorDefault);
if (newUUID) {
uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, newUUID);
CFRelease(newUUID);
}
return [uuidString autorelease];
}
- (BOOL)isEqualToStringIgnoringCase:(NSString*)inString
{
return ([self compare:inString options:NSCaseInsensitiveSearch] == NSOrderedSame);
}
- (BOOL)hasCaseInsensitivePrefix:(NSString*)inString
{
if ([self length] < [inString length])
return NO;
return ([self compare:inString options:NSCaseInsensitiveSearch range:NSMakeRange(0, [inString length])] == NSOrderedSame);
}
- (BOOL)isLooselyValidatedURI
{
return ([self hasCaseInsensitivePrefix:@"javascript:"] || [self hasCaseInsensitivePrefix:@"data:"]);
}
- (BOOL)isPotentiallyDangerousURI
{
return ([self hasCaseInsensitivePrefix:@"javascript:"] || [self hasCaseInsensitivePrefix:@"data:"]);
}
- (BOOL)isValidURI
{
// isValid() will only be true for valid, well-formed URI strings
GURL testURL([self UTF8String]);
// |javascript:| and |data:| URIs might not have passed the test,
// but spaces will work OK, so evaluate them separately.
if ((testURL.is_valid()) || [self isLooselyValidatedURI]) {
return YES;
}
return NO;
}
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet
{
NSScanner* cleanerScanner = [NSScanner scannerWithString:self];
NSMutableString* cleanString = [NSMutableString stringWithCapacity:[self length]];
// Make sure we don't skip whitespace, which NSScanner does by default
[cleanerScanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@""]];
while (![cleanerScanner isAtEnd]) {
NSString* stringFragment;
if ([cleanerScanner scanUpToCharactersFromSet:characterSet intoString:&stringFragment])
[cleanString appendString:stringFragment];
[cleanerScanner scanCharactersFromSet:characterSet intoString:nil];
}
return cleanString;
}
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet
withString:(NSString*)string
{
NSScanner* cleanerScanner = [NSScanner scannerWithString:self];
NSMutableString* cleanString = [NSMutableString stringWithCapacity:[self length]];
// Make sure we don't skip whitespace, which NSScanner does by default
[cleanerScanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@""]];
while (![cleanerScanner isAtEnd])
{
NSString* stringFragment;
if ([cleanerScanner scanUpToCharactersFromSet:characterSet intoString:&stringFragment])
[cleanString appendString:stringFragment];
if ([cleanerScanner scanCharactersFromSet:characterSet intoString:nil])
[cleanString appendString:string];
}
return cleanString;
}
- (NSString*)stringByTruncatingTo:(unsigned int)maxCharacters at:(ETruncationType)truncationType
{
if ([self length] > maxCharacters)
{
NSMutableString *mutableCopy = [self mutableCopy];
[mutableCopy truncateTo:maxCharacters at:truncationType];
return [mutableCopy autorelease];
}
return self;
}
- (NSString *)stringByTruncatingToWidth:(float)inWidth at:(ETruncationType)truncationType
withAttributes:(NSDictionary *)attributes
{
if ([self sizeWithAttributes:attributes].width > inWidth)
{
NSMutableString *mutableCopy = [self mutableCopy];
[mutableCopy truncateToWidth:inWidth at:truncationType withAttributes:attributes];
return [mutableCopy autorelease];
}
return self;
}
- (NSString *)stringByTrimmingWhitespace
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
-(NSString *)stringByRemovingAmpEscapes
{
NSMutableString* dirtyStringMutant = [NSMutableString stringWithString:self];
[dirtyStringMutant replaceOccurrencesOfString:@"&amp;"
withString:@"&"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"&quot;"
withString:@"\""
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"&lt;"
withString:@"<"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"&gt;"
withString:@">"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"&mdash;"
withString:@"-"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"&apos;"
withString:@"'"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
// fix import from old Firefox versions, which exported &#39; instead of a plain apostrophe
[dirtyStringMutant replaceOccurrencesOfString:@"&#39;"
withString:@"'"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
return [dirtyStringMutant stringByRemovingCharactersInSet:[NSCharacterSet controlCharacterSet]];
}
-(NSString *)stringByAddingAmpEscapes
{
NSMutableString* dirtyStringMutant = [NSMutableString stringWithString:self];
[dirtyStringMutant replaceOccurrencesOfString:@"&"
withString:@"&amp;"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"\""
withString:@"&quot;"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@"<"
withString:@"&lt;"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
[dirtyStringMutant replaceOccurrencesOfString:@">"
withString:@"&gt;"
options:NSLiteralSearch
range:NSMakeRange(0,[dirtyStringMutant length])];
return [NSString stringWithString:dirtyStringMutant];
}
@end
@implementation NSMutableString (ChimeraMutableStringUtils)
- (void)truncateTo:(unsigned)maxCharacters at:(ETruncationType)truncationType
{
if ([self length] <= maxCharacters)
return;
NSRange replaceRange;
replaceRange.length = [self length] - maxCharacters;
switch (truncationType) {
case kTruncateAtStart:
replaceRange.location = 0;
break;
case kTruncateAtMiddle:
replaceRange.location = maxCharacters / 2;
break;
case kTruncateAtEnd:
replaceRange.location = maxCharacters;
break;
default:
#if DEBUG
NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif
replaceRange.location = maxCharacters;
break;
}
[self replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
}
- (void)truncateToWidth:(float)maxWidth
at:(ETruncationType)truncationType
withAttributes:(NSDictionary *)attributes
{
// First check if we have to truncate at all.
if ([self sizeWithAttributes:attributes].width <= maxWidth)
return;
// Essentially, we perform a binary search on the string length
// which fits best into maxWidth.
float width = maxWidth;
int lo = 0;
int hi = [self length];
int mid;
// Make a backup copy of the string so that we can restore it if we fail low.
NSMutableString *backup = [self mutableCopy];
while (hi >= lo) {
mid = (hi + lo) / 2;
// Cut to mid chars and calculate the resulting width
[self truncateTo:mid at:truncationType];
width = [self sizeWithAttributes:attributes].width;
if (width > maxWidth) {
// Fail high - string is still to wide. For the next cut, we can simply
// work on the already cut string, so we don't restore using the backup.
hi = mid - 1;
}
else if (width == maxWidth) {
// Perfect match, abort the search.
break;
}
else {
// Fail low - we cut off too much. Restore the string before cutting again.
lo = mid + 1;
[self setString:backup];
}
}
// Perform the final cut (unless this was already a perfect match).
if (width != maxWidth)
[self truncateTo:hi at:truncationType];
[backup release];
}
@end
@implementation NSString (ChimeraFilePathStringUtils)
- (NSString*)volumeNamePathComponent
{
// if the file doesn't exist, then componentsToDisplayForPath will return nil,
// so back up to the nearest existing dir
NSString* curPath = self;
while (![[NSFileManager defaultManager] fileExistsAtPath:curPath])
{
NSString* parentDirPath = [curPath stringByDeletingLastPathComponent];
if ([parentDirPath isEqualToString:curPath])
break; // avoid endless loop
curPath = parentDirPath;
}
NSArray* displayComponents = [[NSFileManager defaultManager] componentsToDisplayForPath:curPath];
if ([displayComponents count] > 0)
return [displayComponents objectAtIndex:0];
return self;
}
- (NSString*)displayNameOfLastPathComponent
{
return [[NSFileManager defaultManager] displayNameAtPath:self];
}
@end
@implementation NSString (CaminoURLStringUtils)
- (BOOL)isBlankURL
{
return ([self isEqualToString:@"about:blank"] || [self isEqualToString:@""]);
}
// Excluded character list comes from RFC2396 and by examining Safari's behaviour
- (NSString*)unescapedURI
{
NSString *unescapedURI = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(CFStringRef)self,
CFSTR(" \"\';/?:@&=+$,#"),
kCFStringEncodingUTF8);
return unescapedURI ? [unescapedURI autorelease] : self;
}
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Camino code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Nate Weaver (Wevah) - wevah@derailer.org
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <Cocoa/Cocoa.h>
@interface NSURL (CaminoExtensions)
// This takes an NSURL to a local file, and if that file is a file that
// represents a URL, returns the URL it contains. Otherwise, returns the
// passed URL. Supports .url, .webloc and .ftploc files.
+ (NSURL*)decodeLocalFileURL:(NSURL*)url;
+(NSURL*)URLFromInetloc:(NSString*)inFile;
+(NSURL*)URLFromIEURLFile:(NSString*)inFile;
@end
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Camino code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Nate Weaver (Wevah) - wevah@derailer.org
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import "NSURL+Utils.h"
@implementation NSURL (CaminoExtensions)
+ (NSURL*)decodeLocalFileURL:(NSURL*)url
{
NSString* urlPathString = [url path];
NSString* ext = [[urlPathString pathExtension] lowercaseString];
OSType fileType = NSHFSTypeCodeFromFileType(NSHFSTypeOfFile(urlPathString));
if ([ext isEqualToString:@"url"] || fileType == 'LINK') {
url = [NSURL URLFromIEURLFile:urlPathString];
}
else if ([ext isEqualToString:@"webloc"] || [ext isEqualToString:@"ftploc"] ||
fileType == 'ilht' || fileType == 'ilft')
{
url = [NSURL URLFromInetloc:urlPathString];
}
return url;
}
//
// Reads the URL from a .webloc/.ftploc file.
// Returns the URL, or nil on failure.
//
+(NSURL*)URLFromInetloc:(NSString*)inFile
{
FSRef ref;
NSURL *ret = nil;
if (inFile && FSPathMakeRef((UInt8 *)[inFile fileSystemRepresentation], &ref, NULL) == noErr) {
short resRef;
resRef = FSOpenResFile(&ref, fsRdPerm);
if (resRef != -1) { // Has resouce fork.
Handle urlResHandle;
if ((urlResHandle = Get1Resource('url ', 256))) { // Has 'url ' resource with ID 256.
long size;
size = GetMaxResourceSize(urlResHandle);
// Begin Google Modified
// ret = [NSURL URLWithString:[NSString stringWithCString:(char *)*urlResHandle length:size]];
NSString *urlString = [[[NSString alloc] initWithBytes:(void *)*urlResHandle
length:size
encoding:NSMacOSRomanStringEncoding] // best guess here
autorelease];
ret = [NSURL URLWithString:urlString];
// End Google Modified
}
CloseResFile(resRef);
}
if (!ret) { // Look for valid plist data.
NSDictionary *plist;
if ((plist = [[NSDictionary alloc] initWithContentsOfFile:inFile])) {
ret = [NSURL URLWithString:[plist objectForKey:@"URL"]];
[plist release];
}
}
}
return ret;
}
//
// Reads the URL from a .url file.
// Returns the URL or nil on failure.
//
+(NSURL*)URLFromIEURLFile:(NSString*)inFile
{
NSURL *ret = nil;
// Is this really an IE .url file?
if (inFile) {
NSCharacterSet *newlines = [NSCharacterSet characterSetWithCharactersInString:@"\r\n"];
// Begin Google Modified
// NSScanner *scanner = [NSScanner scannerWithString:[NSString stringWithContentsOfFile:inFile]];
NSString *fileString = [NSString stringWithContentsOfFile:inFile
encoding:NSWindowsCP1252StringEncoding // best guess here
error:nil];
NSScanner *scanner = [NSScanner scannerWithString:fileString];
// End Google Modified
[scanner scanUpToString:@"[InternetShortcut]" intoString:nil];
if ([scanner scanString:@"[InternetShortcut]" intoString:nil]) {
// Scan each non-empty line in this section. We don't need to explicitly scan the newlines or
// whitespace because NSScanner ignores these by default.
NSString *line;
while ([scanner scanUpToCharactersFromSet:newlines intoString:&line]) {
if ([line hasPrefix:@"URL="]) {
ret = [NSURL URLWithString:[line substringFromIndex:4]];
break;
}
else if ([line hasPrefix:@"["]) {
// This is the start of a new section, so if we haven't found an URL yet, we should bail.
break;
}
}
}
}
return ret;
}
@end
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <smfr@smfr.org>
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <AppKit/AppKit.h>
@interface NSWorkspace(CaminoDefaultBrowserAdditions)
- (NSArray*)installedBrowserIdentifiers; // sort order not specified
- (NSString*)defaultBrowserIdentifier;
- (NSURL*)defaultBrowserURL;
- (NSArray*)installedFeedViewerIdentifiers;
- (NSString*)defaultFeedViewerIdentifier;
- (NSURL*)defaultFeedViewerURL;
- (void)setDefaultBrowserWithIdentifier:(NSString*)bundleID;
- (void)setDefaultFeedViewerWithIdentifier:(NSString*)bundleID;
- (NSURL*)urlOfApplicationWithIdentifier:(NSString*)bundleID;
- (NSString*)identifierForBundle:(NSURL*)inBundleURL;
- (NSString*)displayNameForFile:(NSURL*)inFileURL;
// OS feature checks
+ (NSString*)osVersionString;
+ (BOOL)isLeopardOrHigher;
@end
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <smfr@smfr.org>
* Josh Aas <josh@mozilla.com>
* Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import "NSWorkspace+Utils.h"
@implementation NSWorkspace(CaminoDefaultBrowserAdditions)
- (NSArray*)installedBrowserIdentifiers
{
NSArray* apps = [(NSArray*)LSCopyAllHandlersForURLScheme(CFSTR("https")) autorelease];
// add the default if it isn't there
NSString* defaultHandler = [self defaultBrowserIdentifier];
if (defaultHandler && ([apps indexOfObject:defaultHandler] == NSNotFound))
apps = [apps arrayByAddingObject:defaultHandler];
return apps;
}
- (NSArray*)installedFeedViewerIdentifiers
{
NSArray* apps = [(NSArray*)LSCopyAllHandlersForURLScheme(CFSTR("feed")) autorelease];
// add the default if it isn't there
NSString* defaultHandler = [self defaultFeedViewerIdentifier];
if (defaultHandler && ([apps indexOfObject:defaultHandler] == NSNotFound))
apps = [apps arrayByAddingObject:defaultHandler];
return apps;
}
- (NSString*)defaultBrowserIdentifier
{
NSString* defaultBundleId = [(NSString*)LSCopyDefaultHandlerForURLScheme(CFSTR("http")) autorelease];
// Sometimes LaunchServices likes to pretend there's no default browser.
// If that happens, we'll assume it's probably Safari.
if (!defaultBundleId)
defaultBundleId = @"com.apple.safari";
return defaultBundleId;
}
- (NSString*)defaultFeedViewerIdentifier
{
return [(NSString*)LSCopyDefaultHandlerForURLScheme(CFSTR("feed")) autorelease];
}
- (NSURL*)defaultBrowserURL
{
NSString* defaultBundleId = [self defaultBrowserIdentifier];
if (defaultBundleId)
return [self urlOfApplicationWithIdentifier:defaultBundleId];
return nil;
}
- (NSURL*)defaultFeedViewerURL
{
NSString* defaultBundleId = [self defaultFeedViewerIdentifier];
if (defaultBundleId)
return [self urlOfApplicationWithIdentifier:defaultBundleId];
return nil;
}
- (void)setDefaultBrowserWithIdentifier:(NSString*)bundleID
{
LSSetDefaultHandlerForURLScheme(CFSTR("http"), (CFStringRef)bundleID);
LSSetDefaultHandlerForURLScheme(CFSTR("https"), (CFStringRef)bundleID);
LSSetDefaultRoleHandlerForContentType(kUTTypeHTML, kLSRolesViewer, (CFStringRef)bundleID);
LSSetDefaultRoleHandlerForContentType(kUTTypeURL, kLSRolesViewer, (CFStringRef)bundleID);
}
- (void)setDefaultFeedViewerWithIdentifier:(NSString*)bundleID
{
LSSetDefaultHandlerForURLScheme(CFSTR("feed"), (CFStringRef)bundleID);
}
- (NSURL*)urlOfApplicationWithIdentifier:(NSString*)bundleID
{
if (!bundleID)
return nil;
NSURL* appURL = nil;
if (LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)bundleID, NULL, NULL, (CFURLRef*)&appURL) == noErr)
return [appURL autorelease];
return nil;
}
- (NSString*)identifierForBundle:(NSURL*)inBundleURL
{
if (!inBundleURL) return nil;
NSBundle* tmpBundle = [NSBundle bundleWithPath:[[inBundleURL path] stringByStandardizingPath]];
if (tmpBundle)
{
NSString* tmpBundleID = [tmpBundle bundleIdentifier];
if (tmpBundleID && ([tmpBundleID length] > 0)) {
return tmpBundleID;
}
}
return nil;
}
- (NSString*)displayNameForFile:(NSURL*)inFileURL
{
NSString *name;
LSCopyDisplayNameForURL((CFURLRef)inFileURL, (CFStringRef *)&name);
return [name autorelease];
}
//
// +osVersionString
//
// Returns the system version string from
// /System/Library/CoreServices/SystemVersion.plist
// (as recommended by Apple).
//
+ (NSString*)osVersionString
{
NSDictionary* versionInfo = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
return [versionInfo objectForKey:@"ProductVersion"];
}
//
// +systemVersion
//
// Returns the host's OS version as returned by the 'sysv' gestalt selector,
// 10.x.y = 0x000010xy
//
+ (long)systemVersion
{
static SInt32 sSystemVersion = 0;
if (!sSystemVersion)
Gestalt(gestaltSystemVersion, &sSystemVersion);
return (long)sSystemVersion;
}
//
// +isLeopardOrHigher
//
// returns YES if we're on 10.5 or better
//
+ (BOOL)isLeopardOrHigher
{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
return YES;
#else
return [self systemVersion] >= 0x1050;
#endif
}
@end
Name: Cocoa extension code from Camino
Short Name: camino
URL: http://caminobrowser.org/
Version: unknown
License: MPL 1.1/GPL 2.0/LGPL 2.1
Description:
This directory contains the following files come from (Mozilla) Camino
(src/camino/src/extensions):
NSPasteboard+Utils.h/mm
NSScreen+Utils.h/m
NSString+Utils.h/m
NSURL+Utils.h/m
NSWorkspace+Utils.h/m
Local modifications:
- NSURL+Utils.m was modified to use non-deprecated Cocoa APIs to allow
compilation on future versions of Mac OS X.
- NSString+Utils.m was renamed to NSString+Utils.mm and modified to use GURL
for validation in -[NSString isValidURI].
- NSPasteboard+Utils.mm was modified to add an argument to
-[NSPasteboard getURLs:andTitles:] to determine whether or not filenames in
the drag should be converted to file URLs.
-[NSPasteboard htmlFromRtf] added to do rtf->html conversion.
- NSWorkspace+Utils.m was modified to compile on the x86_64 architecture.
-----------------------------------------------------------------
Also includes IME panel from Gecko, which is based on WebKit's implementation.
Although it comes from Mozilla (http://mxr.mozilla.org), it uses the original
WebKit license.
Local modifitations:
- Add #ifdef'd definifitions of a few symbols to support 10.5 SDK.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment