Esempio 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);
}
Esempio n. 2
0
/* load_history indicates whether the history list is being loaded at startup.
** If true then don't save the new entries with mateconf (since they are being read
** using mateconf
*/
void
append_history_entry(MCData *mcdata, const char * entry, gboolean load_history)
{
    MatePanelApplet *applet = mcdata->applet;
    MateConfValue *history;
    GSList *list = NULL;
    int pos, i;

    /* remove older dupes */
    for(pos = 0; pos <= MC_HISTORY_LIST_LENGTH - 1; pos++)
	{
	    if(exists_history_entry(pos) && strcmp(entry, history_command[pos]) == 0)
		/* dupe found */
		delete_history_entry(pos);
	}

    /* delete oldest entry */
    if(history_command[0] != NULL)
	free(history_command[0]);

    /* move entries */
    for(pos = 0; pos < MC_HISTORY_LIST_LENGTH - 1; pos++)
	{
	    history_command[pos] = history_command[pos+1];
	    /* printf("%s\n", history_command[pos]); */
	}

    /* append entry */
    history_command[MC_HISTORY_LIST_LENGTH - 1] = (char *)malloc(sizeof(char) * (strlen(entry) + 1));
    strcpy(history_command[MC_HISTORY_LIST_LENGTH - 1], entry);
    
    if (load_history)
    	return;

    /* If not writable, just keeps the history around for this session */
    if ( ! mc_key_writable (mcdata, "history"))
        return;
    	
    /* Save history - this seems like a waste to do it every time it's updated 
    ** but it doesn't seem to work when called on the destroy signal of the applet 
    */
    for(i = 0; i < MC_HISTORY_LIST_LENGTH; i++)
	{
	    MateConfValue *value_entry;
	    
	    value_entry = mateconf_value_new (MATECONF_VALUE_STRING);
	    if(exists_history_entry(i)) {
	    	mateconf_value_set_string (value_entry, (gchar *) get_history_entry(i));
	    	list = g_slist_append (list, value_entry);
	    }        
	    
	}

    history = mateconf_value_new (MATECONF_VALUE_LIST);
    if (list) {
    	mateconf_value_set_list_type (history, MATECONF_VALUE_STRING);
        mateconf_value_set_list (history, list);
        mate_panel_applet_mateconf_set_value (applet, "history", history, NULL);
    }
   
    while (list) {
    	MateConfValue *value = list->data;
    	mateconf_value_free (value);
    	list = g_slist_next (list);
    }
   
    mateconf_value_free (history);
    
}
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;
}