3
* Copyright (c) 2003-2006 Benedikt Meurer <benny@xfce.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
37
#include <libxfce4ui/libxfce4ui.h>
38
#include <xfconf/xfconf.h>
43
#define OPTION_FORTUNES 1
47
static const gchar *titles[] = {
48
N_("Tips and Tricks"),
54
static GtkWidget *dlg = NULL;
55
static guint option = OPTION_TIPS;
56
static gchar *fortune_cmd = NULL;
57
static GPtrArray *tips = NULL;
61
read_tips_from_file (void)
69
/* read the whole file */
70
g_file_get_contents (TIPSDIR "/tips", &data, &len, &error);
72
tips = g_ptr_array_new ();
75
g_ptr_array_add (tips, g_strdup_printf (_("Could not load tips database (%s)."),
82
entry = g_malloc (len + 1);
92
g_ptr_array_add (tips, g_strdup(entry));
93
/* skip the following line break character(s) */
94
if (data[i] == '\r' && (i + 1) < len && data[i + 1] == '\n')
100
entry[j++] = data[i];
110
free_tip (gpointer data, gpointer user_data)
117
autostart_enabled (void)
119
gboolean enabled = FALSE;
122
rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, "autostart/xfce4-tips-autostart.desktop", TRUE);
123
if (G_LIKELY (rc != NULL))
125
xfce_rc_set_group (rc, "Desktop Entry");
126
enabled = !xfce_rc_read_bool_entry (rc, "Hidden", TRUE);
136
autostart_toggled (GtkToggleButton *button)
138
gboolean active = gtk_toggle_button_get_active (button);
141
rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, "autostart/xfce4-tips-autostart.desktop", FALSE);
142
if (G_LIKELY (rc != NULL))
144
xfce_rc_set_group (rc, "Desktop Entry");
145
xfce_rc_write_bool_entry (rc, "Hidden", !active);
153
item_cb (GtkComboBox *combobox, XfconfChannel *channel)
155
gint active_item = gtk_combo_box_get_active (combobox);
159
option = active_item;
160
xfconf_channel_set_bool (channel, "/fortunes", option == 1);
163
gtk_window_set_title (GTK_WINDOW (dlg), _(titles[option]));
171
GError *error = NULL;
174
gchar *buffer = NULL;
176
if (fortune_cmd != NULL && g_spawn_command_line_sync (fortune_cmd, &out, &err, NULL, &error))
178
if (out != NULL && *out != '\0')
180
/* check output for valid UTF-8 */
181
if (g_utf8_validate (out, -1, NULL))
185
/* we got something else than UTF-8, try to convert it from the user's locale */
186
buffer = g_locale_to_utf8 (out, -1, NULL, NULL, NULL);
189
/* converting it from the user's locale failed too, we give up */
190
buffer = g_strdup_printf (_("Invalid output of fortune."));
195
buffer = g_strdup_printf (_("Executing fortune failed (%s)"), err);
203
buffer = g_strdup_printf (_("Executing fortune failed (%s)"), error->message);
204
g_error_free (error);
213
next_cb(GtkWidget *widget, GtkTextBuffer *textbuf)
215
gchar *buffer = NULL;
219
/* clear the text buffer */
220
gtk_text_buffer_get_bounds (textbuf, &start, &end);
221
gtk_text_buffer_delete (textbuf, &start, &end);
227
if (! tips || tips->len == 0)
228
buffer = _("Error while loading tips.");
230
/* no need to check or convert the encoding of our own tips file as it is already UTF-8 */
231
buffer = g_ptr_array_index (tips, g_random_int_range(0, tips->len));
234
case OPTION_FORTUNES:
236
buffer = run_fortune ();
241
/* add the text to the buffer */
242
gtk_text_buffer_get_end_iter (textbuf, &end);
243
gtk_text_buffer_insert (textbuf, &end, buffer, -1);
245
if (option == OPTION_FORTUNES)
252
main (int argc, char **argv)
254
GError *error = NULL;
261
GtkWidget *close_btn;
262
GtkWidget *action_area;
263
XfconfChannel *channel;
265
xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
267
gtk_init (&argc, &argv);
269
if (G_UNLIKELY (!xfconf_init (&error))) {
270
xfce_dialog_show_error (NULL, error, _("Unable to contact settings server"));
271
g_error_free (error);
274
channel = xfconf_channel_get ("xfce4-tips");
276
/* test for fortune */
277
fortune_cmd = g_find_program_in_path ("fortune");
279
read_tips_from_file ();
281
/* fake a SM client id, so the session manager does not restart us */
282
gdk_set_sm_client_id ("FAKED CLIENTID");
284
dlg = xfce_titled_dialog_new_with_buttons (_("Tips and Tricks"), NULL,
285
GTK_DIALOG_NO_SEPARATOR,
287
gtk_window_set_icon_name (GTK_WINDOW (dlg), "xfce4-logo");
288
gtk_window_set_default_size (GTK_WINDOW (dlg), 600, 400);
289
gtk_window_set_position (GTK_WINDOW (dlg), GTK_WIN_POS_CENTER);
290
gtk_window_stick (GTK_WINDOW (dlg));
292
action_area = gtk_dialog_get_action_area (GTK_DIALOG (dlg));
293
gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_EDGE);
295
vbox2 = gtk_vbox_new (FALSE, 6);
296
gtk_container_set_border_width (GTK_CONTAINER (vbox2), 6);
297
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))), vbox2, TRUE, TRUE, 0);
298
gtk_widget_show (vbox2);
300
sw = gtk_scrolled_window_new (NULL, NULL);
301
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
303
GTK_POLICY_AUTOMATIC);
304
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
305
gtk_widget_show (sw);
306
gtk_box_pack_start (GTK_BOX (vbox2), sw, TRUE, TRUE, 0);
308
view = gtk_text_view_new ();
309
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
310
gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
311
gtk_widget_show (view);
312
gtk_container_add (GTK_CONTAINER (sw), view);
314
check = gtk_check_button_new_with_mnemonic (_("Display tips on _startup"));
315
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), autostart_enabled ());
316
g_signal_connect (G_OBJECT (check), "toggled", G_CALLBACK (autostart_toggled), NULL);
317
gtk_box_pack_start (GTK_BOX (vbox2), check, FALSE, FALSE, 0);
318
gtk_widget_show (check);
320
if (fortune_cmd != NULL)
322
gboolean fortunes = xfconf_channel_get_bool (channel, "/fortunes", FALSE);
324
combobox = gtk_combo_box_new_text ();
325
gtk_widget_show (combobox);
327
gtk_combo_box_append_text (GTK_COMBO_BOX (combobox), titles[OPTION_TIPS]);
328
gtk_combo_box_append_text (GTK_COMBO_BOX (combobox), titles[OPTION_FORTUNES]);
330
g_signal_connect (combobox, "changed", G_CALLBACK (item_cb), channel);
331
g_signal_connect (combobox, "changed", G_CALLBACK (next_cb), gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
333
gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), fortunes ? 1 : 0);
335
gtk_box_pack_start (GTK_BOX (action_area), combobox, FALSE, FALSE, 0);
338
next = gtk_button_new_with_label (_("Next"));
339
gtk_dialog_add_action_widget (GTK_DIALOG (dlg), next, GTK_RESPONSE_NONE);
340
gtk_widget_show (next);
342
close_btn = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
343
gtk_dialog_add_action_widget (GTK_DIALOG (dlg), close_btn, GTK_RESPONSE_DELETE_EVENT);
344
gtk_widget_show (close_btn);
346
g_signal_connect (dlg, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
347
g_signal_connect (dlg, "destroy-event", G_CALLBACK (gtk_main_quit), NULL);
348
g_signal_connect (close_btn, "clicked", G_CALLBACK (gtk_main_quit), NULL);
349
g_signal_connect (next, "clicked", G_CALLBACK (next_cb), gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
351
next_cb (next, gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
353
gtk_widget_show (dlg);
358
g_free (fortune_cmd);
361
g_ptr_array_foreach (tips, free_tip, NULL);
362
g_ptr_array_free (tips, TRUE);