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