~ubuntu-branches/ubuntu/vivid/xfce4-session/vivid-proposed

« back to all changes in this revision

Viewing changes to .pc/02_fix-fortune-path.patch/xfce4-tips/main.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2012-05-18 20:21:52 UTC
  • mfrom: (4.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20120518202152-7bm9pla0j3ccfsnk
Tags: 4.10.0-1ubuntu1
* Merge from Debian experimental, remaining Ubuntu changes:
  - debian/control: build-depends on libgnome-keyring-dev to enable GNOME
    Keyring services support.
  - debian/rules: pass --enable-libgnome-keyring to dh_auto_configure.
  - debian/patches:
    + xubuntu_ignore-gdm-lang.patch: do not set $LANG to $GDM_LANG, there's
      already an xsession script to do that, and $GDM_LANG might not contain
      a valid locale code.

* debian/patches:
  - xubuntu_set-xdg-current-desktop.patch: added. Taken from xfce4-utils,
    export XDG_CURRENT_DESKTOP=XFCE, can be useful with alacarte and
    gnome-menus. lp #927172

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
 
/*-
3
 
 * Copyright (c) 2003-2006 Benedikt Meurer <benny@xfce.org>
4
 
 * All rights reserved.
5
 
 *
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)
9
 
 * any later version.
10
 
 *
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.
15
 
 *
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,
19
 
 * MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#ifdef HAVE_MEMORY_H
27
 
#include <memory.h>
28
 
#endif
29
 
#include <stdio.h>
30
 
#ifdef HAVE_STDLIB_H
31
 
#include <stdlib.h>
32
 
#endif
33
 
#ifdef HAVE_STRING_H
34
 
#include <string.h>
35
 
#endif
36
 
 
37
 
#include <libxfce4ui/libxfce4ui.h>
38
 
#include <xfconf/xfconf.h>
39
 
 
40
 
 
41
 
 
42
 
#define OPTION_TIPS 0
43
 
#define OPTION_FORTUNES 1
44
 
 
45
 
 
46
 
 
47
 
static const gchar *titles[] = {
48
 
  N_("Tips and Tricks"),
49
 
  N_("Fortunes")
50
 
};
51
 
 
52
 
 
53
 
 
54
 
static GtkWidget *dlg = NULL;
55
 
static guint      option = OPTION_TIPS;
56
 
static gchar     *fortune_cmd = NULL;
57
 
static GPtrArray *tips = NULL;
58
 
 
59
 
 
60
 
static void
61
 
read_tips_from_file (void)
62
 
{
63
 
  gchar *data;
64
 
  gchar *entry;
65
 
  gsize len;
66
 
  guint i, j;
67
 
  GError *error = NULL;
68
 
 
69
 
  /* read the whole file */
70
 
  g_file_get_contents (TIPSDIR "/tips", &data, &len, &error);
71
 
 
72
 
  tips = g_ptr_array_new ();
73
 
  if (error != NULL)
74
 
    {
75
 
      g_ptr_array_add (tips, g_strdup_printf (_("Could not load tips database (%s)."),
76
 
        error->message));
77
 
      g_free (data);
78
 
      g_error_free (error);
79
 
      return;
80
 
    }
81
 
 
82
 
  entry = g_malloc (len + 1);
83
 
  i = j = 0;
84
 
  while (data[i])
85
 
    {
86
 
      if (data[i] == '%')
87
 
        {
88
 
          /* add a new tip */
89
 
          entry[j] = '\0';
90
 
          j = 0;
91
 
          if (entry[0])
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')
95
 
          i += 2;
96
 
          else
97
 
          i += 1;
98
 
        }
99
 
      else
100
 
        entry[j++] = data[i];
101
 
 
102
 
      i++;
103
 
    }
104
 
  g_free (data);
105
 
  g_free (entry);
106
 
}
107
 
 
108
 
 
109
 
static void
110
 
free_tip (gpointer data, gpointer user_data)
111
 
{
112
 
  g_free (data);
113
 
}
114
 
 
115
 
 
116
 
static gboolean
117
 
autostart_enabled (void)
118
 
{
119
 
  gboolean enabled = FALSE;
120
 
  XfceRc  *rc;
121
 
 
122
 
  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, "autostart/xfce4-tips-autostart.desktop", TRUE);
123
 
  if (G_LIKELY (rc != NULL))
124
 
    {
125
 
      xfce_rc_set_group (rc, "Desktop Entry");
126
 
      enabled = !xfce_rc_read_bool_entry (rc, "Hidden", TRUE);
127
 
      xfce_rc_close (rc);
128
 
    }
129
 
 
130
 
  return enabled;
131
 
}
132
 
 
133
 
 
134
 
 
135
 
static void
136
 
autostart_toggled (GtkToggleButton *button)
137
 
{
138
 
  gboolean active = gtk_toggle_button_get_active (button);
139
 
  XfceRc  *rc;
140
 
 
141
 
  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, "autostart/xfce4-tips-autostart.desktop", FALSE);
142
 
  if (G_LIKELY (rc != NULL))
143
 
    {
144
 
      xfce_rc_set_group (rc, "Desktop Entry");
145
 
      xfce_rc_write_bool_entry (rc, "Hidden", !active);
146
 
      xfce_rc_close (rc);
147
 
    }
148
 
}
149
 
 
150
 
 
151
 
 
152
 
static void
153
 
item_cb (GtkComboBox *combobox, XfconfChannel *channel)
154
 
{
155
 
  gint active_item = gtk_combo_box_get_active (combobox);
156
 
 
157
 
  if (active_item >=0)
158
 
    {
159
 
      option = active_item;
160
 
      xfconf_channel_set_bool (channel, "/fortunes", option == 1);
161
 
    }
162
 
 
163
 
  gtk_window_set_title (GTK_WINDOW (dlg), _(titles[option]));
164
 
}
165
 
 
166
 
 
167
 
 
168
 
static gchar*
169
 
run_fortune (void)
170
 
{
171
 
  GError *error = NULL;
172
 
  gchar *out = NULL;
173
 
  gchar *err = NULL;
174
 
  gchar *buffer = NULL;
175
 
 
176
 
  if (fortune_cmd != NULL && g_spawn_command_line_sync (fortune_cmd, &out, &err, NULL, &error))
177
 
    {
178
 
      if (out != NULL && *out != '\0')
179
 
        {
180
 
          /* check output for valid UTF-8 */
181
 
          if (g_utf8_validate (out, -1, NULL))
182
 
            buffer = out;
183
 
          else
184
 
            {
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);
187
 
              if (buffer == NULL)
188
 
              {
189
 
                /* converting it from the user's locale failed too, we give up */
190
 
                buffer = g_strdup_printf (_("Invalid output of fortune."));
191
 
              }
192
 
            }
193
 
          }
194
 
      else
195
 
        buffer = g_strdup_printf (_("Executing fortune failed (%s)"), err);
196
 
 
197
 
      if (buffer != out)
198
 
        g_free (out);
199
 
      g_free(err);
200
 
    }
201
 
  else
202
 
    {
203
 
      buffer = g_strdup_printf (_("Executing fortune failed (%s)"), error->message);
204
 
      g_error_free (error);
205
 
    }
206
 
 
207
 
  return buffer;
208
 
}
209
 
 
210
 
 
211
 
 
212
 
static void
213
 
next_cb(GtkWidget *widget, GtkTextBuffer *textbuf)
214
 
{
215
 
  gchar *buffer = NULL;
216
 
  GtkTextIter start;
217
 
  GtkTextIter end;
218
 
 
219
 
  /* clear the text buffer */
220
 
  gtk_text_buffer_get_bounds (textbuf, &start, &end);
221
 
  gtk_text_buffer_delete (textbuf, &start, &end);
222
 
 
223
 
  switch (option)
224
 
    {
225
 
      case OPTION_TIPS:
226
 
        {
227
 
          if (! tips || tips->len == 0)
228
 
            buffer = _("Error while loading tips.");
229
 
          else
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));
232
 
          break;
233
 
        }
234
 
      case OPTION_FORTUNES:
235
 
        {
236
 
          buffer = run_fortune ();
237
 
          break;
238
 
        }
239
 
    }
240
 
 
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);
244
 
 
245
 
  if (option == OPTION_FORTUNES)
246
 
    g_free (buffer);
247
 
}
248
 
 
249
 
 
250
 
 
251
 
int
252
 
main (int argc, char **argv)
253
 
{
254
 
  GError    *error = NULL;
255
 
  GtkWidget *sw;
256
 
  GtkWidget *view;
257
 
  GtkWidget *vbox2;
258
 
  GtkWidget *check;
259
 
  GtkWidget *combobox;
260
 
  GtkWidget *next;
261
 
  GtkWidget *close_btn;
262
 
  GtkWidget *action_area;
263
 
  XfconfChannel *channel;
264
 
 
265
 
  xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
266
 
 
267
 
  gtk_init (&argc, &argv);
268
 
 
269
 
  if (G_UNLIKELY (!xfconf_init (&error))) {
270
 
    xfce_dialog_show_error (NULL, error, _("Unable to contact settings server"));
271
 
    g_error_free (error);
272
 
  }
273
 
 
274
 
  channel = xfconf_channel_get ("xfce4-tips");
275
 
 
276
 
  /* test for fortune */
277
 
  fortune_cmd = g_find_program_in_path ("fortune");
278
 
 
279
 
  read_tips_from_file ();
280
 
 
281
 
  /* fake a SM client id, so the session manager does not restart us */
282
 
  gdk_set_sm_client_id ("FAKED CLIENTID");
283
 
 
284
 
  dlg = xfce_titled_dialog_new_with_buttons (_("Tips and Tricks"), NULL,
285
 
                                             GTK_DIALOG_NO_SEPARATOR,
286
 
                                             NULL);
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));
291
 
 
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);
294
 
 
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);
299
 
 
300
 
  sw = gtk_scrolled_window_new (NULL, NULL);
301
 
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
302
 
                                  GTK_POLICY_NEVER,
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);
307
 
 
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);
313
 
 
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);
319
 
 
320
 
  if (fortune_cmd != NULL)
321
 
    {
322
 
      gboolean fortunes = xfconf_channel_get_bool (channel, "/fortunes", FALSE);
323
 
 
324
 
      combobox = gtk_combo_box_new_text ();
325
 
      gtk_widget_show (combobox);
326
 
 
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]);
329
 
 
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)));
332
 
 
333
 
      gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), fortunes ? 1 : 0);
334
 
 
335
 
      gtk_box_pack_start (GTK_BOX (action_area), combobox, FALSE, FALSE, 0);
336
 
    }
337
 
 
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);
341
 
 
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);
345
 
 
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)));
350
 
 
351
 
  next_cb (next, gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
352
 
 
353
 
  gtk_widget_show (dlg);
354
 
 
355
 
  gtk_main ();
356
 
 
357
 
  /* cleanup */
358
 
  g_free (fortune_cmd);
359
 
  if (tips != NULL)
360
 
    {
361
 
      g_ptr_array_foreach (tips, free_tip, NULL);
362
 
      g_ptr_array_free (tips, TRUE);
363
 
    }
364
 
 
365
 
  xfconf_shutdown ();
366
 
 
367
 
  return EXIT_SUCCESS;
368
 
}