From d9a7d54acb7e6ee629baf3864192a9154dd3d104 Mon Sep 17 00:00:00 2001 From: "stuart.morgan%alumni.case.edu" Date: Wed, 31 Oct 2007 15:26:58 +0000 Subject: [PATCH] Camino only - Bug 401532: Split miscellaneous utilities out of MainController. r/sr=mento --- camino/src/application/MainController.h | 3 - camino/src/application/MainController.mm | 85 +++++----------- camino/src/bookmarks/BookmarkButton.mm | 5 +- camino/src/browser/BrowserTabViewItem.mm | 12 ++- camino/src/browser/BrowserWindowController.mm | 6 +- camino/src/browser/PageProxyIcon.mm | 18 ++-- camino/src/download/SaveHeaderSniffer.mm | 5 +- camino/src/extensions/ImageAdditions.h | 50 ---------- camino/src/extensions/ImageAdditions.m | 99 ------------------- camino/src/extensions/NSString+Utils.h | 9 +- camino/src/extensions/NSString+Utils.m | 29 ++---- 11 files changed, 68 insertions(+), 253 deletions(-) diff --git a/camino/src/application/MainController.h b/camino/src/application/MainController.h index c8dbfd77591..6cbb419e419 100644 --- a/camino/src/application/MainController.h +++ b/camino/src/application/MainController.h @@ -199,9 +199,6 @@ typedef enum EBookmarkOpenBehavior - (NSView*)savePanelView; -+ (NSImage*)createImageForDragging:(NSImage*)aIcon title:(NSString*)aTitle; -// utility routine to test if a url is "blank" (either empty or about:blank) -+ (BOOL)isBlankURL:(NSString*)inURL; - (void)closeFindDialog; @end diff --git a/camino/src/application/MainController.mm b/camino/src/application/MainController.mm index 65f8f201be2..22f82b99813 100644 --- a/camino/src/application/MainController.mm +++ b/camino/src/application/MainController.mm @@ -662,7 +662,7 @@ NSString* const kPreviousSessionTerminatedNormallyKey = @"PreviousSessionTermina // The process of creating a new tab in this brand new window loads about:blank for us as a // side effect of calling GetDocument(). We don't need to do it again. - if ([MainController isBlankURL:aURL]) + if (!aURL || [aURL isBlankURL]) [browser disableLoadPage]; else [browser loadURL:aURL referrer:aReferrer focusContent:YES allowPopups:inAllowPopups]; @@ -866,6 +866,27 @@ NSString* const kPreviousSessionTerminatedNormallyKey = @"PreviousSessionTermina } } +// +// This takes an NSURL to a local file, and if that file is a file that contains +// a URL we want and isn't the content itself, we return the URL it contains. +// Otherwise, we return the URL we originally got. Right now this supports .url, +// .webloc and .ftploc files. +// ++ (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; +} + #pragma mark - #pragma mark Delegate/Notification @@ -1039,7 +1060,7 @@ NSString* const kPreviousSessionTerminatedNormallyKey = @"PreviousSessionTermina NSString* homePage = mStartURL ? mStartURL : [[PreferenceManager sharedInstance] homePageUsingStartPage:YES]; BrowserWindowController* controller = [self openBrowserWindowWithURL:homePage andReferrer:nil behind:nil allowPopups:NO]; - if ([MainController isBlankURL:homePage]) + if (!homePage || [homePage isBlankURL]) [controller focusURLBar]; else [[[controller browserWrapper] browserView] setActive:YES]; @@ -1988,65 +2009,7 @@ static int SortByProtocolAndName(NSDictionary* item1, NSDictionary* item2, void* } #pragma mark - -#pragma mark Miscellaneous Utilities -//which may not belong in MainController at all - -// -// This takes an NSURL to a local file, and if that file is a file that contains -// a URL we want and isn't the content itself, we return the URL it contains. -// Otherwise, we return the URL we originally got. Right now this supports .url, -// .webloc and .ftploc files. -// -+ (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; -} - -+ (NSImage*)createImageForDragging:(NSImage*)aIcon title:(NSString*)aTitle -{ - const float kTitleOffset = 2.0f; - - NSDictionary* stringAttrs = [NSDictionary dictionaryWithObjectsAndKeys: - [[NSColor textColor] colorWithAlphaComponent:0.8], NSForegroundColorAttributeName, - [NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, - nil]; - - // get the size of the new image we are creating - NSSize titleSize = [aTitle sizeWithAttributes:stringAttrs]; - NSSize imageSize = NSMakeSize(titleSize.width + [aIcon size].width + kTitleOffset + 2, - titleSize.height > [aIcon size].height ? titleSize.height - : [aIcon size].height); - - // create the image and lock drawing focus on it - NSImage* dragImage = [[[NSImage alloc] initWithSize:imageSize] autorelease]; - [dragImage lockFocus]; - - // draw the image and title in image with translucency - NSRect imageRect = NSMakeRect(0, 0, [aIcon size].width, [aIcon size].height); - [aIcon drawAtPoint:NSMakePoint(0, 0) fromRect:imageRect operation:NSCompositeCopy fraction:0.8]; - - [aTitle drawAtPoint:NSMakePoint([aIcon size].width + kTitleOffset, 0.0) withAttributes:stringAttrs]; - - [dragImage unlockFocus]; - return dragImage; -} - -+ (BOOL)isBlankURL:(NSString*)inURL -{ - BOOL isBlank = NO; - if (!inURL || [inURL isEqualToString:@"about:blank"] || [inURL isEqualToString:@""]) - isBlank = YES; - return isBlank; -} +#pragma mark Find Panel - (void)closeFindDialog { diff --git a/camino/src/bookmarks/BookmarkButton.mm b/camino/src/bookmarks/BookmarkButton.mm index acc17031ee3..98840c67887 100644 --- a/camino/src/bookmarks/BookmarkButton.mm +++ b/camino/src/bookmarks/BookmarkButton.mm @@ -39,6 +39,7 @@ * ***** END LICENSE BLOCK ***** */ #import "BookmarkButton.h" +#import "ImageAdditions.h" #import "NSString+Utils.h" #import "NSPasteboard+Utils.h" #import "DraggableImageAndTextCell.h" @@ -312,8 +313,8 @@ // deallocated too soon. This occurs with SDK >= 10.3, but not earlier. // Change in cleanup strategy? Hold on tight. [[self retain] autorelease]; - [self dragImage:[MainController createImageForDragging:[self image] - title:([item isSeparator] ? @"" : title)] + [self dragImage:[NSImage dragImageWithIcon:[self image] + title:([item isSeparator] ? @"" : title)] at:NSMakePoint(0, NSHeight([self bounds])) offset:NSMakeSize(0, 0) event:aEvent diff --git a/camino/src/browser/BrowserTabViewItem.mm b/camino/src/browser/BrowserTabViewItem.mm index 8c13b445ec5..bab72bfebf5 100644 --- a/camino/src/browser/BrowserTabViewItem.mm +++ b/camino/src/browser/BrowserTabViewItem.mm @@ -36,6 +36,7 @@ * * ***** END LICENSE BLOCK ***** */ +#import "ImageAdditions.h" #import "NSString+Utils.h" #import "NSBezierPath+Utils.h" #import "NSPasteboard+Utils.h" @@ -44,7 +45,6 @@ #import "BrowserTabView.h" #import "BrowserWrapper.h" -#import "MainController.h" #import "BrowserWindowController.h" #import "TruncatingTextAndImageCell.h" #import "TabButtonCell.h" @@ -276,9 +276,13 @@ const int kMenuTruncationChars = 60; NSPoint dragOrigin = [self frame].origin; dragOrigin.y += [self frame].size.height; - [self dragImage: [MainController createImageForDragging:[mLabelCell image] title:title] - at:iconRect.origin offset:NSMakeSize(0.0, 0.0) - event:theEvent pasteboard:pboard source:self slideBack:YES]; + [self dragImage:[NSImage dragImageWithIcon:[mLabelCell image] title:title] + at:iconRect.origin + offset:NSMakeSize(0.0, 0.0) + event:theEvent + pasteboard:pboard + source:self + slideBack:YES]; } } diff --git a/camino/src/browser/BrowserWindowController.mm b/camino/src/browser/BrowserWindowController.mm index 2358b8b70e9..c0d43da09ae 100644 --- a/camino/src/browser/BrowserWindowController.mm +++ b/camino/src/browser/BrowserWindowController.mm @@ -3353,7 +3353,7 @@ enum BWCOpenDest { if (loadHomepage) urlToLoad = [[PreferenceManager sharedInstance] homePageUsingStartPage:NO]; - focusURLBar = locationBarVisible && [MainController isBlankURL:urlToLoad]; + focusURLBar = locationBarVisible && (!urlToLoad || [urlToLoad isBlankURL]); [newView loadURI:urlToLoad referrer:nil flags:NSLoadFlagsNone focusContent:!focusURLBar allowPopups:NO]; } @@ -3511,7 +3511,7 @@ enum BWCOpenDest { // this should really be a class method -(BrowserWindowController*)openNewWindowWithURL:(NSString*)aURLSpec referrer:(NSString*)aReferrer loadInBackground:(BOOL)aLoadInBG allowPopups:(BOOL)inAllowPopups { - BOOL focusURLBar = [MainController isBlankURL:aURLSpec]; + BOOL focusURLBar = !aURLSpec || [aURLSpec isBlankURL]; BrowserWindowController* browser = [self openNewWindow:aLoadInBG]; [browser loadURL:aURLSpec referrer:aReferrer focusContent:!focusURLBar allowPopups:inAllowPopups]; return browser; @@ -3549,7 +3549,7 @@ enum BWCOpenDest { { BrowserTabViewItem* previouslySelected = (BrowserTabViewItem*)[mTabBrowser selectedTabViewItem]; BrowserTabViewItem* newTab = [self openNewTab:aLoadInBG]; - BOOL focusURLBar = [MainController isBlankURL:aURLSpec]; + BOOL focusURLBar = !aURLSpec || [aURLSpec isBlankURL]; // if instructed, tell the tab browser to remember the currently selected tab to // jump back to if this new one is closed w/out switching to any other tabs. diff --git a/camino/src/browser/PageProxyIcon.mm b/camino/src/browser/PageProxyIcon.mm index f1efe1a479d..ec152ee23ac 100644 --- a/camino/src/browser/PageProxyIcon.mm +++ b/camino/src/browser/PageProxyIcon.mm @@ -37,12 +37,12 @@ * * ***** END LICENSE BLOCK ***** */ +#import "PageProxyIcon.h" + +#import "ImageAdditions.h" #import "NSString+Utils.h" #import "NSPasteboard+Utils.h" #import "BrowserWindowController.h" -#import "PageProxyIcon.h" - -#import "MainController.h" #include "nsCRT.h" #include "nsNetUtil.h" @@ -104,7 +104,7 @@ } // don't allow dragging of proxy icon for empty pages - if ((!urlString) || [MainController isBlankURL:urlString]) + if ((!urlString) || [urlString isBlankURL]) return; NSString *cleanedTitle = [titleString stringByReplacingCharactersInSet:[NSCharacterSet controlCharacterSet] withString:@" "]; @@ -114,9 +114,13 @@ [pboard declareURLPasteboardWithAdditionalTypes:[NSArray array] owner:self]; [pboard setDataForURL:urlString title:cleanedTitle]; - [self dragImage: [MainController createImageForDragging:[self image] title:titleString] - at: NSMakePoint(0,0) offset: NSMakeSize(0,0) - event: event pasteboard: pboard source: self slideBack: YES]; + [self dragImage:[NSImage dragImageWithIcon:[self image] title:titleString] + at:NSMakePoint(0,0) + offset:NSMakeSize(0,0) + event:event + pasteboard:pboard + source:self + slideBack:YES]; } @end diff --git a/camino/src/download/SaveHeaderSniffer.mm b/camino/src/download/SaveHeaderSniffer.mm index c8a56f50eac..4ca9078be8f 100644 --- a/camino/src/download/SaveHeaderSniffer.mm +++ b/camino/src/download/SaveHeaderSniffer.mm @@ -252,8 +252,9 @@ nsHeaderSniffer::PerformSave(nsIURI* inOriginalURI) if (url) { nsCAutoString urlFileName; url->GetFileName(urlFileName); // (2) For file URLs, use the file name. - NSString* unescapedString = [NSString unescapedURLString:[NSString stringWithUTF8String:urlFileName.get()]]; - CopyUTF8toUTF16([unescapedString UTF8String], defaultFileName); + NSString* escapedName = [NSString stringWithUTF8String:urlFileName.get()]; + NSString* unescapedName = [escapedName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + CopyUTF8toUTF16([unescapedName UTF8String], defaultFileName); } } diff --git a/camino/src/extensions/ImageAdditions.h b/camino/src/extensions/ImageAdditions.h index 96f6b994c4d..e69de29bb2d 100644 --- a/camino/src/extensions/ImageAdditions.h +++ b/camino/src/extensions/ImageAdditions.h @@ -1,50 +0,0 @@ -/* ***** 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): - * - * - * 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 - -@interface NSImage (ImageAdditions) - -- (void) drawFlippedInRect:(NSRect) rect operation:(NSCompositingOperation) op fraction:(float) delta; -- (void) drawFlippedInRect:(NSRect) rect operation:(NSCompositingOperation) op; - -// the origin is relative to the bottom, left of the window. -- (void)drawTiledInRect:(NSRect)rect origin:(NSPoint)inOrigin operation:(NSCompositingOperation)inOperation; - -- (NSImage*)imageByApplyingBadge:(NSImage*)badge withAlpha:(float)alpha scale:(float)scale; - -@end diff --git a/camino/src/extensions/ImageAdditions.m b/camino/src/extensions/ImageAdditions.m index 175f05227c4..e69de29bb2d 100644 --- a/camino/src/extensions/ImageAdditions.m +++ b/camino/src/extensions/ImageAdditions.m @@ -1,99 +0,0 @@ -/* ***** 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): - * - * - * 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 "ImageAdditions.h" -#import - -@implementation NSImage (ImageAdditions) - -- (void) drawFlippedInRect:(NSRect) rect operation:(NSCompositingOperation) op fraction:(float) delta -{ - CGContextRef context; - - context = [[NSGraphicsContext currentContext] graphicsPort]; - CGContextSaveGState( context ); { - CGContextTranslateCTM( context, 0, NSMaxY( rect ) ); - CGContextScaleCTM( context, 1, -1 ); - - rect.origin.y = 0; - [self drawInRect:rect fromRect:NSZeroRect operation:op fraction:delta]; - } CGContextRestoreGState( context ); -} - -- (void) drawFlippedInRect:(NSRect) rect operation:(NSCompositingOperation) op -{ - [self drawFlippedInRect:rect operation:op fraction:1.0]; -} - -- (void)drawTiledInRect:(NSRect)rect origin:(NSPoint)inOrigin operation:(NSCompositingOperation)inOperation -{ - NSGraphicsContext* gc = [NSGraphicsContext currentContext]; - [gc saveGraphicsState]; - - [gc setPatternPhase:inOrigin]; - - NSColor* patternColor = [NSColor colorWithPatternImage:self]; - [patternColor set]; - NSRectFillUsingOperation(rect, inOperation); - - [gc restoreGraphicsState]; -} - -- (NSImage*)imageByApplyingBadge:(NSImage*)badge withAlpha:(float)alpha scale:(float)scale; -{ - if (!badge) - return self; - - // bad to actually change badge here - [badge setScalesWhenResized:YES]; - [badge setSize:NSMakeSize([self size].width * scale,[self size].height * scale)]; - - // make a new image, copy over our best rep into it - NSImage* newImage = [[[NSImage alloc] initWithSize:[self size]] autorelease]; - NSImageRep* imageRep = [[self bestRepresentationForDevice:nil] copy]; - [newImage addRepresentation:imageRep]; - [imageRep release]; - - [newImage lockFocus]; - [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; - [badge dissolveToPoint:NSMakePoint([self size].width - [badge size].width, 0.0) fraction:alpha]; - [newImage unlockFocus]; - - return newImage; -} - -@end diff --git a/camino/src/extensions/NSString+Utils.h b/camino/src/extensions/NSString+Utils.h index 3979e113c27..d2a2dae84ae 100644 --- a/camino/src/extensions/NSString+Utils.h +++ b/camino/src/extensions/NSString+Utils.h @@ -50,8 +50,6 @@ typedef enum + (id)ellipsisString; + (NSString*)stringWithUUID; -+ (id)escapedURLString:(NSString *)unescapedString; -+ (NSString*)unescapedURLString:(NSString*)escapedString; - (BOOL)isEqualToStringIgnoringCase:(NSString*)inString; - (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet; @@ -77,3 +75,10 @@ typedef enum - (NSString*)displayNameOfLastPathComponent; @end + +@interface NSString (CaminoURLStringUtils) + +// Returns true if the string represents a "blank" URL ("" or "about:blank") +- (BOOL)isBlankURL; + +@end diff --git a/camino/src/extensions/NSString+Utils.m b/camino/src/extensions/NSString+Utils.m index ebe9db40e24..dbdf8928963 100644 --- a/camino/src/extensions/NSString+Utils.m +++ b/camino/src/extensions/NSString+Utils.m @@ -65,26 +65,6 @@ return [uuidString autorelease]; } -+ (id)escapedURLString:(NSString *)unescapedString -{ - NSString *escapedString = - (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, - (CFStringRef)unescapedString, - NULL, - NULL, - kCFStringEncodingUTF8); - return [escapedString autorelease]; -} - -+ (NSString*)unescapedURLString:(NSString*)escapedString -{ - NSString *unescapedString = - (NSString *)CFURLCreateStringByReplacingPercentEscapes(NULL, - (CFStringRef)escapedString, - CFSTR("")); - return [unescapedString autorelease]; -} - - (BOOL)isEqualToStringIgnoringCase:(NSString*)inString { return ([self compare:inString options:NSCaseInsensitiveSearch] == NSOrderedSame); @@ -327,3 +307,12 @@ } @end + +@implementation NSString (CaminoURLStringUtils) + +- (BOOL)isBlankURL +{ + return ([self isEqualToString:@"about:blank"] || [self isEqualToString:@""]); +} + +@end