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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | /*
* 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 <Adium/AIChatControllerProtocol.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIContentControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/AIToolbarControllerProtocol.h>
#import "ESUserIconHandlingPlugin.h"
#import <AIUtilities/AIApplicationAdditions.h>
#import <AIUtilities/AIFileManagerAdditions.h>
#import <AIUtilities/AIMutableOwnerArray.h>
#import <AIUtilities/AIToolbarUtilities.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/AIImageButton.h>
#import <Adium/AIAccount.h>
#import <Adium/AIChat.h>
#import <Adium/AIListContact.h>
#import <Adium/AIListObject.h>
#import <Adium/AIServiceIcons.h>
#define TOOLBAR_ITEM_TAG -999
@interface ESUserIconHandlingPlugin ()
- (void)registerToolbarItem;
- (void)_updateToolbarIconOfChat:(AIChat *)inChat inWindow:(NSWindow *)window;
- (void)_updateToolbarItem:(NSToolbarItem *)item forChat:(AIChat *)chat;
- (void)updateToolbarItemForObject:(AIListObject *)inObject;
@end
/*!
* @class ESUserIconHandlingPlugin
* @brief User icon handling component
*
* This component manages the Adium user icon cache. It also provides a toolbar icon which shows the user icon
* or service icon of the current chat in its window.
*/
@implementation ESUserIconHandlingPlugin
/*!
* @brief Install
*/
- (void)installPlugin
{
//Register our observers
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(listObjectAttributesChanged:)
name:ListObject_AttributesChanged
object:nil];
[self registerToolbarItem];
}
/*!
* @brief Uninstall
*/
- (void)uninstallPlugin
{
[adium.preferenceController unregisterPreferenceObserver:self];
}
//Needs some [self updateToolbarItemForObject:inObject];
/*!
* @brief List object attributes changes
*
* A plugin, or this plugin, modified the display array for the object; ensure our cache is up to date.
*/
- (void)listObjectAttributesChanged:(NSNotification *)notification
{
AIListObject *inObject = [notification object];
NSSet *keys = [[notification userInfo] objectForKey:@"Keys"];
if ([keys containsObject:KEY_USER_ICON]) {
if (inObject) {
[self updateToolbarItemForObject:inObject];
} else {
for (AIChat *chat in adium.interfaceController.openChats) {
NSWindow *window = [adium.interfaceController windowForChat:chat];
if (window) {
[self _updateToolbarIconOfChat:chat
inWindow:window];
}
}
}
}
}
#pragma mark Toolbar Item
/*!
* @brief Register our toolbar item
*
* Our toolbar item shows an image for the current chat, displaying it full size/animating if clicked.
*/
- (void)registerToolbarItem
{
AIImageButton *button;
NSToolbarItem *toolbarItem;
toolbarItems = [[NSMutableSet alloc] init];
validatedItems = [[NSMutableSet alloc] init];
//Toolbar item registration
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(toolbarWillAddItem:)
name:NSToolbarWillAddItemNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(toolbarDidRemoveItem:)
name:NSToolbarDidRemoveItemNotification
object:nil];
button = [[AIImageButton alloc] initWithFrame:NSMakeRect(0,0,32,32)];
toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:@"UserIcon"
label:AILocalizedString(@"Icon",nil)
paletteLabel:AILocalizedString(@"Contact Icon",nil)
toolTip:AILocalizedString(@"Show this contact's icon",nil)
target:self
settingSelector:@selector(setView:)
itemContent:button
action:@selector(dummyAction:)
menu:nil];
[toolbarItem setMinSize:NSMakeSize(32,32)];
[toolbarItem setMaxSize:NSMakeSize(32,32)];
[button setToolbarItem:toolbarItem];
[button setImage:[NSImage imageNamed:@"userIconToolbar" forClass:[self class] loadLazily:YES]];
[button release];
//Register our toolbar item
[adium.toolbarController registerToolbarItem:toolbarItem forToolbarType:@"MessageWindow"];
}
/*!
* @brief After the toolbar has added the item we can set up the submenus
*/
- (void)toolbarWillAddItem:(NSNotification *)notification
{
NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"];
if ([[item itemIdentifier] isEqualToString:@"UserIcon"]) {
[item setEnabled:YES];
//Add menu to toolbar item (for text mode)
NSMenuItem *menuFormRepresentation, *blankMenuItem;
NSMenu *menu;
menuFormRepresentation = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] init] autorelease];
menu = [[[NSMenu allocWithZone:[NSMenu menuZone]] init] autorelease];
[menu setDelegate:self];
[menu setAutoenablesItems:NO];
blankMenuItem = [[NSMenuItem alloc] initWithTitle:@""
target:self
action:@selector(dummyAction:)
keyEquivalent:@""];
[blankMenuItem setRepresentedObject:item];
[blankMenuItem setEnabled:YES];
[menu addItem:blankMenuItem];
[blankMenuItem release];
[menuFormRepresentation setSubmenu:menu];
[menuFormRepresentation setTitle:[item label]];
[item setMenuFormRepresentation:menuFormRepresentation];
//If this is the first item added, start observing for chats becoming visible so we can update the icon
if ([toolbarItems count] == 0) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(chatDidBecomeVisible:)
name:@"AIChatDidBecomeVisible"
object:nil];
}
[toolbarItems addObject:item];
[self performSelector:@selector(toolbarDidAddItem:)
withObject:item
afterDelay:0];
}
}
- (void)toolbarDidAddItem:(NSToolbarItem *)item
{
/* Only need to take action if we haven't already validated the initial state of this item.
* This will only be true when the toolbar is revealed for the first time having been hidden when window opened.
*/
if (![validatedItems containsObject:item]) {
NSEnumerator *enumerator = [[NSApp windows] objectEnumerator];
NSWindow *window;
NSToolbar *thisItemsToolbar = [item toolbar];
//Look at each window to find the toolbar we are in
while ((window = [enumerator nextObject])) {
if ([window toolbar] == thisItemsToolbar) break;
}
if (window) {
[self _updateToolbarItem:item
forChat:[adium.interfaceController activeChatInWindow:window]];
}
}
}
/*!
* @brief Toolbar removed an item.
*
* If the item is one of ours, stop tracking it.
*
* @param notification Notification with an @"item" userInfo key for an NSToolbarItem.
*/
- (void)toolbarDidRemoveItem: (NSNotification *)notification
{
NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"];
if ([toolbarItems containsObject:item]) {
[item setView:nil];
[toolbarItems removeObject:item];
[validatedItems removeObject:item];
if ([toolbarItems count] == 0) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"AIChatDidBecomeVisible"
object:nil];
}
}
}
/*!
* @brief A chat became visible in a window.
*
* Update the item with the @"UserIcon" identifier if necessary
*
* @param notification Notification with an AIChat object and an @"NSWindow" userInfo key
*/
- (void)chatDidBecomeVisible:(NSNotification *)notification
{
[self _updateToolbarIconOfChat:[notification object]
inWindow:[[notification userInfo] objectForKey:@"NSWindow"]];
}
- (void)updateToolbarItemForObject:(AIListObject *)inObject
{
AIChat *chat;
NSWindow *window;
//Update the icon in the toolbar for this contact if a chat is open and we have any toolbar items
if (([toolbarItems count] > 0) &&
[inObject isKindOfClass:[AIListContact class]] &&
(chat = [adium.chatController existingChatWithContact:(AIListContact *)inObject]) &&
(window = [adium.interfaceController windowForChat:chat])) {
[self _updateToolbarIconOfChat:chat
inWindow:window];
}
}
- (void)_updateToolbarItem:(NSToolbarItem *)item forChat:(AIChat *)chat
{
AIListContact *listContact;
NSImage *image;
if ((listContact = chat.listObject.parentContact) && !chat.isGroupChat) {
image = [listContact userIcon];
//Use the serviceIcon if no image can be found
if (!image) image = [AIServiceIcons serviceIconForObject:listContact
type:AIServiceIconLarge
direction:AIIconNormal];
} else {
//If we have no listObject or we have a name, we are a group chat and
//should use the account's service icon
image = [AIServiceIcons serviceIconForObject:chat.account
type:AIServiceIconLarge
direction:AIIconNormal];
}
[(AIImageButton *)[item view] setImage:image];
[validatedItems addObject:item];
}
/*!
* @brief Update the user image toolbar icon in a chat
*
* @param chat The chat for which to retrieve an image
* @param window The window in which the chat resides
*/
- (void)_updateToolbarIconOfChat:(AIChat *)chat inWindow:(NSWindow *)window
{
NSToolbar *toolbar = [window toolbar];
NSEnumerator *enumerator = [[toolbar items] objectEnumerator];
NSToolbarItem *item;
while ((item = [enumerator nextObject])) {
if ([[item itemIdentifier] isEqualToString:@"UserIcon"]) {
[self _updateToolbarItem:item forChat:chat];
break;
}
}
}
/*!
* @brief Empty action for menu item validation purposes
*/
- (IBAction)dummyAction:(id)sender{};
/*!
* @brief Menu needs update
*
* Should only be called for a menu off one of our toolbar items in text-only mode, and only when that menu is about
* to be displayed.
*/
- (void)menuNeedsUpdate:(NSMenu *)menu
{
NSMenuItem *menuItem = [menu itemAtIndex:0];
NSToolbarItem *toolbarItem = [menuItem representedObject];
[menuItem setImage:[[[(AIImageButton *)[toolbarItem view] image] copy] autorelease]];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
return YES;
}
@end
|