28 #include <glib/gi18n.h>
30 #include "dialog-totd.h"
31 #include "dialog-utils.h"
32 #include "gnc-component-manager.h"
38 #define GNC_PREFS_GROUP "dialogs.totd"
39 #define GNC_PREF_CURRENT_TIP "current-tip"
40 #define GNC_PREF_SHOW_TIPS "show-at-startup"
41 #define DIALOG_TOTD_CM_CLASS "dialog-totd"
43 #define GNC_RESPONSE_FORWARD 1
44 #define GNC_RESPONSE_BACK 2
47 void gnc_totd_dialog_response_cb (GtkDialog *dialog, gint reponse, gpointer user_data);
48 void gnc_totd_dialog_startup_toggled_cb (GtkToggleButton *button, gpointer user_data);
51 static gchar **tip_list;
52 static gint tip_count = -1;
53 static gint current_tip_number = -1;
61 GtkTextView *textview;
62 GtkWidget *showcheck_button;
81 gnc_new_tip_number (
TotdDialog *totd_dialog, gint offset)
84 gchar **tip_components = NULL;
87 ENTER(
"TotdDialog %p, offset %d", totd_dialog, offset);
88 current_tip_number += offset;
89 DEBUG(
"clamp %d to '0 <= x < %d'", current_tip_number, tip_count);
90 if (current_tip_number < 0)
91 current_tip_number = tip_count - 1;
92 if (current_tip_number >= tip_count)
93 current_tip_number = 0;
102 if (tip_list[current_tip_number])
103 tip_components = g_strsplit(tip_list[current_tip_number],
"|", 0);
107 if (tip_components[0] == NULL)
117 tip = g_strdup_printf( _(tip_components[0]), tip_components[1]);
120 g_strfreev(tip_components);
121 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(totd_dialog->textview)),
131 void gnc_totd_dialog_response_cb (GtkDialog *dialog,
137 ENTER(
"dialog %p, response %d, user_data %p", dialog, response, user_data);
140 case GNC_RESPONSE_FORWARD:
141 gnc_new_tip_number(totd_dialog, 1);
144 case GNC_RESPONSE_BACK:
145 gnc_new_tip_number(totd_dialog, -1);
148 case GTK_RESPONSE_CLOSE:
149 gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(totd_dialog->dialog));
153 gnc_unregister_gui_component_by_data(DIALOG_TOTD_CM_CLASS, totd_dialog);
154 gtk_widget_destroy(GTK_WIDGET(totd_dialog->dialog));
162 gnc_totd_dialog_startup_toggled_cb (GtkToggleButton *button,
167 active = gtk_toggle_button_get_active(button);
176 gnc_totd_initialize (
void)
178 gchar *filename, *contents, *new_str;
188 if (!g_file_get_contents(filename, &contents, &length, &error))
190 printf(
"Unable to read file: %s\n", error->message);
200 tip_list = g_strsplit(contents,
"\n", 0);
205 for (tip_count = 0; tip_list[tip_count] != NULL; tip_count++)
207 if (*tip_list[tip_count]!=
'\0')
209 g_strstrip(tip_list[tip_count]);
211 contents = g_strdup (tip_list[tip_count]);
214 new_str = g_strjoin (
"\n", contents, tip_list[tip_count], NULL);
222 g_strfreev (tip_list);
224 tip_list = g_strsplit(contents,
"\n", 0);
227 for (tip_count = 0; tip_list[tip_count] != NULL; tip_count++)
229 new_str = g_strcompress(tip_list[tip_count]);
230 g_free(tip_list[tip_count]);
231 tip_list[tip_count] = new_str;
241 PWARN(
"No tips found - Tips of the day window won't be displayed.");
265 show_handler (
const char *class_name, gint component_id,
266 gpointer user_data, gpointer iter_data)
273 LEAVE(
"no data strucure");
277 gtk_window_present(GTK_WINDOW(totd_dialog->dialog));
291 close_handler (gpointer user_data)
297 gnc_unregister_gui_component_by_data(DIALOG_TOTD_CM_CLASS, totd_dialog);
307 gnc_totd_dialog (GtkWindow *parent, gboolean startup)
312 GtkWidget *dialog, *button;
313 GtkTextView *textview;
319 if (startup && !show_tips)
324 if (!gnc_totd_initialize())
329 if (gnc_forall_gui_components(DIALOG_TOTD_CM_CLASS, show_handler, NULL))
334 builder = gtk_builder_new();
335 gnc_builder_add_from_file (builder,
"dialog-totd.glade",
"totd_dialog");
336 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"totd_dialog"));
337 gtk_window_set_transient_for(GTK_WINDOW (dialog), parent);
339 totd_dialog->dialog = dialog;
341 ENTER(
"totd_dialog %p, dialog %p", totd_dialog, dialog);
343 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, totd_dialog);
345 button = GTK_WIDGET(gtk_builder_get_object (builder,
"show_checkbutton"));
346 totd_dialog->showcheck_button = button;
348 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (button), show_tips);
350 textview = GTK_TEXT_VIEW(gtk_builder_get_object (builder,
"tip_textview"));
351 totd_dialog->textview = textview;
353 gnc_new_tip_number(totd_dialog, 1);
355 gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(totd_dialog->dialog));
356 gtk_widget_show(GTK_WIDGET (totd_dialog->dialog));
358 gnc_register_gui_component(DIALOG_TOTD_CM_CLASS,
359 NULL, close_handler, totd_dialog);
361 g_object_unref(G_OBJECT(builder));
gchar * gnc_filepath_locate_data_file(const gchar *name)
#define DEBUG(format, args...)
gboolean gnc_prefs_set_int(const gchar *group, const gchar *pref_name, gint value)
#define ENTER(format, args...)
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
#define PWARN(format, args...)
gboolean gnc_prefs_set_bool(const gchar *group, const gchar *pref_name, gboolean value)
Gnome specific utility functions.
All type declarations for the whole Gnucash engine.
Generic api to store and retrieve preferences.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
#define LEAVE(format, args...)
File path resolution utility functions.
const gchar * QofLogModule