static gboolean
write_theme_to_disk (GnomeThemeMetaInfo  *theme_info,
		     const gchar         *theme_name,
		     const gchar         *theme_description,
		     gboolean		  save_background,
		     GError             **error)
{
  gchar *dir, *theme_name_dir;
  GnomeVFSURI *uri;
  GnomeVFSURI *target_uri;
  GnomeVFSHandle *handle = NULL;
  GnomeVFSFileSize bytes_written;
  gchar *str, *current_background;
  GConfClient *client;
  const gchar *theme_header =
      "[Desktop Entry]\n"
      "Name=%s\n"
      "Type=X-GNOME-Metatheme\n"
      "Comment=%s\n"
      "\n"
      "[X-GNOME-Metatheme]\n"
      "GtkTheme=%s\n"
      "MetacityTheme=%s\n"
      "IconTheme=%s\n";

  theme_name_dir = str_remove_slash (theme_name);
  dir = g_build_filename (g_get_home_dir (), ".themes", theme_name_dir, "index.theme~", NULL);
  g_free (theme_name_dir);

  uri = gnome_vfs_uri_new (dir);
  dir [strlen (dir) - 1] = '\000';
  target_uri = gnome_vfs_uri_new (dir);
  g_free (dir);
  gnome_vfs_create_uri (&handle, uri, GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_WRITE, FALSE, 0644);

  gnome_vfs_truncate_handle (handle, 0);

  /* start making the theme file */
  str = g_strdup_printf (theme_header, theme_name, theme_description,
			 theme_info->gtk_theme_name,
			 theme_info->metacity_theme_name,
			 theme_info->icon_theme_name);
  gnome_vfs_write (handle, str, strlen (str), &bytes_written);
  g_free (str);

  if (theme_info->gtk_color_scheme) {
    gchar *a, *tmp;
    tmp = g_strdup (theme_info->gtk_color_scheme);
    for (a = tmp; *a != '\0'; a++)
      if (*a == '\n')
        *a = ',';
    str = g_strdup_printf ("GtkColorScheme=%s\n", tmp);
    gnome_vfs_write (handle, str, strlen (str), &bytes_written);
    g_free (str);
    g_free (tmp);
  }

  if (theme_info->cursor_theme_name) {
#ifdef HAVE_XCURSOR
    str = g_strdup_printf ("CursorTheme=%s\n"
                           "CursorSize=%i\n",
                           theme_info->cursor_theme_name,
                           theme_info->cursor_size);
#else
    str = g_strdup_printf ("CursorFont=%s\n", theme_info->cursor_theme_name);
#endif
    gnome_vfs_write (handle, str, strlen (str), &bytes_written);
    g_free (str);
  }

  if (save_background) {
    client = gconf_client_get_default ();
    current_background = gconf_client_get_string (client, BACKGROUND_KEY, NULL);

    if (current_background != NULL) {
      str = g_strdup_printf ("BackgroundImage=%s\n", current_background);

      gnome_vfs_write (handle, str, strlen (str), &bytes_written);

      g_free (current_background);
      g_free (str);
    }
    g_object_unref (client);
  }

  gnome_vfs_close (handle);

  gnome_vfs_move_uri (uri, target_uri, TRUE);
  gnome_vfs_uri_unref (uri);
  gnome_vfs_uri_unref (target_uri);

  return TRUE;
}
Esempio n. 2
0
gboolean
mn_vfs_write_entire_file_uri_safe (GnomeVFSURI *uri,
				   gsize file_size,
				   const char *file_contents,
				   unsigned int perms,
				   GError **err)
{
  GnomeVFSResult result;
  char *text_uri;
  GnomeVFSURI *tmp_uri;
  char *tmp_text_uri;
  GnomeVFSURI *old_uri;
  char *old_text_uri;
  gboolean status = FALSE;
  gboolean old_exists;

  g_return_val_if_fail(uri != NULL, FALSE);

  text_uri = gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_PASSWORD);
  tmp_uri = mn_vfs_uri_append_file_suffix(uri, ".tmp");
  tmp_text_uri = gnome_vfs_uri_to_string(tmp_uri, GNOME_VFS_URI_HIDE_PASSWORD);
  old_uri = mn_vfs_uri_append_file_suffix(uri, ".old");
  old_text_uri = gnome_vfs_uri_to_string(old_uri, GNOME_VFS_URI_HIDE_PASSWORD);

  if (mn_vfs_test(tmp_uri, G_FILE_TEST_EXISTS))
    {
      result = gnome_vfs_unlink_from_uri(tmp_uri);
      if (result != GNOME_VFS_OK)
	{
	  g_set_error(err, 0, 0, _("Unable to remove %s: %s."), tmp_text_uri, gnome_vfs_result_to_string(result));
	  goto end;
	}
    }

  result = mn_vfs_write_entire_file_uri(tmp_uri, file_size, file_contents, TRUE, perms);
  if (result != GNOME_VFS_OK)
    {
      g_set_error(err, 0, 0, _("Unable to write %s: %s."), tmp_text_uri, gnome_vfs_result_to_string(result));
      goto end;
    }

  old_exists = mn_vfs_test(uri, G_FILE_TEST_EXISTS);
  if (old_exists)
    {
      result = gnome_vfs_move_uri(uri, old_uri, TRUE);
      if (result != GNOME_VFS_OK)
	{
	  g_set_error(err, 0, 0, _("Unable to rename %s to %s: %s."), text_uri, old_text_uri, gnome_vfs_result_to_string(result));
	  goto end;
	}
    }

  result = gnome_vfs_move_uri(tmp_uri, uri, TRUE);
  if (result != GNOME_VFS_OK)
    {
      g_set_error(err, 0, 0, _("Unable to rename %s to %s: %s."), tmp_text_uri, text_uri, gnome_vfs_result_to_string(result));
      goto end;
    }

  if (old_exists)
    {
      GnomeVFSResult this_result;

      this_result = gnome_vfs_unlink_from_uri(old_uri);
      if (this_result != GNOME_VFS_OK) /* non fatal */
	g_warning(_("unable to delete %s: %s"), old_text_uri, gnome_vfs_result_to_string(this_result));
    }

  status = TRUE;		/* success */

 end:
  g_free(text_uri);
  gnome_vfs_uri_unref(tmp_uri);
  g_free(tmp_text_uri);
  gnome_vfs_uri_unref(old_uri);
  g_free(old_text_uri);

  return status;
}