summaryrefslogtreecommitdiffstats
path: root/macosx/ScanController.mm
blob: ab5b465191059afae2311e26f6f845c0781249e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*  $Id: ScanController.mm,v 1.10 2005/04/27 21:05:24 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.m0k.org/>.
   It may be used under the terms of the GNU General Public License. */
/* These are now called in DriveDetector.h
#include <paths.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IODVDMedia.h>
*/

#include "ScanController.h"
#include "DriveDetector.h"

#define _(a) NSLocalizedString(a,nil)
#define INSERT_STRING @"Insert a DVD"

@implementation ScanController

- (void) TranslateStrings
{
    [fSelectString setStringValue: _( @"Select a DVD:" )];
    [fDetectedCell setTitle:       _( @"Detected volume" )];
    [fFolderCell   setTitle:       _( @"DVD Folder / Image" )];
    [fBrowseButton setTitle:       _( @"Browse" )];
    [fCancelButton setTitle:       _( @"Cancel" )];
    [fOpenButton   setTitle:       _( @"Open" )];
}

- (void) SetHandle: (hb_handle_t *) handle
{
    fHandle    = handle;

    [self TranslateStrings];

    [fStatusField setStringValue: @""];
}

- (void) Show
{
	if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DisableDvdAutoDetect"] == 0)
	{
	/* We manually show these, in case they were hidden during a previous scan
	with the Auto Detect turned off */
	[fMatrix setHidden: NO];
	[fDetectedPopUp setHidden: NO];
	[fFolderField setHidden: NO];
	[fOpenButton setHidden: NO];
	[fCancelButton setHidden: NO];
	[fBrowseButton setHidden: NO];
	
	[fSelectString setStringValue:@"Select a DVD Source:"];
		fDriveDetector = [[DriveDetector alloc] initWithCallback:self selector:@selector(openUpdateDrives:)];
		[fDriveDetector run];
	/* We show the scan choice sheet */
	[NSApp beginSheet:fPanel modalForWindow:fWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
	}
	else
	{
	[fSelectString setStringValue:@""];

	[fDetectedCell  setEnabled: 0];
	[fDetectedPopUp setEnabled: 0];
    [fFolderField   setEnabled: 1];
    [fBrowseButton  setEnabled: 1];
	[fOpenButton    setEnabled: 0];
	[fBrowseButton  setEnabled: 1];
	[fMatrix selectCell: fFolderCell];
	[fMatrix setHidden: YES];
	[fDetectedPopUp setHidden: YES];
	[fFolderField setHidden: YES];
	[fOpenButton setHidden: YES];
	[fCancelButton setHidden: YES];
	[fBrowseButton setHidden: YES];
	/* We go straight to the Browse Sheet */
	[self Browse2: NULL];
	}

}

- (void) openUpdateDrives: (NSDictionary *) drives
{
    if( fDrives )
    {
        [fDrives release];
    }
    fDrives = [[NSDictionary alloc] initWithDictionary: drives];

    NSString * device;
    NSEnumerator * enumerator = [fDrives keyEnumerator];
    [fDetectedPopUp removeAllItems];
    while( ( device = [enumerator nextObject] ) )
    {
        [fDetectedPopUp addItemWithTitle: device];
    }
    
	

    if( ![fDetectedPopUp numberOfItems] )
    {
	[fDetectedPopUp addItemWithTitle: INSERT_STRING];
	[fDetectedPopUp setEnabled: 0];
    [fFolderField   setEnabled: 1];
    [fBrowseButton  setEnabled: 1];
	[fOpenButton    setEnabled: 0];
	[fBrowseButton  setEnabled: 1];
	[fMatrix selectCell: fFolderCell];


    }
	else
	{
	[fDetectedPopUp setEnabled: 1];
    [fFolderField   setEnabled: 0];
    [fBrowseButton  setEnabled: 0];
	[fOpenButton    setEnabled: 1];
	[fBrowseButton  setEnabled: 0];
	}

	[fDetectedPopUp selectItemAtIndex: 0];

}

- (void) EnableUI: (bool) b
{
    [fMatrix        setEnabled: b];
    [fDetectedCell  setEnabled: b];
    [fDetectedPopUp setEnabled: b];
    [fFolderCell    setEnabled: b];
    [fFolderField   setEnabled: b];
    [fBrowseButton  setEnabled: b];
    [fOpenButton    setEnabled: b];

    if( b )
    {
        [self MatrixChanged: nil];
    }
}

- (void) UpdateUI: (hb_state_t *) s
{
    switch( s->state )
    {
#define p s->param.scanning
        case HB_STATE_SCANNING:
		[fSelectString setStringValue:@"HandBrake is Scanning Your Source..."];
            [fStatusField setStringValue: [NSString stringWithFormat:
                _( @"Scanning title %d of %d..." ),
                p.title_cur, p.title_count]];
            [fIndicator setDoubleValue: 100.0 * ( p.title_cur - 1 ) /
                p.title_count];
            break;
#undef p

        case HB_STATE_SCANDONE:
            [self       EnableUI: YES];
            [fIndicator setDoubleValue: 0.0];

            if (hb_list_count(hb_get_titles(fHandle)))
             {
             [fStatusField setStringValue:@""];
				[NSApp endSheet:fPanel];
				[fPanel orderOut:self];
				
            }
            else
            {
             [fStatusField setStringValue:_( @"No valid title found.")];
			 /* If DVD Auto Detect is disabled */
		     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DisableDvdAutoDetect"] == 1)
		     {
			 [NSApp endSheet:fPanel];
	         [fPanel orderOut:self];
			 }
            }
            break;
    }
}

- (IBAction) MatrixChanged: (id) sender
{
    /* Do we have detected drives  and is "Disable DVD Drive Auto Scan" off in prefs*/

    if( [fDetectedPopUp numberOfItems] > 0  && [[NSUserDefaults standardUserDefaults] boolForKey:@"DisableDvdAutoDetect"] == 0)
    {
        [fDetectedCell setEnabled: YES];
		//[fMatrix selectCellAtRow:0 column:0];
		
    }
    else
    {
       //[fMatrix selectCell: fFolderCell];
        [fDetectedCell setEnabled: NO];
		
    }

    /* Enable controls related to the current choice */
	/* If Detected Volume is selected */
    if ( [fMatrix selectedRow] == 0 )
	{
	[fDetectedPopUp setEnabled: YES];
    [fFolderField   setEnabled: NO];
    [fBrowseButton  setEnabled: NO];
	}
	else
	{
	[fDetectedPopUp setEnabled: NO];
    [fFolderField   setEnabled: YES];
    [fBrowseButton  setEnabled: YES];
	}


}



/* Browse:
   Remove the current sheet (the scan panel) and show an OpenPanel.
   Because we can't open the new sheet before the other one is
   completely gone, we use performSelectorOnMainThread so it will be
   done right afterwards */
- (IBAction) Browse: (id) sender
{
    [NSApp endSheet:fPanel];
	[fPanel orderOut:self];
	[self performSelectorOnMainThread:@selector(Browse2:) withObject:nil waitUntilDone:NO];
}
- (void) Browse2: (id) sender
{
    NSOpenPanel * panel;
	
    panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection: NO];
    [panel setCanChooseFiles: YES];
    [panel setCanChooseDirectories: YES ];
    NSString * sourceDirectory;
	if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"])
	{
		sourceDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"];
	}
	else
	{
		sourceDirectory = @"~/Desktop";
		sourceDirectory = [sourceDirectory stringByExpandingTildeInPath];
	}
    /*
	 Old open browse window, lets keep it her for a couple of revs for reference
	 if problems might crop up */
	/*
	 [panel beginSheetForDirectory: sourceDirectory file: nil types: nil
					modalForWindow: [NSApp mainWindow] modalDelegate: self
					didEndSelector: @selector( BrowseDone:returnCode:contextInfo: )
					   contextInfo: nil];
	 */
	[panel beginSheetForDirectory: sourceDirectory file: nil types: nil
				   modalForWindow: fWindow modalDelegate: self
				   didEndSelector: @selector( BrowseDone:returnCode:contextInfo: )
					  contextInfo: nil];
}

/* Get the folder and switch sheets again. Use the same trick as
   above */
- (void) BrowseDone: (NSOpenPanel *) sheet
		 returnCode: (int) returnCode contextInfo: (void *) contextInfo
{
    /* User selected a file to open */
	if( returnCode == NSOKButton )
    {
        [fFolderField setStringValue:
            [[sheet filenames] objectAtIndex: 0]];
        [self Open: nil];
		[self performSelectorOnMainThread: @selector( BrowseDone2: )
							   withObject: nil waitUntilDone: NO];
    }
	else // User clicked Cancel in browse window
	{
		/* If DVD Auto Detect is disabled */
		if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DisableDvdAutoDetect"] == 1)
		{
			/* We close the scan panel altogether */
			    [self Cancel: nil];
		}
		else /* Dvd auto detect is on */
		{
			/* We return back to the scan choice sheet */
			[self performSelectorOnMainThread: @selector( BrowseDone2: )
								   withObject: nil waitUntilDone: NO];
								   
	
		}
	}
    
    
	
}
- (void) BrowseDone2: (id) sender
{
    [NSApp beginSheet:fPanel modalForWindow:fWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
}

- (IBAction) Open: (id) sender
{
  [self         EnableUI: NO];
    [fStatusField setStringValue: _( @"Opening..." )];

	[fDriveDetector stop];

    if( [fMatrix selectedRow] )
    {
	/* we set the last source directory in the prefs here */
	NSString *sourceDirectory = [[fFolderField stringValue] stringByDeletingLastPathComponent];
	[[NSUserDefaults standardUserDefaults] setObject:sourceDirectory forKey:@"LastSourceDirectory"];
    	hb_scan( fHandle, [[fFolderField stringValue] UTF8String], 0 );
    }
    else
    {
	    hb_scan( fHandle, [[fDrives objectForKey: [fDetectedPopUp
                 titleOfSelectedItem]] UTF8String], 0 );
    }
	
	
	
}

- (IBAction) Cancel: (id) sender
{
    [NSApp endSheet:fPanel];
	[fPanel orderOut:self];
}

@end