예제 #1
0
        private void SetupDownloadedSubtitleAndIMDbInfo(Uri uri, string resultSub, IMDb imdb, object param2)
        {
            OnlineSubtitleChoices.Clear();
            if (HasVideo == false)
            {
                return;
            }
            IMDb = imdb;

            if (param2 is List <SubtitleMatch> )
            {
                foreach (var st in (List <SubtitleMatch>)param2)
                {
                    OnlineSubtitleChoices.Add(st);
                }
                Main.ShowOsdMessage(string.Format("{0} subtitles found.", OnlineSubtitleChoices.Count));
            }
            FillSubs(uri);

            if (SubtitleStreams.Any(s => s.Path.ToLowerInvariant() == resultSub.ToLowerInvariant()) == false)
            {
                SubtitleStreams.Add(new SubtitleItem(SubtitleItem.SubtitleType.File, SubtitleItem.SubtitleSubType.Srt, resultSub, "Subtitle"));
            }

            var loadSub = (DownloadedSubtitle = SubtitleStreams.FirstOrDefault(s => s.Path.ToLowerInvariant() == resultSub.ToLowerInvariant()));

            ServiceLocator.GetService <IMainView>().DelayedInvoke(() => { SelectedSubtitle = loadSub; }, 200);
        }
예제 #2
0
        public void LoadSelectedOnlineSubtitle()
        {
            ShowOnlineSubtitles = false;
            BackgroundWorker b = new BackgroundWorker();

            b.DoWork += (sender, args) =>
            {
                DateTime start = DateTime.Now;
                Monitor.Enter(_subtitleSearchLocker);

                try
                {
                    args.Result = SubtitleUtil.DownloadSubtitle(SelectedOnlineSubtitle, Source.LocalPath);
                }
                catch (WebException)
                {
                    Main.ShowOsdMessage("Failed to download subtitle: Internet connection unavailable");
                }
                catch (Exception)
                {
                    Main.ShowOsdMessage("Failed to download subtitle from '" + SelectedOnlineSubtitle.Service + "'");
                }
                finally
                {
                    Monitor.Exit(_subtitleSearchLocker);
                    if (DateTime.Now - start > TimeSpan.FromSeconds(15))
                    {
                        args.Cancel = true;
                    }
                }
            };
            b.RunWorkerCompleted += (sender, args) =>
            {
                if (!args.Cancelled && args.Error == null && args.Result is string && Source != null)
                {
                    FillSubs(Source);
                    string resultSub = (string)args.Result;
                    if (SubtitleStreams.Any(s => s.Path.ToLowerInvariant() == resultSub.ToLowerInvariant()) == false)
                    {
                        SubtitleStreams.Add(new SubtitleItem(SubtitleItem.SubtitleType.File, SubtitleItem.SubtitleSubType.Srt, resultSub, "Subtitle"));
                    }
                    var loadSub = (DownloadedSubtitle = SubtitleStreams.FirstOrDefault(s => s.Path.ToLowerInvariant() == (resultSub).ToLowerInvariant()));
                    ServiceLocator.GetService <IMainView>().DelayedInvoke(() => { SelectedSubtitle = loadSub; }, 200);
                }
            };
            b.RunWorkerAsync();
        }
예제 #3
0
        private void SetBestSubtitles()
        {
            if (!Monitor.TryEnter(_subtitleSearchLocker, 0))
            {
                // no point in doing this if we are already downloading subtitles, which will do the same thing effectively
                return;
            }

            SubtitleItem bestSubMatch = null;

            try
            {
                if (!_subtitleIsDownloading && Source != null &&
                    SubtitleStreams.Any(s => s.Type == SubtitleItem.SubtitleType.File && File.Exists(s.Path)))
                {
                    // if nothing is downloading and we have file subs in the folder
                    string lcode = Main.SubtitleLanguages.Count > 0 ? Main.SubtitleLanguages[0].Id : "";

                    for (int i = 0; i < Main.SubtitleLanguages.Count; i++)
                    {
                        // TODO this should care also for EMBEDDED subs
                        bestSubMatch = SubtitleStreams.Where(s => s.Type == SubtitleItem.SubtitleType.File && File.Exists(s.Path)).OrderBy(f => Path.GetFileNameWithoutExtension(f.Path).ToLowerInvariant().StartsWith(Path.GetFileNameWithoutExtension(Source.LocalPath.ToLowerInvariant())) && (lcode == "" || Path.GetFileNameWithoutExtension(f.Path).EndsWith(lcode)) ? 0 : 1).FirstOrDefault();
                        if (bestSubMatch != null)
                        {
                            break;
                        }
                    }

                    ServiceLocator.GetService <IMainView>().DelayedInvoke(() => { SelectedSubtitle = bestSubMatch; }, 200);
                    return;
                }
            }
            finally
            {
                Monitor.Exit(_subtitleSearchLocker);
            }
            if (bestSubMatch == null)
            {
                DownloadSubtitleForUriAndQueryIMDB(Source);
            }
        }