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 "AIAutoIdlePlugin.h"
18 #import "AIPreferenceController.h"
19 #import "AIStatusController.h"
20 #import <Adium/AIAccount.h>
23 * @class AIAutoIdlePlugin
24 * @brief Provides auto-idle functionality for the state system
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.
30 @implementation AIAutoIdlePlugin
33 * @brief Initialize the auto-idle system
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.
41 automaticIdleSet = NO;
43 //Ensure no idle time is set as we load
44 [[adium preferenceController] setPreference:nil
46 group:GROUP_ACCOUNT_STATUS];
48 NSNotificationCenter *adiumNotificationCenter = [adium notificationCenter];
49 [adiumNotificationCenter addObserver:self
50 selector:@selector(machineIdleUpdate:)
51 name:AIMachineIdleUpdateNotification
53 [adiumNotificationCenter addObserver:self
54 selector:@selector(machineIsActive:)
55 name:AIMachineIsActiveNotification
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];
74 [automaticIdleDate release];
80 * @brief Preferences changed
82 * Note whether we are supposed to report idle time, and, if so, after how much time.
84 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
85 object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
87 reportIdleTime = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE] boolValue];
88 idleTimeInterval = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE_INTERVAL] doubleValue];
92 * @brief Invoked when machine idle updates
94 * Invoked when Adium has an update on machine activity. If we are not yet idle, and the current length of inactivity
95 * is over the threshold, set our accounts to idle status.
97 - (void)machineIdleUpdate:(NSNotification *)notification
99 if (!automaticIdleSet && reportIdleTime) {
100 double duration = [[[notification userInfo] objectForKey:@"Duration"] doubleValue];
102 if (duration > idleTimeInterval) {
103 //If we are over the idle threshold, set our accounts to idle
104 automaticIdleSet = YES;
105 automaticIdleDate = [[[notification userInfo] objectForKey:@"IdleSince"] retain];
106 [[adium preferenceController] setPreference:automaticIdleDate
108 group:GROUP_ACCOUNT_STATUS];
114 * @brief Invoked when machine becomes active
116 * Invoked when Adium has an update on machine activity. If we are currently idle, set our accounts back to active.
117 * This method checks to make sure that the current idle is the one we've set. If it is not, we do not remove the
118 * idle time (It's not nice to remove an idle you didn't set).
120 - (void)machineIsActive:(NSNotification *)notification
122 if (automaticIdleSet) {
123 //Only clear the idle status if it's the one we set, otherwise it's not ours to touch.
124 if ([[adium preferenceController] preferenceForKey:@"IdleSince" group:GROUP_ACCOUNT_STATUS] == automaticIdleDate) {
125 [[adium preferenceController] setPreference:nil
127 group:GROUP_ACCOUNT_STATUS];
131 [automaticIdleDate release];
132 automaticIdleDate = nil;
133 automaticIdleSet = NO;