Using {{{setDefaultPermitDenyForAccount}}} on the gaim thread. Changed an autoreleas...
[adiumx.git] / Source / AIContactSortSelectionPlugin.m
blob0a3357a38b089c224cfdc4fcaaf7183e436e5654
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 "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;
40 @end
42 /*!
43  * @class AIContactSortSelectionPlugin
44  * @brief Component to manage contact sorting selection
45  */
46 @implementation AIContactSortSelectionPlugin
48 /*!
49  * @brief Install
50  */
51 - (void)installPlugin
53         enableConfigureSort = NO;
54         
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
64                                                                          object:nil];
65         
66         [[adium contactController] registerListSortController:[[[AIAlphabeticalSort alloc] init] autorelease]];
67         [[adium contactController] registerListSortController:[[[ESStatusSort alloc] init] autorelease]];
68         [[adium contactController] registerListSortController:[[[AIManualSort alloc] init] autorelease]];
71 /*!
72  * @brief Deallocate
73  */
74 - (void)dealloc
76         [menuItem_configureSort release]; menuItem_configureSort = nil;
77         [super dealloc];
80 /*!
81  * @brief Our available sort controllers changed
82  */
83 - (void)adiumFinishedLaunching:(NSNotification *)notification
85         //Inform the contactController of the active sort controller
86         [self _setActiveSortControllerFromPreferences];
87         
88         [self _configureSortSelectionMenuItems];
91 /*!
92  * @brief Set the active sort controller from the preferences
93  */
94 - (void)_setActiveSortControllerFromPreferences
96         NSEnumerator                            *enumerator;
97         AISortController                        *controller;
98         NSString                                        *identifier;
99         
100         //
101         identifier = [[adium preferenceController] preferenceForKey:KEY_CURRENT_SORT_MODE_IDENTIFIER
102                                                                                                                   group:PREF_GROUP_CONTACT_SORTING];
103         
104         //
105         enumerator = [[[adium contactController] sortControllerArray] objectEnumerator];
106         while((controller = [enumerator nextObject])){
107                 if([identifier compare:[controller identifier]] == NSOrderedSame){
108                         [[adium contactController] setActiveSortController:controller];
109                         break;
110                 }
111         }
112         
113         //Temporary failsafe for old preferences
114         if (!controller){
115                 [[adium contactController] setActiveSortController:[[[adium contactController] sortControllerArray] objectAtIndex:0]];
116         }
120  * @brief Configure the sort selection menu items
121  */
122 - (void)_configureSortSelectionMenuItems
124     NSMenu                              *sortSelectionMenu;
125     NSMenuItem                  *menuItem;
126     NSEnumerator                *enumerator;
127         AISortController        *controller;
128         
129     //Create the menu
130     sortSelectionMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
131         
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]
136                                                                                                                                                  target:self
137                                                                                                                                                  action:@selector(changedSortSelection:)
138                                                                                                                                   keyEquivalent:@""] autorelease];
139                 [menuItem setRepresentedObject:controller];
140                 
141                 //Add the menu item
142                 [[adium menuController] addMenuItem:menuItem toLocation:LOC_View_Sorting];              
143         }
144         
145         //Add the menu item for configuring the sort
146         menuItem_configureSort = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:CONFIGURE_SORT_MENU_TITLE
147                                                                                                                                                                   target:self
148                                                                                                                                                                   action:@selector(configureSort:)
149                                                                                                                                                    keyEquivalent:@""];
150         [[adium menuController] addMenuItem:menuItem_configureSort toLocation:LOC_View_Sorting];
151         
152         AISortController        *activeSortController;
153         int                                     index;
154         
155         //Show a check by the active sort controller's menu item...
156         activeSortController = [[adium contactController] activeSortController];
157         
158         index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:activeSortController];
159         if (index != NSNotFound){
160                 [[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOnState];
161         }
162         
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
171  */
172 - (void)changedSortSelection:(id)sender
174         AISortController        *controller = [sender representedObject];
175         
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];
180         }
181         
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];
187         
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
195  */
196 - (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller
198         NSString *configureSortMenuItemTitle = [controller configureSortMenuItemTitle];
199         if (configureSortMenuItemTitle) {
200                 [menuItem_configureSort setTitle:configureSortMenuItemTitle];
201                 enableConfigureSort = YES;
202         } else {
203                 [menuItem_configureSort setTitle:CONFIGURE_SORT_MENU_TITLE];
204                 enableConfigureSort = NO;
205         }
209  * @brief Configure the currently active sort
210  */
211 - (void)configureSort:(id)sender
213         AISortController *controller = [[adium contactController] activeSortController];
214         [ESContactSortConfigurationWindowController showSortConfigurationWindowForController:controller];
217 /* 
218  * @brief Validate menu items
220  * All memu items should always be enabled except for menuItem_configureSort, which may be disabled
221  */
222 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
224         if (menuItem == menuItem_configureSort)
225                 return enableConfigureSort;
226         else
227                 return YES;
230 @end