diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPAppController.h | 2 | ||||
-rw-r--r-- | Source/SPAppController.m | 50 | ||||
-rw-r--r-- | Source/SPEncodingPopupAccessory.h | 41 | ||||
-rw-r--r-- | Source/SPEncodingPopupAccessory.m | 118 | ||||
-rw-r--r-- | Source/TableDocument.h | 4 | ||||
-rw-r--r-- | Source/TableDocument.m | 87 |
6 files changed, 212 insertions, 90 deletions
diff --git a/Source/SPAppController.h b/Source/SPAppController.h index d328d353..6a5f9b6a 100644 --- a/Source/SPAppController.h +++ b/Source/SPAppController.h @@ -32,6 +32,8 @@ BOOL isNewFavorite; SPPreferenceController *prefsController; + + id encodingPopUp; } // IBAction methods diff --git a/Source/SPAppController.m b/Source/SPAppController.m index 2dba0c68..97029a68 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -27,6 +27,7 @@ #import "SPAppController.h" #import "TableDocument.h" #import "SPPreferenceController.h" +#import "SPEncodingPopupAccessory.h" #import <Sparkle/Sparkle.h> @@ -47,6 +48,23 @@ return self; } + +- (void)panelSelectionDidChange:(id)sender +{ + + if([sender isKindOfClass:[NSOpenPanel class]]) { + if([[[[sender filename] pathExtension] lowercaseString] isEqualToString:@"sql"]) { + [encodingPopUp setEnabled:YES]; + [sender setAllowsMultipleSelection:NO]; + } else { + [encodingPopUp setEnabled:NO]; + [sender setAllowsMultipleSelection:YES]; + } + } + +} + + - (IBAction)openConnectionSheet:(id)sender { NSOpenPanel *panel = [NSOpenPanel openPanel]; @@ -55,18 +73,21 @@ [panel setCanChooseDirectories:NO]; [panel setAllowsMultipleSelection:YES]; [panel setResolvesAliases:YES]; - // [panel setAccessoryView:encodingAccessoryView]; - // Set up encoding list - // [encodingPopUp setEnabled:NO]; - + // // Set up encoding list + // If no lastSqlFileEncoding in prefs set it to UTF-8 if(![[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"]) { [[NSUserDefaults standardUserDefaults] setInteger:4 forKey:@"lastSqlFileEncoding"]; [[NSUserDefaults standardUserDefaults] synchronize]; } - // [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; + [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"] + includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; + + [encodingPopUp setEnabled:NO]; + + // [self setupPopUp:encodingPopUp selectedEncoding:[[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; int returnCode = [panel runModalForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"spf", @"sql", nil]]; if( returnCode ) @@ -86,6 +107,11 @@ // Opens a sql file and insert its content into the Custom Query editor if([[[filename pathExtension] lowercaseString] isEqualToString:@"sql"]) { + // if encodingPopUp is defined the filename comes from an openPanel and + // the encodingPopUp contains the chosen encoding; otherwise autodetect encoding + if(encodingPopUp) + [[NSUserDefaults standardUserDefaults] setInteger:[[encodingPopUp selectedItem] tag] forKey:@"lastSqlFileEncoding"]; + // Check if at least one document exists if (![[[NSDocumentController sharedDocumentController] documents] count]) { @@ -94,7 +120,19 @@ // Manually open a new document, setting SPAppController as sender to trigger autoconnection if (firstTableDocument = [[NSDocumentController sharedDocumentController] makeUntitledDocumentOfType:@"DocumentType" error:nil]) { [firstTableDocument setShouldAutomaticallyConnect:NO]; - [firstTableDocument initQueryEditorWithString:[self contentOfFile:filename]]; + if(encodingPopUp) { + NSError *error = nil; + NSString *content = [NSString stringWithContentsOfFile:filename encoding:[[encodingPopUp selectedItem] tag] error:&error]; + if(error != nil) { + NSAlert *errorAlert = [NSAlert alertWithError:error]; + [errorAlert runModal]; + return; + } + [firstTableDocument initQueryEditorWithString:content]; + + } + else + [firstTableDocument initQueryEditorWithString:[self contentOfFile:filename]]; [[NSDocumentController sharedDocumentController] addDocument:firstTableDocument]; [firstTableDocument makeWindowControllers]; [firstTableDocument showWindows]; diff --git a/Source/SPEncodingPopupAccessory.h b/Source/SPEncodingPopupAccessory.h new file mode 100644 index 00000000..7f594095 --- /dev/null +++ b/Source/SPEncodingPopupAccessory.h @@ -0,0 +1,41 @@ +// +// $Id$ +// +// SPEncodingPopupAccessory.h +// sequel-pro +// +// Created by Hans-Jörg Bibiko on August 22, 2009 +// +// 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> + +enum { + NoStringEncoding = 0xFFFFFFFF +}; + +@interface SPEncodingPopupAccessory : NSObject { +@public + IBOutlet NSPopUpButton *encodingPopUp; + IBOutlet NSView *encodingAccessoryView; +} + ++ (NSArray *)enabledEncodings; ++ (NSView *)encodingAccessory:(NSUInteger)encoding includeDefaultEntry:(BOOL)includeDefaultItem encodingPopUp:(NSPopUpButton **)popup; ++ (void)setupPopUp:(NSPopUpButton *)popup selectedEncoding:(NSUInteger)selectedEncoding withDefaultEntry:(BOOL)includeDefaultItem; + +@end diff --git a/Source/SPEncodingPopupAccessory.m b/Source/SPEncodingPopupAccessory.m new file mode 100644 index 00000000..c5b30630 --- /dev/null +++ b/Source/SPEncodingPopupAccessory.m @@ -0,0 +1,118 @@ +// +// $Id$ +// +// SPEncodingPopupAccessory.m +// sequel-pro +// +// Created by Hans-Jörg Bibiko on August 22, 2009 +// +// 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 "SPEncodingPopupAccessory.h" + + +@implementation SPEncodingPopupAccessory + ++ (NSView *)encodingAccessory:(NSUInteger)encoding includeDefaultEntry:(BOOL)includeDefaultItem encodingPopUp:(NSPopUpButton **)popup { + SPEncodingPopupAccessory *owner = [[[SPEncodingPopupAccessory alloc] init] autorelease]; + // Rather than caching, load the accessory view everytime, as it might appear in multiple panels simultaneously. + if (![NSBundle loadNibNamed:@"EncodingPopupView" owner:owner]) { + NSLog(@"Failed to load EncodingPopupView.nib"); + return nil; + } + if (popup) *popup = owner->encodingPopUp; + [[self class] setupPopUp:owner->encodingPopUp selectedEncoding:encoding withDefaultEntry:includeDefaultItem]; + return [owner->encodingAccessoryView autorelease]; +} + +/** + * Returns the actual enabled list of encodings for open/save SQL files. + */ ++ (NSArray *)enabledEncodings +{ + static const NSInteger plainTextFileStringEncodingsSupported[] = { + kCFStringEncodingUTF8, + kCFStringEncodingUTF16, + kCFStringEncodingUTF16BE, + kCFStringEncodingUTF16LE, + kCFStringEncodingUTF32, + kCFStringEncodingWindowsLatin1, + kCFStringEncodingISOLatin1, + kCFStringEncodingWindowsLatin2, + kCFStringEncodingISOLatin2, + kCFStringEncodingISOLatin3, + kCFStringEncodingISOLatin4, + kCFStringEncodingWindowsLatin5, + kCFStringEncodingKOI8_R, + kCFStringEncodingKOI8_U, + kCFStringEncodingMacRoman, + kCFStringEncodingMacJapanese, + kCFStringEncodingShiftJIS, + kCFStringEncodingEUC_JP, + kCFStringEncodingISO_2022_JP, + kCFStringEncodingMacChineseTrad, + kCFStringEncodingMacChineseSimp, + kCFStringEncodingBig5, + kCFStringEncodingGB_18030_2000, + kCFStringEncodingEUC_CN, + kCFStringEncodingEUC_TW, + kCFStringEncodingMacKorean, + kCFStringEncodingEUC_KR, + -1 + }; + NSStringEncoding encoding; + NSInteger cnt = 0; + NSMutableArray *encs = [NSMutableArray array]; + while (plainTextFileStringEncodingsSupported[cnt] != -1) + if ((encoding = CFStringConvertEncodingToNSStringEncoding(plainTextFileStringEncodingsSupported[cnt++])) != kCFStringEncodingInvalidId) + [encs addObject:[NSNumber numberWithUnsignedInteger:encoding]]; + + return encs; +} + +/** + * This method initializes the provided popup with list of encodings; + * it also sets up the selected encoding as indicated and if includeDefaultItem is YES. + * Otherwise the tags are set to the NSStringEncoding value for the encoding. + */ ++ (void)setupPopUp:(NSPopUpButton *)popup selectedEncoding:(NSUInteger)selectedEncoding withDefaultEntry:(BOOL)includeDefaultItem +{ + NSArray *encs = [self enabledEncodings]; + NSUInteger cnt, numEncodings, itemToSelect = 0; + + // Put the encodings in the popup + [popup removeAllItems]; + + // Make sure the initial selected encoding appears in the list + if (!includeDefaultItem && (selectedEncoding != NoStringEncoding) && ![encs containsObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]) encs = [encs arrayByAddingObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]; + + numEncodings = [encs count]; + + // Fill with encodings + for (cnt = 0; cnt < numEncodings; cnt++) { + NSStringEncoding enc = [[encs objectAtIndex:cnt] unsignedIntegerValue]; + [popup addItemWithTitle:[NSString localizedNameOfStringEncoding:enc]]; + [[popup lastItem] setTag:enc]; + [[popup lastItem] setEnabled:YES]; + if (enc == selectedEncoding) itemToSelect = [popup numberOfItems] - 1; + } + + [popup selectItemAtIndex:itemToSelect]; +} + + +@end diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 22c836db..6f36ea83 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -29,10 +29,6 @@ #import <MCPKit/MCPKit.h> #import <WebKit/WebKit.h> -enum { - NoStringEncoding = 0xFFFFFFFF -}; - @class SPConnectionController, SPUserManager; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 70d4089f..cd5967cb 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -49,6 +49,7 @@ #import "SPPrintAccessory.h" #import "QLPreviewPanel.h" #import "SPUserManager.h" +#import "SPEncodingPopupAccessory.h" // Used for printing #import "MGTemplateEngine.h" @@ -1587,82 +1588,6 @@ #pragma mark Menu methods /** - * Returns the actual enabled list of encodings for open/save SQL files. - */ -- (NSArray *)enabledEncodings -{ - static const NSInteger plainTextFileStringEncodingsSupported[] = { - kCFStringEncodingUTF8, - kCFStringEncodingUTF16, - kCFStringEncodingUTF16BE, - kCFStringEncodingUTF16LE, - kCFStringEncodingUTF32, - kCFStringEncodingWindowsLatin1, - kCFStringEncodingISOLatin1, - kCFStringEncodingWindowsLatin2, - kCFStringEncodingISOLatin2, - kCFStringEncodingISOLatin3, - kCFStringEncodingISOLatin4, - kCFStringEncodingWindowsLatin5, - kCFStringEncodingKOI8_R, - kCFStringEncodingKOI8_U, - kCFStringEncodingMacRoman, - kCFStringEncodingMacJapanese, - kCFStringEncodingShiftJIS, - kCFStringEncodingEUC_JP, - kCFStringEncodingISO_2022_JP, - kCFStringEncodingMacChineseTrad, - kCFStringEncodingMacChineseSimp, - kCFStringEncodingBig5, - kCFStringEncodingGB_18030_2000, - kCFStringEncodingEUC_CN, - kCFStringEncodingEUC_TW, - kCFStringEncodingMacKorean, - kCFStringEncodingEUC_KR, - -1 - }; - NSStringEncoding encoding; - NSInteger cnt = 0; - NSMutableArray *encs = [NSMutableArray array]; - while (plainTextFileStringEncodingsSupported[cnt] != -1) - if ((encoding = CFStringConvertEncodingToNSStringEncoding(plainTextFileStringEncodingsSupported[cnt++])) != kCFStringEncodingInvalidId) - [encs addObject:[NSNumber numberWithUnsignedInteger:encoding]]; - - return encs; -} - -/** - * This method initializes the provided popup with list of encodings; - * it also sets up the selected encoding as indicated and if includeDefaultItem is YES. - * Otherwise the tags are set to the NSStringEncoding value for the encoding. - */ -- (void)setupPopUp:(NSPopUpButton *)popup selectedEncoding:(NSUInteger)selectedEncoding withDefaultEntry:(BOOL)includeDefaultItem -{ - NSArray *encs = [self enabledEncodings]; - NSUInteger cnt, numEncodings, itemToSelect = 0; - - // Put the encodings in the popup - [popup removeAllItems]; - - // Make sure the initial selected encoding appears in the list - if (!includeDefaultItem && (selectedEncoding != NoStringEncoding) && ![encs containsObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]) encs = [encs arrayByAddingObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]; - - numEncodings = [encs count]; - - // Fill with encodings - for (cnt = 0; cnt < numEncodings; cnt++) { - NSStringEncoding enc = [[encs objectAtIndex:cnt] unsignedIntegerValue]; - [popup addItemWithTitle:[NSString localizedNameOfStringEncoding:enc]]; - [[popup lastItem] setTag:enc]; - [[popup lastItem] setEnabled:YES]; - if (enc == selectedEncoding) itemToSelect = [popup numberOfItems] - 1; - } - - [popup selectItemAtIndex:itemToSelect]; -} - - -/** * Opens SP session file(s) or a SQL file */ - (IBAction)openConnectionSheet:(id)sender @@ -1674,7 +1599,8 @@ [panel setCanChooseDirectories:NO]; [panel setAllowsMultipleSelection:YES]; [panel setResolvesAliases:YES]; - [panel setAccessoryView:encodingAccessoryView]; + [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[prefs integerForKey:@"lastSqlFileEncoding"] + includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; // Set up encoding list [encodingPopUp setEnabled:NO]; @@ -1685,7 +1611,7 @@ [prefs synchronize]; } - [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; + // [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; [panel beginSheetForDirectory:nil file:@"" @@ -1791,7 +1717,8 @@ if( [sender tag] == 0 ) { // Save the editor's content as SQL file - [panel setAccessoryView:encodingAccessoryView]; + [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[prefs integerForKey:@"lastSqlFileEncoding"] + includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; // [panel setMessage:NSLocalizedString(@"Save SQL file", @"Save SQL file")]; [panel setAllowedFileTypes:[NSArray arrayWithObjects:@"sql", nil]]; if(![prefs stringForKey:@"lastSqlFileName"]) { @@ -1810,7 +1737,7 @@ // Set up encoding list [encodingPopUp setEnabled:YES]; - [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; + // [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; } else if([sender tag] == 1){ |