// Simple Lyric plugin // (C) 2005 Fernando Herrera // This file is Under GPL 2.0 // Compile with "mcs -target:library -out:Lyrics.dll Lyrics.cs -pkg:muine-plugin" using Muine.PluginLib; using System; using System.IO; using System.Net; using System.Xml; using Gtk; public class Lyrics : Plugin { private IPlayer player; private Window lyrics; TextView view; private class AsyncUpdate { public void UpdateLyrics (string[] artists, string title, TextView view) { bool found = false; TextBuffer buffer = view.Buffer; buffer.Clear(); if (artists.Length > 0 && artists[0] != null && title != null) { buffer.InsertAtCursor ("\nSearching for lyrics...\n"); buffer.ApplyTag ("title", buffer.StartIter, buffer.EndIter); WebClient client = new WebClient(); Stream data = client.OpenRead ("http://lyrc.com.ar/xsearch.php?songname="+title+"&artist="+artists[0]+"&act=1"); StreamReader reader = new StreamReader (data); string s = reader.ReadToEnd (); XmlTextReader xmlreader = new XmlTextReader (s, XmlNodeType.Element, null); while (xmlreader.Read ()) { if (xmlreader.LocalName=="lyric") { xmlreader.Read (); buffer.Clear(); buffer.InsertAtCursor (artists[0] + ": " + title + "\n\n"); buffer.ApplyTag ("title", buffer.StartIter, buffer.EndIter); buffer.InsertAtCursor (xmlreader.Value); buffer.InsertAtCursor ("\n\nLyrics provided by lyrc.com.ar"); buffer.ApplyTag ("copy", buffer.GetIterAtLine (buffer.LineCount - 1), buffer.EndIter); found = true; break; } } xmlreader.Close(); } if (found == false) { buffer.Clear(); buffer.InsertAtCursor ("\nLyrics not found!\n"); buffer.ApplyTag ("title", buffer.StartIter, buffer.EndIter); } } } private delegate void AsyncDelegate(string[] artists, string title, TextView view); public override void Initialize(IPlayer player) { this.player = player; ActionEntry [] action_entries = new Gtk.ActionEntry [] { new ActionEntry ("ShowLyrics", Gtk.Stock.Italic, "Show _Lyrics ", "L", null, new EventHandler (OnShowLyrics)) }; ActionGroup action_group = new ActionGroup ("LyricsPluginActions"); action_group.Add (action_entries); player.UIManager.InsertActionGroup (action_group, -1); player.UIManager.AddUi (this.player.UIManager.NewMergeId (), "/MenuBar/FileMenu/ExtraFileActions", "ShowLyricsMenuItem", "ShowLyrics", UIManagerItemType.Menuitem, false); player.SongChangedEvent += new SongChangedEventHandler (OnSongChangedEvent); view = new TextView (); TextBuffer buffer = view.Buffer; TextTag titleTag = new TextTag ("title"); titleTag.Weight = Pango.Weight.Bold; buffer.TagTable.Add (titleTag); TextTag copyTag = new TextTag ("copy"); copyTag.Weight = Pango.Weight.Light; copyTag.Style = Pango.Style.Italic; buffer.TagTable.Add (copyTag); lyrics = null; } private void OnHideLyrics (object sender, Gtk.DeleteEventArgs args) { lyrics.Hide (); args.RetVal = true; } private void OnShowLyrics (object sender, EventArgs e) { AsyncUpdate au = new AsyncUpdate (); AsyncDelegate dlgt = new AsyncDelegate(au.UpdateLyrics); if (lyrics != null) { lyrics.ShowAll (); lyrics.Present (); return; } lyrics = new Window ("Lyrics"); lyrics.SetDefaultSize (600,400); ScrolledWindow scroll = new ScrolledWindow (); scroll.VscrollbarPolicy = PolicyType.Automatic; scroll.HscrollbarPolicy = PolicyType.Automatic; scroll.Add (view); lyrics.Add (scroll); lyrics.DeleteEvent += OnHideLyrics; dlgt.BeginInvoke(player.PlayingSong.Artists, player.PlayingSong.Title, view, null, null); lyrics.ShowAll (); } private void OnSongChangedEvent (ISong song) { if (song == null) return; if (lyrics != null) { AsyncUpdate au = new AsyncUpdate (); AsyncDelegate dlgt = new AsyncDelegate(au.UpdateLyrics); dlgt.BeginInvoke(song.Artists, song.Title, view, null, null); } } }