#include #include #include #include #include #include #include #include "gnome-theme-tarball.h" /* Private Functions */ gboolean tarball_contains_file (gchar *tarball_uri, gchar *filename) { int status; char *command, *result; long matches; command = g_strdup_printf ("sh -c 'tar ztf \"%s\" | grep -c %s'", tarball_uri, filename); printf ("Command: %s\n", command); if (g_spawn_command_line_sync (command, &result, NULL, &status, NULL) && status== 0) { /* remove trailing white space */ if (result[strlen(result)-1] == '\n') result[strlen(result)-1] = 0; matches = strtol (result, NULL , 10); if (matches > 0) return TRUE; } return FALSE; } /* Public Functions */ GnomeThemeTarball * gnome_theme_tarball_new (void) { GnomeThemeTarball *theme_tarball; theme_tarball = g_new0 (GnomeThemeTarball, 1); return theme_tarball; } void gnome_theme_tarball_free (GnomeThemeTarball *theme_tarball) { g_free (theme_tarball->src_uri); g_free (theme_tarball->tmp_uri); g_free (theme_tarball->target_dir); g_free (theme_tarball); } void gnome_theme_tarball_get_type (GnomeThemeTarball *theme_tarball) { /* check for theme engine */ if (tarball_contains_file (theme_tarball->tmp_uri, "configure")) { theme_tarball->type = THEME_TARBALL_TYPE_ENGINE; } /* check for gtk/metacity theme */ else if (tarball_contains_file (theme_tarball->tmp_uri, "gtkrc") || tarball_contains_file (theme_tarball->tmp_uri, "metacity-theme-1.xml")) { theme_tarball->type = THEME_TARBALL_TYPE_STANDARD; theme_tarball->target_dir = g_build_filename (g_get_home_dir (), ".themes", NULL); } /* check for icon theme */ else if (tarball_contains_file (theme_tarball->tmp_uri, "index.theme")) { theme_tarball->type = THEME_TARBALL_TYPE_ICON; theme_tarball->target_dir = g_build_filename (g_get_home_dir (), ".icons", NULL); } /* unknown tarball type */ else { theme_tarball->type = THEME_TARBALL_TYPE_UNKNOWN; } } void gnome_theme_tarball_get_compression (GnomeThemeTarball *theme_tarball) { gchar *path = theme_tarball->src_uri; int len = strlen (path); if (path && len > 7 && !strcmp (path + len - 7, ".tar.gz")) { theme_tarball->compression = GZIP; } else if (path && len > 8 && !strcmp (path + len - 8, ".tar.bz2")) { theme_tarball->compression = BZIP2; } else { theme_tarball->compression = UNKNOWN; } } GnomeThemeTarball * gnome_theme_tarball_init (gchar *uri) { GnomeThemeTarball *theme_tarball = gnome_theme_tarball_new(); theme_tarball->src_uri = uri; gnome_theme_tarball_get_type (theme_tarball); gnome_theme_tarball_get_compression (theme_tarball); return theme_tarball; } void gnome_theme_tarball_install (GnomeThemeTarball *theme_tarball) { gchar *command = strdup(""); int status; theme_tarball->tmp_uri = g_build_filename (g_get_tmp_dir(), g_path_get_basename (theme_tarball->src_uri), NULL); /* transfer the tarball to temp dir */ printf ("Temp: %s\n", theme_tarball->tmp_uri); gnome_vfs_xfer_uri (gnome_vfs_uri_new (theme_tarball->src_uri), gnome_vfs_uri_new (theme_tarball->tmp_uri), GNOME_VFS_XFER_RECURSIVE, GNOME_VFS_XFER_ERROR_MODE_ABORT, GNOME_VFS_XFER_OVERWRITE_MODE_QUERY, NULL, NULL); /* determine the type of theme */ gnome_theme_tarball_get_type (theme_tarball); /* untar the tarball */ if (theme_tarball->compression == GZIP) { command = g_strdup_printf ("sh -c 'gzip -d -c < \"%s\" | tar xf - -C \"%s\"'", theme_tarball->tmp_uri, theme_tarball->target_dir); } else if (theme_tarball->compression == BZIP2) { command = g_strdup_printf ("sh -c 'bzip2 -d -c < \"%s\" | tar xf - -C \"%s\"'", theme_tarball->tmp_uri, theme_tarball->target_dir); } printf ("Command: %s\n", command); if (g_spawn_command_line_sync (command, NULL, NULL, &status, NULL) && status == 0) gnome_vfs_unlink (theme_tarball->tmp_uri); printf ("Theme Successfully Installed"); g_free (command); }