Merged [13632]: Patch from Gayle Laakmann to not append @gmail.com if @googlemail...
[adiumx.git] / Source / AIDockAccountStatusPlugin.m
blob37c624a441e0991f07a7bde5c9a059daab45d6e8
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  */
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;
31 @end
33 /*!
34  * @class AIDockAccountStatusPlugin
35  * @brief Maintain the dock icon state in relation to global account status
36  *
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.
39  */
40 @implementation AIDockAccountStatusPlugin
42 /*!
43  * @brief Install plugin
44  */
45 - (void)installPlugin
47         //Observe account status changes
48         [[adium contactController] registerListObjectObserver:self];
50     //Observer preference changes
51         [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_GENERAL];
54 /*!
55  * @brief Uninstall plugin
56  */
57 - (void)uninstallPlugin
59     //Remove observers
60         [[adium contactController] unregisterListObjectObserver:self];
61         [[adium preferenceController] unregisterPreferenceObserver:self];
62     [[NSNotificationCenter defaultCenter] removeObserver:self];
65 /*!
66  * @brief Handle preference changes
67  *
68  * When the active dock icon changes, call updateListObject:keys:silent: to update its state to the global account state
69  */
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];
75         }
78 /*!
79  * @brief Update the dock icon state in response to an account changing status
80  *
81  * If one ore more accounts are online, set the Online icon state.  Similarly, handle the Connecting, Away, and Idle
82  * dock icon states.
83  */
84 - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
86         if(inObject == nil || [inObject isKindOfClass:[AIAccount class]]){
87                 BOOL    shouldUpdateStatus = NO;
88                 
89                 if(inObject == nil || [inModifiedKeys containsObject:@"Online"]){
90                         if([self _accountsWithBoolKey:@"Online"] > 0){
91                                 [[adium dockController] setIconStateNamed:@"Online"];
92                         }else{
93                                 [[adium dockController] removeIconStateNamed:@"Online"];
94                         }
95                         shouldUpdateStatus = YES;
96                 }
97                 if(inObject == nil || [inModifiedKeys containsObject:@"Connecting"]){
98                         if([self _accountsWithBoolKey:@"Connecting"] > 0){
99                                 [[adium dockController] setIconStateNamed:@"Connecting"];
100                         }else{
101                                 [[adium dockController] removeIconStateNamed:@"Connecting"];
102                         }
103                         shouldUpdateStatus = YES;
104                 }
105                 
106                 if(inObject == nil || [inModifiedKeys containsObject:@"StatusState"]){
107                         shouldUpdateStatus = YES;
108                 }
110                 if(inObject == nil || [inModifiedKeys containsObject:@"IdleSince"]){
111                         if([self _accountsWithKey:@"IdleSince"] > 0){
112                                 [[adium dockController] setIconStateNamed:@"Idle"];
113                         }else{
114                                 [[adium dockController] removeIconStateNamed:@"Idle"];
115                         }       
116                 }
117                 
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"];
125                         } else {
126                                 [[adium dockController] removeIconStateNamed:@"Away"];
127                         }
129                         if (activeStatusType == AIInvisibleStatusType) {
130                                 [[adium dockController] setIconStateNamed:@"Invisible"];
131                         } else {
132                                 [[adium dockController] removeIconStateNamed:@"Invisible"];
133                         }
134                 }
135         }
137         return(nil);
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
145  */
146 - (BOOL)_accountsWithBoolKey:(NSString *)inKey
148     NSEnumerator    *enumerator = [[[adium accountController] accountArray] objectEnumerator];
149     AIAccount       *account;
151     while((account = [enumerator nextObject])){
152                 if([account integerStatusObjectForKey:inKey]) return(YES);
153     }
155     return(NO);
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
163  */
164 - (BOOL)_accountsWithKey:(NSString *)inKey
166     NSEnumerator    *enumerator = [[[adium accountController] accountArray] objectEnumerator];
167     AIAccount       *account;
169     while((account = [enumerator nextObject])){
170                 if([account statusObjectForKey:inKey]) return(YES);
171     }
173     return(NO);
176 @end