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 NSDictionary *prefDict = [[adium preferenceController] preferencesForGroup:PREF_GROUP_CONTACT_SORTING];
50 sortGroups = [[prefDict objectForKey:KEY_SORT_GROUPS] boolValue];
51 sortByLastName = [[prefDict objectForKey:KEY_SORT_BY_LAST_NAME] boolValue];
55 * @brief Non-localized identifier
57 - (NSString *)identifier{
58 return(@"Alphabetical");
62 * @brief Localized display name
64 - (NSString *)displayName{
65 return(AILocalizedString(@"Sort Contacts Alphabetically",nil));
69 * @brief Status keys which, when changed, should trigger a resort
71 - (NSSet *)statusKeysRequiringResort{
76 * @brief Attribute keys which, when changed, should trigger a resort
78 - (NSSet *)attributeKeysRequiringResort{
79 return([NSSet setWithObject:@"Display Name"]);
82 #pragma mark Configuration
84 * @brief Window title when configuring the sort
86 * Subclasses should provide a title for configuring the sort only if configuration is possible.
87 * @result Localized title. If nil, the menu item will be disabled.
89 - (NSString *)configureSortWindowTitle{
90 return(AILocalizedString(@"Configure Alphabetical Sort",nil));
94 * @brief Nib name for configuration
96 - (NSString *)configureNibName{
97 return @"AlphabeticalSortConfiguration";
101 * @brief View did load
104 [checkBox_sortByLastName setLocalizedString:AILocalizedString(@"Sort contacts by last name",nil)];
105 [checkBox_sortGroups setLocalizedString:AILocalizedString(@"Sort groups alphabetically",nil)];
107 [checkBox_sortByLastName setState:sortByLastName];
108 [checkBox_sortGroups setState:sortGroups];
112 * @brief Preference changed
114 * Sort controllers should live update as preferences change.
116 - (IBAction)changePreference:(id)sender
118 if (sender == checkBox_sortGroups){
119 sortGroups = [sender state];
120 [[adium preferenceController] setPreference:[NSNumber numberWithBool:sortGroups]
121 forKey:KEY_SORT_GROUPS
122 group:PREF_GROUP_CONTACT_SORTING];
123 }else if (sender == checkBox_sortByLastName){
124 sortByLastName = [sender state];
125 [[adium preferenceController] setPreference:[NSNumber numberWithBool:sortByLastName]
126 forKey:KEY_SORT_BY_LAST_NAME
127 group:PREF_GROUP_CONTACT_SORTING];
130 [[adium contactController] sortContactList];
135 * @brief Alphabetical sort
137 int alphabeticalSort(id objectA, id objectB, BOOL groups)
139 //If we were not passed groups or if we should be sorting groups, sort alphabetically
142 NSString *space = @" ";
143 NSString *displayNameA = [objectA displayName];
144 NSString *displayNameB = [objectB displayName];
145 NSArray *componentsA = [displayNameA componentsSeparatedByString:space];
146 NSArray *componentsB = [displayNameB componentsSeparatedByString:space];
148 NSComparisonResult returnValue = [[componentsA lastObject] caseInsensitiveCompare:[componentsB lastObject]];
149 //If the last names are the same, compare the whole object, which will amount to sorting these objects by first name
150 if (returnValue == NSOrderedSame){
151 returnValue = [displayNameA caseInsensitiveCompare:displayNameB];
154 return (returnValue);
156 return([[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]]);
159 //If sorting groups, do a caseInsesitiveCompare; otherwise, keep groups in manual order
161 return([[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]]);
162 }else if([objectA orderIndex] > [objectB orderIndex]){
163 return(NSOrderedDescending);
165 return(NSOrderedAscending);
171 * @brief Sort function
173 - (sortfunc)sortFunction{
174 return(&alphabeticalSort);