Ejemplo n.º 1
0
/* Properties dialog
 */
static void
save_macros_to_mateconf (MCData *mc)
{
    MCPrefsDialog *dialog;
    GtkTreeIter    iter;
    MateConfValue    *patterns;
    MateConfValue    *commands;
    GSList        *pattern_list = NULL;
    GSList        *command_list = NULL;
    MateConfClient   *client;

    dialog = &mc->prefs_dialog;

    if (!gtk_tree_model_get_iter_first  (GTK_TREE_MODEL (dialog->macros_store), &iter))
	return;

    patterns = mateconf_value_new (MATECONF_VALUE_LIST);
    mateconf_value_set_list_type (patterns, MATECONF_VALUE_STRING);

    commands = mateconf_value_new (MATECONF_VALUE_LIST);
    mateconf_value_set_list_type (commands, MATECONF_VALUE_STRING);

    do {
	char *pattern = NULL;
	char *command = NULL;

	gtk_tree_model_get (
		GTK_TREE_MODEL (dialog->macros_store), &iter,
		0, &pattern,
		1, &command,
		-1);

	pattern_list = g_slist_prepend (pattern_list,
					mateconf_value_new_from_string (MATECONF_VALUE_STRING, pattern, NULL));
	command_list = g_slist_prepend (command_list,
					mateconf_value_new_from_string (MATECONF_VALUE_STRING, command, NULL));
    } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (dialog->macros_store), &iter));

    pattern_list = g_slist_reverse (pattern_list);
    command_list = g_slist_reverse (command_list);

    mateconf_value_set_list_nocopy (patterns, pattern_list); pattern_list = NULL;
    mateconf_value_set_list_nocopy (commands, command_list); command_list = NULL;
    
    client = mateconf_client_get_default ();
    mateconf_client_set (client, "/apps/mini-commander/macro_patterns",
		    patterns, NULL);
    mateconf_client_set (client, "/apps/mini-commander/macro_commands",
		    commands, NULL);

    mateconf_value_free (patterns);
    mateconf_value_free (commands);
}
MateConfValue*
mateconf_value_new_list_from_string(MateConfValueType list_type,
                                  const gchar* str,
				  GError** err)
{
  int i, len;
  gboolean escaped, pending_chars;
  GString *string;
  MateConfValue* value;
  GSList *list;

  g_return_val_if_fail(list_type != MATECONF_VALUE_LIST, NULL);
  g_return_val_if_fail(list_type != MATECONF_VALUE_PAIR, NULL);

  if (!g_utf8_validate (str, -1, NULL))
    {
      g_set_error (err, MATECONF_ERROR,
                   MATECONF_ERROR_PARSE_ERROR,
                   _("Text contains invalid UTF-8"));
      return NULL;
    }
  
  if (str[0] != '[')
    {
      if (err)
	*err = mateconf_error_new(MATECONF_ERROR_PARSE_ERROR,
			       _("Didn't understand `%s' (list must start with a '[')"),
			       str);
      return NULL;
    }

  len = strlen(str);

  /* Note: by now len is sure to be 1 or larger, so len-1 will never be
   * negative */
  if (str[len-1] != ']')
    {
      if (err)
	*err = mateconf_error_new(MATECONF_ERROR_PARSE_ERROR,
			       _("Didn't understand `%s' (list must end with a ']')"),
			       str);
      return NULL;
    }

  if (strstr(str, "[]"))
    {
      value = mateconf_value_new(MATECONF_VALUE_LIST);
      mateconf_value_set_list_type(value, list_type);

      return value;
    }

  escaped = FALSE;
  pending_chars = FALSE;
  list = NULL;
  string = g_string_new(NULL);

  for (i = 1; str[i] != '\0'; i++)
    {
      if ( ! escaped &&
	  (str[i] == ',' ||
	   str[i] == ']'))
	{
	  MateConfValue* val;
	  val = mateconf_value_new_from_string(list_type, string->str, err);

	  if (err && *err != NULL)
	    {
	      /* Free values so far */
	      g_slist_foreach(list, (GFunc)mateconf_value_free, NULL);
	      g_slist_free(list);

	      g_string_free(string, TRUE);

	      return NULL;
	    }

	  g_string_assign(string, "");
	  list = g_slist_prepend(list, val);
	  if (str[i] == ']' &&
	      i != len-1)
	    {
	      /* Free values so far */
	      g_slist_foreach(list, (GFunc)mateconf_value_free, NULL);
	      g_slist_free(list);

	      g_string_free(string, TRUE);

	      if (err)
		*err = mateconf_error_new(MATECONF_ERROR_PARSE_ERROR,
				       _("Didn't understand `%s' (extra unescaped ']' found inside list)"),
				       str);
	      return NULL;
	    }
	  pending_chars = FALSE;
	}
      else if ( ! escaped && str[i] == '\\')
	{
	  escaped = TRUE;
	  pending_chars = TRUE;
	}
      else
	{
	  g_string_append_c(string, str[i]);
	  escaped = FALSE;
	  pending_chars = TRUE;
	}
    }

  g_string_free(string, TRUE);

  if (pending_chars)
    {
      /* Free values so far */
      g_slist_foreach(list, (GFunc)mateconf_value_free, NULL);
      g_slist_free(list);

      g_string_free(string, TRUE);

      if (err)
	*err = mateconf_error_new(MATECONF_ERROR_PARSE_ERROR,
			       _("Didn't understand `%s' (extra trailing characters)"),
			       str);
      return NULL;
    }

  /* inverse list as we were prepending to it */
  list = g_slist_reverse(list);

  value = mateconf_value_new(MATECONF_VALUE_LIST);
  mateconf_value_set_list_type(value, list_type);

  mateconf_value_set_list_nocopy(value, list);

  return value;
}