Ejemplo n.º 1
0
        private void bgwUpdate_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(100);
            DatabaseManager.DatabaseManager databaseManager = new DatabaseManager.DatabaseManager();
            string localAppFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Personal TV Organiser";

            databaseManager.DataSource = localAppFolder + "\\db.sqlite";
            databaseManager.InitializeConnection();
            Update         update           = new Update();
            List <Series>  seriesToUpdate   = new List <Series>();
            List <Episode> episodesToUpdate = new List <Episode>();

            try
            {
                databaseManager.OpenConnection();
                Dictionary <string, string> settings = databaseManager.GetSettings("previousServerTime");
                databaseManager.CloseConnection();
                Update updates         = _tvdbAPI.GetUpdates(settings["previousServerTime"]);
                int    totalUpdates    = _favourites.Count + updates.EpisodeUpdates.Count;
                int    percentProgress = 0;
                double progress        = 0;
                bgwUpdate.ReportProgress(percentProgress);
                foreach (int seriesID in _favourites.Keys)
                {
                    if (updates.SeriesUpdates.Contains(seriesID))
                    {
                        Series seriesUpdates = _tvdbAPI.GetSeriesUpdates(seriesID);
                        if (seriesUpdates != null)
                        {
                            seriesToUpdate.Add(seriesUpdates);
                        }
                    }
                    ++progress;
                    bgwUpdate.ReportProgress((int)Math.Round(progress / totalUpdates * 100.0));
                }
                foreach (int episodeID in updates.EpisodeUpdates)
                {
                    Episode episodeUpdates = _tvdbAPI.GetEpisodeUpdates(episodeID);
                    if (episodeUpdates != null)
                    {
                        if (_favourites.Keys.Contains(episodeUpdates.SeriesID))
                        {
                            episodesToUpdate.Add(episodeUpdates);
                        }
                    }
                    ++progress;
                    bgwUpdate.ReportProgress((int)Math.Round(progress / totalUpdates * 100.0));
                }
                databaseManager.OpenConnection();
                databaseManager.BeginTransaction();
                foreach (Series series in seriesToUpdate)
                {
                    databaseManager.UpdateSeries(series);
                }
                string seriesName = "";
                foreach (Episode episode in episodesToUpdate)
                {
                    seriesName = _favourites[episode.SeriesID].SeriesName;
                    databaseManager.UpdateEpisode(episode, seriesName);
                }
                databaseManager.UpdateSetting("previousServerTime", updates.Time);
                databaseManager.Commit();
                bgwUpdate.ReportProgress(100);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (databaseManager.TheTransaction != null)
                {
                    databaseManager.Rollback();
                }
                databaseManager.CloseConnection();
            }
        }