~ubuntu-branches/ubuntu/quantal/gnome-panel/quantal

« back to all changes in this revision

Viewing changes to .pc/14_revert-timedate-change.patch/applets/clock/set-timezone.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-02-08 00:17:11 UTC
  • mfrom: (1.13.15)
  • Revision ID: package-import@ubuntu.com-20120208001711-npwmthl0c6iy3s9a
Tags: 1:3.3.5-0ubuntu1
* New upstream release.
* debian/control.in:
  - Bump minimum glib to 2.31.14
* debian/patches/13_disable_missing_help.patch:
  Updated patch from bugzilla
* debian/patches/14_revert-timedate-change.patch:
  - Revert switch to systemd timedate protocol until ubuntu-system-
    services supports it

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Codethink Limited
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program 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
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 *
 
18
 * Author: Ryan Lortie <desrt@desrt.ca>
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#  include "config.h"
 
23
#endif
 
24
 
 
25
#include <gio/gio.h>
 
26
#include <polkit/polkit.h>
 
27
 
 
28
#include "set-timezone.h"
 
29
 
 
30
#define CACHE_VALIDITY_SEC 20
 
31
 
 
32
#define MECHANISM_BUS_NAME    "org.freedesktop.timedate1"
 
33
#define MECHANISM_OBJECT_PATH "/org/freedesktop/timedate1"
 
34
#define MECHANISM_INTERFACE   "org.freedesktop.timedate1"
 
35
 
 
36
typedef struct {
 
37
  gint     value;
 
38
  guint64  stamp;
 
39
} Cache;
 
40
 
 
41
static Cache can_set_timezone_cache;
 
42
 
 
43
static GDBusConnection *
 
44
get_system_bus (GError **error)
 
45
{
 
46
  static GDBusConnection *system;
 
47
  static gboolean initialised;
 
48
  static GError *saved_error;
 
49
 
 
50
  if (!initialised)
 
51
    {
 
52
      system = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &saved_error);
 
53
      initialised = TRUE;
 
54
    }
 
55
 
 
56
  if (system == NULL && error)
 
57
    *error = g_error_copy (saved_error);
 
58
 
 
59
  return system;
 
60
}
 
61
 
 
62
static int
 
63
can_set (Cache *cache, const gchar *method_name)
 
64
{
 
65
  guint64 now = g_get_monotonic_time ();
 
66
 
 
67
  if (now - cache->stamp > (CACHE_VALIDITY_SEC * 1000000))
 
68
    {
 
69
      PolkitAuthority *authority;
 
70
      PolkitSubject   *subject;
 
71
      PolkitAuthorizationResult *res;
 
72
 
 
73
      authority = polkit_authority_get_sync (NULL, NULL);
 
74
      subject = polkit_unix_session_new_for_process_sync (getpid (), NULL, NULL);
 
75
 
 
76
      res = polkit_authority_check_authorization_sync (authority,
 
77
                                                       subject,
 
78
                                                       "org.freedesktop.timedate1.set-timezone",
 
79
                                                       NULL,
 
80
                                                       POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
 
81
                                                       NULL,
 
82
                                                       NULL);
 
83
 
 
84
        cache->stamp = g_get_monotonic_time ();
 
85
 
 
86
        if (res == NULL)
 
87
          cache->value = 0;
 
88
        else
 
89
          {
 
90
            if (polkit_authorization_result_get_is_authorized (res))
 
91
              cache->value = 2;
 
92
            else if (polkit_authorization_result_get_is_challenge (res))
 
93
              cache->value = 1;
 
94
            else
 
95
              cache->value = 0;
 
96
 
 
97
            g_object_unref (res);
 
98
          }
 
99
 
 
100
        g_object_unref (authority);
 
101
        g_object_unref (subject);
 
102
    }
 
103
 
 
104
  return cache->value;
 
105
}
 
106
 
 
107
gint
 
108
can_set_system_timezone (void)
 
109
{
 
110
  return can_set (&can_set_timezone_cache, "CanSetTimezone");
 
111
}
 
112
 
 
113
gboolean
 
114
set_system_timezone_finish (GAsyncResult  *result,
 
115
                            GError       **error)
 
116
{
 
117
  GDBusConnection *system_bus = get_system_bus (NULL);
 
118
  GVariant *reply;
 
119
 
 
120
  /* detect if we set an error due to being unable to get the system bus */
 
121
  if (g_simple_async_result_is_valid (result, NULL, set_system_timezone_async))
 
122
    {
 
123
      g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
 
124
                                             error);
 
125
      return FALSE;
 
126
    }
 
127
 
 
128
  g_assert (system_bus != NULL);
 
129
 
 
130
  reply = g_dbus_connection_call_finish (system_bus, result, error);
 
131
 
 
132
  if (reply != NULL)
 
133
    g_variant_unref (reply);
 
134
 
 
135
  return reply != NULL;
 
136
}
 
137
 
 
138
void
 
139
set_system_timezone_async (const gchar         *tz,
 
140
                           GAsyncReadyCallback  callback,
 
141
                           gpointer             user_data)
 
142
{
 
143
  GDBusConnection *system_bus;
 
144
  GError *error = NULL;
 
145
 
 
146
  system_bus = get_system_bus (&error);
 
147
 
 
148
  if (system_bus == NULL)
 
149
    {
 
150
      GSimpleAsyncResult *simple;
 
151
 
 
152
      simple = g_simple_async_result_new (NULL, callback, user_data,
 
153
                                          set_system_timezone_async);
 
154
      g_simple_async_result_set_from_error (simple, error);
 
155
      g_simple_async_result_complete_in_idle (simple);
 
156
      g_object_unref (simple);
 
157
      g_error_free (error);
 
158
    }
 
159
 
 
160
  g_dbus_connection_call (system_bus, MECHANISM_BUS_NAME,
 
161
                          MECHANISM_OBJECT_PATH, MECHANISM_INTERFACE,
 
162
                          "SetTimezone", g_variant_new ("(sb)", tz, TRUE),
 
163
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
 
164
                          callback, user_data);
 
165
}