Skip to content

Commit

Permalink
Merge pull request #15 from SamPlusPlus/AlbumArt
Browse files Browse the repository at this point in the history
Album art
  • Loading branch information
xolarity committed Dec 29, 2012
2 parents 40bcf0d + 2fb6bff commit f3a7e0e
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 32 deletions.
124 changes: 92 additions & 32 deletions FrmMain.cs
Expand Up @@ -2,13 +2,15 @@
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using mshtml;
using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Controls;
using Microsoft.WindowsAPICodePack.Taskbar;
using WinGrooves.Utils;

namespace WinGrooves
{
Expand Down Expand Up @@ -74,6 +76,9 @@ public class FrmMain : System.Windows.Forms.Form
private bool isbuttonPaused, isMusicPlaying;
private ThumbnailToolbarButton buttonPause;
private ThumbnailToolbarButton buttonNext;
private TabbedThumbnail _customThumbnail; //Taskbar image icon

private string _cachedSongTitle; //Used to tell if song has changed

public FrmMain()
{
Expand Down Expand Up @@ -477,16 +482,24 @@ private void FrmMain_Load(object sender, System.EventArgs e)
//Thumbnail buttons for win7 users
if (TaskbarManager.IsPlatformSupported)
{

//Add a thumbnail image during peak
_customThumbnail = new TabbedThumbnail(this.Handle, this.Handle);
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(_customThumbnail);


buttonPrev.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Previous_Click);
buttonPause.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Play_Click);
buttonNext.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Next_Click);

//Add the buttons (kinda of ugly tough)
ThumbnailToolbarButton[] buttonList = new ThumbnailToolbarButton[3];
buttonList[0] = buttonPrev; buttonList[1] = buttonPause; buttonList[2] = buttonNext;
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(this.Handle, buttonList);

}
}

/*
* Simulates a browser click on an html element
/// <param name="action">the HTML id of the element to click on/param>
Expand Down Expand Up @@ -723,48 +736,61 @@ private void currentSongTimer_Tick(object sender, EventArgs e)
}

object songTitle = webBrowser1.Document.InvokeScript("getSongTitle");
object songArtist = webBrowser1.Document.InvokeScript("getSongArtist");
//set the Windows title
if (songTitle.ToString().Length > 0)


//Since the song title changed we must update all the information
if (String.IsNullOrEmpty(_cachedSongTitle) || _cachedSongTitle != songTitle.ToString())
{
this.Text = songTitle + " - " + songArtist + " - WinGrooves";
//set the tray icon text if it is less than 63 characters (the max allowed)
if ((songTitle.ToString().Length + songArtist.ToString().Length + 3) < 63)
{
notifyIcon1.Text = songTitle + " - " + songArtist;
}
else
//cache new song title so we can tell if it changes again
_cachedSongTitle = songTitle.ToString();


//Update thumbnail with album art cover
UpdateThumbnail();

//Set the Windows title
object songArtist = webBrowser1.Document.InvokeScript("getSongArtist");

if (songTitle.ToString().Length > 0)
{
try // Get what you can up to max length.
this.Text = songTitle + " - " + songArtist + " - WinGrooves";
//set the tray icon text if it is less than 63 characters (the max allowed)
if ((songTitle.ToString().Length + songArtist.ToString().Length + 3) < 63)
{
notifyIcon1.Text = (songTitle + " - " + songArtist).Substring(0, 62);
notifyIcon1.Text = songTitle + " - " + songArtist;
}
catch // Possible you land right on and under, throwing exception. handle with old fallback.
else
{
notifyIcon1.Text = ("WinGrooves");
try // Get what you can up to max length.
{
notifyIcon1.Text = (songTitle + " - " + songArtist).Substring(0, 62);
}
catch
// Possible you land right on and under, throwing exception. handle with old fallback.
{
notifyIcon1.Text = ("WinGrooves");
}
}
}
}

//control thumbail icons
if (TaskbarManager.IsPlatformSupported)
{
//the element class of the play button on grooveshark changes according to the music state (contains play/paused/nothing)
//I can't figure a better way to control the thumbnail states.
if (Convert.ToBoolean(webBrowser1.Document.InvokeScript("getMusicState")))
{
buttonPause.Icon = Properties.Resources.PlayerPause;
isbuttonPaused = false;
}
else
//control thumbail icons
if (TaskbarManager.IsPlatformSupported)
{
buttonPause.Icon = Properties.Resources.PlayerPlay;
isbuttonPaused = true;
//the element class of the play button on grooveshark changes according to the music state (contains play/paused/nothing)
//I can't figure a better way to control the thumbnail states.
if (Convert.ToBoolean(webBrowser1.Document.InvokeScript("getMusicState")))
{
buttonPause.Icon = Properties.Resources.PlayerPause;
isbuttonPaused = false;
}
else
{
buttonPause.Icon = Properties.Resources.PlayerPlay;
isbuttonPaused = true;
}

}
}


// HandleThumbnailButtons();
}
catch (NullReferenceException)
{
Expand Down Expand Up @@ -829,5 +855,39 @@ private void Dislike_Click(object sender, EventArgs e)
{
DislikeCurrentSong();
}

#region Thumbnail Methods

/// <summary>
/// Updates the thumbnail on the Taskbar in Windows 7
/// </summary>
private void UpdateThumbnail()
{

var url = webBrowser1.Document.GetElementById("now-playing-image").GetAttribute("src");

if (String.IsNullOrEmpty(url) || url.EndsWith("40_album.png"))
{
//there is no album art for this song so lets just redraw the application.
_customThumbnail.SetImage(ImageUtils.ApplicationThumbnail(this));
}
else
{
//Calculate Album Art URL
string urlBase = url.Substring(0, url.LastIndexOf("/") + 1);
string fileName = url.Substring(url.LastIndexOf("/") + 1);

if (fileName.StartsWith("40_"))
{
//Build the url for the 120 pixel album art
fileName = String.Format("120{0}", fileName.Substring(fileName.IndexOf("_")));
url = urlBase + fileName;
}
Bitmap albumPreview = ImageUtils.BitmapFromUrl(url);
_customThumbnail.SetImage(albumPreview ?? ImageUtils.ApplicationThumbnail(this));
}
}
#endregion

}
}
45 changes: 45 additions & 0 deletions Utils/ImageUtils.cs
@@ -0,0 +1,45 @@
using System;
using System.Drawing;
using System.Net;

namespace WinGrooves.Utils
{
public static class ImageUtils
{
/// <summary>
/// Retrieves the image at the url.
/// </summary>
/// <param name="url">url of image.</param>
/// <returns>Returns image as bitmap, or null if not available.</returns>
public static Bitmap BitmapFromUrl(string url)
{
if (String.IsNullOrEmpty(url))
{
return null;
}
var request = WebRequest.Create(url);

using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
if (stream == null) return null;
return (Bitmap)Image.FromStream(stream);
}
}

/// <summary>
/// Returns a bitmap of the current form.
/// </summary>
/// <returns>Returns a bitmap of the current form</returns>
public static Bitmap ApplicationThumbnail(System.Windows.Forms.Form form)
{
Graphics myGraphics = form.CreateGraphics();
Size s = form.Size;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, s);
return memoryImage;
}

}
}
9 changes: 9 additions & 0 deletions WinGrooves.csproj
Expand Up @@ -119,6 +119,13 @@
<Reference Include="Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack">
<HintPath>WindowsAPICodePack\Microsoft.WindowsAPICodePack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAPICodePack.Shell">
<HintPath>WindowsAPICodePack\Microsoft.WindowsAPICodePack.Shell.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System">
<Name>System</Name>
</Reference>
Expand All @@ -134,6 +141,7 @@
<Reference Include="System.XML">
<Name>System.XML</Name>
</Reference>
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AboutBox1.cs">
Expand Down Expand Up @@ -165,6 +173,7 @@
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="Settings.cs" />
<Compile Include="Utils\ImageUtils.cs" />
<EmbeddedResource Include="AboutBox1.resx">
<DependentUpon>AboutBox1.cs</DependentUpon>
</EmbeddedResource>
Expand Down

0 comments on commit f3a7e0e

Please sign in to comment.