commit 96a4bebe9ec38cd3d5ce73133f34a974e6048b6f
Author: vrishab <vrishab.in@gmail.com>
Date:   Sat Oct 22 06:35:00 2016 +0530

    Name new playlists based on the selected tracks

diff --git a/shell/rb-playlist-manager.c b/shell/rb-playlist-manager.c
index c4809a6..2ba9e10 100644
--- a/shell/rb-playlist-manager.c
+++ b/shell/rb-playlist-manager.c
@@ -1265,6 +1265,80 @@ rb_playlist_manager_save_playlist_file (RBPlaylistManager *mgr, RBSource *source
 	g_object_unref (builder);
 }
 
+static gboolean
+is_property_value_valid (const gchar *value)
+{
+	gchar *tmp;
+	gchar *prop_value;
+	gboolean valid = TRUE;
+
+	tmp = g_strdup (value);
+	prop_value = (gchar *) g_strstrip (tmp);
+
+	if (strlen (prop_value) == 0 || g_strcmp0 (prop_value, _("Unknown")) == 0) {
+		valid = FALSE;
+	}
+
+	g_free (tmp);
+
+	return valid;
+}
+
+/*
+ * Returns a playlist name based on the entries selected in 'selected_source',
+ * if possible, else returns a default name.
+ */
+static gchar *
+get_new_static_playlist_name (RBSource *selected_source)
+{
+	int i;
+	GList *selected_prop_values;
+
+	RhythmDBPropType props[] = {
+		RHYTHMDB_PROP_ALBUM,
+		RHYTHMDB_PROP_ARTIST,
+		RHYTHMDB_PROP_COMPOSER,
+		RHYTHMDB_PROP_GENRE,
+	};
+
+	/* Properties will be processed in the declared order.  */
+	for (i = 0; i < G_N_ELEMENTS (props); i++) {
+		selected_prop_values = rb_source_gather_selected_properties (selected_source, props[i]);
+
+		/* If all selected entries have the same property value, use it as playlist name. */
+		if (g_list_length (selected_prop_values) == 1) {
+			gchar *prop_value;
+
+			prop_value = (gchar *) g_list_first (selected_prop_values)->data;
+			if (is_property_value_valid (prop_value) == FALSE)
+				continue;
+
+			return g_strdup (prop_value);
+
+		} else if (g_list_length (selected_prop_values) == 2) {
+			/* If all selected entries have only 2 property values, combine them and use
+			   it as playlist name. */
+			gchar *prop_first_value;
+			gchar *prop_second_value;
+
+			/* First value of property */
+			prop_first_value = (gchar *) (g_list_first (selected_prop_values)->data);
+			if (is_property_value_valid (prop_first_value) == FALSE)
+				continue;
+
+			/* Second value of property */
+			prop_second_value = (gchar *) (g_list_last (selected_prop_values)->data);
+			if (is_property_value_valid (prop_second_value) == FALSE)
+				continue;
+
+			/* Combine values ( eg. 'Album1 / Album2' or 'Artist1 / Artist2' etc ) */
+			return g_strconcat (prop_first_value, " / ", prop_second_value, NULL);
+		}
+	}
+
+	return g_strdup(_("New Playlist"));
+}
+
 static void
 save_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer data)
 {
@@ -1278,11 +1352,13 @@ add_to_new_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpoin
 	RBPlaylistManager *mgr = RB_PLAYLIST_MANAGER (data);
 	GList *entries;
 	RBSource *playlist_source;
+	char *playlist_name;
 
 	rb_debug ("add to new playlist");
 
+	playlist_name = get_new_static_playlist_name (mgr->priv->selected_source);
 	entries = rb_source_copy (mgr->priv->selected_source);
-	playlist_source = rb_playlist_manager_new_playlist (mgr, NULL, FALSE);
+	playlist_source = rb_playlist_manager_new_playlist (mgr, playlist_name, FALSE);
 	rb_source_paste (playlist_source, entries);
 
 	g_list_foreach (entries, (GFunc)rhythmdb_entry_unref, NULL);
