From 08d22923bf05aae53616137e612e8f1e7dd26b81 Mon Sep 17 00:00:00 2001 From: Dreamcooled Date: Wed, 10 Sep 2014 22:39:19 +0200 Subject: [PATCH] Finalized Favicon Stuff. Fixed Reset of NewEpisode Message. v0.15.6 --- SjUpdater/MainWindow.xaml | 16 +-- SjUpdater/MainWindow.xaml.cs | 2 +- SjUpdater/Properties/AssemblyInfo.cs | 4 +- SjUpdater/SjUpdater.csproj | 1 + SjUpdater/Utils/FavIcon.cs | 133 +++++++++++++++----- SjUpdater/Utils/StringToFaviconConverter.cs | 6 +- SjUpdater/ViewModel/ShowViewModel.cs | 10 +- 7 files changed, 122 insertions(+), 50 deletions(-) diff --git a/SjUpdater/MainWindow.xaml b/SjUpdater/MainWindow.xaml index abcfa28..7e6d317 100644 --- a/SjUpdater/MainWindow.xaml +++ b/SjUpdater/MainWindow.xaml @@ -497,14 +497,16 @@ - diff --git a/SjUpdater/MainWindow.xaml.cs b/SjUpdater/MainWindow.xaml.cs index a15c187..4969b8b 100644 --- a/SjUpdater/MainWindow.xaml.cs +++ b/SjUpdater/MainWindow.xaml.cs @@ -288,12 +288,12 @@ private void on_ShowViewClicked(object sender, ShowViewModel showView) { if (!IsVisible) Show(); - showView.Show.NewEpisodes = false; OnShowViewClicked(showView); } private void OnShowViewClicked(ShowViewModel showView) { + showView.Show.NewEpisodes = false; ShowGrid.DataContext = showView; FilterFlyout.DataContext = showView; SwitchPage(1); diff --git a/SjUpdater/Properties/AssemblyInfo.cs b/SjUpdater/Properties/AssemblyInfo.cs index dea27ff..bdeb3d6 100644 --- a/SjUpdater/Properties/AssemblyInfo.cs +++ b/SjUpdater/Properties/AssemblyInfo.cs @@ -49,6 +49,6 @@ // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.15.5.0")] -[assembly: AssemblyFileVersion("0.15.5.0")] +[assembly: AssemblyVersion("0.15.6.0")] +[assembly: AssemblyFileVersion("0.15.6.0")] [assembly: GuidAttribute("7CE08AB1-B0EE-4D6F-9AB0-28C2F23CB155")] diff --git a/SjUpdater/SjUpdater.csproj b/SjUpdater/SjUpdater.csproj index 8768b15..5fdec98 100644 --- a/SjUpdater/SjUpdater.csproj +++ b/SjUpdater/SjUpdater.csproj @@ -88,6 +88,7 @@ .\SmartThreadPool.dll + diff --git a/SjUpdater/Utils/FavIcon.cs b/SjUpdater/Utils/FavIcon.cs index fd02fb5..2fe5aa3 100644 --- a/SjUpdater/Utils/FavIcon.cs +++ b/SjUpdater/Utils/FavIcon.cs @@ -1,22 +1,30 @@ using System; using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Xml.Serialization; +using Brushes = System.Windows.Media.Brushes; namespace SjUpdater.Utils { - public class FavIcon + public class FavIcon : PropertyChangedImpl { private static Dictionary _dictCache = new Dictionary(); private readonly static string cachePath = Path.Combine(Path.GetTempPath(), "sjupdater", "faviconcache"); + private ImageSource _image; + private string _name; + private static object lockobj = new object(); static FavIcon() { @@ -33,24 +41,90 @@ static FavIcon() } - public static ImageSource Get(String value) + public FavIcon(String name) { - try + Name = name; + } + + public String Name + { + get { return _name; } + set { + if (_name == value) return; + _name = value; + var b = GetFromCache(value); - if (b == null) b = GetFromUrl(value); - //todo: draw two letters - return b; + if (b == null) b = GetFromLetters(value); + Image = b; + + StaticInstance.ThreadPool.QueueWorkItem(() => + { + lock (lockobj) + { + var x= Get(value); + if (x != null) + { + Image = x; + } + } + }); + OnPropertyChanged(); + } + } + + public ImageSource Image + { + get { return _image; } + set + { + if (_image == value) return; + _image = value; + OnPropertyChanged(); + } + } + + + private static BitmapImage Get(String value) + { + try + { + return GetFromCache(value) ?? GetFromUrl(value); } - catch (Exception) + catch (Exception ex) { return null; } + } + + private static BitmapImage GetFromLetters(String value) + { + + + Bitmap bmp = new Bitmap(48,48); + RectangleF rectf = new RectangleF(0,0,48,48); + Graphics g = Graphics.FromImage(bmp); + //g.Clear(System.Drawing.Color.Gray); + + g.SmoothingMode = SmoothingMode.AntiAlias; + g.InterpolationMode = InterpolationMode.HighQualityBicubic; + g.PixelOffsetMode = PixelOffsetMode.HighQuality; + StringFormat format = new StringFormat(); + format.LineAlignment = StringAlignment.Center; + format.Alignment = StringAlignment.Center; + g.DrawString(""+value.ToUpper().First(), new Font("Tahoma", 40,FontStyle.Bold,GraphicsUnit.Pixel), System.Drawing.Brushes.Blue, rectf, format); + + g.Flush(); + MemoryStream ms = new MemoryStream(); + bmp.Save(ms,ImageFormat.Png); + ms.Position = 0; + return CachedBitmap.BitmapImageFromStream(ms); } + @@ -89,6 +163,7 @@ static String FindUrl(string value) HttpWebRequest req = HttpWebRequest.CreateHttp(value); req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)"; req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + req.AllowAutoRedirect = true; var res = req.GetResponse(); StreamReader reader = new StreamReader(res.GetResponseStream()); String html = reader.ReadToEnd(); @@ -117,34 +192,28 @@ private static BitmapImage GetFromUrl(string value) { String url = FindUrl(value); if (url == null) return null; - String[] url_parts = new Uri(url).DnsSafeHost.Split(new char[] {'.'}); + String[] url_parts = new Uri(url).DnsSafeHost.Split(new char[] { '.' }); String key = url_parts[url_parts.Length - 2]; - try - { - MemoryStream ms = new MemoryStream(); - HttpWebRequest request = WebRequest.CreateHttp(url); - - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - Stream responseStream = response.GetResponseStream(); - responseStream.CopyTo(ms); - responseStream.Close(); - response.Close(); - - ms.Position = 0; - FileStream f = new FileStream(Path.Combine(cachePath,key+url.Substring(url.LastIndexOf('.'))),FileMode.Create,FileAccess.Write); - ms.CopyTo(f); + + MemoryStream ms = new MemoryStream(); + HttpWebRequest request = WebRequest.CreateHttp(url); + + HttpWebResponse response = request.GetResponse() as HttpWebResponse; + Stream responseStream = response.GetResponseStream(); + responseStream.CopyTo(ms); + responseStream.Close(); + response.Close(); + + ms.Position = 0; + FileStream f = new FileStream(Path.Combine(cachePath,key+url.Substring(url.LastIndexOf('.'))),FileMode.Create,FileAccess.Write); + ms.CopyTo(f); - f.Close(); - ms.Position = 0; - var bmap = CachedBitmap.BitmapImageFromStream(ms); - _dictCache.Add(key, bmap); - return bmap; + f.Close(); + ms.Position = 0; + var bmap = CachedBitmap.BitmapImageFromStream(ms); + _dictCache.Add(key, bmap); + return bmap; - } - catch (Exception e) - { - return null; - } } } } diff --git a/SjUpdater/Utils/StringToFaviconConverter.cs b/SjUpdater/Utils/StringToFaviconConverter.cs index 3348ddc..6e2ea68 100644 --- a/SjUpdater/Utils/StringToFaviconConverter.cs +++ b/SjUpdater/Utils/StringToFaviconConverter.cs @@ -14,11 +14,11 @@ public class StringToFaviconConverter : IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - if (targetType != typeof(ImageSource) || value.GetType() != typeof(string)) + if (targetType != typeof(FavIcon) || value.GetType() != typeof(string)) { - throw new ArgumentException(); + // throw new ArgumentException(); } - return FavIcon.Get(value as String); + return new FavIcon(value as String); } public object ConvertBack(object value, Type targetType, diff --git a/SjUpdater/ViewModel/ShowViewModel.cs b/SjUpdater/ViewModel/ShowViewModel.cs index aa462af..bb5c75e 100644 --- a/SjUpdater/ViewModel/ShowViewModel.cs +++ b/SjUpdater/ViewModel/ShowViewModel.cs @@ -15,7 +15,7 @@ public class ShowViewModel : PropertyChangedImpl private readonly FavShowData _show; private readonly ObservableCollection _lisSeasons; private readonly Dispatcher _dispatcher; - private Visibility backgroundImageVisibility; + private Visibility _backgroundImageVisibility; private static readonly Comparer SeasonComparer = Comparer.Create( delegate(SeasonPanoramaViewModel m1, SeasonPanoramaViewModel m2) @@ -50,7 +50,7 @@ public ShowViewModel(FavShowData show) } _lisSeasons.Sort(SeasonComparer); - backgroundImageVisibility = Settings.Instance.EnableImages ? Visibility.Visible : Visibility.Hidden; + _backgroundImageVisibility = Settings.Instance.EnableImages ? Visibility.Visible : Visibility.Hidden; } private void update_source(object sender, NotifyCollectionChangedEventArgs e) @@ -204,13 +204,13 @@ public Visibility BackgroundImageVisibility { get { - return backgroundImageVisibility; + return _backgroundImageVisibility; } set { - if (backgroundImageVisibility != value) + if (_backgroundImageVisibility != value) { - backgroundImageVisibility = value; + _backgroundImageVisibility = value; } } }