summaryrefslogtreecommitdiff
path: root/src/main.c
blob: c77ddbdf16af627e9b77bf9931c3d15ac08f1eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Novell, Inc.
 *
 * Authors: Vincent Untz
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * The code is originally based on gnome-clock-applet-mechanism-main.c, which
 * is under the same license and with the following copyright:
 *
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <glib.h>
#include <glib-object.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "cups-pk-helper-mechanism.h"

#define BUS_NAME "org.opensuse.cups-pk-helper.Mechanism"

static DBusGProxy *
get_bus_proxy (DBusGConnection *connection)
{
        DBusGProxy *bus_proxy;

        bus_proxy = dbus_g_proxy_new_for_name (connection,
                                               DBUS_SERVICE_DBUS,
                                               DBUS_PATH_DBUS,
                                               DBUS_INTERFACE_DBUS);
        return bus_proxy;
}

static gboolean
acquire_name_on_proxy (DBusGProxy  *bus_proxy,
                       GError     **error)
{
        guint    result;
        gboolean res;

        g_assert (bus_proxy != NULL);

        res = dbus_g_proxy_call (bus_proxy,
                                 "RequestName",
                                 error,
                                 G_TYPE_STRING, BUS_NAME,
                                 G_TYPE_UINT, 0,
                                 G_TYPE_INVALID,
                                 G_TYPE_UINT, &result,
                                 G_TYPE_INVALID);
        if (!res ||
            result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
                return FALSE;

        return TRUE;
}

int
main (int argc, char **argv)
{
        GError          *error;
        GMainLoop       *loop;
        CphMechanism    *mechanism;
        DBusGProxy      *bus_proxy;
        DBusGConnection *connection;
        int              ret;

        if (!g_thread_supported ())
                g_thread_init (NULL);

        dbus_g_thread_init ();
        g_type_init ();

        error = NULL;

        connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
        if (connection == NULL) {
                g_warning ("Could not connect to system bus: %s",
                           error->message);
                g_error_free (error);
                return 1;
        }

        bus_proxy = get_bus_proxy (connection);
        if (bus_proxy == NULL) {
                g_warning ("Could not construct bus_proxy objects");
                return 1;
        }

        if (!acquire_name_on_proxy (bus_proxy, &error) ) {
                if (error != NULL) {
                        g_warning ("Could not acquire name: %s",
                                   error->message);
                        g_error_free (error);
                } else {
                        g_warning ("Could not acquire name");
                }

                return 1;
        }

        mechanism = cph_mechanism_new ();

        if (mechanism == NULL) {
                g_warning ("Could not create mechanism object");
                return 1;
        }

        loop = g_main_loop_new (NULL, FALSE);

        g_main_loop_run (loop);

        g_object_unref (mechanism);
        g_main_loop_unref (loop);

        return 0;
}