Update Spanish translation
[gnumeric.git] / src / dialogs / dialog-autosave.c
blobf3c16dfab376a52683a50dcc77b7f721e7ef4b4b
1 /*
2 * dialog-autosave.c:
4 * Authors:
5 * Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
6 * Miguel de Icaza (miguel@kernel.org)
8 * (C) Copyright 2000, 2001 by Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 **/
23 #include <gnumeric-config.h>
24 #include <gnumeric.h>
25 #include <dialogs/dialogs.h>
26 #include <dialogs/help.h>
27 #include <workbook.h>
28 #include <workbook-control.h>
29 #include <wbc-gtk.h>
30 #include <gui-util.h>
32 #include <goffice/goffice.h>
33 #include <glib/gi18n-lib.h>
36 typedef struct {
37 GtkWidget *dialog;
38 GtkWidget *minutes_entry;
39 GtkWidget *prompt_cb;
40 GtkWidget *autosave_on_off;
41 GtkWidget *ok_button;
42 GtkWidget *cancel_button;
43 Workbook *wb;
44 WBCGtk *wbcg;
45 } autosave_t;
47 #define AUTOSAVE_KEY "autosave-setup-dialog"
49 static void
50 autosave_set_sensitivity (G_GNUC_UNUSED GtkWidget *widget,
51 autosave_t *state)
53 gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off));
54 gint minutes;
55 gboolean minutes_err = entry_to_int (GTK_ENTRY (state->minutes_entry), &minutes, FALSE);
57 gtk_widget_set_sensitive (state->minutes_entry, active);
58 gtk_widget_set_sensitive (state->prompt_cb, active);
60 gtk_widget_set_sensitive (state->ok_button,
61 !active || (!minutes_err && minutes > 0));
64 gboolean
65 dialog_autosave_prompt (WBCGtk *wbcg)
67 char const *uri = go_doc_get_uri (
68 wb_control_get_doc (GNM_WBC (wbcg)));
69 GtkWidget *dialog = gtk_message_dialog_new (wbcg_toplevel (wbcg),
70 GTK_DIALOG_DESTROY_WITH_PARENT,
71 GTK_MESSAGE_QUESTION,
72 GTK_BUTTONS_YES_NO,
73 _("Do you want to save the workbook %s?"),
74 uri);
75 gint result = gtk_dialog_run (GTK_DIALOG (dialog));
77 gtk_widget_destroy (dialog);
79 return result == GTK_RESPONSE_YES;
82 static void
83 cb_autosave_cancel (G_GNUC_UNUSED GtkWidget *button,
84 autosave_t *state)
86 gtk_widget_destroy (state->dialog);
89 static void
90 cb_autosave_ok (G_GNUC_UNUSED GtkWidget *button, autosave_t *state)
92 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off))) {
93 gboolean prompt = gtk_toggle_button_get_active
94 (GTK_TOGGLE_BUTTON (state->prompt_cb));
95 int minutes, secs;
96 gboolean minutes_err = entry_to_int (GTK_ENTRY (state->minutes_entry),
97 &minutes, TRUE);
99 g_return_if_fail (!minutes_err); /* Why is ok active? */
101 secs = 60 * MIN (minutes, G_MAXINT / 60);
102 g_object_set (state->wbcg,
103 "autosave-time", secs,
104 "autosave-prompt", prompt,
105 NULL);
106 } else {
107 g_object_set (state->wbcg, "autosave-time", 0, NULL);
109 gtk_widget_destroy (state->dialog);
112 void
113 dialog_autosave (WBCGtk *wbcg)
115 GtkBuilder *gui;
116 autosave_t *state;
117 int secs;
118 gboolean prompt;
120 g_return_if_fail (wbcg != NULL);
122 if (gnm_dialog_raise_if_exists (wbcg, AUTOSAVE_KEY))
123 return;
124 gui = gnm_gtk_builder_load ("res:ui/autosave.ui", NULL, GO_CMD_CONTEXT (wbcg));
125 if (gui == NULL)
126 return;
128 g_object_get (wbcg,
129 "autosave-time", &secs,
130 "autosave-prompt", &prompt,
131 NULL);
133 state = g_new (autosave_t, 1);
134 state->wbcg = wbcg;
135 state->wb = wb_control_get_workbook (GNM_WBC (wbcg));
137 state->dialog = go_gtk_builder_get_widget (gui, "AutoSave");
138 state->minutes_entry = go_gtk_builder_get_widget (gui, "minutes");
139 state->prompt_cb = go_gtk_builder_get_widget (gui, "prompt_on_off");
140 state->autosave_on_off = go_gtk_builder_get_widget (gui, "autosave_on_off");
141 state->ok_button = go_gtk_builder_get_widget (gui, "button1");
142 state->cancel_button = go_gtk_builder_get_widget (gui, "button2");
144 if (!state->dialog || !state->minutes_entry || !state->prompt_cb ||
145 !state->autosave_on_off) {
146 go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
147 _("Could not create the autosave dialog."));
148 g_free (state);
149 return;
152 float_to_entry (GTK_ENTRY (state->minutes_entry),
153 secs / 60);
155 gnm_editable_enters (GTK_WINDOW (state->dialog),
156 state->minutes_entry);
158 g_signal_connect (G_OBJECT (state->autosave_on_off),
159 "toggled",
160 G_CALLBACK (autosave_set_sensitivity), state);
161 g_signal_connect (G_OBJECT (state->minutes_entry),
162 "changed",
163 G_CALLBACK (autosave_set_sensitivity), state);
164 g_signal_connect (G_OBJECT (state->ok_button),
165 "clicked",
166 G_CALLBACK (cb_autosave_ok), state);
167 g_signal_connect (G_OBJECT (state->cancel_button),
168 "clicked",
169 G_CALLBACK (cb_autosave_cancel), state);
171 g_object_set_data_full (G_OBJECT (state->dialog),
172 "state", state, (GDestroyNotify)g_free);
173 gnm_init_help_button (
174 go_gtk_builder_get_widget (gui, "button3"),
175 GNUMERIC_HELP_LINK_AUTOSAVE);
177 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->autosave_on_off),
178 secs > 0);
179 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->prompt_cb),
180 prompt);
182 autosave_set_sensitivity (NULL, state);
183 gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
184 AUTOSAVE_KEY);
185 gtk_widget_show (state->dialog);
187 g_object_unref (gui);