forked from DerPate2010/Xbmc2ndScr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainVm.cs
783 lines (670 loc) · 26.2 KB
/
MainVm.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;
using Okra.Core;
using TmdbWrapper;
using XBMCRPC.Audio.Fields;
using XBMCRPC.Files;
using XBMCRPC.List;
using XBMCRPC.List.Fields;
using XBMCRPC.List.Item;
using XBMCRPC.List.Items;
using XBMCRPC.Playlist;
using XBMCRPC.Video.Fields;
using Xbmc2S.Model.Download;
using Xbmc2S.RT.UPnP;
using Files = XBMCRPC.Methods.Files;
namespace Xbmc2S.Model
{
public class MainVm:BindableBase
{
internal IPlatformServices PlatformServices { get; private set; }
private AppContext _appContext;
private TvShowSource _tvShowSource;
private MusicArtistSource _musicArtistSource;
private TVShowVm _lastTvShow;
public SettingsVm Settings { get { return _appContext.Settings; } }
public LibraryManager LibraryManager { get; private set; }
public MainVm(IPlatformServices platformServices, IView view)
{
PlatformServices = platformServices;
_appContext = new AppContext(PlatformServices, view);
_appContext.Settings.SettingsChanged += Settings_SettingsChanged;
InitHomePageGroups();
LibraryManager= new LibraryManager(_appContext);
}
void Settings_SettingsChanged(object sender, EventArgs e)
{
_sourcesCache.Clear();
InitHomePageGroups();
}
private void InitHomePageGroups()
{
MovieGroup = new MovieGroup(_appContext) {Title = "Movies"};
PeopleGroup = new PeopleGroup(_appContext) {Title = "People"};
TvGroup = new TVGroup(_appContext) {Title = "TV Shows"};
MusicGroup = new MusicGroup(_appContext) {Title = "Music"};
}
public PeopleGroup PeopleGroup
{
get { return _peopleGroup; }
set { SetProperty(ref _peopleGroup, value); }
}
public MusicGroup MusicGroup
{
get { return _musicGroup; }
set { SetProperty(ref _musicGroup, value); }
}
public TVGroup TvGroup
{
get { return _tvGroup; }
set { SetProperty(ref _tvGroup, value); }
}
public MovieGroup MovieGroup
{
get { return _movieGroup; }
set { SetProperty(ref _movieGroup, value); }
}
public MusicArtistSource MusicArtistSource
{
get { return _musicArtistSource; }
set { _musicArtistSource = value; OnPropertyChanged();}
}
public RemoteVm RemoteControl
{
get
{
if (_remoteControl == null)
{
_remoteControl= new RemoteVm(_appContext);
}
return _remoteControl;
}
}
public CurrentConnectionVm CurrentConnection
{
get
{
if (_currentConnection == null)
{
_currentConnection = new CurrentConnectionVm(_appContext);
}
return _currentConnection;
}
}
public WatchListVm GetWatchList(IWatchListView view)
{
//return new WatchListVm(view, _appContext.WatchList);
throw new NotSupportedException();
}
private Dictionary<ItemsSourceReference, IItemsSource> _sourcesCache = new Dictionary<ItemsSourceReference, IItemsSource>(new ItemSourceRefComparer());
private MovieGroup _movieGroup;
private TVGroup _tvGroup;
private MusicGroup _musicGroup;
private PeopleGroup _peopleGroup;
private List<GroupBase> _items;
private RemoteVm _remoteControl;
private CurrentConnectionVm _currentConnection;
public IItemsSource GetMovieSource(ItemsSourceReference itemsSourceReference = null)
{
IItemsSource itemsSource=null;
if (itemsSourceReference != null)
{
ItemsSourceReference current=null;
if (_sourcesCache.TryGetValue(itemsSourceReference, out itemsSource))
{
current = itemsSource.GetStateRepresentation();
}
if (!itemsSourceReference.Equals(current))
{
switch (itemsSourceReference.Type)
{
case ItemsSourceType.Movie:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.All:
itemsSource = new AllMovieSource(_appContext);
break;
case ItemsSourceFilter.Cast:
itemsSource = new CastMovieSource(_appContext, itemsSourceReference.Param);
break;
case ItemsSourceFilter.Id:
itemsSource = new IdMovieSource(_appContext, int.Parse(itemsSourceReference.Param));
break;
case ItemsSourceFilter.Title:
itemsSource = new TitleMovieSource(_appContext, itemsSourceReference.Param);
break;
case ItemsSourceFilter.FullText:
itemsSource = new FullTextMovieSource(_appContext, itemsSourceReference.Param);
break;
}
break;
case ItemsSourceType.Episode:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.Cast:
itemsSource = new CastEpisodesSource(_appContext, itemsSourceReference.Param);
break;
}
break;
case ItemsSourceType.TVShow:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.Title:
itemsSource = new TitleTVShowSource(_appContext,itemsSourceReference.Param);
break;
case ItemsSourceFilter.Cast:
itemsSource = new CastTTVShowSource(_appContext,itemsSourceReference.Param);
break;
case ItemsSourceFilter.All:
itemsSource = new TvShowSource(_appContext);
break;
case ItemsSourceFilter.Id:
itemsSource = new TvShowIdSource(_appContext, int.Parse(itemsSourceReference.Param));
break;
}
break;
case ItemsSourceType.WatchList:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.All:
itemsSource = new WatchlistMovieSource(_appContext);
break;
}
break;
case ItemsSourceType.Season:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.All:
itemsSource = new WatchlistMovieSource(_appContext);
break;
}
break;
case ItemsSourceType.Extern:
switch (itemsSourceReference.Filter)
{
case ItemsSourceFilter.Cast:
itemsSource = new CastTmdbResult(itemsSourceReference.Param);
break;
case ItemsSourceFilter.Title:
itemsSource = new TmdbResult(itemsSourceReference.Param);
break;
}
break;
}
}
_sourcesCache[itemsSourceReference] = itemsSource;
current = itemsSource.GetStateRepresentation();
if ((itemsSource.Selected==null ||
itemsSourceReference.Selection != current.Selection) &&
itemsSourceReference.Selection>=0)
{
itemsSource.RestoreSelection(itemsSourceReference.Selection);
}
}
if (itemsSource == null)
{
itemsSource = new AllMovieSource(_appContext);
}
return itemsSource;
}
private CurrentPlaybackVm _currentPlaybackVm;
public async Task<CurrentPlaybackVm> GetCurrentPlayingItem()
{
if (_currentPlaybackVm == null)
{
_currentPlaybackVm = new CurrentPlaybackVm(_appContext);
}
else
{
_currentPlaybackVm.RefreshNow();
}
return _currentPlaybackVm;
}
public PersonVm SearchPerson(string name)
{
var person = new PersonVm(name);
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Movie, Filter = ItemsSourceFilter.Cast, Param = name };
person.Movies= GetMovieSource(movieRef);
movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Episode, Filter = ItemsSourceFilter.Cast, Param = name };
person.Episodes= GetMovieSource(movieRef);
movieRef = new ItemsSourceReference() { Type = ItemsSourceType.TVShow, Filter = ItemsSourceFilter.Cast, Param = name };
person.TVShows= GetMovieSource(movieRef);
movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Extern, Filter = ItemsSourceFilter.Cast, Param = name };
person.External = GetMovieSource(movieRef);
return person;
}
public void ResetMusic()
{
if (MusicArtistSource == null)
{
MusicArtistSource = new MusicArtistSource(_appContext);
}
}
public async Task<TVShowVm> GetTVShow(int tvshowid)
{
if (_lastTvShow != null && _lastTvShow.ID == tvshowid)
{
return _lastTvShow;
}
var tvshow = await _appContext.XBMC.VideoLibrary.GetTVShowDetails(tvshowid, TVShow.AllFields());
_lastTvShow = new TVShowVm(tvshow.tvshowdetails, _appContext, false);
return _lastTvShow ;
}
public AlbumSource GetAlbums(int artistId)
{
return new AlbumSource(_appContext, artistId);
}
public async Task<AlbumVm> GetAlbum(int albumId)
{
var album = await _appContext.XBMC.AudioLibrary.GetAlbumDetails(albumId, Album.AllFields());
return new AlbumVm(album.albumdetails, _appContext);
}
public async Task<ArtistVm> GetArtist(int artistId)
{
var artist = await _appContext.XBMC.AudioLibrary.GetArtistDetails(artistId, Artist.AllFields());
return new ArtistVm(artist.artistdetails, _appContext);
}
public IDownloadManager GetDownloads()
{
return _appContext.Downloads;
}
public Task Suspend()
{
return _appContext.Downloads.PersistDownloads();
}
public Task<int> GetSearchFullCount(string query)
{
return FullTextMovieSource.GetResultCount(_appContext, query);
}
public void GotoAllMovies()
{
var movieRef = new ItemsSourceReference() {Type = ItemsSourceType.Movie, Filter = ItemsSourceFilter.All};
_appContext.View.GotoMovies(movieRef);
}
public void GoTo(MovieVm clickedItem)
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Movie, Filter = ItemsSourceFilter.Id, Param = clickedItem.Id.ToString()};
_appContext.View.GotoMovie(movieRef);
}
public IItemsSource GetMovieSource(object movieSourceRef)
{
return GetMovieSource(movieSourceRef as ItemsSourceReference);
}
public IItemsSource SearchFullText(string query)
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Movie, Filter = ItemsSourceFilter.FullText, Param = query};
return GetMovieSource(movieRef);
}
public IItemsSource SearchTitle(string query)
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Movie, Filter = ItemsSourceFilter.Title, Param = query };
return GetMovieSource(movieRef);
}
public void GotoWatchList()
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.WatchList, Filter = ItemsSourceFilter.All};
_appContext.View.GotoMovies(movieRef);
}
public IItemsSource SearchExtern(string query)
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.Extern, Filter = ItemsSourceFilter.Title, Param = query };
return GetMovieSource(movieRef);
}
public IItemsSource SearchShow(string query)
{
var movieRef = new ItemsSourceReference() { Type = ItemsSourceType.TVShow, Filter = ItemsSourceFilter.Title, Param = query };
return GetMovieSource(movieRef);
}
public async Task<DirectoryInfo> GetDirectory(Media mediaType)
{
var sources = await _appContext.XBMC.Files.GetSources(mediaType,sort: new Sort(){method = Sort_method.label, order = Sort_order.@ascending});
var di = DirectoryInfo.GetRootDirectoryInfo();
if (sources.sources == null) return di;
di.Items = sources.sources.Select(s => new DirectoryInfo(s)).ToList<DirectoryItem>();
return di;
}
public async Task<DirectoryInfo> GetDirectory(string path, Media mediaType)
{
var content = await _appContext.XBMC.Files.GetDirectory(path, mediaType, properties: new XBMCRPC.List.Fields.Files()
{
FilesItem.title
}, sort: new Sort() { method = Sort_method.label, order = Sort_order.@ascending });
var di = new DirectoryInfo(path,content);
return di;
}
public async Task<FileInfo> GetFileDetails(string path, Media mediaType)
{
var file = await _appContext.XBMC.Files.GetFileDetails(path, mediaType, XBMCRPC.List.Fields.Files.AllFields());
return new FileInfo(file.filedetails, _appContext);
}
public async Task GetUnlistedFiles(IList<string> listToFill, Media mediaType = Media.video)
{
var sources = await _appContext.XBMC.Files.GetSources(mediaType);
foreach (var source in sources.sources)
{
await GetUnlistedFiles(listToFill, mediaType, source.file);
}
}
public async Task GetUnlistedFiles(IList<string> listToFill, Media mediaType, string directory)
{
GetDirectoryResponse files;
try
{
files = await _appContext.XBMC.Files.GetDirectory(directory, mediaType, new XBMCRPC.List.Fields.Files() { FilesItem.mimetype });
}
catch (Exception ex)
{
return;
}
if (files.files != null)
{
foreach (var file in files.files)
{
if (file.filetype == File_filetype.directory)
{
await GetUnlistedFiles(listToFill, mediaType, file.file);
}
else
{
if (file.id == 0)
{
listToFill.Add(file.file);
}
}
}
}
}
public async Task GoTo(ItemsSourceReference itemsSourceReference)
{
switch (itemsSourceReference.Type)
{
case ItemsSourceType.Episode:
var episode = await
_appContext.XBMC.VideoLibrary.GetEpisodeDetails(Convert.ToInt32(itemsSourceReference.Param),
Episode.AllFields());
var vm = new EpisodeVm(episode.episodedetails, _appContext);
vm.GoTo();
break;
case ItemsSourceType.Movie:
_appContext.View.GotoMovie(itemsSourceReference);
break;
}
}
public void RetryConnection()
{
_appContext.Init();
_sourcesCache.Clear();
InitHomePageGroups();
}
}
public abstract class DirectoryItem
{
public string Label { get; protected set; }
public string Path { get; protected set; }
protected DirectoryItem(string label, string path)
{
Label = label;
Path = path;
}
protected DirectoryItem(File file)
{
var baseFile = file.AsMediaDetailsBase;
if (baseFile != null)
{
if (string.IsNullOrEmpty(file.AsVideoDetailsFile.title))
{
if (string.IsNullOrEmpty(file.AsAudioDetailsMedia.title))
{
Label = baseFile.label;
}
else
{
Label = file.AsAudioDetailsMedia.title;
}
}
else
{
Label = file.AsVideoDetailsFile.title;
}
}
else
{
Label = System.IO.Path.GetFileName(file.file.TrimEnd('\\', '/'));
}
Path = file.file;
}
}
public class DirectoryInfo:DirectoryItem
{
public DirectoryInfo(File file)
: base(file)
{
Path = System.IO.Path.GetDirectoryName(file.file);
}
private DirectoryInfo(string label) : base(label, string.Empty)
{
}
public DirectoryInfo(SourcesItem sourcesItem) : base(sourcesItem.label, sourcesItem.file)
{
}
public DirectoryInfo(string path, GetDirectoryResponse getDirectoryResponse)
: base(System.IO.Path.GetFileName(path), path)
{
if (getDirectoryResponse.files != null)
{
Items = getDirectoryResponse.files.Select(GetDirItem).ToList();
}
}
private DirectoryItem GetDirItem(File arg)
{
if (arg.filetype == File_filetype.directory)
{
return new DirectoryInfo(arg);
}
return new FileInfo(arg);
}
public static DirectoryInfo GetRootDirectoryInfo()
{
return new DirectoryInfo("Root");
}
public List<FileInfo> Files { get; set; }
public List<DirectoryInfo> Directories { get; set; }
public List<DirectoryItem> Items { get; set; }
}
public class FileInfo : DirectoryItem, IItemDetails
{
public FileInfo(File file):base(file)
{
switch (file.type)
{
case Base_type.movie:
Type=ItemsSourceType.Movie;
break;
case Base_type.episode:
Type=ItemsSourceType.Episode;
break;
}
Id = file.id;
}
public bool TryGetLibraryRef(out ItemsSourceReference reference)
{
if (Type != null && Id > 0)
{
reference = new ItemsSourceReference(Type.Value, Id);
return true;
}
reference = null;
return false;
}
public ItemsSourceType? Type { get; set; }
public FileInfo(File filedetails, IAppContext appContext) : this(filedetails)
{
Size = filedetails.size;
Modified = filedetails.lastmodified;
MimeType = filedetails.mimetype;
SecondLabel = filedetails.tagline;
var file = filedetails.AsMediaDetailsBase;
if (file != null)
{
Images = new VideoImages(appContext, file);
}
var media = filedetails.AsMediaDetailsBase;
if (media != null)
{
Images = new VideoImages(appContext, media);
}
Play = new PlayFileCommand(appContext, Path);
}
public string MimeType { get; set; }
public string Modified { get; set; }
public int Size { get; set; }
public class PlayFileCommand : ICommand
{
private readonly IAppContext _appContext;
private readonly string _path;
public PlayFileCommand(IAppContext appContext, string path)
{
_appContext = appContext;
_path = path;
}
public bool CanExecute(object parameter)
{
return true;
}
public async void Execute(object parameter)
{
await _appContext.XBMC.Player.Open(new ItemFile() { file = _path});
_appContext.View.GotoCurrentPlaying();
}
public event EventHandler CanExecuteChanged;
}
public int Runtime { get; private set; }
public int Year { get; private set; }
public string Content { get; private set; }
public IEnumerable<ICastVm> Cast { get; private set; }
public string Genre { get; private set; }
public string Country { get; private set; }
public string OriginalTitle { get; private set; }
public double Rating { get; private set; }
public double RatingBase5 { get; set; }
public string Studio { get; private set; }
public string Writer { get; private set; }
public string Director { get; private set; }
public string Trailer { get; private set; }
public DelegateCommand PlayTrailer { get; private set; }
public bool IsOffline { get; private set; }
public bool IsWatched { get; set; }
public ICommand Play { get; private set; }
public ICommand PrepareDownload { get; private set; }
public DelegateCommand ToWatchlist { get; private set; }
public DelegateCommand FromWatchlist { get; private set; }
public int Id { get; set; }
public string SecondLabel { get; private set; }
public void GoTo()
{
throw new NotImplementedException();
}
public VideoImages Images { get; private set; }
}
public class LibraryManager
{
private readonly AppContext _appContext;
public LibraryManager(AppContext appContext)
{
_appContext = appContext;
CleanVideoLib = new DelegateCommand(CleanVideoExecute);
CleanAudioLib = new DelegateCommand(CleanAudioExecute);
ScanVideoLib = new DelegateCommand(ScanVideoExecute);
ScanAudioLib = new DelegateCommand(ScanAudioExecute);
}
private async void ScanAudioExecute()
{
try
{
await _appContext.XBMC.AudioLibrary.Scan();
}
catch (Exception)
{
}
}
private async void ScanVideoExecute()
{
try
{
await _appContext.XBMC.VideoLibrary.Scan();
}
catch (Exception)
{
}
}
private async void CleanAudioExecute()
{
try
{
await _appContext.XBMC.AudioLibrary.Clean();
}
catch (Exception)
{
}
}
private async void CleanVideoExecute()
{
try
{
await _appContext.XBMC.VideoLibrary.Clean();
}
catch (Exception)
{
} }
public ICommand CleanVideoLib { get; private set; }
public ICommand CleanAudioLib { get; private set; }
public ICommand ScanVideoLib { get; private set; }
public ICommand ScanAudioLib { get; private set; }
}
public class PersonVm:BindableBase
{
private readonly string _name;
public IItemsSource Movies { get; set; }
private TmdbPerson _info;
private Task _infoTask;
public PersonVm(string name)
{
_name = name;
}
public TmdbPerson Info
{
get
{
if (_info == null && _infoTask == null)
{
_infoTask = LoadInfoAsync();
}
return _info;
}
private set
{
if (Equals(value, _info)) return;
_info = value;
OnPropertyChanged();
}
}
public IItemsSource Episodes { get; set; }
public IItemsSource TVShows { get; set; }
public IItemsSource External { get; set; }
private async Task LoadInfoAsync()
{
var tmdbConfig = await TheMovieDb.GetConfiguration();
var tmdbInfo = await TheMovieDb.SearchPersonAsync(_name);
if (tmdbInfo.Results != null && tmdbInfo.Results.Count == 1)
{
var details = await tmdbInfo.Results[0].PersonAsync();
Info = new TmdbPerson(tmdbConfig, details);
}
}
}
}