Skip to content

Commit

Permalink
Finalized Favicon Stuff. Fixed Reset of NewEpisode Message. v0.15.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamcooled committed Sep 10, 2014
1 parent 024eb2a commit 08d2292
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 50 deletions.
16 changes: 9 additions & 7 deletions SjUpdater/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,16 @@
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="4"
Command="{Binding DownloadCommand, ElementName=Window}"
<Button Margin="4" Padding="4,0" Background="{DynamicResource LabelTextBrush}"
Command="{Binding DownloadCommand, ElementName=Window}" ToolTip="{Binding Key}"
CommandParameter="{Binding Value}" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}" />
<Image Source="{Binding Key,Converter={StaticResource StringToFaviconConverter}}"></Image>
</StackPanel>

<ContentPresenter Content="{Binding Key,Converter={StaticResource StringToFaviconConverter}}" >
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Image Source="{Binding Image}" Width="16" Height="16"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand Down
2 changes: 1 addition & 1 deletion SjUpdater/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions SjUpdater/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
1 change: 1 addition & 0 deletions SjUpdater/SjUpdater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<HintPath>.\SmartThreadPool.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml" />
Expand Down
133 changes: 101 additions & 32 deletions SjUpdater/Utils/FavIcon.cs
Original file line number Diff line number Diff line change
@@ -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<String, BitmapImage> _dictCache = new Dictionary<string, BitmapImage>();
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()
{

Expand All @@ -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);
}






Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
}
}
6 changes: 3 additions & 3 deletions SjUpdater/Utils/StringToFaviconConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions SjUpdater/ViewModel/ShowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ShowViewModel : PropertyChangedImpl
private readonly FavShowData _show;
private readonly ObservableCollection<SeasonPanoramaViewModel> _lisSeasons;
private readonly Dispatcher _dispatcher;
private Visibility backgroundImageVisibility;
private Visibility _backgroundImageVisibility;

private static readonly Comparer<SeasonPanoramaViewModel> SeasonComparer =
Comparer<SeasonPanoramaViewModel>.Create( delegate(SeasonPanoramaViewModel m1, SeasonPanoramaViewModel m2)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -204,13 +204,13 @@ public Visibility BackgroundImageVisibility
{
get
{
return backgroundImageVisibility;
return _backgroundImageVisibility;
}
set
{
if (backgroundImageVisibility != value)
if (_backgroundImageVisibility != value)
{
backgroundImageVisibility = value;
_backgroundImageVisibility = value;
}
}
}
Expand Down

0 comments on commit 08d2292

Please sign in to comment.