root/Tuna/trunk/TNProfilerController.m

Revision 5798, 7.3 kB (checked in by morris, 5 days ago)

Update for WiredAdditions?

  • Property svn:keywords set to Id Rev
Line 
1 /* $Id$ */
2
3 /*
4  *  Copyright (c) 2005-2008 Axel Andersson
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "NSDocumentController-TNAdditions.h"
30 #import "TNDocument.h"
31 #import "TNProfilerController.h"
32 #import "TNSettings.h"
33
34 @interface TNProfilerController(Private)
35
36 - (void)_updateButtons;
37 - (void)_success;
38 - (void)_error;
39
40 @end
41
42
43 @implementation TNProfilerController(Private)
44
45 - (void)_updateButtons {
46         [_scriptComboBox setEnabled:!_started];
47         [_setButton setEnabled:!_started];
48         [_argumentsComboBox setEnabled:!_started];
49        
50         if(_started) {
51                 [_launchButton setTitle:NSLS(@"Stop", @"Launch/Stop button title")];
52                 [_progressIndicator startAnimation:self];
53         } else {
54                 [_launchButton setTitle:NSLS(@"Launch", @"Launch/Stop button title")];
55                 [_progressIndicator stopAnimation:self];
56         }
57 }
58
59
60
61 - (void)_success {
62         TNDocument                      *document;
63         NSMutableArray          *scripts, *arguments;
64         NSString                        *path;
65        
66         path = [_path stringByAppendingPathComponent:@"tmon.out"];
67         document = [[NSDocumentController sharedDocumentController] makeUntitledDocumentOfType:@"Tuna Session" fromFile:path];
68        
69         if(document) {
70                 [document updateChangeCount:NSChangeDone];
71                 [document showWindows];
72                 [document retain];
73                
74                 scripts = [[TNSettings objectForKey:TNScriptsHistory] mutableCopy];
75                
76                 if(![scripts containsObject:[_scriptComboBox stringValue]]) {
77                         [scripts insertObject:[_scriptComboBox stringValue] atIndex:0];
78                        
79                         if([scripts count] > 5)
80                                 [scripts removeLastObject];
81                        
82                         [TNSettings setObject:scripts forKey:TNScriptsHistory];
83                 }
84                
85                 [scripts release];
86                
87                 arguments = [[TNSettings objectForKey:TNArgumentsHistory] mutableCopy];
88
89                 if(![arguments containsObject:[_argumentsComboBox stringValue]]) {
90                         [arguments insertObject:[_argumentsComboBox stringValue] atIndex:0];
91                        
92                         if([arguments count] > 5)
93                                 [arguments removeLastObject];
94                        
95                         [TNSettings setObject:arguments forKey:TNArgumentsHistory];
96                 }
97                
98                 [arguments release];
99                
100                 [self close];
101         } else {
102                 NSBeginAlertSheet(NSLS(@"Couldn't Open Session", @"Error dialog title"),
103                                                   @"OK",
104                                                   NULL,
105                                                   NULL,
106                                                   [self window],
107                                                   NULL,
108                                                   NULL,
109                                                   NULL,
110                                                   NULL,
111                                                   NSLS(@"Tuna could not open the session just written.", @"Error dialog description"));
112         }
113 }
114
115
116
117 - (void)_error {
118         NSFileHandle    *fileHandle;
119         NSData                  *data;
120         NSString                *string;
121        
122         fileHandle      = [_pipe fileHandleForReading];
123         data            = [fileHandle availableData];
124         string          = [NSString stringWithData:data encoding:NSISOLatin1StringEncoding];
125        
126         NSBeginAlertSheet(NSLS(@"Couldn't Profile Script", @"Error dialog title"),
127                                           @"OK",
128                                           NULL,
129                                           NULL,
130                                           [self window],
131                                           NULL,
132                                           NULL,
133                                           NULL,
134                                           NULL,
135                                           NSLS(@"Tuna could not profile the script, because of Perl errors:\n\n%@", @"Error dialog description (errors)"),
136                                           string);
137 }
138
139 @end
140
141
142
143 @implementation TNProfilerController
144
145 - (id)init {
146         self = [super initWithWindowNibName:@"Profiler"];
147        
148         [[NSNotificationCenter defaultCenter]
149                 addObserver:self
150                    selector:@selector(taskDidTerminate:)
151                            name:NSTaskDidTerminateNotification
152                          object:NULL];
153        
154         _path = [[NSFileManager temporaryPathWithPrefix:@"Tuna"] retain];
155         [[NSFileManager defaultManager] createDirectoryAtPath:_path];
156        
157         [self retain];
158        
159         return self;
160 }
161
162
163
164 - (void)dealloc {
165         [[NSNotificationCenter defaultCenter] removeObserver:self];
166        
167         [_task release];
168         [_pipe release];
169        
170         [[NSFileManager defaultManager] removeFileAtPath:_path handler:NULL];
171         [_path release];
172        
173         [super dealloc];
174 }
175
176
177
178 #pragma mark -
179
180 - (void)windowDidLoad {
181         [[self window] center];
182     [self setShouldCascadeWindows:YES];
183     [self setWindowFrameAutosaveName:@"Profiler"];
184        
185         [_scriptComboBox addItemsWithObjectValues:[TNSettings objectForKey:TNScriptsHistory]];
186         [_argumentsComboBox addItemsWithObjectValues:[TNSettings objectForKey:TNArgumentsHistory]];
187 }
188
189
190
191
192 - (void)windowWillClose:(NSNotification *)notification {
193         if(_task) {
194                 [_task terminate];
195                 [_task release];
196                 _task = NULL;
197         }
198
199         [self autorelease];
200 }
201
202
203
204 - (void)taskDidTerminate:(NSNotification *)notification {
205         if([notification object] == _task) {
206                 if([_task terminationStatus] == 0)
207                         [self _success];
208                 else if(_started)
209                         [self _error];
210                
211                 _started = NO;
212
213                 [_task release];
214                 _task = NULL;
215                
216                 [_pipe release];
217                 _pipe = NULL;
218                
219                 [self _updateButtons];
220         }
221 }
222
223
224
225 - (void)setPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(NSInteger)returnCode contextInfo:(void  *)contextInfo {
226         if(returnCode == NSOKButton)
227                 [_scriptComboBox setStringValue:[[openPanel filename] stringByAbbreviatingWithTildeInPath]];
228 }
229
230
231
232 #pragma mark -
233
234 - (IBAction)set:(id)sender {
235         NSOpenPanel             *openPanel;
236
237         openPanel = [NSOpenPanel openPanel];
238         [openPanel setCanChooseDirectories:NO];
239         [openPanel setCanChooseFiles:YES];
240         [openPanel setAllowsMultipleSelection:NO];
241         [openPanel beginSheetForDirectory:NULL
242                                                                  file:NULL
243                                                                 types:[NSArray arrayWithObjects:@"pl", @"PL", NULL]
244                                            modalForWindow:[self window]
245                                                 modalDelegate:self
246                                            didEndSelector:@selector(setPanelDidEnd:returnCode:contextInfo:)
247                                                   contextInfo:NULL];
248 }
249
250
251
252 - (IBAction)launch:(id)sender {
253         NSString                *script, *argument;
254         NSMutableArray  *arguments;
255         NSArray                 *components;
256         NSUInteger              i, count;
257        
258         if(_started) {
259                 [_task terminate];
260         } else {
261                 script = [[_scriptComboBox stringValue] stringByStandardizingPath];
262                
263                 if([script length] == 0) {
264                         NSBeep();
265                        
266                         return;
267                 }
268                
269                 arguments = [NSMutableArray arrayWithObjects:@"-d:DProf", script, NULL];
270                 components = [[_argumentsComboBox stringValue] componentsSeparatedByCharactersFromSet:
271                         [NSCharacterSet whitespaceCharacterSet]];
272                
273                 count = [components count];
274                
275                 for(i = 0; i < count; i++) {
276                         argument = [components objectAtIndex:i];
277                        
278                         if([argument hasPrefix:@"~"])
279                                 argument = [argument stringByExpandingTildeInPath];
280                        
281                         [arguments addObject:argument];
282                 }
283                
284                 _pipe = [[NSPipe alloc] init];
285                 _task = [[NSTask alloc] init];
286                 [_task setLaunchPath:@"/usr/bin/perl"];
287                 [_task setCurrentDirectoryPath:_path];
288                 [_task setStandardError:_pipe];
289                 [_task setArguments:arguments];
290                 [_task launch];
291         }
292
293         _started = !_started;
294        
295         [self _updateButtons];
296 }
297
298
299
300 - (IBAction)cancel:(id)sender {
301         [self close];
302 }
303
304 @end
Note: See TracBrowser for help on using the browser.