From fb4490a4b59be5ea57fb156dad74c1ba97ca2d23 Mon Sep 17 00:00:00 2001 From: abhibeckert Date: Sat, 6 Sep 2008 06:31:40 +0000 Subject: implemneted favourite remove button in connect panel switched from NSArray to NSMutableArray for favourite storage now using KVO to be notified when to save favourites to NSUserDefaults no-longer block the user from having two favourites with identical data --- English.lproj/DBView.xib | 118 +++++++++++++++-------------------------------- TableDocument.h | 5 +- TableDocument.m | 40 +++++++--------- 3 files changed, 55 insertions(+), 108 deletions(-) diff --git a/English.lproj/DBView.xib b/English.lproj/DBView.xib index ff3917d5..81b2bcc4 100644 --- a/English.lproj/DBView.xib +++ b/English.lproj/DBView.xib @@ -34,8 +34,9 @@ NSWindow View {3.40282e+38, 3.40282e+38} + {780, 480} - + 256 YES @@ -66,7 +67,6 @@ {{176, 6}, {8, 10}} - YES 130560 @@ -87,7 +87,6 @@ 260 {{-1, -2}, {28, 26}} - YES 67239424 @@ -118,7 +117,6 @@ 260 {{26, -2}, {28, 26}} - YES 67239424 @@ -143,7 +141,6 @@ 260 {{80, -2}, {28, 26}} - YES 67239424 @@ -170,7 +167,6 @@ 260 {{53, -2}, {28, 26}} - YES 67239424 @@ -197,7 +193,6 @@ 290 {{107, -2}, {83, 26}} - YES -2080244224 @@ -237,7 +232,6 @@ 4352 {189, 367} - YES @@ -330,7 +324,6 @@ {{1, 1}, {189, 367}} - @@ -346,7 +339,6 @@ -2147483392 {{175, 1}, {15, 481}} - _doScroller: 9.979253e-01 @@ -356,7 +348,6 @@ 256 {{-100, -100}, {141, 11}} - 257 _doScroller: @@ -365,7 +356,6 @@ {191, 369} - 530 @@ -388,7 +378,6 @@ 4352 {189, 114} - YES @@ -452,7 +441,6 @@ {{1, 1}, {189, 114}} - @@ -463,7 +451,6 @@ -2147483392 {{175, 1}, {15, 481}} - _doScroller: 9.979253e-01 @@ -473,7 +460,6 @@ 256 {{-100, -100}, {141, 11}} - 257 _doScroller: @@ -482,7 +468,6 @@ {{0, 370}, {191, 116}} - 530 @@ -493,13 +478,11 @@ {{0, 20}, {191, 486}} - 2 {189, 504} - NSView @@ -512,7 +495,6 @@ 274 {{-7, -10}, {659, 518}} - YES @@ -549,7 +531,6 @@ {{595, 6}, {10, 13}} - YES 130560 @@ -570,7 +551,6 @@ 257 {{387, 8}, {55, 11}} - YES 67239424 @@ -591,7 +571,6 @@ 257 {{443, 6}, {135, 15}} - YES -1539178944 @@ -706,14 +685,12 @@ 4352 {612, 253} - YES 256 {612, 17} - @@ -721,7 +698,6 @@ -2147483392 {{-26, 0}, {16, 17}} - YES @@ -1254,7 +1230,6 @@ {{1, 17}, {612, 253}} - @@ -1265,7 +1240,6 @@ -2147483392 {{85, 17}, {15, 68}} - _doScroller: 8.170732e-01 @@ -1275,7 +1249,6 @@ -2147483392 {{1, 255}, {612, 15}} - 1 _doScroller: @@ -1290,7 +1263,6 @@ {{1, 0}, {612, 17}} - @@ -1300,7 +1272,6 @@ {{-1, 24}, {614, 271}} - 562 @@ -1315,7 +1286,6 @@ 290 {{107, 0}, {506, 26}} - YES -2080244224 @@ -1336,7 +1306,6 @@ 260 {{-1, 0}, {28, 26}} - YES 604110336 @@ -1361,7 +1330,6 @@ 260 {{26, 0}, {28, 26}} - YES 604110336 @@ -1386,7 +1354,6 @@ 260 {{53, 0}, {28, 26}} - YES 604110336 @@ -1411,7 +1378,6 @@ 260 {{80, 0}, {28, 26}} - YES 67239424 @@ -1434,7 +1400,6 @@ {613, 295} - NSView @@ -1447,7 +1412,6 @@ 264 {{7, 167}, {46, 14}} - YES 67239424 @@ -1474,14 +1438,12 @@ 4352 {612, 121} - YES 256 {612, 17} - @@ -1489,7 +1451,6 @@ -2147483392 {{-26, 0}, {16, 17}} - YES @@ -1742,7 +1703,6 @@ {{1, 17}, {612, 121}} - @@ -1753,7 +1713,6 @@ -2147483392 {{84, 17}, {15, 67}} - _doScroller: 8.170732e-01 @@ -1763,7 +1722,6 @@ -2147483392 {{1, 123}, {612, 15}} - 1 _doScroller: @@ -1778,7 +1736,6 @@ {{1, 0}, {612, 17}} - @@ -1788,7 +1745,6 @@ {{-1, 22}, {614, 139}} - 562 @@ -1803,7 +1759,6 @@ 258 {{80, -2}, {533, 26}} - YES -2080244224 @@ -1824,7 +1779,6 @@ 260 {{-1, -2}, {28, 26}} - YES 604110336 @@ -1849,7 +1803,6 @@ 260 {{26, -2}, {28, 26}} - YES 604110336 @@ -1874,7 +1827,6 @@ 260 {{53, -2}, {28, 26}} - YES 67239424 @@ -1894,18 +1846,15 @@ {{0, 304}, {613, 181}} - NSView {{7, 10}, {613, 485}} - {{10, 7}, {624, 498}} - Structure @@ -3357,13 +3306,11 @@ {{198, 0}, {647, 504}} - NSView {845, 504} - YES @@ -3372,17 +3319,14 @@ {{800, 464}, {32, 32}} - 28682 1.000000e+02 {845, 504} - - {{0, 0}, {1920, 1178}} - {0, 22} + {780, 502} {3.40282e+38, 3.40282e+38} DBView @@ -3400,6 +3344,7 @@ View {3.40282e+38, 3.40282e+38} + {256, 191} 256 @@ -4017,7 +3962,7 @@ {{0, 0}, {1920, 1178}} - {0, 22} + {256, 213} {3.40282e+38, 3.40282e+38} @@ -4040,6 +3985,7 @@ View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -4315,7 +4261,7 @@ {{1, 9}, {262, 139}} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -4332,6 +4278,7 @@ View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -4485,7 +4432,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {306, 122}} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -4499,6 +4446,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -4606,7 +4554,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {262, 127}} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -4619,7 +4567,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {3.40282e+38, 3.40282e+38} + {256, 191} + {162.05, 5} 256 @@ -4706,8 +4655,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {220, 112}} {{0, 0}, {1920, 1178}} - {0, 22} - {3.40282e+38, 3.40282e+38} + {162.05, 27} + {256, 213} 9 @@ -4720,6 +4669,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {350, 200} 256 @@ -5143,7 +5093,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {667, 425}} {{0, 0}, {1920, 1178}} - {0, 22} + {350, 222} {3.40282e+38, 3.40282e+38} @@ -5160,6 +5110,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -5291,7 +5242,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {381, 247} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -5305,6 +5256,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -5535,7 +5487,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {418, 191} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -6107,6 +6059,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {300, 200} 256 @@ -6290,7 +6243,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {411, 341} {{0, 0}, {1920, 1178}} - {0, 22} + {300, 222} {3.40282e+38, 3.40282e+38} @@ -6596,6 +6549,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {213, 107} 256 @@ -6741,7 +6695,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {405, 267}} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 129} {3.40282e+38, 3.40282e+38} @@ -6755,6 +6709,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {213, 50} 256 @@ -6789,7 +6744,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{1, 9}, {375, 72}} {{0, 0}, {1920, 1178}} - {0, 22} + {213, 72} {3.40282e+38, 3.40282e+38} @@ -8045,6 +8000,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} + {350, 250} 256 @@ -8295,7 +8251,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {432, 282} {{0, 0}, {1920, 1178}} - {0, 22} + {350, 272} {3.40282e+38, 3.40282e+38} @@ -8346,7 +8302,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 2322 - {588, 0} + {588, 72} @@ -10731,11 +10687,11 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - connectSheetRemoveFromFavorites: - + remove: + - 4518 + 4519 @@ -16774,9 +16730,9 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin - {{83, 165}, {519, 335}} + {{99, 63}, {519, 335}} com.apple.InterfaceBuilder.CocoaPlugin - {{83, 165}, {519, 335}} + {{99, 63}, {519, 335}} {{213, 447}, {519, 335}} @@ -17398,7 +17354,7 @@ Y2hhbmdlIHRoZSBvcmRlcg - 4518 + 4519 @@ -17713,7 +17669,6 @@ Y2hhbmdlIHRoZSBvcmRlcg closeSheet: connect: connectSheetAddToFavorites: - connectSheetRemoveFromFavorites: connectToDB: copyCreateTableSyntax: export: @@ -17769,7 +17724,6 @@ Y2hhbmdlIHRoZSBvcmRlcg id id id - id diff --git a/TableDocument.h b/TableDocument.h index 830dc36e..1ca4c982 100644 --- a/TableDocument.h +++ b/TableDocument.h @@ -79,7 +79,7 @@ CMMCPConnection *mySQLConnection; - NSArray *favorites; + NSMutableArray *favorites; NSArray *variables; NSString *selectedDatabase; NSString *selectedFavorite; @@ -102,7 +102,6 @@ - (id)selectedFavorite; - (NSString *)selectedFavoritePassword; - (void)connectSheetAddToFavorites:(id)sender; -- (void)connectSheetRemoveFromFavorites:(id)sender; - (void)addToFavoritesHost:(NSString *)host socket:(NSString *)socket user:(NSString *)user password:(NSString *)password port:(NSString *)port database:(NSString *)database @@ -111,6 +110,7 @@ sshUser:(NSString *)sshUser // no-longer in use sshPassword:(NSString *)sshPassword // no-longer in use sshPort:(NSString *)sshPort; // no-longer in use +- (NSMutableArray *)favorites; //alert sheets method - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo; @@ -221,3 +221,4 @@ @end extern NSString *TableDocumentFavoritesControllerSelectionIndexDidChange; +extern NSString *TableDocumentFavoritesControllerFavoritesDidChange; diff --git a/TableDocument.m b/TableDocument.m index fc31049b..d109c0e0 100644 --- a/TableDocument.m +++ b/TableDocument.m @@ -35,6 +35,7 @@ #import NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocumentFavoritesControllerSelectionIndexDidChange"; +NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFavoritesControllerFavoritesDidChange"; @implementation TableDocument @@ -54,6 +55,9 @@ NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocum { // register selection did change handler for favorites controller (used in connect sheet) [favoritesController addObserver:self forKeyPath:@"selectionIndex" options:NSKeyValueChangeInsertion context:TableDocumentFavoritesControllerSelectionIndexDidChange]; + + // register value change handler for favourites, so we can save them to preferences + [self addObserver:self forKeyPath:@"favorites" options:0 context:TableDocumentFavoritesControllerFavoritesDidChange]; // register double click for the favorites view (double click favorite to connect) [connectFavoritesTableView setTarget:self]; @@ -69,10 +73,15 @@ NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocum { if (context == TableDocumentFavoritesControllerSelectionIndexDidChange) { [self chooseFavorite:self]; + return; } - else { - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; - } + + if (context == TableDocumentFavoritesControllerFavoritesDidChange) { + [prefs setObject:[self favorites] forKey:@"favorites"]; + return; + } + + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } @@ -275,16 +284,16 @@ reused when user hits the close button of the variablseSheet or of the createTab selectedFavorite = [[favoritesButton titleOfSelectedItem] retain]; } -- (NSArray *)favorites +- (NSMutableArray *)favorites { // if no favorites, load from user defaults if (!favorites) { - favorites = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"]]; + favorites = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"]]; } // if no favorites in user defaults, load empty ones if (!favorites) { - favorites = [[NSArray array] retain]; + favorites = [[NSMutableArray array] retain]; } return favorites; @@ -325,12 +334,6 @@ reused when user hits the close button of the variablseSheet or of the createTab [self addToFavoritesHost:[hostField stringValue] socket:[socketField stringValue] user:[userField stringValue] password:[passwordField stringValue] port:[portField stringValue] database:[databaseField stringValue] useSSH:false sshHost:@"" sshUser:@"" sshPassword:@"" sshPort:@""]; } -- (void)connectSheetRemoveFromFavorites:(id)sender -{ - NSBeep(); - NSLog(@"Remove from favourites is not yet implemented."); -} - /** * add actual connection to favorites */ @@ -343,8 +346,6 @@ reused when user hits the close button of the variablseSheet or of the createTab sshPassword:(NSString *)sshPassword // no-longer in use sshPort:(NSString *)sshPort // no-longer in use { - NSEnumerator *enumerator = [favorites objectEnumerator]; - id favorite; NSString *favoriteName = [NSString stringWithFormat:@"%@@%@", user, host]; if (![database isEqualToString:@""]) favoriteName = [NSString stringWithFormat:@"%@ %@", database, favoriteName]; @@ -354,28 +355,19 @@ reused when user hits the close button of the variablseSheet or of the createTab NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"Please enter at least a host or socket.", @"message of panel when host/socket are missing"), NSLocalizedString(@"OK", @"OK button"), nil, nil); return; } - - // test if favorite name isn't used by another favorite - while (favorite = [enumerator nextObject]) { - if ([[favorite objectForKey:@"name"] isEqualToString:favoriteName]) { - NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), [NSString stringWithFormat:NSLocalizedString(@"Favorite %@ has already been saved!\nOpen Preferences to change the names of the favorites.", @"message of panel when favorite name has already been used"), favoriteName], NSLocalizedString(@"OK", @"OK button"), nil, nil); - return; - } - } [self willChangeValueForKey:@"favorites"]; // write favorites and password NSDictionary *newFavorite = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:favoriteName, host, socket, user, port, database, nil] forKeys:[NSArray arrayWithObjects:@"name", @"host", @"socket", @"user", @"port", @"database", nil]]; - favorites = [[favorites arrayByAddingObject:newFavorite] retain]; + [favorites addObject:newFavorite]; if (![password isEqualToString:@""]) { [keyChainInstance addPassword:password forName:[NSString stringWithFormat:@"Sequel Pro : %@", favoriteName] account:[NSString stringWithFormat:@"%@@%@/%@", user, host, database]]; } - [prefs setObject:favorites forKey:@"favorites"]; // select new favorite selectedFavorite = [favoriteName retain]; -- cgit v1.2.3