Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / lib / .svn / text-base / ephy-file-chooser.c.svn-base
blobe5e9b1250fb3a3f2113c16ab447c391114754019
1 /*
2  *  Copyright © 2003 Marco Pesenti Gritti
3  *  Copyright © 2003, 2004 Christian Persch
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *  $Id$
20  */
22 #include "config.h"
24 #include "ephy-file-chooser.h"
25 #include "ephy-file-helpers.h"
26 #include "eel-gconf-extensions.h"
27 #include "ephy-state.h"
28 #include "ephy-gui.h"
29 #include "ephy-debug.h"
30 #include "ephy-stock-icons.h"
32 #include <gtk/gtkstock.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34 #include <glib/gi18n.h>
36 #define EPHY_FILE_CHOOSER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_FILE_CHOOSER, EphyFileChooserPrivate))
38 struct _EphyFileChooserPrivate
40         char *persist_key;
43 static void ephy_file_chooser_class_init        (EphyFileChooserClass *klass);
44 static void ephy_file_chooser_init              (EphyFileChooser *dialog);
46 enum
48         PROP_0,
49         PROP_PERSIST_KEY
52 static GObjectClass *parent_class = NULL;
54 GType
55 ephy_file_chooser_get_type (void)
57         static GType type = 0;
59         if (G_UNLIKELY (type == 0))
60         {
61                 const GTypeInfo our_info =
62                 {
63                         sizeof (EphyFileChooserClass),
64                         NULL,
65                         NULL,
66                         (GClassInitFunc) ephy_file_chooser_class_init,
67                         NULL,
68                         NULL,
69                         sizeof (EphyFileChooser),
70                         0,
71                         (GInstanceInitFunc) ephy_file_chooser_init
72                 };
74                 type = g_type_register_static (GTK_TYPE_FILE_CHOOSER_DIALOG,
75                                                "EphyFileChooser",
76                                                &our_info, 0);
77         }
79         return type;
82 static void
83 current_folder_changed_cb (GtkFileChooser *chooser, EphyFileChooser *dialog)
85         if (dialog->priv->persist_key != NULL)
86         {
87                 char *dir;
89                 dir = gtk_file_chooser_get_current_folder (chooser);
91                 eel_gconf_set_path (dialog->priv->persist_key, dir);
93                 g_free (dir);
94         }
97 static void
98 ephy_file_chooser_init (EphyFileChooser *dialog)
100         dialog->priv = EPHY_FILE_CHOOSER_GET_PRIVATE (dialog);
103 static GObject *
104 ephy_file_chooser_constructor (GType type,
105                                guint n_construct_properties,
106                                GObjectConstructParam *construct_params)
109         GObject *object;
110         char *downloads_dir;
112         object = parent_class->constructor (type, n_construct_properties,
113                                             construct_params);
115         downloads_dir = ephy_file_get_downloads_dir ();
116         gtk_file_chooser_add_shortcut_folder
117                 (GTK_FILE_CHOOSER (object), downloads_dir, NULL);
118         g_free (downloads_dir);
120         gtk_window_set_icon_name (GTK_WINDOW (object), EPHY_STOCK_EPHY);
122         return object;
125 static void
126 ephy_file_chooser_finalize (GObject *object)
128         EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
130         g_free (dialog->priv->persist_key);
132         LOG ("EphyFileChooser finalised");
134         G_OBJECT_CLASS (parent_class)->finalize (object);
137 void
138 ephy_file_chooser_set_persist_key (EphyFileChooser *dialog, const char *key)
140         char *dir, *expanded, *converted;
142         g_return_if_fail (key != NULL && key[0] != '\0');
144         dialog->priv->persist_key = g_strdup (key);
146         dir = eel_gconf_get_string (key);
147         if (dir != NULL)
148         {
149                 converted = g_filename_from_utf8
150                         (dir, -1, NULL, NULL, NULL);
152                 if (converted != NULL)
153                 {
154                         expanded = gnome_vfs_expand_initial_tilde (converted);
156                         gtk_file_chooser_set_current_folder
157                                 (GTK_FILE_CHOOSER (dialog), expanded);
159                         g_free (expanded);
160                         g_free (converted);
161                 }
163                 g_free (dir);
164         }
166         g_signal_connect (dialog, "current-folder-changed",
167                           G_CALLBACK (current_folder_changed_cb), dialog);
170 const char *
171 ephy_file_chooser_get_persist_key (EphyFileChooser *dialog)
173         g_return_val_if_fail (EPHY_IS_FILE_CHOOSER (dialog), NULL);
175         return dialog->priv->persist_key;
178 GtkFileFilter *
179 ephy_file_chooser_add_pattern_filter (EphyFileChooser *dialog,
180                                       const char *title,
181                                       const char *first_pattern,
182                                       ...)
184         GtkFileFilter *filth;
185         va_list args;
186         const char *pattern;
188         filth = gtk_file_filter_new ();
190         va_start (args, first_pattern);
192         pattern = first_pattern;
193         while (pattern != NULL)
194         {
195                 gtk_file_filter_add_pattern (filth, pattern);
196                 pattern = va_arg (args, const char *);
197         }
198         va_end (args);
200         gtk_file_filter_set_name (filth, title);
202         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filth);
204         return filth;
207 GtkFileFilter *
208 ephy_file_chooser_add_mime_filter (EphyFileChooser *dialog,
209                                    const char *title,
210                                    const char *first_mimetype,
211                                    ...)
213         GtkFileFilter *filth;
214         va_list args;
215         const char *mimetype;
217         filth = gtk_file_filter_new ();
219         va_start (args, first_mimetype);
221         mimetype = first_mimetype;
222         while (mimetype != NULL)
223         {
224                 gtk_file_filter_add_mime_type (filth, mimetype);
225                 mimetype = va_arg (args, const char *);
226         }
227         va_end (args);
229         gtk_file_filter_set_name (filth, title);
231         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filth);
233         return filth;
236 static void
237 ephy_file_chooser_set_property (GObject *object,
238                                 guint prop_id,
239                                 const GValue *value,
240                                 GParamSpec *pspec)
242         EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
243         
244         switch (prop_id)
245         {
246                 case PROP_PERSIST_KEY:
247                         ephy_file_chooser_set_persist_key (dialog, g_value_get_string (value));
248                         break;
249         }
252 static void
253 ephy_file_chooser_get_property (GObject *object,
254                                 guint prop_id,
255                                 GValue *value,
256                                 GParamSpec *pspec)
258         EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
260         switch (prop_id)
261         {
262                 case PROP_PERSIST_KEY:
263                         g_value_set_string (value, ephy_file_chooser_get_persist_key (dialog));
264                         break;
265         }
268 static void
269 ephy_file_chooser_class_init (EphyFileChooserClass *klass)
271         GObjectClass *object_class = G_OBJECT_CLASS (klass);
273         parent_class = g_type_class_peek_parent (klass);
275         object_class->constructor = ephy_file_chooser_constructor;
276         object_class->finalize = ephy_file_chooser_finalize;
277         object_class->get_property = ephy_file_chooser_get_property;
278         object_class->set_property = ephy_file_chooser_set_property;
280         g_object_class_install_property (object_class,
281                                          PROP_PERSIST_KEY,
282                                          g_param_spec_string ("persist-key",
283                                                               "Persist Key",
284                                                               "The gconf key to which to persist the selected directory",
285                                                               NULL,
286                                                               G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
288         g_type_class_add_private (object_class, sizeof (EphyFileChooserPrivate));
291 EphyFileChooser *
292 ephy_file_chooser_new (const char *title,
293                        GtkWidget *parent,
294                        GtkFileChooserAction action,
295                        const char *persist_key,
296                        EphyFileFilterDefault default_filter)
298         EphyFileChooser *dialog;
299         GtkFileFilter *filter[EPHY_FILE_FILTER_LAST];
301         g_return_val_if_fail (default_filter >= 0 && default_filter <= EPHY_FILE_FILTER_LAST, NULL);
303         dialog = EPHY_FILE_CHOOSER (g_object_new (EPHY_TYPE_FILE_CHOOSER,
304                                                   "title", title,
305                                                   "action", action,
306                                                   NULL));
308         /* NOTE: We cannot set this property on object construction time.
309          * This is because GtkFileChooserDialog overrides the gobject
310          * constructor; the GtkFileChooser delegate will only be set
311          * _after_ our instance_init and construct-param setters will have
312          * run.
313          */
314         if (persist_key != NULL)
315         {
316                 ephy_file_chooser_set_persist_key (dialog, persist_key);
317         }
319         if (action == GTK_FILE_CHOOSER_ACTION_OPEN          ||
320             action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
321             action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
322         {
323                 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
324                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
325                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
326                                         NULL);
327                 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
328                                                  GTK_RESPONSE_ACCEPT);
329         }
330         else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
331         {
332                 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
333                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
334                                         GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
335                                         NULL);
336                 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
337                                                  GTK_RESPONSE_ACCEPT);
338         }
340         if (default_filter != EPHY_FILE_FILTER_NONE)
341         {
342                 filter[EPHY_FILE_FILTER_ALL_SUPPORTED] =
343                         ephy_file_chooser_add_mime_filter
344                                 (dialog,
345                                  _("All supported types"),
346                                  "text/html",
347                                  "application/xhtml+xml",
348                                  "text/xml",
349                                  "image/png",
350                                  "image/jpeg",
351                                  "image/gif",
352                                  NULL);
354                 filter[EPHY_FILE_FILTER_WEBPAGES] =
355                         ephy_file_chooser_add_mime_filter
356                                 (dialog, _("Web pages"),
357                                  "text/html",
358                                  "application/xhtml+xml",
359                                  "text/xml",
360                                  NULL);
362                 filter[EPHY_FILE_FILTER_IMAGES] =
363                         ephy_file_chooser_add_mime_filter
364                                 (dialog, _("Images"),
365                                  "image/png",
366                                  "image/jpeg",
367                                  "image/gif",
368                                  NULL);
370                 filter[EPHY_FILE_FILTER_ALL] =
371                         ephy_file_chooser_add_pattern_filter
372                                 (dialog, _("All files"), "*", NULL);
374                 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog),
375                                              filter[default_filter]);
376         }
378         if (parent != NULL)
379         {
380                 gtk_window_set_transient_for (GTK_WINDOW (dialog),
381                                               GTK_WINDOW (parent));
382                 gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parent)),
383                                              GTK_WINDOW (dialog));
384                 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
385         }
387         return dialog;