OSDN Git Service

Only futz with frame durations at render-time when using VFR.
[handbrake-jp/handbrake-jp-git.git] / macosx / HBOutputPanelController.m
1 /**
2  * @file
3  * @date 18.5.2007
4  *
5  * Implementation of class HBOutputPanelController.
6  */
7
8 #import "HBOutputPanelController.h"
9 #import "HBOutputRedirect.h"
10
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
16
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
23
24 @implementation HBOutputPanelController
25
26 /**
27  * Initializes the object, creates outputTextStorage and starts redirection of stderr.
28  */
29 - (id)init
30 {
31         if (self = [super init])
32         {
33                 /* We initialize the outputTextStorage object for the activity window */
34         outputTextStorage = [[NSTextStorage alloc] init];
35         
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
42             */
43         outputLogFile = @"~/Library/Application Support/HandBrake/HandBrake-activitylog.txt";
44         outputLogFile = [[outputLogFile stringByExpandingTildeInPath]retain];
45         
46         /* We check for an existing output log file here */
47         if ([fileManager fileExistsAtPath:outputLogFile] == 0) 
48         {
49             /* if not, then we create a new blank one */
50             [fileManager createFileAtPath:outputLogFile contents:nil attributes:nil];
51         }
52         
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];
57         
58         
59                 [[HBOutputRedirect stderrRedirect] addListener:self];
60                 [[HBOutputRedirect stdoutRedirect] addListener:self];
61         
62         
63         }
64         return self;
65 }
66
67 /**
68  * Stops redirection of stderr and releases resources.
69  */
70 - (void)dealloc
71 {
72         [[HBOutputRedirect stderrRedirect] removeListener:self];
73         [[HBOutputRedirect stdoutRedirect] removeListener:self];        
74         [outputTextStorage release];
75         [outputPanel release];
76         [super dealloc];
77 }
78
79 /**
80  * Loads output panel from OutputPanel.nib and shows it.
81  */
82 - (IBAction)showOutputPanel:(id)sender
83 {
84         if (!outputPanel)
85         {
86                 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
87                 NSAssert(loadSucceeded, @"Could not open nib file");
88                 
89                 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
90                 [[textView layoutManager] replaceTextStorage:outputTextStorage];
91                 [[textView enclosingScrollView] setLineScroll:10];
92                 [[textView enclosingScrollView] setPageScroll:20];
93         }
94                 
95     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
96         [outputPanel orderFront:nil];
97
98     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
99 }
100
101 /**
102  * Displays text received from HBOutputRedirect in the text view.
103  */
104 - (void)stderrRedirect:(NSString *)text
105 {
106         
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];
111     
112         /* remove text from outputTextStorage as defined by TextStorageUpperSizeLimit and TextStorageLowerSizeLimit */
113     if ([outputTextStorage length] > TextStorageUpperSizeLimit)
114                 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
115     
116     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
117     
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]);
123     fclose(f);
124     
125     
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.
129     */
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];
137     
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];
144 }
145 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
146
147 /**
148  * Clears the output window.
149  */
150 - (IBAction)clearOutput:(id)sender
151 {
152         [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
153 }
154
155 /**
156  * Copies all text in the output window to pasteboard.
157  */
158 - (IBAction)copyAllOutputToPasteboard:(id)sender
159 {
160         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
161         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
162         [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
163 }
164
165 /**
166  * Opens the activity log txt file in users default editor.
167  */
168 - (IBAction)openActivityLogFile:(id)sender
169 {
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];
173     [myScript release];
174 }
175
176 - (IBAction)clearActivityLogFile:(id)sender
177 {
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];
182 }
183
184 - (void)windowWillClose:(NSNotification *)aNotification
185 {
186     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];
187 }
188
189
190 @end