5 * Implementation of class HBOutputPanelController.
8 #import "HBOutputPanelController.h"
9 #import "HBOutputRedirect.h"
11 /// Maximum amount of characters that can be shown in the view.
12 // Original value used by cleaner
13 //#define TextStorageUpperSizeLimit 20000
14 // lets use this higher value for now for better gui debugging
15 #define TextStorageUpperSizeLimit 40000
17 /// When old output is removed, this is the amount of characters that will be
18 /// left in outputTextStorage.
19 // Original value used by cleaner
20 //#define TextStorageLowerSizeLimit 15000
21 // lets use this higher value for now for better gui debugging
22 #define TextStorageLowerSizeLimit 35000
24 @implementation HBOutputPanelController
27 * Initializes the object, creates outputTextStorage and starts redirection of stderr.
31 if (self = [super init])
33 /* We initialize the outputTextStorage object for the activity window */
34 outputTextStorage = [[NSTextStorage alloc] init];
36 /* We declare the default NSFileManager into fileManager */
37 NSFileManager * fileManager = [NSFileManager defaultManager];
38 /* Establish the log file location to write to */
39 /* We are initially using a .txt file as opposed to a .log file since it will open by
40 * default with the users text editor instead of the .log default Console.app, should
41 * create less confusion for less experienced users when we ask them to paste the log for support
43 outputLogFile = @"~/Library/Application Support/HandBrake/HandBrake-activitylog.txt";
44 outputLogFile = [[outputLogFile stringByExpandingTildeInPath]retain];
46 /* We check for an existing output log file here */
47 if ([fileManager fileExistsAtPath:outputLogFile] == 0)
49 /* if not, then we create a new blank one */
50 [fileManager createFileAtPath:outputLogFile contents:nil attributes:nil];
52 /* We overwrite the existing output log with the date for starters the output log to start fresh with the new session */
53 /* Use the current date and time for the new output log header */
54 NSString *startOutputLogString = [NSString stringWithFormat: @"HandBrake Activity Log for Session (Cleared): %@\n\n", [[NSDate date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil]];
56 [startOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
58 [[HBOutputRedirect stderrRedirect] addListener:self];
59 [[HBOutputRedirect stdoutRedirect] addListener:self];
67 * Stops redirection of stderr and releases resources.
71 [[HBOutputRedirect stderrRedirect] removeListener:self];
72 [[HBOutputRedirect stdoutRedirect] removeListener:self];
73 [outputTextStorage release];
74 [outputPanel release];
79 * Loads output panel from OutputPanel.nib and shows it.
81 - (IBAction)showOutputPanel:(id)sender
85 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
86 NSAssert(loadSucceeded, @"Could not open nib file");
88 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
89 [[textView layoutManager] replaceTextStorage:outputTextStorage];
90 [[textView enclosingScrollView] setLineScroll:10];
91 [[textView enclosingScrollView] setPageScroll:20];
94 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
95 [outputPanel orderFront:nil];
97 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
101 * Displays text received from HBOutputRedirect in the text view.
103 - (void)stderrRedirect:(NSString *)text
106 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
107 /* Actually write the libhb output to the text view (outputTextStorage) */
108 [outputTextStorage appendAttributedString:attributedString];
109 [attributedString release];
111 /* remove text from outputTextStorage as defined by TextStorageUpperSizeLimit and TextStorageLowerSizeLimit */
112 if ([outputTextStorage length] > TextStorageUpperSizeLimit)
113 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
115 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
117 /* We use a c function to write to the log file without reading it into memory
118 * as it should be faster and easier on memory than using cocoa's writeToFile
119 * thanks ritsuka !!*/
120 FILE *f = fopen([outputLogFile UTF8String], "a");
121 fprintf(f, "%s", [text UTF8String]);
125 /* Below uses Objective-C to write to the file, though it is slow and uses
126 * more memory than the c function above. For now, leaving this in here
127 * just in case and commented out.
129 /* Put the new incoming string from libhb into an nsstring for appending to our log file */
130 //NSString *newOutputString = [[NSString alloc] initWithString:text];
131 /*get the current log file and put it into an NSString */
132 /* HACK ALERT: must be a way to do it without reading the whole log into memory
133 Performance note: could batch write to the log, but want to get each line as it comes out of
134 libhb in case of a crash or freeze so we see exactly what the last thing was before crash*/
135 //NSString *currentOutputLogString = [[NSString alloc]initWithContentsOfFile:outputLogFile encoding:NSUTF8StringEncoding error:NULL];
137 /* Append the new libhb output string to the existing log file string */
138 //currentOutputLogString = [currentOutputLogString stringByAppendingString:newOutputString];
139 /* Save the new modified log file string back to disk */
140 //[currentOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
141 /* Release the new libhb output string */
142 //[newOutputString release];
144 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
147 * Clears the output window.
149 - (IBAction)clearOutput:(id)sender
151 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
152 /* We want to rewrite the app version info to the top of the activity window so it is always present */
153 NSString *versionStringFull = [[NSString stringWithFormat: @"Handbrake Version: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleGetInfoString"]] stringByAppendingString: [NSString stringWithFormat: @" (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]];
154 time_t _now = time( NULL );
155 struct tm * now = localtime( &_now );
156 fprintf(stderr, "[%02d:%02d:%02d] macgui: %s\n", now->tm_hour, now->tm_min, now->tm_sec, [versionStringFull UTF8String]);
161 * Copies all text in the output window to pasteboard.
163 - (IBAction)copyAllOutputToPasteboard:(id)sender
165 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
166 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
167 [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
172 * Opens the activity log txt file in users default editor.
174 - (IBAction)openActivityLogFile:(id)sender
176 /* Opens the activity window log file in the users default text editor */
177 NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"Finder\" to open (POSIX file \"", outputLogFile, @"\")"]];
178 [myScript executeAndReturnError: nil];
182 - (IBAction)clearActivityLogFile:(id)sender
184 /* We overwrite the existing output log with the new date and time header */
185 /* Use the current date and time for the new output log header */
186 NSString *startOutputLogString = [NSString stringWithFormat: @"HandBrake Activity Log for Session Starting: %@\n\n", [[NSDate date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil]];
187 [startOutputLogString writeToFile:outputLogFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
189 /* We want to rewrite the app version info to the top of the activity window so it is always present */
190 NSString *versionStringFull = [[NSString stringWithFormat: @"macgui: Handbrake Version: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleGetInfoString"]] stringByAppendingString: [NSString stringWithFormat: @" (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]];
191 [versionStringFull writeToFile:outputLogFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
195 - (void)windowWillClose:(NSNotification *)aNotification
197 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];