From 520b200d5420430d0f30d0f155766364d3ad3ff2 Mon Sep 17 00:00:00 2001
From: daniel g. siegel <dgsiegel@gnome.org>
Date: Mon, 12 Oct 2009 18:53:57 +0200
Subject: [PATCH] add random mode to muine

add a random button to both album and song windows, which
either queues a random album when pressing the button or
just selects an album, depending on whether the
/schemas/apps/muine/queue_random key is set or not.
---
 data/glade/AddWindow.glade |   69 ++++++++++++++++++++++++++++++++++++++++++++
 data/muine.schemas.in      |   11 +++++++
 src/AddWindow.cs           |   25 ++++++++++++++-
 src/HandleView.cs          |   23 ++++++++++++++-
 4 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/data/glade/AddWindow.glade b/data/glade/AddWindow.glade
index 4b3ef0e..df81eb9 100644
--- a/data/glade/AddWindow.glade
+++ b/data/glade/AddWindow.glade
@@ -46,6 +46,75 @@
 	  </child>
 
 	  <child>
+	    <widget class="GtkButton" id="queuerandom_button">
+	      <property name="visible">True</property>
+	      <property name="can_default">True</property>
+	      <property name="can_focus">True</property>
+	      <property name="relief">GTK_RELIEF_NORMAL</property>
+	      <property name="focus_on_click">True</property>
+	      <property name="response_id">3</property>
+
+	      <child>
+		<widget class="GtkAlignment" id="alignment26">
+		  <property name="visible">True</property>
+		  <property name="xalign">0.5</property>
+		  <property name="yalign">0.5</property>
+		  <property name="xscale">0</property>
+		  <property name="yscale">0</property>
+		  <property name="top_padding">0</property>
+		  <property name="bottom_padding">0</property>
+		  <property name="left_padding">0</property>
+		  <property name="right_padding">0</property>
+
+		  <child>
+		    <widget class="GtkHBox" id="hbox52">
+		      <property name="visible">True</property>
+		      <property name="homogeneous">False</property>
+		      <property name="spacing">2</property>
+
+		      <child>
+			<widget class="GtkImage" id="queuerandom_button_image">
+			  <property name="visible">True</property>
+			  <property name="xalign">0.5</property>
+			  <property name="yalign">0.5</property>
+			  <property name="xpad">0</property>
+			  <property name="ypad">0</property>
+			</widget>
+			<packing>
+			  <property name="padding">0</property>
+			  <property name="expand">False</property>
+			  <property name="fill">False</property>
+			</packing>
+		      </child>
+
+		      <child>
+			<widget class="GtkLabel" id="label61">
+			  <property name="visible">True</property>
+			  <property name="label" translatable="yes">_Random</property>
+			  <property name="use_underline">True</property>
+			  <property name="use_markup">False</property>
+			  <property name="justify">GTK_JUSTIFY_LEFT</property>
+			  <property name="wrap">False</property>
+			  <property name="selectable">False</property>
+			  <property name="xalign">0.5</property>
+			  <property name="yalign">0.5</property>
+			  <property name="xpad">0</property>
+			  <property name="ypad">0</property>
+			</widget>
+			<packing>
+			  <property name="padding">0</property>
+			  <property name="expand">False</property>
+			  <property name="fill">False</property>
+			</packing>
+		      </child>
+		    </widget>
+		  </child>
+		</widget>
+	      </child>
+	    </widget>
+	  </child>
+
+	  <child>
 	    <widget class="GtkButton" id="queue_button">
 	      <property name="visible">True</property>
 	      <property name="sensitive">False</property>
diff --git a/data/muine.schemas.in b/data/muine.schemas.in
index 9b0c016..c6ba0a9 100644
--- a/data/muine.schemas.in
+++ b/data/muine.schemas.in
@@ -189,5 +189,16 @@
 	<long>Whether or not the tray icon shows song change notifications</long>
       </locale>
     </schema>
+    <schema>
+      <key>/schemas/apps/muine/queue_random</key>
+      <applyto>/apps/muine/queue_random</applyto>
+      <owner>muine</owner>
+      <type>bool</type>
+      <default>1</default>
+      <locale name="C">
+        <short>Queue Albums when pressing the Random button</short>
+	<long>Whether or not queue the album when pressing the Random button</long>
+      </locale>
+    </schema>
   </schemalist>
 </gconfschemafile>
diff --git a/src/AddWindow.cs b/src/AddWindow.cs
index 408c2da..467c986 100644
--- a/src/AddWindow.cs
+++ b/src/AddWindow.cs
@@ -31,7 +31,8 @@ namespace Muine
 			Close       = Gtk.ResponseType.Close,
 			DeleteEvent = Gtk.ResponseType.DeleteEvent,
 			Play        = 1,
-			Queue       = 2
+			Queue       = 2,
+			QueueRandom = 3
 		};
 #endregion Enums.ResponseType
 #endregion Enums
@@ -52,6 +53,8 @@ namespace Muine
 
 #region Constants
 		private const uint search_timeout = 100;
+				private const string GConfKeyRandom = "/apps/muine/queue_random";
+				private const bool GConfDefaultRandom = true;
 #endregion Constants
 
 
@@ -63,6 +66,8 @@ namespace Muine
 		[Glade.Widget] private Gtk.Image          play_button_image;
 		[Glade.Widget] private Gtk.Button         queue_button;
 		[Glade.Widget] private Gtk.Image          queue_button_image;
+		[Glade.Widget] private Gtk.Button         queuerandom_button;
+		[Glade.Widget] private Gtk.Image          queuerandom_button_image;
 		[Glade.Widget] private Gtk.ScrolledWindow scrolledwindow;
 		
 		private AddWindowEntry entry = new AddWindowEntry ();
@@ -103,7 +108,10 @@ namespace Muine
 			  ("stock_media-play", Gtk.IconSize.Button);
 
 			this.queue_button_image.SetFromStock
-			  ("stock_timer", Gtk.IconSize.Button);
+			  ("gtk-add", Gtk.IconSize.Button);
+
+			this.queuerandom_button_image.SetFromStock
+			  ("stock_shuffle", Gtk.IconSize.Button);
 
 			// Cell Renderer
 			this.text_renderer.Ellipsize = Pango.EllipsizeMode.End;
@@ -510,6 +518,19 @@ namespace Muine
 				list.SelectNext ();
 
 				break;
+			case (int) ResponseType.QueueRandom:
+				list.SelectRandom ();
+
+				bool queue  = (bool) Config.Get (GConfKeyRandom, GConfDefaultRandom);
+
+				if (queue)
+					if (QueueEvent != null)
+						QueueEvent (list.Selected);
+				
+				entry.GrabFocus ();
+				
+				break;
+
 				
 			default:
 				throw new ArgumentException ();
diff --git a/src/HandleView.cs b/src/HandleView.cs
index ca7fdb2..60ff6ff 100644
--- a/src/HandleView.cs
+++ b/src/HandleView.cs
@@ -29,6 +29,7 @@ namespace Muine
 	{
 		// Variables
 		private HandleModel model;
+		private Random random;
 
 		// Constructor
 		public HandleView ()
@@ -39,7 +40,8 @@ namespace Muine
 			RulesHint       = true;
 			EnableSearch    = false;
 			HeadersVisible  = false;
-                        FixedHeightMode = true;
+			FixedHeightMode = true;
+			this.random = new Random();
 		}
 
 		// Properties
@@ -117,6 +119,25 @@ namespace Muine
 			SetCursor (path, Columns [0], false);
 		}
 
+		
+		// Methods :: Public :: SelectRandom
+		public void SelectRandom ()
+		{
+			int selected = random.Next(Model.Length);
+			
+			TreeIter iter;
+			if (!Model.IterNthChild (out iter, selected))
+			{
+				SelectFirst ();
+			}
+			else
+			{
+				SetCursor (Model.GetPath(iter), Columns [0], true);
+			}
+			ScrollToCell (Model.GetPath(iter), Columns [0], true, 0.5f, 0.5f);
+		}
+
+
 		// Methods :: Public :: ForwardKeyPress
 		// 	Hack to forward key press events to the treeview
 		public bool ForwardKeyPress (Widget orig_widget, Gdk.EventKey e)
-- 
1.6.5


