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 "AIAccountController.h"
18 #import "AIContactController.h"
19 #import "AIDockAccountStatusPlugin.h"
20 #import "AIDockController.h"
21 #import "AIStatusController.h"
22 #import "AIPreferenceController.h"
23 #import <Adium/AIAccount.h>
24 #import <Adium/AIListObject.h>
25 #import <Adium/AIStatus.h>
27 @interface AIDockAccountStatusPlugin (PRIVATE)
28 - (BOOL)_accountsWithBoolKey:(NSString *)inKey;
29 - (BOOL)_accountsWithKey:(NSString *)inKey;
30 - (void)_updateIconForKey:(NSString *)key;
34 * @class AIDockAccountStatusPlugin
35 * @brief Maintain the dock icon state in relation to global account status
37 * This class manages the dock icon state via the dockController. It specifies the icon which should be shown based
38 * on an aggregated, global account status.
40 @implementation AIDockAccountStatusPlugin
43 * @brief Install plugin
47 //Observe account status changes
48 [[adium contactController] registerListObjectObserver:self];
50 //Observer preference changes
51 [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_GENERAL];
55 * @brief Uninstall plugin
57 - (void)uninstallPlugin
60 [[adium contactController] unregisterListObjectObserver:self];
61 [[adium preferenceController] unregisterPreferenceObserver:self];
62 [[NSNotificationCenter defaultCenter] removeObserver:self];
66 * @brief Handle preference changes
68 * When the active dock icon changes, call updateListObject:keys:silent: to update its state to the global account state
70 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
71 object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
73 if(!key || [key isEqualToString:KEY_ACTIVE_DOCK_ICON]){
74 [self updateListObject:nil keys:nil silent:NO];
79 * @brief Update the dock icon state in response to an account changing status
81 * If one ore more accounts are online, set the Online icon state. Similarly, handle the Connecting, Away, and Idle
84 - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
86 if(inObject == nil || [inObject isKindOfClass:[AIAccount class]]){
87 BOOL shouldUpdateStatus = NO;
89 if(inObject == nil || [inModifiedKeys containsObject:@"Online"]){
90 if([self _accountsWithBoolKey:@"Online"] > 0){
91 [[adium dockController] setIconStateNamed:@"Online"];
93 [[adium dockController] removeIconStateNamed:@"Online"];
95 shouldUpdateStatus = YES;
97 if(inObject == nil || [inModifiedKeys containsObject:@"Connecting"]){
98 if([self _accountsWithBoolKey:@"Connecting"] > 0){
99 [[adium dockController] setIconStateNamed:@"Connecting"];
101 [[adium dockController] removeIconStateNamed:@"Connecting"];
103 shouldUpdateStatus = YES;
106 if(inObject == nil || [inModifiedKeys containsObject:@"StatusState"]){
107 shouldUpdateStatus = YES;
110 if(inObject == nil || [inModifiedKeys containsObject:@"IdleSince"]){
111 if([self _accountsWithKey:@"IdleSince"] > 0){
112 [[adium dockController] setIconStateNamed:@"Idle"];
114 [[adium dockController] removeIconStateNamed:@"Idle"];
118 if(shouldUpdateStatus){
119 BOOL iconSupportsInvisible = [[adium dockController] currentIconSupportsIconStateNamed:@"Invisible"];
120 AIStatusType activeStatusType = [[adium statusController] activeStatusTypeTreatingInvisibleAsAway:!iconSupportsInvisible];
123 if (activeStatusType == AIAwayStatusType) {
124 [[adium dockController] setIconStateNamed:@"Away"];
126 [[adium dockController] removeIconStateNamed:@"Away"];
129 if (activeStatusType == AIInvisibleStatusType) {
130 [[adium dockController] setIconStateNamed:@"Invisible"];
132 [[adium dockController] removeIconStateNamed:@"Invisible"];
141 * @brief Return if any accounts have a TRUE value for the specified key
143 * @param inKey The status key to search on
144 * @result YES if any account returns TRUE for the boolean status object for inKey
146 - (BOOL)_accountsWithBoolKey:(NSString *)inKey
148 NSEnumerator *enumerator = [[[adium accountController] accountArray] objectEnumerator];
151 while((account = [enumerator nextObject])){
152 if([account integerStatusObjectForKey:inKey]) return(YES);
159 * @brief Return if any accounts have a non-nil value for the specified key
161 * @param inKey The status key to search on
162 * @result YES if any account returns a non-nil value for the status object for inKey
164 - (BOOL)_accountsWithKey:(NSString *)inKey
166 NSEnumerator *enumerator = [[[adium accountController] accountArray] objectEnumerator];
169 while((account = [enumerator nextObject])){
170 if([account statusObjectForKey:inKey]) return(YES);