Put NSAutoreleasePool usage around other distributed notification observer methods
[adiumx.git] / Source / AIContactSortSelectionPlugin.m
blobc35a05331f2c48a7f8ac88f0a79b695510bb25ad
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 <Adium/AIContactControllerProtocol.h>
18 #import "AIContactSortSelectionPlugin.h"
19 #import <Adium/AIMenuControllerProtocol.h>
20 #import <Adium/AIPreferenceControllerProtocol.h>
21 #import "ESContactSortConfigurationWindowController.h"
22 #import "AIAlphabeticalSort.h"
23 #import "ESStatusSort.h"
24 #import "AIManualSort.h"
26 #import <AIUtilities/AIDictionaryAdditions.h>
27 #import <AIUtilities/AIMenuAdditions.h>
28 #import <AIUtilities/AIStringAdditions.h>
29 #import <Adium/AISortController.h>
31 #define CONTACT_SORTING_DEFAULT_PREFS   @"SortingDefaults"
32 #define CONFIGURE_SORT_MENU_TITLE               [AILocalizedString(@"Configure Sorting",nil) stringByAppendingEllipsis]
33 #define SORT_MENU_TITLE                                 AILocalizedString(@"Sort Contacts",nil)
35 @interface AIContactSortSelectionPlugin (PRIVATE)
36 - (void)sortControllerListChanged:(NSNotification *)notification;
37 - (NSMenu *)_sortSelectionMenu;
38 - (void)_setActiveSortControllerFromPreferences;
39 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller;
40 - (void)_configureSortSelectionMenuItems;
41 @end
43 /*!
44  * @class AIContactSortSelectionPlugin
45  * @brief Component to manage contact sorting selection
46  */
47 @implementation AIContactSortSelectionPlugin
49 /*!
50  * @brief Install
51  */
52 - (void)installPlugin
54         enableConfigureSort = NO;
55         
56     //Register our default preferences
57     [[adium preferenceController] registerDefaults:[NSDictionary dictionaryNamed:CONTACT_SORTING_DEFAULT_PREFS 
58                                                                                                                                                 forClass:[self class]] 
59                                                                                   forGroup:PREF_GROUP_CONTACT_SORTING];
61         //Wait for Adium to finish launching before we set up the sort controller
62         [[adium notificationCenter] addObserver:self
63                                                                    selector:@selector(adiumFinishedLaunching:)
64                                                                            name:AIApplicationDidFinishLoadingNotification
65                                                                          object:nil];
66         
67         [[adium contactController] registerListSortController:[[[AIAlphabeticalSort alloc] init] autorelease]];
68         [[adium contactController] registerListSortController:[[[ESStatusSort alloc] init] autorelease]];
69         [[adium contactController] registerListSortController:[[[AIManualSort alloc] init] autorelease]];
72 /*!
73  * @brief Deallocate
74  */
75 - (void)dealloc
77         [menuItem_configureSort release]; menuItem_configureSort = nil;
78         [super dealloc];
81 /*!
82  * @brief Our available sort controllers changed
83  */
84 - (void)adiumFinishedLaunching:(NSNotification *)notification
86         //Inform the contactController of the active sort controller
87         [self _setActiveSortControllerFromPreferences];
88         
89         [self _configureSortSelectionMenuItems];
92 /*!
93  * @brief Set the active sort controller from the preferences
94  */
95 - (void)_setActiveSortControllerFromPreferences
97         NSEnumerator                            *enumerator;
98         AISortController                        *controller;
99         NSString                                        *identifier;
100         
101         //
102         identifier = [[adium preferenceController] preferenceForKey:KEY_CURRENT_SORT_MODE_IDENTIFIER
103                                                                                                                   group:PREF_GROUP_CONTACT_SORTING];
104         
105         //
106         enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
107         while ((controller = [enumerator nextObject])) {
108                 if ([identifier compare:[controller identifier]] == NSOrderedSame) {
109                         [[adium contactController] setActiveSortController:controller];
110                         break;
111                 }
112         }
113         
114         //Temporary failsafe for old preferences
115         if (!controller) {
116                 [[adium contactController] setActiveSortController:[[[adium contactController] sortControllerArray] objectAtIndex:0]];
117         }
121  * @brief Configure the sort selection menu items
122  */
123 - (void)_configureSortSelectionMenuItems
125     NSMenu                              *sortSelectionMenu;
126     NSMenuItem                  *menuItem;
127     NSEnumerator                *enumerator;
128         AISortController        *controller;
129         
130     //Create the menu
131     sortSelectionMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
132         
133         //Add each sort controller
134         enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
135         while ((controller = [enumerator nextObject])) {
136                 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[controller displayName]
137                                                                                                                                                  target:self
138                                                                                                                                                  action:@selector(changedSortSelection:)
139                                                                                                                                   keyEquivalent:@""] autorelease];
140                 [menuItem setRepresentedObject:controller];
141                 
142                 //Add the menu item
143                 [[adium menuController] addMenuItem:menuItem toLocation:LOC_View_Sorting];              
144         }
145         
146         //Add the menu item for configuring the sort
147         menuItem_configureSort = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:CONFIGURE_SORT_MENU_TITLE
148                                                                                                                                                                   target:self
149                                                                                                                                                                   action:@selector(configureSort:)
150                                                                                                                                                    keyEquivalent:@""];
151         [[adium menuController] addMenuItem:menuItem_configureSort toLocation:LOC_View_Sorting];
152         
153         AISortController        *activeSortController;
154         int                                     index;
155         
156         //Show a check by the active sort controller's menu item...
157         activeSortController = [[adium contactController] activeSortController];
158         
159         index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:activeSortController];
160         if (index != NSNotFound) {
161                 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOnState];
162         }
163         
164         ///...and set the Configure Sort menu title appropriately
165         [self _setConfigureSortMenuItemTitleForController:activeSortController];
169  * @brief Configure the currently active sort
170  */
171 - (void)configureSort:(id)sender
173         AISortController *controller = [[adium contactController] activeSortController];
174         [ESContactSortConfigurationWindowController showSortConfigurationWindowForController:controller];
178  * @brief Changed sort selection
180  * @param sender <tt>NSMenuItem</tt> with an <tt>AISortController</tt> representedObject
181  */
182 - (void)changedSortSelection:(id)sender
184         AISortController        *controller = [sender representedObject];
185         
186         //Uncheck the old active sort controller
187         int index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:[[adium contactController] activeSortController]];
188         if (index != NSNotFound) {
189                 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOffState];
190         }
191         
192         //Save the new preference
193         [[adium preferenceController] setPreference:[controller identifier] forKey:KEY_CURRENT_SORT_MODE_IDENTIFIER group:PREF_GROUP_CONTACT_SORTING];
195         //Inform the contact controller of the new active sort controller
196         [[adium contactController] setActiveSortController:controller];
197         
198         //Check the menu item and update the configure sort menu item title
199         [sender setState:NSOnState];
200         [self _setConfigureSortMenuItemTitleForController:controller];
201         
202         if ([ESContactSortConfigurationWindowController sortConfigurationIsOpen]) {
203                 [self configureSort:nil];
204         }
208  * @brief Update the "configure sort" menu item for controller
209  */
210 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller
212         NSString *configureSortMenuItemTitle = [controller configureSortMenuItemTitle];
213         if (configureSortMenuItemTitle) {
214                 [menuItem_configureSort setTitle:configureSortMenuItemTitle];
215                 enableConfigureSort = YES;
216         } else {
217                 [menuItem_configureSort setTitle:CONFIGURE_SORT_MENU_TITLE];
218                 enableConfigureSort = NO;
219         }
222 /* 
223  * @brief Validate menu items
225  * All memu items should always be enabled except for menuItem_configureSort, which may be disabled
226  */
227 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
229         if (menuItem == menuItem_configureSort)
230                 return enableConfigureSort;
231         else
232                 return YES;
235 @end