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 <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;
44 * @class AIContactSortSelectionPlugin
45 * @brief Component to manage contact sorting selection
47 @implementation AIContactSortSelectionPlugin
54 enableConfigureSort = NO;
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
67 [[adium contactController] registerListSortController:[[[AIAlphabeticalSort alloc] init] autorelease]];
68 [[adium contactController] registerListSortController:[[[ESStatusSort alloc] init] autorelease]];
69 [[adium contactController] registerListSortController:[[[AIManualSort alloc] init] autorelease]];
77 [menuItem_configureSort release]; menuItem_configureSort = nil;
82 * @brief Our available sort controllers changed
84 - (void)adiumFinishedLaunching:(NSNotification *)notification
86 //Inform the contactController of the active sort controller
87 [self _setActiveSortControllerFromPreferences];
89 [self _configureSortSelectionMenuItems];
93 * @brief Set the active sort controller from the preferences
95 - (void)_setActiveSortControllerFromPreferences
97 NSEnumerator *enumerator;
98 AISortController *controller;
102 identifier = [[adium preferenceController] preferenceForKey:KEY_CURRENT_SORT_MODE_IDENTIFIER
103 group:PREF_GROUP_CONTACT_SORTING];
106 enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
107 while ((controller = [enumerator nextObject])) {
108 if ([identifier compare:[controller identifier]] == NSOrderedSame) {
109 [[adium contactController] setActiveSortController:controller];
114 //Temporary failsafe for old preferences
116 [[adium contactController] setActiveSortController:[[[adium contactController] sortControllerArray] objectAtIndex:0]];
121 * @brief Configure the sort selection menu items
123 - (void)_configureSortSelectionMenuItems
125 NSMenu *sortSelectionMenu;
126 NSMenuItem *menuItem;
127 NSEnumerator *enumerator;
128 AISortController *controller;
131 sortSelectionMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
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]
138 action:@selector(changedSortSelection:)
139 keyEquivalent:@""] autorelease];
140 [menuItem setRepresentedObject:controller];
143 [[adium menuController] addMenuItem:menuItem toLocation:LOC_View_Sorting];
146 //Add the menu item for configuring the sort
147 menuItem_configureSort = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:CONFIGURE_SORT_MENU_TITLE
149 action:@selector(configureSort:)
151 [[adium menuController] addMenuItem:menuItem_configureSort toLocation:LOC_View_Sorting];
153 AISortController *activeSortController;
156 //Show a check by the active sort controller's menu item...
157 activeSortController = [[adium contactController] activeSortController];
159 index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:activeSortController];
160 if (index != NSNotFound) {
161 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOnState];
164 ///...and set the Configure Sort menu title appropriately
165 [self _setConfigureSortMenuItemTitleForController:activeSortController];
169 * @brief Configure the currently active sort
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
182 - (void)changedSortSelection:(id)sender
184 AISortController *controller = [sender representedObject];
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];
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];
198 //Check the menu item and update the configure sort menu item title
199 [sender setState:NSOnState];
200 [self _setConfigureSortMenuItemTitleForController:controller];
202 if ([ESContactSortConfigurationWindowController sortConfigurationIsOpen]) {
203 [self configureSort:nil];
208 * @brief Update the "configure sort" menu item for controller
210 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller
212 NSString *configureSortMenuItemTitle = [controller configureSortMenuItemTitle];
213 if (configureSortMenuItemTitle) {
214 [menuItem_configureSort setTitle:configureSortMenuItemTitle];
215 enableConfigureSort = YES;
217 [menuItem_configureSort setTitle:CONFIGURE_SORT_MENU_TITLE];
218 enableConfigureSort = NO;
223 * @brief Validate menu items
225 * All memu items should always be enabled except for menuItem_configureSort, which may be disabled
227 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
229 if (menuItem == menuItem_configureSort)
230 return enableConfigureSort;