| 1 | // |
|---|
| 2 | // sync_login.mm |
|---|
| 3 | // wesnoth |
|---|
| 4 | // |
|---|
| 5 | // Created by Kyle Poole on 5/10/10. |
|---|
| 6 | // Copyright 2010 __MyCompanyName__. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "sync_login.h" |
|---|
| 10 | #include "game_preferences.hpp" |
|---|
| 11 | |
|---|
| 12 | #include <vector> |
|---|
| 13 | #include "string_utils.hpp" |
|---|
| 14 | #include "sync_main.h" |
|---|
| 15 | #include "filesystem.hpp" |
|---|
| 16 | |
|---|
| 17 | @implementation sync_login |
|---|
| 18 | |
|---|
| 19 | @synthesize email, password, activity, loginButton, cancelButton; |
|---|
| 20 | |
|---|
| 21 | extern UIView *gLandscapeView; |
|---|
| 22 | extern bool gPauseForOpenFeint; |
|---|
| 23 | |
|---|
| 24 | extern std::vector<std::string> gServerFiles; |
|---|
| 25 | std::vector<std::string> gServerFiles; |
|---|
| 26 | extern std::vector<std::string> gLocalFiles; |
|---|
| 27 | std::vector<std::string> gLocalFiles; |
|---|
| 28 | extern sync_main *syncMainViewController; |
|---|
| 29 | sync_main *syncMainViewController; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. |
|---|
| 34 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
|---|
| 35 | if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { |
|---|
| 36 | // Custom initialization |
|---|
| 37 | } |
|---|
| 38 | return self; |
|---|
| 39 | } |
|---|
| 40 | */ |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. |
|---|
| 44 | - (void)viewDidLoad { |
|---|
| 45 | [super viewDidLoad]; |
|---|
| 46 | email.text = [NSString stringWithUTF8String: preferences::sync_login_str().c_str()]; |
|---|
| 47 | password.text = [NSString stringWithUTF8String:preferences::sync_password().c_str()]; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /* |
|---|
| 53 | // Override to allow orientations other than the default portrait orientation. |
|---|
| 54 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|---|
| 55 | // Return YES for supported orientations |
|---|
| 56 | return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|---|
| 57 | } |
|---|
| 58 | */ |
|---|
| 59 | |
|---|
| 60 | - (void)didReceiveMemoryWarning { |
|---|
| 61 | // Releases the view if it doesn't have a superview. |
|---|
| 62 | [super didReceiveMemoryWarning]; |
|---|
| 63 | |
|---|
| 64 | // Release any cached data, images, etc that aren't in use. |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | - (void)viewDidUnload { |
|---|
| 68 | [super viewDidUnload]; |
|---|
| 69 | // Release any retained subviews of the main view. |
|---|
| 70 | // e.g. self.myOutlet = nil; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | - (void)dealloc { |
|---|
| 75 | [super dealloc]; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | - (void) doLogin |
|---|
| 79 | { |
|---|
| 80 | NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; |
|---|
| 81 | |
|---|
| 82 | preferences::set_sync_login([email.text UTF8String]); |
|---|
| 83 | preferences::set_sync_password([password.text UTF8String]); |
|---|
| 84 | |
|---|
| 85 | NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", email.text, password.text]; |
|---|
| 86 | NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; |
|---|
| 87 | |
|---|
| 88 | NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; |
|---|
| 89 | |
|---|
| 90 | NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; |
|---|
| 91 | [request setURL:[NSURL URLWithString:@"http://www.wesnothsync.com/index.php?mobile=1"]]; |
|---|
| 92 | [request setHTTPMethod:@"POST"]; |
|---|
| 93 | [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; |
|---|
| 94 | [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; |
|---|
| 95 | [request setHTTPBody:postData]; |
|---|
| 96 | |
|---|
| 97 | NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; |
|---|
| 98 | NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; |
|---|
| 99 | NSLog(@"%@", returnString); |
|---|
| 100 | |
|---|
| 101 | std::string returnStr = [returnString UTF8String]; |
|---|
| 102 | |
|---|
| 103 | [activity stopAnimating]; |
|---|
| 104 | loginButton.enabled = YES; |
|---|
| 105 | cancelButton.enabled = YES; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | if (returnStr.size() == 0) |
|---|
| 109 | { |
|---|
| 110 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; |
|---|
| 111 | [alert show]; |
|---|
| 112 | [alert release]; |
|---|
| 113 | } |
|---|
| 114 | else if (returnStr.find("Error") == 0) |
|---|
| 115 | { |
|---|
| 116 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Response" message:returnString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; |
|---|
| 117 | [alert show]; |
|---|
| 118 | [alert release]; |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | gServerFiles = utils::split(returnStr, '\n'); |
|---|
| 123 | |
|---|
| 124 | std::vector<shared_string> localFiles; |
|---|
| 125 | get_files_in_dir(get_saves_dir(), &localFiles); |
|---|
| 126 | gLocalFiles.clear(); |
|---|
| 127 | gLocalFiles.push_back(get_saves_dir() + "/"); |
|---|
| 128 | for(int i=0; i < localFiles.size(); i++) |
|---|
| 129 | { |
|---|
| 130 | gLocalFiles.push_back(localFiles[i]); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | [self.view removeFromSuperview]; |
|---|
| 134 | |
|---|
| 135 | syncMainViewController = [[sync_main alloc] initWithNibName:@"sync_main" bundle:nil]; |
|---|
| 136 | |
|---|
| 137 | CGRect frameRect = syncMainViewController.view.frame; |
|---|
| 138 | #ifdef __IPAD__ |
|---|
| 139 | frameRect.origin.x = (1024-480)/2; |
|---|
| 140 | frameRect.origin.y = (768-320)/2; |
|---|
| 141 | #else |
|---|
| 142 | frameRect.origin.x = 0; |
|---|
| 143 | frameRect.origin.y = 0; |
|---|
| 144 | #endif |
|---|
| 145 | syncMainViewController.view.frame = frameRect; |
|---|
| 146 | [gLandscapeView addSubview:syncMainViewController.view]; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | [pool release]; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | -(IBAction)login:(id)sender |
|---|
| 153 | { |
|---|
| 154 | [activity startAnimating]; |
|---|
| 155 | loginButton.enabled = NO; |
|---|
| 156 | cancelButton.enabled = NO; |
|---|
| 157 | |
|---|
| 158 | [NSThread detachNewThreadSelector: @selector(doLogin) toTarget:self withObject:nil]; |
|---|
| 159 | |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | -(IBAction)cancel:(id)sender |
|---|
| 163 | { |
|---|
| 164 | [self.view removeFromSuperview]; |
|---|
| 165 | gLandscapeView.hidden = YES; |
|---|
| 166 | gPauseForOpenFeint = false; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | -(IBAction)emailNext:(id)sender |
|---|
| 170 | { |
|---|
| 171 | [email resignFirstResponder]; |
|---|
| 172 | [password becomeFirstResponder]; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | -(IBAction)hideEmailKeyboard:(id)sender |
|---|
| 176 | { |
|---|
| 177 | [email resignFirstResponder]; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | -(IBAction)hidePasswordKeyboard:(id)sender |
|---|
| 181 | { |
|---|
| 182 | [password resignFirstResponder]; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | - (BOOL)textFieldShouldReturn:(UITextField *)textField |
|---|
| 186 | { |
|---|
| 187 | [textField resignFirstResponder]; |
|---|
| 188 | |
|---|
| 189 | if ([textField isEqual:email]) |
|---|
| 190 | { |
|---|
| 191 | [password becomeFirstResponder]; |
|---|
| 192 | } |
|---|
| 193 | return YES; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | @end |
|---|