1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
4  * Copyright (C) 2012-2021 MATE Developers
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 
36 #include <glib.h>
37 #include <glib-object.h>
38 
39 #include <dbus/dbus-glib.h>
40 #include <dbus/dbus-glib-lowlevel.h>
41 
42 
43 #include "msd-datetime-mechanism.h"
44 
45 static DBusGProxy *
get_bus_proxy(DBusGConnection * connection)46 get_bus_proxy (DBusGConnection *connection)
47 {
48         DBusGProxy *bus_proxy;
49 
50 	bus_proxy = dbus_g_proxy_new_for_name (connection,
51                                                DBUS_SERVICE_DBUS,
52                                                DBUS_PATH_DBUS,
53                                                DBUS_INTERFACE_DBUS);
54         return bus_proxy;
55 }
56 
57 #define BUS_NAME "org.mate.SettingsDaemon.DateTimeMechanism"
58 
59 static gboolean
acquire_name_on_proxy(DBusGProxy * bus_proxy)60 acquire_name_on_proxy (DBusGProxy *bus_proxy)
61 {
62         GError     *error;
63         guint       result;
64         gboolean    res;
65         gboolean    ret;
66 
67         ret = FALSE;
68 
69         if (bus_proxy == NULL) {
70                 goto out;
71         }
72 
73         error = NULL;
74 	res = dbus_g_proxy_call (bus_proxy,
75                                  "RequestName",
76                                  &error,
77                                  G_TYPE_STRING, BUS_NAME,
78                                  G_TYPE_UINT, 0,
79                                  G_TYPE_INVALID,
80                                  G_TYPE_UINT, &result,
81                                  G_TYPE_INVALID);
82         if (! res) {
83                 if (error != NULL) {
84                         g_warning ("Failed to acquire %s: %s", BUS_NAME, error->message);
85                         g_error_free (error);
86                 } else {
87                         g_warning ("Failed to acquire %s", BUS_NAME);
88                 }
89                 goto out;
90 	}
91 
92  	if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
93                 if (error != NULL) {
94                         g_warning ("Failed to acquire %s: %s", BUS_NAME, error->message);
95                         g_error_free (error);
96                 } else {
97                         g_warning ("Failed to acquire %s", BUS_NAME);
98                 }
99                 goto out;
100         }
101 
102         ret = TRUE;
103 
104  out:
105         return ret;
106 }
107 
108 static DBusGConnection *
get_system_bus(void)109 get_system_bus (void)
110 {
111         GError          *error;
112         DBusGConnection *bus;
113 
114         error = NULL;
115         bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
116         if (bus == NULL) {
117                 g_warning ("Couldn't connect to system bus: %s", error->message);
118                 g_error_free (error);
119         }
120         return bus;
121 }
122 
123 int
main(int argc,char ** argv)124 main (int argc, char **argv)
125 {
126         GMainLoop             *loop;
127         MsdDatetimeMechanism  *mechanism;
128         DBusGProxy            *bus_proxy;
129         DBusGConnection       *connection;
130         int                    ret;
131 
132         ret = 1;
133 
134         dbus_g_thread_init ();
135 
136         connection = get_system_bus ();
137         if (connection == NULL) {
138                 goto out;
139         }
140 
141         bus_proxy = get_bus_proxy (connection);
142         if (bus_proxy == NULL) {
143                 g_warning ("Could not construct bus_proxy object; bailing out");
144                 goto out;
145         }
146 
147         if (!acquire_name_on_proxy (bus_proxy) ) {
148                 g_warning ("Could not acquire name; bailing out");
149                 goto out;
150         }
151 
152         mechanism = msd_datetime_mechanism_new ();
153 
154         if (mechanism == NULL) {
155                 goto out;
156         }
157 
158         loop = g_main_loop_new (NULL, FALSE);
159 
160         g_main_loop_run (loop);
161 
162         g_object_unref (mechanism);
163         g_main_loop_unref (loop);
164         ret = 0;
165 
166 out:
167         return ret;
168 }
169