Put NSAutoreleasePool usage around other distributed notification observer methods
[adiumx.git] / Source / AIAutoIdlePlugin.m
blob4e5f07b79764c1dc4cd92eb3540ba49fcc9f0ad9
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 "AIAutoIdlePlugin.h"
18 #import <Adium/AIPreferenceControllerProtocol.h>
19 #import "AIStatusController.h"
20 #import <Adium/AIAccount.h>
22 /*!
23  * @class AIAutoIdlePlugin
24  * @brief Provides auto-idle functionality for the state system
25  *
26  * This class implements auto-idling.  When the user is inactive for a period of time, the idle status flag is set
27  * for all accounts.  Once the user returns this flag is removed.  This plugin works independently of the state
28  * system, so this automatic idle flag won't interfere with the currently active state.
29  */
30 @implementation AIAutoIdlePlugin
32 /*!
33  * @brief Initialize the auto-idle system
34  *
35  * When AIMachineIdleUpdateNotification is posted, check the time idle against the time at which to report that we
36  * are idle (as specified by the user in the preferences).  When AIMachineIsActiveNotification, return us from idle
37  * if we reported as idle previously.
38  */
39 - (void)installPlugin
41         automaticIdleSet = NO;
43         //Ensure no idle time is set as we load
44         [[adium preferenceController] setPreference:nil
45                                                                                  forKey:@"IdleSince"
46                                                                                   group:GROUP_ACCOUNT_STATUS];
48         NSNotificationCenter *adiumNotificationCenter = [adium notificationCenter];
49         [adiumNotificationCenter addObserver:self
50                                     selector:@selector(machineIdleUpdate:)
51                                         name:AIMachineIdleUpdateNotification
52                                       object:nil];
53         [adiumNotificationCenter addObserver:self
54                                     selector:@selector(machineIsActive:)
55                                         name:AIMachineIsActiveNotification
56                                       object:nil];
58         //Observe preference changes for updating if and when we should report being idle
59         [[adium preferenceController] registerPreferenceObserver:self 
60                                                                                                         forGroup:PREF_GROUP_STATUS_PREFERENCES];        
63 - (void)uninstallPlugin
65         [[adium notificationCenter] removeObserver:self];
66         [[adium preferenceController] unregisterPreferenceObserver:self];
69 /*!
70  * @brief Preferences changed
71  *
72  * Note whether we are supposed to report idle time, and, if so, after how much time.
73  */
74 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
75                                                         object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
77         reportIdleTime = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE] boolValue];
78         idleTimeInterval = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE_INTERVAL] doubleValue];       
81 /*!
82  * @brief Invoked when machine idle updates
83  *
84  * Invoked when Adium has an update on machine activity.  If we are not yet idle, and the current length of inactivity
85  * is over the threshold, set our accounts to idle status.
86  */
87 - (void)machineIdleUpdate:(NSNotification *)notification
89         if (!automaticIdleSet && reportIdleTime) {
90                 double  duration = [[[notification userInfo] objectForKey:@"Duration"] doubleValue];
91                 
92                 if (duration > idleTimeInterval) {
93                         //If we are over the idle threshold, set our accounts to idle
94                         automaticIdleSet = YES;
95                         [[adium preferenceController] setPreference:[[notification userInfo] objectForKey:@"IdleSince"]
96                                                                                                  forKey:@"IdleSince"
97                                                                                                   group:GROUP_ACCOUNT_STATUS];
98                 }
99         }
103  * @brief Invoked when machine becomes active
105  * Invoked when Adium has an update on machine activity.  If we are currently idle, set our accounts back to active.
106  */
107 - (void)machineIsActive:(NSNotification *)notification
109         if (automaticIdleSet) {
110                 [[adium preferenceController] setPreference:nil
111                                                                                          forKey:@"IdleSince"
112                                                                                           group:GROUP_ACCOUNT_STATUS];          
113                 automaticIdleSet = NO;
114         }
117 @end