diff options
author | abhibeckert <abhi@abhibeckert.com> | 2008-09-29 09:49:36 +0000 |
---|---|---|
committer | abhibeckert <abhi@abhibeckert.com> | 2008-09-29 09:49:36 +0000 |
commit | 8b39f25ee75d7db1eff68aaa71b92d3282e4c054 (patch) | |
tree | 75e5fd2f66210a5d7170f92c40903fe9fe1c5b57 /trunk/MainController.m | |
parent | c0e7a42a5deba6b1529db959411a416ab114edba (diff) | |
download | sequelpro-8b39f25ee75d7db1eff68aaa71b92d3282e4c054.tar.gz sequelpro-8b39f25ee75d7db1eff68aaa71b92d3282e4c054.tar.bz2 sequelpro-8b39f25ee75d7db1eff68aaa71b92d3282e4c054.zip |
tag release candidate 2 of 0.9.2 releaserelease-0.9.2
Diffstat (limited to 'trunk/MainController.m')
-rw-r--r-- | trunk/MainController.m | 829 |
1 files changed, 829 insertions, 0 deletions
diff --git a/trunk/MainController.m b/trunk/MainController.m new file mode 100644 index 00000000..548c0434 --- /dev/null +++ b/trunk/MainController.m @@ -0,0 +1,829 @@ +// +// MainController.m +// sequel-pro +// +// Created by lorenz textor (lorenz@textor.ch) on Wed May 01 2002. +// Copyright (c) 2002-2003 Lorenz Textor. All rights reserved. +// +// 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/> +// Or mail to <lorenz@textor.ch> + +#import "MainController.h" +#import "KeyChain.h" +#import "TableDocument.h" + + +@implementation MainController + +- (IBAction)openPreferences:(id)sender +/* +opens the preferences window +*/ +{ +//get favorites if they exist + [favorites release]; + if ( [prefs objectForKey:@"favorites"] != nil ) { + favorites = [[NSMutableArray alloc] initWithArray:[prefs objectForKey:@"favorites"]]; + } else { + favorites = [[NSMutableArray array] retain]; + } + [tableView reloadData]; + + if ( [prefs boolForKey:@"reloadAfterAdding"] ) { + [reloadAfterAddingSwitch setState:NSOnState]; + } else { + [reloadAfterAddingSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"reloadAfterEditing"] ) { + [reloadAfterEditingSwitch setState:NSOnState]; + } else { + [reloadAfterEditingSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"reloadAfterRemoving"] ) { + [reloadAfterRemovingSwitch setState:NSOnState]; + } else { + [reloadAfterRemovingSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"showError"] ) { + [showErrorSwitch setState:NSOnState]; + } else { + [showErrorSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"dontShowBlob"] ) { + [dontShowBlobSwitch setState:NSOnState]; + } else { + [dontShowBlobSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"limitRows"] ) { + [limitRowsSwitch setState:NSOnState]; + } else { + [limitRowsSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"useMonospacedFonts"] ) { + [useMonospacedFontsSwitch setState:NSOnState]; + } else { + [useMonospacedFontsSwitch setState:NSOffState]; + } + if ( [prefs boolForKey:@"fetchRowCount"] ) { + [fetchRowCountSwitch setState:NSOnState]; + } else { + [fetchRowCountSwitch setState:NSOffState]; + } + [nullValueField setStringValue:[prefs stringForKey:@"nullValue"]]; + [limitRowsField setStringValue:[prefs stringForKey:@"limitRowsValue"]]; + [self chooseLimitRows:self]; + [encodingPopUpButton selectItemWithTitle:[prefs stringForKey:@"encoding"]]; + + [preferencesWindow makeKeyAndOrderFront:self]; +} + +- (IBAction)addFavorite:(id)sender +/* +adds a favorite +*/ +{ + int code; + + isNewFavorite = YES; + + [nameField setStringValue:@""]; + [hostField setStringValue:@""]; + [socketField setStringValue:@""]; + [userField setStringValue:@""]; + [passwordField setStringValue:@""]; + [portField setStringValue:@""]; + [databaseField setStringValue:@""]; + [sshCheckbox setState:NSOffState]; + [sshUserField setEnabled:NO]; + [sshPasswordField setEnabled:NO]; + [sshHostField setEnabled:NO]; + [sshPortField setEnabled:NO]; + [sshHostField setStringValue:@""]; + [sshUserField setStringValue:@""]; + [sshPortField setStringValue:@"8888"]; + [sshPasswordField setStringValue:@""]; + + [NSApp beginSheet:favoriteSheet + modalForWindow:preferencesWindow modalDelegate:self + didEndSelector:nil contextInfo:nil]; + code = [NSApp runModalForWindow:favoriteSheet]; + + [NSApp endSheet:favoriteSheet]; + [favoriteSheet orderOut:nil]; + + if ( code == 1 ) { + if ( ![[socketField stringValue] isEqualToString:@""] ) { + //set host to localhost if socket is used + [hostField setStringValue:@"localhost"]; + } + // get ssh settings + NSString *sshHost, *sshUser, *sshPassword, *sshPort; + NSNumber *ssh; + if ( [sshCheckbox state] == NSOnState ) { + if ( [[sshHostField stringValue] isEqualToString:@""] ) { + sshHost = [hostField stringValue]; + } else { + sshHost = [sshHostField stringValue]; + } + if ( [[sshUserField stringValue] isEqualToString:@""] ) { + sshUser = [userField stringValue]; + } else { + sshUser = [sshUserField stringValue]; + } + if ( [[sshPasswordField stringValue] isEqualToString:@""] ) { + sshPassword = [passwordField stringValue]; + } else { + sshPassword = [sshPasswordField stringValue]; + } + if ( [[sshPortField stringValue] isEqualToString:@""] ) { + sshPort = [portField stringValue]; + } else { + sshPort = [sshPortField stringValue]; + } + ssh = [NSNumber numberWithInt:1]; + } else { + sshHost = @""; + sshUser = @""; + sshPassword = @""; + sshPort = @""; + ssh = [NSNumber numberWithInt:0]; + } + NSDictionary *favorite = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:[nameField stringValue], [hostField stringValue], [socketField stringValue], [userField stringValue], [portField stringValue], [databaseField stringValue], ssh, sshHost, sshUser, sshPort, nil] + forKeys:[NSArray arrayWithObjects:@"name", @"host", @"socket", @"user", @"port", @"database", @"useSSH", @"sshHost", @"sshUser", @"sshPort", nil]]; + [favorites addObject:favorite]; + if ( ![[passwordField stringValue] isEqualToString:@""] ) + [keyChainInstance addPassword:[passwordField stringValue] + forName:[NSString stringWithFormat:@"Sequel Pro : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]; + if ( ![sshPassword isEqualToString:@""] ) + [keyChainInstance addPassword:sshPassword + forName:[NSString stringWithFormat:@"Sequel Pro SSHTunnel : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]; + [tableView reloadData]; + [tableView selectRow:[tableView numberOfRows]-1 byExtendingSelection:NO]; + } + + isNewFavorite = NO; +} + +- (IBAction)removeFavorite:(id)sender +/* +removes a favorite +*/ +{ + if ( ![tableView numberOfSelectedRows] ) + return; + + NSString *name = [[favorites objectAtIndex:[tableView selectedRow]] objectForKey:@"name"]; + NSString *user = [[favorites objectAtIndex:[tableView selectedRow]] objectForKey:@"user"]; + NSString *host = [[favorites objectAtIndex:[tableView selectedRow]] objectForKey:@"host"]; + NSString *database = [[favorites objectAtIndex:[tableView selectedRow]] objectForKey:@"database"]; + [keyChainInstance deletePasswordForName:[NSString stringWithFormat:@"Sequel Pro : %@", name] + account:[NSString stringWithFormat:@"%@@%@/%@", user, host, database]]; + [keyChainInstance deletePasswordForName:[NSString stringWithFormat:@"Sequel Pro SSHTunnel : %@", name] + account:[NSString stringWithFormat:@"%@@%@/%@", user, host, database]]; + [favorites removeObjectAtIndex:[tableView selectedRow]]; + [tableView reloadData]; +} + +- (IBAction)copyFavorite:(id)sender +/* +copies a favorite +*/ +{ + if ( ![tableView numberOfSelectedRows] ) + return; + + NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithDictionary:[favorites objectAtIndex:[tableView selectedRow]]]; + [tempDictionary setObject:[NSString stringWithFormat:@"%@Copy", [tempDictionary objectForKey:@"name"]] forKey:@"name"]; +// [tempDictionary setObject:[NSString stringWithFormat:@"%@Copy", [tempDictionary objectForKey:@"user"]] forKey:@"user"]; + + [favorites insertObject:tempDictionary atIndex:[tableView selectedRow]+1]; + [tableView selectRow:[tableView selectedRow]+1 byExtendingSelection:NO]; + + [tableView reloadData]; +} + +- (IBAction)chooseLimitRows:(id)sender +/* +enables or disables limitRowsField (depending on the state of limitRowsSwitch) +*/ +{ + if ( [limitRowsSwitch state] == NSOnState ) { + [limitRowsField setEnabled:YES]; + [limitRowsField selectText:self]; + } else { + [limitRowsField setEnabled:NO]; + } +} + +- (IBAction)closeFavoriteSheet:(id)sender +/* +close the favoriteSheet and save favorite if user hit save +*/ +{ + NSEnumerator *enumerator = [favorites objectEnumerator]; + id favorite; + int count; + +//test if user has entered at least name and host/socket + if ( [sender tag] && + ([[nameField stringValue] isEqualToString:@""] || + ([[hostField stringValue] isEqualToString:@""] && [[socketField stringValue] isEqualToString:@""])) ) { + NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"Please enter at least name and host or socket!", @"message of panel when name/host/socket are missing"), NSLocalizedString(@"OK", @"OK button"), nil, nil); + return; + } + +//test if favorite name isn't used by another favorite + count = 0; + if ( [sender tag] ) { + while ( (favorite = [enumerator nextObject]) ) { + if ( [[favorite objectForKey:@"name"] isEqualToString:[nameField stringValue]] ) + { + if ( isNewFavorite || (!isNewFavorite && (count != [tableView selectedRow])) ) { + NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), [NSString stringWithFormat:NSLocalizedString(@"Favorite %@ has already been saved!\nPlease specify another name.", @"message of panel when favorite name has already been used"), [nameField stringValue]], NSLocalizedString(@"OK", @"OK button"), nil, nil); + return; + } + } +/* + if ( [[favorite objectForKey:@"host"] isEqualToString:[hostField stringValue]] && + [[favorite objectForKey:@"user"] isEqualToString:[userField stringValue]] && + [[favorite objectForKey:@"database"] isEqualToString:[databaseField stringValue]] ) { + if ( isNewFavorite || (!isNewFavorite && (count != [tableView selectedRow])) ) { + NSRunAlertPanel(@"Error", @"There is already a favorite with the same host, user and database!", @"OK", nil, nil); + return; + } + } +*/ + count++; + } + } + + [NSApp stopModalWithCode:[sender tag]]; +} + +- (IBAction)toggleUseSSH:(id)sender +/* +enables/disables ssh tunneling +*/ +{ + if ( [sshCheckbox state] == NSOnState ) { + [sshUserField setEnabled:YES]; + [sshPasswordField setEnabled:YES]; + [sshHostField setEnabled:YES]; + [sshPortField setEnabled:YES]; + } else { + [sshUserField setEnabled:NO]; + [sshPasswordField setEnabled:NO]; + [sshHostField setEnabled:NO]; + [sshPortField setEnabled:NO]; + } +} + +//services menu methods +- (void)doPerformQueryService:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error +/* +passes the query to the last created document +*/ +{ + NSString *pboardString; + NSArray *types; + + types = [pboard types]; + + if (![types containsObject:NSStringPboardType] || !(pboardString = [pboard stringForType:NSStringPboardType])) { + *error = @"Pasteboard couldn't give string."; + return; + } + +//check if there exists a document + if ( ![[[NSDocumentController sharedDocumentController] documents] count] ) { + *error = @"No Documents open!"; + return; + } + +//pass query to last created document +// [[[NSDocumentController sharedDocumentController] currentDocument] doPerformQueryService:pboardString]; + [[[[NSDocumentController sharedDocumentController] documents] objectAtIndex:[[[NSDocumentController sharedDocumentController] documents] count]-1] doPerformQueryService:pboardString]; + + return; +} + + +//menu methods +- (IBAction)donate:(id)sender +/* +opens donate link in default browser +*/ +{ + [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/sequel-pro/wiki/Donations"]]; +} + +//menu methods +- (IBAction)visitWebsite:(id)sender +/* +opens donate link in default browser +*/ +{ + [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/sequel-pro"]]; +} + +- (IBAction)visitHelpWebsite:(id)sender +/* +opens donate link in default browser +*/ +{ + [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/sequel-pro/wiki/FAQ"]]; +} + +- (IBAction)checkForUpdates:(id)sender +/* +checks for updates and opens download page in default browser +*/ +{ + NSLog(@"[MainController checkForUpdates:] is not currently functional."); +// CMMCPConnection *tempConnection = [[CMMCPConnection alloc] initToHost:@"com.google.code.sequel-pro" +// withLogin:@"sequel-pro" +// password:@"" +// usingPort:nil]; +// CMMCPResult *tempResult; +// NSString *version; +// int code; +// +// //connect to db +// if ( ![tempConnection isConnected] ) { +// //no connection +// NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"Error while trying to get the current version number!\nBe sure that you are connected to the internet and try again later.", @"message of panel when check for current version number failed"), NSLocalizedString(@"OK", @"OK button"), nil, nil); +// return; +// } +// if ( ![tempConnection selectDB:@"usr_web8_1"] ) { +// //db not found +// NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"Error while trying to get the current version number!\nBe sure that you are connected to the internet and try again later.", @"message of panel when check for current version number failed"), NSLocalizedString(@"OK", @"OK button"), nil, nil); +// return; +// } +// //get current version +// tempResult = [tempConnection queryString:@"SELECT * FROM sequel-pro"]; +// if ( ![tempResult numOfRows] ) { +// //error in query +// NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"Error while trying to get the current version number!\nBe sure that you are connected to the internet and try again later.", @"message of panel when check for current version number failed"), NSLocalizedString(@"OK", @"OK button"), nil, nil); +// return; +// } +// version = [[tempResult fetchRowAsArray] objectAtIndex:0]; +// //check versions +// if ( [version isEqualToString:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] ) { +// //no new version +// NSRunInformationalAlertPanel(NSLocalizedString(@"No update available", @"title of panel when no update is available"), NSLocalizedString(@"There is no newer version available!", @"message of panel when no update is available"), NSLocalizedString(@"OK", @"OK button"), nil, nil); +// } else { +// //new version available +// code = NSRunInformationalAlertPanel(NSLocalizedString(@"Update available", @"title of panel when update is available"), [NSString stringWithFormat:NSLocalizedString(@"There is a newer version available (version %@)!\nClick OK to open the download site.", @"message of panel when update is available"), version], NSLocalizedString(@"OK", @"OK button"), NSLocalizedString(@"Cancel", @"cancel button"), nil); +// if ( code == NSAlertDefaultReturn ) { +// [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/sequel-pro/download.php"]]; +// } +// } +// [tempConnection disconnect]; +// [tempConnection release]; +} + + +//tableView datasource methods +- (int)numberOfRowsInTableView:(NSTableView *)aTableView +{ + return [favorites count]; +} + +- (id)tableView:(NSTableView *)aTableView + objectValueForTableColumn:(NSTableColumn *)aTableColumn + row:(int)rowIndex +{ + return [[favorites objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]]; +} + + +//tableView drag&drop datasource methods +- (BOOL)tableView:(NSTableView *)tv writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard +{ + int originalRow; + NSArray *pboardTypes; + + if ( [rows count] == 1 ) { + pboardTypes=[NSArray arrayWithObjects:@"SequelProPreferencesPasteboard", nil]; + originalRow = [[rows objectAtIndex:0] intValue]; + + [pboard declareTypes:pboardTypes owner:nil]; + [pboard setString:[[NSNumber numberWithInt:originalRow] stringValue] forType:@"SequelProPreferencesPasteboard"]; + + return YES; + } else { + return NO; + } +} + +- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row + proposedDropOperation:(NSTableViewDropOperation)operation +{ + NSArray *pboardTypes = [[info draggingPasteboard] types]; + int originalRow; + + if ([pboardTypes count] == 1 && row != -1) + { + if ([[pboardTypes objectAtIndex:0] isEqualToString:@"SequelProPreferencesPasteboard"]==YES && operation==NSTableViewDropAbove) + { + originalRow = [[[info draggingPasteboard] stringForType:@"SequelProPreferencesPasteboard"] intValue]; + + if (row != originalRow && row != (originalRow+1)) + { + return NSDragOperationMove; + } + } + } + + return NSDragOperationNone; +} + +- (BOOL)tableView:(NSTableView*)tv acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation +{ + int originalRow; + int destinationRow; + NSMutableDictionary *draggedRow; + + originalRow = [[[info draggingPasteboard] stringForType:@"SequelProPreferencesPasteboard"] intValue]; + destinationRow = row; + + if ( destinationRow > originalRow ) + destinationRow--; + + draggedRow = [NSMutableDictionary dictionaryWithDictionary:[favorites objectAtIndex:originalRow]]; + [favorites removeObjectAtIndex:originalRow]; + [favorites insertObject:draggedRow atIndex:destinationRow]; + + [tableView reloadData]; + [tableView selectRow:destinationRow byExtendingSelection:NO]; + + return YES; +} + +- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex +/* +opens sheet to edit favorite and saves favorite if user hit OK +*/ +{ + int code; + NSDictionary *favorite = [favorites objectAtIndex:rowIndex]; + +// set up fields + [nameField setStringValue:[favorite objectForKey:@"name"]]; + [hostField setStringValue:[favorite objectForKey:@"host"]]; + [socketField setStringValue:[favorite objectForKey:@"socket"]]; + [userField setStringValue:[favorite objectForKey:@"user"]]; + [portField setStringValue:[favorite objectForKey:@"port"]]; + [databaseField setStringValue:[favorite objectForKey:@"database"]]; + [passwordField setStringValue:[keyChainInstance + getPasswordForName:[NSString stringWithFormat:@"Sequel Pro : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]]; +// set up ssh fields + if ( [[favorite objectForKey:@"useSSH"] intValue] == 1 ) { + [sshCheckbox setState:NSOnState]; + [sshUserField setEnabled:YES]; + [sshPasswordField setEnabled:YES]; + [sshHostField setEnabled:YES]; + [sshPortField setEnabled:YES]; + [sshHostField setStringValue:[favorite objectForKey:@"sshHost"]]; + [sshUserField setStringValue:[favorite objectForKey:@"sshUser"]]; + [sshPortField setStringValue:[favorite objectForKey:@"sshPort"]]; + [sshPasswordField setStringValue:[keyChainInstance + getPasswordForName:[NSString stringWithFormat:@"Sequel Pro SSHTunnel : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]]; + } else { + [sshCheckbox setState:NSOffState]; + [sshUserField setEnabled:NO]; + [sshPasswordField setEnabled:NO]; + [sshHostField setEnabled:NO]; + [sshPortField setEnabled:NO]; + [sshHostField setStringValue:@""]; + [sshUserField setStringValue:@""]; + [sshPortField setStringValue:@""]; + [sshPasswordField setStringValue:@""]; + } + + +// run sheet + [NSApp beginSheet:favoriteSheet + modalForWindow:preferencesWindow modalDelegate:self + didEndSelector:nil contextInfo:nil]; + code = [NSApp runModalForWindow:favoriteSheet]; + + [NSApp endSheet:favoriteSheet]; + [favoriteSheet orderOut:nil]; + + if ( code == 1 ) { + if ( ![[socketField stringValue] isEqualToString:@""] ) { + //set host to localhost if socket is used + [hostField setStringValue:@"localhost"]; + } +//get ssh settings + NSString *sshHost, *sshUser, *sshPassword, *sshPort; + NSNumber *ssh; + if ( [sshCheckbox state] == NSOnState ) { + if ( [[sshHostField stringValue] isEqualToString:@""] ) { + sshHost = [hostField stringValue]; + } else { + sshHost = [sshHostField stringValue]; + } + if ( [[sshUserField stringValue] isEqualToString:@""] ) { + sshUser = [userField stringValue]; + } else { + sshUser = [sshUserField stringValue]; + } + if ( [[sshPasswordField stringValue] isEqualToString:@""] ) { + sshPassword = [passwordField stringValue]; + } else { + sshPassword = [sshPasswordField stringValue]; + } + if ( [[sshPortField stringValue] isEqualToString:@""] ) { + sshPort = [portField stringValue]; + } else { + sshPort = [sshPortField stringValue]; + } + ssh = [NSNumber numberWithInt:1]; + } else { + sshHost = @""; + sshUser = @""; + sshPassword = @""; + sshPort = @""; + ssh = [NSNumber numberWithInt:0]; + } +//replace password + [keyChainInstance deletePasswordForName:[NSString stringWithFormat:@"Sequel Pro : %@", [favorite objectForKey:@"name"]] + account:[NSString stringWithFormat:@"%@@%@/%@", [favorite objectForKey:@"user"], + [favorite objectForKey:@"host"], [favorite objectForKey:@"database"]]]; + if ( ![[passwordField stringValue] isEqualToString:@""] ) + [keyChainInstance addPassword:[passwordField stringValue] + forName:[NSString stringWithFormat:@"Sequel Pro : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]; +//replace ssh password + [keyChainInstance deletePasswordForName:[NSString stringWithFormat:@"Sequel Pro SSHTunnel : %@", [favorite objectForKey:@"name"]] + account:[NSString stringWithFormat:@"%@@%@/%@", [favorite objectForKey:@"user"], + [favorite objectForKey:@"host"], [favorite objectForKey:@"database"]]]; + if ( ([sshCheckbox state] == NSOnState) && ![sshPassword isEqualToString:@""] ) { + [keyChainInstance addPassword:sshPassword + forName:[NSString stringWithFormat:@"Sequel Pro SSHTunnel : %@", [nameField stringValue]] + account:[NSString stringWithFormat:@"%@@%@/%@", [userField stringValue], [hostField stringValue], + [databaseField stringValue]]]; + } +//replace favorite + favorite = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:[nameField stringValue], [hostField stringValue], [socketField stringValue], [userField stringValue], [portField stringValue], [databaseField stringValue], ssh, sshHost, sshUser, sshPort, nil] + forKeys:[NSArray arrayWithObjects:@"name", @"host", @"socket", @"user", @"port", @"database", @"useSSH", @"sshHost", @"sshUser", @"sshPort", nil]]; + [favorites replaceObjectAtIndex:rowIndex withObject:favorite]; + [tableView reloadData]; + } + + return NO; +} + + +//window delegate methods +- (BOOL)windowShouldClose:(id)sender +/* +saves the preferences +*/ +{ + if ( sender == preferencesWindow ) { + if ( [reloadAfterAddingSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"reloadAfterAdding"]; + } else { + [prefs setBool:NO forKey:@"reloadAfterAdding"]; + } + if ( [reloadAfterEditingSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"reloadAfterEditing"]; + } else { + [prefs setBool:NO forKey:@"reloadAfterEditing"]; + } + if ( [reloadAfterRemovingSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"reloadAfterRemoving"]; + } else { + [prefs setBool:NO forKey:@"reloadAfterRemoving"]; + } + if ( [showErrorSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"showError"]; + } else { + [prefs setBool:NO forKey:@"showError"]; + } + if ( [dontShowBlobSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"dontShowBlob"]; + } else { + [prefs setBool:NO forKey:@"dontShowBlob"]; + } + if ( [limitRowsSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"limitRows"]; + } else { + [prefs setBool:NO forKey:@"limitRows"]; + } + if ( [useMonospacedFontsSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"useMonospacedFonts"]; + } else { + [prefs setBool:NO forKey:@"useMonospacedFonts"]; + } + if ( [fetchRowCountSwitch state] == NSOnState ) { + [prefs setBool:YES forKey:@"fetchRowCount"]; + } else { + [prefs setBool:NO forKey:@"fetchRowCount"]; + } + [prefs setObject:[nullValueField stringValue] forKey:@"nullValue"]; + if ( [limitRowsField intValue] > 0 ) { + [prefs setInteger:[limitRowsField intValue] forKey:@"limitRowsValue"]; + } else { + [prefs setInteger:1 forKey:@"limitRowsValue"]; + } + [prefs setObject:[encodingPopUpButton titleOfSelectedItem] forKey:@"encoding"]; + + [prefs setObject:favorites forKey:@"favorites"]; + } + return YES; +} + + +//other methods +- (void)awakeFromNib +/* +code that need to be executed when the nib file is loaded +*/ +{ + NSEnumerator *enumerator; + id favorite; + NSString *name, *host, *user, *database, *password; +// int code; + +//register MainController as services provider + [NSApp setServicesProvider:self]; + +//register MainController for AppleScript events + [[ NSScriptExecutionContext sharedScriptExecutionContext] setTopLevelObject: self ]; + + [GrowlApplicationBridge setGrowlDelegate:self]; + + prefs = [[NSUserDefaults standardUserDefaults] retain]; + isNewFavorite = NO; + [prefs registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:YES], @"limitRows", + [NSNumber numberWithInt:1000], @"limitRowsValue", + nil]]; + +//set standard preferences if no preferences are found + if ( [prefs objectForKey:@"reloadAfterAdding"] == nil ) + { + [prefs setObject:@"0.3" forKey:@"version"]; + [prefs setBool:YES forKey:@"reloadAfterAdding"]; + [prefs setBool:YES forKey:@"reloadAfterEditing"]; + [prefs setBool:NO forKey:@"reloadAfterRemoving"]; + [prefs setObject:@"NULL" forKey:@"nullValue"]; +// [prefs setBool:YES forKey:@"showError"]; +// [prefs setBool:NO forKey:@"dontShowBlob"]; +// [prefs setBool:NO forKey:@"limitRows"]; +// [prefs setInteger:100 forKey:@"limitRowsValue"]; +// [prefs setObject:[NSString stringWithString:NSHomeDirectory()] forKey:@"savePath"]; +// [prefs setObject:[NSString stringWithString:NSHomeDirectory()] forKey:@"openPath"]; + } + +//new preferences and changes in v0.4 + if ( [prefs objectForKey:@"showError"] == nil ) + { + [prefs setObject:@"0.4" forKey:@"version"]; + //set standard values for new preferences + [prefs setBool:YES forKey:@"showError"]; + [prefs setBool:NO forKey:@"dontShowBlob"]; +// [prefs setBool:NO forKey:@"limitRows"]; +// [prefs setInteger:100 forKey:@"limitRowsValue"]; + [prefs setObject:[NSString stringWithString:NSHomeDirectory()] forKey:@"savePath"]; + [prefs setObject:[NSString stringWithString:NSHomeDirectory()] forKey:@"openPath"]; + //remove old preferences + [prefs removeObjectForKey:@"allowDragAndDropReordering"]; + //rewrite passwords to keychain (with new format) + if ( [prefs objectForKey:@"favorites"] ) { + NSRunAlertPanel(NSLocalizedString(@"Warning", @"warning"), NSLocalizedString(@"With version 0.4 Sequel Pro has introduced a new format to save passwords in the Keychain.\nPlease allow Sequel Pro to decrypt all passwords of your favorites. Otherwise you have to reenter all passwords of your saved favorites in the Preferences.", @"message of panel when passwords have to be updated for v0.4"), NSLocalizedString(@"OK", @"OK button"), nil, nil); + enumerator = [[prefs objectForKey:@"favorites"] objectEnumerator]; + while ( (favorite = [enumerator nextObject]) ) { + //replace password + name = [favorite objectForKey:@"name"]; + host = [favorite objectForKey:@"host"]; + user = [favorite objectForKey:@"user"]; + database = [favorite objectForKey:@"database"]; + password = [keyChainInstance getPasswordForName:[NSString stringWithFormat:@"%@/%@", host, database] + account:user]; + [keyChainInstance deletePasswordForName:[NSString stringWithFormat:@"%@/%@", host, database] account:user]; + if ( ![password isEqualToString:@""] ) + [keyChainInstance addPassword:password + forName:[NSString stringWithFormat:@"Sequel Pro : %@", name] + account:[NSString stringWithFormat:@"%@@%@/%@", user, host, database]]; + } + } + } +//new preferences and changes in v0.5 + if ( [[prefs objectForKey:@"version"] isEqualToString:@"0.4"] ) + { + [prefs setObject:@"0.5" forKey:@"version"]; + //set standard values for new preferences + [prefs setObject:@"ISO Latin 1" forKey:@"encoding"]; + [prefs setBool:NO forKey:@"useMonospacedFonts"]; + //add socket field to favorites + if ( [prefs objectForKey:@"favorites"] ) { + NSMutableArray *tempFavorites = [NSMutableArray array]; + NSMutableDictionary *tempFavorite; + enumerator = [[prefs objectForKey:@"favorites"] objectEnumerator]; + while ( (favorite = [enumerator nextObject]) ) { + tempFavorite = [NSMutableDictionary dictionaryWithDictionary:favorite]; + [tempFavorite setObject:@"" forKey:@"socket"]; + [tempFavorites addObject:[NSDictionary dictionaryWithDictionary:tempFavorite]]; + } + [prefs setObject:tempFavorites forKey:@"favorites"]; + } + } +//new preferences and changes in v0.7 + if ( [[prefs objectForKey:@"version"] isEqualToString:@"0.5"] || + [[prefs objectForKey:@"version"] isEqualToString:@"0.6beta"] || + [[prefs objectForKey:@"version"] isEqualToString:@"0.7b2"] ) + { + [prefs setObject:@"0.7b3" forKey:@"version"]; + [prefs setObject:@"Autodetect" forKey:@"encoding"]; + [prefs setBool:YES forKey:@"fetchRowCount"]; + } + +//set up interface +/* + enumerator = [tableColumns objectEnumerator]; + while ( (column = [enumerator nextObject]) ) + { + [[column dataCell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; + } +*/ + [tableView registerForDraggedTypes:[NSArray arrayWithObjects:@"SequelProPreferencesPasteboard", nil]]; + [tableView reloadData]; +} + + +// SSHTunnel methods +- (id)authenticate:(NSScriptCommand *)command { + NSDictionary *args = [command evaluatedArguments]; + NSString *givenQuery = [ args objectForKey:@"query"]; + NSString *tunnelName = [ args objectForKey:@"tunnelName"]; + NSString *fifo = [ args objectForKey:@"fifo"]; +NSLog(@"tunnel: %@ / query: %@ / fifo: %@",tunnelName,givenQuery,fifo); + NSFileHandle *fh = [ NSFileHandle fileHandleForWritingAtPath: fifo ]; + [ fh writeData: [ @"xy" dataUsingEncoding: NSASCIIStringEncoding]]; + [ fh closeFile ]; +NSLog(@"password written"); + return @"OK"; + +/* + [ query setStringValue: givenQuery ]; + [NSApp beginSheet: alertSheet + modalForWindow: mainWindow + modalDelegate: nil + didEndSelector: nil + contextInfo: nil]; + [NSApp runModalForWindow: alertSheet]; + // Sheet is up here. + [NSApp endSheet: alertSheet]; + [alertSheet orderOut: self]; + if ( sheetStatus == 0) + { + password = [ passwd stringValue ]; + [ passwd setStringValue: @"" ]; + return password ; + } + else + { + [[tunnelTask objectForKey: @"task" ] terminate ]; + } + sheetStatus = nil; + return @""; +*/ +} + +- (id)handleQuitScriptCommand:(NSScriptCommand *)command +/* what exactly is this for? */ +{ + [ NSApp terminate: self ]; +} + +@end |