~ubuntu-branches/debian/squeeze/galeon/squeeze

« back to all changes in this revision

Viewing changes to utils/gul-file-chooser.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Howard
  • Date: 2004-06-06 09:02:01 UTC
  • Revision ID: james.westby@ubuntu.com-20040606090201-yhx6ruhq8um7ggs2
Tags: upstream-1.3.15
ImportĀ upstreamĀ versionĀ 1.3.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2003 Marco Pesenti Gritti
 
3
 *  Copyright (C) 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
 
 
25
#include "gul-file-chooser.h"
 
26
#include "eel-gconf-extensions.h"
 
27
#include "gul-state.h"
 
28
#include "galeon-debug.h"
 
29
 
 
30
#include <gtk/gtkstock.h>
 
31
#include <libgnomevfs/gnome-vfs-utils.h>
 
32
 
 
33
#define GUL_FILE_CHOOSER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GUL_TYPE_FILE_CHOOSER, GulFileChooserPrivate))
 
34
 
 
35
struct GulFileChooserPrivate
 
36
{
 
37
        char *persist_key;
 
38
};
 
39
 
 
40
static void gul_file_chooser_class_init (GulFileChooserClass *klass);
 
41
static void gul_file_chooser_init               (GulFileChooser *dialog);
 
42
 
 
43
enum
 
44
{
 
45
        PROP_0,
 
46
        PROP_PERSIST_KEY
 
47
};
 
48
 
 
49
static GObjectClass *parent_class = NULL;
 
50
 
 
51
GType
 
52
gul_file_chooser_get_type (void)
 
53
{
 
54
        static GType type = 0;
 
55
 
 
56
        if (type == 0)
 
57
        {
 
58
                static const GTypeInfo our_info =
 
59
                {
 
60
                        sizeof (GulFileChooserClass),
 
61
                        NULL,
 
62
                        NULL,
 
63
                        (GClassInitFunc) gul_file_chooser_class_init,
 
64
                        NULL,
 
65
                        NULL,
 
66
                        sizeof (GulFileChooser),
 
67
                        0,
 
68
                        (GInstanceInitFunc) gul_file_chooser_init
 
69
                };
 
70
 
 
71
                type = g_type_register_static (GTK_TYPE_FILE_CHOOSER_DIALOG,
 
72
                                               "GulFileChooser",
 
73
                                               &our_info, 0);
 
74
        }
 
75
 
 
76
        return type;
 
77
}
 
78
 
 
79
static void
 
80
current_folder_changed_cb (GtkFileChooser *chooser, GulFileChooser *dialog)
 
81
{
 
82
        if (dialog->priv->persist_key != NULL)
 
83
        {
 
84
                char *dir;
 
85
                char *converted;
 
86
 
 
87
                dir = gtk_file_chooser_get_current_folder (chooser);
 
88
 
 
89
                converted = g_filename_to_utf8 (dir, -1, NULL, NULL, NULL);
 
90
                eel_gconf_set_string (dialog->priv->persist_key, converted);
 
91
 
 
92
                g_free (converted);
 
93
                g_free (dir);
 
94
        }
 
95
}
 
96
 
 
97
static void
 
98
gul_file_chooser_init (GulFileChooser *dialog)
 
99
{
 
100
        dialog->priv = GUL_FILE_CHOOSER_GET_PRIVATE (dialog);
 
101
 
 
102
        dialog->priv->persist_key = NULL;
 
103
}
 
104
 
 
105
static void
 
106
gul_file_chooser_finalize (GObject *object)
 
107
{
 
108
        GulFileChooser *dialog = GUL_FILE_CHOOSER (object);
 
109
 
 
110
        g_free (dialog->priv->persist_key);
 
111
 
 
112
        LOG ("GulFileChooser finalised")
 
113
 
 
114
        G_OBJECT_CLASS (parent_class)->finalize (object);
 
115
}
 
116
 
 
117
void
 
118
gul_file_chooser_set_persist_key (GulFileChooser *dialog, const char *key)
 
119
{
 
120
        char *dir, *expanded, *converted;
 
121
 
 
122
        g_return_if_fail (key != NULL && key[0] != '\0');
 
123
 
 
124
        dialog->priv->persist_key = g_strdup (key);
 
125
 
 
126
        dir = eel_gconf_get_string (key);
 
127
        if (dir != NULL)
 
128
        {
 
129
                converted = g_filename_from_utf8
 
130
                        (dir, -1, NULL, NULL, NULL);
 
131
 
 
132
                if (converted != NULL)
 
133
                {
 
134
                        expanded = gnome_vfs_expand_initial_tilde (converted);
 
135
 
 
136
                        gtk_file_chooser_set_current_folder
 
137
                                (GTK_FILE_CHOOSER (dialog), expanded);
 
138
 
 
139
                        g_free (expanded);
 
140
                        g_free (converted);
 
141
                }
 
142
 
 
143
                g_free (dir);
 
144
        }
 
145
 
 
146
        g_signal_connect (dialog, "current-folder-changed",
 
147
                          G_CALLBACK (current_folder_changed_cb), dialog);
 
148
}
 
149
 
 
150
const char *
 
151
gul_file_chooser_get_persist_key (GulFileChooser *dialog)
 
152
{
 
153
        g_return_val_if_fail (GUL_IS_FILE_CHOOSER (dialog), NULL);
 
154
 
 
155
        return dialog->priv->persist_key;
 
156
}
 
157
 
 
158
static void
 
159
gul_file_chooser_set_property (GObject *object,
 
160
                                guint prop_id,
 
161
                                const GValue *value,
 
162
                                GParamSpec *pspec)
 
163
{
 
164
        GulFileChooser *dialog = GUL_FILE_CHOOSER (object);
 
165
        
 
166
        switch (prop_id)
 
167
        {
 
168
                case PROP_PERSIST_KEY:
 
169
                        gul_file_chooser_set_persist_key (dialog, g_value_get_string (value));
 
170
                        break;
 
171
        }
 
172
}
 
173
 
 
174
static void
 
175
gul_file_chooser_get_property (GObject *object,
 
176
                                guint prop_id,
 
177
                                GValue *value,
 
178
                                GParamSpec *pspec)
 
179
{
 
180
        GulFileChooser *dialog = GUL_FILE_CHOOSER (object);
 
181
 
 
182
        switch (prop_id)
 
183
        {
 
184
                case PROP_PERSIST_KEY:
 
185
                        g_value_set_string (value, gul_file_chooser_get_persist_key (dialog));
 
186
                        break;
 
187
        }
 
188
}
 
189
 
 
190
static void
 
191
gul_file_chooser_class_init (GulFileChooserClass *klass)
 
192
{
 
193
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
194
 
 
195
        parent_class = g_type_class_peek_parent (klass);
 
196
 
 
197
        object_class->finalize = gul_file_chooser_finalize;
 
198
        object_class->get_property = gul_file_chooser_get_property;
 
199
        object_class->set_property = gul_file_chooser_set_property;
 
200
 
 
201
        g_object_class_install_property (object_class,
 
202
                                         PROP_PERSIST_KEY,
 
203
                                         g_param_spec_string ("persist-key",
 
204
                                                              "Persist Key",
 
205
                                                              "The gconf key to which to persist the selected directory",
 
206
                                                              NULL,
 
207
                                                              G_PARAM_READWRITE));
 
208
 
 
209
        g_type_class_add_private (object_class, sizeof (GulFileChooserPrivate));
 
210
}
 
211
 
 
212
GulFileChooser  *
 
213
gul_file_chooser_new (const char *title,
 
214
                       GtkWidget *parent,
 
215
                       GtkFileChooserAction action,
 
216
                       const char *persist_key)
 
217
{
 
218
        GulFileChooser *dialog;
 
219
 
 
220
        dialog = GUL_FILE_CHOOSER (g_object_new (GUL_TYPE_FILE_CHOOSER,
 
221
                                                  "title", title,
 
222
                                                  "action", action,
 
223
                                                  NULL));
 
224
 
 
225
        /* NOTE: We cannot set this property on object construction time.
 
226
         * This is because GtkFileChooserDialog overrides the gobject
 
227
         * constructor; the GtkFileChooser delegate will only be set
 
228
         * _after_ our instance_init and construct-param setters will have
 
229
         * run.
 
230
         */
 
231
        if (persist_key != NULL)
 
232
        {
 
233
                gul_file_chooser_set_persist_key (dialog, persist_key);
 
234
        }
 
235
 
 
236
        if (action == GTK_FILE_CHOOSER_ACTION_OPEN          ||
 
237
            action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
 
238
            action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
 
239
        {
 
240
                gtk_dialog_add_buttons (GTK_DIALOG (dialog),
 
241
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
242
                                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
 
243
                                        NULL);
 
244
                gtk_dialog_set_default_response (GTK_DIALOG (dialog),
 
245
                                                 GTK_RESPONSE_ACCEPT);
 
246
        }
 
247
        else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
 
248
        {
 
249
                gtk_dialog_add_buttons (GTK_DIALOG (dialog),
 
250
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
251
                                        GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
 
252
                                        NULL);
 
253
                gtk_dialog_set_default_response (GTK_DIALOG (dialog),
 
254
                                                 GTK_RESPONSE_ACCEPT);
 
255
        }
 
256
 
 
257
        if (parent != NULL)
 
258
        {
 
259
                gtk_window_set_transient_for (GTK_WINDOW (dialog),
 
260
                                              GTK_WINDOW (parent));
 
261
 
 
262
                gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
 
263
        }
 
264
 
 
265
        return dialog;
 
266
}