From 83fa503079f7a049ffab9747b0360046474f3253 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Thu, 3 May 2012 12:05:01 +0000 Subject: Add support for saving the state of the connection favorites outline view. --- Source/SPAppController.m | 49 +++++++++++++++++++----------- Source/SPConnectionController.m | 2 +- Source/SPConnectionControllerDataSource.m | 12 +------- Source/SPConnectionControllerDelegate.m | 22 ++++++++++++++ Source/SPConnectionControllerInitializer.m | 36 +++++++++++++++++++--- Source/SPConstants.h | 1 + Source/SPConstants.m | 1 + Source/SPFavoritesController.m | 10 ++++-- Source/SPGroupNode.h | 7 +++++ Source/SPGroupNode.m | 8 ++++- Source/SPTreeNode.m | 1 + 11 files changed, 111 insertions(+), 38 deletions(-) (limited to 'Source') diff --git a/Source/SPAppController.m b/Source/SPAppController.m index da25b07f..4d3cc92b 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -38,12 +38,12 @@ #import "SPAlertSheets.h" #import "SPChooseMenuItemDialog.h" #import "SPCustomQuery.h" +#import "SPFavoritesController.h" +#import "SPEditorTokens.h" #import #import -#import "SPEditorTokens.h" - #pragma mark lex init /* @@ -2227,21 +2227,27 @@ YY_BUFFER_STATE yy_scan_string (const char *); } /** - * If Sequel Pro is terminating kill all running BASH scripts and release all HTML output controller + * If Sequel Pro is terminating kill all running BASH scripts and release all HTML output controller. + * + * TODO: Remove a lot of this duplicate code. */ - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - - if(lastBundleBlobFilesDirectory != nil) + if (lastBundleBlobFilesDirectory != nil) { [[NSFileManager defaultManager] removeItemAtPath:lastBundleBlobFilesDirectory error:nil]; + } // Kill all registered BASH commands - for (NSWindow *aWindow in [NSApp orderedWindows]) { - if([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) { - for(SPDatabaseDocument *doc in [[aWindow windowController] documents]) { - for(NSDictionary* cmd in [doc runningActivities]) { + for (NSWindow *aWindow in [NSApp orderedWindows]) + { + if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) { + for (SPDatabaseDocument *doc in [[aWindow windowController] documents]) + { + for (NSDictionary* cmd in [doc runningActivities]) + { NSInteger pid = [[cmd objectForKey:@"pid"] intValue]; NSTask *killTask = [[NSTask alloc] init]; + [killTask setLaunchPath:@"/bin/sh"]; [killTask setArguments:[NSArray arrayWithObjects:@"-c", [NSString stringWithFormat:@"kill -9 -%ld", pid], nil]]; [killTask launch]; @@ -2251,9 +2257,12 @@ YY_BUFFER_STATE yy_scan_string (const char *); } } } - for(NSDictionary* cmd in [self runningActivities]) { + + for (NSDictionary* cmd in [self runningActivities]) + { NSInteger pid = [[cmd objectForKey:@"pid"] intValue]; NSTask *killTask = [[NSTask alloc] init]; + [killTask setLaunchPath:@"/bin/sh"]; [killTask setArguments:[NSArray arrayWithObjects:@"-c", [NSString stringWithFormat:@"kill -9 -%ld", pid], nil]]; [killTask launch]; @@ -2261,9 +2270,13 @@ YY_BUFFER_STATE yy_scan_string (const char *); [killTask release]; } - for(id c in bundleHTMLOutputController) { + for (id c in bundleHTMLOutputController) + { [c release]; } + + // Make sure we save any changes made to the connection outline view's state + [[SPFavoritesController sharedFavoritesController] saveFavorites]; return YES; @@ -2278,13 +2291,13 @@ YY_BUFFER_STATE yy_scan_string (const char *); { [[NSNotificationCenter defaultCenter] removeObserver:self]; - if(bundleItems) [bundleItems release]; - if(bundleUsedScopes) [bundleUsedScopes release]; - if(bundleHTMLOutputController) [bundleHTMLOutputController release]; - if(bundleCategories) [bundleCategories release]; - if(bundleTriggers) [bundleTriggers release]; - if(bundleKeyEquivalents) [bundleKeyEquivalents release]; - if(installedBundleUUIDs) [installedBundleUUIDs release]; + if (bundleItems) [bundleItems release]; + if (bundleUsedScopes) [bundleUsedScopes release]; + if (bundleHTMLOutputController) [bundleHTMLOutputController release]; + if (bundleCategories) [bundleCategories release]; + if (bundleTriggers) [bundleTriggers release]; + if (bundleKeyEquivalents) [bundleKeyEquivalents release]; + if (installedBundleUUIDs) [installedBundleUUIDs release]; if (runningActivitiesArray) [runningActivitiesArray release]; [prefsController release], prefsController = nil; diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 350616ce..7e3e793b 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -1223,7 +1223,7 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, - (void)_reloadFavoritesViewData { [favoritesOutlineView reloadData]; - [favoritesOutlineView expandItem:[[favoritesRoot childNodes] objectAtIndex:0] expandChildren:YES]; + [favoritesOutlineView expandItem:[[favoritesRoot childNodes] objectAtIndex:0] expandChildren:NO]; [favoritesOutlineView scrollRowToVisible:[favoritesOutlineView selectedRow]]; } diff --git a/Source/SPConnectionControllerDataSource.m b/Source/SPConnectionControllerDataSource.m index cb9c71bf..91fd6398 100644 --- a/Source/SPConnectionControllerDataSource.m +++ b/Source/SPConnectionControllerDataSource.m @@ -60,7 +60,7 @@ - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { SPTreeNode *node = (SPTreeNode *)item; - + return (![node isGroup]) ? [[[node representedObject] nodeFavorite] objectForKey:SPFavoriteNameKey] : [[node representedObject] nodeName]; } @@ -90,14 +90,4 @@ } } -- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object -{ - return [NSKeyedUnarchiver unarchiveObjectWithData:object]; -} - -- (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(id)item -{ - return [NSKeyedArchiver archivedDataWithRootObject:item]; -} - @end diff --git a/Source/SPConnectionControllerDelegate.m b/Source/SPConnectionControllerDelegate.m index 389bd46c..bc6800c3 100644 --- a/Source/SPConnectionControllerDelegate.m +++ b/Source/SPConnectionControllerDelegate.m @@ -47,6 +47,8 @@ static NSString *SPDatabaseImage = @"database-small"; - (NSString *)_stripInvalidCharactersFromString:(NSString *)subject; +- (void)_setNodeIsExpanded:(BOOL)expanded fromNotification:(NSNotification *)notification; + @end @implementation SPConnectionController (SPConnectionControllerDelegate) @@ -154,6 +156,16 @@ static NSString *SPDatabaseImage = @"database-small"; return ([[item parentNode] parentNode] != nil); } +- (void)outlineViewItemDidCollapse:(NSNotification *)notification +{ + [self _setNodeIsExpanded:NO fromNotification:notification]; +} + +- (void)outlineViewItemDidExpand:(NSNotification *)notification +{ + [self _setNodeIsExpanded:YES fromNotification:notification]; +} + #pragma mark - #pragma mark Outline view drag & drop @@ -557,4 +569,14 @@ static NSString *SPDatabaseImage = @"database-small"; } } +#pragma mark - +#pragma mark Private API + +- (void)_setNodeIsExpanded:(BOOL)expanded fromNotification:(NSNotification *)notification +{ + SPGroupNode *node = [[[notification userInfo] valueForKey:@"NSObject"] representedObject]; + + [node setNodeIsExpanded:expanded]; +} + @end diff --git a/Source/SPConnectionControllerInitializer.m b/Source/SPConnectionControllerInitializer.m index a9aa6ca8..7bafe157 100644 --- a/Source/SPConnectionControllerInitializer.m +++ b/Source/SPConnectionControllerInitializer.m @@ -26,7 +26,9 @@ #import "SPConnectionControllerInitializer.h" #import "SPKeychain.h" #import "SPFavoritesController.h" +#import "SPTreeNode.h" #import "SPFavoriteNode.h" +#import "SPGroupNode.h" #import "SPDatabaseViewController.h" static NSString *SPConnectionViewNibName = @"ConnectionView"; @@ -35,6 +37,7 @@ static NSString *SPConnectionViewNibName = @"ConnectionView"; - (void)_reloadFavoritesViewData; - (void)_selectNode:(SPTreeNode *)node; +- (void)_restoreOutlineViewStateNode:(SPTreeNode *)node; - (SPTreeNode *)_favoriteNodeForFavoriteID:(NSInteger)favoriteID; @@ -102,6 +105,7 @@ static NSString *SPConnectionViewNibName = @"ConnectionView"; [self _reloadFavoritesViewData]; [self setUpFavoritesOutlineView]; [self setUpSelectedConnectionFavorite]; + [self _restoreOutlineViewStateNode:favoritesRoot]; // Set sort items currentSortItem = [prefs integerForKey:SPFavoritesSortedBy]; @@ -240,10 +244,7 @@ static NSString *SPConnectionViewNibName = @"ConnectionView"; // 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]; + [favoritesOutlineView setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES]; } /** @@ -271,4 +272,31 @@ static NSString *SPConnectionViewNibName = @"ConnectionView"; } } +#pragma mark - +#pragma mark Private API + +/** + * Restores the outline views group nodes expansion state. + * + * @param node The node to traverse + */ +- (void)_restoreOutlineViewStateNode:(SPTreeNode *)node +{ + if ([node isGroup]) { + if ([[node representedObject] nodeIsExpanded]) { + [favoritesOutlineView expandItem:node]; + } + else { + [favoritesOutlineView collapseItem:node]; + } + + for (SPTreeNode *childNode in [node childNodes]) + { + if ([childNode isGroup]) { + [self _restoreOutlineViewStateNode:childNode]; + } + } + } +} + @end diff --git a/Source/SPConstants.h b/Source/SPConstants.h index c2775716..e696e71f 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -434,6 +434,7 @@ extern NSString *SPPreferenceToolbarShortcuts; extern NSString *SPFavoritesRootKey; extern NSString *SPFavoriteChildrenKey; extern NSString *SPFavoritesGroupNameKey; +extern NSString *SPFavoritesGroupIsExpandedKey; extern NSString *SPFavoriteIDKey; extern NSString *SPFavoriteNameKey; extern NSString *SPFavoriteDatabaseKey; diff --git a/Source/SPConstants.m b/Source/SPConstants.m index 79de29a8..024f1df0 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -255,6 +255,7 @@ NSString *SPPreferenceToolbarShortcuts = @"SPPreferenceToolbarShortcut NSString *SPFavoritesRootKey = @"Favorites Root"; NSString *SPFavoriteChildrenKey = @"Children"; NSString *SPFavoritesGroupNameKey = @"Name"; +NSString *SPFavoritesGroupIsExpandedKey = @"IsExpanded"; NSString *SPFavoriteIDKey = @"id"; NSString *SPFavoriteNameKey = @"name"; NSString *SPFavoriteDatabaseKey = @"database"; diff --git a/Source/SPFavoritesController.m b/Source/SPFavoritesController.m index 45eb5c9a..1dee4a2b 100644 --- a/Source/SPFavoritesController.m +++ b/Source/SPFavoritesController.m @@ -262,6 +262,8 @@ static SPFavoritesController *sharedFavoritesController = nil; SPGroupNode *rootGroupNode = [[SPGroupNode alloc] init]; SPGroupNode *favoritesGroupNode = [[SPGroupNode alloc] initWithName:[[root objectForKey:SPFavoritesGroupNameKey] uppercaseString]]; + [favoritesGroupNode setNodeIsExpanded:[[root objectForKey:SPFavoritesGroupIsExpandedKey] boolValue]]; + SPTreeNode *rootNode = [[SPTreeNode alloc] initWithRepresentedObject:rootGroupNode]; SPTreeNode *favoritesNode = [[SPTreeNode alloc] initWithRepresentedObject:favoritesGroupNode]; @@ -282,7 +284,7 @@ static SPFavoritesController *sharedFavoritesController = nil; [favoritesNode release]; favoritesTree = rootNode; - + pthread_mutex_unlock(&favoritesLock); } @@ -298,11 +300,13 @@ static SPFavoritesController *sharedFavoritesController = nil; { id node = nil; SPTreeNode *treeNode = nil; - + if ([nodeData objectForKey:SPFavoritesGroupNameKey] && [nodeData objectForKey:SPFavoriteChildrenKey]) { node = [[SPGroupNode alloc] initWithName:[nodeData objectForKey:SPFavoritesGroupNameKey]]; + [node setNodeIsExpanded:[[nodeData objectForKey:SPFavoritesGroupIsExpandedKey] boolValue]]; + treeNode = [[SPTreeNode alloc] initWithRepresentedObject:node]; [node release]; @@ -323,7 +327,7 @@ static SPFavoritesController *sharedFavoritesController = nil; [node release]; } - + return [treeNode autorelease]; } diff --git a/Source/SPGroupNode.h b/Source/SPGroupNode.h index 8c4f2e84..0cef04e9 100644 --- a/Source/SPGroupNode.h +++ b/Source/SPGroupNode.h @@ -32,6 +32,8 @@ */ @interface SPGroupNode : NSObject { + BOOL nodeIsExpanded; + NSString *nodeName; } @@ -40,6 +42,11 @@ */ @property (readwrite, retain) NSString *nodeName; +/** + * @property nodeIsExpanded Indicates whether the group node is expanded + */ +@property (readwrite, assign) BOOL nodeIsExpanded; + - (id)initWithName:(NSString *)name; + (SPGroupNode *)groupNodeWithName:(NSString *)name; diff --git a/Source/SPGroupNode.m b/Source/SPGroupNode.m index 7bf1124b..d52dd54b 100644 --- a/Source/SPGroupNode.m +++ b/Source/SPGroupNode.m @@ -27,10 +27,12 @@ // Constants static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; +static NSString *SPGroupNodeIsExpandedKey = @"SPGroupNodeIsExpanded"; @implementation SPGroupNode @synthesize nodeName; +@synthesize nodeIsExpanded; #pragma mark - #pragma mark Initialisation @@ -39,6 +41,7 @@ static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; { if ((self = [super init])) { [self setNodeName:nil]; + [self setNodeIsExpanded:YES]; } return self; @@ -66,6 +69,7 @@ static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; SPGroupNode *node = [[[self class] allocWithZone:zone] init]; [node setNodeName:[self nodeName]]; + [node setNodeIsExpanded:[self nodeIsExpanded]]; return node; } @@ -76,6 +80,7 @@ static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; - (id)initWithCoder:(NSCoder *)coder { [self setNodeName:[coder decodeObjectForKey:SPGroupNodeNameKey]]; + [self setNodeIsExpanded:[[coder decodeObjectForKey:SPGroupNodeIsExpandedKey] boolValue]]; return self; } @@ -83,6 +88,7 @@ static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:[self nodeName] forKey:SPGroupNodeNameKey]; + [coder encodeObject:[NSNumber numberWithBool:[self nodeIsExpanded]] forKey:SPGroupNodeIsExpandedKey]; } #pragma mark - @@ -90,7 +96,7 @@ static NSString *SPGroupNodeNameKey = @"SPGroupNodeName"; - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p ('%@')>", [self className], self, [self nodeName]]; + return [NSString stringWithFormat:@"<%@: %p ('%@', %@)>", [self className], self, [self nodeName], [NSNumber numberWithBool:[self nodeIsExpanded]]]; } #pragma mark - diff --git a/Source/SPTreeNode.m b/Source/SPTreeNode.m index f787816a..82c57832 100644 --- a/Source/SPTreeNode.m +++ b/Source/SPTreeNode.m @@ -248,6 +248,7 @@ static NSString *SPTreeNodeIsGroupKey = @"SPTreeNodeIsGroup"; NSString *name = (![self parentNode]) ? NSLocalizedString(@"Favorites", @"favorites label") : [object nodeName]; [dictionary setObject:name ? name : @"" forKey:SPFavoritesGroupNameKey]; + [dictionary setObject:[NSNumber numberWithBool:[object nodeIsExpanded]] forKey:SPFavoritesGroupIsExpandedKey]; [dictionary setObject:children forKey:SPFavoriteChildrenKey]; } -- cgit v1.2.3