1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* gcr-viewer-tool.c: Command line utility
4
Copyright (C) 2011 Collabora Ltd.
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.
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.
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.
21
Author: Stef Walter <stefw@collabora.co.uk>
28
#include "gcr-dbus-constants.h"
29
#define DEBUG_FLAG GCR_DEBUG_PROMPT
30
#include "gcr-debug.h"
32
#include <glib/gi18n.h>
35
#include <pango/pango.h>
42
#define QUIT_TIMEOUT 10
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;
51
on_timeout_quit (gpointer unused)
53
_gcr_debug ("%d second inactivity timeout, quitting", QUIT_TIMEOUT);
56
return FALSE; /* Don't run again */
62
if (g_getenv ("GCR_PERSIST") != NULL)
66
timeout_source = g_timeout_add_seconds (QUIT_TIMEOUT, on_timeout_quit, NULL);
73
g_source_remove (timeout_source);
78
on_prompter_prompting (GObject *obj,
82
if (gcr_system_prompter_get_prompting (the_prompter))
89
on_bus_acquired (GDBusConnection *connection,
93
_gcr_debug ("bus acquired: %s", name);
95
if (!registered_prompter)
96
gcr_system_prompter_register (the_prompter, connection);
98
registered_prompter = TRUE;
102
on_name_acquired (GDBusConnection *connection,
106
_gcr_debug ("acquired name: %s", name);
108
if (g_strcmp0 (name, GCR_DBUS_PROMPTER_SYSTEM_BUS_NAME) == 0)
109
acquired_system_prompter = TRUE;
111
else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_PRIVATE_BUS_NAME) == 0)
112
acquired_private_prompter = TRUE;
116
on_name_lost (GDBusConnection *connection,
120
_gcr_debug ("lost name: %s", name);
122
/* Called like so when no connection can be made */
123
if (connection == NULL) {
124
g_warning ("couldn't connect to session bus");
127
} else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_SYSTEM_BUS_NAME) == 0) {
128
acquired_system_prompter = TRUE;
130
} else if (g_strcmp0 (name, GCR_DBUS_PROMPTER_PRIVATE_BUS_NAME) == 0) {
131
acquired_private_prompter = TRUE;
137
log_handler (const gchar *log_domain,
138
GLogLevelFlags log_level,
139
const gchar *message,
144
/* Note that crit and err are the other way around in syslog */
146
switch (G_LOG_LEVEL_MASK & log_level) {
147
case G_LOG_LEVEL_ERROR:
150
case G_LOG_LEVEL_CRITICAL:
153
case G_LOG_LEVEL_WARNING:
156
case G_LOG_LEVEL_MESSAGE:
159
case G_LOG_LEVEL_INFO:
162
case G_LOG_LEVEL_DEBUG:
170
/* Log to syslog first */
172
syslog (level, "%s: %s", log_domain, message);
174
syslog (level, "%s", message);
176
/* And then to default handler for aborting and stuff like that */
177
g_log_default_handler (log_domain, log_level, message, user_data);
181
printerr_handler (const gchar *string)
183
/* Print to syslog and stderr */
184
syslog (LOG_WARNING, "%s", string);
185
fprintf (stderr, "%s", string);
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;
195
openlog ("gcr-prompter", LOG_PID, LOG_AUTH);
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);
208
main (int argc, char *argv[])
210
guint system_owner_id;
211
guint private_owner_id;
214
gtk_init (&argc, &argv);
217
/* internationalisation */
218
setlocale (LC_ALL, "");
222
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
223
textdomain (GETTEXT_PACKAGE);
224
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
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);
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,
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,
255
if (registered_prompter)
256
gcr_system_prompter_unregister (the_prompter, TRUE);
258
g_bus_unown_name (system_owner_id);
259
g_bus_unown_name (private_owner_id);
261
g_object_unref (the_prompter);