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 "ESMetaContactContentsPlugin.h"
18 #import <Adium/AIContactControllerProtocol.h>
19 #import <Adium/AIInterfaceControllerProtocol.h>
20 #import <Adium/AIMenuControllerProtocol.h>
21 #import <Adium/AIListContact.h>
22 #import <Adium/AIListObject.h>
23 #import <Adium/AIMetaContact.h>
24 #import <Adium/AIAbstractListController.h>
25 #import <Adium/AIServiceIcons.h>
26 #import <AIUtilities/AIImageAdditions.h>
27 #import <AIUtilities/AIMenuAdditions.h>
29 #define META_TOOLTIP_ICON_SIZE NSMakeSize(11,11)
31 #define EXPAND_CONTACT AILocalizedString(@"Expand combined contact", nil)
32 #define COLLAPSE_CONTACT AILocalizedString(@"Collapse combined contact", nil)
34 * @class ESMetaContactContentsPlugin
35 * @brief Tooltip component: Show the contacts contained by metaContacts, with service and status state.
37 @implementation ESMetaContactContentsPlugin
44 //Install our tooltip entry
45 [[adium interfaceController] registerContactListTooltipEntry:self secondaryEntry:YES];
47 contextualMenuItem = [[NSMenuItem alloc] initWithTitle:EXPAND_CONTACT
49 action:@selector(toggleMetaContactExpansion:)
51 [[adium menuController] addContextualMenuItem:contextualMenuItem
52 toLocation:Context_Contact_Manage];
57 [contextualMenuItem release]; contextualMenuItem = nil;
63 * @brief Tooltip label
65 * @result A label, or nil if no tooltip entry should be shown
67 - (NSString *)labelForObject:(AIListObject *)inObject
69 if ([inObject isKindOfClass:[AIMetaContact class]]) {
70 return AILocalizedString(@"Contacts",nil);
77 * @brief Tooltip entry
79 * @result The tooltip entry, or nil if no tooltip should be shown
81 - (NSAttributedString *)entryForObject:(AIListObject *)inObject
83 NSMutableAttributedString *entry = nil;
85 if ([inObject isKindOfClass:[AIMetaContact class]]) {
86 NSArray *listContacts = [(AIMetaContact *)inObject listContacts];
88 //Only display the contents if it has more than one contact within it.
89 if ([listContacts count] > 1) {
90 NSMutableString *entryString;
91 AIListContact *contact;
92 NSEnumerator *enumerator;
93 BOOL shouldAppendString = NO;
94 BOOL shouldAppendServiceIcon = ![(AIMetaContact *)inObject containsOnlyOneService];
96 entry = [[NSMutableAttributedString alloc] init];
97 entryString = [entry mutableString];
99 enumerator = [listContacts objectEnumerator];
100 while ((contact = [enumerator nextObject])) {
101 NSImage *statusIcon, *serviceIcon;
103 if (shouldAppendString) {
104 [entryString appendString:@"\r"];
106 shouldAppendString = YES;
109 statusIcon = [[contact displayArrayObjectForKey:@"Tab Status Icon"] imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
112 NSTextAttachment *attachment;
113 NSTextAttachmentCell *cell;
115 cell = [[NSTextAttachmentCell alloc] init];
116 [cell setImage:statusIcon];
118 attachment = [[NSTextAttachment alloc] init];
119 [attachment setAttachmentCell:cell];
122 [entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
123 [attachment release];
126 [entryString appendString:[contact formattedUID]];
128 if (shouldAppendServiceIcon) {
129 serviceIcon = [[AIServiceIcons serviceIconForObject:contact type:AIServiceIconSmall direction:AIIconNormal]
130 imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
132 NSTextAttachment *attachment;
133 NSTextAttachmentCell *cell;
135 cell = [[NSTextAttachmentCell alloc] init];
136 [cell setImage:serviceIcon];
138 attachment = [[NSTextAttachment alloc] init];
139 [attachment setAttachmentCell:cell];
142 [entryString appendString:@" "];
143 [entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
144 [attachment release];
151 return [entry autorelease];
155 - (void)toggleMetaContactExpansion:(id)sender
157 AIListObject *listObject = [[adium menuController] currentContextMenuObject];
158 if ([listObject isKindOfClass:[AIMetaContact class]]) {
159 BOOL currentlyExpandable = [(AIMetaContact *)listObject isExpandable];
161 if (currentlyExpandable) {
162 [[adium notificationCenter] postNotificationName:AIPerformCollapseItemNotification
164 [(AIMetaContact *)listObject setExpandable:NO];
167 [(AIMetaContact *)listObject setExpandable:YES];
168 [[adium notificationCenter] postNotificationName:AIPerformExpandItemNotification
174 - (void)menu:(NSMenu *)menu needsUpdateForMenuItem:(NSMenuItem *)menuItem
176 AIListObject *listObject = [[adium menuController] currentContextMenuObject];
177 if (menuItem == contextualMenuItem) {
178 if ([listObject isKindOfClass:[AIMetaContact class]]) {
179 BOOL currentlyExpandable = [(AIMetaContact *)listObject isExpandable];
180 if (currentlyExpandable) {
181 [menuItem setTitle:COLLAPSE_CONTACT];
183 [menuItem setTitle:EXPAND_CONTACT];
186 [[menuItem menu] removeItem:menuItem];