Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/gsm-app.c
blob: 3a63bacf7c2dbb2bcce72382cd940642b8826a53 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* app.c
 * Copyright (C) 2007 Novell, Inc.
 *
 * 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 of the
 * License, 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
 * Lesser 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>
#include <string.h>
#include <sys/wait.h>

#include "gsm-app.h"

enum {
  EXITED,
  REGISTERED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

enum {
	PROP_0,

	PROP_DESKTOP_FILE,
	PROP_CLIENT_ID,

	LAST_PROP
};

static void set_property (GObject *object, guint prop_id,
			  const GValue *value, GParamSpec *pspec);
static void get_property (GObject *object, guint prop_id,
			  GValue *value, GParamSpec *pspec);
static void dispose      (GObject *object);

static const char *get_basename (GsmApp *app);
static pid_t       launch       (GsmApp *app, GError **err);

G_DEFINE_TYPE (GsmApp, gsm_app, G_TYPE_OBJECT)

static void
gsm_app_init (GsmApp *app)
{
  app->pid = -1;
}

static void
gsm_app_class_init (GsmAppClass *app_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (app_class);

  object_class->set_property = set_property;
  object_class->get_property = get_property;
  object_class->dispose = dispose;

  app_class->get_basename = get_basename;
  app_class->launch = launch;

  g_object_class_install_property (object_class,
				   PROP_DESKTOP_FILE,
				   g_param_spec_string ("desktop-file",
							"Desktop file",
							"Freedesktop .desktop file",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_CLIENT_ID,
				   g_param_spec_string ("client-id",
							"Client ID",
							"Session management client ID",
							NULL,
							G_PARAM_READWRITE));

  signals[EXITED] =
    g_signal_new ("exited",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GsmAppClass, exited),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);

  signals[REGISTERED] =
    g_signal_new ("registered",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GsmAppClass, registered),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);
}

static void
set_property (GObject *object, guint prop_id,
	      const GValue *value, GParamSpec *pspec)
{
  GsmApp *app = GSM_APP (object);
  const char *desktop_file;
  char *phase;
  GError *error = NULL;

  switch (prop_id)
    {
    case PROP_DESKTOP_FILE:
      if (app->desktop_file)
	egg_desktop_file_free (app->desktop_file);
      desktop_file = g_value_get_string (value);
      if (!desktop_file)
	{
	  app->desktop_file = NULL;
	  break;
	}

      app->desktop_file = egg_desktop_file_new (desktop_file, &error);
      if (!app->desktop_file)
	{
	  g_warning ("Could not parse desktop file %s: %s",
		     desktop_file, error->message);
	  g_error_free (error);
	  break;
	}

      phase = egg_desktop_file_get_string (app->desktop_file,
					   "X-GNOME-Autostart-Phase", NULL);
      if (phase)
	{
	  if (!strcmp (phase, "Initialization"))
	    app->phase = GSM_SESSION_PHASE_INITIALIZATION;
	  else if (!strcmp (phase, "WindowManager"))
	    app->phase = GSM_SESSION_PHASE_WINDOW_MANAGER;
	  else if (!strcmp (phase, "Panel"))
	    app->phase = GSM_SESSION_PHASE_PANEL;
	  else if (!strcmp (phase, "Desktop"))
	    app->phase = GSM_SESSION_PHASE_DESKTOP;
	  else
	    app->phase = GSM_SESSION_PHASE_APPLICATION;

	  g_free (phase);
	}
      else
	app->phase = GSM_SESSION_PHASE_APPLICATION;
      break;

    case PROP_CLIENT_ID:
      g_free (app->client_id);
      app->client_id = g_value_dup_string (value);
      break;

    default:
      break;
    }
}

static void
get_property (GObject *object, guint prop_id,
	      GValue *value, GParamSpec *pspec)
{
  GsmApp *app = GSM_APP (object);

  switch (prop_id)
    {
    case PROP_DESKTOP_FILE:
      if (app->desktop_file)
	g_value_set_string (value, egg_desktop_file_get_source (app->desktop_file));
      else
	g_value_set_string (value, NULL);
      break;

    case PROP_CLIENT_ID:
      g_value_set_string (value, app->client_id);
      break;

    default:
      break;
    }
}

static void
dispose(GObject *object)
{
  GsmApp *app = GSM_APP (object);

  if (app->desktop_file)
    {
      egg_desktop_file_free (app->desktop_file);
      app->desktop_file = NULL;
    }

  if (app->startup_id)
    {
      g_free (app->startup_id);
      app->startup_id = NULL;
    }

  if (app->client_id)
    {
      g_free (app->client_id);
      app->client_id = NULL;
    }
}

/**
 * gsm_app_get_basename:
 * @app: a %GsmApp
 *
 * Returns an identifying name for @app, e.g. the basename of the path to
 * @app's desktop file (if any).
 *
 * Return value: an identifying name for @app, or %NULL.
 **/
const char *
gsm_app_get_basename (GsmApp *app)
{
  return GSM_APP_GET_CLASS (app)->get_basename (app);
}

static const char *
get_basename (GsmApp *app)
{
  const char *location, *slash;

  if (!app->desktop_file)
    return NULL;

  location = egg_desktop_file_get_source (app->desktop_file);

  slash = strrchr (location, '/');
  if (slash)
    return slash + 1;
  else
    return location;
}

/**
 * gsm_app_get_phase:
 * @app: a %GsmApp
 *
 * Returns @app's startup phase.
 *
 * Return value: @app's startup phase
 **/
GsmSessionPhase
gsm_app_get_phase (GsmApp *app)
{
  g_return_val_if_fail (GSM_IS_APP (app), GSM_SESSION_PHASE_APPLICATION);

  return app->phase;
}

/**
 * gsm_app_is_disabled:
 * @app: a %GsmApp
 *
 * Tests if @app is disabled
 *
 * Return value: whether or not @app is disabled
 **/
gboolean
gsm_app_is_disabled (GsmApp *app)
{
  g_return_val_if_fail (GSM_IS_APP (app), FALSE);

  if (GSM_APP_GET_CLASS (app)->is_disabled)
    return GSM_APP_GET_CLASS (app)->is_disabled (app);
  else
    return FALSE;
}

gboolean
gsm_app_provides (GsmApp *app, const char *service)
{
  char **provides;
  gsize len, i;

  g_return_val_if_fail (GSM_IS_APP (app), FALSE);

  if (!app->desktop_file)
    return FALSE;

  provides = egg_desktop_file_get_string_list (app->desktop_file,
					       "X-GNOME-Provides",
					       &len, NULL);
  if (!provides)
    return FALSE;

  for (i = 0; i < len; i++)
    {
      if (!strcmp (provides[i], service))
	{
	  g_strfreev (provides);
	  return TRUE;
	}
    }

  g_strfreev (provides);
  return FALSE;
}

static void 
app_exited (GPid pid, gint status, gpointer data)
{
  if (WIFEXITED (status))
    g_signal_emit (GSM_APP (data), signals[EXITED], 0);
}

static pid_t
launch (GsmApp  *app,
	GError **err)
{
  char *env[2] = { NULL, NULL };
  gboolean success;

  g_return_val_if_fail (app->desktop_file != NULL, (pid_t)-1);

  if (egg_desktop_file_get_boolean (app->desktop_file,
				    "X-GNOME-Autostart-Notify", NULL) ||
      egg_desktop_file_get_boolean (app->desktop_file,
				    "AutostartNotify", NULL))
    env[0] = g_strdup_printf ("DESKTOP_AUTOSTART_ID=%s", app->client_id);

#if 0
  g_debug ("launching %s with client_id %s\n",
	    gsm_app_get_basename (app), app->client_id);
#endif

  success =
    egg_desktop_file_launch (app->desktop_file, NULL, err,
			     EGG_DESKTOP_FILE_LAUNCH_PUTENV, env,
			     EGG_DESKTOP_FILE_LAUNCH_FLAGS, G_SPAWN_DO_NOT_REAP_CHILD,
			     EGG_DESKTOP_FILE_LAUNCH_RETURN_PID, &app->pid,
			     EGG_DESKTOP_FILE_LAUNCH_RETURN_STARTUP_ID, &app->startup_id,
			     NULL);

  g_free (env[0]);

  if (success)
    {
      /* In case the app belongs to Initialization phase, we monitor
       * if it exits to emit proper "exited" signal to session. */
      if (app->phase == GSM_SESSION_PHASE_INITIALIZATION)
        g_child_watch_add ((GPid) app->pid, app_exited, app);

      return app->pid;
    }
  else
    return (pid_t) -1;
}

/**
 * gsm_app_launch:
 * @app: a %GsmApp
 * @err: an error pointer
 *
 * Launches @app
 *
 * Return value: the pid of the new process, or -1 on error
 **/
pid_t
gsm_app_launch (GsmApp *app, GError **err)
{
  return GSM_APP_GET_CLASS (app)->launch (app, err);
}

/**
 * gsm_app_registered:
 * @app: a %GsmApp
 *
 * Emits "registered" signal in @app
 **/
void
gsm_app_registered (GsmApp *app)
{
  g_return_if_fail (GSM_IS_APP (app));

  g_signal_emit (app, signals[REGISTERED], 0);
}