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 "AIContactController.h"
18 #import "AIContactSortSelectionPlugin.h"
19 #import "AIMenuController.h"
20 #import "AIPreferenceController.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 <Adium/AISortController.h>
30 #define CONTACT_SORTING_DEFAULT_PREFS @"SortingDefaults"
31 #define CONFIGURE_SORT_MENU_TITLE AILocalizedString(@"Configure Sorting...",nil)
32 #define SORT_MENU_TITLE AILocalizedString(@"Sort Contacts",nil)
34 @interface AIContactSortSelectionPlugin (PRIVATE)
35 - (void)sortControllerListChanged:(NSNotification *)notification;
36 - (NSMenu *)_sortSelectionMenu;
37 - (void)_setActiveSortControllerFromPreferences;
38 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller;
39 - (void)_configureSortSelectionMenuItems;
43 * @class AIContactSortSelectionPlugin
44 * @brief Component to manage contact sorting selection
46 @implementation AIContactSortSelectionPlugin
53 enableConfigureSort = NO;
55 //Register our default preferences
56 [[adium preferenceController] registerDefaults:[NSDictionary dictionaryNamed:CONTACT_SORTING_DEFAULT_PREFS
57 forClass:[self class]]
58 forGroup:PREF_GROUP_CONTACT_SORTING];
60 //Wait for Adium to finish launching before we set up the sort controller
61 [[adium notificationCenter] addObserver:self
62 selector:@selector(adiumFinishedLaunching:)
63 name:Adium_CompletedApplicationLoad
66 [[adium contactController] registerListSortController:[[[AIAlphabeticalSort alloc] init] autorelease]];
67 [[adium contactController] registerListSortController:[[[ESStatusSort alloc] init] autorelease]];
68 [[adium contactController] registerListSortController:[[[AIManualSort alloc] init] autorelease]];
76 [menuItem_configureSort release]; menuItem_configureSort = nil;
81 * @brief Our available sort controllers changed
83 - (void)adiumFinishedLaunching:(NSNotification *)notification
85 //Inform the contactController of the active sort controller
86 [self _setActiveSortControllerFromPreferences];
88 [self _configureSortSelectionMenuItems];
92 * @brief Set the active sort controller from the preferences
94 - (void)_setActiveSortControllerFromPreferences
96 NSEnumerator *enumerator;
97 AISortController *controller;
101 identifier = [[adium preferenceController] preferenceForKey:KEY_CURRENT_SORT_MODE_IDENTIFIER
102 group:PREF_GROUP_CONTACT_SORTING];
105 enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
106 while((controller = [enumerator nextObject])){
107 if([identifier compare:[controller identifier]] == NSOrderedSame){
108 [[adium contactController] setActiveSortController:controller];
113 //Temporary failsafe for old preferences
115 [[adium contactController] setActiveSortController:[[[adium contactController] sortControllerArray] objectAtIndex:0]];
120 * @brief Configure the sort selection menu items
122 - (void)_configureSortSelectionMenuItems
124 NSMenu *sortSelectionMenu;
125 NSMenuItem *menuItem;
126 NSEnumerator *enumerator;
127 AISortController *controller;
130 sortSelectionMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
132 //Add each sort controller
133 enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
134 while((controller = [enumerator nextObject])){
135 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[controller displayName]
137 action:@selector(changedSortSelection:)
138 keyEquivalent:@""] autorelease];
139 [menuItem setRepresentedObject:controller];
142 [[adium menuController] addMenuItem:menuItem toLocation:LOC_View_Sorting];
145 //Add the menu item for configuring the sort
146 menuItem_configureSort = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:CONFIGURE_SORT_MENU_TITLE
148 action:@selector(configureSort:)
150 [[adium menuController] addMenuItem:menuItem_configureSort toLocation:LOC_View_Sorting];
152 AISortController *activeSortController;
155 //Show a check by the active sort controller's menu item...
156 activeSortController = [[adium contactController] activeSortController];
158 index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:activeSortController];
159 if (index != NSNotFound){
160 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOnState];
163 ///...and set the Configure Sort menu title appropriately
164 [self _setConfigureSortMenuItemTitleForController:activeSortController];
168 * @brief Changed sort selection
170 * @param sender <tt>NSMenuItem</tt> with an <tt>AISortController</tt> representedObject
172 - (void)changedSortSelection:(id)sender
174 AISortController *controller = [sender representedObject];
176 //Uncheck the old active sort controller
177 int index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:[[adium contactController] activeSortController]];
178 if (index != NSNotFound){
179 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOffState];
182 //Save the new preference
183 [[adium preferenceController] setPreference:[controller identifier] forKey:KEY_CURRENT_SORT_MODE_IDENTIFIER group:PREF_GROUP_CONTACT_SORTING];
185 //Inform the contact controller of the new active sort controller
186 [[adium contactController] setActiveSortController:controller];
188 //Check the menu item and update the configure sort menu item title
189 [sender setState:NSOnState];
190 [self _setConfigureSortMenuItemTitleForController:controller];
194 * @brief Update the "configure sort" menu item for controller
196 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller
198 NSString *configureSortMenuItemTitle = [controller configureSortMenuItemTitle];
199 if (configureSortMenuItemTitle) {
200 [menuItem_configureSort setTitle:configureSortMenuItemTitle];
201 enableConfigureSort = YES;
203 [menuItem_configureSort setTitle:CONFIGURE_SORT_MENU_TITLE];
204 enableConfigureSort = NO;
209 * @brief Configure the currently active sort
211 - (void)configureSort:(id)sender
213 AISortController *controller = [[adium contactController] activeSortController];
214 [ESContactSortConfigurationWindowController showSortConfigurationWindowForController:controller];
218 * @brief Validate menu items
220 * All memu items should always be enabled except for menuItem_configureSort, which may be disabled
222 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
224 if (menuItem == menuItem_configureSort)
225 return enableConfigureSort;