source: gs2-extensions/iOS-1.x/trunk/GreenstoneApplication.mm@ 22548

Last change on this file since 22548 was 22548, checked in by davidb, 12 years ago

Original version of Greenstone2 app for iPhone/iPod-touch

File size: 7.7 KB
Line 
1#import <Foundation/NSDictionary.h>
2
3#import "GreenstoneApplication.h"
4#import "GreenstoneButtonBar.h"
5
6#import "GreenstoneAPI.h"
7
8static GreenstoneApplication* _toplevelApp = nil;
9static UITransitionView* _transView = nil;
10static SimpleWebView* _webView = nil;
11static GreenstoneAPI* _greenstoneAPI = nil;
12
13static NSString* _inCollection = nil;
14static NSMutableDictionary* _collectionToRowId = nil;
15
16@implementation GreenstoneApplication
17
18+(GreenstoneApplication*) toplevelApp {
19 return (_toplevelApp);
20}
21
22+(GreenstoneAPI*) greenstoneAPI {
23 return (_greenstoneAPI);
24}
25
26+(CGRect)workAreaRect
27{
28 CGRect screen_rect = [UIHardware fullScreenApplicationContentRect];
29
30 int wa_x_dim = screen_rect.size.width;
31 int wa_y_dim = screen_rect.size.height - BOT_NAVBAR_HEIGHT;
32
33 CGRect wa_rect = CGRectMake(0.0f,0.0f,wa_x_dim,wa_y_dim);
34
35 return wa_rect;
36}
37
38
39+(void) setInCollection:(NSString*)collection
40{
41 NSLog(@"Setting in collection = %@",collection);
42 if (_inCollection != nil) {
43 // [_inCollection release]; // **** memory leak????
44 }
45 _inCollection = nil;
46
47 if (collection != nil) {
48 _inCollection = [[NSString alloc] initWithString:collection];
49 }
50}
51
52+(int) inCollectionRowId
53{
54 if (_inCollection == nil) {
55 return -1;
56 }
57
58 NSNumber* rowid_num = [_collectionToRowId objectForKey:_inCollection];
59 int rowid = [rowid_num intValue];
60
61 return rowid;
62}
63
64-(NSString*) languagePref
65{
66 int rowid = [GreenstoneApplication inCollectionRowId];
67
68 if (rowid>=0) {
69
70 PrefView* prefview = (PrefView*)[_prefCascadeView directEntry:rowid];
71 NSString* lang = [prefview languagePref];
72
73 return lang; // lang is allocated, make sure calling method does release
74 }
75 else {
76 return nil;
77 }
78}
79
80
81
82-(void) applicationDidFinishLaunching:(NSNotification *)aNotification
83{
84 _toplevelApp = self;
85 _greenstoneAPI = new GreenstoneAPI(self);
86
87 _inCollection = nil;
88 _collectionToRowId = nil;
89
90 if (!_greenstoneAPI->sanityCheck()) {
91 return;
92 }
93
94 CGRect screen_rect = [UIHardware fullScreenApplicationContentRect];
95 screen_rect.origin.x = screen_rect.origin.y = 0.0f;
96
97 _window = [[UIWindow alloc] initWithContentRect: screen_rect];
98
99 CGRect workarea_rect = [GreenstoneApplication workAreaRect];
100
101 // Set up top-level browsing and search menu CascadeViews
102 _browseCascadeView = [BrowseCascadeView alloc];
103 _browseCascadeView = [_browseCascadeView initWithFrame:workarea_rect withTitle: @"Browse collection:"];
104
105 _queryCascadeView = [QueryCascadeView alloc];
106 _queryCascadeView = [_queryCascadeView initWithFrame:workarea_rect withTitle: @"Search collection:"];
107
108 _prefCascadeView = [PrefCascadeView alloc];
109 _prefCascadeView = [_prefCascadeView initWithFrame:workarea_rect withTitle: @"Collection preferences:"];
110
111 NSMutableArray* collist = _greenstoneAPI->collectionList();
112
113 int collist_len = [collist count];
114 _collectionToRowId = [[NSMutableDictionary alloc] initWithCapacity:collist_len];
115
116 NSEnumerator* collist_enumerator = [collist objectEnumerator];
117 int i = 0;
118 NSString* colnamedir;
119 while (colnamedir = [collist_enumerator nextObject]) {
120 ColInfoResponse_t& colinfo = _greenstoneAPI->collectInfo(colnamedir);
121 NSString* colname = _greenstoneAPI->collectionName(colinfo);
122
123 [_browseCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
124 [_queryCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
125 [_prefCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
126
127 NSNumber* inum = [[NSNumber alloc] initWithInt: i];
128 [_collectionToRowId setObject:inum forKey:colnamedir];
129 [inum release]; // **** should probably add this in
130
131 i++;
132 }
133 [collist release];
134
135
136 if (_webView == nil) {
137 // setup Web browser view
138 _webView = [[SimpleWebView alloc] initWithFrame:workarea_rect];
139 }
140
141 if (_transView == nil) {
142 // Set up transition view
143 _transView = [[UITransitionView alloc] initWithFrame: workarea_rect];
144 }
145
146 // Set up button bar
147 int butbar_x_org = 0.0f;
148 int butbar_y_org = workarea_rect.size.height+1;
149 int butbar_x_dim = screen_rect.size.width;
150 int butbar_y_dim = BOT_NAVBAR_HEIGHT;
151
152 CGRect butbarrect = CGRectMake(butbar_x_org,butbar_y_org,butbar_x_dim,butbar_y_dim);
153 _butbar = [[GreenstoneButtonBar alloc] initInView:_window withFrame:butbarrect];
154
155 // Usual standard window stuff
156 [_window orderFront: self];
157 [_window makeKey: self];
158 [_window _setHidden: NO];
159
160 [_window addSubview: _transView ];
161 [_window addSubview: _butbar];
162
163 [_transView transition:0 toView:_browseCascadeView];
164}
165
166
167enum compassDirection { itNorth=0, itEast=1, itSouth=2, itWest=3};
168
169static int compass_degrees[4] = { 0, 90, 180, 270 };
170
171static int current_orientation = kOrientationVertical;
172static int current_compass_dir = itNorth;
173static int current_compass_degs = 0;
174
175-(void) deviceOrientationChanged:(struct __GSEvent *)event
176{
177 int new_orientation = [UIHardware deviceOrientation: YES];
178
179 int new_compass_dir = -1;
180
181 switch (new_orientation)
182 {
183 case kOrientationVertical:
184 new_compass_dir = itNorth;
185 break;
186
187 case kOrientationHorizontalLeft:
188 new_compass_dir = itEast;
189 break;
190
191 case kOrientationHorizontalRight:
192 new_compass_dir = itWest;
193 break;
194 case kOrientationVerticalUpsideDown:
195 new_compass_dir = itSouth;
196 break;
197 }
198
199 if (new_compass_dir>=0) {
200 int new_compass_degs = compass_degrees[new_compass_dir];
201 int degs_delta = current_compass_degs - new_compass_degs;
202
203 if (degs_delta != 0) {
204
205 current_orientation = new_orientation;
206 current_compass_degs = new_compass_degs;
207 current_compass_dir = new_compass_dir;
208
209 [_webView setTransform:CGAffineTransformIdentity];
210 [_webView setRotationBy:new_compass_degs];
211 }
212 }
213
214 [super deviceOrientationChanged:event];
215}
216
217
218
219-(void) buttonBarClicked:(id)sender
220{
221 int button = [sender tag];
222 int transStyle = UITransitionNextShiftUp;
223
224 int rowid = [GreenstoneApplication inCollectionRowId];
225
226 switch (button)
227 {
228 case GSBBBrowse:
229 if (rowid>=0) {
230 [_transView transition:transStyle toView:[_browseCascadeView directEntry:rowid]];
231 }
232 else {
233 [_transView transition:transStyle toView:_browseCascadeView];
234 }
235 break;
236
237 case GSBBQuery:
238 if (rowid>=0) {
239 [_transView transition:transStyle toView:[_queryCascadeView directEntry:rowid]];
240 }
241 else {
242 [_transView transition:transStyle toView:_queryCascadeView];
243 }
244 break;
245
246 case GSBBPref:
247 if (rowid>=0) {
248 [_transView transition:transStyle toView:[_prefCascadeView directEntry:rowid]];
249 }
250 else {
251 [_transView transition:transStyle toView:_prefCascadeView];
252 }
253 break;
254
255 default:
256 NSLog(@"Unrecognised button bar click: %d",button);
257 break;
258 }
259}
260
261
262+(void) transitionView:(int)eventNum fromView:(UIView*)from toView:(UIView*)to
263{
264 [_transView transition:eventNum fromView:from toView:to];
265}
266
267+(void) startWebViewBusy
268{
269 // cerr << "**** start busy" << endl;
270 // stub for providing "busy" cue (e.g. busy spinner, or HUD)
271}
272
273+(void) stopWebViewBusy
274{
275 // cerr << "**** end busy" << endl;
276 // stub for providing "busy" cue (e.g. busy spinner, or HUD)
277}
278
279+(void) loadURL:(NSURL*)url
280{
281 [_webView loadURL:url];
282}
283
284+(void) showWebView:(int)eventNum fromView:(UIView*)from withURL:(NSURL*)url
285{
286 [_webView loadURL:url];
287 [_webView setParent:from];
288 [_transView transition:eventNum fromView:from toView:_webView];
289}
290
291
292
293-(void) alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
294{
295 [sheet dismissAnimated:YES];
296 [sheet release];
297}
298
299
300-(void) dealloc
301{
302 [_browseCascadeView release];
303 [_queryCascadeView release];
304 [_prefCascadeView release];
305
306 [_window release];
307 [_butbar release];
308
309 [super dealloc];
310}
311
312
313@end
314
315
316
Note: See TracBrowser for help on using the repository browser.