changeset 15848:695ef54aca48

merge of '36e65545c68cbe4b96476b945c9ae5fd60f34c9d' and '60ba6c5b6094dd6f1f6482c687d9d209856f770b'
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Tue, 20 Mar 2007 01:17:35 +0000
parents b6983e807711 (current diff) a2ab257116ce (diff)
children 3da9d5da9054
files doc/gaim-text.1.in
diffstat 4 files changed, 440 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/finch/libgnt/Makefile.am	Mon Mar 19 23:51:24 2007 +0000
+++ b/finch/libgnt/Makefile.am	Tue Mar 20 01:17:35 2007 +0000
@@ -17,6 +17,7 @@
 	gntcolors.c \
 	gntcombobox.c \
 	gntentry.c \
+	gntfilesel.c \
 	gntkeys.c \
 	gntlabel.c \
 	gntline.c \
@@ -41,6 +42,7 @@
 	gntcolors.h \
 	gntcombobox.h \
 	gntentry.h \
+	gntfilesel.h \
 	gntkeys.h \
 	gntlabel.h \
 	gntline.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/finch/libgnt/gntfilesel.c	Tue Mar 20 01:17:35 2007 +0000
@@ -0,0 +1,356 @@
+#include "gntbutton.h"
+#include "gntentry.h"
+#include "gntfilesel.h"
+#include "gntlabel.h"
+#include "gntmarshal.h"
+#include "gntstyle.h"
+#include "gnttree.h"
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <glob.h>
+
+enum
+{
+	SIG_FILE_SELECTED,
+	SIGS
+};
+
+static GntWindowClass *parent_class = NULL;
+static guint signals[SIGS] = { 0 };
+static void (*orig_map)(GntWidget *widget);
+
+static void
+gnt_file_sel_destroy(GntWidget *widget)
+{
+	GntFileSel *sel = GNT_FILE_SEL(widget);
+	g_free(sel->current);
+}
+
+static char *
+process_path(const char *path)
+{
+	char **splits = NULL;
+	int i, j;
+	char *str, *ret;
+
+	splits = g_strsplit(path, G_DIR_SEPARATOR_S, -1);
+	for (i = 0, j = 0; splits[i]; i++) {
+		if (strcmp(splits[i], ".") == 0) {
+		} else if (strcmp(splits[i], "..") == 0) {
+			if (j)
+				j--;
+		} else {
+			if (i != j) {
+				g_free(splits[j]);
+				splits[j] = splits[i];
+				splits[i] = NULL;
+			}
+			j++;
+		}
+	}
+	g_free(splits[j]);
+	splits[j] = NULL;
+	str = g_build_pathv(G_DIR_SEPARATOR_S, splits);
+	ret = g_strdup_printf(G_DIR_SEPARATOR_S "%s", str);
+	g_free(str);
+	g_strfreev(splits);
+	return ret;
+}
+
+static void
+update_location(GntFileSel *sel)
+{
+	char *old;
+	const char *tmp;
+	tmp = (const char*)gnt_tree_get_selection_data(GNT_TREE(sel->files));
+	old = g_strdup_printf("%s%s%s", sel->current, sel->current[1] ? G_DIR_SEPARATOR_S : "", tmp ? tmp : "");
+	gnt_entry_set_text(GNT_ENTRY(sel->location), old);
+	g_free(old);
+}
+
+static gboolean
+location_changed(GntFileSel *sel, GError **err)
+{
+	GDir *dir;
+	const char *str;
+
+	gnt_tree_remove_all(GNT_TREE(sel->dirs));
+	gnt_tree_remove_all(GNT_TREE(sel->files));
+	gnt_entry_set_text(GNT_ENTRY(sel->location), NULL);
+	if (sel->current == NULL) {
+		if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(sel), GNT_WIDGET_MAPPED))
+			gnt_widget_draw(GNT_WIDGET(sel));
+		return TRUE;
+	}
+	
+	dir = g_dir_open(sel->current, 0, err);
+	if (dir == NULL || *err) {
+		g_printerr("GntFileSel: error opening location %s (%s)\n",
+			sel->current, *err ? (*err)->message : "reason unknown");
+		return FALSE;
+	}
+
+	if (*sel->current != '\0' && strcmp(sel->current, G_DIR_SEPARATOR_S))
+		gnt_tree_add_row_after(GNT_TREE(sel->dirs), g_strdup(".."),
+				gnt_tree_create_row(GNT_TREE(sel->dirs), ".."), NULL, NULL);
+
+	while ((str = g_dir_read_name(dir)) != NULL) {
+		char *fp = g_build_filename(sel->current, str, NULL);
+		struct stat st;
+
+		if (stat(fp, &st)) {
+			g_printerr("Error stating location %s\n", fp);
+		} else {
+			if (S_ISDIR(st.st_mode))
+				gnt_tree_add_row_after(GNT_TREE(sel->dirs), g_strdup(str),
+						gnt_tree_create_row(GNT_TREE(sel->dirs), str), NULL, NULL);
+			else {
+				char size[128];
+				snprintf(size, sizeof(size), "%ld", (long)st.st_size);
+
+				gnt_tree_add_row_after(GNT_TREE(sel->files), g_strdup(str),
+						gnt_tree_create_row(GNT_TREE(sel->files), str, size, ""), NULL, NULL);
+			}
+		}
+		g_free(fp);
+	}
+	if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(sel), GNT_WIDGET_MAPPED))
+		gnt_widget_draw(GNT_WIDGET(sel));
+	return TRUE;
+}
+
+static gboolean
+dir_key_pressed(GntTree *tree, const char *key, GntFileSel *sel)
+{
+	if (strcmp(key, "\r") == 0) {
+		/* XXX: if we are moving up the tree, make sure the current node is selected after the redraw */
+		char *str = g_strdup(gnt_tree_get_selection_data(tree));
+		char *path = g_build_filename(sel->current, str, NULL);
+		char *dir = g_path_get_basename(sel->current);
+		if (!gnt_file_sel_set_current_location(sel, path)) {
+			gnt_tree_set_selected(tree, str);
+		} else if (strcmp(str, "..") == 0) {
+			gnt_tree_set_selected(tree, dir);
+		}
+		g_free(dir);
+		g_free(str);
+		g_free(path);
+		return TRUE;
+	}
+	return FALSE;
+}
+
+static gboolean
+location_key_pressed(GntTree *tree, const char *key, GntFileSel *sel)
+{
+	if (strcmp(key, "\r") == 0) {
+		int count;
+		glob_t gl;
+		char *path;
+		char *str;
+		struct stat st;
+		int glob_ret;
+
+		str = (char*)gnt_entry_get_text(GNT_ENTRY(sel->location));
+		if (*str == G_DIR_SEPARATOR)
+			path = g_strdup(str);
+		else
+			path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", sel->current, str);
+		str = process_path(path);
+		g_free(path);
+		path = str;
+
+		if (!stat(path, &st)) {
+			if (S_ISDIR(st.st_mode)) {
+				gnt_file_sel_set_current_location(sel, path);
+				goto success;
+			}
+		}
+
+		glob_ret = glob(path, GLOB_MARK, NULL, &gl);
+		if (!glob_ret) {  /* XXX: do something with the return value */
+			char *loc = g_path_get_dirname(gl.gl_pathv[0]);
+
+			stat(gl.gl_pathv[0], &st);
+			gnt_file_sel_set_current_location(sel, loc);  /* XXX: check the return value */
+			g_free(loc);
+			if (!S_ISDIR(st.st_mode)) {
+				gnt_tree_remove_all(GNT_TREE(sel->files));
+				for (count = 0; count < gl.gl_pathc; count++) {
+					char *tmp = process_path(gl.gl_pathv[count]);
+					loc = g_path_get_dirname(tmp);
+					if (g_utf8_collate(sel->current, loc) == 0) {
+						char *base = g_path_get_basename(tmp);
+						char size[128];
+						snprintf(size, sizeof(size), "%ld", (long)st.st_size);
+						gnt_tree_add_row_after(GNT_TREE(sel->files), base,
+								gnt_tree_create_row(GNT_TREE(sel->files), base, size, ""), NULL, NULL);
+					}
+					g_free(loc);
+					g_free(tmp);
+				}
+				gnt_widget_draw(sel->files);
+			}
+		} else {
+			gnt_tree_remove_all(GNT_TREE(sel->files));
+			gnt_widget_draw(sel->files);
+		}
+		globfree(&gl);
+success:
+		g_free(path);
+		return TRUE;
+	}
+	return FALSE;
+}
+
+static void
+file_sel_changed(GntWidget *widget, gpointer old, gpointer current, GntFileSel *sel)
+{
+	update_location(sel);
+}
+
+static void
+gnt_file_sel_map(GntWidget *widget)
+{
+	orig_map(widget);
+	update_location(GNT_FILE_SEL(widget));
+}
+
+static void
+gnt_file_sel_class_init(GntFileSelClass *klass)
+{
+	GntWidgetClass *kl = GNT_WIDGET_CLASS(klass);
+	parent_class = GNT_WINDOW_CLASS(klass);
+	kl->destroy = gnt_file_sel_destroy;
+	orig_map = kl->map;
+	kl->map = gnt_file_sel_map;
+
+	signals[SIG_FILE_SELECTED] = 
+		g_signal_new("file_selected",
+					 G_TYPE_FROM_CLASS(klass),
+					 G_SIGNAL_RUN_LAST,
+					 G_STRUCT_OFFSET(GntFileSelClass, file_selected),
+					 NULL, NULL,
+					 gnt_closure_marshal_VOID__STRING_STRING,
+					 G_TYPE_NONE, 0);
+	gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), GNT_BINDABLE_CLASS(klass));
+
+	GNTDEBUG;
+}
+
+static void
+gnt_file_sel_init(GTypeInstance *instance, gpointer class)
+{
+	GNTDEBUG;
+}
+
+/******************************************************************************
+ * GntFileSel API
+ *****************************************************************************/
+GType
+gnt_file_sel_get_gtype(void)
+{
+	static GType type = 0;
+
+	if(type == 0)
+	{
+		static const GTypeInfo info = {
+			sizeof(GntFileSelClass),
+			NULL,					/* base_init		*/
+			NULL,					/* base_finalize	*/
+			(GClassInitFunc)gnt_file_sel_class_init,
+			NULL,					/* class_finalize	*/
+			NULL,					/* class_data		*/
+			sizeof(GntFileSel),
+			0,						/* n_preallocs		*/
+			gnt_file_sel_init,			/* instance_init	*/
+			NULL
+		};
+
+		type = g_type_register_static(GNT_TYPE_WINDOW,
+									  "GntFileSel",
+									  &info, 0);
+	}
+
+	return type;
+}
+
+GntWidget *gnt_file_sel_new()
+{
+	GntWidget *widget = g_object_new(GNT_TYPE_FILE_SEL, NULL);
+	GntFileSel *sel = GNT_FILE_SEL(widget);
+	GntWidget *hbox, *vbox;
+
+	vbox = gnt_vbox_new(FALSE);
+	gnt_box_set_pad(GNT_BOX(vbox), 0);
+	gnt_box_set_alignment(GNT_BOX(vbox), GNT_ALIGN_LEFT);
+
+	/* The dir. and files list */
+	hbox = gnt_hbox_new(FALSE);
+	gnt_box_set_pad(GNT_BOX(hbox), 0);
+
+	sel->dirs = gnt_tree_new();
+	gnt_tree_set_compare_func(GNT_TREE(sel->dirs), (GCompareFunc)g_utf8_collate);
+	gnt_tree_set_hash_fns(GNT_TREE(sel->dirs), g_str_hash, g_str_equal, g_free);
+	gnt_tree_set_column_titles(GNT_TREE(sel->dirs), "Directories");
+	gnt_tree_set_show_title(GNT_TREE(sel->dirs), TRUE);
+	gnt_tree_set_col_width(GNT_TREE(sel->dirs), 0, 20);
+	g_signal_connect(G_OBJECT(sel->dirs), "key_pressed", G_CALLBACK(dir_key_pressed), sel);
+
+	sel->files = gnt_tree_new_with_columns(2);  /* Name, Size, Modified  */
+	gnt_tree_set_compare_func(GNT_TREE(sel->files), (GCompareFunc)g_utf8_collate);
+	/*gnt_tree_set_column_titles(GNT_TREE(sel->files), "Filename", "Size", "Modified");*/
+	gnt_tree_set_column_titles(GNT_TREE(sel->files), "Filename", "Size");
+	gnt_tree_set_show_title(GNT_TREE(sel->files), TRUE);
+	gnt_tree_set_col_width(GNT_TREE(sel->files), 0, 25);
+	gnt_tree_set_col_width(GNT_TREE(sel->files), 1, 10);
+	/*gnt_tree_set_col_width(GNT_TREE(sel->files), 2, 10);*/
+	g_signal_connect(G_OBJECT(sel->files), "selection_changed", G_CALLBACK(file_sel_changed), sel);
+
+	gnt_box_add_widget(GNT_BOX(hbox), sel->dirs);
+	gnt_box_add_widget(GNT_BOX(hbox), sel->files);
+	gnt_box_add_widget(GNT_BOX(vbox), hbox);
+
+	/* The location entry */
+	sel->location = gnt_entry_new(NULL);
+	gnt_box_add_widget(GNT_BOX(vbox), sel->location);
+	g_signal_connect(G_OBJECT(sel->location), "key_pressed", G_CALLBACK(location_key_pressed), sel);
+
+	/* The buttons */
+	hbox = gnt_hbox_new(FALSE);
+	sel->cancel = gnt_button_new("Cancel");
+	sel->select = gnt_button_new("Select");
+	gnt_box_add_widget(GNT_BOX(hbox), sel->cancel);
+	gnt_box_add_widget(GNT_BOX(hbox), sel->select);
+	gnt_box_add_widget(GNT_BOX(vbox), hbox);
+
+	gnt_box_add_widget(GNT_BOX(sel), vbox);
+	return widget;
+}
+
+gboolean gnt_file_sel_set_current_location(GntFileSel *sel, const char *path)
+{
+	char *old;
+	GError *error = NULL;
+	gboolean ret = TRUE;
+
+	old = sel->current;
+	sel->current = process_path(path);
+	if (!location_changed(sel, &error)) {
+		g_error_free(error);
+		error = NULL;
+		g_free(sel->current);
+		sel->current = old;
+		location_changed(sel, &error);
+		ret = FALSE;
+	} else
+		g_free(old);
+
+	update_location(sel);
+	return ret;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/finch/libgnt/gntfilesel.h	Tue Mar 20 01:17:35 2007 +0000
@@ -0,0 +1,64 @@
+#ifndef GNT_FILE_SEL_H
+#define GNT_FILE_SEL_H
+
+#include "gntwindow.h"
+#include "gnt.h"
+#include "gntcolors.h"
+#include "gntkeys.h"
+
+#define GNT_TYPE_FILE_SEL				(gnt_file_sel_get_gtype())
+#define GNT_FILE_SEL(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), GNT_TYPE_FILE_SEL, GntFileSel))
+#define GNT_FILE_SEL_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), GNT_TYPE_FILE_SEL, GntFileSelClass))
+#define GNT_IS_FILE_SEL(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), GNT_TYPE_FILE_SEL))
+#define GNT_IS_FILE_SEL_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), GNT_TYPE_FILE_SEL))
+#define GNT_FILE_SEL_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), GNT_TYPE_FILE_SEL, GntFileSelClass))
+
+#define GNT_FILE_SEL_FLAGS(obj)				(GNT_FILE_SEL(obj)->priv.flags)
+#define GNT_FILE_SEL_SET_FLAGS(obj, flags)		(GNT_FILE_SEL_FLAGS(obj) |= flags)
+#define GNT_FILE_SEL_UNSET_FLAGS(obj, flags)	(GNT_FILE_SEL_FLAGS(obj) &= ~(flags))
+
+typedef struct _GnFileSel			GntFileSel;
+typedef struct _GnFileSelPriv		GntFileSelPriv;
+typedef struct _GnFileSelClass		GntFileSelClass;
+
+struct _GnFileSel
+{
+	GntWindow parent;
+
+	GntWidget *dirs;     /* list of files */
+	GntWidget *files;    /* list of directories */
+	GntWidget *location; /* location entry */
+
+	GntWidget *select;   /* select button */
+	GntWidget *cancel;   /* cancel button */
+
+	char *current; /* Full path of the current location */
+	/* XXX: someone should make these useful */
+	gboolean must_exist; /* Make sure the selected file (the name entered in 'location') exists */
+	gboolean dirsonly;   /* Show only directories */
+};
+
+struct _GnFileSelClass
+{
+	GntWindowClass parent;
+
+	void (*file_selected)(GntFileSel *sel, const char *path, const char *filename);
+	void (*gnt_reserved1)(void);
+	void (*gnt_reserved2)(void);
+	void (*gnt_reserved3)(void);
+	void (*gnt_reserved4)(void);
+};
+
+G_BEGIN_DECLS
+
+GType gnt_file_sel_get_gtype(void);
+
+GntWidget *gnt_file_sel_new();
+
+gboolean gnt_file_sel_set_current_location(GntFileSel *sel, const char *path);
+
+const char *gnt_file_sel_get_current_location(GntFileSel *sel);
+
+G_END_DECLS
+
+#endif /* GNT_FILE_SEL_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/finch/libgnt/test/file.c	Tue Mar 20 01:17:35 2007 +0000
@@ -0,0 +1,18 @@
+#include "gnt.h"
+#include "gntfilesel.h"
+
+int main()
+{
+	freopen(".error", "w", stderr);
+	gnt_init();
+
+	GntWidget *w = gnt_file_sel_new();
+	gnt_file_sel_set_current_location(GNT_FILE_SEL(w), "/home/");
+	gnt_widget_show(w);
+
+	gnt_main();
+
+	gnt_quit();
+	return 0;
+}
+