| 1 | // |
|---|
| 2 | // sync_upload.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_upload.h" |
|---|
| 10 | #import "sync_main.h" |
|---|
| 11 | |
|---|
| 12 | @implementation sync_upload |
|---|
| 13 | |
|---|
| 14 | @synthesize activity; |
|---|
| 15 | |
|---|
| 16 | extern sync_main *syncMainViewController; |
|---|
| 17 | extern UIView *gLandscapeView; |
|---|
| 18 | extern std::vector<std::string> gLocalFiles; |
|---|
| 19 | bool gIsUploading = false; |
|---|
| 20 | int gUploadNum; |
|---|
| 21 | |
|---|
| 22 | #pragma mark - |
|---|
| 23 | #pragma mark View lifecycle |
|---|
| 24 | |
|---|
| 25 | /* |
|---|
| 26 | - (void)viewDidLoad { |
|---|
| 27 | [super viewDidLoad]; |
|---|
| 28 | |
|---|
| 29 | // Uncomment the following line to preserve selection between presentations. |
|---|
| 30 | self.clearsSelectionOnViewWillAppear = NO; |
|---|
| 31 | |
|---|
| 32 | // Uncomment the following line to display an Edit button in the navigation bar for this view controller. |
|---|
| 33 | // self.navigationItem.rightBarButtonItem = self.editButtonItem; |
|---|
| 34 | } |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | /* |
|---|
| 38 | - (void)viewWillAppear:(BOOL)animated { |
|---|
| 39 | [super viewWillAppear:animated]; |
|---|
| 40 | } |
|---|
| 41 | */ |
|---|
| 42 | /* |
|---|
| 43 | - (void)viewDidAppear:(BOOL)animated { |
|---|
| 44 | [super viewDidAppear:animated]; |
|---|
| 45 | } |
|---|
| 46 | */ |
|---|
| 47 | /* |
|---|
| 48 | - (void)viewWillDisappear:(BOOL)animated { |
|---|
| 49 | [super viewWillDisappear:animated]; |
|---|
| 50 | } |
|---|
| 51 | */ |
|---|
| 52 | /* |
|---|
| 53 | - (void)viewDidDisappear:(BOOL)animated { |
|---|
| 54 | [super viewDidDisappear:animated]; |
|---|
| 55 | } |
|---|
| 56 | */ |
|---|
| 57 | /* |
|---|
| 58 | // Override to allow orientations other than the default portrait orientation. |
|---|
| 59 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|---|
| 60 | // Return YES for supported orientations |
|---|
| 61 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
|---|
| 62 | } |
|---|
| 63 | */ |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | #pragma mark - |
|---|
| 67 | #pragma mark Table view data source |
|---|
| 68 | |
|---|
| 69 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|---|
| 70 | // Return the number of sections. |
|---|
| 71 | return 1; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|---|
| 76 | // Return the number of rows in the section. |
|---|
| 77 | return (gLocalFiles.size() - 1); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | // Customize the appearance of table view cells. |
|---|
| 82 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|---|
| 83 | |
|---|
| 84 | static NSString *CellIdentifier = @"Cell"; |
|---|
| 85 | |
|---|
| 86 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|---|
| 87 | if (cell == nil) { |
|---|
| 88 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // Configure the cell... |
|---|
| 92 | cell.textLabel.text = [NSString stringWithUTF8String:gLocalFiles[indexPath.row + 1].c_str()]; |
|---|
| 93 | |
|---|
| 94 | return cell; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /* |
|---|
| 99 | // Override to support conditional editing of the table view. |
|---|
| 100 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|---|
| 101 | // Return NO if you do not want the specified item to be editable. |
|---|
| 102 | return YES; |
|---|
| 103 | } |
|---|
| 104 | */ |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | /* |
|---|
| 108 | // Override to support editing the table view. |
|---|
| 109 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|---|
| 110 | |
|---|
| 111 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
|---|
| 112 | // Delete the row from the data source |
|---|
| 113 | [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|---|
| 114 | } |
|---|
| 115 | else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|---|
| 116 | // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | */ |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /* |
|---|
| 123 | // Override to support rearranging the table view. |
|---|
| 124 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|---|
| 125 | } |
|---|
| 126 | */ |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /* |
|---|
| 130 | // Override to support conditional rearranging of the table view. |
|---|
| 131 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|---|
| 132 | // Return NO if you do not want the item to be re-orderable. |
|---|
| 133 | return YES; |
|---|
| 134 | } |
|---|
| 135 | */ |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | #pragma mark - |
|---|
| 139 | #pragma mark Table view delegate |
|---|
| 140 | |
|---|
| 141 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|---|
| 142 | |
|---|
| 143 | if (gIsUploading) |
|---|
| 144 | return; |
|---|
| 145 | |
|---|
| 146 | // upload the file... |
|---|
| 147 | gUploadNum = indexPath.row; |
|---|
| 148 | gIsUploading = true; |
|---|
| 149 | |
|---|
| 150 | [activity startAnimating]; |
|---|
| 151 | |
|---|
| 152 | [NSThread detachNewThreadSelector: @selector(doUpload) toTarget:self withObject:nil]; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | - (void)doUpload |
|---|
| 159 | { |
|---|
| 160 | NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; |
|---|
| 161 | |
|---|
| 162 | std::string upload = gLocalFiles[0] + gLocalFiles[gUploadNum + 1]; |
|---|
| 163 | NSString *uploadUrl = [NSString stringWithUTF8String:upload.c_str()]; |
|---|
| 164 | |
|---|
| 165 | NSData *postdata = [[NSData alloc] initWithContentsOfFile:uploadUrl]; |
|---|
| 166 | |
|---|
| 167 | NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; |
|---|
| 168 | [request setURL:[NSURL URLWithString:@"http://www.wesnothsync.com/upload.php?mobile=1"]]; |
|---|
| 169 | [request setHTTPMethod:@"POST"]; |
|---|
| 170 | NSString *boundary = @"---------------------------14737809831466499882746641449"; |
|---|
| 171 | NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; |
|---|
| 172 | [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; |
|---|
| 173 | NSMutableData *postbody = [NSMutableData data]; |
|---|
| 174 | [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; |
|---|
| 175 | [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploaded\"; filename=\"%s\"\r\n", gLocalFiles[gUploadNum + 1].c_str()] dataUsingEncoding:NSUTF8StringEncoding]]; |
|---|
| 176 | [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; |
|---|
| 177 | [postbody appendData:[NSData dataWithData:postdata]]; |
|---|
| 178 | [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; |
|---|
| 179 | [request setHTTPBody:postbody]; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; |
|---|
| 183 | NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; |
|---|
| 184 | NSLog(@"%@", returnString); |
|---|
| 185 | |
|---|
| 186 | [activity stopAnimating]; |
|---|
| 187 | gIsUploading = false; |
|---|
| 188 | |
|---|
| 189 | if ([returnData length] == 0) |
|---|
| 190 | { |
|---|
| 191 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; |
|---|
| 192 | [alert show]; |
|---|
| 193 | [alert release]; |
|---|
| 194 | } |
|---|
| 195 | else |
|---|
| 196 | { |
|---|
| 197 | std::string returnStr = [returnString UTF8String]; |
|---|
| 198 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Response" message:returnString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; |
|---|
| 199 | [alert show]; |
|---|
| 200 | [alert release]; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | [pool release]; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | #pragma mark - |
|---|
| 209 | #pragma mark Memory management |
|---|
| 210 | |
|---|
| 211 | - (void)didReceiveMemoryWarning { |
|---|
| 212 | // Releases the view if it doesn't have a superview. |
|---|
| 213 | [super didReceiveMemoryWarning]; |
|---|
| 214 | |
|---|
| 215 | // Relinquish ownership any cached data, images, etc that aren't in use. |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | - (void)viewDidUnload { |
|---|
| 219 | // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. |
|---|
| 220 | // For example: self.myOutlet = nil; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | - (void)dealloc { |
|---|
| 225 | [super dealloc]; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | -(IBAction)onDone:(id)sender |
|---|
| 229 | { |
|---|
| 230 | [self.view removeFromSuperview]; |
|---|
| 231 | |
|---|
| 232 | syncMainViewController = [[sync_main alloc] initWithNibName:@"sync_main" bundle:nil]; |
|---|
| 233 | |
|---|
| 234 | CGRect frameRect = syncMainViewController.view.frame; |
|---|
| 235 | #ifdef __IPAD__ |
|---|
| 236 | frameRect.origin.x = (1024-480)/2; |
|---|
| 237 | frameRect.origin.y = (768-320)/2; |
|---|
| 238 | #else |
|---|
| 239 | frameRect.origin.x = 0; |
|---|
| 240 | frameRect.origin.y = 0; |
|---|
| 241 | #endif |
|---|
| 242 | syncMainViewController.view.frame = frameRect; |
|---|
| 243 | [gLandscapeView addSubview:syncMainViewController.view]; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | @end |
|---|
| 247 | |
|---|