root/trunk/Inspectors/JVChatUserInspector.m

Revision 3746, 9.0 kB (checked in by akempgen, 1 year ago)

forgot some brackets

Line 
1 #import "JVChatUserInspector.h"
2 #import "JVChatRoomPanel.h"
3 #import "JVBuddy.h"
4 #import "MVFileTransferController.h"
5
6 @implementation JVDirectChatPanel (JVDirectChatPanelInspection)
7 - (id <JVInspector>) inspector {
8         return [[[JVChatUserInspector alloc] initWithChatUser:[self target]] autorelease];
9 }
10 @end
11
12 #pragma mark -
13
14 @implementation JVChatRoomMember (JVChatRoomMemberInspection)
15 - (id <JVInspector>) inspector {
16         return [[[JVChatUserInspector alloc] initWithChatUser:[self user]] autorelease];
17 }
18 @end
19
20 #pragma mark -
21
22 @implementation MVChatUser (MVChatUserInspection)
23 - (id <JVInspector>) inspector {
24         return [[[JVChatUserInspector alloc] initWithChatUser:self] autorelease];
25 }
26 @end
27
28 #pragma mark -
29
30 @implementation JVChatUserInspector
31 - (id) initWithChatUser:(MVChatUser *) user {
32         if( ( self = [self init] ) )
33                 _user = [user retain];
34         return self;
35 }
36
37 - (void) dealloc {
38         [[NSNotificationCenter defaultCenter] removeObserver:self];
39
40         [_localTimeUpdated release];
41         [_localTimeUpdateTimer release];
42         [_updateTimer release];
43         [_user release];
44
45         _localTimeUpdated = nil;
46         _localTimeUpdateTimer = nil;
47         _updateTimer = nil;
48         _user = nil;
49
50         [super dealloc];
51 }
52
53 #pragma mark -
54
55 - (NSView *) view {
56         if( ! _nibLoaded ) _nibLoaded = [NSBundle loadNibNamed:@"JVChatMemberInspector" owner:self];
57         return view;
58 }
59
60 - (NSSize) minSize {
61         return NSMakeSize( 220., 359. );
62 }
63
64 - (NSString *) title {
65         return [_user nickname];
66 }
67
68 - (NSString *) type {
69         return NSLocalizedString( @"User", "chat user inspector type" );
70 }
71
72 - (void) willLoad {
73         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( infoUpdated: ) name:MVChatUserInformationUpdatedNotification object:_user];
74         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( attributeUpdated: ) name:MVChatUserAttributeUpdatedNotification object:_user];
75         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( errorOccurred: ) name:MVChatConnectionErrorNotification object:[_user connection]];
76
77         _addressResolved = NO;
78         [progress startAnimation:nil];
79         [_user refreshInformation];
80
81         [_updateTimer invalidate];
82         [_updateTimer release];
83         _updateTimer = [[NSTimer scheduledTimerWithTimeInterval:120. target:self selector:@selector( updateInformation ) userInfo:nil repeats:YES] retain];
84
85         [nickname setObjectValue:[_user nickname]];
86 //      if( [[_user buddy] picture] )
87 //              [image setImage:[[_user buddy] picture]];
88 }
89
90 - (BOOL) shouldUnload {
91         [[view window] makeFirstResponder:view];
92         return YES;
93 }
94
95 - (void) didUnload {
96         [_localTimeUpdated release];
97         _localTimeUpdated = nil;
98
99         [_localTimeUpdateTimer invalidate];
100         [_localTimeUpdateTimer release];
101         _localTimeUpdateTimer = nil;
102
103         [_updateTimer invalidate];
104         [_updateTimer release];
105         _updateTimer = nil;
106 }
107
108 #pragma mark -
109
110 - (void) updateInformation {
111         [_user refreshInformation];
112         [progress startAnimation:nil];
113 }
114
115 #pragma mark -
116
117 - (NSMutableAttributedString *) _convertRawMessage:(NSData *) message withBaseFont:(NSFont *) baseFont {
118         if( ! message || ! [message length] ) return nil;
119         if( ! baseFont ) baseFont = [NSFont labelFontOfSize:11.];
120
121         NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:[[_user connection] encoding]], @"StringEncoding", [NSNumber numberWithBool:[[NSUserDefaults standardUserDefaults] boolForKey:@"JVChatStripMessageColors"]], @"IgnoreFontColors", [NSNumber numberWithBool:[[NSUserDefaults standardUserDefaults] boolForKey:@"JVChatStripMessageFormatting"]], @"IgnoreFontTraits", baseFont, @"BaseFont", nil];
122         NSMutableAttributedString *messageString = [NSMutableAttributedString attributedStringWithChatFormat:message options:options];
123
124         if( ! messageString ) {
125                 [options setObject:[NSNumber numberWithUnsignedInt:NSISOLatin1StringEncoding] forKey:@"StringEncoding"];
126                 messageString = [NSMutableAttributedString attributedStringWithChatFormat:message options:options];
127         }
128
129         return messageString;
130 }
131
132 - (void) gotAddress:(NSString *) ip {
133         [address setObjectValue:( ip ? ip : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
134         [address setToolTip:( ip ? ip : nil )];
135         _addressResolved = YES;
136         [progress stopAnimation:nil];
137 }
138
139 - (oneway void) lookupAddress {
140         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
141         NSString *ip = [[NSHost hostWithName:[_user address]] address];
142         [self performSelectorOnMainThread:@selector( gotAddress: ) withObject:ip waitUntilDone:YES];
143         [pool release];
144 }
145
146 - (void) infoUpdated:(NSNotification *) notification {
147         if( [_user isIdentified] ) {
148                 [class setObjectValue:NSLocalizedString( @"Registered user", "registered user class" )];
149         } else if( [_user isServerOperator] ) {
150                 [class setObjectValue:NSLocalizedString( @"Server operator", "server operator class" )];
151         } else {
152                 [class setObjectValue:NSLocalizedString( @"Normal user", "normal user class" )];
153         }
154        
155         NSData *awayData = [_user awayStatusMessage];
156         NSAttributedString *awayString = [self _convertRawMessage:awayData withBaseFont:nil];
157         [away setObjectValue:( awayString && [awayString length] ? awayString : (NSAttributedString *)NSLocalizedString( @"n/a", "not applicable or not available" ) )];
158
159         [username setObjectValue:( [_user username] ? [_user username] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
160
161         [hostname setObjectValue:( [_user address] ? [_user address] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
162         [hostname setToolTip:( [_user address] ? [_user address] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
163
164         if( ! _addressResolved ) [NSThread detachNewThreadSelector:@selector( lookupAddress ) toTarget:self withObject:nil];
165
166         [server setObjectValue:( [_user serverAddress] ? [_user serverAddress] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
167         [server setToolTip:( [_user serverAddress] ? [_user serverAddress] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
168
169         [realName setObjectValue:( [_user realName] ? [_user realName] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
170         [realName setToolTip:( [_user realName] ? [_user realName] : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
171
172         [connected setObjectValue:( ! ( [_user status] == MVChatUserOfflineStatus ) ? MVReadableTime( [[_user dateConnected] timeIntervalSince1970], YES ) : NSLocalizedString( @"offline", "offline, not connected" ) )];
173         [idle setObjectValue:( ! ( [_user status] == MVChatUserOfflineStatus ) ? MVReadableTime( [[NSDate date] timeIntervalSince1970] + [_user idleTime], YES ) : NSLocalizedString( @"n/a", "not applicable or not available" ) )];
174
175         if( _addressResolved ) [progress stopAnimation:nil];
176 }
177
178 - (void) attributeUpdated:(NSNotification *) notification {
179         NSString *key = [[notification userInfo] objectForKey:@"attribute"];
180         if( [key isEqualToString:MVChatUserPingAttribute] ) {
181                 NSTimeInterval pingSeconds = [[_user attributeForKey:key] doubleValue];
182                 NSString *pingString = [NSString stringWithFormat:@"%.2f %@", pingSeconds, NSLocalizedString( @"seconds", "plural seconds" )];
183                 [ping setObjectValue:pingString];
184                 [ping setToolTip:pingString];
185         } else if( [key isEqualToString:MVChatUserLocalTimeAttribute] ) {
186                 [_localTimeUpdated release];
187                 _localTimeUpdated = [[NSDate date] retain];
188
189                 [self updateLocalTime];
190                 if( ! _localTimeUpdateTimer )
191                         _localTimeUpdateTimer = [[NSTimer scheduledTimerWithTimeInterval:10. target:self selector:@selector( updateLocalTime ) userInfo:nil repeats:YES] retain];
192         } else if( [key isEqualToString:MVChatUserClientInfoAttribute] ) {
193                 [clientInfo setObjectValue:[_user attributeForKey:key]];
194                 [clientInfo setToolTip:[_user attributeForKey:key]];
195         } else if( [key isEqualToString:MVChatUserKnownRoomsAttribute] ) {
196                 NSString *rms = [[_user attributeForKey:key] componentsJoinedByString:NSLocalizedString( @", ", "room list separator" )];
197                 [rooms setObjectValue:rms];
198                 [rooms setToolTip:rms];
199         }
200 }
201
202 - (void) errorOccurred:(NSNotification *) notification {
203         NSError *error = [[notification userInfo] objectForKey:@"error"];
204         if( [error code] == MVChatConnectionNoSuchUserError ) {
205                 MVChatUser *user = [[error userInfo] objectForKey:@"user"];
206                 if( [user isEqualTo:_user] )
207                         [self gotAddress:nil];
208         }
209 }
210
211 - (void) updateLocalTime {
212         NSCalendarDate *current = [_user attributeForKey:MVChatUserLocalTimeAttribute];
213         NSTimeInterval diff = ABS( [_localTimeUpdated timeIntervalSinceNow] );
214
215         NSCalendarDate *adjusted = [[NSCalendarDate alloc] initWithTimeInterval:diff sinceDate:current];
216         NSString *format = [[NSUserDefaults standardUserDefaults] objectForKey:NSShortTimeDateFormatString];
217         NSCalendarDate *formatedDate = [adjusted dateWithCalendarFormat:format timeZone:[current timeZone]];
218         [adjusted release];
219
220         [localTime setObjectValue:formatedDate];
221         [localTime setToolTip:[formatedDate description]];
222 }
223
224 - (IBAction) sendPing:(id) sender {
225         [_user refreshAttributeForKey:MVChatUserPingAttribute];
226 }
227
228 - (IBAction) requestLocalTime:(id) sender {
229         [_user refreshAttributeForKey:MVChatUserLocalTimeAttribute];
230 }
231
232 - (IBAction) requestClientInfo:(id) sender {
233         [_user refreshAttributeForKey:MVChatUserClientInfoAttribute];
234 }
235 @end
Note: See TracBrowser for help on using the browser.