Adium

Ticket #1117: huh2.diff

File huh2.diff, 10.6 kB (added by kiel, 3 years ago)

Same patch riddled with NSLogs

  • /Users/kiel/adium/Source/ESBlockingPlugin.m

    old new  
    1717#import "AIAccountController.h" 
    1818#import "AIContactController.h" 
    1919#import "AIMenuController.h" 
     20#import "AIToolbarController.h" 
     21#import "AIInterfaceController.h" 
     22#import "AIChatController.h" 
    2023#import "ESBlockingPlugin.h" 
    2124#import <AIUtilities/AIMenuAdditions.h> 
     25#import <AIUtilities/AIToolbarUtilities.h> 
    2226#import <Adium/AIAccount.h> 
    2327#import <Adium/AIListContact.h> 
    2428#import <Adium/AIMetaContact.h> 
     29#import <Adium/AIChat.h> 
    2530 
    2631#define BLOCK_CONTACT   AILocalizedString(@"Block","Block Contact menu item") 
    2732#define UNBLOCK_CONTACT AILocalizedString(@"Unblock","Unblock Contact menu item") 
    2833 
     34NSString * TOOLBAR_ITEM_IDENTIFIER = @"chatItem"; 
     35 
    2936@interface ESBlockingPlugin(PRIVATE) 
    3037- (void)_blockContact:(AIListContact *)contact unblock:(BOOL)unblock; 
    3138- (BOOL)_searchPrivacyListsForListContact:(AIListContact *)contact withDesiredResult:(BOOL)desiredResult; 
    3239- (void)accountConnected:(NSNotification *)notification; 
     40- (BOOL)allContactsBlocked:(NSArray *)contacts; 
     41- (IBAction)blockOrUnblockParticipants:(NSToolbarItem *)senderItem; 
     42 
     43//protocols 
     44- (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent; 
     45//- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem; 
     46 
     47//notifications 
     48- (void)chatDidBecomeVisible:(NSNotification *)notification; 
     49- (void)toolbarWillAddItem:(NSNotification *)notification; 
     50- (void)toolbarDidRemoveItem:(NSNotification *)notification; 
     51 
     52//toolbar item methods 
     53- (void)updateToolbarIconOfChat:(AIChat *)inChat inWindow:(NSWindow *)window; 
     54- (void)updateToolbarItem:(NSToolbarItem *)item forChat:(AIChat *)chat; 
     55- (void)updateToolbarItemForObject:(AIListObject *)inObject; 
    3356@end 
    3457 
     58#pragma mark - 
    3559@implementation ESBlockingPlugin 
    3660 
    3761- (void)installPlugin 
     
    5579                                                                   selector:@selector(accountConnected:) 
    5680                                                                           name:ACCOUNT_CONNECTED 
    5781                                                                         object:nil]; 
     82         
     83        //create the block toolbar item 
     84        chatToolbarItems = [[NSMutableSet alloc] init]; 
     85        NSToolbarItem   *chatItem = [AIToolbarUtilities toolbarItemWithIdentifier:TOOLBAR_ITEM_IDENTIFIER 
     86                                                                                                                                                label:BLOCK_CONTACT 
     87                                                                                                                                 paletteLabel:BLOCK_CONTACT 
     88                                                                                                                                          toolTip:BLOCK_CONTACT 
     89                                                                                                                                           target:self 
     90                                                                                                                          settingSelector:@selector(setImage:) 
     91                                                                                                                                  itemContent:[NSImage imageNamed:@"remove.png"] 
     92                                                                                                                                           action:@selector(blockOrUnblockParticipants:) 
     93                                                                                                                                                 menu:nil]; 
     94        [[adium toolbarController] registerToolbarItem:chatItem forToolbarType:@"MessageWindow"]; 
     95         
     96        [[NSNotificationCenter defaultCenter] addObserver:self 
     97                                                                                         selector:@selector(toolbarWillAddItem:) 
     98                                                                                                 name:NSToolbarWillAddItemNotification 
     99                                                                                           object:nil]; 
     100        [[NSNotificationCenter defaultCenter] addObserver:self 
     101                                                                                         selector:@selector(toolbarDidRemoveItem:) 
     102                                                                                                 name:NSToolbarDidRemoveItemNotification 
     103                                                                                           object:nil]; 
     104        [[adium contactController] registerListObjectObserver:self]; 
    58105} 
    59106 
    60107- (void)uninstallPlugin 
    61108{ 
    62109        [[adium notificationCenter] removeObserver:self]; 
     110        [[adium contactController] unregisterListObjectObserver:self]; 
     111        [[NSNotificationCenter defaultCenter] removeObserver:self]; 
     112        [chatToolbarItems release]; 
    63113        [blockContactMenuItem release]; 
    64114        [blockContactContextualMenuItem release]; 
    65115} 
    66116 
     117/*! 
     118 * @brief Block or unblock contacts 
     119 * 
     120 * @param contacts The contacts to block or unblock 
     121 * @param block Flag indicating what the operation should achieve: NO for unblock, YES for block. 
     122 */ 
     123- (void)contacts:(NSArray *)contacts blockOrUnblock:(BOOL)block 
     124{ 
     125        NSEnumerator    *contactEnumerator = [contacts objectEnumerator]; 
     126        AIListContact   *currentContact = nil; 
     127         
     128        while ((currentContact = [contactEnumerator nextObject])) { 
     129                if ([currentContact isBlocked] != block) { 
     130                        [currentContact setIsBlocked:block updateList:YES]; 
     131                } 
     132        } 
     133} 
    67134 
    68135- (IBAction)blockContact:(id)sender 
    69136{ 
     
    192259        return NO; 
    193260} 
    194261 
     262#pragma mark - 
    195263#pragma mark Private 
    196264//Private -------------------------------------------------------------------------------------------------------------- 
    197265 
     
    258326        } 
    259327} 
    260328 
     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 
    261534@end