From 8ceb5be8c231395cdc72b0646dd494c0c5b4e233 Mon Sep 17 00:00:00 2001 From: Max Lohrmann Date: Tue, 3 Apr 2018 23:13:31 +0200 Subject: Names of newly created favorites could be overwritten with the host name under specific conditions (#3015) Also minified some duplicate code along the way --- Source/SPConnectionController.m | 83 ++++++++++++----------------------------- 1 file changed, 23 insertions(+), 60 deletions(-) (limited to 'Source') diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 921d4312..407c22e1 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -1318,6 +1318,14 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, } theFavorite = [self selectedFavorite]; } + + void (^_setOrRemoveKey)(NSString *, id) = ^(NSString *key, id value) { + if (value) { + [theFavorite setObject:value forKey:key]; + } else { + [theFavorite removeObjectForKey:key]; + } + }; // Set the name - either taking the provided name, or generating one. if ([[self name] length]) { @@ -1332,75 +1340,27 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, // Set standard details for the connection [theFavorite setObject:[NSNumber numberWithInteger:[self type]] forKey:SPFavoriteTypeKey]; - if ([self host]) { - [theFavorite setObject:[self host] forKey:SPFavoriteHostKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteHostKey]; - } - if ([self socket]) { - [theFavorite setObject:[self socket] forKey:SPFavoriteSocketKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSocketKey]; - } - if ([self user]) { - [theFavorite setObject:[self user] forKey:SPFavoriteUserKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteUserKey]; - } - if ([self port]) { - [theFavorite setObject:[self port] forKey:SPFavoritePortKey]; - } else { - [theFavorite removeObjectForKey:SPFavoritePortKey]; - } - if ([self database]) { - [theFavorite setObject:[self database] forKey:SPFavoriteDatabaseKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteDatabaseKey]; - } + _setOrRemoveKey(SPFavoriteHostKey, [self host]); + _setOrRemoveKey(SPFavoriteSocketKey, [self socket]); + _setOrRemoveKey(SPFavoriteUserKey, [self user]); + _setOrRemoveKey(SPFavoritePortKey, [self port]); + _setOrRemoveKey(SPFavoriteDatabaseKey, [self database]); [theFavorite setObject:[NSNumber numberWithInteger:[self colorIndex]] forKey:SPFavoriteColorIndexKey]; // SSL details [theFavorite setObject:[NSNumber numberWithInteger:[self useSSL]] forKey:SPFavoriteUseSSLKey]; [theFavorite setObject:[NSNumber numberWithInteger:[self sslKeyFileLocationEnabled]] forKey:SPFavoriteSSLKeyFileLocationEnabledKey]; - if ([self sslKeyFileLocation]) { - [theFavorite setObject:[self sslKeyFileLocation] forKey:SPFavoriteSSLKeyFileLocationKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSLKeyFileLocationKey]; - } + _setOrRemoveKey(SPFavoriteSSLKeyFileLocationKey, [self sslKeyFileLocation]); [theFavorite setObject:[NSNumber numberWithInteger:[self sslCertificateFileLocationEnabled]] forKey:SPFavoriteSSLCertificateFileLocationEnabledKey]; - if ([self sslCertificateFileLocation]) { - [theFavorite setObject:[self sslCertificateFileLocation] forKey:SPFavoriteSSLCertificateFileLocationKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSLCertificateFileLocationKey]; - } + _setOrRemoveKey(SPFavoriteSSLCertificateFileLocationKey, [self sslCertificateFileLocation]); [theFavorite setObject:[NSNumber numberWithInteger:[self sslCACertFileLocationEnabled]] forKey:SPFavoriteSSLCACertFileLocationEnabledKey]; - if ([self sslCACertFileLocation]) { - [theFavorite setObject:[self sslCACertFileLocation] forKey:SPFavoriteSSLCACertFileLocationKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSLCACertFileLocationKey]; - } + _setOrRemoveKey(SPFavoriteSSLCACertFileLocationKey, [self sslCACertFileLocation]); // SSH details - if ([self sshHost]) { - [theFavorite setObject:[self sshHost] forKey:SPFavoriteSSHHostKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSHHostKey]; - } - if ([self sshUser]) { - [theFavorite setObject:[self sshUser] forKey:SPFavoriteSSHUserKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSHUserKey]; - } - if ([self sshPort]) { - [theFavorite setObject:[self sshPort] forKey:SPFavoriteSSHPortKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSHPortKey]; - } + _setOrRemoveKey(SPFavoriteSSHHostKey, [self sshHost]); + _setOrRemoveKey(SPFavoriteSSHUserKey, [self sshUser]); + _setOrRemoveKey(SPFavoriteSSHPortKey, [self sshPort]); [theFavorite setObject:[NSNumber numberWithInteger:[self sshKeyLocationEnabled]] forKey:SPFavoriteSSHKeyLocationEnabledKey]; - if ([self sshKeyLocation]) { - [theFavorite setObject:[self sshKeyLocation] forKey:SPFavoriteSSHKeyLocationKey]; - } else { - [theFavorite removeObjectForKey:SPFavoriteSSHKeyLocationKey]; - } + _setOrRemoveKey(SPFavoriteSSHKeyLocationKey, [self sshKeyLocation]); /** @@ -1535,6 +1495,9 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, [self _sortFavorites]; [self _scrollToSelectedNode]; } + + // after saving the favorite, the name is never autogenerated (ie. overridable), regardless of the value (#3015) + favoriteNameFieldWasAutogenerated = NO; [[NSNotificationCenter defaultCenter] postNotificationName:SPConnectionFavoritesChangedNotification object:self]; #endif -- cgit v1.2.3