OSDN Git Service

MacGui: Add "Auto" to audio sample rate selections
[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 initWithWindowNibName:@"OutputPanel"]) )
32     {
33         /* NSWindowController likes to lazily load its window nib. Since this
34          * controller tries to touch the outlets before accessing the window, we
35          * need to force it to load immadiately by invoking its accessor.
36          *
37          * If/when we switch to using bindings, this can probably go away.
38          */
39         [self window];
40
41         /* We initialize the outputTextStorage object for the activity window */
42         outputTextStorage = [[NSTextStorage alloc] init];
43
44         /* We declare the default NSFileManager into fileManager */
45         NSFileManager * fileManager = [NSFileManager defaultManager];
46         /* Establish the log file location to write to */
47         /* We are initially using a .txt file as opposed to a .log file since it will open by
48          * default with the users text editor instead of the .log default Console.app, should
49          * create less confusion for less experienced users when we ask them to paste the log for support
50          */
51         outputLogFile = @"~/Library/Application Support/HandBrake/HandBrake-activitylog.txt";
52         outputLogFile = [[outputLogFile stringByExpandingTildeInPath]retain];
53
54         /* We check for an existing output log file here */
55         if( [fileManager fileExistsAtPath:outputLogFile] == 0 )
56         {
57             /* if not, then we create a new blank one */
58             [fileManager createFileAtPath:outputLogFile contents:nil attributes:nil];
59         }
60         /* We overwrite the existing output log with the date for starters the output log to start fresh with the new session */
61         /* Use the current date and time for the new output log header */
62         NSString *startOutputLogString = [NSString stringWithFormat: @"HandBrake Activity Log for Session (Cleared): %@\n\n", [[NSDate  date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil]];
63
64         [startOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
65
66         [[HBOutputRedirect stderrRedirect] addListener:self];
67         [[HBOutputRedirect stdoutRedirect] addListener:self];
68
69         [self setWindowFrameAutosaveName:@"OutputPanelFrame"];
70         [[textView layoutManager] replaceTextStorage:outputTextStorage];
71         [[textView enclosingScrollView] setLineScroll:10];
72         [[textView enclosingScrollView] setPageScroll:20];
73     }
74     return self;
75 }
76
77 /**
78  * Stops redirection of stderr and releases resources.
79  */
80 - (void)dealloc
81 {
82     [[HBOutputRedirect stderrRedirect] removeListener:self];
83     [[HBOutputRedirect stdoutRedirect] removeListener:self];
84     [outputTextStorage release];
85     [super dealloc];
86 }
87
88 /**
89  * Loads output panel from OutputPanel.nib and shows it.
90  */
91 - (IBAction)showOutputPanel:(id)sender
92 {
93     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
94     [self showWindow:sender];
95
96     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
97 }
98
99 /**
100  * Displays text received from HBOutputRedirect in the text view.
101  */
102 - (void)stderrRedirect:(NSString *)text
103 {
104         
105     NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
106         /* Actually write the libhb output to the text view (outputTextStorage) */
107     [outputTextStorage appendAttributedString:attributedString];
108     [attributedString release];
109     
110         /* remove text from outputTextStorage as defined by TextStorageUpperSizeLimit and TextStorageLowerSizeLimit */
111     if ([outputTextStorage length] > TextStorageUpperSizeLimit)
112                 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
113     
114     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
115     
116     /* We use a c function to write to the log file without reading it into memory 
117         * as it should be faster and easier on memory than using cocoa's writeToFile
118         * thanks ritsuka !!*/
119     FILE *f = fopen([outputLogFile UTF8String], "a");
120     fprintf(f, "%s", [text UTF8String]);
121     fclose(f);
122     
123     
124     /* Below uses Objective-C to write to the file, though it is slow and uses
125         * more memory than the c function above. For now, leaving this in here
126         * just in case and commented out.
127     */
128     /* Put the new incoming string from libhb into an nsstring for appending to our log file */
129     //NSString *newOutputString = [[NSString alloc] initWithString:text];
130     /*get the current log file and put it into an NSString */
131     /* HACK ALERT: must be a way to do it without reading the whole log into memory 
132         Performance note: could batch write to the log, but want to get each line as it comes out of
133         libhb in case of a crash or freeze so we see exactly what the last thing was before crash*/
134     //NSString *currentOutputLogString = [[NSString alloc]initWithContentsOfFile:outputLogFile encoding:NSUTF8StringEncoding error:NULL];
135     
136     /* Append the new libhb output string to the existing log file string */
137     //currentOutputLogString = [currentOutputLogString stringByAppendingString:newOutputString];
138     /* Save the new modified log file string back to disk */
139     //[currentOutputLogString writeToFile:outputLogFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
140     /* Release the new libhb output string */
141     //[newOutputString release];
142 }
143 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
144
145 /**
146  * Clears the output window.
147  */
148 - (IBAction)clearOutput:(id)sender
149 {
150         [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
151     /* We want to rewrite the app version info to the top of the activity window so it is always present */
152     NSString *versionStringFull = [[NSString stringWithFormat: @"Handbrake Version: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleGetInfoString"]] stringByAppendingString: [NSString stringWithFormat: @" (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]];
153     time_t _now = time( NULL );
154     struct tm * now  = localtime( &_now );
155     fprintf(stderr, "[%02d:%02d:%02d] macgui: %s\n", now->tm_hour, now->tm_min, now->tm_sec, [versionStringFull UTF8String]);
156     
157 }
158
159 /**
160  * Copies all text in the output window to pasteboard.
161  */
162 - (IBAction)copyAllOutputToPasteboard:(id)sender
163 {
164         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
165         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
166         [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
167     
168 }
169
170 /**
171  * Opens the activity log txt file in users default editor.
172  */
173 - (IBAction)openActivityLogFile:(id)sender
174 {
175     /* Opens the activity window log file in the users default text editor */
176     NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"Finder\" to open (POSIX file \"", outputLogFile, @"\")"]];
177     [myScript executeAndReturnError: nil];
178     [myScript release];
179 }
180
181 - (IBAction)clearActivityLogFile:(id)sender
182 {
183     /* We overwrite the existing output log with the new date and time header */
184         /* Use the current date and time for the new output log header */
185         NSString *startOutputLogString = [NSString stringWithFormat: @"HandBrake Activity Log for Session Starting: %@\n\n", [[NSDate  date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil]];
186         [startOutputLogString writeToFile:outputLogFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
187         
188         /* We want to rewrite the app version info to the top of the activity window so it is always present */
189         NSString *versionStringFull = [[NSString stringWithFormat: @"macgui: Handbrake Version: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleGetInfoString"]] stringByAppendingString: [NSString stringWithFormat: @" (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]];
190         [versionStringFull writeToFile:outputLogFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
191         
192 }
193
194 - (void)windowWillClose:(NSNotification *)aNotification
195 {
196     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];
197 }
198
199
200 @end