ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gtkbfc/gtkbfc.c
Revision: 1.16
Committed: Sat Aug 18 14:26:44 2007 UTC (16 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /* this file is publiched under the gnu gpl license, version 3 or any later */
2
3 #define _GNU_SOURCE
4
5 #include <dlfcn.h>
6 #include <glib.h>
7 #include <gtk/gtk.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14
15 static const char version[] = "gtkbfc version 0.0";
16
17 static char *helper = "/etc/gtkbfc-helper";
18
19 static GObject *(*old_constructor) (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties);
20
21 static gchar *last_path;
22
23 #if 0
24 # define DEBUG(stmt) stmt;
25 #else
26 # define DEBUG(stmt)
27 #endif
28
29 /////////////////////////////////////////////////////////////////////////////
30
31 gchar *gtk_file_chooser_get_filename (GtkFileChooser *chooser)
32 {
33 DEBUG ((printf ("gtk_file_chooser_get_filename<%s>\n", last_path)))
34 return g_strdup (last_path ? last_path : "/nonex1st4nt");
35 }
36
37 gboolean gtk_file_chooser_select_filename (GtkFileChooser *chooser, const char *filename)
38 {
39 return 1;
40 }
41
42 void gtk_file_chooser_unselect_all (GtkFileChooser *chooser)
43 {
44 }
45
46 gboolean gtk_file_chooser_set_filename (GtkFileChooser *chooser, const char *filename)
47 {
48 return 1;
49 }
50
51 void gtk_file_chooser_set_current_name (GtkFileChooser *chooser, const char *filename)
52 {
53 }
54
55 GSList *gtk_file_chooser_get_filenames (GtkFileChooser *chooser)
56 {
57 GSList *rv = 0;
58 rv = g_slist_prepend (rv, gtk_file_chooser_get_filename (chooser));
59 return rv;
60 }
61
62 gboolean gtk_file_chooser_set_current_folder (GtkFileChooser *chooser, const gchar *folder)
63 {
64 return 1;
65 }
66
67 gchar * gtk_file_chooser_get_current_folder (GtkFileChooser *chooser)
68 {
69 DEBUG ((printf ("gtk_file_chooser_get_current_folder\n")))
70 return g_strdup ("/tmp/gtkbfc");
71 }
72
73 /////////////////////////////////////////////////////////////////////////////
74
75 static const gchar *
76 from_uri (const gchar *uri)
77 {
78 const gchar *rv = uri;
79
80 if (uri && uri == strstr (uri, "file:///"))
81 rv = uri + 7;
82
83 return rv;
84 }
85
86 static gchar *
87 to_uri (gchar *file)
88 {
89 gchar *rv = NULL;
90
91 if (file)
92 {
93 GString *str = g_string_new ("file://");
94
95 str = g_string_append (str, file);
96 rv = g_strdup (str->str);
97 g_string_free (str, TRUE);
98 g_free (file);
99 }
100
101 return rv;
102 }
103
104 static GSList *
105 to_uris (GSList *files)
106 {
107 GSList *item = files;
108
109 for (; item; item = g_slist_next (item))
110 {
111 GString *str = g_string_new ("file://");
112 gchar *cur = item->data;
113
114 str = g_string_append (str, cur);
115 item->data = g_string_free (str, FALSE);
116 g_free (cur);
117 }
118
119 return files;
120 }
121
122 gchar *gtk_file_chooser_get_uri (GtkFileChooser *chooser)
123 {
124 gchar *uri = to_uri(gtk_file_chooser_get_filename (chooser));
125 DEBUG ((printf ("gtk_file_chooser_get_uri<%s>\n", uri)));
126 return uri;
127 }
128
129 gboolean gtk_file_chooser_set_uri (GtkFileChooser *chooser, const char *uri)
130 {
131 return gtk_file_chooser_set_filename (chooser, from_uri (uri));
132 }
133
134 GSList *gtk_file_chooser_get_uris (GtkFileChooser *chooser)
135 {
136 return to_uris (gtk_file_chooser_get_filenames (chooser));
137 }
138
139 gboolean gtk_file_chooser_set_current_folder_uri(GtkFileChooser *chooser, const gchar *uri)
140 {
141 return gtk_file_chooser_set_current_folder (chooser, from_uri (uri));
142 }
143
144 gchar *gtk_file_chooser_get_current_folder_uri (GtkFileChooser *chooser)
145 {
146 return to_uri (gtk_file_chooser_get_current_folder (chooser));
147 }
148
149 /////////////////////////////////////////////////////////////////////////////
150
151 static gboolean disable_cb (gpointer data)
152 {
153 ((void (*)(void *, const char *)) dlsym (RTLD_NEXT, "gtk_file_chooser_set_filename"))(GTK_FILE_CHOOSER (data), "/tmp/gtkbfc/empty");
154
155 GtkWidget *toplevel = gtk_widget_get_toplevel (data);
156 if (toplevel) gtk_widget_set_sensitive (toplevel, 0);
157
158 gtk_widget_unref (data);
159
160 return FALSE;
161 }
162
163 static gboolean read_result (GIOChannel *source, GIOCondition condition, gpointer data)
164 {
165 if (last_path)
166 {
167 g_free (last_path);
168 last_path = 0;
169 }
170
171 g_io_channel_read_to_end (source, &last_path, 0, 0);
172 g_io_channel_unref (source);
173
174 GtkWidget *toplevel = gtk_widget_get_toplevel (data);
175 if (toplevel) gtk_widget_set_sensitive (toplevel, 1);
176
177 if (!last_path || *last_path != '/')
178 {
179 DEBUG ((printf ("read_result cancel\n")))
180 g_free (last_path);
181 last_path = 0;
182
183 if (toplevel)
184 {
185 GdkEvent ev = { GDK_DELETE };
186 g_signal_emit_by_name (toplevel, "delete-event", &ev);
187 }
188 }
189 else
190 {
191 DEBUG ((printf ("read_result<%s>\n", last_path)))
192 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (data), FALSE);
193 g_signal_emit_by_name (GTK_FILE_CHOOSER (data), "file-activated");
194 }
195
196 gtk_widget_unref (data);
197
198 return FALSE;
199 }
200
201 static gboolean map_cb (GtkWidget *widget, gpointer user_data)
202 {
203 gchar *argv[10];
204 gboolean save = 0;
205 char xid [40];
206
207 argv [0] = helper;
208
209 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (widget)))
210 {
211 case GTK_FILE_CHOOSER_ACTION_SAVE: argv [1] = "save "; save = 1;break;
212 case GTK_FILE_CHOOSER_ACTION_OPEN: argv [1] = "load "; break;
213 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER: argv [1] = "mkdir "; save = 1; break;
214 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: argv [1] = "cd "; break;
215 default: argv [1] = "<unknown action> "; break;
216 }
217
218 GdkWindow *w = gtk_widget_get_parent_window (GTK_WIDGET (widget));
219
220 snprintf (xid, 80, "%lu", (unsigned long)(w ? gdk_x11_drawable_get_xid (w) : 0));
221 argv [2] = xid;
222 argv [3] = 0;
223
224 mkdir ("/tmp/gtkbfc", 0777);
225 close (open ("/tmp/gtkbfc/empty", O_WRONLY|O_CREAT|O_TRUNC, 0666));
226
227 ((void (*)(void *, const char *)) dlsym (RTLD_NEXT, "gtk_file_chooser_set_filename"))(GTK_FILE_CHOOSER (widget), "/tmp/gtkbfc/empty");
228
229 gint stdout_fd = -1;
230
231 gtk_widget_ref (widget); disable_cb (widget);
232
233 if (g_spawn_async_with_pipes (0, argv, 0, 0, 0, 0, 0, 0, &stdout_fd, 0, 0))
234 {
235 GIOChannel *channel = g_io_channel_unix_new (stdout_fd);
236
237 gtk_widget_ref (widget); g_io_add_watch (channel, G_IO_IN | G_IO_ERR | G_IO_HUP, read_result, widget);
238
239 // disable again in an idle handler, to get around race conditions
240 gtk_widget_ref (widget); g_idle_add (disable_cb, widget);
241 }
242 }
243
244 static GObject *new_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties)
245 {
246 GObject *ob = old_constructor (type, n_construct_properties, construct_properties);
247 g_signal_connect_after (ob, "map", G_CALLBACK (map_cb), 0);
248 return ob;
249 }
250
251 __attribute__ ((constructor))
252 static void init ()
253 {
254 g_type_init ();
255
256 GType type = GTK_TYPE_FILE_CHOOSER_WIDGET;
257 GObjectClass *klass = g_type_class_ref (type);
258
259 old_constructor = klass->constructor;
260 klass->constructor = new_constructor;
261 }
262