From 2869eec4281c855939e6b020c64dded74def442f Mon Sep 17 00:00:00 2001
From: Maxime Graulich <maxime.graulich@gmail.com>
Date: Tue, 18 Feb 2014 09:20:33 +0000
Subject: [PATCH] iOS: some fixes

---
 contrib/mobile/iOS/Onelab/AppDelegate.mm      |  7 +++
 contrib/mobile/iOS/Onelab/EAGLView.h          |  1 +
 contrib/mobile/iOS/Onelab/EAGLView.mm         |  4 +-
 .../mobile/iOS/Onelab/ModelListController.mm  |  1 -
 .../mobile/iOS/Onelab/ModelViewController.mm  | 50 ++++++++++---------
 .../iOS/Onelab/ParametersViewController.mm    |  3 +-
 contrib/mobile/iOS/Onelab/Utils.mm            |  4 +-
 7 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/contrib/mobile/iOS/Onelab/AppDelegate.mm b/contrib/mobile/iOS/Onelab/AppDelegate.mm
index 863f1df045..e8ae08fe07 100644
--- a/contrib/mobile/iOS/Onelab/AppDelegate.mm
+++ b/contrib/mobile/iOS/Onelab/AppDelegate.mm
@@ -8,6 +8,7 @@
 
 #import "AppDelegate.h"
 #import "ModelListController.h"
+#import "Utils.h"
 
 @implementation AppDelegate
 
@@ -20,6 +21,12 @@
         self.splitViewController = [storyboard instantiateViewControllerWithIdentifier:@"SplitViewController"];
     }
     compute = false;
+	if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
+    {
+        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
+        [[NSUserDefaults standardUserDefaults] synchronize];
+        [Utils copyRes];
+    }
     return YES;
 }
 							
diff --git a/contrib/mobile/iOS/Onelab/EAGLView.h b/contrib/mobile/iOS/Onelab/EAGLView.h
index caa7c1eb1d..540514329d 100644
--- a/contrib/mobile/iOS/Onelab/EAGLView.h
+++ b/contrib/mobile/iOS/Onelab/EAGLView.h
@@ -29,6 +29,7 @@
 @public
 	/* our GModel drawing class */
     drawContext *mContext;
+	BOOL rotate;
 }
 
 - (void)drawView;
diff --git a/contrib/mobile/iOS/Onelab/EAGLView.mm b/contrib/mobile/iOS/Onelab/EAGLView.mm
index 8e8086879a..62b72b02bc 100644
--- a/contrib/mobile/iOS/Onelab/EAGLView.mm
+++ b/contrib/mobile/iOS/Onelab/EAGLView.mm
@@ -82,6 +82,7 @@
             break;
         case 3:
         {
+			rotate = true;
             mContext->eventHandler(3,position.x,position.y);
         }
             break;
@@ -174,7 +175,8 @@
     CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
 
     CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
-
+	free(buffer);
+	free(buffer2);
     return [UIImage imageWithCGImage:imageRef];
 }
 
diff --git a/contrib/mobile/iOS/Onelab/ModelListController.mm b/contrib/mobile/iOS/Onelab/ModelListController.mm
index a96ccc887a..ad6357b70a 100644
--- a/contrib/mobile/iOS/Onelab/ModelListController.mm
+++ b/contrib/mobile/iOS/Onelab/ModelListController.mm
@@ -25,7 +25,6 @@
 	
     models = [[NSMutableArray alloc] init];
     NSString *docsPath = [Utils getApplicationDocumentsDirectory];
-    [Utils copyRes];
     NSArray *docs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docsPath error:NULL];
     for(NSString* doc in docs) {
         NSString *docPath = [NSString stringWithFormat:@"%@/%@/", docsPath, doc];
diff --git a/contrib/mobile/iOS/Onelab/ModelViewController.mm b/contrib/mobile/iOS/Onelab/ModelViewController.mm
index bcd14710ae..5df8f41098 100644
--- a/contrib/mobile/iOS/Onelab/ModelViewController.mm
+++ b/contrib/mobile/iOS/Onelab/ModelViewController.mm
@@ -57,7 +57,8 @@
 
 -(void)viewWillAppear:(BOOL)animated
 {
-	if(self.initialModel != nil) {
+	//if(self.initialModel != nil) {
+	if([[UIDevice currentDevice].model isEqualToString:@"iPad"] || [[UIDevice currentDevice].model isEqualToString:@"iPad Simulator"]) {
 		_loadingAlert = [[UIAlertView alloc] initWithTitle:@"Please wait..." message: @"The model is loading" delegate: self cancelButtonTitle: nil otherButtonTitles: nil];
 		[_loadingAlert show];
 	}
@@ -156,7 +157,8 @@
 	[_progressIndicator startAnimating];
 	[[UIApplication sharedApplication] cancelAllLocalNotifications];
 	dispatch_group_t group = dispatch_group_create();
-	dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+	dispatch_queue_t queue = dispatch_queue_create("org.geuz.onelab", NULL);
+	dispatch_group_async(group, queue, ^{
 		_computeBackgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
 			[[UIApplication sharedApplication] endBackgroundTask: _computeBackgroundTaskIdentifier];
 			_computeBackgroundTaskIdentifier = UIBackgroundTaskInvalid;
@@ -178,7 +180,7 @@
 		_computeBackgroundTaskIdentifier = UIBackgroundTaskInvalid;
     });
 	
-	dispatch_group_notify(group, dispatch_get_main_queue(), ^{
+	dispatch_group_notify(group, queue, ^{
 		[UIApplication sharedApplication].applicationIconBadgeNumber = -1;
 		[_runStopButton setAction:@selector(compute)];
 		[_runStopButton setTitle:@"Run"];
@@ -212,18 +214,19 @@
 -(void)prevAnimation { animation_prev(); [self requestRender]; }
 -(IBAction)pinch:(UIPinchGestureRecognizer *)sender
 {
-    if([sender numberOfTouches] > 2) return;
-    float mScale = scaleFactor;
-    if (sender.state == UIGestureRecognizerStateBegan)
-        mScale = scaleFactor;
-    else if(sender.state == UIGestureRecognizerStateChanged)
-        mScale = scaleFactor * [sender scale];
-    else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled){
-        scaleFactor *= [sender scale];
-        mScale = scaleFactor;
-    }
-    mScale = MAX(0.1, mScale);
-    glView->mContext->eventHandler(2,mScale);
+    if(!glView->rotate && [sender numberOfTouches] == 2) {
+		float mScale = scaleFactor;
+		if (sender.state == UIGestureRecognizerStateBegan)
+			mScale = scaleFactor;
+		else if(sender.state == UIGestureRecognizerStateChanged)
+			mScale = scaleFactor * [sender scale];
+		else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled){
+			scaleFactor *= [sender scale];
+			mScale = scaleFactor;
+		}
+		mScale = MAX(0.1, mScale);
+		glView->mContext->eventHandler(2,mScale);
+	}
     [glView drawView];
 }
 
@@ -244,6 +247,7 @@
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint touchPoint = [touch locationInView:self.view];
     glView->mContext->eventHandler(4, touchPoint.x, touchPoint.y);
+	glView->rotate = false;
 }
 
 - (IBAction)singleTap:(UITapGestureRecognizer *)sender {
@@ -313,14 +317,9 @@
 	if(buttonIndex == 0) [_errors removeAllObjects];
 	else [_errors removeLastObject];
 	if(_errors.count > 0) {
-		[_errorAlert setMessage:[_errors lastObject]];
+		//[_errorAlert setMessage:[_errors lastObject]];
 		[_errorAlert show];
 	}
-	else {
-		[_errorAlert dismissWithClickedButtonIndex:0 animated:YES];
-		//[_errorAlert release];
-		_errorAlert = nil;
-	}
 }
 
 #pragma mark - Split view
@@ -371,10 +370,13 @@ void getBitmap(void *self, const char *text, int textsize, unsigned char **map,
     [lbl setBackgroundColor:[UIColor clearColor]];
     CGSize lblSize = [[lbl text] sizeWithAttributes:@{NSFontAttributeName:[lbl font]}];
     *realWidth = lblSize.width;
-    int i;
-    for(i=2;i<*realWidth;i*=2); *width = i;
+	int i=2;
+	while(i<*realWidth) i*=2;
+	*width = i;
     *height = lblSize.height;
-    for(i=2;i<*height;i*=2); *height = i;
+	i=2;
+	while(i<*height) i*=2;
+    *height = i;
     UIGraphicsBeginImageContextWithOptions(CGSizeMake(*width, *height), NO, 0.0);
     [lbl.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
diff --git a/contrib/mobile/iOS/Onelab/ParametersViewController.mm b/contrib/mobile/iOS/Onelab/ParametersViewController.mm
index d29af6217b..14f2dbcf30 100644
--- a/contrib/mobile/iOS/Onelab/ParametersViewController.mm
+++ b/contrib/mobile/iOS/Onelab/ParametersViewController.mm
@@ -22,7 +22,7 @@
 - (void)awakeFromNib
 {
     self.clearsSelectionOnViewWillAppear = NO;
-    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
+	self.preferredContentSize = CGSizeMake(320.0, 600.0);
     [super awakeFromNib];
 }
 
@@ -41,7 +41,6 @@
 	
 	[self.navigationController setToolbarHidden:NO];
 	control = [[UISegmentedControl alloc] initWithItems:[[NSArray alloc] initWithObjects:@"Model", @"Display", nil]];
-	control.segmentedControlStyle = UISegmentedControlStyleBar;
 	UIBarButtonItem *controlBtn = [[UIBarButtonItem alloc] initWithCustomView:control];
 	UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 	self.toolbarItems = [[NSArray alloc] initWithObjects:flexibleSpace, controlBtn, flexibleSpace, nil];
diff --git a/contrib/mobile/iOS/Onelab/Utils.mm b/contrib/mobile/iOS/Onelab/Utils.mm
index 62f97789b8..357ac21e14 100644
--- a/contrib/mobile/iOS/Onelab/Utils.mm
+++ b/contrib/mobile/iOS/Onelab/Utils.mm
@@ -24,11 +24,11 @@
     NSString *docPath = [paths objectAtIndex:0]; //Get the docs directory
     
     NSArray *resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resPath error:NULL];
-    
+    //[[NSFileManager defaultManager] removeItemAtPath:[docPath stringByAppendingString:@"/"] error:nil];
     for (NSString* obj in resContents){
         NSError* error;
         if (![[NSFileManager defaultManager] copyItemAtPath:[resPath stringByAppendingPathComponent:obj] toPath:[docPath stringByAppendingPathComponent:obj] error:&error])
-            NSLog(@"Error: %@", error);;
+            NSLog(@"Error: %@", error);
     }
 }
 
-- 
GitLab