diff options
-rw-r--r-- | Source/SPQueryConsole.h | 3 | ||||
-rw-r--r-- | Source/SPQueryConsole.m | 54 | ||||
-rw-r--r-- | Source/TableDocument.h | 4 | ||||
-rw-r--r-- | Source/TableDocument.m | 65 |
4 files changed, 82 insertions, 44 deletions
diff --git a/Source/SPQueryConsole.h b/Source/SPQueryConsole.h index e5cb1156..81ac07bd 100644 --- a/Source/SPQueryConsole.h +++ b/Source/SPQueryConsole.h @@ -65,7 +65,8 @@ - (void)showMessageInConsole:(NSString *)message; - (void)showErrorInConsole:(NSString *)error; -- (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSDictionary *)contextInfo; +- (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSMutableDictionary *)contextInfo; +- (void)removeRegisteredDocumentWithFileURL:(NSURL *)fileURL; - (void)addFavorite:(NSString *)favorite forFileURL:(NSURL *)fileURL; - (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL; - (void)favoritesForFileURL:(NSURL *)fileURL; diff --git a/Source/SPQueryConsole.m b/Source/SPQueryConsole.m index 663855fe..bcea3e32 100644 --- a/Source/SPQueryConsole.m +++ b/Source/SPQueryConsole.m @@ -328,13 +328,14 @@ static SPQueryConsole *sharedQueryConsole = nil; #pragma mark - #pragma mark DocumentsController -- (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSDictionary *)contextInfo +- (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSMutableDictionary *)contextInfo { // Register a new untiled document and return its URL if(fileURL == nil) { NSURL *new = [NSURL URLWithString:[[NSString stringWithFormat:@"Untitled %d", untitledDocumentCounter] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; untitledDocumentCounter++; + if(![favoritesContainer objectForKey:[new absoluteString]]) [favoritesContainer setObject:[NSMutableArray array] forKey:[new absoluteString]]; if(![historyContainer objectForKey:[new absoluteString]]) @@ -342,25 +343,48 @@ static SPQueryConsole *sharedQueryConsole = nil; return new; } - - // Register a spf file - // if(![favoritesContainer objectForKey:[fileURL absoluteString]]) { - // if(contextInfo != nil && [contextInfo objectForKey:@"queryFavorites"] != nil) - // [favoritesContainer setObject:[[contextInfo objectForKey:@"queryFavorites"] mutableCopy] forKey:[fileURL absoluteString]]; - // else - // [favoritesContainer setObject:[NSMutableArray array] forKey:[fileURL absoluteString]]; - // } - // if(![historyContainer objectForKey:[fileURL absoluteString]]) { - // if(contextInfo != nil && [contextInfo objectForKey:@"queryHistory"] != nil) - // [historyContainer setObject:[[contextInfo objectForKey:@"queryHistory"] mutableCopy] forKey:[fileURL absoluteString]]; - // else - // [historyContainer setObject:[NSMutableArray array] forKey:[fileURL absoluteString]]; - // } + + // Register a spf file to manage all query favorites and query history items + // file path based in a dictionary whereby the key represents the file name. + if(![favoritesContainer objectForKey:[fileURL absoluteString]]) { + if(contextInfo != nil && [contextInfo objectForKey:@"queryFavorites"] && [[contextInfo objectForKey:@"queryFavorites"] count]) { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [arr addObjectsFromArray:[contextInfo objectForKey:@"queryFavorites"]]; + [favoritesContainer setObject:arr forKey:[fileURL absoluteString]]; + [arr release]; + } else { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [favoritesContainer setObject:arr forKey:[fileURL absoluteString]]; + [arr release]; + } + } + if(![historyContainer objectForKey:[fileURL absoluteString]]) { + if(contextInfo != nil && [contextInfo objectForKey:@"queryHistory"] && [[contextInfo objectForKey:@"queryHistory"] count]) { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [arr addObjectsFromArray:[contextInfo objectForKey:@"queryHistory"]]; + [historyContainer setObject:arr forKey:[fileURL absoluteString]]; + [arr release]; + } else { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [historyContainer setObject:arr forKey:[fileURL absoluteString]]; + [arr release]; + } + } return fileURL; } +- (void)removeRegisteredDocumentWithFileURL:(NSURL *)fileURL +{ + + if([favoritesContainer objectForKey:[fileURL absoluteString]]) + [favoritesContainer removeObjectForKey:[fileURL absoluteString]]; + if([historyContainer objectForKey:[fileURL absoluteString]]) + [historyContainer removeObjectForKey:[fileURL absoluteString]]; + +} + - (void)addFavorite:(NSString *)favorite forFileURL:(NSURL *)fileURL { diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 09401cea..868b5dcd 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -143,7 +143,7 @@ enum sp_current_query_mode NSString *queryEditorInitString; NSDictionary *spfSession; - NSDictionary *spfPreferences; + NSMutableDictionary *spfPreferences; NSMutableDictionary *spfDocData; } @@ -204,7 +204,7 @@ enum sp_current_query_mode - (void)refreshCurrentDatabase; - (void)saveConnectionPanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo; - (IBAction)validateSaveConnectionAccessory:(id)sender; -- (void)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInBackground onlyPreferences:(BOOL)saveOnlyPreferences; +- (BOOL)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInBackground onlyPreferences:(BOOL)saveOnlyPreferences; - (IBAction)closePasswordSheet:(id)sender; // Getter methods diff --git a/Source/TableDocument.m b/Source/TableDocument.m index c2b7e45f..8d0ad62b 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -87,7 +87,7 @@ queryEditorInitString = nil; spfSession = nil; - spfPreferences = nil; + spfPreferences = [[NSMutableDictionary alloc] init]; spfDocData = [[NSMutableDictionary alloc] init]; } @@ -411,13 +411,10 @@ [self setFileURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; - if([spf objectForKey:@"queryFavorites"] || [spf objectForKey:@"queryHistory"]) { - spfPreferences = [NSDictionary dictionaryWithObjectsAndKeys: - [spf objectForKey:@"queryFavorites"], @"queryFavorites", - [spf objectForKey:@"queryHistory"], @"queryHistory", - nil]; - } - + if([spf objectForKey:@"queryFavorites"]) + [spfPreferences setObject:[spf objectForKey:@"queryFavorites"] forKey:@"queryFavorites"]; + if([spf objectForKey:@"queryHistory"]) + [spfPreferences setObject:[spf objectForKey:@"queryHistory"] forKey:@"queryHistory"]; [spfDocData setObject:[NSNumber numberWithBool:NO] forKey:@"auto_connect"]; if([spf objectForKey:@"auto_connect"] && [[spf valueForKey:@"auto_connect"] boolValue]) { @@ -600,9 +597,11 @@ else [tableWindow makeFirstResponder:[tablesListInstance valueForKeyPath:@"tablesListView"]]; - NSURL *anURL = [[SPQueryConsole sharedQueryConsole] registerDocumentWithFileURL:[self fileURL] andContextInfo:spfPreferences]; + NSURL *anURL = [[SPQueryConsole sharedQueryConsole] registerDocumentWithFileURL:[self fileURL] andContextInfo:[spfPreferences retain]]; [self setFileURL:anURL]; + [spfPreferences release]; + if(spfSession != nil) [self restoreSession]; @@ -1924,9 +1923,12 @@ */ - (void)applicationWillTerminate:(NSNotification *)notification { - // Auto-save spf file based connection + // Auto-save preferences to spf file based connection if([self fileURL] && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) - [self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]; + if(![self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]) { + NSLog(@"Preference data for file ‘%@’ could not be saved.", [[[self fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]); + NSBeep(); + } [tablesListInstance selectionShouldChangeInTableView:nil]; } @@ -1975,9 +1977,9 @@ // Save As… or Save } else if([sender tag] == 1005 || [sender tag] == 1004) { - // If Save was invoked check for fileURL and save the spf file without save panel + // If Save was invoked check for fileURL and Untitled docs and save the spf file without save panel // otherwise ask for file name - if([sender tag] == 1004 && [[[self fileURL] absoluteString] length]) { + if([sender tag] == 1004 && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) { [self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:NO]; return; } @@ -2096,11 +2098,11 @@ } } -- (void)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInBackground onlyPreferences:(BOOL)saveOnlyPreferences +- (BOOL)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInBackground onlyPreferences:(BOOL)saveOnlyPreferences { // Do not save if no connection is/was available if(saveInBackground && ([self mySQLVersion] == nil || ![[self mySQLVersion] length])) - return; + return NO; NSMutableDictionary *spfDocData_temp = [NSMutableDictionary dictionary]; @@ -2128,7 +2130,7 @@ // Check for save file URL // TODO maybe alert ? - if(![[[self fileURL] absoluteString] length]) return; + if(![[[self fileURL] absoluteString] length] && ![[[self fileURL] absoluteString] hasPrefix:@"/"]) return NO; NSError *readError = nil; NSString *convError = nil; @@ -2145,19 +2147,19 @@ defaultButton:NSLocalizedString(@"OK", @"OK button") alternateButton:nil otherButton:nil - informativeTextWithFormat:NSLocalizedString(@"Connection data file couldn't be read.", @"error while reading connection data file")]; + informativeTextWithFormat:NSLocalizedString(@"Connection data file couldn't be read. Please try to save the document under a different name.", @"message error while reading connection data file and suggesting to save it under a differnet name")]; [alert setAlertStyle:NSCriticalAlertStyle]; [alert runModal]; - [self close]; - return; + // [self close]; + return NO; } // For dispatching later if(![[spf objectForKey:@"format"] isEqualToString:@"connection"]) { NSLog(@"SPF file format is not 'connection'."); - [self close]; - return; + // [self close]; + return NO; } // Update the keys @@ -2179,7 +2181,7 @@ [alert setAlertStyle:NSCriticalAlertStyle]; [alert runModal]; - return; + return NO; } NSError *error = nil; @@ -2187,11 +2189,12 @@ if(error != nil){ NSAlert *errorAlert = [NSAlert alertWithError:error]; [errorAlert runModal]; + return NO; } [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[self fileURL]]; - return; + return YES; } @@ -2346,7 +2349,7 @@ [alert setAlertStyle:NSCriticalAlertStyle]; [alert runModal]; - return; + return NO; } NSError *error = nil; @@ -2354,6 +2357,7 @@ if(error != nil){ NSAlert *errorAlert = [NSAlert alertWithError:error]; [errorAlert runModal]; + return NO; } // TODO take favs and history frm untitle or old file name with me @@ -2366,6 +2370,8 @@ // Store doc data permanently [spfDocData removeAllObjects]; [spfDocData addEntriesFromDictionary:spfDocData_temp]; + + return YES; } @@ -2966,8 +2972,15 @@ return NO; } else { // Auto-save spf file based connection - if([self fileURL] && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) - [self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]; + if([self fileURL] && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) { + BOOL isSaved = [self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]; + if(isSaved) + [[SPQueryConsole sharedQueryConsole] removeRegisteredDocumentWithFileURL:[self fileURL]]; + return isSaved; + } else if([self fileURL] && [[[self fileURL] absoluteString] length] && ![[[self fileURL] absoluteString] hasPrefix:@"/"]) { + [[SPQueryConsole sharedQueryConsole] removeRegisteredDocumentWithFileURL:[self fileURL]]; + return YES; + } } return YES; } |