OSDN Git Service

MacGui: Sanity Check AC3 passthrough against any input audio that is not AC3.
[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         /* 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]];
55         
56         [startOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
57         
58                 [[HBOutputRedirect stderrRedirect] addListener:self];
59                 [[HBOutputRedirect stdoutRedirect] addListener:self];
60         
61         
62         }
63         return self;
64 }
65
66 /**
67  * Stops redirection of stderr and releases resources.
68  */
69 - (void)dealloc
70 {
71         [[HBOutputRedirect stderrRedirect] removeListener:self];
72         [[HBOutputRedirect stdoutRedirect] removeListener:self];        
73         [outputTextStorage release];
74         [outputPanel release];
75         [super dealloc];
76 }
77
78 /**
79  * Loads output panel from OutputPanel.nib and shows it.
80  */
81 - (IBAction)showOutputPanel:(id)sender
82 {
83         if (!outputPanel)
84         {
85                 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
86                 NSAssert(loadSucceeded, @"Could not open nib file");
87                 
88                 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
89                 [[textView layoutManager] replaceTextStorage:outputTextStorage];
90                 [[textView enclosingScrollView] setLineScroll:10];
91                 [[textView enclosingScrollView] setPageScroll:20];
92         }
93                 
94     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
95         [outputPanel orderFront:nil];
96
97     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
98 }
99
100 /**
101  * Displays text received from HBOutputRedirect in the text view.
102  */
103 - (void)stderrRedirect:(NSString *)text
104 {
105         
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];
110     
111         /* remove text from outputTextStorage as defined by TextStorageUpperSizeLimit and TextStorageLowerSizeLimit */
112     if ([outputTextStorage length] > TextStorageUpperSizeLimit)
113                 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
114     
115     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
116     
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]);
122     fclose(f);
123     
124     
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.
128     */
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];
136     
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];
143 }
144 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
145
146 /**
147  * Clears the output window.
148  */
149 - (IBAction)clearOutput:(id)sender
150 {
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]);
157     
158 }
159
160 /**
161  * Copies all text in the output window to pasteboard.
162  */
163 - (IBAction)copyAllOutputToPasteboard:(id)sender
164 {
165         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
166         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
167         [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
168     
169 }
170
171 /**
172  * Opens the activity log txt file in users default editor.
173  */
174 - (IBAction)openActivityLogFile:(id)sender
175 {
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];
179     [myScript release];
180 }
181
182 - (IBAction)clearActivityLogFile:(id)sender
183 {
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];
188         
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];
192         
193 }
194
195 - (void)windowWillClose:(NSNotification *)aNotification
196 {
197     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];
198 }
199
200
201 @end