Example #1
0
        public static subRes Show(Window owner, BindingList <subRes> candidates)
        {
            subRes _result = null;

            ChooseSubtitles _box = new ChooseSubtitles();

            _box.Owner = owner;
            _box.WindowStartupLocation    = owner != null ? WindowStartupLocation.CenterOwner : WindowStartupLocation.CenterScreen;
            _box.SubtitlesBox.DataContext = candidates;
            var res = _box.ShowDialog();

            if (res.HasValue && res.Value && _box.SubtitlesBox.SelectedItem != null)
            {
                _result = _box.SubtitlesBox.SelectedItem as subRes;
            }

            return(_result);
        }
Example #2
0
        public bool GetSubtitle(string movieFilename, string sumCD, string language, string imdbid, string query)
        {
            bool _result = false;

            imdbid = FixImdbId(imdbid);

            if (FileManager.DisableOpenSubtitles)
            {
                return(false);
            }

            if (m_Token == null)
            {
                m_Token = Login();
            }

            if (!string.IsNullOrEmpty(movieFilename) && File.Exists(movieFilename) && m_osdbProxy != null && m_Token != null)
            {
                // search by moviehash and size
                subrt subrt = DoSearch(movieFilename, sumCD, language, imdbid, query, OSSearchType.MovieHashAndSize);
                if (subrt == null)
                {
                    // search by imdbid (if any)
                    subrt = DoSearch(movieFilename, sumCD, language, imdbid, query, OSSearchType.IMDbId);
                    if (subrt == null)
                    {
                        // search by keywords (if any)
                        subrt = DoSearch(movieFilename, sumCD, language, imdbid, query, OSSearchType.Query);
                    }
                }

                if (subrt != null && subrt.data != null && subrt.data.Length != 0)
                {
                    BindingList <subRes> _results = new BindingList <subRes>(subrt.data);
                    subRes _winner = null;
                    BindingList <subRes> _candidates = new BindingList <subRes>();
                    foreach (subRes _subRes in _results)
                    {
                        if (_subRes.SubSumCD == sumCD)
                        {
                            _candidates.Add(_subRes);
                        }
                    }
                    // if the candidates list has only one match, declare it a winner - TOO DANGEROUS
                    //if (_candidates.Count == 1)
                    //{
                    //    _winner = _candidates[0];
                    //}
                    // several candidates available => must show dialog and ask user to choose (if application wants this)
                    if (_winner == null && _candidates.Count >= 1 && NeedUserConfirmation != null)
                    {
                        // ask user
                        _winner = NeedUserConfirmation(_candidates);
                    }
                    if (_winner != null)
                    {
                        subdata _subdata = null;
                        try
                        {
                            _subdata = m_osdbProxy.DownloadSubtitles(m_Token, new string[] { _winner.IDSubtitleFile });
                        }
                        catch { }
                        if (_subdata != null)
                        {
                            foreach (subtitle _subtitle in _subdata.data)
                            {
                                if (_winner.IDSubtitleFile == _subtitle.idsubtitlefile)
                                {
                                    string _targetName = Path.ChangeExtension(movieFilename, Path.GetExtension(_winner.SubFileName));
                                    byte[] _data       = DecodeAndDecompress(_subtitle.data);
                                    // maybe check here if subtitle exists...
                                    SaveSubtitle(_targetName, true, _data);
                                    _data   = null;
                                    _result = true;
                                }
                            }
                        }
                    }
                }
            }
            return(_result);
        }