From b31bb2c4981d0a44119c43eac7525233a7c3b0e3 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 22 Jan 2012 22:40:05 +0000 Subject: Split out connection controller initialization. --- Source/SPConnectionController.h | 2 - Source/SPConnectionController.m | 136 +------------------- Source/SPConnectionControllerInitializer.h | 44 +++++++ Source/SPConnectionControllerInitializer.m | 196 +++++++++++++++++++++++++++++ Source/SPConnectionHandler.m | 4 +- Source/SPDatabaseDocument.m | 2 +- Source/SPFavoritesController.h | 2 +- 7 files changed, 250 insertions(+), 136 deletions(-) create mode 100644 Source/SPConnectionControllerInitializer.h create mode 100644 Source/SPConnectionControllerInitializer.m (limited to 'Source') diff --git a/Source/SPConnectionController.h b/Source/SPConnectionController.h index c7f191a5..5a80ebc4 100644 --- a/Source/SPConnectionController.h +++ b/Source/SPConnectionController.h @@ -212,8 +212,6 @@ @property (readonly, assign) BOOL isConnecting; -- (id)initWithDocument:(SPDatabaseDocument *)document; - // Connection processes - (IBAction)initiateConnection:(id)sender; #ifndef SP_REFACTOR /* method decls */ diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 2e6790f6..c5119bb7 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -48,7 +48,9 @@ static NSString *SPExportFavorites = @"ExportFavorites"; static NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist"; @interface NSSavePanel (NSSavePanel_unpublishedUntilSnowLeopardAPI) + - (void)setShowsHiddenFiles:(BOOL)flag; + @end @interface SPConnectionController () @@ -67,7 +69,7 @@ static NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist"; - (void)_updateFavoritePasswordsFromField:(NSControl *)control; -static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key); +static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, void *key); @end @@ -103,134 +105,6 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v @synthesize isConnecting; -#pragma mark - - -/** - * Initialise the connection controller, linking it to the - * parent document and setting up the parent window. - */ -- (id)initWithDocument:(SPDatabaseDocument *)document -{ - if ((self = [super init])) { - - // Weak reference - dbDocument = document; - - databaseConnectionSuperview = [dbDocument databaseView]; - databaseConnectionView = [dbDocument valueForKey:@"contentViewSplitter"]; - - // Keychain references - connectionKeychainItemName = nil; - connectionKeychainItemAccount = nil; - connectionSSHKeychainItemName = nil; - connectionSSHKeychainItemAccount = nil; - - isEditing = NO; - isConnecting = NO; - - sshTunnel = nil; - mySQLConnection = nil; - cancellingConnection = NO; - mySQLConnectionCancelled = NO; - - favoriteNameFieldWasTouched = YES; - - // Load the connection nib, keeping references to the top-level objects for later release - nibObjectsToRelease = [[NSMutableArray alloc] init]; - NSArray *connectionViewTopLevelObjects = nil; - NSNib *nibLoader = [[NSNib alloc] initWithNibNamed:@"ConnectionView" bundle:[NSBundle mainBundle]]; - - [nibLoader instantiateNibWithOwner:self topLevelObjects:&connectionViewTopLevelObjects]; - [nibObjectsToRelease addObjectsFromArray:connectionViewTopLevelObjects]; - [nibLoader release]; - - // Hide the main view and position and display the connection view - [databaseConnectionView setHidden:YES]; - [connectionView setFrame:[databaseConnectionView frame]]; - [databaseConnectionSuperview addSubview:connectionView]; - [connectionSplitView setPosition:[[dbDocument valueForKey:@"dbTablesTableView"] frame].size.width ofDividerAtIndex:0]; - [connectionSplitView setDelegate:self]; - - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollViewFrameChanged:) name:NSViewFrameDidChangeNotification object:nil]; - - // Generic folder image for use in the outline view's groups - folderImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain]; - - [folderImage setSize:NSMakeSize(16, 16)]; - - // Set up a keychain instance and preferences reference, and create the initial favorites list - keychain = [[SPKeychain alloc] init]; - prefs = [[NSUserDefaults standardUserDefaults] retain]; - - // Create a reference to the favorites controller, forcing the data to be loaded from disk and the - // tree constructor. - favoritesController = [SPFavoritesController sharedFavoritesController]; - - // Tree reference - favoritesRoot = [favoritesController favoritesTree]; - - // Update the UI - [self _reloadFavoritesViewData]; - - // Set sort items - currentSortItem = [prefs integerForKey:SPFavoritesSortedBy]; - reverseFavoritesSort = [prefs boolForKey:SPFavoritesSortedInReverse]; - - // Register double click action for the favorites outline view (double click favorite to connect) - [favoritesOutlineView setTarget:self]; - [favoritesOutlineView setDoubleAction:@selector(nodeDoubleClicked:)]; - - // Register drag types for the favorites outline view - [favoritesOutlineView registerForDraggedTypes:[NSArray arrayWithObject:SPFavoritesPasteboardDragType]]; - [favoritesOutlineView setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES]; - - // Preserve expanded group nodes - [favoritesOutlineView setAutosaveExpandedItems:YES]; - - // Registered to be notified of changes to connection information - [self addObserver:self forKeyPath:SPFavoriteNameKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteHostKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteUserKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteDatabaseKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSocketKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoritePortKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteUseSSLKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSHHostKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSHUserKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSHPortKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSHKeyLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSHKeyLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLKeyFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLKeyFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLCertificateFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLCertificateFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLCACertFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - [self addObserver:self forKeyPath:SPFavoriteSSLCACertFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; - - SPTreeNode *favorite = [self _favoriteNodeForFavoriteID:[prefs integerForKey:([prefs boolForKey:SPSelectLastFavoriteUsed]) ? SPLastFavoriteID : SPDefaultFavorite]]; - - if (favorite) { - - NSNumber *typeNumber = [[[favorite representedObject] nodeFavorite] objectForKey:SPFavoriteTypeKey]; - - previousType = (typeNumber) ? [typeNumber integerValue] : SPTCPIPConnection; - - [self _selectNode:favorite]; - - [self resizeTabViewToConnectionType:[[[[favorite representedObject] nodeFavorite] objectForKey:SPFavoriteTypeKey] integerValue] animating:NO]; - - [favoritesOutlineView scrollRowToVisible:[favoritesOutlineView selectedRow]]; - } - else { - previousType = SPTCPIPConnection; - - [self resizeTabViewToConnectionType:SPTCPIPConnection animating:NO]; - } - } - - return self; -} - #pragma mark - #pragma mark Connection processes @@ -1284,7 +1158,7 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v [indexes release]; - [nodes sortUsingFunction:compareFavoritesUsingKey context:key]; + [nodes sortUsingFunction:_compareFavoritesUsingKey context:key]; [nodes addObjectsFromArray:groupNodes]; @@ -1304,7 +1178,7 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v * * @return An integer (NSComparisonResult) indicating the order of the comparison */ -static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key) +static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, void *key) { NSString *dictKey = (NSString *)key; diff --git a/Source/SPConnectionControllerInitializer.h b/Source/SPConnectionControllerInitializer.h new file mode 100644 index 00000000..7a0a0e1e --- /dev/null +++ b/Source/SPConnectionControllerInitializer.h @@ -0,0 +1,44 @@ +// +// $Id$ +// +// SPConnectionControllerInitializer.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on January 22, 2012 +// Copyright (c) 2012 Stuart Connolly. 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 + +#import "SPConnectionController.h" + +/** + * @category SPConnectionControllerInitializer SPConnectionControllerInitializer.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * Connection controller initialization category. + */ +@interface SPConnectionController (SPConnectionControllerInitializer) + +- (id)initWithDocument:(SPDatabaseDocument *)document; + +- (void)loadNib; +- (void)registerForNotifications; +- (void)setUpFavoritesOutlineView; +- (void)setUpSelectedConnectionFavorite; + +@end diff --git a/Source/SPConnectionControllerInitializer.m b/Source/SPConnectionControllerInitializer.m new file mode 100644 index 00000000..5c11f301 --- /dev/null +++ b/Source/SPConnectionControllerInitializer.m @@ -0,0 +1,196 @@ +// +// $Id$ +// +// SPConnectionControllerInitializer.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on January 22, 2012 +// Copyright (c) 2012 Stuart Connolly. 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 + +#import "SPConnectionControllerInitializer.h" +#import "SPKeychain.h" +#import "SPFavoritesController.h" +#import "SPFavoriteNode.h" +#import "SPDatabaseViewController.h" + +static NSString *SPConnectionViewNibName = @"ConnectionView"; + +@interface SPConnectionController () + +- (void)_reloadFavoritesViewData; +- (void)_selectNode:(SPTreeNode *)node; +- (SPTreeNode *)_favoriteNodeForFavoriteID:(NSInteger)favoriteID; + +@end + +@implementation SPConnectionController (SPConnectionControllerInitializer) + +/** + * Initialise the connection controller, linking it to the parent document and setting up the parent window. + */ +- (id)initWithDocument:(SPDatabaseDocument *)document +{ + if ((self = [super init])) { + + // Weak reference + dbDocument = document; + + databaseConnectionSuperview = [dbDocument databaseView]; + databaseConnectionView = [dbDocument valueForKey:@"contentViewSplitter"]; + + // Keychain references + connectionKeychainItemName = nil; + connectionKeychainItemAccount = nil; + connectionSSHKeychainItemName = nil; + connectionSSHKeychainItemAccount = nil; + + isEditing = NO; + isConnecting = NO; + sshTunnel = nil; + mySQLConnection = nil; + cancellingConnection = NO; + mySQLConnectionCancelled = NO; + favoriteNameFieldWasTouched = YES; + + [self loadNib]; + [self registerForNotifications]; + + // Hide the main view and position and display the connection view + [databaseConnectionView setHidden:YES]; + [connectionView setFrame:[databaseConnectionView frame]]; + [databaseConnectionSuperview addSubview:connectionView]; + [connectionSplitView setPosition:[[dbDocument valueForKey:@"dbTablesTableView"] frame].size.width ofDividerAtIndex:0]; + [connectionSplitView setDelegate:self]; + + // Generic folder image for use in the outline view's groups + folderImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain]; + + [folderImage setSize:NSMakeSize(16, 16)]; + + // Set up a keychain instance and preferences reference, and create the initial favorites list + keychain = [[SPKeychain alloc] init]; + prefs = [[NSUserDefaults standardUserDefaults] retain]; + + // Create a reference to the favorites controller, forcing the data to be loaded from disk + // and the tree to be constructed. + favoritesController = [SPFavoritesController sharedFavoritesController]; + + // Tree reference + favoritesRoot = [favoritesController favoritesTree]; + + // Update the UI + [self _reloadFavoritesViewData]; + [self setUpFavoritesOutlineView]; + [self setUpSelectedConnectionFavorite]; + + // Set sort items + currentSortItem = [prefs integerForKey:SPFavoritesSortedBy]; + reverseFavoritesSort = [prefs boolForKey:SPFavoritesSortedInReverse]; + } + + return self; +} + +/** + * Loads the connection controllers UI nib. + */ +- (void)loadNib +{ + // Load the connection nib, keeping references to the top-level objects for later release + nibObjectsToRelease = [[NSMutableArray alloc] init]; + + NSArray *connectionViewTopLevelObjects = nil; + NSNib *nibLoader = [[NSNib alloc] initWithNibNamed:SPConnectionViewNibName bundle:[NSBundle mainBundle]]; + + [nibLoader instantiateNibWithOwner:self topLevelObjects:&connectionViewTopLevelObjects]; + [nibObjectsToRelease addObjectsFromArray:connectionViewTopLevelObjects]; + [nibLoader release]; +} + +/** + * Registers for various notifications. + */ +- (void)registerForNotifications +{ + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollViewFrameChanged:) name:NSViewFrameDidChangeNotification object:nil]; + + // Registered to be notified of changes to connection information + [self addObserver:self forKeyPath:SPFavoriteNameKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteHostKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteUserKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteDatabaseKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSocketKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoritePortKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteUseSSLKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSHHostKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSHUserKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSHPortKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSHKeyLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSHKeyLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLKeyFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLKeyFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLCertificateFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLCertificateFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLCACertFileLocationEnabledKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; + [self addObserver:self forKeyPath:SPFavoriteSSLCACertFileLocationKey options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; +} + +/** + * Performs any set up necessary for the favorities outline view. + */ +- (void)setUpFavoritesOutlineView +{ + // Register double click action for the favorites outline view (double click favorite to connect) + [favoritesOutlineView setTarget:self]; + [favoritesOutlineView setDoubleAction:@selector(nodeDoubleClicked:)]; + + // Register drag types for the favorites outline view + [favoritesOutlineView registerForDraggedTypes:[NSArray arrayWithObject:SPFavoritesPasteboardDragType]]; + [favoritesOutlineView setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES]; + + // Preserve expanded group nodes + [favoritesOutlineView setAutosaveExpandedItems:YES]; +} + +/** + * Sets up the selected connection favorite according to the user's preferences. + */ +- (void)setUpSelectedConnectionFavorite +{ + SPTreeNode *favorite = [self _favoriteNodeForFavoriteID:[prefs integerForKey:[prefs boolForKey:SPSelectLastFavoriteUsed] ? SPLastFavoriteID : SPDefaultFavorite]]; + + if (favorite) { + + NSNumber *typeNumber = [[[favorite representedObject] nodeFavorite] objectForKey:SPFavoriteTypeKey]; + + previousType = typeNumber ? [typeNumber integerValue] : SPTCPIPConnection; + + [self _selectNode:favorite]; + [self resizeTabViewToConnectionType:[[[[favorite representedObject] nodeFavorite] objectForKey:SPFavoriteTypeKey] integerValue] animating:NO]; + + [favoritesOutlineView scrollRowToVisible:[favoritesOutlineView selectedRow]]; + } + else { + previousType = SPTCPIPConnection; + + [self resizeTabViewToConnectionType:SPTCPIPConnection animating:NO]; + } +} + +@end diff --git a/Source/SPConnectionHandler.m b/Source/SPConnectionHandler.m index bca96ce1..ccdad4d1 100644 --- a/Source/SPConnectionHandler.m +++ b/Source/SPConnectionHandler.m @@ -30,7 +30,7 @@ #import "SPKeychain.h" #import "RegexKitLite.h" -@interface SPConnectionController (PrivateAPI) +@interface SPConnectionController () - (void)_restoreConnectionInterface; @@ -405,6 +405,7 @@ if (connectionKeychainItemName) { [self setPassword:[keychain getPasswordForName:connectionKeychainItemName account:connectionKeychainItemAccount]]; } + if (connectionSSHKeychainItemName) { [self setSshPassword:[keychain getPasswordForName:connectionSSHKeychainItemName account:connectionSSHKeychainItemAccount]]; } @@ -417,6 +418,7 @@ // Currently only SSH port bind errors offer a 3rd option in the error dialog, but if this ever changes // this will definitely need to be updated. else if (returnCode == NSAlertOtherReturn) { + // Extract the local port number that SSH attempted to bind to from the debug output NSString *tunnelPort = [[[errorDetailText string] componentsMatchedByRegex:@"LOCALHOST:([0-9]+)" capture:1L] lastObject]; diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index bf9f96c7..a70089eb 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -34,6 +34,7 @@ enum { #import "SPDatabaseDocument.h" #import "SPConnectionController.h" +#import "SPConnectionControllerInitializer.h" #import "SPTablesList.h" #import "SPTableStructure.h" @@ -246,7 +247,6 @@ static NSString *SPCreateSyntx = @"SPCreateSyntax"; #endif - - (void)awakeFromNib { #ifndef SP_REFACTOR diff --git a/Source/SPFavoritesController.h b/Source/SPFavoritesController.h index 3bde2376..e15ab385 100644 --- a/Source/SPFavoritesController.h +++ b/Source/SPFavoritesController.h @@ -45,7 +45,7 @@ } /** - * @property favoritesTree + * @property favoritesTree The current favorites tree */ @property (readonly) SPTreeNode *favoritesTree; -- cgit v1.2.3