Skip to content

Instantly share code, notes, and snippets.

@benlangfeld
Created October 25, 2010 14:04
Show Gist options
  • Save benlangfeld/645006 to your computer and use it in GitHub Desktop.
Save benlangfeld/645006 to your computer and use it in GitHub Desktop.
Really nasty MUC controller implementation
@import <MessageBoard/MessageBoard.j>
@import <StropheCappuccino/MUC/TNStropheMUCRoom.j>
@implementation MUCController : CPViewController
{
BOOL _active;
@outlet CPTextField _subjectBox;
@outlet CPScrollView _conversationBox;
TNMessageBoard _messageBoard;
@outlet CPTextField _messageBox;
TNStropheMUCRoom _session @accessors(property = session);
}
- (id)init
{
self = [super init];
if (self)
{
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(gotConnection:)
name:TNStropheConnectionStatusConnected
object:nil];
}
return self;
}
- (void)awakeFromCib
{
[super awakeFromCib];
_messageBoard = [[TNMessageBoard alloc] initWithFrame:[_conversationBox bounds]];
[_conversationBox setDocumentView:_messageBoard];
[_messageBoard setFrameSize:[_conversationBox contentSize]];
}
- (void)gotConnection:(CPNotification)aNotification
{
_session = [TNStropheMUCRoom joinRoom:[[Network currentNetwork] domain] + @"-public"
onService:[[CPBundle mainBundle] objectForInfoDictionaryKey:@"XMPPMUCService"]
usingConnection:[aNotification object]
withNick:[[User currentUser] username]];
[self setup];
_active = YES;
}
- (void)setup
{
[_session setDelegate:self];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(memberJoined:)
name:TNStropheMUCContactJoinedNotification
object:[_session roster]];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(memberLeft:)
name:TNStropheMUCContactLeftNotification
object:[_session roster]];
[_session join];
}
- (void)mucRoom:(TNStropheMUCRoom)aRoom receivedNewSubject:(CPString)aSubject
{
CPLog.debug("Updating MUC subject");
[_subjectBox setObjectValue:aSubject];
}
- (void)mucRoom:(TNStropheMUCRoom)aRoom receivedMessage:(CPDictionary)aMessage
{
CPLog.debug("Updating MUC");
[_messageBoard addMessage:[aMessage valueForKey:"body"]
from:[aMessage valueForKey:"from"]
date:[aMessage valueForKey:"time"]
color:[CPColor cyanColor]];
[self scrollToBottom];
}
- (void)memberJoined:(CPNotification)aNotification
{
var member = [[aNotification userInfo] valueForKey:@"contact"];
[_messageBoard addMessage:[member nickname] + @" joined the room."
from:nil
date:[CPDate dateWithTimeIntervalSinceNow:0]
color:[CPColor whiteColor]];
[self scrollToBottom];
}
- (void)memberLeft:(CPNotification)aNotification
{
var member = [[aNotification userInfo] valueForKey:@"contact"];
[_messageBoard addMessage:[member nickname] + @" left the room."
from:nil
date:[CPDate dateWithTimeIntervalSinceNow:0]
color:[CPColor whiteColor]];
[self scrollToBottom];
}
- (void)scrollToBottom
{
// Scroll to the bottom
var verticalOffset = [_messageBoard boundsSize].height - [[_conversationBox contentView] boundsSize].height;
[[_conversationBox contentView] scrollToPoint:CGPointMake(0,verticalOffset)];
[_conversationBox reflectScrolledClipView:[_conversationBox contentView]];
}
- (@action)setSubject:(id)aTextField
{
CPLog.debug("Setting MUC subject");
if (!_active)
{
CPLog.error("MUC controller is inactive and cannot be used!");
return;
}
[_session setSubject:[aTextField objectValue]];
}
- (@action)sendMessageToMuc:(id)aTextField
{
CPLog.debug("Sending a message to MUC");
if (!_active)
{
CPLog.error("MUC controller is inactive and cannot be used!");
return;
}
[_session sayToRoom:[aTextField objectValue]];
[aTextField setObjectValue:@""];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment