Updated Spanish translation
[evolution.git] / e-util / gal-view-instance-save-as-dialog.c
blobc29d719a56cd62e11c1fd0bfac572df0c760b0d9
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9 * for more details.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 * Authors:
16 * Chris Lahey <clahey@ximian.com>
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include "gal-view-instance-save-as-dialog.h"
28 #include <glib/gi18n.h>
30 #include "e-misc-utils.h"
31 #include "e-util-private.h"
33 G_DEFINE_TYPE (GalViewInstanceSaveAsDialog, gal_view_instance_save_as_dialog, GTK_TYPE_DIALOG)
35 enum {
36 PROP_0,
37 PROP_INSTANCE
40 enum {
41 COL_GALVIEW_NAME,
42 COL_GALVIEW_DATA
45 /* Static functions */
46 static void
47 gal_view_instance_save_as_dialog_set_instance (GalViewInstanceSaveAsDialog *dialog,
48 GalViewInstance *instance)
50 GtkListStore *store;
51 GtkCellRenderer *renderer;
52 gint ii, view_count;
54 dialog->instance = instance;
56 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
58 view_count = gal_view_collection_get_count (instance->collection);
60 for (ii = 0; ii < view_count; ii++) {
61 GalViewCollectionItem *item;
62 GtkTreeIter iter;
63 gchar *title = NULL;
65 item = gal_view_collection_get_view_item (
66 instance->collection, ii);
68 title = e_str_without_underscores (item->title);
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);
77 g_free (title);
80 gtk_tree_sortable_set_sort_column_id (
81 GTK_TREE_SORTABLE (store),
82 COL_GALVIEW_NAME, GTK_SORT_ASCENDING);
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);
88 dialog->model = GTK_TREE_MODEL (store);
90 renderer = gtk_cell_renderer_text_new ();
92 gtk_tree_view_insert_column_with_attributes (
93 dialog->treeview,
94 COL_GALVIEW_NAME, _("Name"),
95 renderer, "text", COL_GALVIEW_NAME,
96 NULL);
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);
104 static void
105 gvisad_setup_validate_button (GalViewInstanceSaveAsDialog *dialog)
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);
116 static void
117 gvisad_setup_radio_buttons (GalViewInstanceSaveAsDialog *dialog)
119 GtkWidget *widget;
121 widget = dialog->scrolledwindow;
122 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radiobutton_replace))) {
123 GtkTreeIter iter;
124 GtkTreeSelection *selection;
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);
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);
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);
147 gvisad_setup_validate_button (dialog);
150 static void
151 gvisad_radio_toggled (GtkWidget *widget,
152 GalViewInstanceSaveAsDialog *dialog)
154 gvisad_setup_radio_buttons (dialog);
157 static void
158 gvisad_entry_changed (GtkWidget *widget,
159 GalViewInstanceSaveAsDialog *dialog)
161 gvisad_setup_validate_button (dialog);
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)
171 GalViewInstanceSaveAsDialog *dialog;
173 dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
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;
183 default:
184 return;
188 static void
189 gal_view_instance_save_as_dialog_get_property (GObject *object,
190 guint property_id,
191 GValue *value,
192 GParamSpec *pspec)
194 GalViewInstanceSaveAsDialog *dialog;
196 dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
198 switch (property_id) {
199 case PROP_INSTANCE:
200 g_value_set_object (value, dialog->instance);
201 break;
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
205 break;
209 static void
210 gal_view_instance_save_as_dialog_dispose (GObject *object)
212 GalViewInstanceSaveAsDialog *gal_view_instance_save_as_dialog = GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object);
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;
218 /* Chain up to parent's dispose() method. */
219 G_OBJECT_CLASS (gal_view_instance_save_as_dialog_parent_class)->dispose (object);
222 /* Init functions */
223 static void
224 gal_view_instance_save_as_dialog_class_init (GalViewInstanceSaveAsDialogClass *class)
226 GObjectClass *object_class;
228 object_class = (GObjectClass *) class;
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;
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_TYPE_VIEW_INSTANCE,
242 G_PARAM_READWRITE));
245 static void
246 gal_view_instance_save_as_dialog_init (GalViewInstanceSaveAsDialog *dialog)
248 GtkWidget *content_area;
249 GtkWidget *widget;
251 dialog->instance = NULL;
252 dialog->model = NULL;
253 dialog->collection = NULL;
255 dialog->builder = gtk_builder_new ();
256 e_load_ui_builder_definition (
257 dialog->builder, "gal-view-instance-save-as-dialog.ui");
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);
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);
267 gtk_dialog_add_buttons (
268 GTK_DIALOG (dialog),
269 _("_Cancel"), GTK_RESPONSE_CANCEL,
270 _("_Save"), GTK_RESPONSE_OK,
271 NULL);
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");
279 gtk_tree_view_set_reorderable (GTK_TREE_VIEW (dialog->treeview), FALSE);
280 gtk_tree_view_set_headers_visible (dialog->treeview, FALSE);
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);
292 gvisad_setup_radio_buttons (dialog);
293 gvisad_setup_validate_button (dialog);
295 gtk_window_set_title (GTK_WINDOW (dialog), _("Save Current View"));
296 gtk_widget_show (GTK_WIDGET (dialog));
299 /* External methods */
301 * gal_view_instance_save_as_dialog_new
303 * Returns a new dialog for defining views.
305 * Returns: The GalViewInstanceSaveAsDialog.
307 GtkWidget *
308 gal_view_instance_save_as_dialog_new (GalViewInstance *instance)
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;
315 void
316 gal_view_instance_save_as_dialog_save (GalViewInstanceSaveAsDialog *dialog)
318 GalView *view = gal_view_instance_get_current_view (dialog->instance);
319 const gchar *title;
320 const gchar *id = NULL;
321 GalViewCollection *collection;
322 GalViewCollectionItem *item;
323 gint ii, view_count;
325 collection = dialog->instance->collection;
326 view_count = gal_view_collection_get_count (collection);
328 view = gal_view_clone (view);
329 switch (dialog->toggle) {
330 case GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_REPLACE:
331 if (dialog->treeview) {
332 GtkTreeIter iter;
333 GtkTreeSelection *selection;
335 selection = gtk_tree_view_get_selection (dialog->treeview);
336 if (gtk_tree_selection_get_selected (selection, &dialog->model, &iter)) {
337 gtk_tree_model_get (dialog->model, &iter, COL_GALVIEW_DATA, &item, -1);
339 for (ii = 0; ii < view_count; ii++) {
340 GalViewCollectionItem *candidate;
342 candidate = gal_view_collection_get_view_item (collection, ii);
343 if (item == candidate) {
344 id = gal_view_collection_set_nth_view (collection, ii, view);
345 gal_view_collection_save (collection);
351 break;
353 case GAL_VIEW_INSTANCE_SAVE_AS_DIALOG_TOGGLE_CREATE:
354 if (dialog->entry_create && GTK_IS_ENTRY (dialog->entry_create)) {
355 title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_create));
356 id = gal_view_collection_append_with_title (collection, title, view);
357 gal_view_collection_save (collection);
359 break;
362 if (id) {
363 gal_view_instance_set_current_view_id (dialog->instance, id);