Skip to content

Commit

Permalink
aa
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyboy committed Aug 22, 2011
1 parent 07bfa3e commit ff0d4c4
Show file tree
Hide file tree
Showing 177 changed files with 50,574 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Classes/JabberClientAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// JabberClientAppDelegate.h
// JabberClient
//
// Created by cesarerocchi on 8/3/11.
// Copyright 2011 studiomagnolia.com. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "XMPPRoster.h"
#import "XMPP.h"
#import "SMChatDelegate.h"
#import "SMMessageDelegate.h"

@class SMBuddyListViewController;

@interface JabberClientAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SMBuddyListViewController *viewController;

XMPPStream *xmppStream;
XMPPRoster *xmppRoster;

NSString *password;

BOOL isOpen;

__weak NSObject <SMChatDelegate> *_chatDelegate;
__weak NSObject <SMMessageDelegate> *_messageDelegate;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SMBuddyListViewController *viewController;


@property (nonatomic, readonly) XMPPStream *xmppStream;
@property (nonatomic, readonly) XMPPRoster *xmppRoster;

@property (nonatomic, assign) id _chatDelegate;
@property (nonatomic, assign) id _messageDelegate;

- (BOOL)connect;
- (void)disconnect;

@end

191 changes: 191 additions & 0 deletions Classes/JabberClientAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#import "SMBuddyListViewController.h"


@interface JabberClientAppDelegate()

- (void)setupStream;

- (void)goOnline;
- (void)goOffline;

@end


@implementation JabberClientAppDelegate

@synthesize xmppStream;
@synthesize xmppRoster;
@synthesize window;
@synthesize viewController;
@synthesize _chatDelegate;
@synthesize _messageDelegate;


- (void)applicationWillResignActive:(UIApplication *)application {

[self disconnect];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

//[self setupStream];
[self connect];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;
}


- (void)setupStream {

xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];


}

- (void)goOnline {
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
}

- (void)goOffline {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[[self xmppStream] sendElement:presence];
}

- (BOOL)connect {

[self setupStream];

NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"];

if (![xmppStream isDisconnected]) {
return YES;
}


if (jabberID == nil || myPassword == nil) {

return NO;
}

[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
password = myPassword;

NSError *error = nil;
if (![xmppStream connect:&error])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
[alertView release];


return NO;
}

return YES;
}

- (void)disconnect {

[self goOffline];
[xmppStream disconnect];
[_chatDelegate didDisconnect];
}



#pragma mark -
#pragma mark XMPP delegates


- (void)xmppStreamDidConnect:(XMPPStream *)sender {

isOpen = YES;
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];

}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {

[self goOnline];

}


- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {

return NO;

}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {


NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];

[_messageDelegate newMessageReceived:m];
[m release];

}

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {

NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];

if (![presenceFromUser isEqualToString:myUsername]) {

if ([presenceType isEqualToString:@"available"]) {

[_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"jerry.local"]];

} else if ([presenceType isEqualToString:@"unavailable"]) {

[_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"jerry.local"]];

}

}

}


- (void)dealloc {

[xmppStream removeDelegate:self];
[xmppRoster removeDelegate:self];

[xmppStream disconnect];
[xmppStream release];
[xmppRoster release];

[password release];

[viewController release];
[window release];
[super dealloc];
}


@end
38 changes: 38 additions & 0 deletions Classes/SMBuddyListViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// jabberClientViewController.h
// jabberClient
//
// Created by cesarerocchi on 7/13/11.
// Copyright 2011 studiomagnolia.com. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "JabberClientAppDelegate.h"
#import "SMLoginViewController.h"
#import "SMChatViewController.h"
#import "SMChatDelegate.h"

@interface SMBuddyListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, SMChatDelegate> {

UITableView *tView;
NSMutableArray *onlineBuddies;

UIView *addBuddyView;
UITextField *buddyField;


}

@property (nonatomic,retain) IBOutlet UITableView *tView;

@property (nonatomic,retain) IBOutlet UIView *addBuddyView;
@property (nonatomic,retain) IBOutlet UITextField *buddyField;



- (IBAction) addBuddy;
- (IBAction) showLogin;


@end

Loading

0 comments on commit ff0d4c4

Please sign in to comment.