diff options
-rw-r--r-- | Interfaces/English.lproj/MainMenu.xib | 62 | ||||
-rw-r--r-- | Resources/Info.plist | 23 | ||||
-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 |
6 files changed, 256 insertions, 5 deletions
diff --git a/Interfaces/English.lproj/MainMenu.xib b/Interfaces/English.lproj/MainMenu.xib index 6e1e77b2..1380f3b9 100644 --- a/Interfaces/English.lproj/MainMenu.xib +++ b/Interfaces/English.lproj/MainMenu.xib @@ -239,6 +239,26 @@ <reference key="NSOnImage" ref="625762401"/> <reference key="NSMixedImage" ref="315854375"/> </object> + <object class="NSMenuItem" id="849728981"> + <reference key="NSMenu" ref="709725194"/> + <bool key="NSIsHidden">YES</bool> + <string key="NSTitle">Open Connection</string> + <string key="NSKeyEquiv">o</string> + <int key="NSKeyEquivModMask">1048576</int> + <int key="NSMnemonicLoc">2147483647</int> + <reference key="NSOnImage" ref="625762401"/> + <reference key="NSMixedImage" ref="315854375"/> + </object> + <object class="NSMenuItem" id="256715652"> + <reference key="NSMenu" ref="709725194"/> + <bool key="NSIsHidden">YES</bool> + <string key="NSTitle">Save Connection</string> + <string key="NSKeyEquiv">s</string> + <int key="NSKeyEquivModMask">1048576</int> + <int key="NSMnemonicLoc">2147483647</int> + <reference key="NSOnImage" ref="625762401"/> + <reference key="NSMixedImage" ref="315854375"/> + </object> <object class="NSMenuItem" id="320151386"> <reference key="NSMenu" ref="709725194"/> <string key="NSTitle">Add To Favorites</string> @@ -2484,6 +2504,22 @@ </object> <int key="connectionID">942</int> </object> + <object class="IBConnectionRecord"> + <object class="IBActionConnection" key="connection"> + <string key="label">openConnectionSheet:</string> + <reference key="source" ref="63651044"/> + <reference key="destination" ref="849728981"/> + </object> + <int key="connectionID">948</int> + </object> + <object class="IBConnectionRecord"> + <object class="IBActionConnection" key="connection"> + <string key="label">saveConnectionSheet:</string> + <reference key="source" ref="63651044"/> + <reference key="destination" ref="256715652"/> + </object> + <int key="connectionID">949</int> + </object> </object> <object class="IBMutableOrderedSet" key="objectRecords"> <object class="NSArray" key="orderedObjects"> @@ -2706,6 +2742,8 @@ <reference ref="3878246"/> <reference ref="934534577"/> <reference ref="320151386"/> + <reference ref="849728981"/> + <reference ref="256715652"/> </object> <reference key="parent" ref="86447006"/> </object> @@ -3695,6 +3733,16 @@ <reference key="object" ref="784426029"/> <reference key="parent" ref="71086556"/> </object> + <object class="IBObjectRecord"> + <int key="objectID">943</int> + <reference key="object" ref="849728981"/> + <reference key="parent" ref="709725194"/> + </object> + <object class="IBObjectRecord"> + <int key="objectID">944</int> + <reference key="object" ref="256715652"/> + <reference key="parent" ref="709725194"/> + </object> </object> </object> <object class="NSMutableDictionary" key="flattenedProperties"> @@ -4035,6 +4083,8 @@ <string>938.IBPluginDependency</string> <string>940.IBPluginDependency</string> <string>941.IBPluginDependency</string> + <string>943.IBPluginDependency</string> + <string>944.IBPluginDependency</string> </object> <object class="NSMutableArray" key="dict.values"> <bool key="EncodedWithXMLCoder">YES</bool> @@ -4305,7 +4355,7 @@ <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>{{270, 526}, {209, 193}}</string> + <string>{{270, 486}, {212, 233}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <reference ref="9"/> <string>{{180, 535}, {182, 253}}</string> @@ -4415,6 +4465,8 @@ w6gg4oaSIGZhY2FkZV0</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> + <string>com.apple.InterfaceBuilder.CocoaPlugin</string> + <string>com.apple.InterfaceBuilder.CocoaPlugin</string> </object> </object> <object class="NSMutableDictionary" key="unlocalizedProperties"> @@ -4437,7 +4489,7 @@ w6gg4oaSIGZhY2FkZV0</string> </object> </object> <nil key="sourceID"/> - <int key="maxID">942</int> + <int key="maxID">949</int> </object> <object class="IBClassDescriber" key="IBDocument.Classes"> <object class="NSMutableArray" key="referencedPartialClassDescriptions"> @@ -4976,10 +5028,11 @@ w6gg4oaSIGZhY2FkZV0</string> <string>flushPrivileges:</string> <string>flushTable:</string> <string>import:</string> - <string>insertNULLvalue:</string> + <string>openConnectionSheet:</string> <string>optimizeTable:</string> <string>removeDatabase:</string> <string>repairTable:</string> + <string>saveConnectionSheet:</string> <string>saveServerVariables:</string> <string>setDatabases:</string> <string>showCreateTableSyntax:</string> @@ -5024,6 +5077,7 @@ w6gg4oaSIGZhY2FkZV0</string> <string>id</string> <string>id</string> <string>id</string> + <string>id</string> </object> </object> <object class="NSMutableDictionary" key="outlets"> @@ -5045,6 +5099,7 @@ w6gg4oaSIGZhY2FkZV0</string> <string>extendedTableInfoInstance</string> <string>favoritesButton</string> <string>historyControl</string> + <string>listFilterField</string> <string>queryProgressBar</string> <string>saveVariablesButton</string> <string>sidebarGrabber</string> @@ -5087,6 +5142,7 @@ w6gg4oaSIGZhY2FkZV0</string> <string>id</string> <string>id</string> <string>id</string> + <string>NSSearchField</string> <string>id</string> <string>NSButton</string> <string>id</string> diff --git a/Resources/Info.plist b/Resources/Info.plist index 2b7e68d0..f62891db 100644 --- a/Resources/Info.plist +++ b/Resources/Info.plist @@ -9,7 +9,7 @@ <dict> <key>CFBundleTypeExtensions</key> <array> - <string>????</string> + <string>spf</string> </array> <key>CFBundleTypeIconFile</key> <string>appicon.icns</string> @@ -17,7 +17,25 @@ <string>DocumentType</string> <key>CFBundleTypeOSTypes</key> <array> - <string>????</string> + <string>connection</string> + </array> + <key>CFBundleTypeRole</key> + <string>Editor</string> + <key>NSDocumentClass</key> + <string>TableDocument</string> + </dict> + <dict> + <key>CFBundleTypeExtensions</key> + <array> + <string>sql</string> + </array> + <key>CFBundleTypeIconFile</key> + <string>appicon.icns</string> + <key>CFBundleTypeName</key> + <string>DocumentType</string> + <key>CFBundleTypeOSTypes</key> + <array> + <string>sql text</string> </array> <key>CFBundleTypeRole</key> <string>Editor</string> @@ -75,6 +93,7 @@ <key>NSSendTypes</key> <array> <string>NSStringPboardType</string> + <string>NSURLPboardType</string> </array> </dict> </array> 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 |