/
SelectDVDHandler.cs
510 lines (468 loc) · 16.4 KB
/
SelectDVDHandler.cs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using MediaPortal.Dialogs;
using MediaPortal.GUI.Library;
using MediaPortal.Player;
using MediaPortal.Services;
using MediaPortal.Util;
using MediaPortal.Video.Database;
namespace MediaPortal.GUI.Video
{
/// <summary>
/// Service class to display DVD selection dialog
/// </summary>
public class SelectDVDHandler : ISelectDVDHandler
{
public string ShowSelectDVDDialog(int parentId)
{
return ShowSelectDriveDialog(parentId, true);
}
public string ShowSelectDriveDialog(int parentId, bool DVDonly)
{
Log.Info("SelectDVDHandler: ShowSelectDVDDialog()");
//check if dvd is inserted
List<GUIListItem> rootDrives = VirtualDirectories.Instance.Movies.GetRootExt();
for (int i = rootDrives.Count - 1; i >= 0; i--)
{
GUIListItem item = (GUIListItem)rootDrives[i];
if (Util.Utils.getDriveType(item.Path) == 5) //cd or dvd drive
{
string driverLetter = item.Path.Substring(0, 1);
string fileName = DVDonly
? String.Format(@"{0}:\VIDEO_TS\VIDEO_TS.IFO", driverLetter)
: String.Format(@"{0}:\", driverLetter);
if (DVDonly && !File.Exists(fileName))
{
rootDrives.RemoveAt(i);
}
else if (!DVDonly && !Directory.Exists(fileName))
{
rootDrives.RemoveAt(i);
}
}
else
{
rootDrives.RemoveAt(i);
}
}
if (rootDrives.Count > 0)
{
try
{
if (rootDrives.Count == 1)
{
GUIListItem ritem = (GUIListItem)rootDrives[0];
return ritem.Path; // Only one DVD available, play it!
}
SetIMDBThumbs(rootDrives, false);
// Display a dialog with all drives to select from
GUIDialogSelect2 dlgSel =
(GUIDialogSelect2)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_SELECT2);
if (null == dlgSel)
{
Log.Info("SelectDVDHandler: Could not open dialog, defaulting to first drive found");
GUIListItem ritem = (GUIListItem)rootDrives[0];
return ritem.Path;
}
dlgSel.Reset();
dlgSel.SetHeading(DVDonly ? 196 : 2201); // Choose movie | select source
for (int i = 0; i < rootDrives.Count; i++)
{
GUIListItem dlgItem = new GUIListItem();
dlgItem = (GUIListItem)rootDrives[i];
Log.Debug("SelectDVDHandler: adding list item of possible playback location - {0}", dlgItem.Path);
dlgSel.Add(dlgItem);
}
dlgSel.DoModal(parentId);
if (dlgSel.SelectedLabel != -1)
{
return dlgSel.SelectedLabelText.Substring(1, 2);
}
else
{
return null;
}
}
catch (Exception ex)
{
Log.Warn("SelectDVDHandler: could not determine dvd path - {0},{1}", ex.Message, ex.StackTrace);
return null;
}
}
//no disc in drive...
GUIDialogOK dlgOk = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
dlgOk.SetHeading(1020); //information
Log.Error("SelectDVDHandler: ShowSelectDriveDialog - Plz Insert Disk");
dlgOk.SetLine(1, 219); //no disc
dlgOk.DoModal(parentId);
Log.Info("SelectDVDHandler: did not find a movie");
return null;
}
public bool OnPlayDVD(String drive, int parentId)
{
Log.Info("SelectDVDHandler: OnPlayDVD() playing DVD {0}", drive);
if (g_Player.Playing && g_Player.IsDVD)
{
if (g_Player.CurrentFile.Equals(drive + @"\VIDEO_TS\VIDEO_TS.IFO"))
{
return true;
}
}
if (Util.Utils.getDriveType(drive) == 5) //cd or dvd drive
{
string driverLetter = drive.Substring(0, 1);
string fileName = String.Format(@"{0}:\VIDEO_TS\VIDEO_TS.IFO", driverLetter);
if (!VirtualDirectories.Instance.Movies.RequestPin(fileName))
{
return false;
}
if (File.Exists(fileName))
{
IMDBMovie movieDetails = new IMDBMovie();
VideoDatabase.GetMovieInfo(fileName, ref movieDetails);
int idFile = VideoDatabase.GetFileId(fileName);
int idMovie = VideoDatabase.GetMovieId(fileName);
int timeMovieStopped = 0;
byte[] resumeData = null;
if ((idMovie >= 0) && (idFile >= 0))
{
timeMovieStopped = VideoDatabase.GetMovieStopTimeAndResumeData(idFile, out resumeData, g_Player.SetResumeBDTitleState);
//Log.Info("GUIVideoFiles: OnPlayBackStopped for DVD - idFile={0} timeMovieStopped={1} resumeData={2}", idFile, timeMovieStopped, resumeData);
if (timeMovieStopped > 0)
{
string title = Path.GetFileName(fileName);
VideoDatabase.GetMovieInfoById(idMovie, ref movieDetails);
if (movieDetails.Title != string.Empty)
{
title = movieDetails.Title;
}
GUIResumeDialog.Result result =
GUIResumeDialog.ShowResumeDialog(title, timeMovieStopped,
GUIResumeDialog.MediaType.DVD);
if (result == GUIResumeDialog.Result.Abort)
return false;
if (result == GUIResumeDialog.Result.PlayFromBeginning)
timeMovieStopped = 0;
}
}
if (g_Player.Playing)
{
g_Player.Stop();
}
g_Player.PlayDVD(drive + @"\VIDEO_TS\VIDEO_TS.IFO");
g_Player.ShowFullScreenWindow();
if (g_Player.Playing && timeMovieStopped > 0)
{
if (g_Player.IsDVD)
{
// send resume thread async
var msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SET_RESUME_STATE, 0, 0, 0, 0, 0, resumeData);
GUIWindowManager.SendThreadMessage(msg);
}
else
{
Log.Debug("SelectDVDHandler.OnPlayDVD - skipping");
g_Player.SeekAbsolute(timeMovieStopped);
}
}
return true;
}
}
//no disc in drive...
GUIDialogOK dlgOk = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
dlgOk.SetHeading(3); //my videos
Log.Error("SelectDVDHandler: OnPlayDVD() Plz Insert Disk (ShowSelectDriveDialog)");
dlgOk.SetLine(1, 219); //no disc
dlgOk.DoModal(parentId);
return false;
}
// Changed - cover for movies with the same name
public void SetIMDBThumbs(IList items, bool markWatchedFiles)
{
try
{
GUIListItem pItem;
ISelectBDHandler selectBdHandler;
bool dedicatedMovieFolderChecked = false;
bool isDedicatedMovieFolder = false;
if (GlobalServiceProvider.IsRegistered<ISelectBDHandler>())
{
selectBdHandler = GlobalServiceProvider.Get<ISelectBDHandler>();
}
else
{
selectBdHandler = new SelectBDHandler();
GlobalServiceProvider.Add<ISelectBDHandler>(selectBdHandler);
}
for (int x = 0; x < items.Count; x++)
{
string strThumb = string.Empty;
pItem = (GUIListItem)items[x];
string file = string.Empty;
bool isFolderPinProtected = (pItem.IsFolder && IsFolderPinProtected(pItem.Path));
IMDBMovie movie = pItem.AlbumInfoTag as IMDBMovie;
if (movie == null)
{
IMDBMovie.SetMovieData(pItem);
movie = pItem.AlbumInfoTag as IMDBMovie;
}
// Check for everymovieinitsownfolder only once for all items (defined share is the same for all)
if (!dedicatedMovieFolderChecked && !string.IsNullOrEmpty(pItem.Path))
{
if (!pItem.IsRemote && !VirtualDirectories.Instance.Movies.IsRootShare(pItem.Path))
{
dedicatedMovieFolderChecked = true;
isDedicatedMovieFolder = Util.Utils.IsFolderDedicatedMovieFolder(pItem.Path);
}
}
// Skip DVD & BD backup folder
if (pItem.IsFolder && !pItem.IsBdDvdFolder)
{
if (pItem.Label == "..")
{
continue;
}
// If this is enabled you'll see the thumb of the first movie in that dir - but if you put serveral movies into that dir you'll be irritated...
if (!pItem.IsRemote && isDedicatedMovieFolder)
{
string[] strFiles = null;
try
{
strFiles = Directory.GetFiles(pItem.Path);
}
catch (Exception) { }
if (strFiles != null)
{
for (int i = 0; i < strFiles.Length; ++i)
{
string extension = Path.GetExtension(strFiles[i]);
if (VirtualDirectory.IsImageFile(extension))
{
if (DaemonTools.IsEnabled)
{
file = strFiles[i];
break;
}
continue;
}
if (VirtualDirectory.IsValidExtension(strFiles[i], Util.Utils.VideoExtensions, false))
{
// Skip hidden files
if ((File.GetAttributes(strFiles[i]) & FileAttributes.Hidden) == FileAttributes.Hidden)
{
continue;
}
file = strFiles[i];
break;
}
}
}
}
}
// Check for DVD folder
else if (pItem.IsBdDvdFolder && IsDvdDirectory(pItem.Path))
{
file = GetFolderVideoFile(pItem.Path);
}
// Check for BD folder
else if (pItem.IsBdDvdFolder && selectBdHandler.IsBDDirectory(pItem.Path))
{
file = selectBdHandler.GetFolderVideoFile(pItem.Path);
}
else if (!pItem.IsFolder ||
(pItem.IsFolder && VirtualDirectory.IsImageFile(Path.GetExtension(pItem.Path).ToLowerInvariant())))
{
file = pItem.Path;
}
else
{
continue;
}
if (!string.IsNullOrEmpty(file))
{
int id = movie.ID;
// Set thumb for movies
if (id > 0 && !movie.IsEmpty)
{
if (Util.Utils.IsDVD(pItem.Path))
{
pItem.Label = String.Format("({0}:) {1}", pItem.Path.Substring(0, 1), movie.Title);
}
string titleExt = movie.Title + "{" + id + "}";
strThumb = Util.Utils.GetCoverArt(Thumbs.MovieTitle, titleExt);
}
if (!Util.Utils.FileExistsInCache(strThumb) || string.IsNullOrEmpty(strThumb))
{
string fPic = string.Empty;
string fPicTbn = string.Empty;
string path = pItem.Path;
Util.Utils.RemoveStackEndings(ref path);
Util.Utils.RemoveStackEndings(ref file);
string jpgExt = ".jpg";
string tbnExt = ".tbn";
string folderJpg = @"\folder.jpg";
if (pItem.IsBdDvdFolder)
{
fPic = string.Concat(pItem.Path,@"\", Path.GetFileNameWithoutExtension(path), jpgExt);
fPicTbn = string.Concat(pItem.Path, @"\", Path.GetFileNameWithoutExtension(path), tbnExt);
}
else
{
fPic = Path.ChangeExtension(file, jpgExt);
fPicTbn = Path.ChangeExtension(file, tbnExt);
}
if (File.Exists(fPic))
{
strThumb = fPic;
}
else if (File.Exists(fPicTbn))
{
strThumb = fPicTbn;
}
else
{
if (!pItem.IsFolder && isDedicatedMovieFolder)
{
fPic = Path.GetDirectoryName(pItem.Path) + folderJpg;
if (File.Exists(fPic))
{
strThumb = fPic;
}
else
{
continue;
}
}
else
{
continue;
}
}
}
pItem.ThumbnailImage = strThumb;
pItem.IconImageBig = strThumb;
pItem.IconImage = strThumb;
strThumb = Util.Utils.ConvertToLargeCoverArt(strThumb);
if (Util.Utils.FileExistsInCache(strThumb))
{
pItem.ThumbnailImage = strThumb;
}
movie = null;
} // <-- file == empty
} // of for (int x = 0; x < items.Count; ++x)
}
catch (ThreadAbortException)
{
// Will be logged in thread main code
}
catch (Exception ex)
{
Log.Error("SelectDVDHandler: SetIMDbThumbs: {0}", ex.Message);
}
}
public bool IsDvdDirectory(string path)
{
if (File.Exists(path + @"\VIDEO_TS\VIDEO_TS.IFO"))
{
return true;
}
return false;
}
private bool IsFolderPinProtected(string folder)
{
string pinCode = string.Empty;
return VirtualDirectories.Instance.Movies.IsProtectedShare(folder, out pinCode);
}
public string GetFolderVideoFile(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
// IFind first movie file in folder
string strExtension = Path.GetExtension(path).ToLowerInvariant();
if (VirtualDirectory.IsImageFile(strExtension))
{
return path;
}
else
{
if (VirtualDirectories.Instance.Movies.IsRemote(path))
{
return string.Empty;
}
if (!path.EndsWith(@"\"))
{
path = path + @"\";
}
string[] strDirs = null;
try
{
strDirs = Directory.GetDirectories(path, "video_ts");
}
catch (Exception) {}
if (strDirs != null)
{
if (strDirs.Length == 1)
{
Log.Debug("GUIVideoFiles: DVD folder detected - {0}", strDirs[0]);
return String.Format(@"{0}\VIDEO_TS.IFO", strDirs[0]);
}
else
{
return string.Empty;
}
}
string[] strFiles = null;
try
{
strFiles = Directory.GetFiles(path);
}
catch (Exception) {}
if (strFiles != null)
{
for (int i = 0; i < strFiles.Length; ++i)
{
string extensionension = Path.GetExtension(strFiles[i]);
if (VirtualDirectory.IsImageFile(extensionension))
{
if (DaemonTools.IsEnabled)
{
return strFiles[i];
}
continue;
}
if (VirtualDirectory.IsValidExtension(strFiles[i], Util.Utils.VideoExtensions, false))
{
// Skip hidden files
if ((File.GetAttributes(strFiles[i]) & FileAttributes.Hidden) == FileAttributes.Hidden)
{
continue;
}
return strFiles[i];
}
}
}
}
return string.Empty;
}
}
}