// // ChatWindowController.m // Proteus // // Created by Julian Cain on 7/14/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "ChatWindowController.h" #import "Conversation.h" #import "ConversationController.h" #import "Proteus_AppDelegate.h" @implementation ChatWindowController @synthesize participantsTableView = _participantsTableView; @synthesize conversationController = _conversationController; @synthesize webViewSuperview = _webViewSuperview; @synthesize conversation = _conversation; @synthesize fromToolbarItem = _fromToolbarItem; @synthesize fromUsername = _fromUsername; - (id)init { if (self = [super initWithWindowNibName:@"ChatWindow"]) { // ... } return self; } - (void)awakeFromNib { [[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge ]; [[self window] setContentBorderThickness:36.0 forEdge:NSMinYEdge]; [self.window setBottomCornerRounded:NO]; NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; [self.fromToolbarItem removeAllItems]; NSArray * accounts = [userDefaults arrayForKey:@"accounts"]; for (NSDictionary * dict in accounts) { NSString * accountName = [dict objectForKey:@"username"]; NSMenuItem * item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"From: %@", nil), accountName] action:@selector(selectFrom:) keyEquivalent:@"" ]; [item setTarget:self]; [item setRepresentedObject:accountName]; [[self.fromToolbarItem menu] addItem:item]; [item release]; } /** * Always use the first account. This should be stored as in NSUserDefaults * mapping to's and from's. */ self.fromUsername = [accounts objectAtIndex:0]; } - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector { BOOL result = NO; Proteus_AppDelegate * delegate = (Proteus_AppDelegate *)[NSApp delegate] ; (void)delegate; if (commandSelector == @selector(insertNewline:)) { NSString * msgStr = [textView string]; if (msgStr.length) { NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ]; NSMutableArray * accounts = [userDefaults objectForKey:@"accounts"]; #warning ":TODO: use the selected \"from\" account" // self.fromUsername NSString * fromUsername = [[accounts objectAtIndex:0] objectForKey:@"username" ]; NSLog( @"Sending: %@ to %@ from %@.", msgStr, self.conversation.name, fromUsername ); /** * Send the message through the conversation. */ [delegate.purple sendConversationMessage:msgStr to:self.conversation.name from:fromUsername ]; NSString * basePath = [NSString stringWithFormat:@"%@/Default.chatstyle", [[NSBundle mainBundle] builtInPlugInsPath] ]; NSString * path = [NSString stringWithFormat:@"%@/msg_out.html", basePath ]; NSData * data = [[NSData alloc] initWithContentsOfFile:path]; NSString *msgOut = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ]; [data release]; path = [NSString stringWithFormat:@"%@/msg_out_next.html", basePath ]; data = [[NSData alloc] initWithContentsOfFile:path]; NSString * avatarUrl = [[basePath stringByAppendingPathComponent:@"Images"] stringByAppendingPathComponent:@"silhouette.png" ]; NSMutableString * html = [NSMutableString stringWithString:msgOut]; [html replaceOccurrencesOfString:@"%name%" withString:fromUsername options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%message%" withString:msgStr options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%time%" withString:[[NSDate date] description] options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%avatar%" withString:avatarUrl options:0 range:NSMakeRange(0, [html length]) ]; /** * :TODO: html encode */ [self.conversation.webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"appendMessage(\"%@\", true);", [self encodeHTMLString:html]] ]; } result = YES; } return result; } - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView { return self.conversationController.conversations.count; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { return [[self.conversationController.conversations objectAtIndex:rowIndex] name] ; } - (void)tableViewSelectionDidChange:(NSNotification *)aNotification { NSIndexSet * selectedIndexes = [self.participantsTableView selectedRowIndexes ]; NSLog(@"Set existing conversation from %@.", self.conversation.name); self.conversation = [self.conversationController.conversations objectAtIndex:[selectedIndexes lastIndex] ]; NSLog(@"Set existing conversation to %@.", self.conversation.name); // :TODO: set webView // self.webView = aConversation.webView; [[[self.webViewSuperview subviews] lastObject] removeFromSuperview]; self.conversation.webView.frame = self.webViewSuperview.bounds; [self.webViewSuperview addSubview:self.conversation.webView]; } - (void)addConversation:(Conversation *)aConversation { Conversation * conversation = nil; for (conversation in self.conversationController.conversations) { if ([conversation.name isEqualToString:aConversation.name]) { NSLog(@"Using existing conversation for %@.", aConversation.name); break; } } if (!conversation) { [self.conversationController.conversations addObject:aConversation]; /** * Reload the participants. */ [self.participantsTableView reloadData]; } } - (void)updateConversation:(Conversation *)aConversation message:(NSString *)aMessage time:(NSDate *)aTime { NSLog(@"Set existing conversation to %@.", aConversation.name); self.conversation = aConversation; // [[[self.webViewSuperview subviews] lastObject] removeFromSuperview]; // self.conversation.webView.frame = self.webViewSuperview.bounds; // [self.webViewSuperview addSubview:self.conversation.webView]; NSString * basePath = [NSString stringWithFormat:@"%@/Default.chatstyle", [[NSBundle mainBundle] builtInPlugInsPath] ]; NSString * path = [NSString stringWithFormat:@"%@/msg_in.html", basePath]; NSData * data = [[NSData alloc] initWithContentsOfFile:path]; NSString * msgIn = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ]; [data release]; path = [NSString stringWithFormat:@"%@/msg_in_next.html", basePath]; NSString * avatarUrl2 = [[basePath stringByAppendingPathComponent:@"Images"] stringByAppendingPathComponent:@"silhouette.png" ]; // left NSMutableString * html = nil; // right html = [NSMutableString stringWithString:msgIn]; [html replaceOccurrencesOfString:@"%name%" withString:aConversation.name options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%message%" withString:aMessage options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%time%" withString:[aTime description] options:0 range:NSMakeRange(0, [html length]) ]; [html replaceOccurrencesOfString:@"%avatar%" withString:avatarUrl2 options:0 range:NSMakeRange(0, [html length]) ]; [aConversation.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"appendMessage(\"%@\", true);", [self encodeHTMLString:html]] ]; } - (void)selectFrom:(id)sender { self.fromUsername = [sender representedObject]; } - (NSMutableString *)encodeHTMLString:(NSMutableString *)inString { [inString replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:0 range:NSMakeRange(0, [inString length]) ]; [inString replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:0 range:NSMakeRange(0, [inString length]) ]; [inString replaceOccurrencesOfString:@"\n" withString:@"" options:0 range:NSMakeRange(0, [inString length]) ]; [inString replaceOccurrencesOfString:@"\r" withString:@"
" options:0 range:NSMakeRange(0, [inString length]) ]; return inString; } - (void)dealloc { [_participantsTableView release], _participantsTableView = 0; [_conversationController release], _conversationController = 0; [_webViewSuperview release], _webViewSuperview = 0; [_conversation release], _conversation = 0; [_fromToolbarItem release], _fromToolbarItem = nil; [_fromUsername release], _fromUsername = nil; [super dealloc]; } @end