~siretart/gnucash/ubuntu-fullsource

« back to all changes in this revision

Viewing changes to src/gnome-utils/dialog-reset-warnings.c

  • Committer: Reinhard Tartler
  • Date: 2008-08-03 07:25:46 UTC
  • Revision ID: siretart@tauware.de-20080803072546-y6p8xda8zpfi62ys
import gnucash_2.2.4.orig.tar.gz

The original tarball had the md5sum: 27e660297dc5b8ce574515779d05a5a5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * dialog-reset-warnings.c -- "Resert Warnings" dialog
 
3
 * Copyright (C) 2005 David Hampton
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 2 of
 
8
 * the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, contact:
 
17
 *
 
18
 * Free Software Foundation           Voice:  +1-617-542-5942
 
19
 * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
 
20
 * Boston, MA  02110-1301,  USA       gnu@gnu.org
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <gtk/gtk.h>
 
26
#include <glade/glade.h>
 
27
 
 
28
#include "dialog-utils.h"
 
29
#include "gnc-gconf-utils.h"
 
30
#include "gnc-engine.h"
 
31
#include "gnc-ui.h"
 
32
#include "gnc-component-manager.h"
 
33
#include "dialog-reset-warnings.h"
 
34
 
 
35
/* This static indicates the debugging module that this .o belongs to.  */
 
36
static QofLogModule log_module = GNC_MOD_PREFS;
 
37
 
 
38
#define GCONF_SECTION                   "dialogs/reset_warnings"
 
39
#define DIALOG_RESET_WARNINGS_CM_CLASS  "reset-warnings"
 
40
#define GCONF_ENTRY_LIST                "gconf_entries"
 
41
#define TIPS_STRING                     "tips"
 
42
 
 
43
void gnc_reset_warnings_select_all_cb (GtkButton *button, gpointer user_data);
 
44
void gnc_reset_warnings_unselect_all_cb (GtkButton *button, gpointer user_data);
 
45
void gnc_reset_warnings_response_cb (GtkDialog *dialog, gint arg1, gpointer user_data);
 
46
 
 
47
 
 
48
 
 
49
static void
 
50
gnc_reset_warnings_update_widgets (GtkWidget *dialog_widget)
 
51
{
 
52
  GtkWidget *box1, *box2, *nada, *buttons;
 
53
  GtkWidget *apply;
 
54
  GList *list, *tmp;
 
55
  gboolean any = FALSE, checked = FALSE;
 
56
 
 
57
  ENTER(" ");
 
58
  box1 = gnc_glade_lookup_widget(dialog_widget, "perm_vbox_and_label");
 
59
  box2 = gnc_glade_lookup_widget(dialog_widget, "perm_vbox");
 
60
  list = gtk_container_get_children(GTK_CONTAINER(box2));
 
61
  if (list) {
 
62
    gtk_widget_show_all(box1);
 
63
    for (tmp = list; tmp; tmp = g_list_next(tmp)) {
 
64
      if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tmp->data))) {
 
65
        checked = TRUE;
 
66
        break;
 
67
      }
 
68
    }
 
69
    g_list_free(list);
 
70
    any = TRUE;
 
71
  } else {
 
72
    gtk_widget_hide(box1);
 
73
  }
 
74
 
 
75
  box1 = gnc_glade_lookup_widget(dialog_widget, "temp_vbox_and_label");
 
76
  box2 = gnc_glade_lookup_widget(dialog_widget, "temp_vbox");
 
77
  list = gtk_container_get_children(GTK_CONTAINER(box2));
 
78
  if (list) {
 
79
    gtk_widget_show_all(box1);
 
80
    for (tmp = list; tmp; tmp = g_list_next(tmp)) {
 
81
      if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tmp->data))) {
 
82
        checked = TRUE;
 
83
        break;
 
84
      }
 
85
    }
 
86
    g_list_free(list);
 
87
    any = TRUE;
 
88
  } else {
 
89
    gtk_widget_hide(box1);
 
90
  }
 
91
 
 
92
  nada = gnc_glade_lookup_widget(dialog_widget, "no_warnings");
 
93
  buttons = gnc_glade_lookup_widget(dialog_widget, "hbuttonbox");
 
94
  apply = gnc_glade_lookup_widget(dialog_widget, "applybutton");
 
95
  if (any) {
 
96
    gtk_widget_show(buttons);
 
97
    gtk_widget_hide(nada);
 
98
    gtk_widget_set_sensitive(apply, checked);
 
99
  } else {
 
100
    gtk_widget_hide(buttons);
 
101
    gtk_widget_show(nada);
 
102
    gtk_widget_set_sensitive(apply, FALSE);
 
103
  }
 
104
  LEAVE(" ");
 
105
}
 
106
 
 
107
 
 
108
static void
 
109
gnc_reset_warnings_select_common (GtkButton *button,
 
110
                                  gboolean selected)
 
111
{
 
112
  GtkWidget *vbox;
 
113
 
 
114
  ENTER("button %p, selected %d", button, selected);
 
115
  vbox = gnc_glade_lookup_widget(GTK_WIDGET(button), "perm_vbox");
 
116
  gtk_container_foreach(GTK_CONTAINER(vbox),
 
117
                        (GtkCallback)gtk_toggle_button_set_active,
 
118
                        GINT_TO_POINTER(selected));
 
119
  vbox = gnc_glade_lookup_widget(GTK_WIDGET(button), "temp_vbox");
 
120
  gtk_container_foreach(GTK_CONTAINER(vbox),
 
121
                        (GtkCallback)gtk_toggle_button_set_active,
 
122
                        GINT_TO_POINTER(selected));
 
123
  gnc_reset_warnings_update_widgets(GTK_WIDGET(button));
 
124
  LEAVE(" ");
 
125
}
 
126
 
 
127
 
 
128
void
 
129
gnc_reset_warnings_select_all_cb (GtkButton *button,
 
130
                                  gpointer user_data)
 
131
{
 
132
  gnc_reset_warnings_select_common(button, TRUE);
 
133
}
 
134
 
 
135
 
 
136
void
 
137
gnc_reset_warnings_unselect_all_cb (GtkButton *button,
 
138
                                    gpointer user_data)
 
139
{
 
140
  gnc_reset_warnings_select_common(button, FALSE);
 
141
}
 
142
 
 
143
 
 
144
static void
 
145
gnc_reset_warnings_find_remove (GtkWidget *widget,
 
146
                                const gchar *name)
 
147
{
 
148
  ENTER("widget %p, name %s", widget, name);
 
149
  if (strcmp(gtk_widget_get_name(widget), name) == 0) {
 
150
    DEBUG("destroying widget %s", name);
 
151
    gtk_widget_destroy(widget);
 
152
  }
 
153
  LEAVE(" ");
 
154
}
 
155
 
 
156
 
 
157
static void
 
158
gnc_reset_warnings_apply_one (GtkWidget *widget,
 
159
                              GtkDialog *dialog)
 
160
{
 
161
  const char *name;
 
162
 
 
163
  ENTER("widget %p, dialog %p", widget, dialog);
 
164
  if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
 
165
    LEAVE("not active");
 
166
    return;
 
167
  }
 
168
 
 
169
  name = gtk_widget_get_name(widget);
 
170
  gnc_gconf_unset(NULL, name, NULL);
 
171
  gtk_widget_destroy(widget);
 
172
  LEAVE(" ");
 
173
}
 
174
 
 
175
 
 
176
static void
 
177
gnc_reset_warnings_apply_changes (GtkDialog *dialog)
 
178
{
 
179
  GtkWidget *vbox;
 
180
 
 
181
  ENTER("dialog %p", dialog);
 
182
  vbox = gnc_glade_lookup_widget(GTK_WIDGET(dialog), "perm_vbox");
 
183
  gtk_container_foreach(GTK_CONTAINER(vbox),
 
184
                        (GtkCallback)gnc_reset_warnings_apply_one,
 
185
                        dialog);
 
186
  vbox = gnc_glade_lookup_widget(GTK_WIDGET(dialog), "temp_vbox");
 
187
  gtk_container_foreach(GTK_CONTAINER(vbox),
 
188
                        (GtkCallback)gnc_reset_warnings_apply_one,
 
189
                        dialog);
 
190
  gnc_reset_warnings_update_widgets(GTK_WIDGET(dialog));
 
191
  LEAVE(" ");
 
192
}
 
193
 
 
194
 
 
195
static void
 
196
gnc_reset_warnings_revert_changes (GtkDialog *dialog)
 
197
{
 
198
  GSList *entries, *tmp;
 
199
  GConfEntry *entry;
 
200
 
 
201
  ENTER("dialog %p", dialog);
 
202
 
 
203
  entries = g_object_get_data(G_OBJECT(dialog), GCONF_ENTRY_LIST);
 
204
  for (tmp = entries; tmp; tmp = g_slist_next(tmp)) {
 
205
    entry = tmp->data;
 
206
    gnc_gconf_set_int (NULL, entry->key,
 
207
                       gconf_value_get_int(entry->value), NULL);
 
208
  }
 
209
  LEAVE(" ");
 
210
}
 
211
 
 
212
 
 
213
void
 
214
gnc_reset_warnings_response_cb (GtkDialog *dialog,
 
215
                                gint response,
 
216
                                gpointer user_data)
 
217
{
 
218
  switch (response) {
 
219
    case GTK_RESPONSE_APPLY:
 
220
      gnc_reset_warnings_apply_changes(dialog);
 
221
      break;
 
222
 
 
223
    case GTK_RESPONSE_OK:
 
224
      gnc_gconf_remove_notification(G_OBJECT(dialog), GCONF_WARNINGS,
 
225
                                    DIALOG_RESET_WARNINGS_CM_CLASS);
 
226
      gnc_reset_warnings_apply_changes(dialog);
 
227
      gnc_save_window_size(GCONF_SECTION, GTK_WINDOW(dialog));
 
228
      gnc_unregister_gui_component_by_data(DIALOG_RESET_WARNINGS_CM_CLASS,
 
229
                                           dialog);
 
230
      gtk_widget_destroy(GTK_WIDGET(dialog));
 
231
      break;
 
232
 
 
233
    default:
 
234
      gnc_gconf_remove_notification(G_OBJECT(dialog), GCONF_WARNINGS,
 
235
                                    DIALOG_RESET_WARNINGS_CM_CLASS);
 
236
      gnc_reset_warnings_revert_changes(dialog);
 
237
      gnc_unregister_gui_component_by_data(DIALOG_RESET_WARNINGS_CM_CLASS,
 
238
                                           dialog);
 
239
      gtk_widget_destroy(GTK_WIDGET(dialog));
 
240
  }
 
241
}
 
242
 
 
243
static void
 
244
gnc_reset_warnings_add_one (GConfEntry *entry, GtkWidget *box)
 
245
{
 
246
  const gchar *name, *schema_name, *desc, *long_desc = NULL;
 
247
  GtkWidget *checkbox;
 
248
  GConfSchema *schema = NULL;
 
249
 
 
250
  ENTER(" ");
 
251
  name = strrchr(entry->key, '/') + 1;
 
252
  schema_name = gconf_entry_get_schema_name(entry);
 
253
  if (schema_name)
 
254
    schema = gnc_gconf_get_schema(NULL, schema_name, NULL);
 
255
  if (schema) {
 
256
    DEBUG("found schema %p", schema);
 
257
    desc = gconf_schema_get_short_desc(schema);
 
258
    DEBUG("description %s", desc);
 
259
    long_desc = gconf_schema_get_long_desc(schema);
 
260
    checkbox = gtk_check_button_new_with_label(desc ? desc : name);
 
261
    if (long_desc) {
 
262
      GtkTooltips *tips;
 
263
      tips = g_object_get_data(G_OBJECT(box), TIPS_STRING);
 
264
      if (!tips) {
 
265
        tips = gtk_tooltips_new();
 
266
        g_object_set_data(G_OBJECT(box), TIPS_STRING, tips);
 
267
      }
 
268
      gtk_tooltips_set_tip(tips, checkbox, long_desc, NULL);
 
269
    }
 
270
    gconf_schema_free(schema);
 
271
  } else {
 
272
    DEBUG("no schema");
 
273
    checkbox = gtk_check_button_new_with_label(name);
 
274
  }
 
275
 
 
276
  gtk_widget_set_name(checkbox, entry->key);
 
277
  g_signal_connect_swapped(G_OBJECT(checkbox), "toggled",
 
278
                           (GCallback)gnc_reset_warnings_update_widgets,
 
279
                           box);
 
280
  gtk_box_pack_start_defaults(GTK_BOX(box), checkbox);
 
281
  LEAVE(" ");
 
282
}
 
283
 
 
284
 
 
285
static GSList *
 
286
gnc_reset_warnings_add_section (const gchar *section, GtkWidget *box)
 
287
{
 
288
  GSList *entries, *tmp;
 
289
  GConfEntry *entry;
 
290
 
 
291
  ENTER(" ");
 
292
  entries = gnc_gconf_client_all_entries(section);
 
293
  for (tmp = entries; tmp; tmp = g_slist_next(tmp)) {
 
294
    entry = tmp->data;
 
295
    if (gconf_value_get_int(entry->value) != 0) {
 
296
      gnc_reset_warnings_add_one(entry, box);
 
297
    }
 
298
  }
 
299
 
 
300
  LEAVE(" ");
 
301
  return entries;
 
302
}
 
303
 
 
304
 
 
305
static void
 
306
gnc_reset_warnings_release_entries (GSList *entries)
 
307
{
 
308
  GSList *tmp;
 
309
 
 
310
  ENTER(" ");
 
311
  for (tmp = entries; tmp; tmp = g_slist_next(tmp)) {
 
312
    gconf_entry_free(tmp->data);
 
313
  }
 
314
  g_slist_free(entries);
 
315
  LEAVE(" ");
 
316
}
 
317
 
 
318
static void
 
319
gnc_reset_warnings_gconf_changed (GConfClient *client,
 
320
                                  guint cnxn_id,
 
321
                                  GConfEntry *entry,
 
322
                                  gpointer user_data)
 
323
{
 
324
  GtkWidget *dialog, *box;
 
325
  GList *list;
 
326
 
 
327
  g_return_if_fail(GTK_IS_DIALOG(user_data));
 
328
 
 
329
  ENTER("entry %p, data %p", entry, user_data);
 
330
  dialog = GTK_WIDGET(user_data);
 
331
  DEBUG("entry key '%s', value as %p, value as int %d", entry->key, entry->value, gconf_value_get_int(entry->value));
 
332
 
 
333
  /* Which box is affected */
 
334
  if (strstr(entry->key, "permanent") != 0) {
 
335
    box = gnc_glade_lookup_widget(GTK_WIDGET(dialog), "perm_vbox");
 
336
  } else {
 
337
    box = gnc_glade_lookup_widget(GTK_WIDGET(dialog), "temp_vbox");
 
338
  }
 
339
 
 
340
  if (gconf_value_get_int(entry->value) != 0) {
 
341
    gnc_reset_warnings_add_one (entry, box);
 
342
    DEBUG("added checkbox for %s", entry->key);
 
343
  } else {
 
344
    /* Don't know if we were invoked by the dialog removing the
 
345
     * warning, or if the remove happened somewhere else like
 
346
     * gconf-editor.  Can't hurt to run the widgets and try to remove
 
347
     * it.  Worst case we can't find it because its already been
 
348
     * deleted. */
 
349
    list = gtk_container_get_children(GTK_CONTAINER(box));
 
350
    g_list_foreach(list, (GFunc)gnc_reset_warnings_find_remove, entry->key);
 
351
    g_list_free(list);
 
352
  }
 
353
  gnc_reset_warnings_update_widgets(dialog);
 
354
  LEAVE(" ");
 
355
}
 
356
 
 
357
 
 
358
static gboolean
 
359
show_handler (const char *class, gint component_id,
 
360
              gpointer user_data, gpointer iter_data)
 
361
{
 
362
  GtkWidget *dialog;
 
363
 
 
364
  ENTER(" ");
 
365
  dialog = GTK_WIDGET(user_data);
 
366
  gtk_window_present(GTK_WINDOW(dialog));
 
367
  LEAVE(" ");
 
368
  return(TRUE);
 
369
}
 
370
 
 
371
 
 
372
static void
 
373
close_handler (gpointer user_data)
 
374
{
 
375
  GtkWidget *dialog = user_data;
 
376
 
 
377
  ENTER(" ");
 
378
  dialog = GTK_WIDGET(user_data);
 
379
  gnc_unregister_gui_component_by_data(DIALOG_RESET_WARNINGS_CM_CLASS, dialog);
 
380
  gtk_widget_destroy(dialog);
 
381
  LEAVE(" ");
 
382
}
 
383
 
 
384
 
 
385
void
 
386
gnc_reset_warnings_dialog (GtkWidget *main_window)
 
387
{
 
388
  GtkWidget *dialog, *box;
 
389
  GladeXML *xml;
 
390
  GSList *perm_list, *temp_list;
 
391
 
 
392
  ENTER("");
 
393
  if (gnc_forall_gui_components(DIALOG_RESET_WARNINGS_CM_CLASS,
 
394
                                show_handler, NULL)) {
 
395
    LEAVE("existing window");
 
396
    return;
 
397
  }
 
398
 
 
399
  DEBUG("Opening dialog-reset-warnings.glade:");
 
400
  xml = gnc_glade_xml_new("dialog-reset-warnings.glade", "Reset Warnings");
 
401
  dialog = glade_xml_get_widget(xml, "Reset Warnings");
 
402
  glade_xml_signal_autoconnect_full(xml, gnc_glade_autoconnect_full_func,
 
403
                                    dialog);
 
404
 
 
405
  DEBUG("permanent");
 
406
  box = glade_xml_get_widget(xml, "perm_vbox");
 
407
  perm_list = gnc_reset_warnings_add_section(GCONF_WARNINGS_PERM, box);
 
408
 
 
409
  DEBUG("temporary");
 
410
  box = glade_xml_get_widget(xml, "temp_vbox");
 
411
  temp_list = gnc_reset_warnings_add_section(GCONF_WARNINGS_TEMP, box);
 
412
 
 
413
  g_object_set_data_full(G_OBJECT(dialog), GCONF_ENTRY_LIST,
 
414
                         g_slist_concat (perm_list, temp_list),
 
415
                         (GDestroyNotify)gnc_reset_warnings_release_entries);
 
416
 
 
417
  gnc_reset_warnings_update_widgets(dialog);
 
418
 
 
419
  gnc_gconf_add_notification(G_OBJECT(dialog), GCONF_WARNINGS,
 
420
                             gnc_reset_warnings_gconf_changed,
 
421
                             DIALOG_RESET_WARNINGS_CM_CLASS);
 
422
 
 
423
  gnc_restore_window_size(GCONF_SECTION, GTK_WINDOW(dialog));
 
424
 
 
425
  gnc_register_gui_component (DIALOG_RESET_WARNINGS_CM_CLASS,
 
426
                              NULL, close_handler, dialog);
 
427
 
 
428
  gtk_widget_show(dialog);
 
429
  LEAVE(" ");
 
430
}