Debian Codegraph @

Xpjproject/pjsip-apps/src/ipjsystest/Classes/RootViewController.m

raw
1 /*
2 * Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #import "RootViewController.h"
19 #import "gui.h"
20 #import "systest.h"
21
22 /* Sleep interval duration, change to shorter duration for better
23 * interaction but make sure there is enough time for the other
24 * thread to do what it's supposed to do
25 */
26 #define SLEEP_INTERVAL 0.5
27
28 @implementation RootViewController
29 @synthesize titles;
30 @synthesize menus;
31 @synthesize testView;
32
33 RootViewController *view;
34
35 bool systest_initialized;
36 bool thread_quit;
37 gui_menu *gmenu;
38 int section;
39 int row;
40 const char *ctitle;
41 const char *cmsg;
42 enum gui_flag cflag;
43
44 pj_status_t gui_init(gui_menu *menu)
45 {
46 PJ_UNUSED_ARG(menu);
47 return PJ_SUCCESS;
48 }
49
50 /* Run GUI main loop */
51 pj_status_t gui_start(gui_menu *menu)
52 {
53 view.titles = [NSMutableArray arrayWithCapacity:menu->submenu_cnt];
54 view.menus = [NSMutableArray arrayWithCapacity:menu->submenu_cnt];
55 NSMutableArray *smenu;
56 for (int i = 0; i < menu->submenu_cnt; i++) {
57 NSString *str = [NSString stringWithFormat:@"%s" , menu->submenus[i]->title];
58 [view.titles addObject: str];
59 smenu = [NSMutableArray arrayWithCapacity:menu->submenus[i]->submenu_cnt];
60 /* We do not need the last two menus of the "Tests" menu (NULL and "Exit"),
61 * so subtract by 2
62 */
63 for (int j = 0; j < menu->submenus[i]->submenu_cnt - (i==0? 2: 0); j++) {
64 str = [NSString stringWithFormat:@"%s" , menu->submenus[i]->submenus[j]->title];
65 [smenu addObject:str];
66 }
67 [view.menus addObject:smenu];
68 }
69 gmenu = menu;
70
71 return PJ_SUCCESS;
72 }
73
74 /* Signal GUI mainloop to stop */
75 void gui_destroy(void)
76 {
77 }
78
79 /* AUX: display messagebox */
80 enum gui_key gui_msgbox(const char *title, const char *message, enum gui_flag flag)
81 {
82 ctitle = title;
83 cmsg = message;
84 cflag = flag;
85 [view performSelectorOnMainThread:@selector(showMsg) withObject:nil waitUntilDone:YES];
86
87 view.testView.key = 0;
88 while(view.testView.key == 0) {
89 /* Let the main thread do its job (refresh the view) while we wait for
90 * user interaction (button click)
91 */
92 [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
93 }
94
95 if (view.testView.key == 1)
96 return KEY_OK;
97 else
98 return (flag == WITH_YESNO? KEY_NO: KEY_CANCEL);
99 }
100
101 /* AUX: sleep */
102 void gui_sleep(unsigned sec)
103 {
104 [NSThread sleepForTimeInterval:sec];
105 }
106
107 - (void)viewDidLoad {
108 [super viewDidLoad];
109
110 view = self;
111
112 /* Get a writable path for output files */
113 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
114 NSString *documentsDirectory = [paths objectAtIndex:0];
115 [documentsDirectory getCString:doc_path maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
116 strncat(doc_path, "/", PATH_LENGTH);
117
118 /* Get path for our test resources (the wav files) */
119 NSString *resPath = [[NSBundle mainBundle] resourcePath];
120 [resPath getCString:res_path maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
121 strncat(res_path, "/", PATH_LENGTH);
122
123 systest_initialized = false;
124 thread_quit = false;
125 [NSThread detachNewThreadSelector:@selector(startTest) toTarget:self withObject:nil];
126 /* Let our new thread initialize */
127 while (!systest_initialized) {
128 [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
129 }
130
131 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
132 // self.navigationItem.rightBarButtonItem = self.editButtonItem;
133 }
134
135 /*
136 - (void)viewWillAppear:(BOOL)animated {
137 [super viewWillAppear:animated];
138 }
139 */
140 /*
141 - (void)viewDidAppear:(BOOL)animated {
142 [super viewDidAppear:animated];
143 }
144 */
145 /*
146 - (void)viewWillDisappear:(BOOL)animated {
147 [super viewWillDisappear:animated];
148 }
149 */
150 /*
151 - (void)viewDidDisappear:(BOOL)animated {
152 [super viewDidDisappear:animated];
153 }
154 */
155
156 /*
157 // Override to allow orientations other than the default portrait orientation.
158 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
159 // Return YES for supported orientations.
160 return (interfaceOrientation == UIInterfaceOrientationPortrait);
161 }
162 */
163
164 - (void)didReceiveMemoryWarning {
165 // Releases the view if it doesn't have a superview.
166 [super didReceiveMemoryWarning];
167
168 // Release any cached data, images, etc that aren't in use.
169 }
170
171 - (void)viewDidUnload {
172 // Release anything that can be recreated in viewDidLoad or on demand.
173 // e.g. self.myOutlet = nil;
174 self.titles = nil;
175 self.menus = nil;
176
177 thread_quit = true;
178 [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
179 }
180
181
182 #pragma mark Table view methods
183
184
185 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
186 return [titles count];
187 }
188
189
190 // Customize the number of rows in the table view.
191 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
192 return [[menus objectAtIndex:section] count];
193 }
194
195
196 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
197 // The header for the section is the region name -- get this from the region at the section index.
198 return [titles objectAtIndex:section];
199 }
200
201 // Customize the appearance of table view cells.
202 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
203
204 static NSString *CellIdentifier = @"Cell";
205
206 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
207 if (cell == nil) {
208 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
209 }
210
211 // Configure the cell.
212 cell.textLabel.text = [[menus objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
213
214 return cell;
215 }
216
217 - (void)startTest {
218 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
219 if (systest_init() != PJ_SUCCESS) {
220 [pool release];
221 return;
222 }
223
224 systest_run();
225
226 systest_initialized = 1;
227 while(!thread_quit) {
228 section = -1;
229 while (section == -1) {
230 [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
231 }
232 (*gmenu->submenus[section]->submenus[row]->handler)();
233 cmsg = NULL;
234 [view performSelectorOnMainThread:@selector(showMsg) withObject:nil waitUntilDone:YES];
235 }
236
237 systest_deinit();
238 [pool release];
239 }
240
241 - (void)showMsg {
242 if (cmsg == NULL) {
243 self.testView.testDesc.text = [self.testView.testDesc.text stringByAppendingString: @"Finished"];
244 [self.testView.testDesc scrollRangeToVisible:NSMakeRange([self.testView.testDesc.text length] - 1, 1)];
245 [self.testView.button1 setHidden:true];
246 [self.testView.button2 setHidden:true];
247 return;
248 }
249 self.testView.title = [NSString stringWithFormat:@"%s", ctitle];
250 self.testView.testDesc.text = [self.testView.testDesc.text stringByAppendingString: [NSString stringWithFormat:@"%s\n\n", cmsg]];
251 [self.testView.testDesc scrollRangeToVisible:NSMakeRange([self.testView.testDesc.text length] - 1, 1)];
252
253 [self.testView.button1 setHidden:false];
254 [self.testView.button2 setHidden:false];
255 if (cflag == WITH_YESNO) {
256 [self.testView.button1 setTitle:@"Yes" forState:UIControlStateNormal];
257 [self.testView.button2 setTitle:@"No" forState:UIControlStateNormal];
258 } else if (cflag == WITH_OK) {
259 [self.testView.button1 setTitle:@"OK" forState:UIControlStateNormal];
260 [self.testView.button2 setHidden:true];
261 } else if (cflag == WITH_OKCANCEL) {
262 [self.testView.button1 setTitle:@"OK" forState:UIControlStateNormal];
263 [self.testView.button2 setTitle:@"Cancel" forState:UIControlStateNormal];
264 }
265 }
266
267
268 // Override to support row selection in the table view.
269 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
270
271 // Navigation logic may go here -- for example, create and push another view controller.
272 // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
273 // [self.navigationController pushViewController:anotherViewController animated:YES];
274 // [anotherViewController release];
275
276 if (self.testView == nil) {
277 self.testView = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:[NSBundle mainBundle]];
278 }
279
280 [self.navigationController pushViewController:self.testView animated:YES];
281 self.testView.title = [[menus objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
282 self.testView.testDesc.text = @"";
283 section = indexPath.section;
284 row = indexPath.row;
285 }
286
287
288 /*
289 // Override to support conditional editing of the table view.
290 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
291 // Return NO if you do not want the specified item to be editable.
292 return YES;
293 }
294 */
295
296
297 /*
298 // Override to support editing the table view.
299 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
300
301 if (editingStyle == UITableViewCellEditingStyleDelete) {
302 // Delete the row from the data source.
303 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
304 }
305 else if (editingStyle == UITableViewCellEditingStyleInsert) {
306 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
307 }
308 }
309 */
310
311
312 /*
313 // Override to support rearranging the table view.
314 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
315 }
316 */
317
318
319 /*
320 // Override to support conditional rearranging of the table view.
321 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
322 // Return NO if you do not want the item to be re-orderable.
323 return YES;
324 }
325 */
326
327
328 - (void)dealloc {
329 [super dealloc];
330 }
331
332
333 @end
334