aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2010-08-19 13:57:15 +0000
committerBibiko <bibiko@eva.mpg.de>2010-08-19 13:57:15 +0000
commit8915074c6393bdc5cbaa7900299122f0d4e365be (patch)
tree639dc351971b8b7661fa216ebfb41c9406fb2c0e /Source
parent6cf7762d7ae11c75698c11ffee0822b3ead24932 (diff)
downloadsequelpro-8915074c6393bdc5cbaa7900299122f0d4e365be.tar.gz
sequelpro-8915074c6393bdc5cbaa7900299122f0d4e365be.tar.bz2
sequelpro-8915074c6393bdc5cbaa7900299122f0d4e365be.zip
• added SPFileManagerAdditions
- [(NSString*)applicationSupportDirectoryForSubDirectory:error:] 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. • added SPThemesSupportFolder constant
Diffstat (limited to 'Source')
-rw-r--r--Source/SPAppController.m8
-rw-r--r--Source/SPConstants.h4
-rw-r--r--Source/SPConstants.m5
-rw-r--r--Source/SPFileManagerAdditions.h32
-rw-r--r--Source/SPFileManagerAdditions.m143
-rw-r--r--Source/SPPreferenceController.m5
6 files changed, 193 insertions, 4 deletions
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 <http://code.google.com/p/sequel-pro/>
+
+#import <Cocoa/Cocoa.h>
+#import <Foundation/Foundation.h>
+
+@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 <http://code.google.com/p/sequel-pro/>
+
+
+#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;