~ubuntu-branches/ubuntu/maverick/gnome-session/maverick

« back to all changes in this revision

Viewing changes to gnome-session/gnome-session-remove.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2009-04-14 19:24:09 UTC
  • mto: (1.3.1 upstream) (2.1.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 112.
  • Revision ID: james.westby@ubuntu.com-20090414192409-koxw9bt1lf1zr01z
ImportĀ upstreamĀ versionĀ 2.26.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003 Sun Microsystems, Inc.
3
 
 *
4
 
 * This library is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Library General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2 of the License, or (at your option) any later version.
8
 
 *
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Library General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Library General Public
15
 
 * License along with this library; if not, write to the
16
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
 * Boston, MA 02111-1307, USA.
18
 
 *
19
 
 * Author:
20
 
 *    Mark McLoughlin <mark@skynet.ie>
21
 
 */
22
 
 
23
 
#include <config.h>
24
 
 
25
 
#include <string.h>
26
 
 
27
 
#include <glib/gi18n.h>
28
 
#include <gtk/gtk.h>
29
 
#include <libgnome/gnome-program.h>
30
 
#include <libgnomeui/gnome-client.h>
31
 
#include <libgnomeui/gnome-ui-init.h>
32
 
 
33
 
#include "gsm-protocol.h"
34
 
 
35
 
static GSList   *clients;
36
 
static gboolean  do_list;
37
 
 
38
 
/* The number of names of programs we've seen.  */
39
 
static int program_count;
40
 
 
41
 
/* The names of the programs we're looking to remove.  */
42
 
static char **program_names;
43
 
 
44
 
/* A count of the number of client programs we are trying to
45
 
   remove.  */
46
 
static int remove_count;
47
 
 
48
 
static const GOptionEntry gsm_remove_opts[] = {
49
 
  { "list", 0, 0, G_OPTION_ARG_NONE, &do_list,
50
 
    N_("List registered clients, then exit"), NULL },
51
 
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
52
 
    &program_names, NULL, N_("PROGRAM...") },
53
 
  {NULL}
54
 
};
55
 
 
56
 
static GsmClient *
57
 
client_factory (gpointer user_data)
58
 
{
59
 
  GsmClient *client;
60
 
 
61
 
  client = g_object_new (GSM_TYPE_CLIENT, NULL);
62
 
 
63
 
  clients = g_slist_prepend (clients, client);
64
 
 
65
 
  return client;
66
 
}
67
 
 
68
 
static void
69
 
client_removed (void)
70
 
{
71
 
  if (--remove_count == 0)
72
 
    gtk_main_quit ();
73
 
}
74
 
 
75
 
static int
76
 
find_program (char *name)
77
 
{
78
 
  if (name != NULL)
79
 
    {
80
 
      int i;
81
 
      for (i = 0; i < program_count; ++i)
82
 
        if (program_names[i] && ! strcmp (name, program_names[i]))
83
 
          return i;
84
 
    }
85
 
  return -1;
86
 
}
87
 
 
88
 
static void
89
 
session_initialized (GsmSession *session, void *ignore)
90
 
{
91
 
  GSList *l;
92
 
  int should_stop = 1;
93
 
 
94
 
  for (l = clients; l; l = l->next)
95
 
    {
96
 
      GsmClient *client = l->data;
97
 
      char *client_program = NULL;
98
 
 
99
 
      if (client->program != NULL)
100
 
        client_program = g_strdup (client->program);
101
 
      else if (client->command != NULL)
102
 
        {
103
 
          char **argv = NULL;
104
 
          int argc;
105
 
 
106
 
          if (g_shell_parse_argv (client->command, &argc, &argv, NULL))
107
 
            {
108
 
              client_program = g_strdup (argv[0]);
109
 
            }
110
 
 
111
 
          if (argv)
112
 
            g_strfreev (argv);
113
 
        }
114
 
 
115
 
      if (do_list)
116
 
        {
117
 
          if (client_program
118
 
              && strcmp (client_program, "gnome-session-remove"))
119
 
            g_print ("  %s\n", client_program);
120
 
        }
121
 
      else
122
 
        {
123
 
          int index = find_program (client_program);
124
 
          if (index > -1)
125
 
            {
126
 
              should_stop = 0;
127
 
              ++remove_count;
128
 
              program_names[index] = NULL;
129
 
              g_print ("Removing '%s' from the session\n", client_program);
130
 
              gsm_client_commit_remove (client);
131
 
              g_signal_connect (client, "remove",
132
 
                                G_CALLBACK (client_removed), NULL);
133
 
            }
134
 
        }
135
 
 
136
 
      if (client_program)
137
 
        g_free (client_program);
138
 
    }
139
 
 
140
 
  if (should_stop)
141
 
    gtk_main_quit ();
142
 
}
143
 
 
144
 
int
145
 
main (int argc, char **argv)
146
 
{
147
 
  GnomeClient *client;
148
 
  GsmProtocol *protocol;
149
 
  GsmSession  *session;
150
 
  GOptionContext *option_context;
151
 
 
152
 
  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
153
 
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
154
 
  textdomain (GETTEXT_PACKAGE);
155
 
 
156
 
  option_context = g_option_context_new ("");
157
 
  g_option_context_add_main_entries (option_context, gsm_remove_opts,
158
 
                                     GETTEXT_PACKAGE);
159
 
 
160
 
  gnome_program_init ("gnome-session-remove",
161
 
                      VERSION, LIBGNOMEUI_MODULE,
162
 
                      argc, argv,
163
 
                      GNOME_PARAM_GOPTION_CONTEXT, option_context,
164
 
                      GNOME_PROGRAM_STANDARD_PROPERTIES,
165
 
                      GNOME_PARAM_NONE);
166
 
 
167
 
  if (program_names)
168
 
    program_names = g_strdupv (program_names);
169
 
  for (program_count = 0;
170
 
       program_names && program_names[program_count];
171
 
       ++program_count)
172
 
    ;
173
 
 
174
 
  if (!do_list && program_count < 1)
175
 
    {
176
 
      g_printerr (_("You must specify at least one program to remove. You can list the programs with --list.\n"));
177
 
      return 1;
178
 
    }
179
 
 
180
 
  client = gnome_master_client ();
181
 
  if (!GNOME_CLIENT_CONNECTED (client))
182
 
    {
183
 
      g_printerr (_("Error: could not connect to the session manager\n"));
184
 
      return 1;
185
 
    }
186
 
 
187
 
  gnome_client_set_restart_style (client, GNOME_RESTART_NEVER);
188
 
 
189
 
  protocol = gsm_protocol_new (client);
190
 
  gsm_protocol_get_current_session (GSM_PROTOCOL (protocol));
191
 
 
192
 
  session = gsm_session_live ((GsmClientFactory) client_factory, NULL);
193
 
  g_signal_connect (session, "initialized",
194
 
                    G_CALLBACK (session_initialized), NULL);
195
 
 
196
 
  if (do_list)
197
 
    g_print (_("Currently registered clients:\n"));
198
 
 
199
 
  gtk_main ();
200
 
 
201
 
  if (! do_list)
202
 
    {
203
 
      int i;
204
 
      for (i = 0; i < program_count; ++i)
205
 
        {
206
 
          if (program_names[i])
207
 
            g_print (_("Couldn't find program %s in session\n"),
208
 
                     program_names[i]);
209
 
        }
210
 
    }
211
 
 
212
 
  return 0;
213
 
}