From 252ac0be49bf409b1eda3726f4f4a47bd6b5b5b6 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 7 Sep 2009 10:11:35 +0000 Subject: =?UTF-8?q?=E2=80=A2=20updated=20SPQueryFavoriteManager=20to=20man?= =?UTF-8?q?ge=20document-based=20and=20global=20query=20favorites=20?= =?UTF-8?q?=E2=80=A2=20disabled=20"Select=20Active=20Query"=20in=20CMTextV?= =?UTF-8?q?iew=20if=20shown=20in=20the=20favorite=20manager=20=E2=80=A2=20?= =?UTF-8?q?code=20cleaning=20and=20simplifications?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMTextView.m | 2 +- Source/CustomQuery.m | 64 +++--- Source/SPQueryController.h | 2 + Source/SPQueryController.m | 11 + Source/SPQueryFavoriteManager.h | 23 +- Source/SPQueryFavoriteManager.m | 455 ++++++++++++++++++++-------------------- Source/SPTableView.m | 2 +- 7 files changed, 270 insertions(+), 289 deletions(-) (limited to 'Source') diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 287bd227..4cf7b155 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -2296,7 +2296,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) } // Validate Select Active Query if ([menuItem action] == @selector(selectCurrentQuery)) { - return ([self isEditable]); + return ([self isEditable] && [[self delegate] isKindOfClass:[CustomQuery class]]); } // Disable "Copy with Column Names" and "Copy as SQL INSERT" // in the main menu diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 155113d9..0a2958ee 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -2398,34 +2398,23 @@ - (void)queryFavoritesHaveBeenUpdated:(id)manager { - NSMutableArray *globalFavorites = [[NSMutableArray alloc] init]; - - // Invoked after adding a favorite from inside CustomQuery - if(manager == nil) { - [globalFavorites addObjectsFromArray:[prefs objectForKey:@"queryFavorites"]]; - } - // Invoked by the query favorite manager - else if(manager == favoritesManager){ - // Update global query favorites - [globalFavorites addObjectsFromArray:[favoritesManager globalQueryFavorites]]; - [prefs setObject:globalFavorites forKey:@"queryFavorites"]; - } + NSMenuItem *headerMenuItem; + NSMenu *menu = [queryFavoritesButton menu]; // Remove all favorites beginning from the end while([queryFavoritesButton numberOfItems] > 6) [queryFavoritesButton removeItemAtIndex:[queryFavoritesButton numberOfItems]-1]; - // Insert new favorites and allow menu items with the same name - NSMenu *menu = [queryFavoritesButton menu]; - - NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Document" action:NULL keyEquivalent:@""]; - [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; - [item setToolTip:[NSString stringWithFormat:@"‘%@’ based favorites", + // Build document-based list + headerMenuItem = [[NSMenuItem alloc] initWithTitle: + [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent] + action:NULL keyEquivalent:@""]; + [headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; + [headerMenuItem setToolTip:[NSString stringWithFormat:@"‘%@’ based favorites", [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent]]]; - [item setIndentationLevel:0]; - [menu addItem:item]; - [item release]; - + [headerMenuItem setIndentationLevel:0]; + [menu addItem:headerMenuItem]; + [headerMenuItem release]; for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) { NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""]; [item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]]; @@ -2434,14 +2423,14 @@ [item release]; } - item = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""]; - [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; - [item setToolTip:@"Globally stored favorites"]; - [item setIndentationLevel:0]; - [menu addItem:item]; - [item release]; - - for (NSDictionary *favorite in globalFavorites) { + // Build global list + headerMenuItem = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""]; + [headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; + [headerMenuItem setToolTip:@"Globally stored favorites"]; + [headerMenuItem setIndentationLevel:0]; + [menu addItem:headerMenuItem]; + [headerMenuItem release]; + for (NSDictionary *favorite in [prefs objectForKey:@"queryFavorites"]) { NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""]; [item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]]; [item setIndentationLevel:1]; @@ -2449,11 +2438,7 @@ [item release]; } - [globalFavorites release]; - - [prefs synchronize]; - - [queryFavoritesSearchField setStringValue:@""]; + // [queryFavoritesSearchField setStringValue:@""]; } @@ -2508,14 +2493,14 @@ } if([saveQueryFavoriteGlobal state] == NSOnState) { - [favorites addObject:[NSDictionary dictionaryWithObjects: + [favorites addObject:[NSMutableDictionary dictionaryWithObjects: [NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], queryToBeAddded, nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]]]; [prefs setObject:favorites forKey:@"queryFavorites"]; } else { - [[SPQueryController sharedQueryController] addFavorite:[NSDictionary dictionaryWithObjects: - [NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], queryToBeAddded, nil] + [[SPQueryController sharedQueryController] addFavorite:[NSMutableDictionary dictionaryWithObjects: + [NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], [queryToBeAddded mutableCopy], nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]] forFileURL:[tableDocumentInstance fileURL]]; } @@ -2563,7 +2548,6 @@ #pragma mark - -// Last but not least - (id)init { if ((self = [super init])) { @@ -2617,7 +2601,7 @@ NSMenu *menu = [queryHistoryButton menu]; NSString *searchPattern = [queryHistorySearchField stringValue]; for (i=2; i< [menu numberOfItems]; i++) - [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", [queryHistorySearchField stringValue]]])]; + [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])]; } diff --git a/Source/SPQueryController.h b/Source/SPQueryController.h index df842745..abef41de 100644 --- a/Source/SPQueryController.h +++ b/Source/SPQueryController.h @@ -84,6 +84,8 @@ - (void)addFavorite:(NSDictionary *)favorite forFileURL:(NSURL *)fileURL; - (void)replaceFavoritesByArray:(NSArray *)favoritesArray forFileURL:(NSURL *)fileURL; +- (void)removeFavoriteAtIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL; +- (void)insertFavorite:(NSDictionary *)favorite atIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL; - (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL; - (void)replaceHistoryByArray:(NSArray *)historyArray forFileURL:(NSURL *)fileURL; diff --git a/Source/SPQueryController.m b/Source/SPQueryController.m index 4345f616..4dbd3749 100644 --- a/Source/SPQueryController.m +++ b/Source/SPQueryController.m @@ -569,6 +569,17 @@ static SPQueryController *sharedQueryController = nil; } +- (void)removeFavoriteAtIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL +{ + [[favoritesContainer objectForKey:[fileURL absoluteString]] removeObjectAtIndex:index]; +} + +- (void)insertFavorite:(NSDictionary *)favorite atIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL +{ + [[favoritesContainer objectForKey:[fileURL absoluteString]] insertObject:favorite atIndex:index]; +} + + @end @implementation SPQueryController (PrivateAPI) diff --git a/Source/SPQueryFavoriteManager.h b/Source/SPQueryFavoriteManager.h index 28a625d5..4268ffe0 100644 --- a/Source/SPQueryFavoriteManager.h +++ b/Source/SPQueryFavoriteManager.h @@ -25,14 +25,6 @@ #import -enum sp_favorite_types -{ - SP_FAVORITETYPE_HEADER = 0, - SP_FAVORITETYPE_GLOBAL = 1, - SP_FAVORITETYPE_CONNECTION = 2, -}; - - @interface NSObject (SPQueryFavoriteManagerDelegate) - (void)queryFavoritesHaveBeenUpdated:(id)manager; @@ -47,33 +39,32 @@ enum sp_favorite_types BOOL delegateRespondsToFavoriteUpdates; + NSDocument *tableDocumentInstance; + NSURL *delegatesFileURL; IBOutlet NSPopUpButton *encodingPopUp; IBOutlet NSTableView *favoritesTableView; IBOutlet NSTextField *favoriteNameTextField; IBOutlet NSTextView *favoriteQueryTextView; IBOutlet id removeButton; + IBOutlet NSArrayController *favoritesArrayController; + NSMutableArray *favorites; - NSMutableArray *favoriteProperties; - - NSMutableString *pendingQueryString; - - NSUInteger selectedRowBeforeChangingSelection; + BOOL isTableCellEditing; } - (id)initWithDelegate:(id)managerDelegate; // Accessors -- (NSMutableArray *)globalQueryFavorites; -- (NSMutableArray *)connectionQueryFavorites; +- (NSMutableArray *)queryFavoritesForFileURL:(NSURL *)fileURL; - (id)customQueryInstance; // IBAction methods - (IBAction)addQueryFavorite:(id)sender; - (IBAction)removeQueryFavorite:(id)sender; - (IBAction)removeAllQueryFavorites:(id)sender; -- (IBAction)copyQueryFavorite:(id)sender; +- (IBAction)duplicateQueryFavorite:(id)sender; - (IBAction)saveFavoriteToFile:(id)sender; - (IBAction)exportFavorites:(id)sender; - (IBAction)importFavoritesByAdding:(id)sender; 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,30 +185,52 @@ #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 */ @@ -226,32 +275,6 @@ [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"removeAllFavorites"]; } -/** - * 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. */ @@ -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 diff --git a/Source/SPTableView.m b/Source/SPTableView.m index f52b44f5..9a0db4fc 100644 --- a/Source/SPTableView.m +++ b/Source/SPTableView.m @@ -48,7 +48,7 @@ return nil; } if([[[[self delegate] class] description] isEqualToString:@"SPQueryFavoriteManager"]) { - if([NSArrayObjectAtIndex([[self delegate] valueForKeyPath:@"favoriteProperties"], row) intValue] == SP_FAVORITETYPE_HEADER) + if([NSArrayObjectAtIndex([[self delegate] valueForKeyPath:@"favorites"], row) objectForKey:@"headerOfFileURL"]) return nil; } -- cgit v1.2.3