aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPConnectionController.m
diff options
context:
space:
mode:
authorstuconnolly <stuart02@gmail.com>2011-01-26 21:44:29 +0000
committerstuconnolly <stuart02@gmail.com>2011-01-26 21:44:29 +0000
commitfa7cff57548edc51420693e6909fe2adb3c18951 (patch)
tree71840428efea0962ae1025f18aa50573b9691890 /Source/SPConnectionController.m
parent9533093c9d107acaaf26110a2ae43f484a101419 (diff)
downloadsequelpro-fa7cff57548edc51420693e6909fe2adb3c18951.tar.gz
sequelpro-fa7cff57548edc51420693e6909fe2adb3c18951.tar.bz2
sequelpro-fa7cff57548edc51420693e6909fe2adb3c18951.zip
Fix and enable drag and drop in the connection favorites outline view.
Diffstat (limited to 'Source/SPConnectionController.m')
-rw-r--r--Source/SPConnectionController.m67
1 files changed, 59 insertions, 8 deletions
diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m
index 2bad361f..c12f1ac4 100644
--- a/Source/SPConnectionController.m
+++ b/Source/SPConnectionController.m
@@ -46,6 +46,7 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist";
- (BOOL)_checkHost;
- (void)_sortFavorites;
+- (void)_sortTreeNode:(SPTreeNode *)node usingKey:(NSString *)key;
- (void)_favoriteTypeDidChange;
- (void)_reloadFavoritesViewData;
- (void)_restoreConnectionInterface;
@@ -56,6 +57,8 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist";
- (void)_updateFavoritePasswordsFromField:(NSControl *)control;
+static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key);
+
@end
@implementation SPConnectionController
@@ -1177,19 +1180,67 @@ static const NSString *SPExportFavoritesFilename = @"SequelProFavorites.plist";
sortKey = SPFavoriteTypeKey;
break;
}
+
+ [self _sortTreeNode:[[favoritesRoot childNodes] objectAtIndex:0] usingKey:sortKey];
+
+ [favoritesController saveFavorites];
- // First sort the contents of all groups
- for (SPTreeNode *node in [[[favoritesRoot childNodes] objectAtIndex:0] groupChildren])
+ [self _reloadFavoritesViewData];
+}
+
+/**
+ * Sorts the supplied tree node using the supplied sort key.
+ *
+ * @param node The tree node to sort
+ * @param key The sort key to sort by
+ */
+- (void)_sortTreeNode:(SPTreeNode *)node usingKey:(NSString *)key
+{
+ NSMutableArray *nodes = [[node mutableChildNodes] mutableCopy];
+
+ for (SPTreeNode *node in nodes)
{
- [[node mutableChildNodes] sortUsingSelector:@selector(compare:)];
+ if ([node isGroup]) {
+ [self _sortTreeNode:node usingKey:key];
+ }
}
- // Secondly sort the root's leaf nodes
- [[[[favoritesRoot childNodes] objectAtIndex:0] childLeafs] sortUsingSelector:@selector(compare:)];
+ NSMutableArray *groupNodes = [[NSMutableArray alloc] init];
- [favoritesController saveFavorites];
-
- [self _reloadFavoritesViewData];
+ for (node in nodes)
+ {
+ if ([node isGroup]) {
+ [groupNodes addObject:node];
+ [nodes removeObject:node];
+ }
+ }
+
+ [nodes sortUsingFunction:compareFavoritesUsingKey context:key];
+
+ [nodes addObjectsFromArray:groupNodes];
+
+ [[node mutableChildNodes] setArray:nodes];
+
+ [groupNodes release];
+}
+
+/**
+ * Sort function used by NSMutableArray's sortUsingFunction:
+ *
+ * @param favorite1 The first of the favorites to compare (and determine sort order)
+ * @param favorite2 The second of the favorites to compare
+ * @param key The sort key to perform the comparison by
+ *
+ * @return An integer (NSComparisonResult) indicating the order of the comparison
+ */
+static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, void *key)
+{
+ NSString *dictKey = (NSString *)key;
+
+ id value1 = [[(SPFavoriteNode *)[(SPTreeNode *)favorite1 representedObject] nodeFavorite] objectForKey:dictKey];
+ id value2 = [[(SPFavoriteNode *)[(SPTreeNode *)favorite2 representedObject] nodeFavorite] objectForKey:dictKey];
+
+ return [value1 compare:value2];
}
/**