| | 329 | /*! |
|---|
| | 330 | * @brief Determine if all the referenced contacts are blocked or unblocked |
|---|
| | 331 | * |
|---|
| | 332 | * @param contacts The contacts to query |
|---|
| | 333 | * @result A flag indicating if all the contacts are blocked or not |
|---|
| | 334 | */ |
|---|
| | 335 | - (BOOL)allContactsBlocked:(NSArray *)contacts |
|---|
| | 336 | { |
|---|
| | 337 | NSEnumerator *contactEnumerator = [contacts objectEnumerator]; |
|---|
| | 338 | AIListContact *currentContact = nil; |
|---|
| | 339 | BOOL allContactsBlocked = YES; |
|---|
| | 340 | |
|---|
| | 341 | //for each contact in the array |
|---|
| | 342 | while ((currentContact = [contactEnumerator nextObject])) { |
|---|
| | 343 | |
|---|
| | 344 | //if the contact is unblocked, then all the contacts in the array aren't blocked |
|---|
| | 345 | if (![currentContact isBlocked]) { |
|---|
| | 346 | allContactsBlocked = NO; |
|---|
| | 347 | break; |
|---|
| | 348 | } |
|---|
| | 349 | } |
|---|
| | 350 | |
|---|
| | 351 | return allContactsBlocked; |
|---|
| | 352 | } |
|---|
| | 353 | |
|---|
| | 354 | /*! |
|---|
| | 355 | * @brief Block or unblock participants of the active chat in a chat window |
|---|
| | 356 | * |
|---|
| | 357 | * If all the participants of the chat are blocked, attempt to unblock each |
|---|
| | 358 | * Else, attempt to block those that are not already blocked. |
|---|
| | 359 | * Then, Update the item for the chat. |
|---|
| | 360 | * |
|---|
| | 361 | * We have to do it this way because a user can (un)block participants of |
|---|
| | 362 | * a chat window in the background by command-clicking the toolbar item. |
|---|
| | 363 | * |
|---|
| | 364 | * @param senderItem The toolbar item that received the event |
|---|
| | 365 | */ |
|---|
| | 366 | - (IBAction)blockOrUnblockParticipants:(NSToolbarItem *)senderItem |
|---|
| | 367 | { |
|---|
| | 368 | NSLog(@"I am being called blockOrUnblockParticipants:"); |
|---|
| | 369 | NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator]; |
|---|
| | 370 | NSWindow *currentWindow = nil; |
|---|
| | 371 | NSToolbar *windowToolbar = nil; |
|---|
| | 372 | NSToolbar *senderToolbar = [senderItem toolbar]; |
|---|
| | 373 | NSLog(@"senderToolbar:%@ senderItem:%@", senderToolbar, senderItem); |
|---|
| | 374 | AIChat *activeChatInWindow = nil; |
|---|
| | 375 | NSArray *participants = nil; |
|---|
| | 376 | |
|---|
| | 377 | //for each open window |
|---|
| | 378 | while ((currentWindow = [windowEnumerator nextObject])) { |
|---|
| | 379 | |
|---|
| | 380 | //if it has a toolbar |
|---|
| | 381 | if ((windowToolbar = [currentWindow toolbar])) { |
|---|
| | 382 | |
|---|
| | 383 | NSLog(@"the toolbar for window %@ is %@ == %@", currentWindow, [currentWindow toolbar], senderToolbar); |
|---|
| | 384 | |
|---|
| | 385 | //look for the sender in the current toolbar |
|---|
| | 386 | if ((windowToolbar == senderToolbar) || ([[windowToolbar items] containsObject:senderItem])) { |
|---|
| | 387 | NSLog(@"sender is in toolbar"); |
|---|
| | 388 | activeChatInWindow = [[adium interfaceController] activeChatInWindow:currentWindow]; |
|---|
| | 389 | participants = [activeChatInWindow participatingListObjects]; |
|---|
| | 390 | |
|---|
| | 391 | //do the deed |
|---|
| | 392 | [self contacts:participants blockOrUnblock:(![self allContactsBlocked:participants])]; |
|---|
| | 393 | [self updateToolbarItem:senderItem forChat:activeChatInWindow]; |
|---|
| | 394 | break; |
|---|
| | 395 | } |
|---|
| | 396 | } |
|---|
| | 397 | } |
|---|
| | 398 | } |
|---|
| | 399 | |
|---|
| | 400 | #pragma mark - |
|---|
| | 401 | #pragma mark Protocols |
|---|
| | 402 | |
|---|
| | 403 | /*! |
|---|
| | 404 | * @brief Update any chat with the list object |
|---|
| | 405 | * |
|---|
| | 406 | * If the list object is (un)blocked, update any chats that we my have open with it. |
|---|
| | 407 | */ |
|---|
| | 408 | - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent |
|---|
| | 409 | { |
|---|
| | 410 | if ([inModifiedKeys containsObject:@"isBlocked"]) { |
|---|
| | 411 | [self updateToolbarItemForObject:inObject]; |
|---|
| | 412 | } |
|---|
| | 413 | |
|---|
| | 414 | return nil; |
|---|
| | 415 | } |
|---|
| | 416 | |
|---|
| | 417 | #pragma mark - |
|---|
| | 418 | #pragma mark Notifications |
|---|
| | 419 | |
|---|
| | 420 | /*! |
|---|
| | 421 | * @brief Toolbar has added an instance of the chat block toolbar item |
|---|
| | 422 | */ |
|---|
| | 423 | - (void)toolbarWillAddItem:(NSNotification *)notification |
|---|
| | 424 | { |
|---|
| | 425 | NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"]; |
|---|
| | 426 | |
|---|
| | 427 | if ([[item itemIdentifier] isEqualToString:TOOLBAR_ITEM_IDENTIFIER]) { |
|---|
| | 428 | |
|---|
| | 429 | //If this is the first item added, start observing for chats becoming visible so we can update the item |
|---|
| | 430 | if ([chatToolbarItems count] == 0) { |
|---|
| | 431 | [[adium notificationCenter] addObserver:self |
|---|
| | 432 | selector:@selector(chatDidBecomeVisible:) |
|---|
| | 433 | name:@"AIChatDidBecomeVisible" |
|---|
| | 434 | object:nil]; |
|---|
| | 435 | } |
|---|
| | 436 | |
|---|
| | 437 | [chatToolbarItems addObject:item]; |
|---|
| | 438 | } |
|---|
| | 439 | } |
|---|
| | 440 | |
|---|
| | 441 | /*! |
|---|
| | 442 | * @brief A toolbar item was removed |
|---|
| | 443 | */ |
|---|
| | 444 | - (void)toolbarDidRemoveItem:(NSNotification *)notification |
|---|
| | 445 | { |
|---|
| | 446 | NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"]; |
|---|
| | 447 | [chatToolbarItems removeObject:item]; |
|---|
| | 448 | |
|---|
| | 449 | if ([chatToolbarItems count] == 0) { |
|---|
| | 450 | [[adium notificationCenter] removeObserver:self |
|---|
| | 451 | name:@"AIChatDidBecomeVisible" |
|---|
| | 452 | object:nil]; |
|---|
| | 453 | } |
|---|
| | 454 | } |
|---|
| | 455 | |
|---|
| | 456 | /*! |
|---|
| | 457 | * @brief A chat became visible in a window. |
|---|
| | 458 | * |
|---|
| | 459 | * Update the window's (un)block toolbar item to reflect the block state of a list object |
|---|
| | 460 | * |
|---|
| | 461 | * @param notification Notification with an AIChat object and an @"NSWindow" userInfo key |
|---|
| | 462 | */ |
|---|
| | 463 | - (void)chatDidBecomeVisible:(NSNotification *)notification |
|---|
| | 464 | { |
|---|
| | 465 | //NSLog(@"chat did become visible: %@", notification); |
|---|
| | 466 | [self updateToolbarIconOfChat:[notification object] |
|---|
| | 467 | inWindow:[[notification userInfo] objectForKey:@"NSWindow"]]; |
|---|
| | 468 | } |
|---|
| | 469 | |
|---|
| | 470 | #pragma mark - |
|---|
| | 471 | #pragma mark Toolbar Item Update Methods |
|---|
| | 472 | |
|---|
| | 473 | /*! |
|---|
| | 474 | * @brief Update the toolbar icon in a chat for a particular contact |
|---|
| | 475 | * |
|---|
| | 476 | * @param inObject The list object we want to update the toolbar item for |
|---|
| | 477 | */ |
|---|
| | 478 | - (void)updateToolbarItemForObject:(AIListObject *)inObject |
|---|
| | 479 | { |
|---|
| | 480 | AIChat *chat = nil; |
|---|
| | 481 | NSWindow *window = nil; |
|---|
| | 482 | |
|---|
| | 483 | //Update the icon in the toolbar for this contact if a chat is open and we have any toolbar items |
|---|
| | 484 | if (([chatToolbarItems count] > 0) && |
|---|
| | 485 | [inObject isKindOfClass:[AIListContact class]] && |
|---|
| | 486 | (chat = [[adium chatController] existingChatWithContact:(AIListContact *)inObject]) && |
|---|
| | 487 | (window = [[adium interfaceController] windowForChat:chat])) { |
|---|
| | 488 | [self updateToolbarIconOfChat:chat |
|---|
| | 489 | inWindow:window]; |
|---|
| | 490 | } |
|---|
| | 491 | } |
|---|
| | 492 | |
|---|
| | 493 | /*! |
|---|
| | 494 | * @brief Update the toolbar item for the particpants of a particular chat |
|---|
| | 495 | * |
|---|
| | 496 | * @param item The toolbar item to modify |
|---|
| | 497 | * @param chat The chat for which the participants are participating in |
|---|
| | 498 | */ |
|---|
| | 499 | - (void)updateToolbarItem:(NSToolbarItem *)item forChat:(AIChat *)chat |
|---|
| | 500 | { |
|---|
| | 501 | if ([self allContactsBlocked:[chat participatingListObjects]]) { |
|---|
| | 502 | //assume unblock appearance |
|---|
| | 503 | [item setLabel:UNBLOCK_CONTACT]; |
|---|
| | 504 | [item setPaletteLabel:UNBLOCK_CONTACT]; |
|---|
| | 505 | } else { |
|---|
| | 506 | //assume block appearance |
|---|
| | 507 | [item setLabel:BLOCK_CONTACT]; |
|---|
| | 508 | [item setPaletteLabel:BLOCK_CONTACT]; |
|---|
| | 509 | } |
|---|
| | 510 | |
|---|
| | 511 | NSLog(@"%@", [item view]); |
|---|
| | 512 | } |
|---|
| | 513 | |
|---|
| | 514 | /*! |
|---|
| | 515 | * @brief Update the (un)block toolbar icon in a chat |
|---|
| | 516 | * |
|---|
| | 517 | * @param chat The chat with the participants |
|---|
| | 518 | * @param window The window in which the chat resides |
|---|
| | 519 | */ |
|---|
| | 520 | - (void)updateToolbarIconOfChat:(AIChat *)chat inWindow:(NSWindow *)window |
|---|
| | 521 | { |
|---|
| | 522 | NSToolbar *toolbar = [window toolbar]; |
|---|
| | 523 | NSEnumerator *enumerator = [[toolbar items] objectEnumerator]; |
|---|
| | 524 | NSToolbarItem *item; |
|---|
| | 525 | |
|---|
| | 526 | while ((item = [enumerator nextObject])) { |
|---|
| | 527 | if ([[item itemIdentifier] isEqualToString:TOOLBAR_ITEM_IDENTIFIER]) { |
|---|
| | 528 | [self updateToolbarItem:item forChat:chat]; |
|---|
| | 529 | break; |
|---|
| | 530 | } |
|---|
| | 531 | } |
|---|
| | 532 | } |
|---|
| | 533 | |
|---|