Example #1
0
        /// <summary>
        /// Handles the OnItemSelectionChanged event of the databaseCheckListBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ItemSelectionChangedEventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void DatabaseCheckListBoxOnItemSelectionChanged(object sender, ItemSelectionChangedEventArgs e)
        {
            if (!_loaded)
            {
                return;
            }

            var sellang = language.SelectedIndex != -1 ? (language.SelectedItem as StackPanel).Tag as string : string.Empty;
            var selidx  = 0;

            language.Items.Clear();

            if (databaseCheckListBox.SelectedItems.Count == 0)
            {
                return;
            }

            var langs = Languages.List.Keys.ToList();

            foreach (ListBoxItem item in databaseCheckListBox.SelectedItems)
            {
                langs = langs.Intersect(Updater.CreateGuide(item.Tag as string).SupportedLanguages).ToList();
            }

            foreach (var lang in langs)
            {
                if (sellang == lang)
                {
                    selidx = language.Items.Count;
                }

                var sp = new StackPanel
                {
                    Orientation = Orientation.Horizontal,
                    Tag         = lang
                };

                sp.Children.Add(new Image
                {
                    Source = new BitmapImage(new Uri("/RSTVShowTracker;component/Images/flag-" + lang + ".png", UriKind.Relative)),
                    Height = 16,
                    Width  = 16,
                    Margin = new Thickness(0, 1, 0, 0)
                });

                sp.Children.Add(new Label
                {
                    Content = " " + Languages.List[lang],
                    Padding = new Thickness(0)
                });

                language.Items.Add(sp);
            }

            language.SelectedIndex = selidx;
        }
Example #2
0
        /// <summary>
        /// Handles the Click event of the searchButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void SearchButtonClick(object sender, RoutedEventArgs e)
        {
            var names = namesTextBox.GetTokens().ToList();

            if (!names.Any())
            {
                MessageBox.Show("Enter at least one show and end it with ; in order to include it.", Signature.Software, MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (databaseCheckListBox.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select at least one database from which to download information for the shows.", Signature.Software, MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            _addedOne   = false;
            _successful = new List <string>();

            _lang   = language.SelectedIndex != -1 ? (language.SelectedItem as StackPanel).Tag as string : "en";
            _guides = new List <Guide>();

            foreach (ListBoxItem db in databaseCheckListBox.Items)
            {
                if (databaseCheckListBox.SelectedItems.Contains(db))
                {
                    _guides.Add(Updater.CreateGuide(db.Tag as string));
                }
            }

            _list = new List <PendingShowListViewItem>();

            foreach (var name in names)
            {
                _list.Add(new PendingShowListViewItem(name));
            }

            PendingShowListViewItemCollection.Clear();
            PendingShowListViewItemCollection.AddRange(_list);

            cancelButton.Visibility  = nextButton.Visibility = Visibility.Visible;
            restartButton.Visibility = backButton.Visibility = Visibility.Collapsed;
            cancelButton.IsEnabled   = true;
            nextButton.IsEnabled     = false;
            addTabItem.Visibility    = Visibility.Collapsed;
            listTabItem.Visibility   = Visibility.Visible;
            tabControl.SelectedIndex = 1;

            _searchThd = new Thread(() =>
            {
                var ok = false;

                foreach (var item in _list)
                {
                    var err = false;

                    Dispatcher.Invoke(() => listView.ScrollIntoView(item));

                    foreach (var guide in _guides)
                    {
                        item.Status = "Searching on " + guide.Name + "...";

                        Dispatcher.Invoke(() => CollectionViewSource.GetDefaultView(listView.ItemsSource).Refresh());

                        try
                        {
                            item.Candidates.AddRange(guide.GetID(item.Name));
                        }
                        catch (Exception ex)
                        {
                            err = true;
                            MainWindow.HandleUnexpectedException(ex);
                        }
                    }

                    if (item.Candidates.Count == 0)
                    {
                        if (err)
                        {
                            item.Group  = "Failed";
                            item.Status = "No shows found, possibly due to errors.";
                        }
                        else
                        {
                            item.Group  = "Failed";
                            item.Status = "No shows found matching this name.";
                        }
                    }
                    else
                    {
                        ok = true;

                        Dispatcher.Invoke(() =>
                        {
                            foreach (var cand in item.Candidates)
                            {
                                var sp = new StackPanel
                                {
                                    Orientation = Orientation.Horizontal
                                };

                                sp.Children.Add(new Label
                                {
                                    Content    = cand.Title,
                                    FontWeight = FontWeights.Bold,
                                    Padding    = new Thickness(0)
                                });

                                sp.Children.Add(new Label
                                {
                                    Content = " at ",
                                    Opacity = 0.5,
                                    Padding = new Thickness(0)
                                });

                                sp.Children.Add(new Image
                                {
                                    Source = new BitmapImage(new Uri(cand.Guide.Icon, UriKind.Absolute)),
                                    Height = 16,
                                    Width  = 16,
                                    Margin = new Thickness(0, 0, 0, 0)
                                });

                                sp.Children.Add(new Label
                                {
                                    Content = " " + cand.Guide.Name,
                                    Padding = new Thickness(0)
                                });

                                item.CandidateSP.Add(sp);
                            }

                            {
                                var sp = new StackPanel
                                {
                                    Orientation = Orientation.Horizontal
                                };

                                sp.Children.Add(new Image
                                {
                                    Source = new BitmapImage(new Uri("pack://application:,,,/RSTVShowTracker;component/Images/cross.png", UriKind.Absolute)),
                                    Height = 16,
                                    Width  = 16,
                                    Margin = new Thickness(0, 0, 0, 0)
                                });

                                sp.Children.Add(new Label
                                {
                                    Content = " None of the above",
                                    Padding = new Thickness(0)
                                });

                                item.CandidateSP.Add(sp);
                            }
                        });

                        item.SelectedCandidate = 0;

                        item.Group          = "Found";
                        item.ShowStatus     = "Collapsed";
                        item.ShowCandidates = "Visible";
                    }

                    Dispatcher.Invoke(() => CollectionViewSource.GetDefaultView(listView.ItemsSource).Refresh());
                }

                if (ok)
                {
                    Dispatcher.Invoke(() => nextButton.IsEnabled = true);
                }

                Dispatcher.Invoke(() =>
                {
                    cancelButton.Visibility = Visibility.Collapsed;
                    backButton.Visibility   = Visibility.Visible;
                    backButton.IsEnabled    = true;
                });
            });
            _searchThd.Start();
        }
        /// <summary>
        /// Updates the specified TV show in the database.
        /// </summary>
        /// <param name="show">The TV show to update.</param>
        /// <param name="callback">The status callback.</param>
        /// <returns>
        /// Updated TV show or <c>null</c>.
        /// </returns>
        public static TVShow Update(TVShow show, Action <int, string> callback = null)
        {
            Log.Info("Updating " + show.Title + "...");

            var st = DateTime.Now;

            if (callback != null)
            {
                callback(0, "Updating " + show.Title + "...");
            }

            Guide guide;

            try
            {
                guide = Updater.CreateGuide(show.Source);
            }
            catch (Exception ex)
            {
                Log.Error("Error while creating guide object for " + show.Title + ".", ex);

                if (callback != null)
                {
                    callback(-1, "Could not get guide object of type " + show.Source + " for " + show.Title + ".");
                }

                return(null);
            }

            TVShow tv;

            try
            {
                tv = guide.GetData(show.SourceID, show.Language);
            }
            catch (Exception ex)
            {
                Log.Error("Error while downloading data from guide for " + show.Title + ".", ex);

                if (callback != null)
                {
                    callback(-1, "Could not get guide data for " + show.Source + "#" + show.SourceID + ".");
                }

                return(null);
            }

            tv.ID          = show.ID;
            tv.Data        = show.Data;
            tv.Directory   = show.Directory;
            tv.EpisodeByID = new Dictionary <int, Episode>();

            if (tv.Title != show.Title)
            {
                tv.Title = show.Title;
            }

            foreach (var ep in tv.Episodes)
            {
                ep.Show = tv;
                ep.ID   = ep.Number + (ep.Season * 1000) + (tv.ID * 1000 * 1000);

                tv.EpisodeByID[ep.Number + (ep.Season * 1000)] = ep;

                Episode op;
                if (show.EpisodeByID.TryGetValue(ep.Number + (ep.Season * 1000), out op) && op.Watched)
                {
                    ep.Watched = true;
                }

                if (!string.IsNullOrWhiteSpace(tv.AirTime) && ep.Airdate != Utils.UnixEpoch)
                {
                    ep.Airdate = DateTime.Parse(ep.Airdate.ToString("yyyy-MM-dd ") + tv.AirTime).ToLocalTimeZone(tv.TimeZone);
                }
            }

            try
            {
                tv.Save();
            }
            catch (Exception ex)
            {
                Log.Error("Error while saving updated database for " + show.Title + ".", ex);

                if (callback != null)
                {
                    callback(-1, "Could not save database for " + show.Title + ".");
                }

                return(null);
            }

            TVShows[tv.ID] = tv;
            DataChange     = DateTime.Now;

            if (callback != null)
            {
                callback(1, "Updated " + show.Title + ".");
            }

            Log.Debug("Successfully updated " + show.Title + " in " + (DateTime.Now - st).TotalSeconds + "s.");

            return(tv);
        }