Skip to content

Commit 8ceb5be

Browse files
committed
Names of newly created favorites could be overwritten with the host name under specific conditions (#3015)
Also minified some duplicate code along the way
1 parent 75781e0 commit 8ceb5be

File tree

1 file changed

+23
-60
lines changed

1 file changed

+23
-60
lines changed

Source/SPConnectionController.m

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,14 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
13181318
}
13191319
theFavorite = [self selectedFavorite];
13201320
}
1321+
1322+
void (^_setOrRemoveKey)(NSString *, id) = ^(NSString *key, id value) {
1323+
if (value) {
1324+
[theFavorite setObject:value forKey:key];
1325+
} else {
1326+
[theFavorite removeObjectForKey:key];
1327+
}
1328+
};
13211329

13221330
// Set the name - either taking the provided name, or generating one.
13231331
if ([[self name] length]) {
@@ -1332,75 +1340,27 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
13321340

13331341
// Set standard details for the connection
13341342
[theFavorite setObject:[NSNumber numberWithInteger:[self type]] forKey:SPFavoriteTypeKey];
1335-
if ([self host]) {
1336-
[theFavorite setObject:[self host] forKey:SPFavoriteHostKey];
1337-
} else {
1338-
[theFavorite removeObjectForKey:SPFavoriteHostKey];
1339-
}
1340-
if ([self socket]) {
1341-
[theFavorite setObject:[self socket] forKey:SPFavoriteSocketKey];
1342-
} else {
1343-
[theFavorite removeObjectForKey:SPFavoriteSocketKey];
1344-
}
1345-
if ([self user]) {
1346-
[theFavorite setObject:[self user] forKey:SPFavoriteUserKey];
1347-
} else {
1348-
[theFavorite removeObjectForKey:SPFavoriteUserKey];
1349-
}
1350-
if ([self port]) {
1351-
[theFavorite setObject:[self port] forKey:SPFavoritePortKey];
1352-
} else {
1353-
[theFavorite removeObjectForKey:SPFavoritePortKey];
1354-
}
1355-
if ([self database]) {
1356-
[theFavorite setObject:[self database] forKey:SPFavoriteDatabaseKey];
1357-
} else {
1358-
[theFavorite removeObjectForKey:SPFavoriteDatabaseKey];
1359-
}
1343+
_setOrRemoveKey(SPFavoriteHostKey, [self host]);
1344+
_setOrRemoveKey(SPFavoriteSocketKey, [self socket]);
1345+
_setOrRemoveKey(SPFavoriteUserKey, [self user]);
1346+
_setOrRemoveKey(SPFavoritePortKey, [self port]);
1347+
_setOrRemoveKey(SPFavoriteDatabaseKey, [self database]);
13601348
[theFavorite setObject:[NSNumber numberWithInteger:[self colorIndex]] forKey:SPFavoriteColorIndexKey];
13611349
// SSL details
13621350
[theFavorite setObject:[NSNumber numberWithInteger:[self useSSL]] forKey:SPFavoriteUseSSLKey];
13631351
[theFavorite setObject:[NSNumber numberWithInteger:[self sslKeyFileLocationEnabled]] forKey:SPFavoriteSSLKeyFileLocationEnabledKey];
1364-
if ([self sslKeyFileLocation]) {
1365-
[theFavorite setObject:[self sslKeyFileLocation] forKey:SPFavoriteSSLKeyFileLocationKey];
1366-
} else {
1367-
[theFavorite removeObjectForKey:SPFavoriteSSLKeyFileLocationKey];
1368-
}
1352+
_setOrRemoveKey(SPFavoriteSSLKeyFileLocationKey, [self sslKeyFileLocation]);
13691353
[theFavorite setObject:[NSNumber numberWithInteger:[self sslCertificateFileLocationEnabled]] forKey:SPFavoriteSSLCertificateFileLocationEnabledKey];
1370-
if ([self sslCertificateFileLocation]) {
1371-
[theFavorite setObject:[self sslCertificateFileLocation] forKey:SPFavoriteSSLCertificateFileLocationKey];
1372-
} else {
1373-
[theFavorite removeObjectForKey:SPFavoriteSSLCertificateFileLocationKey];
1374-
}
1354+
_setOrRemoveKey(SPFavoriteSSLCertificateFileLocationKey, [self sslCertificateFileLocation]);
13751355
[theFavorite setObject:[NSNumber numberWithInteger:[self sslCACertFileLocationEnabled]] forKey:SPFavoriteSSLCACertFileLocationEnabledKey];
1376-
if ([self sslCACertFileLocation]) {
1377-
[theFavorite setObject:[self sslCACertFileLocation] forKey:SPFavoriteSSLCACertFileLocationKey];
1378-
} else {
1379-
[theFavorite removeObjectForKey:SPFavoriteSSLCACertFileLocationKey];
1380-
}
1356+
_setOrRemoveKey(SPFavoriteSSLCACertFileLocationKey, [self sslCACertFileLocation]);
13811357

13821358
// SSH details
1383-
if ([self sshHost]) {
1384-
[theFavorite setObject:[self sshHost] forKey:SPFavoriteSSHHostKey];
1385-
} else {
1386-
[theFavorite removeObjectForKey:SPFavoriteSSHHostKey];
1387-
}
1388-
if ([self sshUser]) {
1389-
[theFavorite setObject:[self sshUser] forKey:SPFavoriteSSHUserKey];
1390-
} else {
1391-
[theFavorite removeObjectForKey:SPFavoriteSSHUserKey];
1392-
}
1393-
if ([self sshPort]) {
1394-
[theFavorite setObject:[self sshPort] forKey:SPFavoriteSSHPortKey];
1395-
} else {
1396-
[theFavorite removeObjectForKey:SPFavoriteSSHPortKey];
1397-
}
1359+
_setOrRemoveKey(SPFavoriteSSHHostKey, [self sshHost]);
1360+
_setOrRemoveKey(SPFavoriteSSHUserKey, [self sshUser]);
1361+
_setOrRemoveKey(SPFavoriteSSHPortKey, [self sshPort]);
13981362
[theFavorite setObject:[NSNumber numberWithInteger:[self sshKeyLocationEnabled]] forKey:SPFavoriteSSHKeyLocationEnabledKey];
1399-
if ([self sshKeyLocation]) {
1400-
[theFavorite setObject:[self sshKeyLocation] forKey:SPFavoriteSSHKeyLocationKey];
1401-
} else {
1402-
[theFavorite removeObjectForKey:SPFavoriteSSHKeyLocationKey];
1403-
}
1363+
_setOrRemoveKey(SPFavoriteSSHKeyLocationKey, [self sshKeyLocation]);
14041364

14051365

14061366
/**
@@ -1535,6 +1495,9 @@ - (void)_saveCurrentDetailsCreatingNewFavorite:(BOOL)createNewFavorite validateD
15351495
[self _sortFavorites];
15361496
[self _scrollToSelectedNode];
15371497
}
1498+
1499+
// after saving the favorite, the name is never autogenerated (ie. overridable), regardless of the value (#3015)
1500+
favoriteNameFieldWasAutogenerated = NO;
15381501

15391502
[[NSNotificationCenter defaultCenter] postNotificationName:SPConnectionFavoritesChangedNotification object:self];
15401503
#endif

0 commit comments

Comments
 (0)