28 #include <glib/gi18n.h> 30 #include "dialog-totd.h" 31 #include "dialog-utils.h" 32 #include "gnc-component-manager.h" 39 #define GNC_PREFS_GROUP "dialogs.totd" 40 #define GNC_PREF_CURRENT_TIP "current-tip" 41 #define GNC_PREF_SHOW_TIPS "show-at-startup" 42 #define DIALOG_TOTD_CM_CLASS "dialog-totd" 44 #define GNC_RESPONSE_FORWARD 1 45 #define GNC_RESPONSE_BACK 2 48 void gnc_totd_dialog_response_cb (GtkDialog *dialog, gint response, gpointer user_data);
49 void gnc_totd_dialog_startup_toggled_cb (GtkToggleButton *button, gpointer user_data);
52 static gchar **tip_list;
53 static gint tip_count = -1;
54 static gint current_tip_number = -1;
57 static QofLogModule log_module = GNC_MOD_GUI;
62 GtkTextView *textview;
63 GtkWidget *showcheck_button;
82 gnc_new_tip_number (
TotdDialog *totd_dialog, gint offset)
85 gchar **tip_components = NULL;
88 ENTER(
"TotdDialog %p, offset %d", totd_dialog, offset);
89 g_return_if_fail (tip_list != NULL);
90 current_tip_number += offset;
91 DEBUG(
"clamp %d to '0 <= x < %d'", current_tip_number, tip_count);
92 if (current_tip_number < 0)
93 current_tip_number = tip_count - 1;
94 if (current_tip_number >= tip_count)
95 current_tip_number = 0;
104 if (tip_list[current_tip_number])
105 tip_components = g_strsplit(tip_list[current_tip_number],
"|", 0);
109 if (tip_components == NULL)
119 tip = g_strdup_printf( _(tip_components[0]), tip_components[1]);
122 g_strfreev(tip_components);
123 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(totd_dialog->textview)),
133 void gnc_totd_dialog_response_cb (GtkDialog *dialog,
139 ENTER(
"dialog %p, response %d, user_data %p", dialog, response, user_data);
142 case GNC_RESPONSE_FORWARD:
143 gnc_new_tip_number(totd_dialog, 1);
146 case GNC_RESPONSE_BACK:
147 gnc_new_tip_number(totd_dialog, -1);
150 case GTK_RESPONSE_CLOSE:
151 gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(totd_dialog->dialog));
155 gnc_unregister_gui_component_by_data(DIALOG_TOTD_CM_CLASS, totd_dialog);
156 gtk_widget_destroy(GTK_WIDGET(totd_dialog->dialog));
164 gnc_totd_dialog_startup_toggled_cb (GtkToggleButton *button,
169 active = gtk_toggle_button_get_active(button);
178 gnc_totd_initialize (
void)
180 gchar *filename = NULL, *contents = NULL, *new_str = NULL;
182 GError *error = NULL;
191 if (!g_file_get_contents(filename, &contents, &length, &error))
193 printf(
"Unable to read file: %s\n", error->message);
204 tip_list = g_strsplit(contents,
"\n", 0);
209 tip_count = g_strv_length (tip_list);
212 for (tip = 0; tip < tip_count; ++tip)
214 if (*tip_list[tip] !=
'\0')
216 g_strstrip(tip_list[tip]);
218 contents = g_strdup (tip_list[tip]);
221 new_str = g_strjoin (
"\n", contents, tip_list[tip], NULL);
229 g_strfreev (tip_list);
233 tip_list = g_strsplit(contents,
"\n", 0);
234 tip_count = g_strv_length (tip_list);
237 for (tip = 0; tip < tip_count; ++tip)
239 new_str = g_strcompress(tip_list[tip]);
240 g_free(tip_list[tip]);
241 tip_list[tip] = new_str;
267 show_handler (
const char *class_name, gint component_id,
268 gpointer user_data, gpointer iter_data)
275 LEAVE(
"no data structure");
279 gtk_window_set_transient_for (GTK_WINDOW (totd_dialog->dialog),
294 close_handler (gpointer user_data)
300 gnc_unregister_gui_component_by_data(DIALOG_TOTD_CM_CLASS, totd_dialog);
310 gnc_totd_dialog (GtkWindow *parent, gboolean startup)
315 GtkWidget *dialog, *button;
316 GtkTextView *textview;
320 if (startup && !show_tips)
325 if (!gnc_totd_initialize())
336 PWARN(
"No tips found - Tips of the day window won't be displayed.");
339 if (gnc_forall_gui_components(DIALOG_TOTD_CM_CLASS, show_handler, NULL))
344 builder = gtk_builder_new();
345 gnc_builder_add_from_file (builder,
"dialog-totd.glade",
"totd_dialog");
346 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"totd_dialog"));
347 gtk_window_set_transient_for(GTK_WINDOW (dialog), parent);
350 gtk_widget_set_name (GTK_WIDGET(dialog),
"gnc-id-tip-of-the-day");
353 totd_dialog->dialog = dialog;
355 ENTER(
"totd_dialog %p, dialog %p", totd_dialog, dialog);
357 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, totd_dialog);
359 button = GTK_WIDGET(gtk_builder_get_object (builder,
"show_checkbutton"));
360 totd_dialog->showcheck_button = button;
362 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (button), show_tips);
364 textview = GTK_TEXT_VIEW(gtk_builder_get_object (builder,
"tip_textview"));
365 totd_dialog->textview = textview;
367 gnc_new_tip_number(totd_dialog, 1);
369 gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(totd_dialog->dialog), parent);
370 gtk_widget_show(GTK_WIDGET (totd_dialog->dialog));
372 gnc_register_gui_component(DIALOG_TOTD_CM_CLASS,
373 NULL, close_handler, totd_dialog);
375 g_object_unref(G_OBJECT(builder));
389 gnc_totd_dialog_reparent (
void)
391 gnc_forall_gui_components(DIALOG_TOTD_CM_CLASS, show_handler, NULL);
gchar * gnc_filepath_locate_data_file(const gchar *name)
Given a file name, find the file in the directories associated with this application.
GtkWindow * gnc_ui_get_main_window(GtkWidget *widget)
Get a pointer to the final GncMainWindow widget is rooted in.
#define DEBUG(format, args...)
Print a debugging message.
gboolean gnc_prefs_set_int(const gchar *group, const gchar *pref_name, gint value)
Store an integer value into the preferences backend.
#define ENTER(format, args...)
Print a function entry debugging message.
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
#define PWARN(format, args...)
Log a warning.
gboolean gnc_prefs_set_bool(const gchar *group, const gchar *pref_name, gboolean value)
Store a boolean value into the preferences backend.
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)
Get a boolean value from the preferences backend.
#define LEAVE(format, args...)
Print a function exit debugging message.
File path resolution utility functions.