Index: gnome-theme-installer.c
===================================================================
RCS file: /cvs/gnome/gnome-control-center/capplets/theme-switcher/gnome-theme-installer.c,v
retrieving revision 1.6
diff -u -r1.6 gnome-theme-installer.c
--- gnome-theme-installer.c	12 Feb 2004 14:22:57 -0000	1.6
+++ gnome-theme-installer.c	1 Jul 2004 22:05:33 -0000
@@ -18,6 +18,60 @@
 #include "file-transfer-dialog.h"
 #include "gnome-theme-installer.h"
 
+enum {
+	THEME_INVALID,
+	THEME_ICON,
+	THEME_GNOME
+};
+
+enum {
+	TARGZ,
+	TARBZ
+};
+
+typedef struct {
+	gint theme_type;
+	gint filetype;
+	gchar *filename;
+	gchar *target_dir;
+	gchar *theme_tmp_dir;
+} theme_propierties;
+
+
+static int
+file_theme_type(gchar *dir)
+{
+	gchar *file_contents;
+	gchar *filename = NULL;
+	gint file_size;
+	GPatternSpec *pattern;
+	char *uri;
+	GnomeVFSURI *src_uri;
+	
+	filename = g_strdup_printf("%s/index.theme",dir);
+	src_uri = gnome_vfs_uri_new (filename);
+	if (gnome_vfs_uri_exists(src_uri)) {
+		uri = gnome_vfs_get_uri_from_local_path(filename);
+		gnome_vfs_read_entire_file(uri,&file_size,&file_contents);
+		
+		pattern = g_pattern_spec_new("*[Icon Theme]*");
+		if (g_pattern_match_string(pattern,file_contents)) {
+			return THEME_ICON;
+		}
+		
+		pattern = g_pattern_spec_new("*[X-GNOME-Metatheme]*");
+		if (g_pattern_match_string(pattern,file_contents)) {
+			return THEME_GNOME;
+		}
+	}
+	
+	filename = g_strdup_printf("%s/gtk-2.0/gtk.rc",dir);
+	src_uri = gnome_vfs_uri_new (filename);
+	if (gnome_vfs_uri_exists(src_uri)) {
+		return THEME_GNOME;
+	}
+	return THEME_INVALID;
+}
 
 static void
 transfer_cancel_cb (GtkWidget *dlg, gchar *path)
@@ -42,15 +96,15 @@
 {
 	int status;
 	gchar *command;
-	gchar *path = data;
-
+	theme_propierties *theme_props = data;
+	
 	/* this should be something more clever and nonblocking */
-	command = g_strdup_printf ("sh -c 'gzip -d -c < \"%s\" | tar xf - -C \"%s/.themes\"'",
-				   path, g_get_home_dir ());
+	command = g_strdup_printf ("sh -c 'gzip -d -c < \"%s\" | tar xf - -C \"%s\"'",
+				   theme_props->filename, g_get_tmp_dir());
 	if (g_spawn_command_line_sync (command, NULL, NULL, &status, NULL) && status == 0)
-		gnome_vfs_unlink (path);
+		gnome_vfs_unlink (theme_props->filename);
 	g_free (command);
-	g_free (path);
+	g_free (theme_props);
 
 	return FALSE;
 }
@@ -71,15 +125,16 @@
 {
 	int status;
 	gchar *command;
-	gchar *path = data;
+	/* gchar *path = data; */
+	theme_propierties *theme_props = data;
 
 	/* this should be something more clever and nonblocking */
-	command = g_strdup_printf ("sh -c 'bzip2 -d -c < \"%s\" | tar xf - -C \"%s/.themes\"'",
-				   path, g_get_home_dir ());
+	command = g_strdup_printf ("sh -c 'bzip2 -d -c < \"%s\" | tar xf - -C \"%s\"'",
+				   theme_props->filename, g_get_tmp_dir());
 	if (g_spawn_command_line_sync (command, NULL, NULL, &status, NULL) && status == 0)
-		gnome_vfs_unlink (path);
+		gnome_vfs_unlink (theme_props->filename);
 	g_free (command);
-	g_free (path);
+	g_free (theme_props);
 
 	return FALSE;
 }
@@ -87,12 +142,92 @@
 static void
 transfer_done_cb (GtkWidget *dlg, gchar *path)
 {
+	gchar *command,**dir,*theme_dir,*first_line;
+	int status,theme_type;
+	theme_propierties *theme_props;
+	GnomeVFSURI *theme_source_dir, *theme_dest_dir;
+	
+	theme_props =g_new(theme_propierties,1);
 	int len = strlen (path);
-	if (path && len > 7 && !strcmp (path + len - 7, ".tar.gz"))
-		g_idle_add (transfer_done_targz_idle_cb, path);
-	if (path && len > 8 && !strcmp (path + len - 8, ".tar.bz2"))
-		g_idle_add (transfer_done_tarbz2_idle_cb, path);
+	if (path && len > 7 && !strcmp (path + len - 7, ".tar.gz")) {
+		command = g_strdup_printf ("sh -c 'gzip -d -c < \"%s\" | tar ft -  | head -1'",
+				path);
+		theme_props->filetype=TARGZ;
+	} else if (path && len > 8 && !strcmp (path + len - 8, ".tar.bz2")) {
+		command = g_strdup_printf ("sh -c 'bzip2 -d -c < \"%s\" | tar ft - | head -1'",
+				path);
+		theme_props->filetype=TARBZ;
+	} else {
+		GtkWidget *dialog;
+				
+        dialog = gtk_message_dialog_new (NULL,
+			  	       GTK_DIALOG_MODAL,
+				       GTK_MESSAGE_ERROR,
+				       GTK_BUTTONS_OK,
+				       _("The file format is invalid"));
+   	    gtk_dialog_run (GTK_DIALOG (dialog));
+   	    gtk_widget_destroy (dialog);			
+		return;	
+	}
 	gtk_widget_destroy (dlg);
+	
+	
+	/* Uncompress the file in the temp directory */
+	theme_props->filename=g_strdup(path);
+	if (theme_props->filetype == TARBZ )
+			g_idle_add(transfer_done_tarbz2_idle_cb ,theme_props);
+	if (theme_props->filetype == TARGZ )
+			g_idle_add(transfer_done_targz_idle_cb,theme_props);
+	
+	/* What type of theme is ? */	
+	if (g_spawn_command_line_sync (command, &first_line, NULL, &status, NULL) && status == 0) {
+		dir = g_strsplit(g_strchomp(first_line),"/",0);
+		theme_dir=g_build_filename(g_get_tmp_dir(),g_strescape(dir[0],NULL),NULL);
+		theme_props->theme_tmp_dir=g_strdup(theme_dir);
+		
+		theme_type = file_theme_type(theme_dir);
+		if (theme_type == THEME_ICON) {
+			theme_props->target_dir=g_strdup_printf("%s/.icons/%s",g_get_home_dir(),dir[0]);	
+		} else if (theme_type == THEME_GNOME) {
+			theme_props->target_dir = g_strdup_printf("%s/.themes/%s",g_get_home_dir(),dir[0]);		
+		} else {
+			GtkWidget *dialog;
+				
+        	dialog = gtk_message_dialog_new (NULL,
+			  	       GTK_DIALOG_MODAL,
+				       GTK_MESSAGE_ERROR,
+				       GTK_BUTTONS_OK,
+				       _("The file format is invalid"));
+   	    	gtk_dialog_run (GTK_DIALOG (dialog));
+   	    	gtk_widget_destroy (dialog);			
+			return;	
+		}
+		/* Move the Dir to the target dir */
+		theme_source_dir = gnome_vfs_uri_new (theme_props->theme_tmp_dir);
+		theme_dest_dir = gnome_vfs_uri_new (theme_props->target_dir);
+		
+		/* gnome_vfs_move(theme_props->theme_tmp_dir,theme_props->target_dir,TRUE); */
+		if (gnome_vfs_xfer_uri (theme_source_dir,theme_dest_dir,
+								GNOME_VFS_XFER_DELETE_ITEMS,
+								GNOME_VFS_XFER_ERROR_MODE_ABORT,
+								GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
+								NULL,NULL) != GNOME_VFS_OK) {
+			g_warning ("Failed to move the dir");
+		} else {
+			GtkWidget *dialog;
+				
+        	dialog = gtk_message_dialog_new (NULL,
+			  	       GTK_DIALOG_MODAL,
+				       GTK_MESSAGE_INFO,
+				       GTK_BUTTONS_OK,
+				       _("Theme install correct"));
+   	    	gtk_dialog_run (GTK_DIALOG (dialog));
+   	    	gtk_widget_destroy (dialog);			
+			return;	
+		}
+		
+	}
+	
 }
 
 static void
@@ -152,11 +287,9 @@
 		src_uri = gnome_vfs_uri_new (filename);
 		base = gnome_vfs_uri_extract_short_name (src_uri);
 		src = g_list_append (NULL, src_uri);
-		if (icon_theme)
-			path = g_build_filename (g_get_home_dir (), ".icons", base, NULL);
-		else
-			path = g_build_filename (g_get_home_dir (), ".themes", base, NULL);
-
+		
+		path = g_build_filename(g_get_tmp_dir(),base,NULL); 
+		
 		/* To avoid the copy of /root/.themes to /root/.themes/.themes 
 		 * which causes an infinite loop. The user asks to transfer the all 
          * contents of a folder, to a folder under itseld. So ignore the 		
@@ -196,6 +329,7 @@
 		gnome_vfs_uri_list_unref (target);
 		g_free (base);
 		g_free (filename);
+		g_message("Path: %s\n",path);
 		g_signal_connect (G_OBJECT (dlg), "cancel",
 				  G_CALLBACK (transfer_cancel_cb), path);
 		g_signal_connect (G_OBJECT (dlg), "done",
