From 4ce2164c519d4382b67255523c96c9e03f7fdcc6 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 4 Aug 2009 19:14:36 +0000 Subject: =?UTF-8?q?=E2=80=A2=20initial=20support=20for=20open/save=20conne?= =?UTF-8?q?ction=20files=20(*.spf)=20-=20Open/Save=20menu=20items=20are=20?= =?UTF-8?q?hidden=20=E2=80=A2=20initial=20support=20for=20drag&drop=20file?= =?UTF-8?q?s=20*.spf=20and=20*.sql=20to=20SP's=20icon=20or=20double-click?= =?UTF-8?q?=20at=20*.sql=20or=20*.spf=20files=20in=20Finder=20-=20SP=20sta?= =?UTF-8?q?rts=20if=20not=20already=20running=20-=20sql=20files=20will=20b?= =?UTF-8?q?e=20executed=20in=20the=20Custom=20Query=20if=20at=20least=20on?= =?UTF-8?q?e=20connection=20is=20open=20-=20spf=20files=20not=20yet=20supp?= =?UTF-8?q?orted=20-?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/MainController.h | 1 + Source/MainController.m | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ Source/TableDocument.h | 5 +++ Source/TableDocument.m | 53 ++++++++++++++++++++++ 4 files changed, 176 insertions(+) (limited to 'Source') 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 @@ -1480,6 +1480,59 @@ #pragma mark - #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 */ -- cgit v1.2.3