~ubuntu-branches/ubuntu/trusty/gcr/trusty-proposed

« back to all changes in this revision

Viewing changes to gcr/gcr-prompter-tool.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-03 10:18:39 UTC
  • Revision ID: package-import@ubuntu.com-20120503101839-wuvloldm7gmdsnij
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* gcr-viewer-tool.c: Command line utility
 
3
 
 
4
   Copyright (C) 2011 Collabora Ltd.
 
5
 
 
6
   The Gnome Keyring Library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public License as
 
8
   published by the Free Software Foundation; either version 2 of the
 
9
   License, or (at your option) any later version.
 
10
 
 
11
   The Gnome Keyring Library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public
 
17
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
18
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
   Boston, MA 02111-1307, USA.
 
20
 
 
21
   Author: Stef Walter <stefw@collabora.co.uk>
 
22
*/
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include "gcr.h"
 
27
 
 
28
#include "gcr-dbus-constants.h"
 
29
#define DEBUG_FLAG GCR_DEBUG_PROMPT
 
30
#include "gcr-debug.h"
 
31
 
 
32
#include <glib/gi18n.h>
 
33
#include <gtk/gtk.h>
 
34
#include <gdk/gdkx.h>
 
35
#include <pango/pango.h>
 
36
 
 
37
#include <locale.h>
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <syslog.h>
 
41
 
 
42
#define QUIT_TIMEOUT 10
 
43
 
 
44
static GcrSystemPrompter *the_prompter = NULL;
 
45
static gboolean registered_prompter = FALSE;
 
46
static gboolean acquired_system_prompter = FALSE;
 
47
static gboolean acquired_private_prompter = FALSE;
 
48
static guint timeout_source = 0;
 
49
 
 
50
static gboolean
 
51
on_timeout_quit (gpointer unused)
 
52
{
 
53
        _gcr_debug ("%d second inactivity timeout, quitting", QUIT_TIMEOUT);
 
54
        gtk_main_quit ();
 
55
 
 
56
        return FALSE; /* Don't run again */
 
57
}
 
58
 
 
59
static void
 
60
start_timeout (void)
 
61
{
 
62
        if (g_getenv ("GCR_PERSIST") != NULL)
 
63
                return;
 
64
 
 
65
        if (!timeout_source)
 
66
                timeout_source = g_timeout_add_seconds (QUIT_TIMEOUT, on_timeout_quit, NULL);
 
67
}
 
68
 
 
69
static void
 
70
stop_timeout (void)
 
71
{
 
72
        if (timeout_source)
 
73
                g_source_remove (timeout_source);
 
74
        timeout_source = 0;
 
75
}
 
76
 
 
77
static void
 
78
on_prompter_prompting (GObject *obj,
 
79
                       GParamSpec param,
 
80
                       gpointer user_data)
 
81
{
 
82
        if (gcr_system_prompter_get_prompting (the_prompter))
 
83
                stop_timeout ();
 
84
        else
 
85
                start_timeout ();
 
86
}
 
87
 
 
88
static void
 
89
on_bus_acquired (GDBusConnection *connection,
 
90
                 const gchar *name,
 
91
                 gpointer user_data)
 
92
{
 
93
        _gcr_debug ("bus acquired: %s", name);
 
94
 
 
95
        if (!registered_prompter)
 
96
                gcr_system_prompter_register (the_prompter, connection);
 
97
 
 
98
        registered_prompter = TRUE;
 
99
}
 
100
 
 
101
static void
 
102
on_name_acquired (GDBusConnection *connection,
 
103
                  const gchar *name,
 
104
                  gpointer user_data)
 
105
{
 
106
        _gcr_debug ("acquired name: %s", name);
 
107
 
 
108
        if (g_strcmp0 (name, GCR_DBUS_PROMPTER_SYSTEM_BUS_NAME) == 0)
 
109
                acquired_system_prompter = TRUE;
 
110
 
 
111
        else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_PRIVATE_BUS_NAME) == 0)
 
112
                acquired_private_prompter = TRUE;
 
113
}
 
114
 
 
115
static void
 
116
on_name_lost (GDBusConnection *connection,
 
117
              const gchar *name,
 
118
              gpointer user_data)
 
119
{
 
120
        _gcr_debug ("lost name: %s", name);
 
121
 
 
122
        /* Called like so when no connection can be made */
 
123
        if (connection == NULL) {
 
124
                g_warning ("couldn't connect to session bus");
 
125
                gtk_main_quit ();
 
126
 
 
127
        } else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_SYSTEM_BUS_NAME) == 0) {
 
128
                acquired_system_prompter = TRUE;
 
129
 
 
130
        } else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_PRIVATE_BUS_NAME) == 0) {
 
131
                acquired_private_prompter = TRUE;
 
132
 
 
133
        }
 
134
}
 
135
 
 
136
static void
 
137
log_handler (const gchar *log_domain,
 
138
             GLogLevelFlags log_level,
 
139
             const gchar *message,
 
140
             gpointer user_data)
 
141
{
 
142
        int level;
 
143
 
 
144
        /* Note that crit and err are the other way around in syslog */
 
145
 
 
146
        switch (G_LOG_LEVEL_MASK & log_level) {
 
147
        case G_LOG_LEVEL_ERROR:
 
148
                level = LOG_CRIT;
 
149
                break;
 
150
        case G_LOG_LEVEL_CRITICAL:
 
151
                level = LOG_ERR;
 
152
                break;
 
153
        case G_LOG_LEVEL_WARNING:
 
154
                level = LOG_WARNING;
 
155
                break;
 
156
        case G_LOG_LEVEL_MESSAGE:
 
157
                level = LOG_NOTICE;
 
158
                break;
 
159
        case G_LOG_LEVEL_INFO:
 
160
                level = LOG_INFO;
 
161
                break;
 
162
        case G_LOG_LEVEL_DEBUG:
 
163
                level = LOG_DEBUG;
 
164
                break;
 
165
        default:
 
166
                level = LOG_ERR;
 
167
                break;
 
168
        }
 
169
 
 
170
        /* Log to syslog first */
 
171
        if (log_domain)
 
172
                syslog (level, "%s: %s", log_domain, message);
 
173
        else
 
174
                syslog (level, "%s", message);
 
175
 
 
176
        /* And then to default handler for aborting and stuff like that */
 
177
        g_log_default_handler (log_domain, log_level, message, user_data);
 
178
}
 
179
 
 
180
static void
 
181
printerr_handler (const gchar *string)
 
182
{
 
183
        /* Print to syslog and stderr */
 
184
        syslog (LOG_WARNING, "%s", string);
 
185
        fprintf (stderr, "%s", string);
 
186
}
 
187
 
 
188
static void
 
189
prepare_logging ()
 
190
{
 
191
        GLogLevelFlags flags = G_LOG_FLAG_FATAL | G_LOG_LEVEL_ERROR |
 
192
                               G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
 
193
                               G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
 
194
 
 
195
        openlog ("gcr-prompter", LOG_PID, LOG_AUTH);
 
196
 
 
197
        g_log_set_handler (NULL, flags, log_handler, NULL);
 
198
        g_log_set_handler ("Glib", flags, log_handler, NULL);
 
199
        g_log_set_handler ("Gtk", flags, log_handler, NULL);
 
200
        g_log_set_handler ("Gnome", flags, log_handler, NULL);
 
201
        g_log_set_handler ("Gcr", flags, log_handler, NULL);
 
202
        g_log_set_handler ("Gck", flags, log_handler, NULL);
 
203
        g_log_set_default_handler (log_handler, NULL);
 
204
        g_set_printerr_handler (printerr_handler);
 
205
}
 
206
 
 
207
int
 
208
main (int argc, char *argv[])
 
209
{
 
210
        guint system_owner_id;
 
211
        guint private_owner_id;
 
212
 
 
213
        g_type_init ();
 
214
        gtk_init (&argc, &argv);
 
215
 
 
216
#ifdef HAVE_LOCALE_H
 
217
        /* internationalisation */
 
218
        setlocale (LC_ALL, "");
 
219
#endif
 
220
 
 
221
#ifdef HAVE_GETTEXT
 
222
        bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 
223
        textdomain (GETTEXT_PACKAGE);
 
224
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
225
#endif
 
226
 
 
227
        prepare_logging ();
 
228
 
 
229
        the_prompter = gcr_system_prompter_new (GCR_SYSTEM_PROMPTER_SINGLE,
 
230
                                                GCR_TYPE_PROMPT_DIALOG);
 
231
        g_signal_connect (the_prompter, "notify::prompting",
 
232
                          G_CALLBACK (on_prompter_prompting), NULL);
 
233
 
 
234
        system_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
 
235
                                          GCR_DBUS_PROMPTER_SYSTEM_BUS_NAME,
 
236
                                          G_BUS_NAME_OWNER_FLAGS_NONE,
 
237
                                          on_bus_acquired,
 
238
                                          on_name_acquired,
 
239
                                          on_name_lost,
 
240
                                          NULL,
 
241
                                          NULL);
 
242
 
 
243
        private_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
 
244
                                           GCR_DBUS_PROMPTER_PRIVATE_BUS_NAME,
 
245
                                           G_BUS_NAME_OWNER_FLAGS_NONE,
 
246
                                           on_bus_acquired,
 
247
                                           on_name_acquired,
 
248
                                           on_name_lost,
 
249
                                           NULL,
 
250
                                           NULL);
 
251
 
 
252
        start_timeout ();
 
253
        gtk_main ();
 
254
 
 
255
        if (registered_prompter)
 
256
                gcr_system_prompter_unregister (the_prompter, TRUE);
 
257
 
 
258
        g_bus_unown_name (system_owner_id);
 
259
        g_bus_unown_name (private_owner_id);
 
260
 
 
261
        g_object_unref (the_prompter);
 
262
 
 
263
        return 0;
 
264
}