Performed [21405] and [21406], moving the contact list to Source, on adium-1.1
[adiumx.git] / Source / ESAccountEvents.m
blob8be1869b6ef15863b054c89b7fe09a6ca704790e
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 <Adium/AIContactControllerProtocol.h>
18 #import "ESAccountEvents.h"
19 #import <Adium/AIContactAlertsControllerProtocol.h>
20 #import <AIUtilities/AIImageAdditions.h>
21 #import <Adium/AIAccount.h>
23 #define ACCOUNT_CONNECTION_STATUS_GROUPING  4.0
25 /*!
26  * @class ESAccountEvents
27  * @brief Component to handle account-related Contact Alerts events
28  */
29 @implementation ESAccountEvents
31 /*!
32  * @brief Install
33  */
34 - (void)installPlugin
36         accountConnectionStatusGroupingOnlineTimer = nil;
37         accountConnectionStatusGroupingOfflineTimer = nil;
38         
39         //Register the events we generate
40         [[adium contactAlertsController] registerEventID:ACCOUNT_CONNECTED withHandler:self inGroup:AIAccountsEventHandlerGroup globalOnly:YES];
41         [[adium contactAlertsController] registerEventID:ACCOUNT_DISCONNECTED withHandler:self inGroup:AIAccountsEventHandlerGroup globalOnly:YES];
42         [[adium contactAlertsController] registerEventID:ACCOUNT_RECEIVED_EMAIL withHandler:self inGroup:AIOtherEventHandlerGroup globalOnly:YES];
44         //Observe status changes
45     [[adium contactController] registerListObjectObserver:self];
48 - (void)uninstallPlugin
50         [[adium contactController] unregisterListObjectObserver:self];
53 /*!
54  * @brief Short description for an event
55  *
56  * We're global-only, so no short descriptions are needed.
57  */
58 - (NSString *)shortDescriptionForEventID:(NSString *)eventID { return @""; }
60 /*!
61  * @brief Global short description for an event
62  */
63 - (NSString *)globalShortDescriptionForEventID:(NSString *)eventID
65         NSString        *description;
66         
67         if ([eventID isEqualToString:ACCOUNT_CONNECTED]) {
68                 description = AILocalizedString(@"You connect",nil);
69         } else if ([eventID isEqualToString:ACCOUNT_DISCONNECTED]) {
70                 description = AILocalizedString(@"You disconnect",nil);
71         } else if ([eventID isEqualToString:ACCOUNT_RECEIVED_EMAIL]) {
72                 description = AILocalizedString(@"New email notification",nil);
73         } else {
74                 description = @"";      
75         }
76         
77         return description;
80 /*!
81  * @brief English, non-translated global short description for an event
82  *
83  * This exists because old X(tras) relied upon matching the description of event IDs, and I don't feel like making
84  * a converter for old packs.  If anyone wants to fix this situation, please feel free :)
85  *
86  * @result English global short description which should only be used internally
87  */
88 - (NSString *)englishGlobalShortDescriptionForEventID:(NSString *)eventID
90         NSString        *description;
91         
92         if ([eventID isEqualToString:ACCOUNT_CONNECTED]) {
93                 description = @"Connected";
94         } else if ([eventID isEqualToString:ACCOUNT_DISCONNECTED]) {
95                 description = @"Disconnected";
96         } else if ([eventID isEqualToString:ACCOUNT_RECEIVED_EMAIL]) {
97                 description = @"New Mail Received";
98         } else {
99                 description = @"";      
100         }
101         
102         return description;
106  * @brief Long description for an event
107  */
108 - (NSString *)longDescriptionForEventID:(NSString *)eventID forListObject:(AIListObject *)listObject
110         NSString        *description;
111         
112         if ([eventID isEqualToString:ACCOUNT_CONNECTED]) {
113                 description = AILocalizedString(@"When you connect",nil);
114         } else if ([eventID isEqualToString:ACCOUNT_DISCONNECTED]) {
115                 description = AILocalizedString(@"When you disconnect",nil);
116         } else if ([eventID isEqualToString:ACCOUNT_RECEIVED_EMAIL]) {
117                 description = AILocalizedString(@"When you receive a new email notification",nil);
118         } else {
119                 description = @"";
120         }
121         
122         return description;
126  * @brief Natural language description for an event
128  * @param eventID The event identifier
129  * @param listObject The listObject triggering the event
130  * @param userInfo Event-specific userInfo
131  * @param includeSubject If YES, return a full sentence.  If not, return a fragment.
132  * @result The natural language description.
133  */
134 - (NSString *)naturalLanguageDescriptionForEventID:(NSString *)eventID
135                                                                                 listObject:(AIListObject *)listObject
136                                                                                   userInfo:(id)userInfo
137                                                                         includeSubject:(BOOL)includeSubject
139         NSString        *description = nil;
140         
141         if (includeSubject) {
142                 NSString        *format = nil;
143                 if ([eventID isEqualToString:ACCOUNT_CONNECTED]) {
144                         format = AILocalizedString(@"%@ connected",nil);
145                 } else if ([eventID isEqualToString:ACCOUNT_DISCONNECTED]) {
146                         format = AILocalizedString(@"%@ disconnected",nil);
147                 } else if ([eventID isEqualToString:ACCOUNT_RECEIVED_EMAIL]) {
148                         format = AILocalizedString(@"%@ received new email",nil);
149                 }
150                 
151                 if (format) {
152                         description = [NSString stringWithFormat:format,[listObject formattedUID]];
153                 }
154         } else {
155                 if ([eventID isEqualToString:ACCOUNT_CONNECTED]) {
156                         description = AILocalizedString(@"connected",nil);
157                 } else if ([eventID isEqualToString:ACCOUNT_DISCONNECTED]) {
158                         description = AILocalizedString(@"disconnected",nil);
159                 } else if ([eventID isEqualToString:ACCOUNT_RECEIVED_EMAIL]) {
160                         if (userInfo && [userInfo isKindOfClass:[NSString class]]) {
161                                 description = [[(NSString *)userInfo copy] autorelease];
163                         } else {
164                                 description = AILocalizedString(@"received new email",nil);
165                         }
166                 }
167         }
168         
169         return description;
172 - (NSImage *)imageForEventID:(NSString *)eventID
174         static NSImage  *eventImage = nil;
175         if (!eventImage) eventImage = [[NSImage imageNamed:@"pref-accounts" forClass:[self class]] retain];
176         return eventImage;
179 #pragma mark Aggregation and event generation
181  * @brief Update list object
183  * We aggregate account connection events to avoid a quick sign on/sign off from triggering the event
184  */
185 - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
187         if ([inObject isKindOfClass:[AIAccount class]]) { //We only care about accounts
188                 if ([inModifiedKeys containsObject:@"Online"]) {
189                         
190                         if ([[inObject numberStatusObjectForKey:@"Online"] boolValue]) {
191                                 if (accountConnectionStatusGroupingOnlineTimer) {
192                                         [accountConnectionStatusGroupingOnlineTimer invalidate]; [accountConnectionStatusGroupingOnlineTimer release];
193                                 }
194                                 
195                                 accountConnectionStatusGroupingOnlineTimer = [[NSTimer scheduledTimerWithTimeInterval:ACCOUNT_CONNECTION_STATUS_GROUPING
196                                                                                                                                                                                            target:self
197                                                                                                                                                                                          selector:@selector(accountConnection:)
198                                                                                                                                                                                          userInfo:inObject
199                                                                                                                                                                                           repeats:NO] retain];
200                         } else {
201                                 if (accountConnectionStatusGroupingOfflineTimer) {
202                                         [accountConnectionStatusGroupingOfflineTimer invalidate]; [accountConnectionStatusGroupingOfflineTimer release];
203                                 }
204                                 
205                                 accountConnectionStatusGroupingOfflineTimer = [[NSTimer scheduledTimerWithTimeInterval:ACCOUNT_CONNECTION_STATUS_GROUPING
206                                                                                                                                                                                                 target:self
207                                                                                                                                                                                           selector:@selector(accountDisconnection:)
208                                                                                                                                                                                           userInfo:inObject
209                                                                                                                                                                                            repeats:NO] retain];
210                         }
211                 }
212         }
213         
214         return nil;     
218  * @brief Called an account connects and remains online for ACCOUNT_CONNECTION_STATUS_GROUPING
219  */
220 - (void)accountConnection:(NSTimer *)timer
222         [[adium contactAlertsController] generateEvent:ACCOUNT_CONNECTED
223                                                                          forListObject:[timer userInfo]
224                                                                                   userInfo:nil
225                                           previouslyPerformedActionIDs:nil];
226         [accountConnectionStatusGroupingOnlineTimer release]; accountConnectionStatusGroupingOnlineTimer = nil;
230  * @brief Called an account disconnects and remains offline for ACCOUNT_CONNECTION_STATUS_GROUPING
231  */
232 - (void)accountDisconnection:(NSTimer *)timer
234         [[adium contactAlertsController] generateEvent:ACCOUNT_DISCONNECTED
235                                                                          forListObject:[timer userInfo]
236                                                                                   userInfo:nil
237                                           previouslyPerformedActionIDs:nil];
238         [accountConnectionStatusGroupingOfflineTimer release]; accountConnectionStatusGroupingOfflineTimer = nil;
241 @end