Permalink
Browse files

Work around an exception that would occur when changing Query Favorit…

…es while an unconnected connection tab existed (#2266)
  • Loading branch information...
dmoagx committed Jan 17, 2018
1 parent c31bc03 commit 74b18eee1821ec8ec5bd472cd5ddb80eb865c1ed
@@ -451,6 +451,7 @@ extern NSString *SPSelectionDetailTypePrimaryKeyed;
extern NSString *SPSSHEnableMuxingPreference;
extern NSString *SPSSHClientPath;
extern NSString *SPSSLCipherListKey;
extern NSString *SPQueryFavoritesHaveBeenUpdatedNotification;

// URLs
extern NSString *SPDonationsURL;
@@ -248,6 +248,7 @@
NSString *SPSSHEnableMuxingPreference = @"SSHMultiplexingEnabled";
NSString *SPSSHClientPath = @"SSHClientPath";
NSString *SPSSLCipherListKey = @"SSLCipherList";
NSString *SPQueryFavoritesHaveBeenUpdatedNotification = @"QueryFavoritesHaveBeenUpdatedNotification";

// URLs
NSString *SPDonationsURL = @"http://www.sequelpro.com/donate/";
@@ -71,6 +71,7 @@ @interface SPCustomQuery (PrivateAPI)
- (id)_resultDataItemAtRow:(NSInteger)row columnIndex:(NSUInteger)column preserveNULLs:(BOOL)preserveNULLs asPreview:(BOOL)asPreview;
+ (NSString *)linkToHelpTopic:(NSString *)aTopic;
- (void)documentWillClose:(NSNotification *)notification;
- (void)queryFavoritesHaveBeenUpdated:(NSNotification *)notification;

@end

@@ -3391,38 +3392,38 @@ - (void)historyItemsHaveBeenUpdated:(id)manager
/**
* Called by the query favorites manager whenever the query favorites have been updated.
*/
- (void)queryFavoritesHaveBeenUpdated:(id)manager
- (void)queryFavoritesHaveBeenUpdated:(NSNotification *)notification
{
NSMenuItem *headerMenuItem;
NSURL *fileURL = [tableDocumentInstance fileURL];

// Warning: This method may be called before any connection has been made in the current tab (triggered by another tab)!
// There doesn't seem to be a real indicator for this, but fileURL is the closest thing plus we need it below (#2266)
if(!fileURL) return;

NSMenu *menu = [queryFavoritesButton menu];

// Remove all favorites beginning from the end
while([queryFavoritesButton numberOfItems] > 7)
[queryFavoritesButton removeItemAtIndex:[queryFavoritesButton numberOfItems]-1];

// Build document-based list
NSString *tblDocName = [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent];
if(!tblDocName) {
//NSMenuItem will not accept nil as title
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"Document name conversion resulted in nil string!? tableDocumentInstance=%@ fileURL=%@",tableDocumentInstance,[tableDocumentInstance fileURL]]
userInfo:nil];
}
headerMenuItem = [[NSMenuItem alloc] initWithTitle:tblDocName action:NULL keyEquivalent:@""];
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
[headerMenuItem setToolTip:[NSString stringWithFormat:NSLocalizedString(@"%@’ based favorites",@"Query Favorites : List : Section Heading : current connection document : tooltip (arg is the name of the spf file)"), tblDocName]];
[headerMenuItem setIndentationLevel:0];
[menu addItem:headerMenuItem];
[headerMenuItem release];
for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) {
NSString *tblDocName = [[[fileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent];
{
NSMenuItem *headerMenuItem = [[NSMenuItem alloc] initWithTitle:tblDocName action:NULL keyEquivalent:@""];
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
[headerMenuItem setToolTip:[NSString stringWithFormat:NSLocalizedString(@"%@’ based favorites",@"Query Favorites : List : Section Heading : current connection document : tooltip (arg is the name of the spf file)"), tblDocName]];
[headerMenuItem setIndentationLevel:0];
[menu addItem:headerMenuItem];
[headerMenuItem release];
}
for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:fileURL]) {
if (![favorite isKindOfClass:[NSDictionary class]] || ![favorite objectForKey:@"name"]) continue;
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paraStyle setTabStops:@[]];
[paraStyle addTabStop:[[[NSTextTab alloc] initWithType:NSRightTabStopType location:190.0f] autorelease]];
NSDictionary *attributes = @{NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : [NSFont systemFontOfSize:11]};
NSAttributedString *titleString = [[[NSAttributedString alloc]
initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
attributes:attributes] autorelease];
NSAttributedString *titleString = [[[NSAttributedString alloc] initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
attributes:attributes] autorelease];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
if ([favorite objectForKey:@"query"]) {
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
@@ -3434,21 +3435,22 @@ - (void)queryFavoritesHaveBeenUpdated:(id)manager
}

// Build global list
headerMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Global",@"Query Favorites : List : Section Heading : global query favorites") action:NULL keyEquivalent:@""];
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
[headerMenuItem setToolTip:NSLocalizedString(@"Globally stored favorites",@"Query Favorites : List : Section Heading : global : tooltip")];
[headerMenuItem setIndentationLevel:0];
[menu addItem:headerMenuItem];
[headerMenuItem release];
{
NSMenuItem *headerMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Global",@"Query Favorites : List : Section Heading : global query favorites") action:NULL keyEquivalent:@""];
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
[headerMenuItem setToolTip:NSLocalizedString(@"Globally stored favorites",@"Query Favorites : List : Section Heading : global : tooltip")];
[headerMenuItem setIndentationLevel:0];
[menu addItem:headerMenuItem];
[headerMenuItem release];
}
for (NSDictionary *favorite in [prefs objectForKey:SPQueryFavorites]) {
if (![favorite isKindOfClass:[NSDictionary class]] || ![favorite objectForKey:@"name"]) continue;
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paraStyle setTabStops:@[]];
[paraStyle addTabStop:[[[NSTextTab alloc] initWithType:NSRightTabStopType location:190.0f] autorelease]];
NSDictionary *attributes = @{NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : [NSFont systemFontOfSize:11]};
NSAttributedString *titleString = [[[NSAttributedString alloc]
initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
attributes:attributes] autorelease];
NSAttributedString *titleString = [[[NSAttributedString alloc] initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
attributes:attributes] autorelease];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
if ([favorite objectForKey:@"query"]) {
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
@@ -3991,6 +3993,10 @@ - (void)awakeFromNib
selector:@selector(documentWillClose:)
name:SPDocumentWillCloseNotification
object:tableDocumentInstance];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryFavoritesHaveBeenUpdated:)
name:SPQueryFavoritesHaveBeenUpdatedNotification
object:nil];

#ifndef SP_CODA
[prefs addObserver:self forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:NULL];
@@ -425,8 +425,6 @@ - (void)setConnection:(SPMySQLConnection *)theConnection
#ifndef SP_CODA
// Set the fileURL and init the preferences (query favs, filters, and history) if available for that URL
NSURL *newURL = [[SPQueryController sharedQueryController] registerDocumentWithFileURL:[self fileURL] andContextInfo:spfPreferences];
#warning debug code for #2266
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from queryController=%@ oldFileURL=%@ contextInfo=%@", __func__, [SPQueryController sharedQueryController], [self fileURL], spfPreferences);
[self setFileURL:newURL];

// ...but hide the icon while the document is temporary
@@ -3569,8 +3567,6 @@ - (BOOL)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInB
[[SPQueryController sharedQueryController] registerDocumentWithFileURL:[NSURL fileURLWithPath:fileName] andContextInfo:preferences];

NSURL *newURL = [NSURL fileURLWithPath:fileName];
#warning debug code for #2266
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from fileName=%@", __func__, fileName);
[self setFileURL:newURL];
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:fileName]];

@@ -5132,8 +5128,6 @@ - (BOOL)setStateFromConnectionFile:(NSString *)path

if (![self isSaveInBundle]) {
NSURL *newURL = [NSURL fileURLWithPath:path];
#warning debug code for #2266
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from path=%@", __func__, path);
[self setFileURL:newURL];
}

@@ -32,12 +32,6 @@
@class SPDatabaseDocument;
@class SPSplitView;

@interface NSObject (SPQueryFavoriteManagerDelegate)

- (void)queryFavoritesHaveBeenUpdated:(id)manager;

@end

@interface SPQueryFavoriteManager : NSWindowController <NSOpenSavePanelDelegate>
{
#ifndef SP_CODA /* ivars */
@@ -38,8 +38,6 @@
#import "RegexKitLite.h"
#import "SPTextView.h"
#import "SPSplitView.h"
#import "SPAppController.h"
#import "SPAppleScriptSupport.h"

#define SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[multiple selection]", @"[multiple selection]")
#define SP_NO_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[no selection]", @"[no selection]")
@@ -486,9 +484,7 @@ - (IBAction)closeQueryManagerSheet:(id)sender
[prefs setObject:[self queryFavoritesForFileURL:nil] forKey:SPQueryFavorites];

// Inform all opened documents to update the query favorites list
for(id doc in [SPAppDelegate orderedDocuments])
if([[doc valueForKeyPath:@"customQueryInstance"] respondsToSelector:@selector(queryFavoritesHaveBeenUpdated:)])
[[doc valueForKeyPath:@"customQueryInstance"] queryFavoritesHaveBeenUpdated:self];
[[NSNotificationCenter defaultCenter] postNotificationName:SPQueryFavoritesHaveBeenUpdatedNotification object:self];
}
#endif

0 comments on commit 74b18ee

Please sign in to comment.