Sauw Ming | bf16644 | 2010-03-30 12:33:52 +0000 | [diff] [blame] | 1 | // |
| 2 | // ConfigViewController.m |
| 3 | // ipjsua |
| 4 | // |
| 5 | // Created by Liong Sauw Ming on 3/25/10. |
| 6 | // Copyright 2010 Teluu Inc. (http://www.teluu.com). All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "ConfigViewController.h" |
| 10 | #import "ipjsuaAppDelegate.h" |
| 11 | |
| 12 | |
| 13 | @implementation ConfigViewController |
| 14 | @synthesize textView; |
| 15 | @synthesize button1; |
| 16 | @synthesize button2; |
| 17 | |
| 18 | bool kshow = false; |
| 19 | |
| 20 | /* |
| 21 | // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. |
| 22 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
| 23 | if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { |
| 24 | // Custom initialization |
| 25 | } |
| 26 | return self; |
| 27 | } |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | // Implement loadView to create a view hierarchy programmatically, without using a nib. |
| 32 | - (void)loadView { |
| 33 | } |
| 34 | */ |
| 35 | |
| 36 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
| 37 | { |
| 38 | // Dismiss the keyboard when the view outside the text view is touched. |
| 39 | [textView resignFirstResponder]; |
| 40 | [super touchesBegan:touches withEvent:event]; |
| 41 | } |
| 42 | |
| 43 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. |
| 44 | - (void)viewDidLoad { |
| 45 | [super viewDidLoad]; |
| 46 | |
| 47 | [textView setFont:[UIFont fontWithName:@"Courier" size:10]]; |
| 48 | ipjsuaAppDelegate *appd = (ipjsuaAppDelegate *)[[UIApplication sharedApplication] delegate]; |
| 49 | appd.cfgView = self; |
| 50 | |
| 51 | /* Load config file and display it in the text view */ |
| 52 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 53 | NSString *cfgPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/config.cfg"]; |
| 54 | textView.text = [NSMutableString stringWithContentsOfFile:cfgPath encoding:NSASCIIStringEncoding error:NULL]; |
| 55 | |
| 56 | /* Add keyboard show/hide notifications so that we can resize the text view */ |
| 57 | kshow = false; |
| 58 | NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 59 | [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil]; |
| 60 | [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil]; |
| 61 | |
| 62 | /* Add button press event-handlers */ |
| 63 | [self.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:(UIControlEvents)UIControlEventTouchDown]; |
| 64 | [self.button2 addTarget:self action:@selector(button2Pressed:) forControlEvents:(UIControlEvents)UIControlEventTouchDown]; |
| 65 | } |
| 66 | |
| 67 | -(void) keyboardWillShow:(NSNotification *) note |
| 68 | { |
| 69 | if (kshow) return; |
| 70 | |
| 71 | /* Shrink the text view area when the keyboard appears */ |
| 72 | [UIView beginAnimations:nil context:NULL]; |
| 73 | [UIView setAnimationDuration:0.3]; |
| 74 | CGRect r = self.textView.frame, t; |
| 75 | [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t]; |
| 76 | r.size.height -= t.size.height - 51; |
| 77 | self.textView.frame = r; |
| 78 | [UIView commitAnimations]; |
| 79 | kshow = true; |
| 80 | |
| 81 | [self.button1 setEnabled:true]; |
| 82 | [self.button1.titleLabel setEnabled:true]; |
| 83 | [self.button2 setEnabled:true]; |
| 84 | [self.button2.titleLabel setEnabled:true]; |
| 85 | } |
| 86 | |
| 87 | -(void) keyboardWillHide:(NSNotification *) note |
| 88 | { |
| 89 | CGRect r = self.textView.frame, t; |
| 90 | [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t]; |
| 91 | r.size.height += t.size.height - 51; |
| 92 | self.textView.frame = r; |
| 93 | kshow = false; |
| 94 | } |
| 95 | |
| 96 | - (void)button1Pressed:(id)sender { |
| 97 | /* Save the config file */ |
| 98 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 99 | NSString *cfgPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/config.cfg"]; |
| 100 | [self.textView.text writeToFile:cfgPath atomically:NO encoding:NSASCIIStringEncoding error:NULL]; |
| 101 | |
| 102 | [self.textView resignFirstResponder]; |
| 103 | [self.button1 setEnabled:false]; |
| 104 | [self.button1.titleLabel setEnabled:false]; |
| 105 | [self.button2 setEnabled:false]; |
| 106 | [self.button2.titleLabel setEnabled:false]; |
| 107 | } |
| 108 | |
| 109 | - (void)button2Pressed:(id)sender { |
| 110 | /* Reload the config file */ |
| 111 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 112 | NSString *cfgPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/config.cfg"]; |
| 113 | self.textView.text = [NSMutableString stringWithContentsOfFile:cfgPath encoding:NSASCIIStringEncoding error:NULL]; |
| 114 | |
| 115 | [self.textView resignFirstResponder]; |
| 116 | [self.button1 setEnabled:false]; |
| 117 | [self.button1.titleLabel setEnabled:false]; |
| 118 | [self.button2 setEnabled:false]; |
| 119 | [self.button2.titleLabel setEnabled:false]; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | // Override to allow orientations other than the default portrait orientation. |
| 124 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 125 | // Return YES for supported orientations |
| 126 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 127 | } |
| 128 | */ |
| 129 | |
| 130 | - (void)didReceiveMemoryWarning { |
| 131 | // Releases the view if it doesn't have a superview. |
| 132 | [super didReceiveMemoryWarning]; |
| 133 | |
| 134 | // Release any cached data, images, etc that aren't in use. |
| 135 | } |
| 136 | |
| 137 | - (void)viewDidUnload { |
| 138 | // Release any retained subviews of the main view. |
| 139 | // e.g. self.myOutlet = nil; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | - (void)dealloc { |
| 144 | [super dealloc]; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @end |