~ubuntu-branches/ubuntu/precise/policykit-1-gnome/precise-proposed

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-11-02 17:09:07 UTC
  • mfrom: (0.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101102170907-7ns7jl6sm8bx52uw
Tags: 0.99-0ubuntu1
* New upstream release
* debian/control:
  - Build-depend on dh-autoreconf, gnome-common
  - Bump build-depends on libpolkit-agent-1-dev, libpolkit-gobject-1-dev
  - Drop build-depends on quilt, autotools
  - Use Standards-Version 3.9.1
  - Add Vcs-Bzr link
* debian/rules:
  - Use autoreconf.mk
* debian/source:
  - Use source version 3.0
* debian/patches/00git-hide-agent-autostart.patch:
  - Applied upstream
* debian/patches/05-dialog-focus:
  - Make PolicyKit dialogs associate with the application that
    launched them (LP: #433851)
* debian/patches/99_autoreconf.patch:
  - Obsolete

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <gtk/gtk.h>
28
28
#include <glib/gi18n.h>
29
29
#include <polkitagent/polkitagent.h>
 
30
#include <dbus/dbus-glib.h>
 
31
 
 
32
#ifdef HAVE_APPINDICATOR
 
33
#include <libappindicator/app-indicator.h>
 
34
#endif
30
35
 
31
36
#include "polkitgnomelistener.h"
32
37
 
39
44
/* the current set of temporary authorizations */
40
45
static GList *current_temporary_authorizations = NULL;
41
46
 
 
47
#ifdef HAVE_APPINDICATOR
 
48
static AppIndicator *app_indicator = NULL;
 
49
#else
42
50
static GtkStatusIcon *status_icon = NULL;
 
51
#endif
43
52
 
44
53
static void
45
54
revoke_tmp_authz_cb (GObject      *source_object,
69
78
                                                    NULL);
70
79
}
71
80
 
 
81
#ifdef HAVE_APPINDICATOR
 
82
static void
 
83
on_menu_item_activate (GtkMenuItem *menu_item,
 
84
                       gpointer     user_data)
 
85
{
 
86
  revoke_tmp_authz ();
 
87
}
 
88
#else
72
89
static void
73
90
on_status_icon_activate (GtkStatusIcon *status_icon,
74
91
                         gpointer       user_data)
84
101
{
85
102
  revoke_tmp_authz ();
86
103
}
 
104
#endif
87
105
 
88
106
static void
89
107
update_temporary_authorization_icon_real (void)
121
139
  if (current_temporary_authorizations != NULL)
122
140
    {
123
141
      /* show icon */
 
142
#ifdef HAVE_APPINDICATOR
 
143
      if (app_indicator == NULL)
 
144
        {
 
145
          GtkWidget *item, *menu;
 
146
 
 
147
          app_indicator = app_indicator_new ("PolicyKit-gnome",
 
148
                                             "gtk-dialog-authentication",
 
149
                                             APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
 
150
 
 
151
          item = gtk_menu_item_new_with_label (_("Drop all elevated privileges"));
 
152
          g_signal_connect (item,
 
153
                            "activate",
 
154
                            G_CALLBACK (on_menu_item_activate),
 
155
                            NULL);
 
156
          menu = gtk_menu_new ();
 
157
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
 
158
          gtk_widget_show_all (menu);
 
159
 
 
160
          app_indicator_set_menu (app_indicator,
 
161
                                  GTK_MENU (menu));
 
162
          app_indicator_set_status (app_indicator,
 
163
                                    APP_INDICATOR_STATUS_ACTIVE);          
 
164
        }
 
165
#else
124
166
      if (status_icon == NULL)
125
167
        {
126
168
          status_icon = gtk_status_icon_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION);
135
177
                            G_CALLBACK (on_status_icon_popup_menu),
136
178
                            NULL);
137
179
        }
 
180
#endif
138
181
    }
139
182
  else
140
183
    {
141
184
      /* hide icon */
 
185
#ifdef HAVE_APPINDICATOR
 
186
      if (app_indicator != NULL)
 
187
        {
 
188
          app_indicator_set_status (app_indicator,
 
189
                                    APP_INDICATOR_STATUS_PASSIVE);
 
190
          g_object_unref (app_indicator);
 
191
          app_indicator = NULL;
 
192
        }
 
193
#else
142
194
      if (status_icon != NULL)
143
195
        {
144
196
          gtk_status_icon_set_visible (status_icon, FALSE);
145
197
          g_object_unref (status_icon);
146
198
          status_icon = NULL;
147
199
        }
 
200
#endif
148
201
    }
149
202
}
150
203
 
198
251
  update_temporary_authorization_icon (authority);
199
252
}
200
253
 
 
254
// session management support for auto-restart 
 
255
#define SM_DBUS_NAME      "org.gnome.SessionManager"
 
256
#define SM_DBUS_PATH      "/org/gnome/SessionManager"
 
257
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"
 
258
#define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
 
259
 
 
260
static DBusGConnection *bus_connection;
 
261
static DBusGProxy      *sm_proxy;
 
262
static DBusGProxy      *client_proxy = NULL;
 
263
 
 
264
static  GMainLoop *loop;
 
265
 
 
266
 
 
267
static void
 
268
stop_cb (gpointer data)
 
269
{
 
270
        g_main_loop_quit (loop);
 
271
}
 
272
 
 
273
static gboolean
 
274
end_session_response (gboolean is_okay, const gchar *reason)
 
275
{
 
276
        gboolean ret;
 
277
        GError *error = NULL;
 
278
 
 
279
        ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
 
280
                                 &error,
 
281
                                 G_TYPE_BOOLEAN, is_okay,
 
282
                                 G_TYPE_STRING, reason,
 
283
                                 G_TYPE_INVALID,
 
284
                                 G_TYPE_INVALID);
 
285
 
 
286
        if (!ret) {
 
287
                g_warning ("Failed to send session response %s", error->message);
 
288
                g_error_free (error);
 
289
        }
 
290
 
 
291
        return ret;
 
292
}
 
293
 
 
294
static void
 
295
query_end_session_cb (guint flags, gpointer data)
 
296
{
 
297
        end_session_response (TRUE, NULL);
 
298
}
 
299
 
 
300
static void
 
301
end_session_cb (guint flags, gpointer data)
 
302
{
 
303
        end_session_response (TRUE, NULL);
 
304
        g_main_loop_quit (loop);
 
305
}
 
306
 
 
307
 
 
308
static gboolean
 
309
register_client_to_gnome_session (void)
 
310
{
 
311
        GError     *error;
 
312
        gboolean    res;
 
313
        const char *startup_id;
 
314
        const char *app_id;
 
315
        char            *client_id;
 
316
 
 
317
        startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
 
318
        app_id = "polkit-gnome-authentication-agent-1.desktop";
 
319
 
 
320
        error = NULL;
 
321
        bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
322
        if (bus_connection == NULL) {
 
323
           g_message ("Failed to connect to the session bus: %s",
 
324
                      error->message);
 
325
           g_error_free (error);
 
326
           return FALSE;
 
327
        }
 
328
 
 
329
        sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
 
330
                                              SM_DBUS_NAME,
 
331
                                              SM_DBUS_PATH,
 
332
                                              SM_DBUS_INTERFACE);
 
333
        if (sm_proxy == NULL) {
 
334
           g_message("Failed to get session manager");
 
335
           return FALSE;
 
336
        }
 
337
 
 
338
        error = NULL;
 
339
        res = dbus_g_proxy_call (sm_proxy,
 
340
                                 "RegisterClient",
 
341
                                 &error,
 
342
                                 G_TYPE_STRING, app_id,
 
343
                                 G_TYPE_STRING, startup_id,
 
344
                                 G_TYPE_INVALID,
 
345
                                 DBUS_TYPE_G_OBJECT_PATH, &client_id,
 
346
                                 G_TYPE_INVALID);
 
347
        if (! res) {
 
348
                g_warning ("Failed to register client: %s", error->message);
 
349
                g_error_free (error);
 
350
                return FALSE;
 
351
        }
 
352
 
 
353
        // implement the signals to fix "policykit agent not responding"
 
354
        // error (LP: #623819)
 
355
        client_proxy = dbus_g_proxy_new_for_name (bus_connection,
 
356
                                                  SM_DBUS_NAME,
 
357
                                                  client_id,
 
358
                                                  SM_CLIENT_DBUS_INTERFACE);
 
359
 
 
360
        dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
 
361
        dbus_g_proxy_connect_signal (client_proxy, "Stop",
 
362
                                     G_CALLBACK (stop_cb), NULL, NULL);
 
363
 
 
364
        dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
 
365
        dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
 
366
                                     G_CALLBACK (query_end_session_cb), NULL, NULL);
 
367
 
 
368
        dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
 
369
        dbus_g_proxy_connect_signal (client_proxy, "EndSession",
 
370
                                     G_CALLBACK (end_session_cb), NULL, NULL);
 
371
 
 
372
        return TRUE;
 
373
}
 
374
 
201
375
int
202
376
main (int argc, char **argv)
203
377
{
204
378
  gint ret;
205
 
  GMainLoop *loop;
206
379
  PolkitAgentListener *listener;
207
380
  GError *error;
208
381
 
223
396
 
224
397
  loop = g_main_loop_new (NULL, FALSE);
225
398
 
226
 
  authority = polkit_authority_get ();
 
399
  error = NULL;
 
400
  authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
 
401
  if (authority == NULL)
 
402
    {
 
403
      g_warning ("Error getting authority: %s", error->message);
 
404
      g_error_free (error);
 
405
      goto out;
 
406
    }
227
407
  g_signal_connect (authority,
228
408
                    "changed",
229
409
                    G_CALLBACK (on_authority_changed),
253
433
 
254
434
  update_temporary_authorization_icon (authority);
255
435
 
 
436
  register_client_to_gnome_session();
 
437
 
256
438
  g_main_loop_run (loop);
257
439
 
258
440
  ret = 0;