Added `-[NSArray validateAsPropertyList]` and `-[NSDictionary validateAsPropertyList...
[adiumx.git] / Source / ESMetaContactContentsPlugin.m
blob294b10c934434af8c792bad4759f2b95b21653d4
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 "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)
33 /*!
34  * @class ESMetaContactContentsPlugin
35  * @brief Tooltip component: Show the contacts contained by metaContacts, with service and status state.
36  */
37 @implementation ESMetaContactContentsPlugin
39 /*!
40  * @brief Install
41  */
42 - (void)installPlugin
44     //Install our tooltip entry
45     [[adium interfaceController] registerContactListTooltipEntry:self secondaryEntry:YES];
46         
47         contextualMenuItem = [[NSMenuItem alloc] initWithTitle:EXPAND_CONTACT
48                                                                                                         target:self
49                                                                                                         action:@selector(toggleMetaContactExpansion:)
50                                                                                          keyEquivalent:@""];
51         [[adium menuController] addContextualMenuItem:contextualMenuItem
52                                                                            toLocation:Context_Contact_Manage];
55 - (void)dealloc
57         [contextualMenuItem release]; contextualMenuItem = nil;
59         [super dealloc];
62 /*!
63  * @brief Tooltip label
64  *
65  * @result A label, or nil if no tooltip entry should be shown
66  */
67 - (NSString *)labelForObject:(AIListObject *)inObject
69         if ([inObject isKindOfClass:[AIMetaContact class]]) {
70                 return AILocalizedString(@"Contacts",nil);
71         }
72         
73         return nil;
76 /*!
77  * @brief Tooltip entry
78  *
79  * @result The tooltip entry, or nil if no tooltip should be shown
80  */
81 - (NSAttributedString *)entryForObject:(AIListObject *)inObject
83     NSMutableAttributedString   *entry = nil;
84         
85         if ([inObject isKindOfClass:[AIMetaContact class]]) {
86                 NSArray                         *listContacts = [(AIMetaContact *)inObject listContacts];
87                 
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];
98                         
99                         enumerator = [listContacts objectEnumerator];
100                         while ((contact = [enumerator nextObject])) {
101                                 NSImage *statusIcon, *serviceIcon;
102                                 
103                                 if (shouldAppendString) {
104                                         [entryString appendString:@"\r"];
105                                 } else {
106                                         shouldAppendString = YES;
107                                 }
108                                 
109                                 statusIcon = [[contact displayArrayObjectForKey:@"Tab Status Icon"] imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
110                                 
111                                 if (statusIcon) {
112                                         NSTextAttachment                *attachment;
113                                         NSTextAttachmentCell    *cell;
114                                                 
115                                         cell = [[NSTextAttachmentCell alloc] init];
116                                         [cell setImage:statusIcon];
117                                         
118                                         attachment = [[NSTextAttachment alloc] init];
119                                         [attachment setAttachmentCell:cell];
120                                         [cell release];
122                                         [entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
123                                         [attachment release];
124                                 }
125                                 
126                                 [entryString appendString:[contact formattedUID]];
127                                 
128                                 if (shouldAppendServiceIcon) {
129                                         serviceIcon = [[AIServiceIcons serviceIconForObject:contact type:AIServiceIconSmall direction:AIIconNormal]
130                                                                         imageByScalingToSize:META_TOOLTIP_ICON_SIZE];
131                                         if (serviceIcon) {
132                                                 NSTextAttachment                *attachment;
133                                                 NSTextAttachmentCell    *cell;
134                                                 
135                                                 cell = [[NSTextAttachmentCell alloc] init];
136                                                 [cell setImage:serviceIcon];
137                                                 
138                                                 attachment = [[NSTextAttachment alloc] init];
139                                                 [attachment setAttachmentCell:cell];
140                                                 [cell release];
142                                                 [entryString appendString:@" "];
143                                                 [entry appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
144                                                 [attachment release];
145                                         }
146                                 }
147                         }
148                 }
149         }
150     
151     return [entry autorelease];
154 #pragma mark Menu
155 - (void)toggleMetaContactExpansion:(id)sender
157         AIListObject *listObject = [[adium menuController] currentContextMenuObject];
158         if ([listObject isKindOfClass:[AIMetaContact class]]) {
159                 BOOL currentlyExpandable = [(AIMetaContact *)listObject isExpandable];
160                 
161                 if (currentlyExpandable) {
162                         [[adium notificationCenter] postNotificationName:AIPerformCollapseItemNotification
163                                                                                                          object:listObject];
164                         [(AIMetaContact *)listObject setExpandable:NO];
166                 } else {
167                         [(AIMetaContact *)listObject setExpandable:YES];
168                         [[adium notificationCenter] postNotificationName:AIPerformExpandItemNotification
169                                                                                                          object:listObject];
170                 }
171         }
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];
182                         } else {
183                                 [menuItem setTitle:EXPAND_CONTACT];                             
184                         }
185                 } else {
186                         [[menuItem menu] removeItem:menuItem];
187                 }
188         }
191 @end