changeset 17768:51f19b846fc9

Buttons to add and remove smiley themes.
author Gabriel Schulhof <nix@go-nix.ca>
date Mon, 28 May 2007 23:33:30 +0000
parents 043a1139d4e5
children 3223413a9c80
files pidgin/gtkprefs.c pidgin/gtkthemes.c
diffstat 2 files changed, 143 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtkprefs.c	Mon May 28 19:08:05 2007 +0000
+++ b/pidgin/gtkprefs.c	Mon May 28 23:33:30 2007 +0000
@@ -619,12 +619,95 @@
 	return ret;
 }
 
+static void request_theme_file_name_cb (gpointer data, char *theme_file_name)
+{
+	theme_install_theme (theme_file_name, NULL) ;
+}
+
+static void
+add_theme_button_clicked_cb(GtkWidget *button, gpointer null)
+{
+	purple_request_file (NULL, _("Install Theme"), NULL, FALSE, (GCallback)request_theme_file_name_cb, NULL, NULL, NULL, NULL, NULL) ;
+}
+
+static void
+remove_theme_button_clicked_cb(GtkWidget *button, GtkTreeView *tree_view)
+{
+	static GtkTreeModel *tm = NULL;
+	static GtkTreeSelection *sel = NULL;
+	static GtkTreeIter itr;
+	static char *theme_path = NULL, *theme_name = NULL, *slash_before_filename = NULL;
+
+	if ((tm = gtk_tree_view_get_model(tree_view)) == NULL) return;
+	if ((sel = gtk_tree_view_get_selection(tree_view)) == NULL) return;
+	if (!gtk_tree_selection_get_selected(sel, NULL, &itr)) return;
+
+	gtk_tree_model_get(tm, &itr, 2, &theme_path, 3, &theme_name, -1);
+
+	if (theme_name) {
+		if (strcmp(theme_name, "none")) {
+			if(theme_path) {
+				g_print ("g_strrstr(\"%s\", \"%s\")\n", theme_path, G_DIR_SEPARATOR_S);
+				if ((slash_before_filename = g_strrstr(theme_path, G_DIR_SEPARATOR_S)) != NULL) {
+					GtkTreeRowReference *theme_rowref;
+					char *dir_file = NULL;
+					GDir *theme_dir = NULL;
+
+					*slash_before_filename = 0;
+					if ((theme_dir = g_dir_open(theme_path, 0, NULL)) != NULL) {
+						while((dir_file = (char *)g_dir_read_name(theme_dir)) != NULL) {
+							if ((dir_file = g_strdup_printf ("%s%s%s", theme_path, G_DIR_SEPARATOR_S, dir_file)) != NULL) {
+								g_print("Deleting file \"%s\"\n", dir_file);
+#ifdef _WIN32
+								/* UNTESTED ! */
+								DeleteFile(dir_file);
+#else /* !_WIN32 */
+								unlink(dir_file);
+#endif /* _WIN32 */
+								g_free(dir_file);
+							}
+						}
+
+					g_dir_close(theme_dir);
+
+#ifdef _WIN32
+					/* UNTESTED ! */
+					RemoveDirectory(theme_path);
+#else /* !_WIN32 */
+					rmdir(theme_path);
+#endif /* _WIN32 */
+
+					/*
+					 * Currently theme_refresh_theme_list() is rigged to remove all smiley themes and re-probe them. This is fine,
+					 * but for the next minor version we should add a pidgin_themes_remove_smiley_theme(struct smiley_theme *theme)
+					 * to gtkthemes.[ch] which will remove an individual theme (IOW the core of the loop in
+					 * gtkthemes.c:pidgin_smiley_themes_destroy_all()).
+					 */
+					if (previous_smiley_row)
+						gtk_tree_row_reference_free(previous_smiley_row);
+					previous_smiley_row = NULL;
+					if ((theme_rowref = theme_refresh_theme_list()) != NULL)
+						gtk_tree_row_reference_free(theme_rowref);
+
+					} else g_print("Failed to open theme directory\n");
+				} else g_print("Couldn't chop theme file name from path\n");
+			} else g_print("Theme path is NULL\n");
+		} else g_print("Theme name is \"none\"\n");
+	} else g_print("Theme name is NULL!\n");
+
+	g_free(theme_name);
+	g_free(theme_path);
+}
+
 static GtkWidget *
 theme_page()
 {
+	GtkWidget *hbox_buttons;
+	GtkWidget *alignment;
 	GtkWidget *ret;
 	GtkWidget *sw;
 	GtkWidget *view;
+	GtkWidget *add_button, *remove_button;
 	GtkCellRenderer *rend;
 	GtkTreeViewColumn *col;
 	GtkTreeSelection *sel;
@@ -694,6 +777,24 @@
 		gtk_tree_path_free(path);
 	}
 
+	alignment = gtk_alignment_new(1.0, 0.5, 0.0, 1.0);
+	gtk_widget_show(alignment);
+	gtk_box_pack_start(GTK_BOX(ret), alignment, FALSE, TRUE, 0);
+
+	hbox_buttons = gtk_hbox_new(TRUE, PIDGIN_HIG_CAT_SPACE);
+	gtk_widget_show(hbox_buttons);
+	gtk_container_add(GTK_CONTAINER(alignment), hbox_buttons);
+
+	add_button = gtk_button_new_from_stock(GTK_STOCK_ADD);
+	gtk_widget_show(add_button);
+	gtk_box_pack_start(GTK_BOX(hbox_buttons), add_button, FALSE, TRUE, 0);
+	g_signal_connect(G_OBJECT(add_button), "clicked", (GCallback)add_theme_button_clicked_cb, NULL);
+
+	remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
+	gtk_widget_show(remove_button);
+	gtk_box_pack_start(GTK_BOX(hbox_buttons), remove_button, FALSE, TRUE, 0);
+	g_signal_connect(G_OBJECT(remove_button), "clicked", (GCallback)remove_theme_button_clicked_cb, view);
+
 	gtk_widget_show_all(ret);
 
 	pidgin_set_accessible_label (view, label);
--- a/pidgin/gtkthemes.c	Mon May 28 19:08:05 2007 +0000
+++ b/pidgin/gtkthemes.c	Mon May 28 23:33:30 2007 +0000
@@ -92,6 +92,29 @@
 	g_hash_table_destroy(already_freed);
 }
 
+static void pidgin_smiley_themes_destroy_all()
+{
+	static struct smiley_theme *theme = NULL;
+	GSList *iter = NULL;
+
+	for (iter = smiley_themes ; iter ; iter = iter->next) {
+		if ((theme = ((struct smiley_theme *)(iter->data))) != NULL) {
+			pidgin_themes_destroy_smiley_theme(theme);
+
+			g_free(theme->name);
+			g_free(theme->desc);
+			g_free(theme->author);
+			g_free(theme->icon);
+			g_free(theme->path);
+			g_free(theme);
+		}
+	}
+
+	if (smiley_themes)
+		g_slist_free(smiley_themes);
+	smiley_themes = NULL;
+}
+
 void pidgin_themes_load_smiley_theme(const char *file, gboolean load)
 {
 	FILE *f = g_fopen(file, "r");
@@ -253,10 +276,12 @@
 {
 	GDir *dir;
 	const gchar *file;
-	gchar *path;
+	gchar *path, *test_path;
 	int l;
+	char* probedirs[3];
 
-	char* probedirs[3];
+	pidgin_smiley_themes_destroy_all();
+
 	probedirs[0] = g_build_filename(DATADIR, "pixmaps", "pidgin", "emotes", NULL);
 	probedirs[1] = g_build_filename(purple_user_dir(), "smileys", NULL);
 	probedirs[2] = 0;
@@ -264,14 +289,20 @@
 		dir = g_dir_open(probedirs[l], 0, NULL);
 		if (dir) {
 			while ((file = g_dir_read_name(dir))) {
-				path = g_build_filename(probedirs[l], file, "theme", NULL);
+				test_path = g_build_filename(probedirs[l], file, NULL);
+				if (g_file_test(test_path, G_FILE_TEST_IS_DIR)) {
+					path = g_build_filename(probedirs[l], file, "theme", NULL);
 
-				/* Here we check to see that the theme has proper syntax.
-				 * We set the second argument to FALSE so that it doesn't load
-				 * the theme yet.
-				 */
-				pidgin_themes_load_smiley_theme(path, FALSE);
-				g_free(path);
+					/* Here we check to see that the theme has proper syntax.
+					 * We set the second argument to FALSE so that it doesn't load
+					 * the theme yet.
+					 *
+					 * Not quite ... we need to re-load the themes to enable theme removal
+					 */
+					pidgin_themes_load_smiley_theme(path, TRUE);
+					g_free(path);
+				}
+				g_free(test_path);
 			}
 			g_dir_close(dir);
 		} else if (l == 1) {
@@ -279,6 +310,8 @@
 		}
 		g_free(probedirs[l]);
 	}
+
+	current_smiley_theme = (struct smiley_theme *)(smiley_themes ? smiley_themes->data : NULL);
 }
 
 GSList *pidgin_themes_get_proto_smileys(const char *id) {