dialog-autosave.c 5.55 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4
/*
 * dialog-autosave.c:
 *
5
 * Authors:
6
 *        Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
7
 *        Miguel de Icaza (miguel@kernel.org)
8
 *
9
 * (C) Copyright 2000, 2001 by Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
jpekka's avatar
jpekka committed
10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
23
 **/
24 25
#include <gnumeric-config.h>
#include <gnumeric.h>
26
#include "dialogs.h"
27
#include "help.h"
28
#include <workbook.h>
29 30
#include <workbook-control.h>
#include <wbc-gtk.h>
31
#include <gui-util.h>
32

33
#include <goffice/goffice.h>
34
#include <glib/gi18n-lib.h>
35

36 37

typedef struct {
38
	GtkWidget *dialog;
39 40
        GtkWidget *minutes_entry;
        GtkWidget *prompt_cb;
41 42 43 44
	GtkWidget *autosave_on_off;
	GtkWidget *ok_button;
	GtkWidget *cancel_button;
	Workbook  *wb;
45
	WBCGtk  *wbcg;
46 47
} autosave_t;

48 49
#define AUTOSAVE_KEY            "autosave-setup-dialog"

50
static void
Morten Welinder's avatar
Morten Welinder committed
51
autosave_set_sensitivity (G_GNUC_UNUSED GtkWidget *widget,
52
			  autosave_t *state)
53
{
54 55
        gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off));
	gint minutes;
56
	gboolean minutes_err = entry_to_int (GTK_ENTRY (state->minutes_entry), &minutes, FALSE);
57 58 59 60

	gtk_widget_set_sensitive (state->minutes_entry, active);
	gtk_widget_set_sensitive (state->prompt_cb, active);

61 62
	gtk_widget_set_sensitive (state->ok_button,
				  !active || (!minutes_err && minutes > 0));
63 64
}

65
gboolean
66
dialog_autosave_prompt (WBCGtk *wbcg)
67
{
68
	char const *uri   = go_doc_get_uri (
Morten Welinder's avatar
Morten Welinder committed
69
		wb_control_get_doc (GNM_WBC (wbcg)));
70
	GtkWidget *dialog = gtk_message_dialog_new (wbcg_toplevel (wbcg),
71 72 73
					 GTK_DIALOG_DESTROY_WITH_PARENT,
					 GTK_MESSAGE_QUESTION,
					 GTK_BUTTONS_YES_NO,
74
					 _("Do you want to save the workbook %s?"),
75
					 uri);
76 77
	gint result = gtk_dialog_run (GTK_DIALOG (dialog));

78 79
	gtk_widget_destroy (dialog);

80
	return result == GTK_RESPONSE_YES;
81 82 83
}

static void
Morten Welinder's avatar
Morten Welinder committed
84
cb_autosave_cancel (G_GNUC_UNUSED GtkWidget *button,
85
		    autosave_t *state)
86
{
87 88
	gtk_widget_destroy (state->dialog);
}
89

90
static void
Morten Welinder's avatar
Morten Welinder committed
91
cb_autosave_ok (G_GNUC_UNUSED GtkWidget *button, autosave_t *state)
92
{
93
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->autosave_on_off))) {
94 95 96
		gboolean prompt = gtk_toggle_button_get_active
			(GTK_TOGGLE_BUTTON (state->prompt_cb));
		int minutes, secs;
97 98 99 100 101
		gboolean minutes_err = entry_to_int (GTK_ENTRY (state->minutes_entry),
						     &minutes, TRUE);

		g_return_if_fail (!minutes_err); /* Why is ok active? */

Morten Welinder's avatar
Morten Welinder committed
102
		secs = 60 * MIN (minutes, G_MAXINT / 60);
103 104 105 106 107 108 109
		g_object_set (state->wbcg,
			      "autosave-time", secs,
			      "autosave-prompt", prompt,
			      NULL);
	} else {
		g_object_set (state->wbcg, "autosave-time", 0, NULL);
	}
110
	gtk_widget_destroy (state->dialog);
111
}
112

113
void
114
dialog_autosave (WBCGtk *wbcg)
115
{
116
	GtkBuilder *gui;
117
	autosave_t *state;
118 119
	int secs;
	gboolean prompt;
120

121
	g_return_if_fail (wbcg != NULL);
122

Morten Welinder's avatar
Morten Welinder committed
123
	if (gnm_dialog_raise_if_exists (wbcg, AUTOSAVE_KEY))
124
		return;
125
	gui = gnm_gtk_builder_load ("res:ui/autosave.ui", NULL, GO_CMD_CONTEXT (wbcg));
126 127 128
        if (gui == NULL)
                return;

129 130 131 132
	g_object_get (wbcg,
		      "autosave-time", &secs,
		      "autosave-prompt", &prompt,
		      NULL);
133

134
	state = g_new (autosave_t, 1);
135
	state->wbcg = wbcg;
Morten Welinder's avatar
Morten Welinder committed
136
	state->wb   = wb_control_get_workbook (GNM_WBC (wbcg));
137

Morten Welinder's avatar
Morten Welinder committed
138 139 140 141 142 143
	state->dialog = go_gtk_builder_get_widget (gui, "AutoSave");
	state->minutes_entry = go_gtk_builder_get_widget (gui, "minutes");
	state->prompt_cb = go_gtk_builder_get_widget (gui, "prompt_on_off");
	state->autosave_on_off = go_gtk_builder_get_widget (gui, "autosave_on_off");
	state->ok_button = go_gtk_builder_get_widget (gui, "button1");
	state->cancel_button = go_gtk_builder_get_widget (gui, "button2");
144

145
	if (!state->dialog || !state->minutes_entry || !state->prompt_cb ||
146
	    !state->autosave_on_off) {
Jody Goldberg's avatar
Jody Goldberg committed
147
		go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
148 149 150
				 _("Could not create the autosave dialog."));
		g_free (state);
		return;
151
	}
152

153 154
	float_to_entry (GTK_ENTRY (state->minutes_entry),
			secs / 60);
155

Morten Welinder's avatar
Morten Welinder committed
156
	gnm_editable_enters (GTK_WINDOW (state->dialog),
157 158 159 160 161 162 163 164 165 166 167 168 169 170
				  state->minutes_entry);

	g_signal_connect (G_OBJECT (state->autosave_on_off),
		"toggled",
		G_CALLBACK (autosave_set_sensitivity), state);
	g_signal_connect (G_OBJECT (state->minutes_entry),
		"changed",
		G_CALLBACK (autosave_set_sensitivity), state);
	g_signal_connect (G_OBJECT (state->ok_button),
		"clicked",
		G_CALLBACK (cb_autosave_ok), state);
	g_signal_connect (G_OBJECT (state->cancel_button),
		"clicked",
		G_CALLBACK (cb_autosave_cancel), state);
171

172
	g_object_set_data_full (G_OBJECT (state->dialog),
173
		"state", state, (GDestroyNotify)g_free);
Morten Welinder's avatar
Morten Welinder committed
174
	gnm_init_help_button (
Morten Welinder's avatar
Morten Welinder committed
175
		go_gtk_builder_get_widget (gui, "button3"),
176
		GNUMERIC_HELP_LINK_AUTOSAVE);
177

178
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->autosave_on_off),
179 180 181
				      secs > 0);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->prompt_cb),
				      prompt);
182

183
	autosave_set_sensitivity (NULL, state);
Morten Welinder's avatar
Morten Welinder committed
184
	gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
185 186
			       AUTOSAVE_KEY);
	gtk_widget_show (state->dialog);
187 188

	g_object_unref (gui);
189
}