From 8340fe85798a0ed932de04ed9f740781728ab7f1 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Fri, 10 Apr 2009 12:44:06 +0000 Subject: Implementation of enhancement described in issue #75. You can now add the current connection details to your favourites provided it doesn't already exist. --- Source/TableDocument.h | 1 + Source/TableDocument.m | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) (limited to 'Source') diff --git a/Source/TableDocument.h b/Source/TableDocument.h index b9426e31..de062124 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -179,6 +179,7 @@ - (IBAction)viewContent:(id)sender; - (IBAction)viewQuery:(id)sender; - (IBAction)viewStatus:(id)sender; +- (IBAction)addConnectionToFavorites:(id)sender; //toolbar methods - (void)setupToolbar; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index f6e419c5..e8599c47 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -44,6 +44,12 @@ NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocumentFavoritesControllerSelectionIndexDidChange"; NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFavoritesControllerFavoritesDidChange"; +@interface TableDocument (PrivateAPI) + +- (BOOL)_favoriteAlreadyExists:(NSString *)database host:(NSString *)host user:(NSString *)user; + +@end + @implementation TableDocument - (id)init @@ -1351,6 +1357,10 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum return ([self table] != nil && [[self table] isNotEqualTo:@""]); } + if ([menuItem action] == @selector(addConnectionToFavorites:)) { + return (![self _favoriteAlreadyExists:[self database] host:[self host] user:[self user]]); + } + return [super validateMenuItem:menuItem]; } @@ -1423,6 +1433,21 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum [mainToolbar setSelectedItemIdentifier:@"SwitchToTableStatusToolbarItemIdentifier"]; } +/** + * Adds the current database connection details to the user's favorites if it doesn't already exist. + */ +- (IBAction)addConnectionToFavorites:(id)sender +{ + // Obviously don't add if it already exists. We shouldn't really need this as the menu item validation + // enables or disables the menu item based on the same method. Although to be safe do the check anyway + // as we don't know what's calling this method. + if ([self _favoriteAlreadyExists:[self database] host:[self host] user:[self user]]) { + return; + } + + // Add current connection to favorites using the same method as used on the connection sheet to provide consistency. + [self connectSheetAddToFavorites:self]; +} #pragma mark Toolbar Methods @@ -1887,3 +1912,26 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum } @end + +@implementation TableDocument (PrivateAPI) + +/** + * Checks to see if a favorite with the supplied details already exists. + */ +- (BOOL)_favoriteAlreadyExists:(NSString *)database host:(NSString *)host user:(NSString *)user +{ + // Loop the favorites and check their details + for (NSDictionary *favorite in favorites) + { + if ([[favorite objectForKey:@"database"] isEqualToString:database] && + [[favorite objectForKey:@"host"] isEqualToString:host] && + [[favorite objectForKey:@"user"] isEqualToString:user]) { + + return YES; + } + } + + return NO; +} + +@end -- cgit v1.2.3