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];
53 /* We overwrite the existing output log with the date for starters the output log to start fresh with the new session */
54 /* Use the current date and time for the new output log header */
55 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];
59 [[HBOutputRedirect stderrRedirect] addListener:self];
60 [[HBOutputRedirect stdoutRedirect] addListener:self];
68 * Stops redirection of stderr and releases resources.
72 [[HBOutputRedirect stderrRedirect] removeListener:self];
73 [[HBOutputRedirect stdoutRedirect] removeListener:self];
74 [outputTextStorage release];
75 [outputPanel release];
80 * Loads output panel from OutputPanel.nib and shows it.
82 - (IBAction)showOutputPanel:(id)sender
86 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
87 NSAssert(loadSucceeded, @"Could not open nib file");
89 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
90 [[textView layoutManager] replaceTextStorage:outputTextStorage];
91 [[textView enclosingScrollView] setLineScroll:10];
92 [[textView enclosingScrollView] setPageScroll:20];
95 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
96 [outputPanel orderFront:nil];
98 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
102 * Displays text received from HBOutputRedirect in the text view.
104 - (void)stderrRedirect:(NSString *)text
107 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
108 /* Actually write the libhb output to the text view (outputTextStorage) */
109 [outputTextStorage appendAttributedString:attributedString];
110 [attributedString release];
112 /* remove text from outputTextStorage as defined by TextStorageUpperSizeLimit and TextStorageLowerSizeLimit */
113 if ([outputTextStorage length] > TextStorageUpperSizeLimit)
114 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
116 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
118 /* We use a c function to write to the log file without reading it into memory
119 * as it should be faster and easier on memory than using cocoa's writeToFile
120 * thanks ritsuka !!*/
121 FILE *f = fopen([outputLogFile UTF8String], "a");
122 fprintf(f, "%s", [text UTF8String]);
126 /* Below uses Objective-C to write to the file, though it is slow and uses
127 * more memory than the c function above. For now, leaving this in here
128 * just in case and commented out.
130 /* Put the new incoming string from libhb into an nsstring for appending to our log file */
131 //NSString *newOutputString = [[NSString alloc] initWithString:text];
132 /*get the current log file and put it into an NSString */
133 /* HACK ALERT: must be a way to do it without reading the whole log into memory
134 Performance note: could batch write to the log, but want to get each line as it comes out of
135 libhb in case of a crash or freeze so we see exactly what the last thing was before crash*/
136 //NSString *currentOutputLogString = [[NSString alloc]initWithContentsOfFile:outputLogFile encoding:NSUTF8StringEncoding error:NULL];
138 /* Append the new libhb output string to the existing log file string */
139 //currentOutputLogString = [currentOutputLogString stringByAppendingString:newOutputString];
140 /* Save the new modified log file string back to disk */
141 //[currentOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
142 /* Release the new libhb output string */
143 //[newOutputString release];
145 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
148 * Clears the output window.
150 - (IBAction)clearOutput:(id)sender
152 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
156 * Copies all text in the output window to pasteboard.
158 - (IBAction)copyAllOutputToPasteboard:(id)sender
160 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
161 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
162 [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
166 * Opens the activity log txt file in users default editor.
168 - (IBAction)openActivityLogFile:(id)sender
170 /* Opens the activity window log file in the users default text editor */
171 NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"Finder\" to open (POSIX file \"", outputLogFile, @"\")"]];
172 [myScript executeAndReturnError: nil];
176 - (IBAction)clearActivityLogFile:(id)sender
178 /* We overwrite the existing output log with the new date and time header */
179 /* Use the current date and time for the new output log header */
180 NSString *startOutputLogString = [NSString stringWithFormat: @"HandBrake Activity Log for Session Starting: %@\n\n", [[NSDate date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil]];
181 [startOutputLogString writeToFile:outputLogFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
184 - (void)windowWillClose:(NSNotification *)aNotification
186 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];