William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AIAutomaticStatus.h"
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIStatusControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/ESTextAndButtonsWindowController.h>
#import <Adium/AIAccount.h>
#import <Adium/AIStatus.h>
#import <Adium/AIStatusGroup.h>
@interface AIAutomaticStatus ()
- (void)notificationHandler:(NSNotification *)notification;
- (void)triggerAutoAwayWithStatusID:(NSNumber *)statusID;
- (void)returnFromAutoAway;
@end
/*!
* @class AIAutomaticStatus
*
* Automatically set accounts to certain statuses when events occur. Currently this handles:
* - Fast user switching
* - Screensaver activation
* - Idle time
*/
@implementation AIAutomaticStatus
/*!
* @brief Initialize the automatic status system
*/
- (void)installPlugin
{
// Ensure no idle time is set as we load
[adium.preferenceController setPreference:nil
forKey:@"IdleSince"
group:GROUP_ACCOUNT_STATUS];
// Initialize our state information
accountsToReconnect = [[NSMutableSet alloc] init];
previousStatus = [[NSMutableDictionary alloc] init];
// Register our notifications
NSNotificationCenter *notificationCenter;
// FUS events are on the sharedWorkspace's notificationCenter
notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:NSWorkspaceSessionDidBecomeActiveNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:NSWorkspaceSessionDidResignActiveNotification
object:nil];
// Screensaver events are distributed notification events
notificationCenter = [NSDistributedNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:AIScreensaverDidStartNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:AIScreensaverDidStopNotification
object:nil];
// Idle events are in the Adium notification center, posted by the AdiumIdleManager
notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:AIMachineIdleUpdateNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(notificationHandler:)
name:AIMachineIsActiveNotification
object:nil];
// Register for status preference updates
[adium.preferenceController registerPreferenceObserver:self
forGroup:PREF_GROUP_STATUS_PREFERENCES];
}
/*!
* @brief Uninstall plugin
*
* When the plugin is uninstalled, we revert to whatever status we were previously set to.
*/
- (void)uninstallPlugin
{
// Unregister our notifications
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Unregister our preference observations
[adium.preferenceController unregisterPreferenceObserver:self];
// Revert to our stored statuses
if (automaticStatusSet) {
[self returnFromAutoAway];
}
}
/*!
* Deallocate
*/
- (void)dealloc
{
// State information
[accountsToReconnect release];
[previousStatus release];
// Stored status IDs
[fastUserSwitchID release];
[screenSaverID release];
[idleID release];
[super dealloc];
}
/*!
* @brief Preferences changed
*
* Note the status IDs, interval information, and enabled information for our preferences
*/
- (void)preferencesChangedForGroup:(NSString *)group
key:(NSString *)key
object:(AIListObject *)object
preferenceDict:(NSDictionary *)prefDict
firstTime:(BOOL)firstTime
{
if (object) {
return;
}
// Idle reporting
reportIdleEnabled = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE] boolValue];
idleReportInterval = [[prefDict objectForKey:KEY_STATUS_REPORT_IDLE_INTERVAL] doubleValue];
// Idle status change
[idleID release];
idleID = [[prefDict objectForKey:KEY_STATUS_AUTO_AWAY_STATUS_STATE_ID] retain];
idleEnabled = [[prefDict objectForKey:KEY_STATUS_AUTO_AWAY] boolValue];
idleStatusInterval = [[prefDict objectForKey:KEY_STATUS_AUTO_AWAY_INTERVAL] doubleValue];
// Fast user switch
[fastUserSwitchID release];
fastUserSwitchID = [[prefDict objectForKey:KEY_STATUS_FUS_STATUS_STATE_ID] retain];
fastUserSwitchEnabled = [[prefDict objectForKey:KEY_STATUS_FUS] boolValue];
// Screensaver
[screenSaverID release];
screenSaverID = [[prefDict objectForKey:KEY_STATUS_SS_STATUS_STATE_ID] retain];
screenSaverEnabled = [[prefDict objectForKey:KEY_STATUS_SS] boolValue];
}
/*!
* @brief Handle a notification
*
* @param notification The notification to process
*
* When a notification comes in, this checks if it's a start or end event
*
* A start event will set the status, and the end will revert to the previous status.
*/
- (void)notificationHandler:(NSNotification *)notification
{
NSString *notificationName = [notification name];
NSNumber *statusID = nil;
BOOL startEvent = NO, endEvent = NO;
// Start events
if ([notificationName isEqualToString:NSWorkspaceSessionDidResignActiveNotification]) {
AILogWithSignature(@"Fast user switch (start) detected");
startEvent = fastUserSwitchEnabled;
statusID = fastUserSwitchID;
} else if ([notificationName isEqualToString:AIScreensaverDidStartNotification]) {
AILogWithSignature(@"Screensaver (start) detected.");
startEvent = screenSaverEnabled;
statusID = screenSaverID;
} else if ([notificationName isEqualToString:AIMachineIdleUpdateNotification]) {
double duration = [[[notification userInfo] objectForKey:@"Duration"] doubleValue];
// Update our idle time
if (!automaticIdleSet && reportIdleEnabled && duration >= idleReportInterval) {
AILogWithSignature(@"Idle (report) detected.");
automaticIdleSet = YES;
[adium.preferenceController setPreference:[[notification userInfo] objectForKey:@"IdleSince"]
forKey:@"IdleSince"
group:GROUP_ACCOUNT_STATUS];
}
// Idle events require that we've been idle longer than required
startEvent = (idleEnabled && duration >= idleStatusInterval);
statusID = idleID;
// This is very spammy when we're already idle.
if (startEvent && !automaticStatusSet) {
AILogWithSignature(@"Idle (start) detected.");
}
}
// End events
if ([notificationName isEqualToString:NSWorkspaceSessionDidBecomeActiveNotification]) {
AILogWithSignature(@"Fast user switch (end) detected.");
endEvent = fastUserSwitchEnabled;
} else if ([notificationName isEqualToString:AIScreensaverDidStopNotification]) {
AILogWithSignature(@"Screensaver (end) detected.");
endEvent = screenSaverEnabled;
} else if ([notificationName isEqualToString:AIMachineIsActiveNotification]) {
AILogWithSignature(@"Idle (end) detected.");
if (automaticIdleSet) {
automaticIdleSet = NO;
[adium.preferenceController setPreference:nil
forKey:@"IdleSince"
group:GROUP_ACCOUNT_STATUS];
}
endEvent = idleEnabled;
}
if (startEvent && statusID && !automaticStatusSet) {
[self triggerAutoAwayWithStatusID:statusID];
} else if (endEvent && automaticStatusSet) {
[self returnFromAutoAway];
}
}
/*!
* @brief Automatically set an account as away
*
* @param statusID The status ID to change account status to
*
* Sets all available accounts to the status type statusID, while storing the
*/
- (void)triggerAutoAwayWithStatusID:(NSNumber *)statusID
{
AIStatusItem *targetStatusState = [adium.statusController statusStateWithUniqueStatusID:statusID];
// Grab any group memeber if possible
if ([targetStatusState isKindOfClass:[AIStatusGroup class]]) {
targetStatusState = [(AIStatusGroup *)targetStatusState anyContainedStatus];
}
// If we weren't given a valid state, fail.
if (!targetStatusState) {
return;
}
for (AIAccount *account in adium.accountController.accounts) {
AIStatus *currentStatusState = account.statusState;
// Don't modify or store the status of non-available accounts
if (currentStatusState.statusType != AIAvailableStatusType) {
continue;
}
// Store the state of the account
[previousStatus setObject:currentStatusState
forKey:[NSNumber numberWithUnsignedInt:[account hash]]];
AILogWithSignature(@"Setting %@ to status %@", account, targetStatusState);
if (account.online) {
// Set the account's status to our new value
[account setStatusState:(AIStatus *)targetStatusState];
// If this status brought the account offline, add it to the list to reconnect.
if (targetStatusState.statusType == AIOfflineStatusType) {
[accountsToReconnect addObject:account];
}
} else {
[account setStatusStateAndRemainOffline:(AIStatus *)targetStatusState];
}
}
automaticStatusSet = YES;
}
/*!
* @brief Return from automatic away
*
* Returns all accounts with stored status information to their previous status.
*/
- (void)returnFromAutoAway
{
for (AIAccount *account in adium.accountController.accounts) {
AIStatus *previousStatusState = [previousStatus objectForKey:[NSNumber numberWithUnsignedInt:[account hash]]];
// Skip accounts without stored information.
if (!previousStatusState) {
continue;
}
AILogWithSignature(@"Returning %@ to status %@", account, previousStatusState);
if (account.online || [accountsToReconnect containsObject:account]) {
//If online or needs to be reconnected, set the previous state, going online if necessary
[account setStatusState:previousStatusState];
} else {
//If offline, set the state without coming online
[account setStatusStateAndRemainOffline:previousStatusState];
}
}
[accountsToReconnect removeAllObjects];
[previousStatus removeAllObjects];
automaticStatusSet = NO;
}
@end
|