aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPQueryFavoriteManager.m
diff options
context:
space:
mode:
Diffstat (limited to 'Source/SPQueryFavoriteManager.m')
-rw-r--r--Source/SPQueryFavoriteManager.m455
1 files changed, 224 insertions, 231 deletions
diff --git a/Source/SPQueryFavoriteManager.m b/Source/SPQueryFavoriteManager.m
index 5ad6644d..ce10bfa7 100644
--- a/Source/SPQueryFavoriteManager.m
+++ b/Source/SPQueryFavoriteManager.m
@@ -26,6 +26,7 @@
#import "SPQueryFavoriteManager.h"
#import "ImageAndTextCell.h"
#import "SPEncodingPopupAccessory.h"
+#import "SPQueryController.h"
#define DEFAULT_QUERY_FAVORITE_FILE_EXTENSION @"sql"
#define DEFAULT_SEQUELPRO_FILE_EXTENSION @"spf"
@@ -36,7 +37,7 @@
#define QUERY_FAVORITES_PB_DRAG_TYPE @"SequelProQueryFavoritesPasteboard"
@interface SPQueryFavoriteManager (Private)
-- (void)_writePendingQueryString;
+- (void)_initWithNoSelection;
@end
@implementation SPQueryFavoriteManager
@@ -51,12 +52,11 @@
prefs = [NSUserDefaults standardUserDefaults];
- favoriteProperties = [[NSMutableArray alloc] init];
favorites = [[NSMutableArray alloc] init];
- selectedRowBeforeChangingSelection = 0;
- pendingQueryString = [[NSMutableString alloc] init];
delegateRespondsToFavoriteUpdates = [delegate respondsToSelector:@selector(queryFavoritesHaveBeenUpdated:)];
+ tableDocumentInstance = [delegate valueForKeyPath:@"tableDocumentInstance"];
+ delegatesFileURL = [[delegate valueForKeyPath:@"tableDocumentInstance"] fileURL];
}
return self;
@@ -64,9 +64,7 @@
- (void)dealloc
{
- [favoriteProperties release];
[favorites release];
- [pendingQueryString release];
[super dealloc];
}
@@ -88,62 +86,91 @@
options:bindingOptions];
- [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"GLOBAL", @"name", nil]];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_HEADER]];
+ [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ @"Global", @"name",
+ @"", @"headerOfFileURL",
+ @"", @"query",
+ nil]];
// Build data source for global queryFavorites (as mutable copy! otherwise each
// change will be stored in the prefs at once)
if([prefs objectForKey:@"queryFavorites"]) {
- for(id fav in [prefs objectForKey:@"queryFavorites"]) {
+ for(id fav in [prefs objectForKey:@"queryFavorites"])
[favorites addObject:[fav mutableCopy]];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
- }
}
- [favoritesTableView reloadData];
- // Set Remove button state
- [removeButton setEnabled:([favorites count] > 1)];
+ [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ [[[delegatesFileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent], @"name",
+ [delegatesFileURL absoluteString], @"headerOfFileURL",
+ @"", @"query",
+ nil]];
+
+ if([[SPQueryController sharedQueryController] favoritesForFileURL:delegatesFileURL]) {
+ for(id fav in [[SPQueryController sharedQueryController] favoritesForFileURL:delegatesFileURL])
+ [favorites addObject:[fav mutableCopy]];
+ }
+
+
+ // Select the first query if any
+ NSUInteger i = 0;
+ for(i=0; i < [favorites count]; i++ )
+ if(![[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"])
+ break;
- // Select the first query
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
+ [[self window] makeFirstResponder:favoritesTableView];
+ [self _initWithNoSelection];
// Register drag types
[favoritesTableView registerForDraggedTypes:[NSArray arrayWithObject:QUERY_FAVORITES_PB_DRAG_TYPE]];
+
+ [favoritesArrayController setContent:favorites];
+ [favoritesTableView reloadData];
+
+ // Set Remove button state
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
+
}
#pragma mark -
#pragma mark Accessor methods
/**
- * Returns the global query favorites array.
+ * Returns the query favorites array for fileURL.
+ * fileURL == nil → global favorites
*/
-- (NSMutableArray *)globalQueryFavorites
+- (NSMutableArray *)queryFavoritesForFileURL:(NSURL *)fileURL
{
- NSMutableArray *globals = [NSMutableArray array];
-
- NSUInteger i;
-
- for(i=1; i<[favorites count]; i++)
- if([[favoriteProperties objectAtIndex:i] intValue] == SP_FAVORITETYPE_GLOBAL)
- [globals addObject:[favorites objectAtIndex:i]];
+ NSMutableArray *favs = [NSMutableArray array];
+ NSString *fileURLstring;
- return globals;
-}
+ if(fileURL == nil)
+ fileURLstring = @"";
+ else
+ fileURLstring = [fileURL absoluteString];
-/**
- * Returns the global query favorites array.
- */
-- (NSMutableArray *)connectionQueryFavorites
-{
- NSMutableArray *conns = [NSMutableArray array];
-
- NSUInteger i;
-
- for(i=1; i<[favorites count]; i++)
- if([[favoriteProperties objectAtIndex:i] intValue] == SP_FAVORITETYPE_CONNECTION)
- [conns addObject:[favorites objectAtIndex:i]];
+ NSUInteger i = 0;
+
+ // Look for the header specified by fileURL
+ while(i<[favorites count]) {
+ if ([[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"]
+ && [[[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"] isEqualToString:fileURLstring]) {
+ i++;
+ break;
+ }
+ i++;
+ }
+
+ // Take all favorites until the next header or end of favorites
+ for(i; i<[favorites count]; i++) {
- return conns;
+ if(![[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"])
+ [favs addObject:[favorites objectAtIndex:i]];
+ else
+ break;
+
+ }
+
+ return favs;
}
/**
@@ -158,31 +185,53 @@
#pragma mark IBAction methods
/**
- * Adds a query favorite
+ * Adds/Inserts a query favorite
*/
- (IBAction)addQueryFavorite:(id)sender
{
- [self _writePendingQueryString];
- NSMutableDictionary *favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Favorite", @"", nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
+ NSMutableDictionary *favorite;
+ NSUInteger insertIndex;
+
+ // Duplicate a selected favorite if sender == self
+ if(sender == self)
+ favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[[favoriteNameTextField stringValue] stringByAppendingFormat:@" Copy"], [favoriteQueryTextView string], nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
+ // Add a new favorite
+ else
+ favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Favorite", @"", nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
- [favorites addObject:favorite];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
+ if([favoritesTableView numberOfSelectedRows] > 0) {
+ insertIndex = [[favoritesTableView selectedRowIndexes] lastIndex]+1;
+ [favorites insertObject:favorite atIndex:insertIndex];
+ } else {
+ [favorites addObject:favorite];
+ insertIndex = [favorites count] - 1;
+ }
+
+ [favoritesArrayController rearrangeObjects];
[favoritesTableView reloadData];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:([favorites count] - 1)] byExtendingSelection:NO];
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:insertIndex] byExtendingSelection:NO];
[favoritesTableView scrollRowToVisible:[favoritesTableView selectedRow]];
- selectedRowBeforeChangingSelection = [favorites count] - 1;
-
- [removeButton setEnabled:([favorites count] > 1)];
-
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
[[self window] makeFirstResponder:favoriteNameTextField];
}
/**
+ * Duplicates a query favorite
+ */
+- (IBAction)duplicateQueryFavorite:(id)sender
+{
+ if ([favoritesTableView numberOfSelectedRows] == 1)
+ [self addQueryFavorite:self];
+ else
+ NSBeep();
+}
+
+/**
* Removes a query favorite
*/
- (IBAction)removeQueryFavorite:(id)sender
@@ -227,32 +276,6 @@
}
/**
- * Copies a query favorite
- */
-- (IBAction)copyQueryFavorite:(id)sender
-{
- if ([favoritesTableView numberOfSelectedRows] == 1) {
- NSMutableDictionary *favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[[favoriteNameTextField stringValue] stringByAppendingFormat:@" Copy"], [favoriteQueryTextView string], nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
-
- [self _writePendingQueryString];
-
- [favorites addObject:favorite];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
-
- [favoritesTableView reloadData];
-
- // Update selection
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:([favorites count] - 1)] byExtendingSelection:NO];
- selectedRowBeforeChangingSelection = [favorites count] - 1;
-
- [favoritesTableView scrollRowToVisible:[favoritesTableView selectedRow]];
-
- [[self window] makeFirstResponder:favoriteNameTextField];
-
- }
-}
-
-/**
* Saves the currently selected query favorite to a user specified file.
*/
- (IBAction)saveFavoriteToFile:(id)sender
@@ -289,7 +312,20 @@
- (IBAction)importFavoritesByAdding:(id)sender
{
+ NSOpenPanel *panel = [NSOpenPanel openPanel];
+ [panel setCanSelectHiddenExtension:YES];
+ [panel setDelegate:self];
+ [panel setCanChooseDirectories:NO];
+ [panel setAllowsMultipleSelection:NO];
+ [panel setResolvesAliases:YES];
+ [panel beginSheetForDirectory:nil
+ file:@""
+ types:[NSArray arrayWithObjects:@"spf", @"sql", nil]
+ modalForWindow:[self window]
+ modalDelegate:self
+ didEndSelector:@selector(importPanelDidEnd:returnCode:contextInfo:)
+ contextInfo:NULL];
}
- (IBAction)importFavoritesByReplacing:(id)sender
@@ -313,22 +349,26 @@
[NSApp endSheet:[self window] returnCode:0];
[[self window] orderOut:self];
- // Save button was pressed
+ // "Apply Changes" button was pressed
if([sender tag]) {
- // Ensure that last changes will be written to prefs
+
+ // Ensure that last changes will be written back
// if only one favorite is selected; otherwise unstable state
if ([favoritesTableView numberOfSelectedRows] == 1) {
- [self _writePendingQueryString];
[[self window] makeFirstResponder:favoritesTableView];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRowBeforeChangingSelection] byExtendingSelection:NO];
}
- // Inform the delegate that the query favorites have been updated
- if (delegateRespondsToFavoriteUpdates)
+ // Update current document's and global query favorites
+ [[SPQueryController sharedQueryController] replaceFavoritesByArray:
+ [self queryFavoritesForFileURL:delegatesFileURL] forFileURL:delegatesFileURL];
+ [prefs setObject:[self queryFavoritesForFileURL:nil] forKey:@"queryFavorites"];
+
+ // Inform delegate to update
+ if(delegateRespondsToFavoriteUpdates)
[delegate queryFavoritesHaveBeenUpdated:self];
}
-
+
}
#pragma mark -
@@ -379,7 +419,7 @@
if([[aTableColumn identifier] isEqualToString:@"name"] && [anObject length]) {
[[favorites objectAtIndex:rowIndex] setObject:[anObject description] forKey:@"name"];
- [[favorites objectAtIndex:rowIndex] setObject:[favoriteQueryTextView string] forKey:@"query"];
+ // [[favorites objectAtIndex:rowIndex] setObject:[favoriteQueryTextView string] forKey:@"query"];
[favoriteNameTextField setStringValue:[anObject description]];
}
@@ -392,72 +432,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
- [pendingQueryString setString:[favoriteQueryTextView string]];
- [self _writePendingQueryString];
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? NO : YES;
-}
-
-/*
- * Update name and query view and control several table selection modi
- */
-- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
-{
-
- // store the last selected favorite for writing the pending query string
- // - for a single favorite selection
- if([favoritesTableView numberOfSelectedRows] == 1)
- selectedRowBeforeChangingSelection = [favoritesTableView selectedRow];
- // - for multiple favorite selection
- if([favoritesTableView numberOfSelectedRows] > 1) {
-
- // Save query string directly
- if(selectedRowBeforeChangingSelection > 0 && selectedRowBeforeChangingSelection < [favorites count])
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[favoriteQueryTextView string] forKey:@"query"];
-
- // Update name text field
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteNameTextField setEditable:NO];
- [favoriteNameTextField setSelectable:NO];
-
- // This is an "hack"; if one set it to @"" it could happen that
- // the wrong pending query string will be saved
- [favoriteQueryTextView setTextColor:[NSColor clearColor]];
- [favoriteQueryTextView setEditable:NO];
- [favoriteQueryTextView setSelectable:NO];
-
- return;
-
- } else {
- [favoriteNameTextField setEditable:YES];
- [favoriteNameTextField setSelectable:YES];
- [favoriteQueryTextView setSelectable:YES];
- [favoriteQueryTextView setEditable:YES];
- }
-
-
- // only the "GLOBAL" header is in the table
- if([favorites count] < 2) {
- [self _writePendingQueryString];
- [[self window] makeFirstResponder:favoritesTableView];
- [[favoriteNameTextField cell] setPlaceholderString:SP_NO_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteQueryTextView setString:@""];
- [pendingQueryString setString:@""];
- selectedRowBeforeChangingSelection = -1;
- return;
- }
-
- // Update name and query field contents
- NSUInteger row = (NSUInteger)[[aNotification object] selectedRow];
-
- // This is needed if one deletes the last table item
- if(row > [favorites count] - 1) row = [favorites count] - 1;
- if(row < 1) row = 1;
-
- [favoriteNameTextField setStringValue:[[favorites objectAtIndex:row] objectForKey:@"name"]];
- [favoriteQueryTextView setString:[[favorites objectAtIndex:row] objectForKey:@"query"]];
-
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? NO : YES;
}
/*
@@ -466,14 +441,14 @@
*/
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
- if([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER && [[aTableColumn identifier] isEqualToString:@"name"]) {
+ if([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"] && [[aTableColumn identifier] isEqualToString:@"name"]) {
// if([[[favoriteProperties objectAtIndex:rowIndex] objectForKey:@"isGroup"] isEqualToString:@"1"])
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"NSRightFacingTriangleTemplate"]];
// else
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"NSLeftFacingTriangleTemplate"]];
[(ImageAndTextCell*)aCell setIndentationLevel:0];
}
- else if([[favoriteProperties objectAtIndex:rowIndex] intValue] != SP_FAVORITETYPE_HEADER && [[aTableColumn identifier] isEqualToString:@"name"]) {
+ else if(![[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"] && [[aTableColumn identifier] isEqualToString:@"name"]) {
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"dummy-small"]];
[(ImageAndTextCell*)aCell setIndentationLevel:1];
}
@@ -484,7 +459,7 @@
*/
- (CGFloat)tableView:(NSTableView *)aTableView heightOfRow:(NSInteger)rowIndex
{
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? 20 : 18;
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? 20 : 18;
}
/*
@@ -492,7 +467,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
- if([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) {
+ if([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) {
return NO;
} else {
isTableCellEditing = YES;
@@ -505,7 +480,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView isGroupRow:(NSInteger)rowIndex
{
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? YES : NO;
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? YES : NO;
}
/*
* Detect if inline editing was done - then ESC to close the sheet will be activate
@@ -514,6 +489,7 @@
{
isTableCellEditing = NO;
}
+
/*
* Changes in the name text field will be saved in data source directly
* to update the table view accordingly
@@ -522,7 +498,7 @@
{
// Do nothing if no favorite is selected
- if([favoritesTableView selectedRow] < 1) return;
+ if([favoritesTableView numberOfSelectedRows] < 1) return;
id object = [notification object];
@@ -533,27 +509,6 @@
}
-/*
- * Changes in the query text view will be cached as pending query string which will update
- * the data source before selecting an other favorite or before an other event.
- * If multiple rows are selected update name field.
- */
-- (void)textViewDidChangeSelection:(NSNotification *)notification
-{
- id object = [notification object];
-
- if(object == favoriteQueryTextView) {
- if(![favoriteQueryTextView isEditable]) return;
- [pendingQueryString setString:[NSString stringWithString:[favoriteQueryTextView string]]];
- if([favoritesTableView numberOfSelectedRows] > 1) {
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- selectedRowBeforeChangingSelection = -1;
- return;
- }
- }
-}
-
#pragma mark -
#pragma mark Menu validation
@@ -568,7 +523,7 @@
SEL action = [menuItem action];
- if ( (action == @selector(copyQueryFavorite:)) ||
+ if ( (action == @selector(duplicateQueryFavorite:)) ||
(action == @selector(saveFavoriteToFile:)))
{
return ([favoritesTableView numberOfSelectedRows] == 1);
@@ -592,27 +547,26 @@
* Return whether or not the supplied rows can be written.
*/
- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows toPasteboard:(NSPasteboard *)pboard
-{
+{
+
+ // Up to now only one row can be dragged
if ([rows count] == 1) {
-
+
NSArray *pboardTypes = [NSArray arrayWithObject:QUERY_FAVORITES_PB_DRAG_TYPE];
NSInteger originalRow = [[rows objectAtIndex:0] intValue];
-
+
+ if(originalRow < 1) return NO;
+
+ // Do not drag headers
+ if([[favorites objectAtIndex:originalRow] objectForKey:@"headerOfFileURL"]) return NO;
+
[pboard declareTypes:pboardTypes owner:nil];
[pboard setString:[[NSNumber numberWithInt:originalRow] stringValue] forType:QUERY_FAVORITES_PB_DRAG_TYPE];
-
- // Save query string before dragging
- // and suppress the writing of pending query string changes
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[favoriteQueryTextView string] forKey:@"query"];
- selectedRowBeforeChangingSelection = -1;
+
return YES;
- } else {
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRowBeforeChangingSelection] byExtendingSelection:NO];
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteQueryTextView setTextColor:[NSColor clearColor]];
+
}
-
+
return NO;
}
@@ -644,17 +598,26 @@
{
NSInteger originalRow = [[[info draggingPasteboard] stringForType:QUERY_FAVORITES_PB_DRAG_TYPE] intValue];
NSInteger destinationRow = row;
-
- if (destinationRow > originalRow) destinationRow--;
-
+
+ if(destinationRow == originalRow || row < 1) return NO;
+ if(destinationRow > originalRow) destinationRow--;
+
NSMutableDictionary *draggedRow = [NSMutableDictionary dictionaryWithDictionary:[favorites objectAtIndex:originalRow]];
-
+
[favorites removeObjectAtIndex:originalRow];
[favorites insertObject:draggedRow atIndex:destinationRow];
-
+
[favoritesTableView reloadData];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:destinationRow] byExtendingSelection:NO];
-
+ [favoritesArrayController rearrangeObjects];
+
+ if([[favorites objectAtIndex:destinationRow] objectForKey:@"headerOfFileURL"])
+ [self _initWithNoSelection];
+ else if([favoritesTableView numberOfSelectedRows] == 1
+ && [favoritesTableView selectedRow] == originalRow) {
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:destinationRow] byExtendingSelection:NO];
+ [favoritesArrayController setSelectionIndexes:[NSIndexSet indexSetWithIndex:destinationRow]];
+ }
+
return YES;
}
@@ -678,40 +641,77 @@
// get last index
NSUInteger currentIndex = [indexes lastIndex];
- NSUInteger idx = currentIndex;
-
- // Prevend to write pending changes
- selectedRowBeforeChangingSelection = -1;
- [pendingQueryString setString:@""];
while (currentIndex != NSNotFound) {
[favorites removeObjectAtIndex:currentIndex];
- [favoriteProperties removeObjectAtIndex:currentIndex];
// get next index (beginning from the end)
currentIndex = [indexes indexLessThanIndex:currentIndex];
- [favoritesTableView reloadData];
}
+ [favoritesArrayController rearrangeObjects];
+ [favoritesTableView reloadData];
+
// Set focus to favorite list to avoid an unstable state
[[self window] makeFirstResponder:favoritesTableView];
- // Try to reselect a favorite
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
- if(idx > [favorites count]-1)
- idx = [favorites count]-1;
- if(idx != 0)
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:idx] byExtendingSelection:NO];
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
+ }
+ }
+}
+
+/**
+ * Import panel did end method.
+ */
+- (void)importPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(NSString *)contextInfo
+{
+
+ if (returnCode == NSOKButton) {
+
+ NSString *filename = [[panel filenames] objectAtIndex:0];
+ NSError *readError = nil;
+ NSString *convError = nil;
+ NSPropertyListFormat format;
+
+ NSDictionary *spf = nil;
+
+ if([[[filename pathExtension] lowercaseString] isEqualToString:@"spf"]) {
+ NSData *pData = [NSData dataWithContentsOfFile:filename options:NSUncachedRead error:&readError];
+
+ spf = [[NSPropertyListSerialization propertyListFromData:pData
+ mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError] retain];
+
+ if(!spf || readError != nil || [convError length] || !(format == NSPropertyListXMLFormat_v1_0 || format == NSPropertyListBinaryFormat_v1_0)) {
+ NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while reading data file", @"error while reading data file")]
+ defaultButton:NSLocalizedString(@"OK", @"OK button")
+ alternateButton:nil
+ otherButton:nil
+ informativeTextWithFormat:NSLocalizedString(@"File couldn't be read.", @"error while reading data file")];
- if(idx > 1) {
- [favoriteNameTextField setStringValue:[[favorites objectAtIndex:idx] objectForKey:@"name"]];
- [favoriteQueryTextView setString:[[favorites objectAtIndex:idx] objectForKey:@"query"]];
+ [alert setAlertStyle:NSCriticalAlertStyle];
+ [alert runModal];
+ return;
}
- [removeButton setEnabled:([favorites count] > 1)];
+ if([spf objectForKey:@"queryFavorites"] && [[spf objectForKey:@"queryFavorites"] count]) {
+ [favorites addObjectsFromArray:[spf objectForKey:@"queryFavorites"]];
+ [favoritesArrayController rearrangeObjects];
+ [favoritesTableView reloadData];
+ } else {
+ NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while reading data file", @"error while reading data file")]
+ defaultButton:NSLocalizedString(@"OK", @"OK button")
+ alternateButton:nil
+ otherButton:nil
+ informativeTextWithFormat:NSLocalizedString(@"No query favorites found.", @"error that no query favorites found")];
+
+ [alert setAlertStyle:NSInformationalAlertStyle];
+ [alert runModal];
+ return;
+ }
}
}
}
+
/**
* Save panel did end method.
*/
@@ -736,7 +736,8 @@
// 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"];
@@ -749,8 +750,7 @@
if([indexes containsIndex:i])
[favoriteData addObject:[favorites objectAtIndex:i]];
- [data setObject:favoriteData forKey:@"queryFavorites"];
- [spfdata setObject:data forKey:@"data"];
+ [spfdata setObject:favoriteData forKey:@"queryFavorites"];
NSString *err = nil;
NSData *plist = [NSPropertyListSerialization dataFromPropertyList:spfdata
@@ -777,18 +777,11 @@
}
}
-/*
- * Before an other favorite will be chosen or an other event will unfocus the
- * query text view store the query into the data source
- */
-- (void)_writePendingQueryString
+- (void)_initWithNoSelection
{
- if(selectedRowBeforeChangingSelection > 0 && selectedRowBeforeChangingSelection < [favorites count] ) {
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[NSString stringWithString:pendingQueryString] forKey:@"query"];
- } else if(selectedRowBeforeChangingSelection = 0) {
- [[favorites objectAtIndex:[favoritesTableView selectedRow]] setObject:[NSString stringWithString:pendingQueryString] forKey:@"query"];
- }
- [pendingQueryString setString:@""];
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
+ [[favoriteNameTextField cell] setPlaceholderString:SP_NO_SELECTION_PLACEHOLDER_STRING];
+ [favoriteNameTextField setStringValue:@""];
+ [favoriteQueryTextView setString:@""];
}
-
@end