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
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/>.
16 * Chris Lahey <clahey@ximian.com>
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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
)
45 /* Static functions */
47 gal_view_instance_save_as_dialog_set_instance (GalViewInstanceSaveAsDialog
*dialog
,
48 GalViewInstance
*instance
)
51 GtkCellRenderer
*renderer
;
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
;
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
);
73 COL_GALVIEW_NAME
, title
,
74 COL_GALVIEW_DATA
, item
,
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 (
94 COL_GALVIEW_NAME
, _("Name"),
95 renderer
, "text", COL_GALVIEW_NAME
,
99 gtk_tree_sortable_set_sort_column_id (
100 GTK_TREE_SORTABLE (dialog
->model
),
101 COL_GALVIEW_NAME
, GTK_SORT_ASCENDING
);
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
);
112 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
, FALSE
);
117 gvisad_setup_radio_buttons (GalViewInstanceSaveAsDialog
*dialog
)
121 widget
= dialog
->scrolledwindow
;
122 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog
->radiobutton_replace
))) {
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
;
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
;
144 gtk_widget_set_sensitive (widget
, FALSE
);
147 gvisad_setup_validate_button (dialog
);
151 gvisad_radio_toggled (GtkWidget
*widget
,
152 GalViewInstanceSaveAsDialog
*dialog
)
154 gvisad_setup_radio_buttons (dialog
);
158 gvisad_entry_changed (GtkWidget
*widget
,
159 GalViewInstanceSaveAsDialog
*dialog
)
161 gvisad_setup_validate_button (dialog
);
164 /* Method override implementations */
166 gal_view_instance_save_as_dialog_set_property (GObject
*object
,
171 GalViewInstanceSaveAsDialog
*dialog
;
173 dialog
= GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object
);
175 switch (property_id
) {
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
)));
180 gal_view_instance_save_as_dialog_set_instance (dialog
, NULL
);
189 gal_view_instance_save_as_dialog_get_property (GObject
*object
,
194 GalViewInstanceSaveAsDialog
*dialog
;
196 dialog
= GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (object
);
198 switch (property_id
) {
200 g_value_set_object (value
, dialog
->instance
);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
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
);
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 (
237 g_param_spec_object (
241 GAL_TYPE_VIEW_INSTANCE
,
246 gal_view_instance_save_as_dialog_init (GalViewInstanceSaveAsDialog
*dialog
)
248 GtkWidget
*content_area
;
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 (
269 _("_Cancel"), GTK_RESPONSE_CANCEL
,
270 _("_Save"), GTK_RESPONSE_OK
,
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
);
283 dialog
->radiobutton_replace
, "toggled",
284 G_CALLBACK (gvisad_radio_toggled
), dialog
);
286 dialog
->radiobutton_create
, "toggled",
287 G_CALLBACK (gvisad_radio_toggled
), dialog
);
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.
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
);
316 gal_view_instance_save_as_dialog_save (GalViewInstanceSaveAsDialog
*dialog
)
318 GalView
*view
= gal_view_instance_get_current_view (dialog
->instance
);
320 const gchar
*id
= NULL
;
321 GalViewCollection
*collection
;
322 GalViewCollectionItem
*item
;
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
) {
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
);
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
);
363 gal_view_instance_set_current_view_id (dialog
->instance
, id
);