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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2013 Intel, Inc
* Copyright (C) 2011,2012 Red Hat, Inc
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "cc-hostname-entry.h"
#include "hostname-helper.h"
#include <polkit/polkit.h>
struct _CcHostnameEntry
{
GtkEntry parent;
GDBusProxy *hostnamed_proxy;
guint set_hostname_timeout_source_id;
};
G_DEFINE_TYPE (CcHostnameEntry, cc_hostname_entry, GTK_TYPE_ENTRY)
#define SET_HOSTNAME_TIMEOUT 1
static void
cc_hostname_entry_set_hostname (CcHostnameEntry *self)
{
g_autofree gchar *hostname = NULL;
g_autoptr(GVariant) pretty_result = NULL;
g_autoptr(GVariant) static_result = NULL;
g_autoptr(GError) pretty_error = NULL;
g_autoptr(GError) static_error = NULL;
const gchar *text;
text = gtk_entry_get_text (GTK_ENTRY (self));
g_debug ("Setting PrettyHostname to '%s'", text);
pretty_result = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetPrettyHostname",
g_variant_new ("(sb)", text, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &pretty_error);
if (pretty_result == NULL)
g_warning ("Could not set PrettyHostname: %s", pretty_error->message);
/* Set the static hostname */
hostname = pretty_hostname_to_static (text, FALSE);
g_assert (hostname);
g_debug ("Setting StaticHostname to '%s'", hostname);
static_result = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetStaticHostname",
g_variant_new ("(sb)", hostname, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &static_error);
if (static_result == NULL)
g_warning ("Could not set StaticHostname: %s", static_error->message);
}
static char *
get_hostname_property (CcHostnameEntry *self,
const char *property)
{
g_autoptr(GVariant) variant = NULL;
if (!self->hostnamed_proxy)
return g_strdup ("");
variant = g_dbus_proxy_get_cached_property (self->hostnamed_proxy,
property);
if (!variant)
{
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) inner = NULL;
/* Work around systemd-hostname not sending us back
* the property value when changing values */
variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"org.freedesktop.DBus.Properties.Get",
g_variant_new ("(ss)", "org.freedesktop.hostname1", property),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (variant == NULL)
{
g_warning ("Failed to get property '%s': %s", property, error->message);
return NULL;
}
g_variant_get (variant, "(v)", &inner);
return g_variant_dup_string (inner, NULL);
}
else
{
return g_variant_dup_string (variant, NULL);
}
}
static char *
cc_hostname_entry_get_display_hostname (CcHostnameEntry *self)
{
g_autofree gchar *str = NULL;
str = get_hostname_property (self, "PrettyHostname");
/* Empty strings means that we need to fallback */
if (str != NULL &&
*str == '\0')
return get_hostname_property (self, "Hostname");
return g_steal_pointer (&str);
}
static gboolean
set_hostname_timeout (CcHostnameEntry *self)
{
self->set_hostname_timeout_source_id = 0;
cc_hostname_entry_set_hostname (self);
return FALSE;
}
static void
remove_hostname_timeout (CcHostnameEntry *self)
{
if (self->set_hostname_timeout_source_id)
g_source_remove (self->set_hostname_timeout_source_id);
self->set_hostname_timeout_source_id = 0;
}
static void
reset_hostname_timeout (CcHostnameEntry *self)
{
remove_hostname_timeout (self);
self->set_hostname_timeout_source_id = g_timeout_add_seconds (SET_HOSTNAME_TIMEOUT,
(GSourceFunc) set_hostname_timeout,
self);
}
static void
text_changed_cb (CcHostnameEntry *entry)
{
reset_hostname_timeout (entry);
}
static void
cc_hostname_entry_dispose (GObject *object)
{
CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
if (self->set_hostname_timeout_source_id)
{
remove_hostname_timeout (self);
set_hostname_timeout (self);
}
g_clear_object (&self->hostnamed_proxy);
G_OBJECT_CLASS (cc_hostname_entry_parent_class)->dispose (object);
}
static void
cc_hostname_entry_constructed (GObject *object)
{
CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
GPermission *permission;
g_autoptr(GError) error = NULL;
g_autofree gchar *str = NULL;
permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname",
NULL, NULL, NULL);
/* Is hostnamed installed? */
if (permission == NULL)
{
g_debug ("Will not show hostname, hostnamed not installed");
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
return;
}
if (g_permission_get_allowed (permission))
gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
else
{
g_debug ("Not allowed to change the hostname");
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
}
gtk_widget_set_sensitive (GTK_WIDGET (self),
g_permission_get_allowed (permission));
self->hostnamed_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
NULL,
&error);
/* This could only happen if the policy file was installed
* but not hostnamed, which points to a system bug */
if (self->hostnamed_proxy == NULL)
{
g_debug ("Couldn't get hostnamed to start, bailing: %s", error->message);
return;
}
str = cc_hostname_entry_get_display_hostname (CC_HOSTNAME_ENTRY (self));
if (str != NULL)
gtk_entry_set_text (GTK_ENTRY (self), str);
else
gtk_entry_set_text (GTK_ENTRY (self), "");
g_signal_connect (self, "changed", G_CALLBACK (text_changed_cb), NULL);
}
static void
cc_hostname_entry_class_init (CcHostnameEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = cc_hostname_entry_constructed;
object_class->dispose = cc_hostname_entry_dispose;
}
static void
cc_hostname_entry_init (CcHostnameEntry *self)
{
}
CcHostnameEntry *
cc_hostname_entry_new (void)
{
return g_object_new (CC_TYPE_HOSTNAME_ENTRY, NULL);
}
gchar*
cc_hostname_entry_get_hostname (CcHostnameEntry *entry)
{
return get_hostname_property (entry, "Hostname");
}
|