Author: kelnos
Date: 2008-04-28 01:29:25 +0000 (Mon, 28 Apr 2008)
New Revision: 26897

Modified:
   xfconf/trunk/common/xfconf-common-private.h
   xfconf/trunk/xfconf-query/main.c
Log:
add support for getting and setting array properties to xfconf-query


Modified: xfconf/trunk/common/xfconf-common-private.h
===================================================================
--- xfconf/trunk/common/xfconf-common-private.h 2008-04-28 01:29:06 UTC (rev 
26896)
+++ xfconf/trunk/common/xfconf-common-private.h 2008-04-28 01:29:25 UTC (rev 
26897)
@@ -20,6 +20,8 @@
 #ifndef __XFCONF_COMMON_PRIVATE_H__
 #define __XFCONF_COMMON_PRIVATE_H__
 
+#include <dbus/dbus-glib.h>
+
 #define XFCONF_TYPE_G_VALUE_ARRAY  (dbus_g_type_get_collection("GPtrArray", 
G_TYPE_VALUE))
 
 #endif  /* __XFCONF_COMMON_PRIVATE_H__ */

Modified: xfconf/trunk/xfconf-query/main.c
===================================================================
--- xfconf/trunk/xfconf-query/main.c    2008-04-28 01:29:06 UTC (rev 26896)
+++ xfconf/trunk/xfconf-query/main.c    2008-04-28 01:29:25 UTC (rev 26897)
@@ -1,5 +1,6 @@
 /*
  *  Copyright (c) 2008 Stephan Arts <[EMAIL PROTECTED]>
+ *  Copyright (c) 2008 Brian Tarricone <[EMAIL PROTECTED]>
  *
  *  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
@@ -39,6 +40,7 @@
 #endif
 
 #include "xfconf-gvaluefuncs.h"
+#include "xfconf-common-private.h"
 #include "xfconf/xfconf.h"
 
 static gboolean version = FALSE;
@@ -49,7 +51,7 @@
 static gchar *channel_name = NULL;
 static gchar *property_name = NULL;
 static gchar **set_value = NULL;
-static gchar *type = NULL;
+static gchar **type = NULL;
 
 static void
 xfconf_query_get_propname_size (gpointer key, gpointer value, gpointer 
user_data)
@@ -118,7 +120,7 @@
         N_("Create new entry"),
         NULL
     },
-    {    "type", 't', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &type,
+    {    "type", 't', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING_ARRAY, &type,
        N_("Specify the property value type"),
        NULL
     },
@@ -179,60 +181,192 @@
 
     if (property_name)
     {
-        prop_exists = xfconf_channel_has_property(channel, property_name);
-        if (prop_exists || (create && type))
+        /** Remove property */
+        if (remove)
         {
-            GValue value = {0, };
+            xfconf_channel_remove_property(channel, property_name);
+        }
+        /** Read value */
+        else if(set_value == NULL || set_value[0] == NULL)
+        {
+            GValue value = { 0, };
 
-            if(prop_exists)
-                xfconf_channel_get_property(channel, property_name, &value);
-
-            /** Remove property */
-            if (remove)
+            if(!xfconf_channel_get_property(channel, property_name, &value))
             {
-                g_value_unset(&value);
-                xfconf_channel_remove_property(channel, property_name);
+                g_print(_("Property \"%s\" doesn't exist on channel \"%s\"."),
+                        property_name, channel_name);
+                return 1;
             }
-            /** Read value */
-            else if(set_value == NULL)
+            
+            if(XFCONF_TYPE_G_VALUE_ARRAY != G_VALUE_TYPE(&value))
             {
-                const gchar *str_val = _xfconf_string_from_gvalue(&value);
-                g_print("%s\n", str_val);
+                gchar *str_val = _xfconf_string_from_gvalue(&value);
+                g_print("%s\n", str_val ? str_val : _("(unknown)"));
+                g_free(str_val);
                 g_value_unset(&value);
             }
-            /* Write value */
             else
             {
-                if(!prop_exists)
+                GPtrArray *arr = g_value_get_boxed(&value);
+                gint i;
+
+                g_print(_("Value is an array with %d items:\n\n"), arr->len);
+
+                for(i = 0; i < arr->len; ++i)
                 {
-                    GType gtype = _xfconf_gtype_from_string(type);
+                    GValue *item_value = g_ptr_array_index(arr, i);
 
-                    if(G_TYPE_NONE == gtype || G_TYPE_INVALID == gtype)
+                    if(item_value)
                     {
-                        g_printerr(_("Unable to convert \"%s\" to type\n"),
-                                   type);
-                        return 1;
+                        gchar *str_val = 
_xfconf_string_from_gvalue(item_value);
+                        g_print("%s\n", str_val ? str_val : _("(unknown)"));
+                        g_free(str_val);
                     }
+                }
+            }
+        }
+        /* Write value */
+        else {
+            GValue value = { 0, };
+            gint i;
 
-                    g_value_init(&value, gtype);
+            prop_exists = xfconf_channel_has_property(channel, property_name);
+            if(!prop_exists && !create)
+            {
+                g_printerr(_("Property \"%s\" does not exist on channel 
\"%s\".  If you would\n"
+                             "like to create a new property, use the --create 
option.\n"),
+                             property_name, channel_name);
+                return 1;
+            }
+
+            if(!prop_exists && (!type || !type[0]))
+            {
+                g_printerr(_("When creating a new property, you must specify 
the value type.\n"));
+                return 1;
+            }
+
+            if(prop_exists && !(type && type[0]))
+            {
+                /* only care what the existing type is if the user
+                 * didn't specify one */
+                if(!xfconf_channel_get_property(channel, property_name, 
&value))
+                {
+                    g_printerr(_("Failed to get existing type for value.\n"));
+                    return 1;
                 }
+            }
 
-                if(_xfconf_gvalue_from_string(&value, set_value[0]))
+            if(!set_value[1])
+            {
+                /* not an array */
+                GType gtype = G_TYPE_INVALID;
+
+                if(prop_exists)
+                    gtype = G_VALUE_TYPE(&value);
+
+                if(type && type[0])
+                    gtype = _xfconf_gtype_from_string(type[0]);
+
+                if(G_TYPE_INVALID == gtype || G_TYPE_NONE == gtype)
                 {
-                    xfconf_channel_set_property(channel, property_name, 
&value);
+                    g_printerr(_("Unable to determine type of value.\n"));
+                    return 1;
                 }
-                else
+
+                if(XFCONF_TYPE_G_VALUE_ARRAY == gtype)
                 {
-                    g_print(_("ERROR: Could not convert value\n"));
+                    g_printerr(_("You must specify a value type to change an 
array into a single value.\n"));
+                    return 1;
                 }
-                g_value_unset(&value);
+
+                if(G_VALUE_TYPE(&value))
+                    g_value_unset(&value);
+
+                g_value_init(&value, gtype);
+                if(!_xfconf_gvalue_from_string(&value, set_value[0]))
+                {
+                    g_printerr(_("Unable to convert \"%s\" to type \"%s\"\n"),
+                                   set_value[0], g_type_name(gtype));
+                    return 1;
+                }
+
+                if(!xfconf_channel_set_property(channel, property_name, 
&value))
+                {
+                    g_printerr(_("Failed to set property.\n"));
+                    return 1;
+                }
             }
+            else
+            {
+                /* array property */
+                GPtrArray *arr_old = NULL, *arr_new;
+                gint new_values = 0, new_types = 0;
+
+                for(new_values = 0; set_value[new_values]; ++new_values)
+                    ;
+                if(type && type[0])
+                {
+                    for(new_types = 0; type[new_types]; ++new_types)
+                        ;
+                }
+                else if(G_VALUE_TYPE(&value) == XFCONF_TYPE_G_VALUE_ARRAY)
+                {
+                    arr_old = g_value_get_boxed(&value);
+                    new_types = arr_old->len;
+                }
+
+                if(new_values != new_types)
+                {
+                    g_printerr(_("Have %d new values, but could only determine 
%d types.\n"),
+                               new_values, new_types);
+                    return 1;
+                }
+
+                arr_new = g_ptr_array_sized_new(new_values);
+
+                for(i = 0; set_value[i]; ++i) {
+                    GType gtype = G_TYPE_INVALID;
+                    GValue *value_new;
+
+                    if(arr_old)
+                    {
+                        GValue *value_old = g_ptr_array_index(arr_old, i);
+                        gtype = G_VALUE_TYPE(value_old);
+                    }
+                    else
+                        gtype = _xfconf_gtype_from_string(type[i]);
+
+                    if(G_TYPE_INVALID == gtype || G_TYPE_NONE == gtype)
+                    {
+                        g_printerr(_("Unable to determine type of value at 
index %d.\n"),
+                                   i);
+                        return 1;
+                    }
+
+                    value_new = g_new0(GValue, 1);
+                    g_value_init(value_new, gtype);
+                    if(!_xfconf_gvalue_from_string(value_new, set_value[i]))
+                    {
+                        g_printerr(_("Unable to convert \"%s\" to type 
\"%s\"\n"),
+                                   set_value[i], g_type_name(gtype));
+                        return 1;
+                    }
+                    g_ptr_array_add(arr_new, value_new);
+                }
+
+                if(G_VALUE_TYPE(&value))
+                    g_value_unset(&value);
+
+                g_value_init(&value, XFCONF_TYPE_G_VALUE_ARRAY);
+                g_value_set_boxed(&value, arr_new);
+
+                if(!xfconf_channel_set_property(channel, property_name, 
&value))
+                {
+                    g_printerr(_("Failed to set property.\n"));
+                    return 1;
+                }
+            }
         }
-        else
-        {
-            g_print(_("ERROR: Property '%s' missing\nfrom channel '%s'\n"), 
property_name, channel_name);
-            return 1;
-        }
     }
 
     if (list)

_______________________________________________
Xfce4-commits mailing list
[email protected]
http://foo-projects.org/mailman/listinfo/xfce4-commits

Reply via email to