[Goodies-commits] r3842 - xfmpc/trunk/src

Mike Massonnet mmassonnet at xfce.org
Tue Jan 22 11:08:17 CET 2008


Author: mmassonnet
Date: 2008-01-22 10:08:17 +0000 (Tue, 22 Jan 2008)
New Revision: 3842

Added:
   xfmpc/trunk/src/preferences.c
   xfmpc/trunk/src/preferences.h
Modified:
   xfmpc/trunk/src/Makefile.am
   xfmpc/trunk/src/interface.c
   xfmpc/trunk/src/main.c
Log:
* src/Makefile.am, src/preferences.c, src/preferences.h:
  - New GObject preferences interface
* src/interface.c(xfmpc_interface_class_init),
  src/interface.c(xfmpc_interface_finalize):
  - New finalize function
  - Unref XfmpcPreferences
* src/interface.c(xfmpc_interface_init):
  - Get a XfmpcPreferences
  - Read the position of the window
* src/interface.c(xfmpc_interface_closed):
  - Save the position of the window
* src/main.c:
  - Add transformation function G_TYPE_STRING to G_TYPE_INT


Modified: xfmpc/trunk/src/Makefile.am
===================================================================
--- xfmpc/trunk/src/Makefile.am	2008-01-21 19:26:30 UTC (rev 3841)
+++ xfmpc/trunk/src/Makefile.am	2008-01-22 10:08:17 UTC (rev 3842)
@@ -4,6 +4,8 @@
 	main.c								\
 	interface.c							\
 	interface.h							\
+	preferences.c							\
+	preferences.h							\
 	mpdclient.c							\
 	mpdclient.h
 

Modified: xfmpc/trunk/src/interface.c
===================================================================
--- xfmpc/trunk/src/interface.c	2008-01-21 19:26:30 UTC (rev 3841)
+++ xfmpc/trunk/src/interface.c	2008-01-22 10:08:17 UTC (rev 3842)
@@ -26,6 +26,7 @@
 
 #include "interface.h"
 #include "interface-ui.h"
+#include "preferences.h"
 #include "mpdclient.h"
 
 #define BORDER 4
@@ -35,11 +36,14 @@
 static void             xfmpc_interface_class_init              (XfmpcInterfaceClass *klass);
 static void             xfmpc_interface_init                    (XfmpcInterface *interface);
 static void             xfmpc_interface_dispose                 (GObject *object);
+static void             xfmpc_interface_finalize                (GObject *object);
 
 static gboolean         xfmpc_interface_refresh                 (XfmpcInterface *interface);
 
 static gboolean         xfmpc_interface_reconnect               (XfmpcInterface *interface);
 
+static gboolean         xfmpc_interface_closed                  (XfmpcInterface *interface,
+                                                                 GdkEvent *event);
 static void             xfmpc_interface_action_previous         (GtkAction *action,
                                                                  XfmpcInterface *interface);
 static void             xfmpc_interface_action_pp               (GtkAction *action,
@@ -62,6 +66,7 @@
 {
   GtkWindow             parent;
   XfmpcInterfacePriv   *priv;
+  XfmpcPreferences     *preferences;
 };
 
 struct _XfmpcInterfacePriv
@@ -133,6 +138,7 @@
 
   gobject_class = G_OBJECT_CLASS (klass);
   gobject_class->dispose = xfmpc_interface_dispose;
+  gobject_class->finalize = xfmpc_interface_finalize;
 }
 
 static void
@@ -142,11 +148,21 @@
   interface->priv->mpdclient = xfmpc_mpdclient_new ();
   interface->priv->volume = -1;
 
+  interface->preferences = xfmpc_preferences_get ();
+
+  gint posx, posy;
+  g_object_get (G_OBJECT (interface->preferences),
+                "last-window-posx", &posx,
+                "last-window-posy", &posy,
+                NULL);
+
   /* === Window === */
   gtk_window_set_icon_name (GTK_WINDOW (interface), "stock_volume");
   gtk_window_set_title (GTK_WINDOW (interface), _("Xfmpc"));
   gtk_container_set_border_width (GTK_CONTAINER (interface), BORDER);
-  g_signal_connect (G_OBJECT (interface), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
+  g_signal_connect (G_OBJECT (interface), "delete-event", G_CALLBACK (xfmpc_interface_closed), interface);
+  if (G_LIKELY (posx != -1 && posy != -1))
+    gtk_window_move (GTK_WINDOW (interface), posx, posy);
 
   /* === Interface widgets === */
   GtkWidget *image = gtk_image_new_from_stock (GTK_STOCK_MEDIA_PREVIOUS, GTK_ICON_SIZE_BUTTON);
@@ -254,6 +270,14 @@
   (*G_OBJECT_CLASS (parent_class)->dispose) (object);
 }
 
+static void
+xfmpc_interface_finalize (GObject *object)
+{
+  XfmpcInterface *interface = XFMPC_INTERFACE (object);
+  g_object_unref (G_OBJECT (interface->preferences));
+  (*G_OBJECT_CLASS (parent_class)->finalize) (object);
+}
+
 GtkWidget*
 xfmpc_interface_new ()
 {
@@ -299,7 +323,7 @@
 xfmpc_interface_progress_box_press_event (XfmpcInterface *interface,
                                           GdkEventButton *event)
 {
-  if (G_UNLIKELY (event->type != GDK_BUTTON_PRESS && event->button != 1))
+  if (G_UNLIKELY (event->type != GDK_BUTTON_PRESS || event->button != 1))
     return FALSE;
 
   XfmpcMpdclient *mpdclient = interface->priv->mpdclient;
@@ -416,7 +440,7 @@
   /* title */
   xfmpc_interface_set_title (interface, xfmpc_mpdclient_get_title (mpdclient));
 
-  /* subtitle */
+  /* subtitle "by \"artist\" from \"album\" (year)" */
   text = g_strdup_printf (_("by \"%s\" from \"%s\" (%s)"),
                           xfmpc_mpdclient_get_artist (mpdclient),
                           xfmpc_mpdclient_get_album (mpdclient),
@@ -443,8 +467,24 @@
   return FALSE;
 }
 
+static gboolean
+xfmpc_interface_closed (XfmpcInterface *interface,
+                        GdkEvent *event)
+{
+  gint posx, posy;
+  gtk_window_get_position (GTK_WINDOW (interface), &posx, &posy);
 
+  g_object_set (G_OBJECT (interface->preferences),
+                "last-window-posx", posx,
+                "last-window-posy", posy,
+                NULL);
 
+  gtk_main_quit ();
+  return FALSE;
+}
+
+
+
 static void
 xfmpc_interface_action_previous (GtkAction *action,
                                  XfmpcInterface *interface)

Modified: xfmpc/trunk/src/main.c
===================================================================
--- xfmpc/trunk/src/main.c	2008-01-21 19:26:30 UTC (rev 3841)
+++ xfmpc/trunk/src/main.c	2008-01-22 10:08:17 UTC (rev 3842)
@@ -26,6 +26,17 @@
 
 #include "interface.h"
 
+
+
+static void
+transform_string_to_int (const GValue *src,
+                         GValue *dst)
+{
+  g_value_set_int (dst, (gint) strtol (g_value_get_string (src), NULL, 10));
+}
+
+
+
 int
 main (int argc, char *argv[])
 {
@@ -33,6 +44,8 @@
 
   gtk_init (&argc, &argv);
 
+  g_value_register_transform_func (G_TYPE_STRING, G_TYPE_INT, transform_string_to_int);
+
   gtk_window_set_default_icon_name ("xfmpc");
 
   GtkWidget *interface = xfmpc_interface_new ();

Added: xfmpc/trunk/src/preferences.c
===================================================================
--- xfmpc/trunk/src/preferences.c	                        (rev 0)
+++ xfmpc/trunk/src/preferences.c	2008-01-22 10:08:17 UTC (rev 3842)
@@ -0,0 +1,315 @@
+/*
+ *  Copyright (c) 2008 Mike Massonnet <mmassonnet at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+#include <libxfce4util/libxfce4util.h>
+
+#include "preferences.h"
+
+
+
+/* Property identifiers */
+enum
+{
+  PROP_0,
+  PROP_LAST_WINDOW_POSX,
+  PROP_LAST_WINDOW_POSY,
+  N_PROPERTIES,
+};
+
+
+
+static void             xfmpc_preferences_class_init            (XfmpcPreferencesClass *klass);
+static void             xfmpc_preferences_init                  (XfmpcPreferences *preferences);
+static void             xfmpc_preferences_finalize              (GObject *object);
+static void             xfmpc_preferences_get_property          (GObject *object,
+                                                                 guint prop_id,
+                                                                 GValue *value,
+                                                                 GParamSpec *pspec);
+static void             xfmpc_preferences_set_property          (GObject *object,
+                                                                 guint prop_id,
+                                                                 const GValue *value,
+                                                                 GParamSpec *pspec);
+
+static void             xfmpc_preferences_load                  (XfmpcPreferences *preferences);
+
+static void             xfmpc_preferences_store                 (XfmpcPreferences *preferences);
+
+static XfceRc *         xfmpc_preferences_get_rc                (XfmpcPreferences *preferences);
+
+
+
+
+struct _XfmpcPreferencesClass
+{
+  GObjectClass          parent_class;
+};
+
+struct _XfmpcPreferences
+{
+  GObject               parent;
+  GValue                values[N_PROPERTIES];
+};
+
+
+
+static GObjectClass *parent_class = NULL;
+
+
+
+GType
+xfmpc_preferences_get_type ()
+{
+  static GType xfmpc_preferences_type = G_TYPE_INVALID;
+
+  if (G_UNLIKELY (xfmpc_preferences_type == G_TYPE_INVALID))
+    {
+      static const GTypeInfo xfmpc_preferences_info =
+        {
+          sizeof (XfmpcPreferencesClass),
+          (GBaseInitFunc) NULL,
+          (GBaseFinalizeFunc) NULL,
+          (GClassInitFunc) xfmpc_preferences_class_init,
+          (GClassFinalizeFunc) NULL,
+          NULL,
+          sizeof (XfmpcPreferences),
+          0,
+          (GInstanceInitFunc) xfmpc_preferences_init,
+          NULL
+        };
+      xfmpc_preferences_type = g_type_register_static (G_TYPE_OBJECT, "XfmpcPreferences", &xfmpc_preferences_info, 0);
+    }
+
+  return xfmpc_preferences_type;
+}
+
+
+
+static void
+xfmpc_preferences_class_init (XfmpcPreferencesClass *klass)
+{
+  GObjectClass *gobject_class;
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->finalize = xfmpc_preferences_finalize;
+  gobject_class->get_property = xfmpc_preferences_get_property;
+  gobject_class->set_property = xfmpc_preferences_set_property;
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_LAST_WINDOW_POSX,
+                                   g_param_spec_int ("last-window-posx",
+                                                     "LastWindowPosx",
+                                                     "Window position on axis x",
+                                                     -1, G_MAXINT, -1,
+                                                     G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class,
+                                   PROP_LAST_WINDOW_POSY,
+                                   g_param_spec_int ("last-window-posy",
+                                                     "LastWindowPosy",
+                                                     "Window position on axis y",
+                                                     -1, G_MAXINT, -1,
+                                                     G_PARAM_READWRITE));
+}
+
+static void
+xfmpc_preferences_init (XfmpcPreferences *preferences)
+{
+  xfmpc_preferences_load (preferences);
+}
+
+static void
+xfmpc_preferences_finalize (GObject *object)
+{
+  (*G_OBJECT_CLASS (parent_class)->finalize) (object);
+}
+
+static void
+xfmpc_preferences_get_property (GObject *object,
+                                guint prop_id,
+                                GValue *value,
+                                GParamSpec *pspec)
+{
+  XfmpcPreferences *preferences = XFMPC_PREFERENCES (object);
+  GValue *src;
+
+  src = preferences->values + prop_id;
+  if (G_LIKELY (G_IS_VALUE (src)))
+    g_value_copy (src, value);
+  else
+    g_param_value_set_default (pspec, value);
+}
+
+static void
+xfmpc_preferences_set_property (GObject *object,
+                                guint prop_id,
+                                const GValue *value,
+                                GParamSpec *pspec)
+{
+  XfmpcPreferences *preferences = XFMPC_PREFERENCES (object);
+  GValue *dst;
+
+  dst = preferences->values + prop_id;
+  if (G_UNLIKELY (!G_IS_VALUE (dst)))
+    {
+      g_value_init (dst, pspec->value_type);
+      g_param_value_set_default (pspec, dst);
+    }
+
+  if (G_LIKELY (g_param_values_cmp (pspec, value, dst) != 0))
+    {
+      g_value_copy (value, dst);
+      xfmpc_preferences_store (preferences);
+    }
+}
+
+XfmpcPreferences *
+xfmpc_preferences_get ()
+{
+  static XfmpcPreferences *preferences = NULL;
+
+  if (G_UNLIKELY (NULL == preferences))
+    {
+      preferences = g_object_new (XFMPC_TYPE_PREFERENCES, NULL);
+      g_object_add_weak_pointer (G_OBJECT (preferences), (gpointer)&preferences);
+    }
+  else
+    g_object_ref (G_OBJECT (preferences));
+
+  return preferences;
+}
+
+static void
+xfmpc_preferences_load (XfmpcPreferences *preferences)
+{
+  const gchar          *string;
+  GParamSpec          **specs;
+  GParamSpec           *spec;
+  GValue                dst = { 0, };
+  GValue                src = { 0, };
+  XfceRc               *rc;
+  guint                 nspecs;
+  guint                 n;
+
+  rc = xfmpc_preferences_get_rc (preferences);
+  if (G_UNLIKELY (NULL == rc))
+    {
+      g_warning ("Failed to load the preferences");
+      return;
+    }
+
+  g_object_freeze_notify (G_OBJECT (preferences));
+
+  xfce_rc_set_group (rc, "Configuration");
+
+  specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (preferences), &nspecs);
+  for (n = 0; n < nspecs; ++n)
+    {
+      spec = specs[n];
+
+      string = xfce_rc_read_entry (rc, g_param_spec_get_nick (spec), NULL);
+
+      if (G_UNLIKELY (NULL == string))
+        continue;
+
+      g_value_init (&src, G_TYPE_STRING);
+      g_value_set_static_string (&src, string);
+
+      if (spec->value_type == G_TYPE_STRING)
+        g_object_set_property (G_OBJECT (preferences), spec->name, &src);
+      else if (g_value_type_transformable (G_TYPE_STRING, spec->value_type))
+        {
+          g_value_init (&dst, spec->value_type);
+          if (g_value_transform (&src, &dst))
+            g_object_set_property (G_OBJECT (preferences), spec->name, &dst);
+          g_value_unset (&dst);
+        }
+      else
+        g_warning ("Failed to load property \"%s\"", spec->name);
+
+      g_value_unset (&src);
+    }
+  g_free (specs);
+
+  g_object_thaw_notify (G_OBJECT (preferences));
+
+  xfce_rc_close (rc);
+}
+
+static void
+xfmpc_preferences_store (XfmpcPreferences *preferences)
+{
+  const gchar *string;
+  GParamSpec **specs;
+  GParamSpec  *spec;
+  GValue       dst = { 0, };
+  GValue       src = { 0, };
+  XfceRc      *rc;
+  guint        nspecs;
+  guint        n;
+
+  rc = xfmpc_preferences_get_rc (preferences);
+
+  if (G_UNLIKELY (NULL == rc))
+    {
+      g_warning ("Failed to save the preferences");
+      return;
+    }
+
+  xfce_rc_set_group (rc, "Configuration");
+
+  specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (preferences), &nspecs);
+  for (n = 0; n < nspecs; ++n)
+    {
+      spec = specs[n];
+
+      g_value_init (&dst, G_TYPE_STRING);
+
+      if (spec->value_type == G_TYPE_STRING)
+        g_object_get_property (G_OBJECT (preferences), spec->name, &dst);
+      else
+        {
+          g_value_init (&src, spec->value_type);
+          g_object_get_property (G_OBJECT (preferences), spec->name, &src);
+          g_value_transform (&src, &dst);
+          g_value_unset (&src);
+        }
+
+      string = g_value_get_string (&dst);
+
+      if (G_LIKELY (NULL != string))
+        xfce_rc_write_entry (rc, g_param_spec_get_nick (spec), string);
+
+      g_value_unset (&dst);
+    }
+  g_free (specs);
+
+  xfce_rc_close (rc);
+}
+
+static XfceRc *
+xfmpc_preferences_get_rc (XfmpcPreferences *preferences)
+{
+  return xfce_rc_config_open (XFCE_RESOURCE_CONFIG, "xfce4/xfmpcrc", FALSE);
+}
+

Added: xfmpc/trunk/src/preferences.h
===================================================================
--- xfmpc/trunk/src/preferences.h	                        (rev 0)
+++ xfmpc/trunk/src/preferences.h	2008-01-22 10:08:17 UTC (rev 3842)
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2008 Mike Massonnet <mmassonnet at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XFMPC_PREFERENCES_H__
+#define __XFMPC_PREFERENCES_H__
+
+G_BEGIN_DECLS
+
+#define XFMPC_TYPE_PREFERENCES              (xfmpc_preferences_get_type())
+
+#define XFMPC_PREFERENCES(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), XFMPC_TYPE_PREFERENCES, XfmpcPreferences))
+#define XFMPC_PREFERENCES_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), XFMPC_TYPE_PREFERENCES, XfmpcPreferencesClass))
+
+#define XFMPC_IS_PREFERENCES(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XFMPC_TYPE_PREFERENCES))
+#define XFMPC_IS_PREFERENCES_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), XFMPC_TYPE_PREFERENCES))
+
+#define XFMPC_PREFERENCES_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), XFMPC_TYPE_PREFERENCES, XfmpcPreferencesClass))
+
+typedef struct _XfmpcPreferencesClass       XfmpcPreferencesClass;
+typedef struct _XfmpcPreferences            XfmpcPreferences;
+typedef struct _XfmpcPreferencesPriv        XfmpcPreferencesPriv;
+
+GType                   xfmpc_preferences_get_type              () G_GNUC_CONST;
+
+XfmpcPreferences *      xfmpc_preferences_get                   ();
+
+G_END_DECLS
+
+#endif
+




More information about the Goodies-commits mailing list