2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import "AIAlphabeticalSort.h"
18 #import "AIContactController.h"
19 #import "AIPreferenceController.h"
20 #import <AIUtilities/AIDictionaryAdditions.h>
21 #import <Adium/AIListObject.h>
23 #define KEY_SORT_BY_LAST_NAME @"ABC:Sort by Last Name"
24 #define KEY_SORT_GROUPS @"ABC:Sort Groups"
25 #define ALPHABETICAL_SORT_DEFAULT_PREFS @"AlphabeticalSortDefaults"
27 static BOOL sortGroups;
28 static BOOL sortByLastName;
31 * @class AIAlphabeticalSort
32 * @brief Sort controller to sort contacts and groups alphabetically.
34 @implementation AIAlphabeticalSort
37 * @brief Did become active first time
39 * Called only once; gives the sort controller an opportunity to set defaults and load preferences lazily.
41 - (void)didBecomeActiveFirstTime
43 //Register our default preferences
44 [[adium preferenceController] registerDefaults:[NSDictionary dictionaryNamed:ALPHABETICAL_SORT_DEFAULT_PREFS
45 forClass:[self class]]
46 forGroup:PREF_GROUP_CONTACT_SORTING];
48 //Load our preferences
49 sortGroups = [[[adium preferenceController] preferenceForKey:KEY_SORT_GROUPS
50 group:PREF_GROUP_CONTACT_SORTING] boolValue];
51 sortByLastName = [[[adium preferenceController] preferenceForKey:KEY_SORT_BY_LAST_NAME
52 group:PREF_GROUP_CONTACT_SORTING] boolValue];
56 * @brief Non-localized identifier
58 - (NSString *)identifier{
59 return @"Alphabetical";
63 * @brief Localized display name
65 - (NSString *)displayName{
66 return AILocalizedString(@"Sort Contacts Alphabetically",nil);
70 * @brief Status keys which, when changed, should trigger a resort
72 - (NSSet *)statusKeysRequiringResort{
77 * @brief Attribute keys which, when changed, should trigger a resort
79 - (NSSet *)attributeKeysRequiringResort{
80 return [NSSet setWithObject:@"Display Name"];
83 #pragma mark Configuration
85 * @brief Window title when configuring the sort
87 * Subclasses should provide a title for configuring the sort only if configuration is possible.
88 * @result Localized title. If nil, the menu item will be disabled.
90 - (NSString *)configureSortWindowTitle{
91 return AILocalizedString(@"Configure Alphabetical Sort",nil);
95 * @brief Nib name for configuration
97 - (NSString *)configureNibName{
98 return @"AlphabeticalSortConfiguration";
102 * @brief View did load
105 [checkBox_sortByLastName setLocalizedString:AILocalizedString(@"Sort contacts by last name",nil)];
106 [checkBox_sortGroups setLocalizedString:AILocalizedString(@"Sort groups alphabetically",nil)];
108 [checkBox_sortByLastName setState:sortByLastName];
109 [checkBox_sortGroups setState:sortGroups];
113 * @brief Preference changed
115 * Sort controllers should live update as preferences change.
117 - (IBAction)changePreference:(id)sender
119 if (sender == checkBox_sortGroups) {
120 sortGroups = [sender state];
121 [[adium preferenceController] setPreference:[NSNumber numberWithBool:sortGroups]
122 forKey:KEY_SORT_GROUPS
123 group:PREF_GROUP_CONTACT_SORTING];
124 } else if (sender == checkBox_sortByLastName) {
125 sortByLastName = [sender state];
126 [[adium preferenceController] setPreference:[NSNumber numberWithBool:sortByLastName]
127 forKey:KEY_SORT_BY_LAST_NAME
128 group:PREF_GROUP_CONTACT_SORTING];
131 [[adium contactController] sortContactList];
136 * @brief Alphabetical sort
138 int alphabeticalSort(id objectA, id objectB, BOOL groups)
140 //If we were not passed groups or if we should be sorting groups, sort alphabetically
142 if (sortByLastName) {
143 NSString *space = @" ";
144 NSString *displayNameA = [objectA displayName];
145 NSString *displayNameB = [objectB displayName];
146 NSArray *componentsA = [displayNameA componentsSeparatedByString:space];
147 NSArray *componentsB = [displayNameB componentsSeparatedByString:space];
149 NSComparisonResult returnValue = [[componentsA lastObject] caseInsensitiveCompare:[componentsB lastObject]];
150 //If the last names are the same, compare the whole object, which will amount to sorting these objects by first name
151 if (returnValue == NSOrderedSame) {
152 returnValue = [displayNameA caseInsensitiveCompare:displayNameB];
155 return (returnValue);
157 return [[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]];
161 //If sorting groups, do a caseInsesitiveCompare; otherwise, keep groups in manual order
163 return [[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]];
164 } else if ([objectA orderIndex] > [objectB orderIndex]) {
165 return NSOrderedDescending;
167 return NSOrderedAscending;
173 * @brief Sort function
175 - (sortfunc)sortFunction{
176 return &alphabeticalSort;