Source/AIDockUnviewedContentPlugin.m
changeset 3670 add0c83648a5
parent 3669 7905c08bc4d8
child 3671 855cbc039a80
equal deleted inserted replaced
3669:7905c08bc4d8 3670:add0c83648a5
     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  */
       
    16 
       
    17 #import "AIDockUnviewedContentPlugin.h"
       
    18 #import <Adium/AIChatControllerProtocol.h>
       
    19 #import <Adium/AIContentControllerProtocol.h>
       
    20 #import <Adium/AIStatusControllerProtocol.h>
       
    21 #import <Adium/AIDockControllerProtocol.h>
       
    22 #import <AIUtilities/AIArrayAdditions.h>
       
    23 #import <AIUtilities/AIDictionaryAdditions.h>
       
    24 #import <Adium/AIChat.h>
       
    25 
       
    26 @interface AIDockUnviewedContentPlugin ()
       
    27 - (void)removeAlert;
       
    28 - (void)chatWillClose:(NSNotification *)notification;
       
    29 @end
       
    30 
       
    31 /*!
       
    32  * @class AIDockUnviewedContentPlugin
       
    33  * @brief Component responsible for triggering and removing the Alert dock icon state for unviewed content
       
    34  */
       
    35 @implementation AIDockUnviewedContentPlugin
       
    36 
       
    37 /*!
       
    38  * @brief Install
       
    39  */
       
    40 - (void)installPlugin
       
    41 {
       
    42     //init
       
    43     unviewedState = NO;
       
    44 
       
    45 	//Register our default preferences
       
    46     [adium.preferenceController registerDefaults:[NSDictionary dictionaryNamed:@"DockUnviewedContentDefaults"
       
    47 																		forClass:[self class]] 
       
    48 										  forGroup:PREF_GROUP_APPEARANCE];
       
    49 	
       
    50 	//Observe pref changes
       
    51 	[adium.preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_APPEARANCE];
       
    52 }
       
    53 
       
    54 /*!
       
    55  * @brief Uninstall
       
    56  */
       
    57 - (void)uninstallPlugin
       
    58 {
       
    59 	[adium.chatController unregisterChatObserver:self];
       
    60 	[[NSNotificationCenter defaultCenter] removeObserver:self];
       
    61 }
       
    62 
       
    63 #pragma mark Preference observing
       
    64 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
       
    65 							object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
       
    66 {
       
    67 	if (!key || [key isEqualToString:KEY_ANIMATE_DOCK_ICON]) {
       
    68 		BOOL newAnimateDockIcon = [[prefDict objectForKey:KEY_ANIMATE_DOCK_ICON] boolValue];
       
    69 
       
    70 		if (newAnimateDockIcon != animateDockIcon) {
       
    71 			animateDockIcon = newAnimateDockIcon;
       
    72 			
       
    73 			if (animateDockIcon) {
       
    74 				//Register as a chat observer (So we can catch the unviewed content status flag)
       
    75 				[adium.chatController registerChatObserver:self];
       
    76 				
       
    77 				[[NSNotificationCenter defaultCenter] addObserver:self
       
    78 											   selector:@selector(chatWillClose:)
       
    79 												   name:Chat_WillClose object:nil];
       
    80 				
       
    81 			} else {
       
    82 				[self removeAlert];
       
    83 
       
    84 				[adium.chatController unregisterChatObserver:self];
       
    85 				[[NSNotificationCenter defaultCenter] removeObserver:self];
       
    86 			}
       
    87 		}
       
    88 	}
       
    89 }
       
    90 /*!
       
    91  * @brief Chat was updated
       
    92  *
       
    93  * Check for whether inModifiedKeys contains a change to unviewed content. If so, put the dock in the Alert state
       
    94  * if it isn't already and there is unviewed content, or take it out of the Alert state if it is and there is none.
       
    95  *
       
    96  * The alert state, in the default dock icon set, is the Adium duck flapping its wings.
       
    97  */
       
    98 - (NSSet *)updateChat:(AIChat *)inChat keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
       
    99 {
       
   100     if ([inModifiedKeys containsObject:KEY_UNVIEWED_CONTENT]) {
       
   101         if (adium.chatController.unviewedContentCount) {
       
   102             //If this is the first contact with unviewed content, animate the dock
       
   103             if (!unviewedState) {
       
   104 				NSString *iconState;
       
   105 
       
   106 				if (([adium.statusController.activeStatusState statusType] == AIInvisibleStatusType) &&
       
   107 					[adium.dockController currentIconSupportsIconStateNamed:@"InvisibleAlert"]) {
       
   108 					iconState = @"InvisibleAlert";					
       
   109 				} else {
       
   110 					iconState = @"Alert";
       
   111 				}
       
   112 
       
   113                 [adium.dockController setIconStateNamed:iconState];
       
   114                 unviewedState = YES;
       
   115             }
       
   116         } else if (unviewedState) {
       
   117 			//If there are no more contacts with unviewed content, stop animating the dock
       
   118 			[self removeAlert];
       
   119         }
       
   120     }
       
   121 
       
   122     return nil;
       
   123 }
       
   124 
       
   125 /*!
       
   126  * @brief Remove any existing alert state
       
   127  */
       
   128 - (void)removeAlert
       
   129 {
       
   130 	[adium.dockController removeIconStateNamed:@"Alert"];
       
   131 	[[adium dockController] removeIconStateNamed:@"InvisibleAlert"];
       
   132 	unviewedState = NO;
       
   133 }
       
   134 
       
   135 /*!
       
   136  * @brief Respond to a chat closing
       
   137  *
       
   138  * Ensure that when a chat closes we remove the Alert state if necessary.
       
   139  */
       
   140 - (void)chatWillClose:(NSNotification *)notification
       
   141 {
       
   142 	if (!adium.chatController.unviewedContentCount && unviewedState) {
       
   143 		//If there are no more contacts with unviewed content, stop animating the dock
       
   144 		[self removeAlert];
       
   145 	}
       
   146 }
       
   147 
       
   148 @end