1 package org.thestaticvoid.iriverter;
2 
3 import org.eclipse.swt.*;
4 import org.eclipse.swt.widgets.*;
5 import org.eclipse.swt.events.*;
6 import org.eclipse.swt.layout.*;
7 import org.eclipse.swt.custom.*;
8 import org.eclipse.swt.graphics.*;
9 
10 import java.io.*;
11 import java.util.*;
12 
13 public class DVD extends Composite implements SelectionListener, TabItemControl, DVDInfo {
14 	private CTabItem tabItem;
15 	private Combo dvdCombo, titleCombo, audioStreamCombo, subtitlesCombo;
16 	private Map titleInfo, audioStreams, subtitles;
17 	private Button chapterSelection, previewButton, outputVideoSelect;
18 	private Text outputVideoInput;
19 	private Chapters[] chapters;
20 	private String syncDrive, syncOutputVideo;
21 	private DVDProgressDialog progressDialog;
22 	private int syncTitle, syncAudioStream, syncSubtitles;
23 	private Process proc;
24 
DVD(Composite parent, int style, CTabItem tabItem)25 	public DVD(Composite parent, int style, CTabItem tabItem) {
26 		super(parent, style);
27 		this.tabItem = tabItem;
28 
29 		titleInfo = new LinkedHashMap();
30 
31 		InputStream is = getClass().getResourceAsStream("icons/dvd-16.png");
32 		tabItem.setImage(new Image(getDisplay(), is));
33 		tabItem.setText("New DVD");
34 
35 		GridLayout gridLayout = new GridLayout();
36 		gridLayout.horizontalSpacing = 6;
37 		gridLayout.verticalSpacing = 6;
38 		gridLayout.marginHeight = 0;
39 		gridLayout.marginWidth = 12;
40 		gridLayout.marginTop = 12;
41 		gridLayout.marginBottom = 0;
42 		setLayout(gridLayout);
43 
44 		Label dvdLabel = new Label(this, SWT.NONE);
45 		dvdLabel.setText("DVD");
46 		FontData[] fontData = dvdLabel.getFont().getFontData();
47 		fontData[0].setStyle(SWT.BOLD);
48 		dvdLabel.setFont(new Font(getParent().getDisplay(), fontData));
49 		GridData gridData = new GridData();
50 		dvdLabel.setLayoutData(gridData);
51 
52 		Composite dvdComp = new Composite(this, SWT.NONE);
53 		gridLayout = new GridLayout();
54 		gridLayout.horizontalSpacing = 6;
55 		gridLayout.verticalSpacing = 6;
56 		gridLayout.marginHeight = 0;
57 		gridLayout.marginWidth = 0;
58 		dvdComp.setLayout(gridLayout);
59 		dvdComp.setLayoutData(new GridData(GridData.FILL_BOTH));
60 
61 		Composite groupsComp = new Composite(dvdComp, SWT.NONE);
62 		gridLayout = new GridLayout();
63 		gridLayout.horizontalSpacing = 6;
64 		gridLayout.verticalSpacing = 6;
65 		gridLayout.marginHeight = 0;
66 		gridLayout.marginWidth = 0;
67 		gridLayout.numColumns = 2;
68 		gridLayout.makeColumnsEqualWidth = true;
69 		groupsComp.setLayout(gridLayout);
70 		groupsComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
71 
72 		Group locations = new Group(groupsComp, SWT.NONE);
73 		locations.setText("Locations");
74 		gridLayout = new GridLayout();
75 		gridLayout.horizontalSpacing = 6;
76 		gridLayout.verticalSpacing = 6;
77 		gridLayout.numColumns = 2;
78 		locations.setLayout(gridLayout);
79 		locations.setLayoutData(new GridData(GridData.FILL_BOTH));
80 
81 		Label driveLabel = new Label(locations, SWT.NONE);
82 		driveLabel.setText("DVD Drive:");
83 
84 		dvdCombo = new Combo(locations, SWT.DROP_DOWN | SWT.READ_ONLY);
85 		dvdCombo.setItems(getDVDDrives());
86 		dvdCombo.select(0);
87 		dvdCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88 		dvdCombo.addSelectionListener(this);
89 
90 		Label titleLabel = new Label(locations, SWT.NONE);
91 		titleLabel.setText("Title:");
92 
93 		titleCombo = new Combo(locations, SWT.DROP_DOWN | SWT.READ_ONLY);
94 		titleCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95 		titleCombo.addSelectionListener(this);
96 
97 		chapterSelection = new Button(locations, SWT.PUSH);
98 		chapterSelection.setText("Chapters");
99 		gridData = new GridData(GridData.FILL_HORIZONTAL);
100 		gridData.horizontalSpan = 2;
101 		chapterSelection.setLayoutData(gridData);
102 		chapterSelection.addSelectionListener(this);
103 
104 		Group languages = new Group(groupsComp, SWT.NONE);
105 		languages.setText("Languages");
106 		gridLayout = new GridLayout();
107 		gridLayout.horizontalSpacing = 6;
108 		gridLayout.verticalSpacing = 6;
109 		gridLayout.numColumns = 2;
110 		languages.setLayout(gridLayout);
111 		languages.setLayoutData(new GridData(GridData.FILL_BOTH));
112 
113 		Label audioStreamLabel = new Label(languages, SWT.NONE);
114 		audioStreamLabel.setText("Audio Stream:");
115 
116 		audioStreamCombo = new Combo(languages, SWT.DROP_DOWN | SWT.READ_ONLY);
117 		audioStreamCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
118 
119 		Label subtitlesLabel = new Label(languages, SWT.NONE);
120 		subtitlesLabel.setText("Subtitles:");
121 
122 		subtitlesCombo = new Combo(languages, SWT.DROP_DOWN | SWT.READ_ONLY);
123 		subtitlesCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124 
125 		previewButton = new Button(languages, SWT.PUSH);
126 		previewButton.setText("Preview");
127 		gridData = new GridData(GridData.FILL_HORIZONTAL);
128 		gridData.horizontalSpan = 2;
129 		previewButton.setLayoutData(gridData);
130 		previewButton.addSelectionListener(this);
131 
132 		Composite output = new Composite(dvdComp, SWT.NONE);
133 		output.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
134 		gridLayout = new GridLayout();
135 		gridLayout.horizontalSpacing = 6;
136 		gridLayout.verticalSpacing = 6;
137 		gridLayout.marginHeight = 0;
138 		gridLayout.marginWidth = 0;
139 		gridLayout.numColumns = 3;
140 		output.setLayout(gridLayout);
141 
142 		Label outputVideo = new Label(output, SWT.NONE);
143 		outputVideo.setText("Output:");
144 
145 		outputVideoInput = new Text(output, SWT.BORDER);
146 		outputVideoInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
147 
148 		outputVideoSelect = new Button(output, SWT.PUSH);
149 		outputVideoSelect.setText("Select");
150 		gridData = new GridData();
151 		gridData.widthHint = 75;
152 		outputVideoSelect.setLayoutData(gridData);
153 		outputVideoSelect.addSelectionListener(this);
154 	}
155 
widgetDefaultSelected(SelectionEvent e)156 	public void widgetDefaultSelected(SelectionEvent e) {
157 		widgetSelected(e);
158 	}
159 
widgetSelected(SelectionEvent e)160 	public void widgetSelected(SelectionEvent e) {
161 		if (e.getSource() == dvdCombo) {
162 			if (dvdCombo.getText().equals("Other...")) {
163 				DirectoryDialog directoryDialog = new DirectoryDialog(getShell(), SWT.NONE);
164 				directoryDialog.setText("DVD Device");
165 				String dvdDevice = directoryDialog.open();
166 				if (dvdDevice != null && new File(dvdDevice + File.separator + "VIDEO_TS").exists()) {
167 					if (dvdCombo.getItem(0).equals("None Found"))
168 						dvdCombo.remove(0);
169 					dvdCombo.add(dvdDevice, 0);
170 					dvdCombo.select(0);
171 				}
172 			}
173 
174 			setTitleCombo();
175 		}
176 
177 		if (e.getSource() == titleCombo) {
178 			chapters = null;
179 			setLanguageCombos();
180 		}
181 
182 		if (e.getSource() == chapterSelection) {
183 			if (dvdCombo.getText().equals("None Found") || dvdCombo.getText().equals("Other..."))
184 				return;
185 
186 			ChapterDialog chapterDialog = null;
187 			if (chapters == null)
188 				chapterDialog = new ChapterDialog(getParent().getShell(), SWT.NONE, ((DVDTitleInfo) titleInfo.get(titleCombo.getText())).getNumberOfChapters(), true);
189 			else
190 				chapterDialog = new ChapterDialog(getParent().getShell(), SWT.NONE, ((DVDTitleInfo) titleInfo.get(titleCombo.getText())).getNumberOfChapters(), chapters);
191 
192 			chapters = chapterDialog.open();
193 		}
194 
195 		if (e.getSource() == previewButton) {
196 			if (!getDrive().equals("")) {
197 				try {
198 					java.util.List commandList = new ArrayList();
199 					commandList.add(MPlayerInfo.getMPlayerPath() + "mplayer");
200 					commandList.add("-dvd-device");
201 					commandList.add(getDrive());
202 					commandList.add("dvd://" + getTitle());
203 
204 					if (getAudioStream() > -1) {
205 						commandList.add("-aid");
206 						commandList.add("" + getAudioStream());
207 					}
208 
209 					if (getSubtitles() > -1) {
210 						commandList.add("-sid");
211 						commandList.add("" + getSubtitles());
212 					}
213 
214 					String commandStr = "";
215 					String[] command = new String[commandList.size()];
216 					for (int i = 0; i < command.length; i++) {
217 						command[i] = (String) commandList.get(i);
218 						commandStr += command[i] + " ";
219 					}
220 					Logger.logMessage(commandStr, Logger.INFO);
221 
222 					proc = Runtime.getRuntime().exec(command);
223 				} catch (IOException io) {
224 					io.printStackTrace();
225 				}
226 			}
227 		}
228 
229 		if (e.getSource() == outputVideoSelect) {
230 			FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
231 			fileDialog.setText("Output Video");
232 			if (ConverterOptions.getCurrentProfile().getWrapperFormat().equals("mp4")) {
233 				fileDialog.setFilterExtensions(new String[]{"*.mp4"});
234 				fileDialog.setFilterNames(new String[]{"MP4 Video (*.mp4)"});
235 			} else {
236 				fileDialog.setFilterExtensions(new String[]{"*.avi"});
237 				fileDialog.setFilterNames(new String[]{"AVI Video (*.avi)"});
238 			}
239 			String file = fileDialog.open();
240 			if (file != null)
241 				outputVideoInput.setText(file);
242 		}
243 	}
244 
getDVDDrives()245 	private String[] getDVDDrives() {
246 		java.util.List drives = new ArrayList();
247 		if (System.getProperty("os.name").indexOf("Windows") > -1) {
248 			for (char driveLetter = 'D'; driveLetter <= 'Z'; driveLetter++) {
249 				try {
250 					File drive = new File(driveLetter + ":\\");
251 					if (drive.canRead() && new File(drive.getAbsolutePath() + "VIDEO_TS").exists())
252 						drives.add(drive.getAbsolutePath());
253 				} catch (Exception e) {
254 					// empty
255 				}
256 			}
257 		} else {
258 			try {
259 				Process proc = Runtime.getRuntime().exec("mount");
260 
261 				BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
262 				String line;
263 
264 				while ((line = input.readLine()) != null)
265 					if (line.substring(0, 4).equals("/dev")) {
266 						String mountPoint = line.split(" ")[2];
267 
268 						if (new File(mountPoint + File.separator + "VIDEO_TS").exists())
269 							drives.add(mountPoint);
270 					}
271 			} catch (Exception e) {
272 				// empty
273 			}
274 		}
275 
276 
277 		if (drives.size() == 0)
278 			drives.add("None Found");
279 
280 		drives.add("Other...");
281 
282 		String[] driveStrings = new String[drives.size()];
283 		for (int i = 0; i < driveStrings.length; i++)
284 			driveStrings[i] = (String) drives.get(i);
285 
286 		return driveStrings;
287 	}
288 
setTitleCombo()289 	public void setTitleCombo() {
290 		if (dvdCombo.getText().equals("None Found") || dvdCombo.getText().equals("Other...")) {
291 			titleCombo.removeAll();
292 			audioStreamCombo.removeAll();
293 			subtitlesCombo.removeAll();
294 
295 			return;
296 		}
297 
298 		tabItem.setText(dvdCombo.getText());
299 		progressDialog = new DVDProgressDialog(getShell(), SWT.NONE);
300 		DVDInfoReader infoReader = new DVDInfoReader(progressDialog, dvdCombo.getText());
301 		progressDialog.open();
302 
303 		titleInfo = infoReader.getTitleInfo();
304 
305 		titleCombo.removeAll();
306 		for (int i = 0; i < titleInfo.keySet().toArray().length; i++)
307 			titleCombo.add((String) titleInfo.keySet().toArray()[i]);
308 		titleCombo.select(0);
309 
310 		setLanguageCombos();
311 	}
312 
setLanguageCombos()313 	private void setLanguageCombos() {
314 		audioStreams = ((DVDTitleInfo) titleInfo.get(titleCombo.getText())).getAudioStreams();
315 		subtitles = ((DVDTitleInfo) titleInfo.get(titleCombo.getText())).getSubtitles();
316 
317 		audioStreamCombo.removeAll();
318 		for (int i = 0; i < audioStreams.keySet().size(); i++)
319 			audioStreamCombo.add((String) audioStreams.keySet().toArray()[i]);
320 		audioStreamCombo.select(0);
321 
322 		subtitlesCombo.removeAll();
323 		for (int i = 0; i < subtitles.keySet().size(); i++)
324 			subtitlesCombo.add((String) subtitles.keySet().toArray()[i]);
325 		subtitlesCombo.select(0);
326 	}
327 
getTitleInfo()328 	public Map getTitleInfo() {
329 		return titleInfo;
330 	}
331 
setTitleInfo(Map titleInfo)332 	public void setTitleInfo(Map titleInfo) {
333 		this.titleInfo = titleInfo;
334 		for (int i = 0; i < titleInfo.size(); i++)
335 			titleCombo.add((String) titleInfo.keySet().toArray()[i]);
336 
337 		if (titleInfo.size() > 0) {
338 			titleCombo.select(0);
339 			setLanguageCombos();
340 		}
341 	}
342 
setTabItem(CTabItem tabItem)343 	public void setTabItem(CTabItem tabItem) {
344 		this.tabItem = tabItem;
345 	}
346 
setDrive(String drive)347 	public void setDrive(String drive) {
348 		for (int i = 0; i < dvdCombo.getItemCount(); i++)
349 			if (dvdCombo.getItem(i).equals(drive)) {
350 				tabItem.setText(drive);
351 				return;
352 			}
353 
354 		tabItem.setText(drive);
355 
356 		if (dvdCombo.getItem(0).equals("None Found"))
357 			dvdCombo.remove(0);
358 		dvdCombo.add(drive, 0);
359 		dvdCombo.select(0);
360 	}
361 
getDrive()362 	public synchronized String getDrive() {
363 		Display.getDefault().syncExec(new Runnable() {
364 			public void run() {
365 				syncDrive = (dvdCombo.getText().equals("None Found") || dvdCombo.getText().equals("Other...") ? "" : dvdCombo.getText());
366 			}
367 		});
368 
369 		return syncDrive;
370 	}
371 
getTitle()372 	public synchronized int getTitle() {
373 		Display.getDefault().syncExec(new Runnable() {
374 			public void run() {
375 				syncTitle = titleCombo.getText().equals("") ? -1 : Integer.parseInt(titleCombo.getText().substring(0, titleCombo.getText().indexOf('.')));
376 			}
377 		});
378 
379 		return syncTitle;
380 	}
381 
getChapters()382 	public synchronized Chapters[] getChapters() {
383 		return chapters;
384 	}
385 
getAudioStream()386 	public synchronized int getAudioStream() {
387 		Display.getDefault().syncExec(new Runnable() {
388 			public void run() {
389 				syncAudioStream = audioStreams.get(audioStreamCombo.getText()).equals("") ? -1 : Integer.parseInt((String) audioStreams.get(audioStreamCombo.getText()));
390 			}
391 		});
392 
393 		return syncAudioStream;
394 	}
395 
getSubtitles()396 	public synchronized int getSubtitles() {
397 		Display.getDefault().syncExec(new Runnable() {
398 			public void run() {
399 				syncSubtitles = subtitles.get(subtitlesCombo.getText()).equals("") ? -1 : Integer.parseInt((String) subtitles.get(subtitlesCombo.getText()));
400 			}
401 		});
402 
403 		return syncSubtitles;
404 	}
405 
getOutputVideo()406 	public synchronized String getOutputVideo() {
407 		Display.getDefault().syncExec(new Runnable() {
408 			public void run() {
409 				syncOutputVideo = outputVideoInput.getText();
410 			}
411 		});
412 
413 		return syncOutputVideo;
414 	}
415 
setOutputVideo(String outputVideo)416 	public synchronized void setOutputVideo(String outputVideo) {
417 		syncOutputVideo = outputVideo;
418 
419 		Display.getDefault().syncExec(new Runnable() {
420 			public void run() {
421 				outputVideoInput.setText(syncOutputVideo);
422 			}
423 		});
424 	}
425 }
426