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 <Adium/AIAccountControllerProtocol.h>
18 #import <Adium/AIContactControllerProtocol.h>
19 #import <Adium/AIInterfaceControllerProtocol.h>
20 #import "AIContactListEditorPlugin.h"
21 #import <Adium/AIMenuControllerProtocol.h>
22 #import "AINewContactWindowController.h"
23 #import "AINewGroupWindowController.h"
24 #import <Adium/AIToolbarControllerProtocol.h>
25 #import <AIUtilities/AIMenuAdditions.h>
26 #import <AIUtilities/AIToolbarUtilities.h>
27 #import <AIUtilities/AIImageAdditions.h>
28 #import <AIUtilities/AIStringAdditions.h>
29 #import <Adium/AIAccount.h>
30 #import <Adium/AIChat.h>
31 #import <Adium/AIListContact.h>
32 #import <Adium/AIListObject.h>
33 #import <Adium/AIListGroup.h>
34 #import <Adium/AIMetaContact.h>
36 #define ADD_BOOKMARK @"Add Bookmark"
37 #define ADD_BOOKMARK_ELLIPSIS [ADD_BOOKMARK stringByAppendingEllipsis]
39 #define ADD_CONTACT AILocalizedString(@"Add Contact",nil)
40 #define ADD_CONTACT_ELLIPSIS [ADD_CONTACT stringByAppendingEllipsis]
42 #define ADD_CONTACT_TO_GROUP AILocalizedString(@"Add Contact To Group",nil)
43 #define ADD_CONTACT_TO_GROUP_ELLIPSIS [ADD_CONTACT_TO_GROUP stringByAppendingEllipsis]
45 #define ADD_GROUP AILocalizedString(@"Add Group",nil)
46 #define ADD_GROUP_ELLIPSIS [ADD_GROUP stringByAppendingEllipsis]
48 #define DELETE_CONTACT_ELLIPSIS [AILocalizedString(@"Remove Contact",nil) stringByAppendingEllipsis]
49 #define DELETE_CONTACT_CONTEXT_ELLIPSIS [AILocalizedString(@"Remove",nil) stringByAppendingEllipsis]
51 #define RENAME_GROUP AILocalizedString(@"Rename Group",nil)
52 #define RENAME_GROUP_ELLIPSIS [RENAME_GROUP stringByAppendingEllipsis]
54 #define ADD_CONTACT_IDENTIFIER @"AddContact"
55 #define ADD_GROUP_IDENTIFIER @"AddGroup"
59 @interface AIContactListEditorPlugin (PRIVATE)
60 - (void)deleteFromArray:(NSArray *)array;
61 - (void)promptForNewContactOnWindow:(NSWindow *)inWindow selectedListObject:(AIListObject *)inListObject;
62 -(void)promptForNewBookmarkOnWindow:(NSWindow*)window;
66 * @class AIContactListEditorPlugin
67 * @brief Component for managing adding and deleting contacts and groups
69 @implementation AIContactListEditorPlugin
77 NSToolbarItem *toolbarItem;
80 this piece of code is not used for the time being - may implement at later date -eb
83 menuItem_addBookmark = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:ADD_BOOKMARK_ELLIPSIS
85 action:@selector(addBookmark:)
88 [[adium menuController] addMenuItem:menuItem_addBookmark toLocation:LOC_Contact_Manage];
91 menuItem_addContact = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:ADD_CONTACT_ELLIPSIS
93 action:@selector(addContact:)
95 [[adium menuController] addMenuItem:menuItem_addContact toLocation:LOC_Contact_Manage];
97 menuItem_addContactContext = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:ADD_CONTACT_TO_GROUP_ELLIPSIS
99 action:@selector(addContact:)
101 [[adium menuController] addContextualMenuItem:menuItem_addContactContext toLocation:Context_Group_Manage];
103 menuItem_tabAddContact = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:ADD_CONTACT_ELLIPSIS
105 action:@selector(addContactFromTab:)
106 keyEquivalent:@""] autorelease];
107 [[adium menuController] addContextualMenuItem:menuItem_tabAddContact toLocation:Context_Contact_Stranger_ChatAction];
109 [[adium notificationCenter] addObserver:self
110 selector:@selector(addContactRequest:)
111 name:Contact_AddNewContact
115 menuItem_addGroup = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:ADD_GROUP_ELLIPSIS
117 action:@selector(addGroup:)
119 [[adium menuController] addMenuItem:menuItem_addGroup toLocation:LOC_Contact_Manage];
122 menuItem_delete = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:DELETE_CONTACT_ELLIPSIS
124 action:@selector(deleteSelection:)
125 keyEquivalent:@"\b"];
126 [[adium menuController] addMenuItem:menuItem_delete toLocation:LOC_Contact_Manage];
128 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:DELETE_CONTACT_CONTEXT_ELLIPSIS
130 action:@selector(deleteSelectionFromTab:)
131 keyEquivalent:@""] autorelease];
132 [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_Contact_NegativeAction];
134 //Add Contact toolbar item
135 toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:ADD_CONTACT_IDENTIFIER
137 paletteLabel:ADD_CONTACT
138 toolTip:AILocalizedString(@"Add a new contact",nil)
140 settingSelector:@selector(setImage:)
141 itemContent:[NSImage imageNamed:@"AddContact" forClass:[self class] loadLazily:YES]
142 action:@selector(addContact:)
144 [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"ListObject"];
146 //Add Group toolbar item
147 toolbarItem = [AIToolbarUtilities toolbarItemWithIdentifier:ADD_GROUP_IDENTIFIER
149 paletteLabel:ADD_GROUP
150 toolTip:AILocalizedString(@"Add a new group",nil)
152 settingSelector:@selector(setImage:)
153 itemContent:[NSImage imageNamed:@"addGroup" forClass:[self class] loadLazily:YES]
154 action:@selector(addGroup:)
156 [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"ContactList"];
159 // menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:RENAME_GROUP_ELLIPSIS
161 // action:@selector(renameGroup:)
162 // keyEquivalent:@""] autorelease];
163 // [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_Group_Manage];
169 - (void)uninstallPlugin
171 [[adium notificationCenter] removeObserver:self];
175 * @brief Validate our menu items
177 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
179 if (menuItem == menuItem_delete) {
180 return [[adium interfaceController] selectedListObjectInContactList] != nil;
182 } else if (menuItem == menuItem_tabAddContact) {
183 return [[adium menuController] currentContextMenuObject] != nil;
185 } else if (menuItem == menuItem_addGroup || menuItem == menuItem_addContact || menuItem == menuItem_addContactContext) {
186 NSEnumerator *enumerator = [[[adium accountController] accounts] objectEnumerator];
189 while ((account = [enumerator nextObject])) {
190 if ([account contactListEditable]) return YES;
195 } else if (menuItem == menuItem_addBookmark) {
196 return [[[adium interfaceController] activeChat] isGroupChat];
202 //Called by a context menu
203 - (IBAction)renameGroup:(id)sender
205 // AIListObject *object = [[adium menuController] currentContextMenuObject];
206 //<renameGroup> : I wish I worked... :(
208 //Add Bookmark ----------------------------------------------------------------------------------------------------------
209 #pragma mark Add Bookmark
211 * @brief Promp for a new bookmark
213 - (IBAction)addBookmark:(id)sender
215 [self promptForNewBookmarkOnWindow:nil];
218 -(void)promptForNewBookmarkOnWindow:(NSWindow*)window
220 NSLog(@"addBookmark");
222 //Add Contact ----------------------------------------------------------------------------------------------------------
223 #pragma mark Add Contact
225 * @brief Prompt for a new contact
227 - (IBAction)addContact:(id)sender
229 [self promptForNewContactOnWindow:nil selectedListObject:[[adium interfaceController] selectedListObject]];
234 * @brief Prompt for a new contact with the current tab's name
236 - (IBAction)addContactFromTab:(id)sender
238 [self promptForNewContactOnWindow:nil selectedListObject:[[adium menuController] currentContextMenuObject]];
242 * @brief Prompt for a new contact
244 * @param inWindow If non-nil, display the new contact prompt as a sheet on inWindow
245 * @param inListObject If a contact and a stranger, will be autofilled into the new contact window
247 - (void)promptForNewContactOnWindow:(NSWindow *)inWindow selectedListObject:(AIListObject *)inListObject
249 //We only autofill if the selected list object is a contact and a stranger
250 if (![inListObject isKindOfClass:[AIListContact class]] || [(AIListContact *)inListObject isIntentionallyNotAStranger] ||
251 [inListObject isKindOfClass:[AIMetaContact class]]) {
255 [AINewContactWindowController promptForNewContactOnWindow:inWindow
256 name:(inListObject ? [inListObject UID] : nil)
257 service:(inListObject ? [(AIListContact *)inListObject service] : nil)
262 * @brief Add contact request notification
264 * Display the add contact window. Triggered by an incoming Contact_AddNewContact notification
265 * @param notification Notification with a userInfo containing @"UID" and @"Service" keys
267 - (void)addContactRequest:(NSNotification *)notification
269 NSDictionary *userInfo = [notification userInfo];
271 [AINewContactWindowController promptForNewContactOnWindow:nil
272 name:[userInfo objectForKey:@"UID"]
273 service:[userInfo objectForKey:@"AIService"]
274 account:[userInfo objectForKey:@"AIAccount"]];
279 //Add Group ------------------------------------------------------------------------------------------------------------
280 #pragma mark Add Group
282 * @brief Prompt for a new group
284 - (IBAction)addGroup:(id)sender
286 [AINewGroupWindowController promptForNewGroupOnWindow:nil];
290 //Delete Selection -----------------------------------------------------------------------------------------------------
291 #pragma mark Delete Selection
293 * @brief Delete the list objects selected in the contact list
295 - (IBAction)deleteSelection:(id)sender
297 [self deleteFromArray:[[adium interfaceController] arrayOfSelectedListObjectsInContactList]];
301 * @brief Delete the list object associated with the current context menu
303 - (IBAction)deleteSelectionFromTab:(id)sender
305 AIListObject *currentContextMenuObject;
306 if ((currentContextMenuObject = [[adium menuController] currentContextMenuObject])) {
307 [self deleteFromArray:[NSArray arrayWithObject:currentContextMenuObject]];
312 * @brief Delete an array of <tt>AIListObject</tt>s
314 * After a modal confirmation prompt, the objects in the array are deleted.
316 * @param array An <tt>NSArray</tt> of <tt>AIListObject</tt>s.
318 - (void)deleteFromArray:(NSArray *)array
322 int count = [array count];
325 AIListObject *listObject = [array objectAtIndex:0];
326 NSString *name = [listObject displayName];
327 if ([listObject isKindOfClass:[AIListGroup class]]) {
328 message = [NSString stringWithFormat:AILocalizedString(@"This will remove the group \"%@\" from the contact lists of your online accounts. The %i contacts within this group will also be removed.\n\nThis action can not be undone.",nil),
330 [(AIListGroup *)listObject containedObjectsCount]];
333 message = [NSString stringWithFormat:AILocalizedString(@"This will remove %@ from the contact lists of your online accounts.",nil), name];
336 BOOL containsGroup = NO;
337 NSEnumerator *enumerator = [array objectEnumerator];
338 AIListObject *listObject;
340 while ((listObject = [enumerator nextObject]) && !containsGroup) {
341 containsGroup = [listObject isKindOfClass:[AIListGroup class]];
345 message = [NSString stringWithFormat:AILocalizedString(@"This will remove %i items from the contact lists of your online accounts. Contacts in any deleted groups will also be removed.\n\nThis action can not be undone.",nil), count];
347 message = [NSString stringWithFormat:AILocalizedString(@"This will remove %i contacts from the contact lists of your online accounts.",nil), count];
351 //Make sure we're in the front so our prompt is visible
352 [NSApp activateIgnoringOtherApps:YES];
354 //Guard deletion with a warning prompt
355 int result = NSRunAlertPanel(AILocalizedString(@"Remove from list?",nil),
357 AILocalizedString(@"Remove",nil),
358 AILocalizedString(@"Cancel",nil),
361 if (result == NSAlertDefaultReturn) {
362 [[adium contactController] removeListObjects:array];