-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBuildConsoleOutputViewController.m
325 lines (241 loc) · 10.3 KB
/
BuildConsoleOutputViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2012 LMIT Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "BuildConsoleOutputViewController.h"
@implementation BuildConsoleOutputViewController
@synthesize xTextSize,xConsoleAnnotator,htmlConsoleOutput,scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andBuild:(HudsonBuild*)_build
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
build = _build;
[self setXTextSize:@"0"];
showReloadOnActionSheet = NO;
showResumeOnActionSheet = NO;
showStopOnActionSheet = YES;
checkingSize = YES;
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)__scrollView{
return scrollView;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setTitle:NSLocalizedString(@"Console Output",nil)];
[self setMoreButton];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"build_output" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
[htmlConsoleOutput setDelegate:self];
[htmlConsoleOutput loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self fireTimer];
}
#pragma data receive handling
-(void) receiveData:(NSData*)data andHeaders:(NSDictionary *)headers{
[self setXConsoleAnnotator:[headers valueForKey:@"X-Consoleannotator"]];
[self setXTextSize:[headers valueForKey:@"X-Text-Size"]];
hasMoreData = [[headers valueForKey:@"X-More-Data"] isEqualToString:@"true"];
if(checkingSize){
checkingSize = NO;
int maxConsoleSize = [[Configuration getInstance] maxConsoleOutputSize];
int xTextSizeAsInt = [[self xTextSize] intValue];
if(xTextSizeAsInt > maxConsoleSize){
[self setXTextSize:[NSString stringWithFormat:@"%d",(xTextSizeAsInt - maxConsoleSize)]];
}else{
[self setXTextSize:@"0"];
}
[self fireTimer];
return;
}
NSInputStream* inStream = [NSInputStream inputStreamWithData:data];
[inStream open];
NSString* line = nil;
while((line = [CloudBeesWebUtility readLineFromStream:inStream])!=nil){
NSString* javascript = [NSString stringWithFormat:@"fetchNext('%@', 1);",line];
[htmlConsoleOutput stringByEvaluatingJavaScriptFromString:javascript];
}
[inStream close];
if(httpGetter!=nil){
[httpGetter release];
httpGetter = nil;
}
if(hasMoreData){
//[self fireTimer];
}else{
[self stopTimer];
NSString* javascript = @"fetchNext('', 0);";
[htmlConsoleOutput stringByEvaluatingJavaScriptFromString:javascript];
[self setReloadButton];
}
}
-(IBAction)moreButtonClick:(id)sender{
UIActionSheet* actionSheet = nil;
if(showReloadOnActionSheet){
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil destructiveButtonTitle:NSLocalizedString(@"Cancel",nil)
otherButtonTitles:NSLocalizedString(@"Reload",nil),NSLocalizedString(@"Email log",nil), nil];
}else if(showStopOnActionSheet){
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil destructiveButtonTitle:NSLocalizedString(@"Cancel",nil)
otherButtonTitles:NSLocalizedString(@"Stop",nil),NSLocalizedString(@"Email log",nil), nil];
}else if(showResumeOnActionSheet){
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil destructiveButtonTitle:NSLocalizedString(@"Cancel",nil)
otherButtonTitles:NSLocalizedString(@"Resume",nil),NSLocalizedString(@"Email log",nil), nil];
}
[actionSheet showFromBarButtonItem:[self.navigationItem rightBarButtonItem] animated:YES];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
break;
case 1:
if(showReloadOnActionSheet){
[self reloadButtonClick:nil];
}else if(showStopOnActionSheet){
[self stopButtonClick:nil];
}else if(showResumeOnActionSheet){
[self resumeButtonClick:nil];
}
break;
case 2:
[self emailLog];
break;
default:
break;
}
}
-(IBAction)reloadButtonClick:(id)sender{
[self setXTextSize:@"0"];
checkingSize = YES;
[self setStopButton];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"build_output" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
[htmlConsoleOutput loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
-(IBAction)resumeButtonClick:(id)sender{
NSString* javascript = @"showSpinner();";
[htmlConsoleOutput stringByEvaluatingJavaScriptFromString:javascript];
[self setStopButton];
[self fireTimer];
}
-(IBAction)stopButtonClick:(id)sender{
NSString* javascript = @"hideSpinner();";
[htmlConsoleOutput stringByEvaluatingJavaScriptFromString:javascript];
[self setResumeButton];
[self stopTimer];
if(httpGetter!=nil){
[httpGetter stop];
[httpGetter release];
httpGetter = nil;
}
}
-(void)setReloadButton{
showReloadOnActionSheet = YES;
showStopOnActionSheet = NO;
showResumeOnActionSheet = NO;
}
-(void)setResumeButton{
showReloadOnActionSheet = NO;
showStopOnActionSheet = NO;
showResumeOnActionSheet = YES;
}
-(void)setStopButton{
showReloadOnActionSheet = NO;
showStopOnActionSheet = YES;
showResumeOnActionSheet = NO;;
}
-(void)setMoreButton{
UIBarButtonItem* moreButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear.png"] style:UIBarButtonItemStyleBordered
target:self action:@selector(moreButtonClick:)];
[self.navigationItem setRightBarButtonItem:moreButton];
[moreButton release];
}
-(void)fireTimer{
timeoutTimer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(timeoutOccurred:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timeoutTimer forMode:NSDefaultRunLoopMode];
}
-(void)stopTimer{
if(timeoutTimer!=nil){
[timeoutTimer invalidate];
timeoutTimer = nil;
}
}
-(void)timeoutOccurred:(NSTimer*)theTimer{
httpGetter = [[SimpleHTTPGetter alloc] init];
if(checkingSize){
[self stopTimer];
[httpGetter setCheckOnlyStatusCode:YES];
}
NSMutableString* queryString = [[NSMutableString alloc] init];
NSMutableDictionary* headers = [[NSMutableDictionary alloc] init];
if(xConsoleAnnotator!=nil){
[headers setValue:xConsoleAnnotator forKey:@"X-ConsoleAnnotator"];
}
[queryString appendFormat:@"?start=%@",[self xTextSize],nil];
NSString* url = [NSString stringWithFormat:@"%@/logText/progressiveHtml%@",[build url],queryString,nil];
[httpGetter httpGET:url withUsername:[[Configuration getInstance] username] andPassword:[[Configuration getInstance] password] andDataReceiver:self andHeaders:headers];
}
-(void)emailLog {
NSString* javascript = @"document.getElementById('out').innerHTML;";
NSMutableString* htmlPageBuilder = [[NSMutableString alloc] init];
[htmlPageBuilder appendString:[NSString stringWithFormat:@"<!-- Sent with %@ (c)2010-2011 LMIT Software Ltd -->",[[Configuration getInstance] productName]]];
[htmlPageBuilder appendString:@"<html><head><title></title></head><body>"];
[htmlPageBuilder appendString:[htmlConsoleOutput stringByEvaluatingJavaScriptFromString:javascript]];
[htmlPageBuilder appendString:@"</body></html>"];
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
[controller setMailComposeDelegate:self];
[controller setSubject:[build description]];
[controller setToRecipients:nil];
[controller addAttachmentData:[htmlPageBuilder dataUsingEncoding:NSUTF8StringEncoding] mimeType:@"text/html" fileName:@"console.html"];
[controller setMessageBody:@"" isHTML:NO];
[self.navigationController presentModalViewController:controller animated:YES];
[controller release];
[htmlPageBuilder release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[self dismissModalViewControllerAnimated:YES];
}
-(void) viewWillDisappear:(BOOL)animated {
[self stopButtonClick:nil];
[super viewWillDisappear:animated];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait)||(;
}
@end