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 | /*
* 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 "AIListObjectContentsPlugin.h"
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/AIMenuControllerProtocol.h>
#import <Adium/AIListContact.h>
#import <Adium/AIListObject.h>
#import <Adium/AIListGroup.h>
#import <Adium/AIMetaContact.h>
#import <Adium/AIAbstractListController.h>
#import <Adium/AIServiceIcons.h>
#import <AIUtilities/AIImageDrawingAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <Adium/AIStatusIcons.h>
#import <Adium/AIContactHidingController.h>
#define META_TOOLTIP_ICON_SIZE NSMakeSize(11,11)
#define EXPAND_CONTACT AILocalizedString(@"Expand Combined Contact", nil)
#define COLLAPSE_CONTACT AILocalizedString(@"Collapse Combined Contact", nil)
#define MAX_CONTACTS 20
#define MORE_CONTACTS_STRING AILocalizedString(@"%d others", @"Used to describe omitted contacts.\
The first parameter is the number of omitted contacts")
/*!
* @class AIListObjectContentsPlugin
* @brief Tooltip component: Show the contacts contained by metaContacts, with service and status state.
*/
@implementation AIListObjectContentsPlugin
/*!
* @brief Install
*/
- (void)installPlugin
{
//Install our tooltip entry
[adium.interfaceController registerContactListTooltipEntry:self secondaryEntry:YES];
contextualMenuItem = [[NSMenuItem alloc] initWithTitle:EXPAND_CONTACT
target:self
action:@selector(toggleMetaContactExpansion:)
keyEquivalent:@""];
[adium.menuController addContextualMenuItem:contextualMenuItem
toLocation:Context_Contact_ListAction];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(inspectedObjectDidChange:)
name:AIContactInfoInspectorDidChangeInspectedObject
object:nil];
}
- (void)dealloc
{
[contextualMenuItem release]; contextualMenuItem = nil;
[super dealloc];
}
/*!
* @brief Tooltip label
*
* @result A label, or nil if no tooltip entry should be shown
*/
- (NSString *)labelForObject:(AIListObject *)inObject
{
if ([inObject conformsToProtocol:@protocol(AIContainingObject)]) {
return AILocalizedString(@"Contacts",nil);
}
return nil;
}
/*!
* @brief Tooltip entry
*
* @result The tooltip entry, or nil if no tooltip should be shown
*/
- (NSAttributedString *)entryForObject:(AIListObject *)inObject
{
NSMutableAttributedString *entry = nil;
if ([inObject conformsToProtocol:@protocol(AIContainingObject)]) {
id<AIContainingObject> containingObject = (id<AIContainingObject>)inObject;
NSArray *listContacts = [containingObject uniqueContainedObjects];
//Only display the contents of a meta if it has more than one contact within it.
if ([inObject isKindOfClass:[AIListGroup class]] || [listContacts count] > 1) {
BOOL shouldAppendString = NO;
BOOL shouldAppendServiceIcon = ([inObject isKindOfClass:[AIMetaContact class]] && ![(AIMetaContact *)inObject containsOnlyOneService]);
entry = [[NSMutableAttributedString alloc] init];
NSMutableString *entryString = [entry mutableString];
NSUInteger count = 0;
for (AIListContact *contact in listContacts) {
if ([inObject isKindOfClass:[AIListGroup class]] &&
![[AIContactHidingController sharedController] visibilityOfListObject:contact inContainer:containingObject]) {
continue;
}
NSImage *statusIcon, *serviceIcon;
if (shouldAppendString) {
[entryString appendString:@"\r"];
} else {
shouldAppendString = YES;
}
// If there are a lot of contacts, just stop.
if (++count >= MAX_CONTACTS) {
[entryString appendString:[NSString stringWithFormat:MORE_CONTACTS_STRING, listContacts.count - MAX_CONTACTS]];
break;
}
statusIcon = [[AIStatusIcons statusIconForListObject:contact
type:AIStatusIconTab
direction:AIIconNormal] imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
if (statusIcon) {
NSTextAttachment *attachment;
NSTextAttachmentCell *cell;
cell = [[NSTextAttachmentCell alloc] init];
[cell setImage:statusIcon];
attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell:cell];
[cell release];
[entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[attachment release];
[entryString appendString:@" "];
}
if ([inObject isKindOfClass:[AIMetaContact class]]) {
[entryString appendString:contact.formattedUID];
} else if ([inObject isKindOfClass:[AIListGroup class]]) {
[entryString appendString:contact.displayName];
}
if (shouldAppendServiceIcon) {
serviceIcon = [[AIServiceIcons serviceIconForObject:contact type:AIServiceIconSmall direction:AIIconNormal]
imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
if (serviceIcon) {
NSTextAttachment *attachment;
NSTextAttachmentCell *cell;
cell = [[NSTextAttachmentCell alloc] init];
[cell setImage:serviceIcon];
attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell:cell];
[cell release];
[entryString appendString:@" "];
[entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[attachment release];
}
}
}
}
}
return [entry autorelease];
}
- (BOOL)shouldDisplayInContactInspector
{
return YES;
}
#pragma mark Automatic temporary expansion
- (void)inspectedObjectDidChange:(NSNotification *)inNotification
{
AIListObject *oldListObject = [[inNotification userInfo] objectForKey:KEY_PREVIOUS_INSPECTED_OBJECT];
AIListObject *newListObject = [[inNotification userInfo] objectForKey:KEY_NEW_INSPECTED_OBJECT];
if (oldListObject && [oldListObject isKindOfClass:[AIMetaContact class]] &&
[oldListObject boolValueForProperty:@"TemporaryMetaContactExpansion"]) {
[oldListObject setValue:nil
forProperty:@"TemporaryMetaContactExpansion"
notify:NotifyNever];
[[NSNotificationCenter defaultCenter] postNotificationName:AIPerformCollapseItemNotification
object:oldListObject];
}
if (newListObject && [newListObject isKindOfClass:[AIMetaContact class]] &&
![(AIMetaContact *)newListObject isExpanded]) {
[newListObject setValue:[NSNumber numberWithBool:YES]
forProperty:@"TemporaryMetaContactExpansion"
notify:NotifyNever];
[[NSNotificationCenter defaultCenter] postNotificationName:AIPerformExpandItemNotification
object:newListObject];
}
}
#pragma mark Menu
- (void)toggleMetaContactExpansion:(id)sender
{
AIListObject *listObject = adium.menuController.currentContextMenuObject;
if ([listObject isKindOfClass:[AIMetaContact class]]) {
BOOL currentlyExpanded = [(AIMetaContact *)listObject isExpanded];
if (currentlyExpanded) {
[[NSNotificationCenter defaultCenter] postNotificationName:AIPerformCollapseItemNotification
object:listObject];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:AIPerformExpandItemNotification
object:listObject];
}
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
AIListObject *listObject = adium.menuController.currentContextMenuObject;
return ([listObject isKindOfClass:[AIMetaContact class]] &&
[(AIMetaContact *)listObject uniqueContainedObjectsCount] > 1);
}
- (void)menu:(NSMenu *)menu needsUpdateForMenuItem:(NSMenuItem *)menuItem
{
AIListObject *listObject = adium.menuController.currentContextMenuObject;
if (menuItem == contextualMenuItem) {
if ([listObject isKindOfClass:[AIMetaContact class]] &&
[(AIMetaContact *)listObject uniqueContainedObjectsCount] > 1) {
BOOL currentlyExpanded = [(AIMetaContact *)listObject isExpanded];
if (currentlyExpanded) {
[menuItem setTitle:COLLAPSE_CONTACT];
} else {
[menuItem setTitle:EXPAND_CONTACT];
}
} else {
[menuItem setTitle:EXPAND_CONTACT];
}
}
}
@end
|