William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AIContactSortSelectionPlugin.h"
#import <Adium/AIMenuControllerProtocol.h>
#import "ESContactSortConfigurationWindowController.h"
#import "AIAlphabeticalSort.h"
#import "ESStatusSort.h"
#import "AIManualSort.h"
#import <AIUtilities/AIDictionaryAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <Adium/AISortController.h>
#define CONTACT_SORTING_DEFAULT_PREFS @"SortingDefaults"
#define CONFIGURE_SORT_MENU_TITLE [AILocalizedString(@"Configure Sorting",nil) stringByAppendingEllipsis]
#define SORT_MENU_TITLE AILocalizedString(@"Sort Contacts",nil)
@interface AIContactSortSelectionPlugin ()
- (void)_setActiveSortControllerFromPreferences;
- (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller;
- (void)_configureSortSelectionMenuItems;
@end
/*!
* @class AIContactSortSelectionPlugin
* @brief Component to manage contact sorting selection
*/
@implementation AIContactSortSelectionPlugin
/*!
* @brief Install
*/
- (void)installPlugin
{
enableConfigureSort = NO;
//Register our default preferences
[adium.preferenceController registerDefaults:[NSDictionary dictionaryNamed:CONTACT_SORTING_DEFAULT_PREFS
forClass:[self class]]
forGroup:PREF_GROUP_CONTACT_SORTING];
//Wait for Adium to finish launching before we set up the sort controller
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(adiumFinishedLaunching:)
name:AIApplicationDidFinishLoadingNotification
object:nil];
[AISortController registerSortController:[[[AIAlphabeticalSort alloc] init] autorelease]];
[AISortController registerSortController:[[[ESStatusSort alloc] init] autorelease]];
[AISortController registerSortController:[[[AIManualSort alloc] init] autorelease]];
}
/*!
* @brief Deallocate
*/
- (void)dealloc
{
[menuItem_configureSort release]; menuItem_configureSort = nil;
[super dealloc];
}
/*!
* @brief Our available sort controllers changed
*/
- (void)adiumFinishedLaunching:(NSNotification *)notification
{
//Inform the contactController of the active sort controller
[self _setActiveSortControllerFromPreferences];
[self _configureSortSelectionMenuItems];
}
/*!
* @brief Set the active sort controller from the preferences
*/
- (void)_setActiveSortControllerFromPreferences
{
NSString *identifier = [adium.preferenceController preferenceForKey:KEY_CURRENT_SORT_MODE_IDENTIFIER
group:PREF_GROUP_CONTACT_SORTING];
NSEnumerator *enumerator = [[AISortController availableSortControllers] objectEnumerator];
AISortController *controller = nil;
while ((controller = [enumerator nextObject])) {
if ([identifier compare:[controller identifier]] == NSOrderedSame) {
[AISortController setActiveSortController:controller];
break;
}
}
//Temporary failsafe for old preferences
if (!controller) {
[AISortController setActiveSortController:[[AISortController availableSortControllers] objectAtIndex:0]];
}
}
/*!
* @brief Configure the sort selection menu items
*/
- (void)_configureSortSelectionMenuItems
{
NSMenu *sortSelectionMenu;
//Create the menu
sortSelectionMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
//Add each sort controller
for (AISortController *controller in [AISortController availableSortControllers]) {
NSMenuItem *menuItem;
menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:controller.displayName
target:self
action:@selector(changedSortSelection:)
keyEquivalent:@""] autorelease];
[menuItem setRepresentedObject:controller];
//Add the menu item
[adium.menuController addMenuItem:menuItem toLocation:LOC_View_Sorting];
}
//Add the menu item for configuring the sort
menuItem_configureSort = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:CONFIGURE_SORT_MENU_TITLE
target:self
action:@selector(configureSort:)
keyEquivalent:@""];
[adium.menuController addMenuItem:menuItem_configureSort toLocation:LOC_View_Sorting];
AISortController *activeSortController;
NSInteger index;
//Show a check by the active sort controller's menu item...
activeSortController = [AISortController activeSortController];
index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:activeSortController];
if (index != NSNotFound) {
[[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOnState];
}
///...and set the Configure Sort menu title appropriately
[self _setConfigureSortMenuItemTitleForController:activeSortController];
}
/*!
* @brief Configure the currently active sort
*/
- (void)configureSort:(id)sender
{
AISortController *controller = [AISortController activeSortController];
[ESContactSortConfigurationWindowController showSortConfigurationWindowForController:controller];
}
/*!
* @brief Changed sort selection
*
* @param sender <tt>NSMenuItem</tt> with an <tt>AISortController</tt> representedObject
*/
- (void)changedSortSelection:(id)sender
{
AISortController *controller = [sender representedObject];
//Uncheck the old active sort controller
NSInteger index = [[menuItem_configureSort menu] indexOfItemWithRepresentedObject:[AISortController activeSortController]];
if (index != NSNotFound) {
[[[menuItem_configureSort menu] itemAtIndex:index] setState:NSOffState];
}
//Save the new preference
[adium.preferenceController setPreference:[controller identifier] forKey:KEY_CURRENT_SORT_MODE_IDENTIFIER group:PREF_GROUP_CONTACT_SORTING];
//Inform the contact controller of the new active sort controller
[AISortController setActiveSortController:controller];
//Check the menu item and update the configure sort menu item title
[sender setState:NSOnState];
[self _setConfigureSortMenuItemTitleForController:controller];
if ([ESContactSortConfigurationWindowController sortConfigurationIsOpen]) {
[self configureSort:nil];
}
}
/*!
* @brief Update the "configure sort" menu item for controller
*/
- (void)_setConfigureSortMenuItemTitleForController:(AISortController *)controller
{
NSString *configureSortMenuItemTitle = [controller configureSortMenuItemTitle];
if (configureSortMenuItemTitle) {
[menuItem_configureSort setTitle:configureSortMenuItemTitle];
enableConfigureSort = YES;
} else {
[menuItem_configureSort setTitle:CONFIGURE_SORT_MENU_TITLE];
enableConfigureSort = NO;
}
}
/*
* @brief Validate menu items
*
* All memu items should always be enabled except for menuItem_configureSort, which may be disabled
*/
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if (menuItem == menuItem_configureSort)
return enableConfigureSort;
else
return YES;
}
@end
|