Merged [15040]: Trying some magic: 5 seconds after the last unreachable host is repor...
[adiumx.git] / Source / ESAwayStatusWindowPlugin.m
blob0341010aa985324765b678fa5a99a154b9748501
1 //
2 //  ESAwayStatusWindowPlugin.m
3 //  Adium
4 //
5 //  Created by Evan Schoenberg on 4/12/05.
6 //  Copyright 2005 The Adium Team. All rights reserved.
7 //
9 #import "ESAwayStatusWindowPlugin.h"
10 #import "ESAwayStatusWindowController.h"
11 #import "AIContactController.h"
12 #import "AIPreferenceController.h"
13 #import "AISoundController.h"
14 #import "AIStatusController.h"
15 #import <Adium/AIAccount.h>
16 #import <Adium/AIListObject.h>
18 /*!
19  * @class ESAwayStatusWindowPlugin
20  * @brief Component to manage the status window optionally displayed when one or more accounts are away
21  *
22  * XXX - This comopnent should move to being an external, included, disabled by default plugin, with more
23  * options added when it is enabled.
24  */
25 @implementation ESAwayStatusWindowPlugin
27 /*!
28  * @brief Install
29  */
30 - (void)installPlugin
32         showStatusWindow = FALSE;
33         awayAccounts = [[NSMutableSet alloc] init];
35         //Remove any mute-while-stauts-window-is-open mute from the sound controller's preferences
36         [[adium preferenceController] setPreference:nil
37                                                                                  forKey:KEY_SOUND_STATUS_MUTE
38                                                                                   group:PREF_GROUP_SOUNDS];
39         
40         //Observe preference changes for updating if we should show the status window
41         [[adium preferenceController] registerPreferenceObserver:self 
42                                                                                                         forGroup:PREF_GROUP_STATUS_PREFERENCES];
45 - (void)uninstallPlugin
47         [[adium preferenceController] unregisterPreferenceObserver:self];
48         [[adium contactController] unregisterListObjectObserver:self];
51 /*!
52  * @brief Deallocate
53  */
54 - (void)dealloc
56         [awayAccounts release];
58         [super dealloc];
61 /*!
62  * @brief Preferences changed
63  *
64  * Note whether we are supposed to should show the status window, and toggle it if necessary
65  */
66 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
67                                                         object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
69         BOOL oldShowStatusWindow = showStatusWindow;
70         
71         showStatusWindow = [[prefDict objectForKey:KEY_STATUS_SHOW_STATUS_WINDOW] boolValue];
72         
73         if(showStatusWindow != oldShowStatusWindow){
74                 if(showStatusWindow){
75                         /* Register as a list object observer, which will update all objects for us immediately leading to the proper
76                          * status window toggling. */
77                         [[adium contactController] registerListObjectObserver:self];
78                 }else{
79                         //Hide the status window if it is currently visible
80                         [ESAwayStatusWindowController updateStatusWindowWithVisibility:NO];
81                         
82                         //Clear our away account tracking
83                         [awayAccounts removeAllObjects];
84                         
85                         //Stop observing list objects
86                         [[adium contactController] unregisterListObjectObserver:self];
87                 }
88         }
91 /*!
92  * @brief Account status changed.
93  *
94  * Show or hide our status window as appropriate
95  */
96 - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
98         if([inObject isKindOfClass:[AIAccount class]] &&
99            (!inModifiedKeys || [inModifiedKeys containsObject:@"StatusState"] || [inModifiedKeys containsObject:@"Online"])){
100                 if([inObject online] && ([inObject statusType] != AIAvailableStatusType)){
101                         [awayAccounts addObject:inObject];
102                 }else{
103                         [awayAccounts removeObject:inObject];
104                 }
106                 /* We wait until the next run loop so we can have processed multiple changing accounts at once before updating
107                  * our display, preventing flickering through changes as the global state changes and thereby modifies multiple
108                  * account states in a single invocation.
109                  */
110                 [NSObject cancelPreviousPerformRequestsWithTarget:self
111                                                                                                  selector:@selector(processStatusUpdate)
112                                                                                                    object:nil];
113                 [self performSelector:@selector(processStatusUpdate)
114                                    withObject:nil
115                                    afterDelay:0];
116         }
118         //We don't modify any keys
119         return nil;
122 - (void)processStatusUpdate
124         //Tell the window to update, showing/hiding as necessary
125         [ESAwayStatusWindowController updateStatusWindowWithVisibility:([awayAccounts count] > 0)];     
128 @end