Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / WPF

Wrap Panel Virtualization

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
2 Jan 2012CPOL2 min read 53.8K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;
using MediaAssistant.DAL.Helper;
using MediaAssistant.Data;
using MefBasic.Helper;
using Newtonsoft.Json.Linq;

namespace MediaAssistant.Management
{
    [Export]
    public class IMDBMovieScanner : BackgroundScanner
    {
        public IMDBMovieScanner()
        {
            Name = "IMDB Movie Scanner";
        }
        protected override void TryToScan()
        {
            var moviesToScan = DatabaseManager.Instance.GetProcessingMovies();
            if (moviesToScan != null && moviesToScan.Count() > 0)
            {
                foreach (var movie in moviesToScan.OrderByDescending(m=>m.FullPath))
                {
                    if (Job.Store.StopRequested)
                    {
                        return;
                    }
                    Thread.Sleep(1000);
                    PauseIfRequested();
                    JObject jsonObject;
                    ProcessMovie(movie, out jsonObject);
                }
                DatabaseManager.Instance.UpdateProcessingMovies();
            }
        }

        public void ProcessMovie(Movie movie, out JObject jsonObject)
        {
            DatabaseManager.Instance.MarkAsProcessing(movie);
            jsonObject = null;
            if (!string.IsNullOrWhiteSpace(movie.Title) && UpdateMovieFromIMDB(movie, GetImdbApiUrl(movie.Title), out jsonObject)) return;
            if (!File.Exists(movie.FullPath))
            {
                if (string.IsNullOrEmpty(movie.IMDBId)==false)
                {
                    UpdateMovieFromIMDB(movie, GetImdbApiUrlFromId(movie.IMDBId), out jsonObject);
                }
                return;
            }
            string firstCandidate;
            string secondCandidate;
            GetCandidateTitle(movie.FullPath, out firstCandidate, out secondCandidate);
            if (!UpdateMovieFromIMDB(movie, GetImdbApiUrl(firstCandidate), out jsonObject))
            {
                UpdateMovieFromIMDB(movie, GetImdbApiUrl(secondCandidate), out jsonObject);
            }
        }
        public static void GetCandidateTitle(string fullPath, out string firstCandidate, out string secondCandidate)
        {
            var fileInfo = new FileInfo(fullPath);
            if (Utility.GetSupportedMovieCount(fileInfo.Directory.FullName) == 1)
            {
                firstCandidate = CorrectTitle(fileInfo.Directory.Name);
                secondCandidate = CorrectTitle(Path.GetFileNameWithoutExtension(fullPath));
            }
            else
            {
                firstCandidate = CorrectTitle(Path.GetFileNameWithoutExtension(fullPath));
                secondCandidate = CorrectTitle(fileInfo.Directory.Name);
            }
        }
        private static string CorrectTitle(string title)
        {
            var token = title.Trim().Split("`~!^*()[]{};<>+-".ToCharArray());
            return token.First().Trim().Replace(".", " ");
        }
        public bool UpdateMovieFromIMDB(Movie movie, string imdbApiUrl, out JObject jsonObject)
        {
            try
            {
                var data = WebRequestHelper.GetContent(imdbApiUrl);

                jsonObject = JObject.Parse(data);

                var error = (string)jsonObject.SelectToken("Response");

                if (error == "Parse Error")
                {
                    DatabaseManager.Instance.MarkAsFailed(movie);
                    return false;
                }
                if (movie.Status == MovieStatus.Creating)
                {
                    movie.UpdateMovie(jsonObject);
                }
                else
                {
                    SynchronizationContextHelper.Instance.SendInBackground(UpdateMovie,new object[]{movie,jsonObject});
                }

                return true;
            }
            catch (Exception)
            {
                DatabaseManager.Instance.MarkAsFailed(movie);
                jsonObject = null;
                return false;
            }

        }

        private void UpdateMovie(object state)
        {
            var objArray = (object[]) state;
            DataSource.Value.UpdateMovie((Movie) objArray[0], (JObject) objArray[1]);
            DatabaseManager.Instance.UpdateProcessingTitle(DataSource.Value.ShowDrivesMoviesOnly);
            DatabaseManager.Instance.UpdateFailedTitle(DataSource.Value.ShowDrivesMoviesOnly);
        }

        private static string GetImdbApiUrl(string title)
        {
            return string.Format("http://www.imdbapi.com/?i=&t={0}", title);
        }
        private static string GetImdbApiUrlFromId(string imdbId)
        {
            return string.Format("http://www.imdbapi.com/?i={0}&t=", imdbId);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions