[Xfce4-commits] [panel-plugins/xfce4-pulseaudio-plugin] 18/21: Tidy up MprisMenuItem

noreply at xfce.org noreply at xfce.org
Sat Sep 23 21:27:44 CEST 2017


This is an automated email from the git hooks/post-receive script.

b   l   u   e   s   a   b   r   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository panel-plugins/xfce4-pulseaudio-plugin.

commit 3e6ae84ac0c57b4c6dd918074e6f1e9b002466bc
Author: Sean Davis <smd.seandavis at gmail.com>
Date:   Thu Sep 14 06:41:19 2017 -0400

    Tidy up MprisMenuItem
---
 panel-plugin/mprismenuitem.c | 872 ++++++++++++++++++++++++-------------------
 panel-plugin/mprismenuitem.h |  22 +-
 2 files changed, 489 insertions(+), 405 deletions(-)

diff --git a/panel-plugin/mprismenuitem.c b/panel-plugin/mprismenuitem.c
index 1f1c7c4..4c4b689 100644
--- a/panel-plugin/mprismenuitem.c
+++ b/panel-plugin/mprismenuitem.c
@@ -38,14 +38,6 @@
 #include <libxfce4util/libxfce4util.h>
 
 
-static gboolean mpris_menu_item_button_press_event      (GtkWidget       *menuitem,
-                                                         GdkEventButton  *event);
-static gboolean mpris_menu_item_button_release_event    (GtkWidget       *menuitem,
-                                                         GdkEventButton  *event);
-static void     update_packing                          (MprisMenuItem   *self);
-static void     mpris_menu_item_finalize                (GObject         *object);
-
-
 
 struct _MprisMenuItemPrivate {
   GtkWidget *title_label;
@@ -75,11 +67,15 @@ struct _MprisMenuItemPrivate {
   GtkWidget *button_box;
 };
 
+
+
 enum {
   MEDIA_NOTIFY,
   LAST_SIGNAL
 };
 
+
+
 static guint signals[LAST_SIGNAL] = { 0 };
 
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
@@ -90,79 +86,182 @@ G_GNUC_END_IGNORE_DEPRECATIONS
 
 
 
-static void
-mpris_menu_item_class_init (MprisMenuItemClass *item_class)
+/* Static Declarations */
+static void         mpris_menu_item_finalize                (GObject        *object);
+static void         mpris_menu_item_raise                   (MprisMenuItem  *item);
+static void         mpris_menu_item_launch                  (MprisMenuItem  *item);
+static void         mpris_menu_item_raise_or_launch         (MprisMenuItem  *item);
+static GtkWidget *  mpris_menu_item_get_widget_at_event     (MprisMenuItem  *item,
+                                                             GdkEventButton *event);
+static gboolean     mpris_menu_item_button_press_event      (GtkWidget      *item,
+                                                             GdkEventButton *event);
+static gboolean     mpris_menu_item_button_release_event    (GtkWidget      *item,
+                                                             GdkEventButton *event);
+static void         media_play_pause_clicked_event          (GtkButton      *button,
+                                                             gpointer        user_data);
+static void         media_go_previous_clicked_event         (GtkButton      *button,
+                                                             gpointer        user_data);
+static void         media_go_next_clicked_event             (GtkButton      *button,
+                                                             gpointer        user_data);
+static void         menu_item_activate_event                (GtkMenuItem    *item,
+                                                             gpointer        user_data);
+static void         media_notify                            (MprisMenuItem  *item,
+                                                             gchar          *message);
+static GtkWidget *  track_info_label_new                    (void);
+static void         gtk_label_set_markup_printf_escaped     (GtkLabel       *label,
+                                                             const gchar    *format,
+                                                                             ...);
+static void         update_packing                          (MprisMenuItem  *item);
+static gchar *      find_desktop_entry                      (const gchar    *player_name);
+
+
+
+/* Public API */
+GtkWidget*
+mpris_menu_item_new_with_player (const gchar *player,
+                                 const gchar *title,
+                                 const gchar *icon_name,
+                                 const gchar *filename)
 {
-  GObjectClass      *gobject_class =   G_OBJECT_CLASS      (item_class);
-  GtkWidgetClass    *widget_class =    GTK_WIDGET_CLASS    (item_class);
+  MprisMenuItem        *menu_item;
+  MprisMenuItemPrivate *priv;
 
-  gobject_class->finalize = mpris_menu_item_finalize;
-  widget_class->button_press_event   = mpris_menu_item_button_press_event;
-  widget_class->button_release_event = mpris_menu_item_button_release_event;
+  TRACE("entering");
 
-  /**
-   * MprisMenuItem::media-notify:
-   * @menuitem: the #MprisMenuItem for which the value changed
-   * @value: the mpris signal to emit
-   *
-   * Emitted whenever the a media button is clicked.
-   */
-  signals[MEDIA_NOTIFY] = g_signal_new ("media-notify",
-                                        TYPE_MPRIS_MENU_ITEM,
-                                        G_SIGNAL_RUN_LAST,
-                                        0, NULL, NULL,
-                                        g_cclosure_marshal_VOID__STRING,
-                                        G_TYPE_NONE,
-                                        1, G_TYPE_STRING);
+  menu_item = MPRIS_MENU_ITEM (g_object_new (TYPE_MPRIS_MENU_ITEM, NULL));
 
-  g_type_class_add_private (item_class, sizeof (MprisMenuItemPrivate));
+  priv = GET_PRIVATE (menu_item);
+
+  priv->player = g_strdup(player);
+  if (title != NULL)
+    priv->title = g_strdup(title);
+  else
+    priv->title = g_strdup(player);
+  priv->filename = g_strdup(filename);
+
+  update_packing (menu_item);
+
+  gtk_widget_add_events (GTK_WIDGET(menu_item), GDK_SCROLL_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK);
+
+  gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
+
+  return GTK_WIDGET(menu_item);
 }
 
-static void
-gtk_label_set_markup_printf_escaped (GtkLabel    *label,
-                                     const gchar *format,
-                                     ...)
+
+
+GtkWidget*
+mpris_menu_item_new_from_player_name (const gchar *player)
 {
-  va_list args;
-  gchar *str;
+  GtkWidget *widget = NULL;
+  GKeyFile  *key_file;
+  gchar     *file;
+  gchar     *filename;
+  gchar     *full_path;
 
-  va_start (args, format);
-  str = g_markup_vprintf_escaped (format, args);
-  gtk_label_set_markup (label, str);
-  va_end (args);
+  filename = find_desktop_entry (player);
+  if (filename == NULL)
+    {
+      g_free (filename);
+      return NULL;
+    }
 
-  g_free (str);
+  file = g_strconcat("applications/", filename, NULL);
+  g_free (filename);
+
+  key_file = g_key_file_new();
+  if (g_key_file_load_from_data_dirs (key_file, file, &full_path, G_KEY_FILE_NONE, NULL))
+    {
+      gchar *name = g_key_file_get_string (key_file, "Desktop Entry", "Name", NULL);
+      gchar *icon_name = g_key_file_get_string (key_file, "Desktop Entry", "Icon", NULL);
+
+      widget = mpris_menu_item_new_with_player (player, name, icon_name, full_path);
+
+      g_free (name);
+      g_free (icon_name);
+    }
+
+  g_key_file_free (key_file);
+  g_free (file);
+
+  return widget;
 }
 
-static void
-media_notify (MprisMenuItem *item, gchar *message)
+
+
+const gchar *
+mpris_menu_item_get_player (MprisMenuItem *item)
 {
-  g_signal_emit (item, signals[MEDIA_NOTIFY], 0, message);
+  MprisMenuItemPrivate *priv;
+
+  g_return_val_if_fail (IS_MPRIS_MENU_ITEM (item), NULL);
+
+  priv = GET_PRIVATE (item);
+
+  return priv->player;
 }
 
-static void
-media_play_pause_clicked_event (GtkButton *button, gpointer user_data)
+
+
+void
+mpris_menu_item_set_title (MprisMenuItem *item,
+                           const gchar   *title)
 {
-  MprisMenuItem *item = user_data;
-  media_notify (item, "PlayPause");
+  MprisMenuItemPrivate *priv;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  if (title == NULL || *title == '\0')
+    gtk_label_set_markup_printf_escaped (GTK_LABEL (priv->title_label), "<b>%s</b>", priv->title);
+  else
+    gtk_label_set_markup_printf_escaped (GTK_LABEL (priv->title_label), "<b>%s</b>", title);
 }
 
-static void
-media_go_previous_clicked_event (GtkButton *button, gpointer user_data)
+
+
+void
+mpris_menu_item_set_artist (MprisMenuItem *item,
+                            const gchar   *artist)
 {
-  MprisMenuItem *item = user_data;
-  media_notify (item, "Previous");
+  MprisMenuItemPrivate *priv;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  if (artist == NULL || *artist == '\0')
+    gtk_label_set_label (GTK_LABEL (priv->artist_label), NULL);
+  else
+    gtk_label_set_label (GTK_LABEL (priv->artist_label), artist);
 }
 
-static void
-media_go_next_clicked_event (GtkButton *button, gpointer user_data)
+
+
+void
+mpris_menu_item_set_can_go_previous (MprisMenuItem *item,
+                                     gboolean       enabled)
 {
-  MprisMenuItem *item = user_data;
-  media_notify (item, "Next");
+  MprisMenuItemPrivate *priv;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  priv->can_go_previous = enabled;
+
+  if (priv->is_running)
+    gtk_widget_set_sensitive (priv->go_previous, priv->can_go_previous);
+  else
+    gtk_widget_set_sensitive (priv->go_previous, FALSE);
 }
 
-static void
-mpris_menu_item_raise (MprisMenuItem *item)
+
+
+void
+mpris_menu_item_set_can_play (MprisMenuItem *item,
+                              gboolean       enabled)
 {
   MprisMenuItemPrivate *priv;
 
@@ -170,38 +269,44 @@ mpris_menu_item_raise (MprisMenuItem *item)
 
   priv = GET_PRIVATE (item);
 
+  priv->can_play = enabled;
+
   if (priv->is_running)
-    {
-      if (priv->can_raise)
-        {
-          media_notify (item, "Raise");
-        }
-    }
+    gtk_widget_set_sensitive (priv->play_pause, priv->can_play);
+  else
+    gtk_widget_set_sensitive (priv->play_pause, FALSE);
 }
 
-static void
-mpris_menu_item_launch (MprisMenuItem *item)
+
+
+void
+mpris_menu_item_set_can_pause (MprisMenuItem *item,
+                               gboolean       enabled)
 {
   MprisMenuItemPrivate *priv;
-  GAppInfo             *app_info;
 
   g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
   priv = GET_PRIVATE (item);
 
-  if (priv->is_running)
-    return;
+  priv->can_pause = enabled;
 
-  app_info = (GAppInfo*)g_desktop_app_info_new_from_filename (priv->filename);
-  if (app_info != NULL)
+  if (priv->is_running)
     {
-      g_app_info_launch (app_info, NULL, NULL, NULL);
-      g_object_unref (app_info);
+      if (priv->is_playing)
+        {
+          gtk_widget_set_sensitive (priv->play_pause, priv->can_pause);
+        }
     }
+  else
+    gtk_widget_set_sensitive (priv->play_pause, FALSE);
 }
 
-static void
-mpris_menu_item_raise_or_launch (MprisMenuItem *item)
+
+
+void
+mpris_menu_item_set_can_go_next (MprisMenuItem *item,
+                                 gboolean       enabled)
 {
   MprisMenuItemPrivate *priv;
 
@@ -209,120 +314,162 @@ mpris_menu_item_raise_or_launch (MprisMenuItem *item)
 
   priv = GET_PRIVATE (item);
 
+  priv->can_go_next = enabled;
+
   if (priv->is_running)
-    mpris_menu_item_raise (item);
+    gtk_widget_set_sensitive (priv->go_next, priv->can_go_next);
   else
-    mpris_menu_item_launch (item);
+    gtk_widget_set_sensitive (priv->go_next, FALSE);
 }
 
-static void
-menu_item_activate_event (GtkMenuItem *mi, gpointer user_data)
-{
-  mpris_menu_item_raise_or_launch (MPRIS_MENU_ITEM (mi));
-}
 
-static GtkWidget *
-track_info_label_new (void)
+
+void
+mpris_menu_item_set_can_raise (MprisMenuItem *item,
+                               gboolean       can_raise)
 {
-  GtkWidget *label;
+  MprisMenuItemPrivate *priv;
 
-  label = gtk_label_new (NULL);
-  gtk_label_set_width_chars (GTK_LABEL (label), 25);
-  gtk_label_set_max_width_chars (GTK_LABEL (label), 25);
-  gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_MIDDLE);
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
-  gtk_widget_set_halign (label, GTK_ALIGN_START);
+  priv = GET_PRIVATE (item);
 
-  return label;
+  priv->can_raise = can_raise;
 }
 
-static void
-update_packing (MprisMenuItem *self)
+
+
+void
+mpris_menu_item_set_is_running (MprisMenuItem *item,
+                                gboolean       running)
 {
   MprisMenuItemPrivate *priv;
-  GtkBox               *hbox;
-  GtkBox               *vbox;
-  GtkBox               *button_box;
-  GtkStyleContext      *ctx;
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (self));
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  priv = GET_PRIVATE (self);
+  priv = GET_PRIVATE (item);
 
-  TRACE("entering");
+  priv->is_running = running;
 
-  /* Create the widgets */
-  hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
-  vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
+  if (!priv->is_running)
+    {
+      mpris_menu_item_set_title (item, NULL);
+      mpris_menu_item_set_artist (item, _("Not currently playing"));
+      mpris_menu_item_set_can_play (item, FALSE);
+      mpris_menu_item_set_can_pause (item, FALSE);
+      mpris_menu_item_set_can_go_previous (item, FALSE);
+      mpris_menu_item_set_can_go_next (item, FALSE);
+      mpris_menu_item_set_is_playing (item, FALSE);
+      mpris_menu_item_set_is_stopped (item, TRUE);
+    }
+  else
+    {
+      mpris_menu_item_set_can_play (item, priv->can_play);
+      mpris_menu_item_set_can_pause (item, priv->can_pause);
+      mpris_menu_item_set_can_go_next (item, priv->can_go_next);
+      mpris_menu_item_set_can_go_previous (item, priv->can_go_previous);
+      mpris_menu_item_set_is_playing (item, priv->is_playing);
+      mpris_menu_item_set_is_stopped (item, priv->is_stopped);
+    }
+}
 
-  button_box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
-  ctx = gtk_widget_get_style_context (GTK_WIDGET (button_box));
-  gtk_style_context_add_class (ctx, "linked");
 
-  priv->hbox = GTK_WIDGET(hbox);
-  priv->vbox = GTK_WIDGET(vbox);
-  priv->button_box = GTK_WIDGET(button_box);
 
-  priv->go_previous = gtk_button_new_from_icon_name ("media-skip-backward-symbolic", GTK_ICON_SIZE_MENU);
-  priv->play_pause = gtk_button_new_from_icon_name ("media-playback-start-symbolic", GTK_ICON_SIZE_MENU);
-  priv->go_next = gtk_button_new_from_icon_name ("media-skip-forward-symbolic", GTK_ICON_SIZE_MENU);
+void
+mpris_menu_item_set_is_playing (MprisMenuItem *item,
+                                gboolean       playing)
+{
+  MprisMenuItemPrivate *priv;
 
-  priv->title_label = track_info_label_new ();
-  priv->artist_label = track_info_label_new ();
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  priv->image = gtk_image_new_from_icon_name ("audio-x-generic", GTK_ICON_SIZE_LARGE_TOOLBAR);
+  priv = GET_PRIVATE (item);
 
-  /* Pack the widgets */
-G_GNUC_BEGIN_IGNORE_DEPRECATIONS
-  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (self), priv->image);
-G_GNUC_END_IGNORE_DEPRECATIONS
-  gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 24);
+  priv->is_playing = playing;
 
-  gtk_box_pack_start (button_box, priv->go_previous, FALSE, FALSE, 0);
-  gtk_box_pack_start (button_box, priv->play_pause, FALSE, FALSE, 0);
-  gtk_box_pack_start (button_box, priv->go_next, FALSE, FALSE, 0);
+  if (priv->is_playing)
+    {
+      gtk_image_set_from_icon_name (GTK_IMAGE (gtk_button_get_image (GTK_BUTTON (priv->play_pause))), "media-playback-pause-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
+      gtk_widget_set_sensitive (priv->play_pause, priv->can_pause);
+      priv->is_stopped = FALSE;
+    }
+  else
+    {
+      gtk_image_set_from_icon_name (GTK_IMAGE (gtk_button_get_image (GTK_BUTTON (priv->play_pause))), "media-playback-start-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
+      gtk_widget_set_sensitive (priv->play_pause, priv->can_play);
+    }
 
-  gtk_box_pack_start (vbox, priv->title_label, FALSE, FALSE, 0);
-  gtk_box_pack_start (vbox, priv->artist_label, FALSE, FALSE, 0);
+  if (!priv->is_running)
+    {
+      gtk_widget_set_sensitive (priv->play_pause, FALSE);
+    }
+}
 
-  gtk_box_pack_start (hbox, GTK_WIDGET (vbox), TRUE, TRUE, 0);
-  gtk_box_pack_start (hbox, GTK_WIDGET (button_box), FALSE, FALSE, 0);
 
-  /* Configure the widgets */
-  mpris_menu_item_set_title (self, priv->player);
-  mpris_menu_item_set_artist (self, _("Not currently playing"));
 
-  g_signal_connect (priv->play_pause, "clicked", G_CALLBACK (media_play_pause_clicked_event), self);
-  g_signal_connect (priv->go_previous, "clicked", G_CALLBACK (media_go_previous_clicked_event), self);
-  g_signal_connect (priv->go_next, "clicked", G_CALLBACK (media_go_next_clicked_event), self);
-  g_signal_connect (self, "activate", G_CALLBACK (menu_item_activate_event), self);
+void
+mpris_menu_item_set_is_stopped (MprisMenuItem *item,
+                                gboolean       stopped)
+{
+  MprisMenuItemPrivate *priv;
 
-  /* Keep a reference to each */
-  g_object_ref (priv->title_label);
-  g_object_ref (priv->artist_label);
-  g_object_ref (priv->button_box);
-  g_object_ref (priv->vbox);
-  g_object_ref (priv->hbox);
-  g_object_ref (priv->go_previous);
-  g_object_ref (priv->play_pause);
-  g_object_ref (priv->go_next);
-  g_object_ref (priv->image);
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  /* And finally display them */
-  gtk_widget_show_all (priv->button_box);
-  gtk_widget_show_all (priv->hbox);
-  gtk_widget_show_all (priv->vbox);
+  priv = GET_PRIVATE (item);
+
+  priv->is_stopped = stopped;
+
+  if (priv->is_stopped)
+    {
+      if (priv->is_playing)
+        {
+          mpris_menu_item_set_is_playing (item, FALSE);
+        }
+
+      mpris_menu_item_set_title (item, NULL);
+      mpris_menu_item_set_artist (item, _("Not currently playing"));
+    }
+}
+
+
+
+/* Private API */
+static void
+mpris_menu_item_class_init (MprisMenuItemClass *item_class)
+{
+  GObjectClass      *gobject_class =   G_OBJECT_CLASS      (item_class);
+  GtkWidgetClass    *widget_class =    GTK_WIDGET_CLASS    (item_class);
+
+  gobject_class->finalize = mpris_menu_item_finalize;
+  widget_class->button_press_event   = mpris_menu_item_button_press_event;
+  widget_class->button_release_event = mpris_menu_item_button_release_event;
+
+  /**
+   * MprisMenuItem::media-notify:
+   * @menuitem: the #MprisMenuItem for which the value changed
+   * @value: the mpris signal to emit
+   *
+   * Emitted whenever the a media button is clicked.
+   */
+  signals[MEDIA_NOTIFY] = g_signal_new ("media-notify",
+                                        TYPE_MPRIS_MENU_ITEM,
+                                        G_SIGNAL_RUN_LAST,
+                                        0, NULL, NULL,
+                                        g_cclosure_marshal_VOID__STRING,
+                                        G_TYPE_NONE,
+                                        1, G_TYPE_STRING);
 
-  gtk_container_add (GTK_CONTAINER (self), priv->hbox);
+  g_type_class_add_private (item_class, sizeof (MprisMenuItemPrivate));
 }
 
+
+
 static void
-mpris_menu_item_init (MprisMenuItem *self)
+mpris_menu_item_init (MprisMenuItem *item)
 {
   MprisMenuItemPrivate *priv;
 
-  priv = GET_PRIVATE (self);
+  priv = GET_PRIVATE (item);
 
   priv->title_label = NULL;
   priv->artist_label = NULL;
@@ -338,14 +485,16 @@ mpris_menu_item_init (MprisMenuItem *self)
   priv->filename = NULL;
 }
 
+
+
 static void
 mpris_menu_item_finalize (GObject *object)
 {
-  MprisMenuItem        *self;
+  MprisMenuItem        *item;
   MprisMenuItemPrivate *priv;
 
-  self = MPRIS_MENU_ITEM (object);
-  priv = GET_PRIVATE (self);
+  item = MPRIS_MENU_ITEM (object);
+  priv = GET_PRIVATE (item);
 
   if (priv->player)
     g_free (priv->player);
@@ -367,45 +516,104 @@ mpris_menu_item_finalize (GObject *object)
   (*G_OBJECT_CLASS (mpris_menu_item_parent_class)->finalize) (object);
 }
 
+
+
+static void
+mpris_menu_item_raise (MprisMenuItem *item)
+{
+  MprisMenuItemPrivate *priv;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  if (priv->is_running && priv->can_raise)
+    media_notify (item, "Raise");
+}
+
+
+
+static void
+mpris_menu_item_launch (MprisMenuItem *item)
+{
+  MprisMenuItemPrivate *priv;
+  GAppInfo             *app_info;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  if (priv->is_running)
+    return;
+
+  app_info = (GAppInfo*)g_desktop_app_info_new_from_filename (priv->filename);
+  if (app_info != NULL)
+    {
+      g_app_info_launch (app_info, NULL, NULL, NULL);
+      g_object_unref (app_info);
+    }
+}
+
+
+
+static void
+mpris_menu_item_raise_or_launch (MprisMenuItem *item)
+{
+  MprisMenuItemPrivate *priv;
+
+  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+
+  priv = GET_PRIVATE (item);
+
+  if (priv->is_running)
+    mpris_menu_item_raise (item);
+  else
+    mpris_menu_item_launch (item);
+}
+
+
+
 static GtkWidget *
-mpris_menu_item_get_widget_at_event (MprisMenuItem  *menuitem,
+mpris_menu_item_get_widget_at_event (MprisMenuItem  *item,
                                      GdkEventButton *event)
 {
   MprisMenuItemPrivate *priv;
   GtkAllocation         alloc;
   gint                  x, y;
 
-  g_return_val_if_fail (IS_MPRIS_MENU_ITEM (menuitem), NULL);
+  g_return_val_if_fail (IS_MPRIS_MENU_ITEM (item), NULL);
 
-  priv = GET_PRIVATE (menuitem);
+  priv = GET_PRIVATE (item);
 
   gtk_widget_get_allocation (priv->button_box, &alloc);
-  gtk_widget_translate_coordinates (GTK_WIDGET (menuitem), priv->button_box, event->x, event->y, &x, &y);
+  gtk_widget_translate_coordinates (GTK_WIDGET (item), priv->button_box, event->x, event->y, &x, &y);
 
   if (x > 0 && x < alloc.width && y > 0 && y < alloc.height)
     {
       gtk_widget_get_allocation (priv->go_previous, &alloc);
-      gtk_widget_translate_coordinates (GTK_WIDGET (menuitem), priv->go_previous, event->x, event->y, &x, &y);
+      gtk_widget_translate_coordinates (GTK_WIDGET (item), priv->go_previous, event->x, event->y, &x, &y);
 
       if (x > 0 && x < alloc.width && y > 0 && y < alloc.height)
         return GTK_WIDGET (priv->go_previous);
 
       gtk_widget_get_allocation (priv->play_pause, &alloc);
-      gtk_widget_translate_coordinates (GTK_WIDGET (menuitem), priv->play_pause, event->x, event->y, &x, &y);
+      gtk_widget_translate_coordinates (GTK_WIDGET (item), priv->play_pause, event->x, event->y, &x, &y);
 
       if (x > 0 && x < alloc.width && y > 0 && y < alloc.height)
         return GTK_WIDGET (priv->play_pause);
 
       gtk_widget_get_allocation (priv->go_next, &alloc);
-      gtk_widget_translate_coordinates (GTK_WIDGET (menuitem), priv->go_next, event->x, event->y, &x, &y);
+      gtk_widget_translate_coordinates (GTK_WIDGET (item), priv->go_next, event->x, event->y, &x, &y);
 
       if (x > 0 && x < alloc.width && y > 0 && y < alloc.height)
         return GTK_WIDGET (priv->go_next);
     }
 
-  return GTK_WIDGET (menuitem);
+  return GTK_WIDGET (item);
 }
 
+
+
 static gboolean
 mpris_menu_item_button_press_event (GtkWidget      *menuitem,
                                     GdkEventButton *event)
@@ -426,6 +634,8 @@ mpris_menu_item_button_press_event (GtkWidget      *menuitem,
   return TRUE;
 }
 
+
+
 static gboolean
 mpris_menu_item_button_release_event (GtkWidget      *menuitem,
                                       GdkEventButton *event)
@@ -446,258 +656,169 @@ mpris_menu_item_button_release_event (GtkWidget      *menuitem,
   return TRUE;
 }
 
-const gchar *
-mpris_menu_item_get_player (MprisMenuItem *mi)
-{
-  MprisMenuItemPrivate *priv;
-
-  g_return_val_if_fail (IS_MPRIS_MENU_ITEM (mi), NULL);
-
-  priv = GET_PRIVATE (mi);
 
-  return priv->player;
-}
 
-void
-mpris_menu_item_set_title (MprisMenuItem *item,
-                           const gchar   *title)
+static void
+media_play_pause_clicked_event (GtkButton *button, gpointer user_data)
 {
-  MprisMenuItemPrivate *priv;
+  MprisMenuItem *item = user_data;
+  media_notify (item, "PlayPause");
+}
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  priv = GET_PRIVATE (item);
 
-  if (title == NULL || *title == '\0')
-    gtk_label_set_markup_printf_escaped (GTK_LABEL (priv->title_label), "<b>%s</b>", priv->title);
-  else
-    gtk_label_set_markup_printf_escaped (GTK_LABEL (priv->title_label), "<b>%s</b>", title);
+static void
+media_go_previous_clicked_event (GtkButton *button, gpointer user_data)
+{
+  MprisMenuItem *item = user_data;
+  media_notify (item, "Previous");
 }
 
-void
-mpris_menu_item_set_artist (MprisMenuItem *item,
-                            const gchar   *artist)
+
+
+static void
+media_go_next_clicked_event (GtkButton *button, gpointer user_data)
 {
-  MprisMenuItemPrivate *priv;
+  MprisMenuItem *item = user_data;
+  media_notify (item, "Next");
+}
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  priv = GET_PRIVATE (item);
 
-  if (artist == NULL || *artist == '\0')
-    gtk_label_set_label (GTK_LABEL (priv->artist_label), NULL);
-  else
-    gtk_label_set_label (GTK_LABEL (priv->artist_label), artist);
+static void
+menu_item_activate_event (GtkMenuItem *mi, gpointer user_data)
+{
+  mpris_menu_item_raise_or_launch (MPRIS_MENU_ITEM (mi));
 }
 
-void
-mpris_menu_item_set_can_go_previous (MprisMenuItem *item,
-                                     gboolean       enabled)
-{
-  MprisMenuItemPrivate *priv;
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
-  priv = GET_PRIVATE (item);
-
-  priv->can_go_previous = enabled;
-
-  if (priv->is_running)
-    gtk_widget_set_sensitive (priv->go_previous, priv->can_go_previous);
-  else
-    gtk_widget_set_sensitive (priv->go_previous, FALSE);
-}
-
-void
-mpris_menu_item_set_can_play (MprisMenuItem *item,
-                              gboolean       enabled)
+static void
+media_notify (MprisMenuItem *item,
+              gchar         *message)
 {
-  MprisMenuItemPrivate *priv;
-
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
-
-  priv = GET_PRIVATE (item);
+  g_signal_emit (item, signals[MEDIA_NOTIFY], 0, message);
+}
 
-  priv->can_play = enabled;
 
-  if (priv->is_running)
-    gtk_widget_set_sensitive (priv->play_pause, priv->can_play);
-  else
-    gtk_widget_set_sensitive (priv->play_pause, FALSE);
-}
 
-void
-mpris_menu_item_set_can_pause (MprisMenuItem *item,
-                               gboolean       enabled)
+static GtkWidget *
+track_info_label_new (void)
 {
-  MprisMenuItemPrivate *priv;
-
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+  GtkWidget *label;
 
-  priv = GET_PRIVATE (item);
+  label = gtk_label_new (NULL);
+  gtk_label_set_width_chars (GTK_LABEL (label), 25);
+  gtk_label_set_max_width_chars (GTK_LABEL (label), 25);
+  gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_MIDDLE);
 
-  priv->can_pause = enabled;
+  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
+  gtk_widget_set_halign (label, GTK_ALIGN_START);
 
-  if (priv->is_running)
-    {
-      if (priv->is_playing)
-        {
-          gtk_widget_set_sensitive (priv->play_pause, priv->can_pause);
-        }
-    }
-  else
-    gtk_widget_set_sensitive (priv->play_pause, FALSE);
+  return label;
 }
 
-void
-mpris_menu_item_set_can_go_next (MprisMenuItem *item,
-                                 gboolean       enabled)
-{
-  MprisMenuItemPrivate *priv;
-
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
-
-  priv = GET_PRIVATE (item);
-
-  priv->can_go_next = enabled;
 
-  if (priv->is_running)
-    gtk_widget_set_sensitive (priv->go_next, priv->can_go_next);
-  else
-    gtk_widget_set_sensitive (priv->go_next, FALSE);
-}
 
-void
-mpris_menu_item_set_is_playing (MprisMenuItem *item,
-                                gboolean       playing)
+static void
+gtk_label_set_markup_printf_escaped (GtkLabel    *label,
+                                     const gchar *format,
+                                     ...)
 {
-  MprisMenuItemPrivate *priv;
+  va_list args;
+  gchar *str;
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+  va_start (args, format);
+  str = g_markup_vprintf_escaped (format, args);
+  gtk_label_set_markup (label, str);
+  va_end (args);
 
-  priv = GET_PRIVATE (item);
+  g_free (str);
+}
 
-  priv->is_playing = playing;
 
-  if (priv->is_playing)
-    {
-      gtk_image_set_from_icon_name (GTK_IMAGE (gtk_button_get_image (GTK_BUTTON (priv->play_pause))), "media-playback-pause-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
-      gtk_widget_set_sensitive (priv->play_pause, priv->can_pause);
-      priv->is_stopped = FALSE;
-    }
-  else
-    {
-      gtk_image_set_from_icon_name (GTK_IMAGE (gtk_button_get_image (GTK_BUTTON (priv->play_pause))), "media-playback-start-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
-      gtk_widget_set_sensitive (priv->play_pause, priv->can_play);
-    }
 
-  if (!priv->is_running)
-    {
-      gtk_widget_set_sensitive (priv->play_pause, FALSE);
-    }
-}
-
-void
-mpris_menu_item_set_is_stopped (MprisMenuItem *item,
-                                gboolean       stopped)
+static void
+update_packing (MprisMenuItem *item)
 {
   MprisMenuItemPrivate *priv;
+  GtkBox               *hbox;
+  GtkBox               *vbox;
+  GtkBox               *button_box;
+  GtkStyleContext      *ctx;
 
   g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
 
   priv = GET_PRIVATE (item);
 
-  priv->is_stopped = stopped;
-
-  if (priv->is_stopped)
-    {
-      if (priv->is_playing)
-        {
-          mpris_menu_item_set_is_playing (item, FALSE);
-        }
-
-      mpris_menu_item_set_title (item, NULL);
-      mpris_menu_item_set_artist (item, _("Not currently playing"));
-    }
-}
-
-void
-mpris_menu_item_set_is_running (MprisMenuItem *item,
-                                gboolean       running)
-{
-  MprisMenuItemPrivate *priv;
+  TRACE("entering");
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+  /* Create the widgets */
+  hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
+  vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
 
-  priv = GET_PRIVATE (item);
+  button_box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
+  ctx = gtk_widget_get_style_context (GTK_WIDGET (button_box));
+  gtk_style_context_add_class (ctx, "linked");
 
-  priv->is_running = running;
+  priv->hbox = GTK_WIDGET(hbox);
+  priv->vbox = GTK_WIDGET(vbox);
+  priv->button_box = GTK_WIDGET(button_box);
 
-  if (!priv->is_running)
-    {
-      mpris_menu_item_set_title (item, NULL);
-      mpris_menu_item_set_artist (item, _("Not currently playing"));
-      mpris_menu_item_set_can_play (item, FALSE);
-      mpris_menu_item_set_can_pause (item, FALSE);
-      mpris_menu_item_set_can_go_previous (item, FALSE);
-      mpris_menu_item_set_can_go_next (item, FALSE);
-      mpris_menu_item_set_is_playing (item, FALSE);
-      mpris_menu_item_set_is_stopped (item, TRUE);
-    }
-  else
-    {
-      mpris_menu_item_set_can_play (item, priv->can_play);
-      mpris_menu_item_set_can_pause (item, priv->can_pause);
-      mpris_menu_item_set_can_go_next (item, priv->can_go_next);
-      mpris_menu_item_set_can_go_previous (item, priv->can_go_previous);
-      mpris_menu_item_set_is_playing (item, priv->is_playing);
-      mpris_menu_item_set_is_stopped (item, priv->is_stopped);
-    }
-}
+  priv->go_previous = gtk_button_new_from_icon_name ("media-skip-backward-symbolic", GTK_ICON_SIZE_MENU);
+  priv->play_pause = gtk_button_new_from_icon_name ("media-playback-start-symbolic", GTK_ICON_SIZE_MENU);
+  priv->go_next = gtk_button_new_from_icon_name ("media-skip-forward-symbolic", GTK_ICON_SIZE_MENU);
 
-void
-mpris_menu_item_set_can_raise (MprisMenuItem *item,
-                               gboolean       can_raise)
-{
-  MprisMenuItemPrivate *priv;
+  priv->title_label = track_info_label_new ();
+  priv->artist_label = track_info_label_new ();
 
-  g_return_if_fail (IS_MPRIS_MENU_ITEM (item));
+  priv->image = gtk_image_new_from_icon_name ("audio-x-generic", GTK_ICON_SIZE_LARGE_TOOLBAR);
 
-  priv = GET_PRIVATE (item);
+  /* Pack the widgets */
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), priv->image);
+G_GNUC_END_IGNORE_DEPRECATIONS
+  gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 24);
 
-  priv->can_raise = can_raise;
-}
+  gtk_box_pack_start (button_box, priv->go_previous, FALSE, FALSE, 0);
+  gtk_box_pack_start (button_box, priv->play_pause, FALSE, FALSE, 0);
+  gtk_box_pack_start (button_box, priv->go_next, FALSE, FALSE, 0);
 
-GtkWidget*
-mpris_menu_item_new_with_player (const gchar *player,
-                                 const gchar *title,
-                                 const gchar *icon_name,
-                                 const gchar *filename)
-{
-  MprisMenuItem        *menu_item;
-  MprisMenuItemPrivate *priv;
+  gtk_box_pack_start (vbox, priv->title_label, FALSE, FALSE, 0);
+  gtk_box_pack_start (vbox, priv->artist_label, FALSE, FALSE, 0);
 
-  TRACE("entering");
+  gtk_box_pack_start (hbox, GTK_WIDGET (vbox), TRUE, TRUE, 0);
+  gtk_box_pack_start (hbox, GTK_WIDGET (button_box), FALSE, FALSE, 0);
 
-  menu_item = MPRIS_MENU_ITEM (g_object_new (TYPE_MPRIS_MENU_ITEM, NULL));
+  /* Configure the widgets */
+  mpris_menu_item_set_title (item, priv->player);
+  mpris_menu_item_set_artist (item, _("Not currently playing"));
 
-  priv = GET_PRIVATE (menu_item);
+  g_signal_connect (priv->play_pause, "clicked", G_CALLBACK (media_play_pause_clicked_event), item);
+  g_signal_connect (priv->go_previous, "clicked", G_CALLBACK (media_go_previous_clicked_event), item);
+  g_signal_connect (priv->go_next, "clicked", G_CALLBACK (media_go_next_clicked_event), item);
+  g_signal_connect (item, "activate", G_CALLBACK (menu_item_activate_event), item);
 
-  priv->player = g_strdup(player);
-  if (title != NULL)
-    priv->title = g_strdup(title);
-  else
-    priv->title = g_strdup(player);
-  priv->filename = g_strdup(filename);
+  /* Keep a reference to each */
+  g_object_ref (priv->title_label);
+  g_object_ref (priv->artist_label);
+  g_object_ref (priv->button_box);
+  g_object_ref (priv->vbox);
+  g_object_ref (priv->hbox);
+  g_object_ref (priv->go_previous);
+  g_object_ref (priv->play_pause);
+  g_object_ref (priv->go_next);
+  g_object_ref (priv->image);
 
-  update_packing (menu_item);
+  /* And finally display them */
+  gtk_widget_show_all (priv->button_box);
+  gtk_widget_show_all (priv->hbox);
+  gtk_widget_show_all (priv->vbox);
 
-  gtk_widget_add_events (GTK_WIDGET(menu_item), GDK_SCROLL_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK);
+  gtk_container_add (GTK_CONTAINER (item), priv->hbox);
+}
 
-  gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
 
-  return GTK_WIDGET(menu_item);
-}
 
 static gchar *
 find_desktop_entry (const gchar *player_name)
@@ -739,40 +860,3 @@ find_desktop_entry (const gchar *player_name)
 
   return filename;
 }
-
-GtkWidget*
-mpris_menu_item_new_from_player_name (const gchar *player)
-{
-  GtkWidget *widget = NULL;
-  GKeyFile  *key_file;
-  gchar     *file;
-  gchar     *filename;
-  gchar     *full_path;
-
-  filename = find_desktop_entry (player);
-  if (filename == NULL)
-    {
-      g_free (filename);
-      return NULL;
-    }
-
-  file = g_strconcat("applications/", filename, NULL);
-  g_free (filename);
-
-  key_file = g_key_file_new();
-  if (g_key_file_load_from_data_dirs (key_file, file, &full_path, G_KEY_FILE_NONE, NULL))
-    {
-      gchar *name = g_key_file_get_string (key_file, "Desktop Entry", "Name", NULL);
-      gchar *icon_name = g_key_file_get_string (key_file, "Desktop Entry", "Icon", NULL);
-
-      widget = mpris_menu_item_new_with_player (player, name, icon_name, full_path);
-
-      g_free (name);
-      g_free (icon_name);
-    }
-
-  g_key_file_free (key_file);
-  g_free (file);
-
-  return widget;
-}
diff --git a/panel-plugin/mprismenuitem.h b/panel-plugin/mprismenuitem.h
index 80b1c12..3442f05 100644
--- a/panel-plugin/mprismenuitem.h
+++ b/panel-plugin/mprismenuitem.h
@@ -66,36 +66,36 @@ GtkWidget   *mpris_menu_item_new_with_player       (const gchar *player,
 
 GtkWidget   *mpris_menu_item_new_from_player_name  (const gchar *player);
 
-const gchar *mpris_menu_item_get_player            (MprisMenuItem *mi);
+const gchar *mpris_menu_item_get_player            (MprisMenuItem *item);
 
-void         mpris_menu_item_set_title             (MprisMenuItem *mi,
+void         mpris_menu_item_set_title             (MprisMenuItem *item,
                                                     const gchar   *title);
 
-void         mpris_menu_item_set_artist            (MprisMenuItem *mi,
+void         mpris_menu_item_set_artist            (MprisMenuItem *item,
                                                     const gchar   *artist);
 
-void         mpris_menu_item_set_can_go_previous   (MprisMenuItem *mi,
+void         mpris_menu_item_set_can_go_previous   (MprisMenuItem *item,
                                                     gboolean enabled);
 
-void         mpris_menu_item_set_can_play          (MprisMenuItem *mi,
+void         mpris_menu_item_set_can_play          (MprisMenuItem *item,
                                                     gboolean enabled);
 
-void         mpris_menu_item_set_can_pause         (MprisMenuItem *mi,
+void         mpris_menu_item_set_can_pause         (MprisMenuItem *item,
                                                     gboolean enabled);
 
-void         mpris_menu_item_set_can_go_next       (MprisMenuItem *mi,
+void         mpris_menu_item_set_can_go_next       (MprisMenuItem *item,
                                                     gboolean enabled);
 
-void         mpris_menu_item_set_can_raise         (MprisMenuItem *mi,
+void         mpris_menu_item_set_can_raise         (MprisMenuItem *item,
                                                     gboolean can_raise);
 
-void         mpris_menu_item_set_is_running        (MprisMenuItem *mi,
+void         mpris_menu_item_set_is_running        (MprisMenuItem *item,
                                                     gboolean running);
 
-void         mpris_menu_item_set_is_playing        (MprisMenuItem *mi,
+void         mpris_menu_item_set_is_playing        (MprisMenuItem *item,
                                                     gboolean playing);
 
-void         mpris_menu_item_set_is_stopped        (MprisMenuItem *mi,
+void         mpris_menu_item_set_is_stopped        (MprisMenuItem *item,
                                                     gboolean stopped);
 
 G_END_DECLS

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list