summaryrefslogtreecommitdiff
path: root/xfce4-session/xfsm-compat-gnome.c
blob: afef123ec86c24b028bd4e065a89129a49ed8318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* $Id$ */
/*-
 * Copyright (c) 2004 Benedikt Meurer <benny@xfce.org>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *                                                                              
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *                                                                              
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Most parts of this file where taken from gnome-session.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_GNOME

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <libgnome/libgnome.h>
#include <libxfce4util/libxfce4util.h>
#include <gconf/gconf-client.h>

#include <xfce4-session/xfsm-compat-gnome.h>


#define ACCESSIBILITY_KEY "/desktop/gnome/interface/accessibility"
#define AT_STARTUP_KEY    "/desktop/gnome/accessibility/startup/exec_ats"


static GConfClient *gnome_conf_client = NULL;
static pid_t gnome_keyring_daemon_pid = 0;

static void
gnome_keyring_daemon_startup (void)
{
  GError *error = NULL;
  gchar  *sout;
  gchar **lines;
  gint    status;
  long    pid;
  gchar  *pid_str;
  gchar  *end;
  
  g_spawn_command_line_sync ("gnome-keyring-daemon",
                             &sout, NULL, &status, &error);

  if (error != NULL)
    {
      g_printerr ("Failed to run gnome-keyring-daemon: %s\n",
                  error->message);
      g_error_free (error);
    }
  else
    {
      if (WIFEXITED (status) && WEXITSTATUS (status) == 0 && sout != NULL)
        {
          lines = g_strsplit (sout, "\n", 3);

          if (lines[0] != NULL && lines[1] != NULL
              && g_str_has_prefix (lines[1], "GNOME_KEYRING_PID="))
          {
            pid_str = lines[1] + strlen ("GNOME_KEYRING_PID=");
            pid = strtol (pid_str, &end, 10);

            if (end != pid_str)
              {
                gnome_keyring_daemon_pid = pid;
                xfce_putenv (lines[0]);
              }
          }

          g_strfreev (lines);
        }
      else
        {
          /* daemon failed for some reason */
          g_printerr ("gnome-keyring-daemon failed to start correctly, "
                      "exit code: %d\n", WEXITSTATUS (status));
        }
      
      g_free (sout);
    }
}

static void
gnome_keyring_daemon_shutdown (void)
{
  if (gnome_keyring_daemon_pid != 0)
    {
      kill (gnome_keyring_daemon_pid, SIGTERM);
      gnome_keyring_daemon_pid = 0;
    }
}


static void
gnome_ast_startup (void)
{
  GError *error = NULL;
  GSList *list;
  GSList *lp;
  gchar  *path;

  list = gconf_client_get_list (gnome_conf_client, AT_STARTUP_KEY,
                                GCONF_VALUE_STRING, &error);
  
  if (error != NULL)
    {
      g_warning ("Failed to query value of " AT_STARTUP_KEY ": %s",
                 error->message);
      g_error_free (error);
    }
  else
    {
      for (lp = list; lp != NULL; lp = lp->next)
        {
          path = g_find_program_in_path ((const gchar *) lp->data);
          if (path != NULL)
            {
              g_spawn_command_line_async (path, &error);
              if (error != NULL)
                {
                  g_warning ("Failed to execute assistive helper %s: %s",
                             path, error->message);
                  g_error_free (error);
                }
              else
                {
                  /* give it some time to fire up */
                  g_usleep (50 * 1000);
                }
              g_free (path);
            }
          g_free (lp->data);
        }
      g_slist_free (list);
    }
}


void
xfsm_compat_gnome_startup (XfsmSplashScreen *splash)
{
  /* fire up the keyring daemon */
  xfsm_splash_screen_next (splash, _("Starting The Gnome Keyring Daemon"));
  gnome_keyring_daemon_startup ();

  /* connect to the GConf daemon */
  gnome_conf_client = gconf_client_get_default ();
  if (gnome_conf_client != NULL)
    {
      if (gconf_client_get_bool (gnome_conf_client, ACCESSIBILITY_KEY, NULL))
        {
          xfsm_splash_screen_next (splash,
                                   _("Starting Gnome Assistive Technologies"));
          gnome_ast_startup ();
        }
    }
}


void
xfsm_compat_gnome_shutdown (void)
{
  GError *error = NULL;
  gint    status;

  /* shutdown the keyring daemon */
  gnome_keyring_daemon_shutdown ();

  if (gnome_conf_client != NULL)
    {
      g_object_unref (G_OBJECT (gnome_conf_client));
      gnome_conf_client = NULL;
    }

  /* shutdown the GConf daemon */
  if (!g_spawn_command_line_sync ("gconftool-2 --shutdown", NULL,
                                  NULL, &status, &error))
    {
      g_warning ("Failed to shutdown the GConf daemon on logout: %s",
                 error->message);
      g_error_free (error);
    }
  else if (status != 0)
    {
      g_warning ("Failed to shutdown the GConf daemon on logout: "
                 "gconftool-2 returned status %d", status);
    }
}

#endif