Using {{{setDefaultPermitDenyForAccount}}} on the gaim thread. Changed an autoreleas...
[adiumx.git] / Source / AIAlphabeticalSort.m
blobd0695f0c3749ec94421e7e135262e03094d6d505
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 "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;
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         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];
54 /*!
55  * @brief Non-localized identifier
56  */
57 - (NSString *)identifier{
58     return(@"Alphabetical");
61 /*!
62  * @brief Localized display name
63  */
64 - (NSString *)displayName{
65     return(AILocalizedString(@"Sort Contacts Alphabetically",nil));
68 /*!
69  * @brief Status keys which, when changed, should trigger a resort
70  */
71 - (NSSet *)statusKeysRequiringResort{
72         return(nil);
75 /*!
76  * @brief Attribute keys which, when changed, should trigger a resort
77  */
78 - (NSSet *)attributeKeysRequiringResort{
79         return([NSSet setWithObject:@"Display Name"]);
82 #pragma mark Configuration
83 /*!
84  * @brief Window title when configuring the sort
85  *
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.
88  */
89 - (NSString *)configureSortWindowTitle{
90         return(AILocalizedString(@"Configure Alphabetical Sort",nil));  
93 /*!
94  * @brief Nib name for configuration
95  */
96 - (NSString *)configureNibName{
97         return @"AlphabeticalSortConfiguration";
101  * @brief View did load
102  */
103 - (void)viewDidLoad{
104         [checkBox_sortByLastName setLocalizedString:AILocalizedString(@"Sort contacts by last name",nil)];
105         [checkBox_sortGroups setLocalizedString:AILocalizedString(@"Sort groups alphabetically",nil)];
106         
107         [checkBox_sortByLastName setState:sortByLastName];
108         [checkBox_sortGroups setState:sortGroups];
112  * @brief Preference changed
114  * Sort controllers should live update as preferences change.
115  */
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];                        
128         }
129         
130         [[adium contactController] sortContactList];
133 #pragma mark Sorting
135  * @brief Alphabetical sort
136  */
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
140         if (!groups){
141                 if (sortByLastName){
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];
147                         
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];
152                         }
153                         
154                         return (returnValue);
155                 }else{
156                         return([[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]]);
157                 }
158         }else{
159                 //If sorting groups, do a caseInsesitiveCompare; otherwise, keep groups in manual order
160                 if (sortGroups){
161                         return([[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]]);
162                 }else if([objectA orderIndex] > [objectB orderIndex]){
163                         return(NSOrderedDescending);
164                 }else{
165                         return(NSOrderedAscending);
166                 }
167         }
171  * @brief Sort function
172  */
173 - (sortfunc)sortFunction{
174         return(&alphabeticalSort);
177 @end