GCC Code Coverage Report


Directory: ./
File: panels/common/cc-hostname-entry.c
Date: 2024-05-03 09:46:52
Exec Total Coverage
Lines: 0 50 0.0%
Functions: 0 11 0.0%
Branches: 0 23 0.0%

Line Branch Exec Source
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2013 Intel, Inc
4 * Copyright (C) 2011,2012 Red Hat, Inc
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, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21
22 #include "cc-common-resources.h"
23 #include "cc-hostname.h"
24 #include "cc-hostname-entry.h"
25 #include "hostname-helper.h"
26
27 #include <polkit/polkit.h>
28
29 struct _CcHostnameEntry
30 {
31 AdwEntryRow parent;
32
33 guint set_hostname_timeout_source_id;
34 };
35
36 G_DEFINE_TYPE (CcHostnameEntry, cc_hostname_entry, ADW_TYPE_ENTRY_ROW)
37
38 #define SET_HOSTNAME_TIMEOUT 1
39
40 static gboolean
41 set_hostname_timeout (CcHostnameEntry *self)
42 {
43 const gchar *text = NULL;
44
45 self->set_hostname_timeout_source_id = 0;
46
47 text = gtk_editable_get_text (GTK_EDITABLE (self));
48 cc_hostname_set_hostname (cc_hostname_get_default (), text);
49
50 return FALSE;
51 }
52
53 static void
54 reset_hostname_timeout (CcHostnameEntry *self)
55 {
56 g_clear_handle_id (&self->set_hostname_timeout_source_id, g_source_remove);
57
58 self->set_hostname_timeout_source_id = g_timeout_add_seconds (SET_HOSTNAME_TIMEOUT,
59 (GSourceFunc) set_hostname_timeout,
60 self);
61 }
62
63 static void
64 text_changed_cb (CcHostnameEntry *entry)
65 {
66 reset_hostname_timeout (entry);
67 }
68
69 static void
70 cc_hostname_entry_dispose (GObject *object)
71 {
72 CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
73
74 if (self->set_hostname_timeout_source_id)
75 {
76 g_clear_handle_id (&self->set_hostname_timeout_source_id, g_source_remove);
77 set_hostname_timeout (self);
78 }
79
80 G_OBJECT_CLASS (cc_hostname_entry_parent_class)->dispose (object);
81 }
82
83 static void
84 cc_hostname_entry_constructed (GObject *object)
85 {
86 CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
87 GPermission *permission;
88 g_autoptr(GError) error = NULL;
89 g_autofree gchar *str = NULL;
90
91 permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname",
92 NULL, NULL, NULL);
93
94 /* Is hostnamed installed? */
95 if (permission == NULL)
96 {
97 g_debug ("Will not show hostname, hostnamed not installed");
98
99 gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
100
101 return;
102 }
103
104 if (g_permission_get_allowed (permission))
105 gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
106 else
107 {
108 g_debug ("Not allowed to change the hostname");
109 gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
110 }
111
112 str = cc_hostname_get_display_hostname (cc_hostname_get_default ());
113 if (str != NULL)
114 gtk_editable_set_text (GTK_EDITABLE (self), str);
115 else
116 gtk_editable_set_text (GTK_EDITABLE (self), "");
117
118 g_signal_connect (self, "apply", G_CALLBACK (text_changed_cb), NULL);
119
120 adw_entry_row_set_show_apply_button (ADW_ENTRY_ROW (self), TRUE);
121 }
122
123 static void
124 cc_hostname_entry_class_init (CcHostnameEntryClass *klass)
125 {
126 GObjectClass *object_class = G_OBJECT_CLASS (klass);
127
128 object_class->constructed = cc_hostname_entry_constructed;
129 object_class->dispose = cc_hostname_entry_dispose;
130 }
131
132 static void
133 cc_hostname_entry_init (CcHostnameEntry *self)
134 {
135 g_resources_register (cc_common_get_resource ());
136 }
137
138 CcHostnameEntry *
139 cc_hostname_entry_new (void)
140 {
141 return g_object_new (CC_TYPE_HOSTNAME_ENTRY, NULL);
142 }
143