Prevent sending 0-byte files. Fixes #8711.
[adiumx.git] / Source / AIAutoIdlePlugin.m
blob7db02a8b9f09e6c137d2f5e3a64f68d7647a7ff4
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  * Deallocate
71  */
72 - (void)dealloc
74         [automaticIdleDate release];
76         [super dealloc];
79 /*!
80  * @brief Preferences changed
81  *
82  * Note whether we are supposed to report idle time, and, if so, after how much time.
83  */
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];       
91 /*!
92  * @brief Invoked when machine idle updates
93  *
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.
96  */
97 - (void)machineIdleUpdate:(NSNotification *)notification
99         if (!automaticIdleSet && reportIdleTime) {
100                 double  duration = [[[notification userInfo] objectForKey:@"Duration"] doubleValue];
101                 
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
107                                                                                                  forKey:@"IdleSince"
108                                                                                                   group:GROUP_ACCOUNT_STATUS];
109                 }
110         }
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).
119  */
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
126                                                                                                  forKey:@"IdleSince"
127                                                                                                   group:GROUP_ACCOUNT_STATUS];
128                 }
129                 
130                 //Clean up
131                 [automaticIdleDate release];
132                 automaticIdleDate = nil;
133                 automaticIdleSet = NO;
134         }
137 @end