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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | /*
* 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/AIAccountControllerProtocol.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import "AINewContactWindowController.h"
#import "AINewGroupWindowController.h"
#import "OWABSearchWindowController.h"
#import <Adium/AIAddressBookController.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIPopUpButtonAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <Adium/AIAccount.h>
#import <Adium/AIListContact.h>
#import <Adium/AIListGroup.h>
#import <Adium/AILocalizationTextField.h>
#import <Adium/AIService.h>
#import <Adium/AIServiceIcons.h>
#import <Adium/AIServiceMenu.h>
#import <AddressBook/ABPerson.h>
#define ADD_CONTACT_PROMPT_NIB @"AddContact"
#define DEFAULT_GROUP_NAME AILocalizedString(@"Contacts",nil)
@interface AINewContactWindowController ()
- (id)initWithWindowNibName:(NSString *)windowNibName contactName:(NSString *)inName service:(AIService *)inService account:(AIAccount *)inAccount;
- (void)buildGroupMenu;
- (void)buildContactTypeMenu;
- (void)configureForCurrentServiceType;
- (void)ensureValidContactTypeSelection;
- (void)updateAccountList;
- (void)_setServiceType:(AIService *)inService;
- (void)selectServiceType:(id)sender;
- (void)configureControlDimming;
@end
/*!
* @class AINewContactWindowController
* @brief Window controller for adding a new contact
*/
@implementation AINewContactWindowController
/*!
* @brief Prompt for adding a new contact.
*
* @param parentWindow Window on which to show the prompt as a sheet. Pass nil for a panel prompt.
* @param inName Initial value for the contact name field
* @param inService <tt>AIService</tt> for determining the initial service type selection
* @param inAccount If non-nil, the one AIAccount which should be initially enabled for adding this contact
*/
+ (void)promptForNewContactOnWindow:(NSWindow *)parentWindow name:(NSString *)inName service:(AIService *)inService account:(AIAccount *)inAccount
{
AINewContactWindowController *newContactWindow;
newContactWindow = [[self alloc] initWithWindowNibName:ADD_CONTACT_PROMPT_NIB contactName:inName service:inService account:inAccount];
if (parentWindow) {
[parentWindow makeKeyAndOrderFront:nil];
[NSApp beginSheet:[newContactWindow window]
modalForWindow:parentWindow
modalDelegate:newContactWindow
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:nil];
} else {
[newContactWindow showWindow:nil];
[[newContactWindow window] makeKeyAndOrderFront:nil];
}
}
/*!
* @brief Initialize
*/
- (id)initWithWindowNibName:(NSString *)windowNibName contactName:(NSString *)inName service:(AIService *)inService account:(AIAccount *)inAccount
{
if ((self = [super initWithWindowNibName:windowNibName])) {
service = [inService retain];
initialAccount = [inAccount retain];
contactName = [inName retain];
person = nil;
}
return self;
}
/*!
* @brief Deallocate
*/
- (void)dealloc
{
[accounts release];
[contactName release];
[service release];
[initialAccount release];
[person release];
[checkedAccounts release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
/*!
* @brief Setup the window before it is displayed
*/
- (void)windowDidLoad
{
[[self window] center];
//Localized Strings
[[self window] setTitle:AILocalizedString(@"Add Contact",nil)];
[textField_type setLocalizedString:AILocalizedString(@"Contact Type:","Contact type service dropdown label in Add Contact")];
[textField_alias setLocalizedString:AILocalizedString(@"Alias:",nil)];
[textField_inGroup setLocalizedString:AILocalizedString(@"In Group:",nil)];
[textField_addToAccounts setLocalizedString:AILocalizedString(@"On Accounts:",nil)];
[textField_searchInAB setAlwaysMoveRightAnchoredWindow:YES];
[textField_searchInAB setLocalizedString:AILocalizedString(@"Search In Address Book",nil)];
[button_add setLocalizedString:AILocalizedString(@"Add",nil)];
[button_cancel setLocalizedString:AILocalizedString(@"Cancel",nil)];
//Configure the rest of the window
[self buildGroupMenu];
[self buildContactTypeMenu];
[self configureForCurrentServiceType];
if (contactName) [textField_contactName setStringValue:contactName];
//Observe account list and status changes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(accountListChanged:)
name:Account_ListChanged
object:nil];
[[AIContactObserverManager sharedManager] registerListObjectObserver:self];
[self configureControlDimming];
}
/*!
* @brief Window is closing
*/
- (void)windowWillClose:(id)sender
{
[super windowWillClose:sender];
[[AIContactObserverManager sharedManager] unregisterListObjectObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*!
* @brief Called as the user list edit sheet closes, dismisses the sheet
*/
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[sheet orderOut:nil];
}
/*!
* @brief Cancel
*/
- (IBAction)cancel:(id)sender
{
[self closeWindow:nil];
}
/*!
* @brief Perform the addition of the contact
*/
- (IBAction)addContact:(id)sender
{
NSString *UID = [service normalizeUID:[textField_contactName stringValue] removeIgnoredCharacters:YES];
NSString *alias = [textField_contactAlias stringValue];
AIListGroup *group;
AIAccount *account;
//Group
group = ([popUp_targetGroup numberOfItems] ?
[[popUp_targetGroup selectedItem] representedObject] :
nil);
if (!group) group = [adium.contactController groupWithUID:DEFAULT_GROUP_NAME];
AILogWithSignature(@"checkedAccounts is %@", checkedAccounts);
BOOL addedAtLeastOneContact = NO;
//Add contact to our accounts
for (account in accounts) {
if ([account contactListEditable] && [checkedAccounts containsObject:account]) {
AILogWithSignature(@"Accont %@ was checked per its preference; we'll add %@ to it", account, UID);
AIListContact *contact = [adium.contactController contactWithService:service
account:account
UID:UID];
if (contact) {
addedAtLeastOneContact = YES;
// Set the alias. When adding to libpurple, we set the alias in the add, so we're fine adding it here.
if (alias && [alias length])
[contact setDisplayName:alias];
[account addContact:contact toGroup:group];
//Remember the ABPerson's unique ID associated with this contact
if (person)
[contact setAddressBookPerson:person];
//Force this contact to show up on the user's list for a little bit, even if it is offline
//Otherwise they have no good feedback that a contact was added at all.
[contact setValue:[NSNumber numberWithBool:YES] forProperty:@"New Object" notify:YES];
[contact setValue:[NSNumber numberWithBool:NO] forProperty:@"New Object" afterDelay:10.0];
}
}
}
if (addedAtLeastOneContact) {
[self closeWindow:nil];
} else {
NSBeep();
}
}
/*!
* @brief Display a sheet for searching a person within the AB database.
*/
- (IBAction)searchInAB:(id)sender
{
OWABSearchWindowController *abSearchWindow;
abSearchWindow = [[OWABSearchWindowController promptForNewPersonSearchOnWindow:[self window]
initialService:service] retain];
[abSearchWindow setDelegate:self];
}
/*!
* @brief Callback from OWABSearchWindowController
*/
- (void)absearchWindowControllerDidSelectPerson:(OWABSearchWindowController *)controller
{
ABPerson *selectedPerson = [controller selectedPerson];
if (selectedPerson) {
NSString *selectedScreenName = [controller selectedScreenName];
NSString *selectedName = [controller selectedName];
AIService *selectedService = [controller selectedService];
if (selectedScreenName)
[textField_contactName setStringValue:[service normalizeUID:selectedScreenName removeIgnoredCharacters:YES]];
if (selectedName)
[textField_contactAlias setStringValue:selectedName];
if (selectedService) {
[popUp_contactType selectItemWithTitle:[selectedService shortDescription]];
[self selectServiceType:nil];
}
[person release];
person = [selectedPerson retain];
[self configureControlDimming];
}
//Clean up
[controller release];
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
if ([aNotification object] == textField_contactName) {
[self configureControlDimming];
}
}
//Service Type ---------------------------------------------------------------------------------------------------------
#pragma mark Service Type
/*!
* @brief Build and configure the menu of contact service types
*/
- (void)buildContactTypeMenu
{
//Rebuild the menu
[popUp_contactType setMenu:[AIServiceMenu menuOfServicesWithTarget:self
activeServicesOnly:YES
longDescription:NO
format:nil]];
//Ensure our selection is still valid
[self ensureValidContactTypeSelection];
}
/*!
* @brief Ensures that the selected contact type is valid, selecting another if it isn't
*/
- (void)ensureValidContactTypeSelection
{
NSInteger serviceIndex = -1;
//Force our menu to update.. it needs to be correctly validated for the code below to work
[[popUp_contactType menu] update];
//Find the menu item for our current service
if (service) serviceIndex = [popUp_contactType indexOfItemWithRepresentedObject:service];
//If our service is not available we'll have to pick another one
if (service && (serviceIndex == -1 || ![[popUp_contactType itemAtIndex:serviceIndex] isEnabled])) {
[self _setServiceType:nil];
}
//If we don't have a service, pick the first available one
if (!service) {
[self _setServiceType:[[[popUp_contactType menu] firstEnabledMenuItem] representedObject]];
}
//Update our menu and window for the current service
[popUp_contactType selectItemWithRepresentedObject:service];
[self configureForCurrentServiceType];
}
/*!
* @brief Configure any service-dependent controls in our window for the current service
*/
- (void)configureForCurrentServiceType
{
NSString *userNameLabel = [service contactUserNameLabel];
//Update the service icon
[imageView_service setImage:[AIServiceIcons serviceIconForService:service
type:AIServiceIconLarge
direction:AIIconNormal]];
[textField_contactNameLabel setLocalizedString:[(userNameLabel ? userNameLabel :
AILocalizedString(@"Contact ID",nil)) stringByAppendingString:AILocalizedString(@":", "Colon which will be appended after a label such as 'User Name', before an input field")]];
//And the list of accounts
[self updateAccountList];
}
/*!
* @brief User selected a new service type
*/
- (void)selectServiceType:(id)sender
{
[self _setServiceType:[[popUp_contactType selectedItem] representedObject]];
[self configureForCurrentServiceType];
}
/*!
* @brief Set the current service type
*/
- (void)_setServiceType:(AIService *)inService
{
if (inService != service) {
[service release];
service = [inService retain];
}
}
/*
* Validate a menu item
*/
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
NSEnumerator *enumerator = [[adium.accountController accountsCompatibleWithService:[menuItem representedObject]] objectEnumerator];
AIAccount *account;
while ((account = [enumerator nextObject])) {
if (account.contactListEditable) return YES;
}
return NO;
}
/*!
* @brief Update our contact type menu when user accounts change
*/
- (void)accountListChanged:(NSNotification *)notification
{
[self buildContactTypeMenu];
}
/*!
* @brief Update our contact type when account availability changes
*/
- (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
{
if ([inObject isKindOfClass:[AIAccount class]]) {
[self ensureValidContactTypeSelection];
}
return nil;
}
//Add to Group ---------------------------------------------------------------------------------------------------------
#pragma mark Add to Group
/*!
* @brief Build the menu of available destination groups
*/
- (void)buildGroupMenu
{
//Rebuild the menu
NSMenu *menu = [adium.contactController groupMenuWithTarget:self];
//Add a default group name to the menu if there are no groups listed
if ([menu numberOfItems] == 0) {
[menu addItemWithTitle:DEFAULT_GROUP_NAME
target:self
action:@selector(selectGroup:)
keyEquivalent:@""];
}
[menu addItem:[NSMenuItem separatorItem]];
[menu addItemWithTitle:[AILocalizedString(@"New Group",nil) stringByAppendingEllipsis]
target:self
action:@selector(newGroup:)
keyEquivalent:@""];
[popUp_targetGroup setMenu:menu];
[popUp_targetGroup selectItemAtIndex:0];
}
/*!
* @brief Prompt the user to add a new group immediately
*/
- (void)newGroup:(id)sender
{
AINewGroupWindowController *newGroupWindowController;
newGroupWindowController = [AINewGroupWindowController promptForNewGroupOnWindow:[self window]];
//Observe for the New Group window to close
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newGroupDidEnd:)
name:@"NewGroupWindowControllerDidEnd"
object:[newGroupWindowController window]];
}
- (void)newGroupDidEnd:(NSNotification *)inNotification
{
NSWindow *window = [inNotification object];
if ([[window windowController] isKindOfClass:[AINewGroupWindowController class]]) {
AIListGroup *group = [[window windowController] group];
//Rebuild the group menu
[self buildGroupMenu];
/* Select the new group if it exists; otherwise select the first group (so we don't still have New Group... selected).
* If the user cancelled, group will be nil since the group doesn't exist.
*/
if (![popUp_targetGroup selectItemWithRepresentedObject:group]) {
[popUp_targetGroup selectItemAtIndex:0];
}
[[self window] performSelector:@selector(makeKeyAndOrderFront:)
withObject:self
afterDelay:0];
}
//Stop observing
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"NewGroupWindowControllerDidEnd"
object:window];
}
//Add to Accounts ------------------------------------------------------------------------------------------------------
#pragma mark Add to Accounts
/*!
* @brief Update the accounts list
*/
- (void)updateAccountList
{
[accounts release];
accounts = [[adium.accountController accountsCompatibleWithService:service] retain];
[checkedAccounts release];
checkedAccounts = [[NSMutableSet alloc] init];
if (initialAccount && [accounts containsObject:initialAccount]) {
//Select accounts by default
[checkedAccounts addObject:initialAccount];
} else if ([[accounts valueForKeyPath:@"@sum.online"] integerValue] == 1) {
//Only one online account; it should be checked
AIAccount *anAccount;
for (anAccount in accounts) {
if (anAccount.online) {
[checkedAccounts addObject:anAccount];
break;
}
}
} else {
//More than one online account; follow our 'add contact to' preferences
AIAccount *anAccount;
for (anAccount in accounts) {
if ([[anAccount preferenceForKey:KEY_ADD_CONTACT_TO group:PREF_GROUP_ADD_CONTACT] boolValue])
[checkedAccounts addObject:anAccount];
}
}
[tableView_accounts reloadData];
}
- (void)configureControlDimming
{
BOOL shouldEnable = NO;
if (([[textField_contactName stringValue] length] > 0)) {
NSEnumerator *enumerator = [checkedAccounts objectEnumerator];
AIAccount *account;
while (!shouldEnable && (account = [enumerator nextObject]))
if (account.contactListEditable) shouldEnable = YES;
}
[button_add setEnabled:shouldEnable];
}
/*!
* @brief Rows in the accounts table view
*/
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [accounts count];
}
/*!
* @brief Object value for columns in the accounts table view
*/
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"check"]) {
return ([[accounts objectAtIndex:row] contactListEditable] ?
[NSNumber numberWithBool:[checkedAccounts containsObject:[accounts objectAtIndex:row]]] :
[NSNumber numberWithBool:NO]);
} else if ([identifier isEqualToString:@"account"]) {
return [[accounts objectAtIndex:row] explicitFormattedUID];
} else {
return @"";
}
}
/*!
* @brief Will display cell
*
* Enable/disable account checkbox as appropriate
*/
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"check"]) {
[cell setEnabled:[[accounts objectAtIndex:row] contactListEditable]];
}
}
/*!
* @brief Set the enabled/disabled state for an account in the account list
*/
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"check"]) {
[[accounts objectAtIndex:row] setPreference:[NSNumber numberWithBool:[object boolValue]]
forKey:KEY_ADD_CONTACT_TO
group:PREF_GROUP_ADD_CONTACT];
if ([object boolValue]) {
[checkedAccounts addObject:[accounts objectAtIndex:row]];
} else {
[checkedAccounts removeObject:[accounts objectAtIndex:row]];
}
[self configureControlDimming];
}
}
/*!
* @brief Empty selector called by the group popUp menu
*/
- (void)selectGroup:(id)sender
{
}
@end
|