changeset 8843:1bbe99a07e36

[gaim-migrate @ 9610] Pekka Riikonen has provided us with a nice file selector request API. This is it. committer: Tailor Script <tailor@pidgin.im>
author Ethan Blanton <elb@pidgin.im>
date Fri, 30 Apr 2004 03:00:05 +0000
parents ad3633f92f91
children 76013c5ef153
files src/gtkrequest.c src/request.c src/request.h
diffstat 3 files changed, 111 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/gtkrequest.c	Thu Apr 29 02:42:31 2004 +0000
+++ b/src/gtkrequest.c	Fri Apr 30 03:00:05 2004 +0000
@@ -1358,12 +1358,77 @@
 	g_free(data);
 }
 
+static void
+file_ok_cb(GtkWidget *button, GaimGtkRequestData *data)
+{
+	const char *name;
+
+	if (!GTK_WIDGET_HAS_FOCUS(button))
+		gtk_widget_grab_focus(button);
+
+	if (data->cbs[0] != NULL) {
+		name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data->dialog));
+		if (gaim_gtk_check_if_dir(name, GTK_FILE_SELECTION(data->dialog)))
+			name = NULL;
+
+		((GaimRequestInputCb)data->cbs[0])(data->user_data, name);
+	}
+
+	gaim_request_close(GAIM_REQUEST_INPUT, data);
+}
+
+static void
+file_cancel_cb(GtkWidget *button, GaimGtkRequestData *data)
+{
+	if (data->cbs[1] != NULL)
+		((GaimRequestInputCb)data->cbs[1])(data->user_data, NULL);
+
+	gaim_request_close(GAIM_REQUEST_INPUT, data);
+}
+
+static void *
+gaim_gtk_request_file(const char *title, const char *filename,
+		      GCallback ok_cb, GCallback cancel_cb,
+		      void *user_data)
+{
+	GaimGtkRequestData *data;
+	GtkWidget *filesel;
+	char *cur_dir, *init_str, tmp[512];
+
+	data = g_new0(GaimGtkRequestData, 1);
+	data->type = GAIM_REQUEST_INPUT;
+	data->user_data = user_data;
+	data->cb_count = 2;
+	data->cbs = g_new0(GCallback, 2);
+	data->cbs[0] = ok_cb;
+	data->cbs[1] = cancel_cb;
+
+	filesel = gtk_file_selection_new(title ? title : _(""));
+	cur_dir = g_get_current_dir();
+	g_snprintf(tmp, sizeof(tmp), "%s" G_DIR_SEPARATOR_S, cur_dir);
+	init_str = g_build_filename(tmp, filename, NULL);
+	gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), init_str);
+	g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)),
+			 "delete_event", G_CALLBACK(file_cancel_cb), data);
+	g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),
+			 "clicked", G_CALLBACK(file_cancel_cb), data);
+	g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),
+			 "clicked", G_CALLBACK(file_ok_cb), data);
+	gtk_widget_show(filesel);
+	g_free(cur_dir);
+	g_free(init_str);
+
+	data->dialog = filesel;
+	return (void *)data;
+}
+
 static GaimRequestUiOps ops =
 {
 	gaim_gtk_request_input,
 	gaim_gtk_request_choice,
 	gaim_gtk_request_action,
 	gaim_gtk_request_fields,
+	gaim_gtk_request_file,
 	gaim_gtk_close_request
 };
 
--- a/src/request.c	Thu Apr 29 02:42:31 2004 +0000
+++ b/src/request.c	Fri Apr 30 03:00:05 2004 +0000
@@ -1330,6 +1330,31 @@
 	}
 }
 
+void *
+gaim_request_file(void *handle,
+		  const char *title, const char *filename,
+		  GCallback ok_cb, GCallback cancel_cb,
+		  void *user_data)
+{
+	GaimRequestUiOps *ops;
+
+	ops = gaim_request_get_ui_ops();
+
+	if (ops != NULL && ops->request_file != NULL) {
+		GaimRequestInfo *info;
+
+		info            = g_new0(GaimRequestInfo, 1);
+		info->type      = GAIM_REQUEST_INPUT;
+		info->handle    = handle;
+		info->ui_handle = ops->request_file(title, filename,
+						    ok_cb, cancel_cb, user_data);
+		handles = g_list_append(handles, info);
+		return info->ui_handle;
+	}
+
+	return NULL;
+}
+
 void
 gaim_request_set_ui_ops(GaimRequestUiOps *ops)
 {
--- a/src/request.h	Thu Apr 29 02:42:31 2004 +0000
+++ b/src/request.h	Fri Apr 30 03:00:05 2004 +0000
@@ -191,8 +191,10 @@
 							const char *cancel_text, GCallback cancel_cb,
 							void *user_data);
 
+	void *(*request_file)(const char *title, const char *filename,
+			      GCallback ok_cb, GCallback cancel_cb,
+			      void *user_data);
 	void (*close_request)(GaimRequestType type, void *ui_handle);
-
 } GaimRequestUiOps;
 
 typedef void (*GaimRequestInputCb)(void *, const char *);
@@ -1251,6 +1253,24 @@
 
 /*@}*/
 
+/**
+ * Displays file selector request dialog.  Returns the selected filename into
+ * the callback.
+ *
+ * @param handle      The plugin or connection handle.
+ * @param title       The title for the dialog (may be NULL)
+ * @param filename    The default filename (may be NULL)
+ * @param ok_cb       The callback for the OK button.
+ * @param cancel_cb   The callback for the cancel button.
+ * @param user_data   The data to pass to the callback.
+ *
+ * @return A UI-specific handle.
+ */
+void *gaim_request_file(void *handle, const char *title, const char *filename,
+			GCallback ok_cb, GCallback cancel_cb,
+			void *user_data);
+
+
 /**************************************************************************/
 /** @name UI Operations API                                               */
 /**************************************************************************/