Put NSAutoreleasePool usage around other distributed notification observer methods
[adiumx.git] / Source / AIAlphabeticalSort.m
blobcecdf604061b3ed761e081bcda8c1b09ff158940
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
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.
8  * 
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.
12  * 
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.
15  */
17 #import "AIAlphabeticalSort.h"
18 #import <Adium/AIContactControllerProtocol.h>
19 #import <Adium/AIPreferenceControllerProtocol.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;
30 /*!
31  * @class AIAlphabeticalSort
32  * @brief Sort controller to sort contacts and groups alphabetically.
33  */
34 @implementation AIAlphabeticalSort
36 /*!
37  * @brief Did become active first time
38  *
39  * Called only once; gives the sort controller an opportunity to set defaults and load preferences lazily.
40  */
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];
47         
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];
55 /*!
56  * @brief Non-localized identifier
57  */
58 - (NSString *)identifier{
59     return @"Alphabetical";
62 /*!
63  * @brief Localized display name
64  */
65 - (NSString *)displayName{
66     return AILocalizedString(@"Sort Contacts Alphabetically",nil);
69 /*!
70  * @brief Status keys which, when changed, should trigger a resort
71  */
72 - (NSSet *)statusKeysRequiringResort{
73         return nil;
76 /*!
77  * @brief Attribute keys which, when changed, should trigger a resort
78  */
79 - (NSSet *)attributeKeysRequiringResort{
80         return [NSSet setWithObject:@"Display Name"];
83 #pragma mark Configuration
84 /*!
85  * @brief Window title when configuring the sort
86  *
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.
89  */
90 - (NSString *)configureSortWindowTitle{
91         return AILocalizedString(@"Configure Alphabetical Sort",nil);   
94 /*!
95  * @brief Nib name for configuration
96  */
97 - (NSString *)configureNibName{
98         return @"AlphabeticalSortConfiguration";
102  * @brief View did load
103  */
104 - (void)viewDidLoad{
105         [checkBox_sortByLastName setLocalizedString:AILocalizedString(@"Sort contacts by last name",nil)];
106         [checkBox_sortGroups setLocalizedString:AILocalizedString(@"Sort groups alphabetically",nil)];
107         
108         [checkBox_sortByLastName setState:sortByLastName];
109         [checkBox_sortGroups setState:sortGroups];
113  * @brief Preference changed
115  * Sort controllers should live update as preferences change.
116  */
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];                        
129         }
130         
131         [[adium contactController] sortContactList];
134 #pragma mark Sorting
136  * @brief Alphabetical sort
137  */
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
141         if (!groups) {
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];
148                         
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];
153                         }
154                         
155                         return (returnValue);
156                 } else {
157                         return [[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]];
158                 }
160         } else {
161                 //If sorting groups, do a caseInsesitiveCompare; otherwise, keep groups in manual order
162                 if (sortGroups) {
163                         return [[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]];
164                 } else if ([objectA orderIndex] > [objectB orderIndex]) {
165                         return NSOrderedDescending;
166                 } else {
167                         return NSOrderedAscending;
168                 }
169         }
173  * @brief Sort function
174  */
175 - (sortfunc)sortFunction{
176         return &alphabeticalSort;
179 @end