From 96e8c7492cb0b27173cbc3a56d703b100dad0e5a Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 28 Aug 2009 21:29:33 +0000 Subject: =?UTF-8?q?=E2=80=A2=20improved=20Query=20Favorite=20Manager=20-?= =?UTF-8?q?=20tableView=20is=20now=20a=20SPTableView=20for=20right-click?= =?UTF-8?q?=20selects=20item=20first=20-=20corrected=20tooltips=20for=20al?= =?UTF-8?q?l=20buttons=20and=20menu=20items=20-=20added=20short-cut=20?= =?UTF-8?q?=E2=8C=98D=20for=20duplicate,=20=E2=8C=AB=20for=20remove,=20and?= =?UTF-8?q?=20=E2=8C=A5=E2=8E=8B=20for=20gear=20menu=20(which=20could=20be?= =?UTF-8?q?=20the=20standard=20for=20all=20gear=20menus=3F)=20-=20remove?= =?UTF-8?q?=20selected=20favorite(s)=20asks=20the=20user=20first=20-=20add?= =?UTF-8?q?ed=20chance=20to=20select=20and=20remove=20more=20than=20one=20?= =?UTF-8?q?favorite=20-=20added=20chance=20to=20export=20selected=20favori?= =?UTF-8?q?tes=20as=20SPF=20file=20The=20file=20structure=20(format=20'que?= =?UTF-8?q?ry=20favorites')=20goes=20conform=20with=20the=20SPF=20'connect?= =?UTF-8?q?ion'=20format.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To be discussed: Should we add a separate Import Favorites menu or should we do that via the general "Open…" menu item since the SPF format tells SP what to do with that file? --- Source/SPQueryFavoriteManager.m | 155 +++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 24 deletions(-) (limited to 'Source/SPQueryFavoriteManager.m') diff --git a/Source/SPQueryFavoriteManager.m b/Source/SPQueryFavoriteManager.m index 6c2c48b0..df5b802a 100644 --- a/Source/SPQueryFavoriteManager.m +++ b/Source/SPQueryFavoriteManager.m @@ -27,6 +27,8 @@ #import "SPEncodingPopupAccessory.h" #define DEFAULT_QUERY_FAVORITE_FILE_EXTENSION @"sql" +#define DEFAULT_SEQUELPRO_FILE_EXTENSION @"spf" + #define QUERY_FAVORITES_PB_DRAG_TYPE @"SequelProQueryFavoritesPasteboard" @implementation SPQueryFavoriteManager @@ -118,21 +120,21 @@ */ - (IBAction)removeQueryFavorite:(id)sender { - if ([favoritesTableView numberOfSelectedRows] == 1) { - [queryFavoritesController removeObjectAtArrangedObjectIndex:[favoritesTableView selectedRow]]; - - [favoritesTableView reloadData]; - - [prefs synchronize]; - - // Set focus to favorite list to avoid an unstable state - [[self window] makeFirstResponder:favoritesTableView]; + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Remove selected query favorites?", @"remove selected query favorites message") + defaultButton:NSLocalizedString(@"Cancel", @"cancel button") + alternateButton:NSLocalizedString(@"Remove", @"remove button") + otherButton:nil + informativeTextWithFormat:NSLocalizedString(@"Are you sure you want to remove all selected query favorites? This action cannot be undone.", @"remove all selected query favorites informative message")]; - // Inform the delegate that the query favorites have been updated - if (delegateRespondsToFavoriteUpdates) { - [delegate queryFavoritesHaveBeenUpdated:self]; - } - } + [alert setAlertStyle:NSCriticalAlertStyle]; + + NSArray *buttons = [alert buttons]; + + // Change the alert's cancel button to have the key equivalent of return + [[buttons objectAtIndex:0] setKeyEquivalent:@"\r"]; + [[buttons objectAtIndex:1] setKeyEquivalent:@""]; + + [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"removeSelectedFavorites"]; } /** @@ -190,12 +192,37 @@ [panel setExtensionHidden:NO]; [panel setAllowsOtherFileTypes:YES]; [panel setCanSelectHiddenExtension:YES]; - + [panel setCanCreateDirectories:YES]; + [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[prefs integerForKey:@"lastSqlFileEncoding"] includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; [encodingPopUp setEnabled:YES]; - [panel beginSheetForDirectory:nil file:[favoriteNameTextField stringValue] modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:NULL]; + [panel beginSheetForDirectory:nil file:[favoriteNameTextField stringValue] modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:@"saveQuery"]; +} + +- (IBAction)exportFavorites:(id)sender +{ + NSSavePanel *panel = [NSSavePanel savePanel]; + + [panel setRequiredFileType:DEFAULT_SEQUELPRO_FILE_EXTENSION]; + + [panel setExtensionHidden:NO]; + [panel setAllowsOtherFileTypes:NO]; + [panel setCanSelectHiddenExtension:YES]; + [panel setCanCreateDirectories:YES]; + + [panel beginSheetForDirectory:nil file:nil modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:@"exportFavorites"]; +} + +- (IBAction)importFavoritesByAdding:(id)sender +{ + +} + +- (IBAction)importFavoritesByReplacing:(id)sender +{ + } /** @@ -266,12 +293,16 @@ { SEL action = [menuItem action]; - if ((action == @selector(removeQueryFavorite:)) || - (action == @selector(copyQueryFavorite:)) || + if ( (action == @selector(copyQueryFavorite:)) || (action == @selector(saveFavoriteToFile:))) { return ([favoritesTableView numberOfSelectedRows] == 1); } + else if ( (action == @selector(removeQueryFavorite:)) || + ( action == @selector(exportFavorites:))) + { + return ([favoritesTableView numberOfSelectedRows] > 0); + } else if (action == @selector(removeAllQueryFavorites:)) { return ([[queryFavoritesController arrangedObjects] count] > 0); } @@ -354,6 +385,31 @@ [queryFavoritesController removeObjects:[queryFavoritesController arrangedObjects]]; } } + if([contextInfo isEqualToString:@"removeSelectedFavorites"]) { + if (returnCode == NSAlertAlternateReturn) { + NSIndexSet *indexes = [favoritesTableView selectedRowIndexes]; + + // get last index + NSUInteger currentIndex = [indexes lastIndex]; + + while (currentIndex != NSNotFound) { + [queryFavoritesController removeObjectAtArrangedObjectIndex:currentIndex]; + // get next index (beginning from the end) + currentIndex = [indexes indexLessThanIndex:currentIndex]; + } + + [favoritesTableView reloadData]; + + [prefs synchronize]; + + // Set focus to favorite list to avoid an unstable state + [[self window] makeFirstResponder:favoritesTableView]; + + // Inform the delegate that the query favorites have been updated + if (delegateRespondsToFavoriteUpdates) + [delegate queryFavoritesHaveBeenUpdated:self]; + } + } } /** @@ -361,15 +417,66 @@ */ - (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { - if (returnCode == NSOKButton) { - NSError *error = nil; + + if([contextInfo isEqualToString:@"saveQuery"]) { + if (returnCode == NSOKButton) { + NSError *error = nil; - [prefs setInteger:[[encodingPopUp selectedItem] tag] forKey:@"lastSqlFileEncoding"]; - [prefs synchronize]; + [prefs setInteger:[[encodingPopUp selectedItem] tag] forKey:@"lastSqlFileEncoding"]; + [prefs synchronize]; - [[favoriteQueryTextView string] writeToFile:[panel filename] atomically:YES encoding:[[encodingPopUp selectedItem] tag] error:&error]; + [[favoriteQueryTextView string] writeToFile:[panel filename] atomically:YES encoding:[[encodingPopUp selectedItem] tag] error:&error]; - if (error) [[NSAlert alertWithError:error] runModal]; + if (error) [[NSAlert alertWithError:error] runModal]; + } + } + else if([contextInfo isEqualToString:@"exportFavorites"]) { + if (returnCode == NSOKButton) { + + // Build a SPF with format = "query favorites" + NSMutableDictionary *spfdata = [NSMutableDictionary dictionary]; + NSMutableArray *favoriteData = [NSMutableArray array]; + NSMutableDictionary *data = [NSMutableDictionary dictionary]; + [spfdata setObject:[NSNumber numberWithInt:1] forKey:@"version"]; + [spfdata setObject:@"query favorites" forKey:@"format"]; + [spfdata setObject:[NSNumber numberWithBool:NO] forKey:@"encrypted"]; + + NSIndexSet *indexes = [favoritesTableView selectedRowIndexes]; + + // get last index + NSUInteger currentIndex = [indexes lastIndex]; + + while (currentIndex != NSNotFound) { + [favoriteData addObject:[[self queryFavorites] objectAtIndex:currentIndex]]; + + // get next index (beginning from the end) + currentIndex = [indexes indexLessThanIndex:currentIndex]; + } + [data setObject:favoriteData forKey:@"queryFavorites"]; + [spfdata setObject:data forKey:@"data"]; + + NSString *err = nil; + NSData *plist = [NSPropertyListSerialization dataFromPropertyList:spfdata + format:NSPropertyListXMLFormat_v1_0 + errorDescription:&err]; + + if(err != nil) { + NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while converting query favorite data", @"error while converting query favorite data")] + defaultButton:NSLocalizedString(@"OK", @"OK button") + alternateButton:nil + otherButton:nil + informativeTextWithFormat:err]; + + [alert setAlertStyle:NSCriticalAlertStyle]; + [alert runModal]; + return; + } + + NSError *error = nil; + [plist writeToFile:[panel filename] options:NSAtomicWrite error:&error]; + if (error) [[NSAlert alertWithError:error] runModal]; + + } } } -- cgit v1.2.3