1 /* $Id: ScanController.mm,v 1.10 2005/04/27 21:05:24 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
6 /* These are now called in DriveDetector.h
8 #include <IOKit/IOKitLib.h>
9 #include <IOKit/IOBSD.h>
10 #include <IOKit/storage/IOMedia.h>
11 #include <IOKit/storage/IODVDMedia.h>
14 #include "ScanController.h"
15 #include "DriveDetector.h"
17 #define _(a) NSLocalizedString(a,nil)
18 #define INSERT_STRING @"Insert a DVD"
20 @implementation ScanController
22 - (void) TranslateStrings
24 [fSelectString setStringValue: _( @"Select a DVD:" )];
25 [fDetectedCell setTitle: _( @"Detected volume" )];
26 [fFolderCell setTitle: _( @"DVD Folder / Image" )];
27 [fBrowseButton setTitle: _( @"Browse" )];
28 [fCancelButton setTitle: _( @"Cancel" )];
29 [fOpenButton setTitle: _( @"Open" )];
32 - (void) SetHandle: (hb_handle_t *) handle
36 [self TranslateStrings];
38 [fStatusField setStringValue: @""];
45 fDriveDetector = [[DriveDetector alloc] initWithCallback: self
46 selector: @selector( openUpdateDrives: )];
49 // Here down continue with existing HB
50 [NSApp beginSheet: fPanel modalForWindow: fWindow
51 modalDelegate: nil didEndSelector: nil contextInfo: nil];
52 [NSApp runModalForWindow: fPanel];
53 [NSApp endSheet: fPanel];
54 [fPanel orderOut: self];
58 - (void) openUpdateDrives: (NSDictionary *) drives
64 fDrives = [[NSDictionary alloc] initWithDictionary: drives];
67 NSEnumerator * enumerator = [fDrives keyEnumerator];
68 [fDetectedPopUp removeAllItems];
69 while( ( device = [enumerator nextObject] ) )
71 [fDetectedPopUp addItemWithTitle: device];
76 if( ![fDetectedPopUp numberOfItems] )
78 [fDetectedPopUp addItemWithTitle: INSERT_STRING];
79 [fDetectedPopUp setEnabled: 0];
80 [fFolderField setEnabled: 1];
81 [fBrowseButton setEnabled: 1];
82 [fOpenButton setEnabled: 0];
83 [fBrowseButton setEnabled: 1];
84 [fMatrix selectCell: fFolderCell];
90 [fDetectedPopUp setEnabled: 1];
91 [fFolderField setEnabled: 0];
92 [fBrowseButton setEnabled: 0];
93 [fOpenButton setEnabled: 1];
94 [fBrowseButton setEnabled: 0];
97 [fDetectedPopUp selectItemAtIndex: 0];
101 - (void) EnableUI: (bool) b
103 [fMatrix setEnabled: b];
104 [fDetectedCell setEnabled: b];
105 [fDetectedPopUp setEnabled: b];
106 [fFolderCell setEnabled: b];
107 [fFolderField setEnabled: b];
108 [fBrowseButton setEnabled: b];
109 [fOpenButton setEnabled: b];
113 [self MatrixChanged: nil];
117 - (void) UpdateUI: (hb_state_t *) s
121 #define p s->param.scanning
122 case HB_STATE_SCANNING:
123 [fStatusField setStringValue: [NSString stringWithFormat:
124 _( @"Scanning title %d of %d..." ),
125 p.title_cur, p.title_count]];
126 [fIndicator setDoubleValue: 100.0 * ( p.title_cur - 1 ) /
131 case HB_STATE_SCANDONE:
132 [self EnableUI: YES];
133 [fIndicator setDoubleValue: 0.0];
135 if( hb_list_count( hb_get_titles( fHandle ) ) )
138 [fStatusField setStringValue: @""];
143 [fStatusField setStringValue:
144 _( @"No valid title found." )];
150 - (IBAction) MatrixChanged: (id) sender
152 /* Do we have detected drives */
154 if( [fDetectedPopUp numberOfItems] > 0 )
156 [fDetectedCell setEnabled: YES];
160 [fMatrix selectCell: fFolderCell];
161 [fDetectedCell setEnabled: NO];
162 [fMatrix selectCellAtRow:1 column:0];
165 /* Enable controls related to the current choice */
167 foo = ( [fMatrix selectedRow] == 0 );
168 [fDetectedPopUp setEnabled: foo];
169 [fFolderField setEnabled: !foo];
170 [fBrowseButton setEnabled: !foo];
175 Remove the current sheet (the scan panel) and show an OpenPanel.
176 Because we can't open the new sheet before the other one is
177 completely gone, we use performSelectorOnMainThread so it will be
178 done right afterwards */
179 - (IBAction) Browse: (id) sender
182 [self performSelectorOnMainThread: @selector( Browse2: )
183 withObject: nil waitUntilDone: NO];
185 - (void) Browse2: (id) sender
189 panel = [NSOpenPanel openPanel];
190 [panel setAllowsMultipleSelection: NO];
191 [panel setCanChooseFiles: YES];
192 [panel setCanChooseDirectories: YES ];
193 NSString * sourceDirectory;
194 if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"])
196 sourceDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"];
200 sourceDirectory = @"~/Desktop";
201 sourceDirectory = [sourceDirectory stringByExpandingTildeInPath];
203 [panel beginSheetForDirectory: sourceDirectory file: nil types: nil
204 modalForWindow: [NSApp mainWindow] modalDelegate: self
205 didEndSelector: @selector( BrowseDone:returnCode:contextInfo: )
209 /* Get the folder and switch sheets again. Use the same trick as
211 - (void) BrowseDone: (NSOpenPanel *) sheet
212 returnCode: (int) returnCode contextInfo: (void *) contextInfo
214 if( returnCode == NSOKButton )
216 [fFolderField setStringValue:
217 [[sheet filenames] objectAtIndex: 0]];
221 [self performSelectorOnMainThread: @selector( BrowseDone2: )
222 withObject: nil waitUntilDone: NO];
224 - (void) BrowseDone2: (id) sender
226 [NSApp beginSheet: fPanel modalForWindow: fWindow
227 modalDelegate: nil didEndSelector: nil contextInfo: nil];
228 [NSApp runModalForWindow: fWindow];
229 [NSApp endSheet: fPanel];
230 [fPanel orderOut: self];
233 - (IBAction) Open: (id) sender
238 [fStatusField setStringValue: _( @"Opening..." )];
241 [fDriveDetector stop];
243 if( [fMatrix selectedRow] )
245 /* we set the last source directory in the prefs here */
246 NSString *sourceDirectory = [[fFolderField stringValue] stringByDeletingLastPathComponent];
247 [[NSUserDefaults standardUserDefaults] setObject:sourceDirectory forKey:@"LastSourceDirectory"];
248 hb_scan( fHandle, [[fFolderField stringValue] UTF8String], 0 );
252 hb_scan( fHandle, [[fDrives objectForKey: [fDetectedPopUp
253 titleOfSelectedItem]] UTF8String], 0 );
260 - (IBAction) Cancel: (id) sender