From 8915074c6393bdc5cbaa7900299122f0d4e365be Mon Sep 17 00:00:00 2001 From: Bibiko Date: Thu, 19 Aug 2010 13:57:15 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20SPFileManagerAdditions=20-=20[(NSSt?= =?UTF-8?q?ring*)applicationSupportDirectoryForSubDirectory:error:]=20Retu?= =?UTF-8?q?rn=20the=20application=20support=20folder=20of=20the=20current?= =?UTF-8?q?=20application=20for=20'subDirectory'.=20If=20this=20folder=20d?= =?UTF-8?q?oesn't=20exist=20it=20will=20be=20created.=20If=20'subDirectory?= =?UTF-8?q?'=20=3D=3D=20nil=20it=20only=20returns=20the=20application=20su?= =?UTF-8?q?pport=20folder=20of=20the=20current=20application.=20=E2=80=A2?= =?UTF-8?q?=20added=20SPThemesSupportFolder=20constant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPAppController.m | 8 ++- Source/SPConstants.h | 4 +- Source/SPConstants.m | 5 +- Source/SPFileManagerAdditions.h | 32 +++++++++ Source/SPFileManagerAdditions.m | 143 ++++++++++++++++++++++++++++++++++++++++ Source/SPPreferenceController.m | 5 +- 6 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 Source/SPFileManagerAdditions.h create mode 100644 Source/SPFileManagerAdditions.m (limited to 'Source') diff --git a/Source/SPAppController.m b/Source/SPAppController.m index b03603bd..88890c46 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -424,14 +424,20 @@ [spfs release]; } else if([[[filename pathExtension] lowercaseString] isEqualToString:[SPColorThemeFileExtension lowercaseString]]) { + NSFileManager *fm = [NSFileManager defaultManager]; - NSString *themePath = [[NSString stringWithString:@"~/Library/Application Support/Sequel Pro/Themes"] stringByExpandingTildeInPath]; + + NSString *themePath = [[NSFileManager defaultManager] applicationSupportDirectoryForSubDirectory:SPThemesSupportFolder error:nil]; + + if(!themePath) return; + if(![fm fileExistsAtPath:themePath isDirectory:nil]) { if(![fm createDirectoryAtPath:themePath withIntermediateDirectories:YES attributes:nil error:nil]) { NSBeep(); return; } } + NSString *newPath = [NSString stringWithFormat:@"%@/%@", themePath, [filename lastPathComponent]]; if(![fm fileExistsAtPath:newPath isDirectory:nil]) { if(![fm copyItemAtPath:filename toPath:newPath error:nil]) { diff --git a/Source/SPConstants.h b/Source/SPConstants.h index 9086128a..1ee44264 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -224,11 +224,13 @@ extern NSString *SPFileExtensionSQL; extern NSString *SPBundleFileExtension; extern NSString *SPColorThemeFileExtension; -// Filenames +// File names extern NSString *SPHTMLPrintTemplate; extern NSString *SPHTMLTableInfoPrintTemplate; extern NSString *SPHTMLHelpTemplate; +// Folder names +extern NSString *SPThemesSupportFolder; // Preference key constants // General Prefpane diff --git a/Source/SPConstants.m b/Source/SPConstants.m index 8fa5aadd..dd6758b1 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -48,11 +48,14 @@ NSString *SPBundleFileExtension = @"spfs"; NSString *SPFileExtensionSQL = @"sql"; NSString *SPColorThemeFileExtension = @"spTheme"; -// Filenames +// File names NSString *SPHTMLPrintTemplate = @"sequel-pro-print-template"; NSString *SPHTMLTableInfoPrintTemplate = @"sequel-pro-table-info-print-template"; NSString *SPHTMLHelpTemplate = @"sequel-pro-mysql-help-template"; +// Folder names +NSString *SPThemesSupportFolder = @"Themes"; + // Preference key constants // General Prefpane NSString *SPDefaultFavorite = @"DefaultFavorite"; diff --git a/Source/SPFileManagerAdditions.h b/Source/SPFileManagerAdditions.h new file mode 100644 index 00000000..70de36f0 --- /dev/null +++ b/Source/SPFileManagerAdditions.h @@ -0,0 +1,32 @@ +// +// $Id$ +// +// SPFileManagerAdditions.h +// sequel-pro +// +// Created by Hans-Jörg Bibiko on August 19, 2010 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import +#import + +@interface NSFileManager (SPFileManagerAdditions) + +- (NSString*)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory error:(NSError **)errorOut; + +@end diff --git a/Source/SPFileManagerAdditions.m b/Source/SPFileManagerAdditions.m new file mode 100644 index 00000000..179f33d7 --- /dev/null +++ b/Source/SPFileManagerAdditions.m @@ -0,0 +1,143 @@ +// +// $Id$ +// +// SPFileManagerAdditions.m +// sequel-pro +// +// Created by Hans-Jörg Bibiko on August 19, 2010 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + + +#import "SPFileManagerAdditions.h" + +enum +{ + DirectoryLocationErrorNoPathFound, + DirectoryLocationErrorFileExistsAtLocation +}; + +NSString* const DirectoryLocationDomain = @"DirectoryLocationDomain"; + + +@implementation NSFileManager (SPFileManagerAdditions) + +/* + * Return the application support folder of the current application for 'subDirectory'. + * If this folder doesn't exist it will be created. If 'subDirectory' == nil it only returns + * the application support folder of the current application. + */ +- (NSString*)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory error:(NSError **)errorOut +{ + // Based on Matt Gallagher on 06 May 2010 + // + // Permission is given to use this source code file, free of charge, in any + // project, commercial or otherwise, entirely at your risk, with the condition + // that any redistribution (in part or whole) of source code must retain + // this copyright and permission notice. Attribution in compiled projects is + // appreciated but not required. + // + + NSError *error; + + NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + + if (![paths count]) { + if (errorOut) { + NSDictionary *userInfo = + [NSDictionary dictionaryWithObjectsAndKeys: + NSLocalizedStringFromTable( + @"No path found for directory in domain.", + @"Errors", + nil), + NSLocalizedDescriptionKey, + [NSNumber numberWithInteger:NSApplicationSupportDirectory], + @"NSSearchPathDirectory", + [NSNumber numberWithInteger:NSUserDomainMask], + @"NSSearchPathDomainMask", + nil]; + *errorOut = [NSError + errorWithDomain:DirectoryLocationDomain + code:DirectoryLocationErrorNoPathFound + userInfo:userInfo]; + } + return nil; + } + + // Use only the first path returned + NSString *resolvedPath = [paths objectAtIndex:0]; + + // Append the application name + resolvedPath = [resolvedPath stringByAppendingPathComponent:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]]; + + // Append the subdirectory if passed + if (subDirectory) + resolvedPath = [resolvedPath stringByAppendingPathComponent:subDirectory]; + + // Check if the path exists already + BOOL exists; + BOOL isDirectory; + exists = [self fileExistsAtPath:resolvedPath isDirectory:&isDirectory]; + if (!exists || !isDirectory) { + if (exists) { + if (errorOut) { + NSDictionary *userInfo = + [NSDictionary dictionaryWithObjectsAndKeys: + NSLocalizedStringFromTable( + @"File exists at requested directory location.", + @"Errors", + nil), + NSLocalizedDescriptionKey, + [NSNumber numberWithInteger:NSApplicationSupportDirectory], + @"NSSearchPathDirectory", + [NSNumber numberWithInteger:NSUserDomainMask], + @"NSSearchPathDomainMask", + nil]; + *errorOut = [NSError + errorWithDomain:DirectoryLocationDomain + code:DirectoryLocationErrorFileExistsAtLocation + userInfo:userInfo]; + } + return nil; + } + + // Create the path if it doesn't exist + NSError *error = nil; + BOOL success = [self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error]; + if (!success) { + if (errorOut) { + *errorOut = error; + } + return nil; + } + } + + if (errorOut) + *errorOut = nil; + + if (!resolvedPath) { + NSBeep(); + NSLog(@"Unable to find or create application support directory:\n%@", error); + } + + + return resolvedPath; + + +} + +@end diff --git a/Source/SPPreferenceController.m b/Source/SPPreferenceController.m index 79f18b62..2810eeb6 100644 --- a/Source/SPPreferenceController.m +++ b/Source/SPPreferenceController.m @@ -31,6 +31,7 @@ #import "SPConnectionController.h" #import "SPColorAdditions.h" #import "SPColorWellCell.h" +#import "SPFileManagerAdditions.h" @interface SPPreferenceController (PrivateAPI) @@ -65,8 +66,10 @@ [NSColor setIgnoresAlpha:NO]; - themePath = [[[NSString stringWithString:@"~/Library/Application Support/Sequel Pro/Themes"] stringByExpandingTildeInPath] retain]; + themePath = [[[NSFileManager defaultManager] applicationSupportDirectoryForSubDirectory:SPThemesSupportFolder error:nil] retain]; + editThemeListItems = [[NSArray arrayWithArray:[self getAvailableThemes]] retain]; + } return self; -- cgit v1.2.3