diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-08-04 19:14:36 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-08-04 19:14:36 +0000 |
commit | 4ce2164c519d4382b67255523c96c9e03f7fdcc6 (patch) | |
tree | 9e6dfab905735ecb5d30268248a52a9dc81db43a /Source | |
parent | 2b92e1f1ddefdde3d341b88f0f09421e05e02953 (diff) | |
download | sequelpro-4ce2164c519d4382b67255523c96c9e03f7fdcc6.tar.gz sequelpro-4ce2164c519d4382b67255523c96c9e03f7fdcc6.tar.bz2 sequelpro-4ce2164c519d4382b67255523c96c9e03f7fdcc6.zip |
• initial support for open/save connection files (*.spf)
- Open/Save menu items are hidden
• initial support for drag&drop files *.spf and *.sql to SP's icon or double-click at *.sql or *.spf files in Finder
- SP starts if not already running
- sql files will be executed in the Custom Query if at least one connection is open
- spf files not yet supported
-
Diffstat (limited to 'Source')
-rw-r--r-- | Source/MainController.h | 1 | ||||
-rw-r--r-- | Source/MainController.m | 117 | ||||
-rw-r--r-- | Source/TableDocument.h | 5 | ||||
-rw-r--r-- | Source/TableDocument.m | 53 |
4 files changed, 176 insertions, 0 deletions
diff --git a/Source/MainController.h b/Source/MainController.h index 59fc0e40..86ec9e4a 100644 --- a/Source/MainController.h +++ b/Source/MainController.h @@ -51,5 +51,6 @@ // Other - (id)handleQuitScriptCommand:(NSScriptCommand *)command; +- (NSString *)contentOfFile:(NSString *)aPath; @end diff --git a/Source/MainController.m b/Source/MainController.m index 25ffdeb9..4e1119a6 100644 --- a/Source/MainController.m +++ b/Source/MainController.m @@ -37,6 +37,48 @@ @implementation MainController + +- (id) init +{ + if ((self = [super init])) { + [NSApp setDelegate: self]; + } + + return self; +} + +/** + * Called if user drag and drops files on Sequel Pro's dock item or double-clicked + * at files *.spf or *.sql + */ +- (void)application:(NSApplication *)app openFiles:(NSArray *)filenames +{ + + for( NSString* filename in filenames ) { + + // Opens a sql file and insert its content to the Custom Query editor + if([[[filename pathExtension] lowercaseString] isEqualToString:@"sql"]) { + + // Check if at least one document exists + if (![[[NSDocumentController sharedDocumentController] documents] count]) { + // TODO : maybe open a connection first + return; + } + + // Pass query to last created document + [[[[NSDocumentController sharedDocumentController] documents] objectAtIndex:([[[NSDocumentController sharedDocumentController] documents] count] - 1)] doPerformQueryService:[self contentOfFile:filename]]; + + } + else if([[[filename pathExtension] lowercaseString] isEqualToString:@"spf"]) { + NSLog(@"open connection %@", filename); + } + else { + NSLog(@"Only files with the extensions ‘spf’ or ‘sql’ are allowed."); + } + } + +} + /** * Called even before init so we can register our preference defaults */ @@ -179,6 +221,81 @@ return NO; } +/* + * Insert content of a plain text file for a given path. + * In addition it tries to figure out the file's text encoding heuristically. + */ +- (NSString *)contentOfFile:(NSString *)aPath +{ + + NSError *err = nil; + NSStringEncoding enc; + NSString *content = nil; + + // Make usage of the UNIX command "file" to get an info + // about file type and encoding. + NSTask *task=[[NSTask alloc] init]; + NSPipe *pipe=[[NSPipe alloc] init]; + NSFileHandle *handle; + NSString *result; + [task setLaunchPath:@"/usr/bin/file"]; + [task setArguments:[NSArray arrayWithObjects:aPath, @"-Ib", nil]]; + [task setStandardOutput:pipe]; + handle=[pipe fileHandleForReading]; + [task launch]; + result=[[NSString alloc] initWithData:[handle readDataToEndOfFile] + encoding:NSASCIIStringEncoding]; + + [pipe release]; + [task release]; + + // UTF16/32 files are detected as application/octet-stream resp. audio/mpeg + if( [result hasPrefix:@"text/plain"] + || [[[aPath pathExtension] lowercaseString] isEqualToString:@"sql"] + || [[[aPath pathExtension] lowercaseString] isEqualToString:@"txt"] + || [result hasPrefix:@"audio/mpeg"] + || [result hasPrefix:@"application/octet-stream"] + ) + { + // if UTF16/32 cocoa will try to find the correct encoding + if([result hasPrefix:@"application/octet-stream"] || [result hasPrefix:@"audio/mpeg"] || [result rangeOfString:@"utf-16"].length) + enc = 0; + else if([result rangeOfString:@"utf-8"].length) + enc = NSUTF8StringEncoding; + else if([result rangeOfString:@"iso-8859-1"].length) + enc = NSISOLatin1StringEncoding; + else if([result rangeOfString:@"us-ascii"].length) + enc = NSASCIIStringEncoding; + else + enc = 0; + + if(enc == 0) // cocoa tries to detect the encoding + content = [NSString stringWithContentsOfFile:aPath usedEncoding:&enc error:&err]; + else + content = [NSString stringWithContentsOfFile:aPath encoding:enc error:&err]; + + if(content) + { + [result release]; + return content; + } + // If UNIX "file" failed try cocoa's encoding detection + content = [NSString stringWithContentsOfFile:aPath encoding:enc error:&err]; + if(content) + { + [result release]; + return content; + } + } + + [result release]; + + NSLog(@"%@ ‘%@’.", NSLocalizedString(@"Couldn't read the file content of", @"Couldn't read the file content of"), aPath); + + return @""; +} + + /** * What exactly is this for? */ diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 29296e9b..c9510a33 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -162,6 +162,9 @@ - (void)closeConnection; - (NSWindow *)getCreateTableSyntaxWindow; - (void) refreshCurrentDatabase; +- (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo; +- (void)saveConnectionPanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo; + // Getter methods - (NSString *)name; @@ -177,6 +180,8 @@ // Menu methods - (BOOL)validateMenuItem:(NSMenuItem *)anItem; +- (IBAction)openConnectionSheet:(id)sender; +- (IBAction)saveConnectionSheet:(id)sender; - (IBAction)import:(id)sender; - (IBAction)export:(id)sender; - (IBAction)exportTable:(id)sender; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index d4a4a53d..e6346ece 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -1481,6 +1481,59 @@ #pragma mark Menu methods /** + * Opens connection file(s) + */ +- (IBAction)openConnectionSheet:(id)sender +{ + + NSOpenPanel *panel = [NSOpenPanel openPanel]; + [panel setCanSelectHiddenExtension:YES]; + [panel setCanChooseDirectories:NO]; + [panel setAllowsMultipleSelection:YES]; + [panel setResolvesAliases:YES]; + + [panel beginSheetForDirectory:nil + file:@"" + types:[NSArray arrayWithObjects:@"spf", nil] + modalForWindow:tableWindow + modalDelegate:self didEndSelector:@selector(openConnectionPanelDidEnd:returnCode:contextInfo:) + contextInfo:NULL]; + +} +- (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo +{ + if ( returnCode ) { + NSArray *fileName = [panel filenames]; + NSLog(@"open: '%@'", [fileName description]); + } +} +/** + * Saves connection(s) to file + */ +- (IBAction)saveConnectionSheet:(id)sender +{ + + NSSavePanel *panel = [NSSavePanel savePanel]; + [panel setAllowedFileTypes:[NSArray arrayWithObjects:@"spf", nil]]; + [panel setAllowsOtherFileTypes:NO]; + [panel setCanSelectHiddenExtension:YES]; + + [panel beginSheetForDirectory:nil + file:[NSString stringWithFormat:@"%@", [self name]] + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:@selector(saveConnectionPanelDidEnd:returnCode:contextInfo:) + contextInfo:NULL]; +} +- (void)saveConnectionPanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo +{ + + if ( returnCode ) { + NSString *fileName = [panel filename]; + NSLog(@"save as: '%@'", fileName); + } +} +/** * Passes the request to the tableDump object */ - (IBAction)import:(id)sender |