No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Chris Lahey <clahey@ximian.com>
18 *
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <glib/gi18n.h>
28 #include "e-util/e-util.h"
29 #include "e-util/e-util-private.h"
30
31 #include "gal-define-views-model.h"
32 #include "gal-view-instance-save-as-dialog.h"
33 #include "gal-view-new-dialog.h"
34
35 G_DEFINE_TYPE (GalViewInstanceSaveAsDialog, gal_view_instance_save_as_dialog, GTK_TYPE_DIALOG)
36
37 enum {
38 PROP_0,
39 PROP_INSTANCE
40 };
41
42 enum {
43 COL_GALVIEW_NAME,
44 COL_GALVIEW_DATA
45 };
46
47 /* Static functions */
48 static void
49 gal_view_instance_save_as_dialog_set_instance (GalViewInstanceSaveAsDialog *dialog,
50 GalViewInstance *instance)
51 {
52 gint i;
53 GtkListStore *store;
54 GtkCellRenderer *renderer;
55 dialog->instance = instance;
56
57 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
58
59 for (i = 0; i < instance->collection->view_count; i++) {
60 GalViewCollectionItem *item = instance->collection->view_data[i];
61 GtkTreeIter iter;
62 gchar *title = NULL;
63
64 /* hide built in views */
65 /*if (item->built_in == 1)
66 continue;*/
67
68 title = e_str_without_underscores (item->title);
69
70 gtk_list_store_append (store, &iter);
71 gtk_list_store_set (
72 store, &iter,
73 COL_GALVIEW_NAME, title,
74 COL_GALVIEW_DATA, item,
75 -1);
76
77 g_free (title);
78 }
79
80 gtk_tree_sortable_set_sort_column_id (
81 GTK_TREE_SORTABLE (store),
82 COL_GALVIEW_NAME, GTK_SORT_ASCENDING);
83
84 /* attaching treeview to model */
85 gtk_tree_view_set_model (dialog->treeview, GTK_TREE_MODEL (store));
86 gtk_tree_view_set_search_column (dialog->treeview, COL_GALVIEW_NAME);
87
88 dialog->model = GTK_TREE_MODEL (store);
89
90 renderer = gtk_cell_renderer_text_new ();
91
92 gtk_tree_view_insert_column_with_attributes (
93 dialog->treeview,
94 COL_GALVIEW_NAME, _("Name"),
95 renderer, "text", COL_GALVIEW_NAME,
96 NULL);
97
98 /* set sort column */
99 gtk_tree_sortable_set_sort_column_id (
100 GTK_TREE_SORTABLE (dialog->model),
101 COL_GALVIEW_NAME, GTK_SORT_ASCENDING);
102 }
103
104 static void
105 gvisad_setup_validate_button (GalViewInstanceSaveAsDialog *dialog)
106 {
107 if ((dialog->toggle == GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_CREATE
108 && g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (dialog->entry_create)), -1) > 0)
109 || dialog->toggle == GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_REPLACE) {
110 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
111 } else {
112 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
113 }
114 }
115
116 static void
117 gvisad_setup_radio_buttons (GalViewInstanceSaveAsDialog *dialog)
118 {
119 GtkWidget *widget;
120
121 widget = dialog->scrolledwindow;
122 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radiobutton_replace))) {
123 GtkTreeIter iter;
124 GtkTreeSelection *selection;
125
126 selection = gtk_tree_view_get_selection (dialog->treeview);
127 if (!gtk_tree_selection_get_selected (selection, &dialog->model, &iter)) {
128 if (gtk_tree_model_get_iter_first (dialog->model, &iter)) {
129 gtk_tree_selection_select_iter (selection, &iter);
130 }
131 }
132
133 gtk_widget_set_sensitive (widget, TRUE);
134 dialog->toggle = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_REPLACE;
135 } else {
136 gtk_widget_set_sensitive (widget, FALSE);
137 }
138
139 widget = dialog->entry_create;
140 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radiobutton_create))) {
141 gtk_widget_set_sensitive (widget, TRUE);
142 dialog->toggle = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_CREATE;
143 } else {
144 gtk_widget_set_sensitive (widget, FALSE);
145 }
146
147 gvisad_setup_validate_button (dialog);
148 }
149
150 static void
151 gvisad_radio_toggled (GtkWidget *widget,
152 GalViewInstanceSaveAsDialog *dialog)
153 {
154 gvisad_setup_radio_buttons (dialog);
155 }
156
157 static void
158 gvisad_entry_changed (GtkWidget *widget,
159 GalViewInstanceSaveAsDialog *dialog)
160 {
161 gvisad_setup_validate_button (dialog);
162 }
163
164 /* Method override implementations */
165 static void
166 gal_view_instance_save_as_dialog_set_property (GObject *object,
167 guint property_id,
168 const GValue *value,
169 GParamSpec *pspec)
170 {
171 GalViewInstanceSaveAsDialog *dialog;
172
173 dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
174
175 switch (property_id) {
176 case PROP_INSTANCE:
177 if (g_value_get_object (value))
178 gal_view_instance_save_as_dialog_set_instance (dialog, GAL_VIEW_INSTANCE (g_value_get_object (value)));
179 else
180 gal_view_instance_save_as_dialog_set_instance (dialog, NULL);
181 break;
182
183 default:
184 return;
185 }
186 }
187
188 static void
189 gal_view_instance_save_as_dialog_get_property (GObject *object,
190 guint property_id,
191 GValue *value,
192 GParamSpec *pspec)
193 {
194 GalViewInstanceSaveAsDialog *dialog;
195
196 dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
197
198 switch (property_id) {
199 case PROP_INSTANCE:
200 g_value_set_object (value, dialog->instance);
201 break;
202
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
205 break;
206 }
207 }
208
209 static void
210 gal_view_instance_save_as_dialog_dispose (GObject *object)
211 {
212 GalViewInstanceSaveAsDialog *gal_view_instance_save_as_dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
213
214 if (gal_view_instance_save_as_dialog->builder)
215 g_object_unref (gal_view_instance_save_as_dialog->builder);
216 gal_view_instance_save_as_dialog->builder = NULL;
217
218 /* Chain up to parent's dispose() method. */
219 G_OBJECT_CLASS (gal_view_instance_save_as_dialog_parent_class)->dispose (object);
220 }
221
222 /* Init functions */
223 static void
224 gal_view_instance_save_as_dialog_class_init (GalViewInstanceSaveAsDialogClass *class)
225 {
226 GObjectClass *object_class;
227
228 object_class = (GObjectClass *) class;
229
230 object_class->set_property = gal_view_instance_save_as_dialog_set_property;
231 object_class->get_property = gal_view_instance_save_as_dialog_get_property;
232 object_class->dispose = gal_view_instance_save_as_dialog_dispose;
233
234 g_object_class_install_property (
235 object_class,
236 PROP_INSTANCE,
237 g_param_spec_object (
238 "instance",
239 "Instance",
240 NULL,
241 GAL_VIEW_INSTANCE_TYPE,
242 G_PARAM_READWRITE));
243 }
244
245 static void
246 gal_view_instance_save_as_dialog_init (GalViewInstanceSaveAsDialog *dialog)
247 {
248 GtkWidget *content_area;
249 GtkWidget *widget;
250
251 dialog->instance = NULL;
252 dialog->model = NULL;
253 dialog->collection = NULL;
254
255 dialog->builder = gtk_builder_new ();
256 e_load_ui_builder_definition (
257 dialog->builder, "gal-view-instance-save-as-dialog.ui");
258
259 widget = e_builder_get_widget (dialog->builder, "vbox-top");
260 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
261 gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
262
263 /* TODO: add position/size saving/restoring */
264 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
265 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 360);
266
267 gtk_dialog_add_buttons (
268 GTK_DIALOG (dialog),
269 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
270 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
271 NULL);
272
273 dialog->scrolledwindow = e_builder_get_widget (dialog->builder, "scrolledwindow2");
274 dialog->treeview = GTK_TREE_VIEW (e_builder_get_widget (dialog->builder, "custom-replace"));
275 dialog->entry_create = e_builder_get_widget (dialog->builder, "entry-create");
276 dialog->radiobutton_replace = e_builder_get_widget (dialog->builder, "radiobutton-replace");
277 dialog->radiobutton_create = e_builder_get_widget (dialog->builder, "radiobutton-create");
278
279 gtk_tree_view_set_reorderable (GTK_TREE_VIEW (dialog->treeview), FALSE);
280 gtk_tree_view_set_headers_visible (dialog->treeview, FALSE);
281
282 g_signal_connect (
283 dialog->radiobutton_replace, "toggled",
284 G_CALLBACK (gvisad_radio_toggled), dialog);
285 g_signal_connect (
286 dialog->radiobutton_create, "toggled",
287 G_CALLBACK (gvisad_radio_toggled), dialog);
288 g_signal_connect (
289 dialog->entry_create, "changed",
290 G_CALLBACK (gvisad_entry_changed), dialog);
291
292 gvisad_setup_radio_buttons (dialog);
293 gvisad_setup_validate_button (dialog);
294
295 gtk_window_set_title (GTK_WINDOW (dialog), _("Save Current View"));
296 gtk_widget_show (GTK_WIDGET (dialog));
297 }
298
299 /* External methods */
300 /**
301 * gal_view_instance_save_as_dialog_new
302 *
303 * Returns a new dialog for defining views.
304 *
305 * Returns: The GalViewInstanceSaveAsDialog.
306 */
307 GtkWidget *
308 gal_view_instance_save_as_dialog_new (GalViewInstance *instance)
309 {
310 GtkWidget *widget = g_object_new (GAL_TYPE_VIEW_INSTANCE_SAVE_AS_DIALOG, NULL);
311 gal_view_instance_save_as_dialog_set_instance (GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (widget), instance);
312 return widget;
313 }
314
315 void
316 gal_view_instance_save_as_dialog_save (GalViewInstanceSaveAsDialog *dialog)
317 {
318 GalView *view = gal_view_instance_get_current_view (dialog->instance);
319 const gchar *title;
320 gint n;
321 const gchar *id = NULL;
322 GalViewCollectionItem *item;
323
324 view = gal_view_clone (view);
325 switch (dialog->toggle) {
326 case GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_REPLACE:
327 if (dialog->treeview) {
328 GtkTreeIter iter;
329 GtkTreeSelection *selection;
330
331 selection = gtk_tree_view_get_selection (dialog->treeview);
332 if (gtk_tree_selection_get_selected (selection, &dialog->model, &iter)) {
333 gtk_tree_model_get (dialog->model, &iter, COL_GALVIEW_DATA, &item, -1);
334
335 for (n = 0; n < dialog->instance->collection->view_count; n++) {
336 if (item == dialog->instance->collection->view_data[n]) {
337 id = gal_view_collection_set_nth_view (dialog->instance->collection, n, view);
338 gal_view_collection_save (dialog->instance->collection);
339 }
340 }
341 }
342
343 }
344 break;
345
346 case GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_CREATE:
347 if (dialog->entry_create && GTK_IS_ENTRY (dialog->entry_create)) {
348 title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_create));
349 id = gal_view_collection_append_with_title (dialog->instance->collection, title, view);
350 gal_view_collection_save (dialog->instance->collection);
351 }
352 break;
353 }
354
355 if (id) {
356 gal_view_instance_set_current_view_id (dialog->instance, id);
357 }
358 }