1 /* dspy-name-row.c
2  *
3  * Copyright 2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This file is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as
7  * published by the Free Software Foundation; either version 3 of the
8  * License, or (at your option) any later version.
9  *
10  * This file is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: LGPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "dspy-name-row"
22 
23 #include "config.h"
24 
25 #include <glib/gi18n.h>
26 
27 #include "dspy-name-row.h"
28 
29 struct _DspyNameRow
30 {
31   GtkListBoxRow  parent_instance;
32 
33   DspyName      *name;
34 
35   GtkLabel      *title;
36   GtkLabel      *subtitle;
37 };
38 
39 G_DEFINE_FINAL_TYPE (DspyNameRow, dspy_name_row, GTK_TYPE_LIST_BOX_ROW)
40 
41 enum {
42   PROP_0,
43   PROP_NAME,
44   N_PROPS
45 };
46 
47 static GParamSpec *properties [N_PROPS];
48 
49 /**
50  * dspy_name_row_new:
51  * @name: a #DspyName
52  *
53  * Create a new #DspyNameRow.
54  *
55  * Returns: (transfer full): a newly created #DspyNameRow
56  */
57 GtkWidget *
dspy_name_row_new(DspyName * name)58 dspy_name_row_new (DspyName *name)
59 {
60   g_return_val_if_fail (DSPY_IS_NAME (name), NULL);
61 
62   return g_object_new (DSPY_TYPE_NAME_ROW,
63                        "name", name,
64                        "visible", TRUE,
65                        NULL);
66 }
67 
68 static void
dspy_name_row_update(DspyNameRow * self)69 dspy_name_row_update (DspyNameRow *self)
70 {
71   g_autoptr(GString) str = NULL;
72   GPid pid;
73 
74   g_assert (DSPY_IS_NAME_ROW (self));
75 
76   pid = dspy_name_get_pid (self->name);
77   str = g_string_new (NULL);
78 
79   if (dspy_name_get_activatable (self->name))
80     g_string_append_printf (str, _("%s: %s"), _("Activatable"), _("Yes"));
81   else
82     g_string_append_printf (str, _("%s: %s"), _("Activatable"), _("No"));
83 
84   if (pid > -1)
85     {
86       g_string_append (str, ", ");
87       g_string_append_printf (str, _("%s: %u"), _("PID"), pid);
88     }
89 
90   gtk_label_set_label (self->subtitle, str->str);
91 
92   gtk_widget_set_tooltip_text (GTK_WIDGET (self),
93                                dspy_name_get_owner (self->name));
94 }
95 
96 static void
dspy_name_row_set_name(DspyNameRow * self,DspyName * name)97 dspy_name_row_set_name (DspyNameRow *self,
98                         DspyName    *name)
99 {
100   g_assert (DSPY_IS_NAME_ROW (self));
101   g_assert (DSPY_IS_NAME (name));
102   g_assert (self->name == NULL);
103 
104   g_set_object (&self->name, name);
105 
106   g_signal_connect_object (self->name,
107                            "notify::pid",
108                            G_CALLBACK (dspy_name_row_update),
109                            self,
110                            G_CONNECT_SWAPPED);
111 
112   g_signal_connect_object (self->name,
113                            "notify::activatable",
114                            G_CALLBACK (dspy_name_row_update),
115                            self,
116                            G_CONNECT_SWAPPED);
117 
118   gtk_label_set_label (self->title, dspy_name_get_name (self->name));
119 
120   dspy_name_row_update (self);
121 }
122 
123 static void
dspy_name_row_finalize(GObject * object)124 dspy_name_row_finalize (GObject *object)
125 {
126   DspyNameRow *self = (DspyNameRow *)object;
127 
128   g_clear_object (&self->name);
129 
130   G_OBJECT_CLASS (dspy_name_row_parent_class)->finalize (object);
131 }
132 
133 static void
dspy_name_row_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)134 dspy_name_row_get_property (GObject    *object,
135                             guint       prop_id,
136                             GValue     *value,
137                             GParamSpec *pspec)
138 {
139   DspyNameRow *self = DSPY_NAME_ROW (object);
140 
141   switch (prop_id)
142     {
143     case PROP_NAME:
144       g_value_set_object (value, dspy_name_row_get_name (self));
145       break;
146 
147     default:
148       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149     }
150 }
151 
152 static void
dspy_name_row_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)153 dspy_name_row_set_property (GObject      *object,
154                             guint         prop_id,
155                             const GValue *value,
156                             GParamSpec   *pspec)
157 {
158   DspyNameRow *self = DSPY_NAME_ROW (object);
159 
160   switch (prop_id)
161     {
162     case PROP_NAME:
163       dspy_name_row_set_name (self, g_value_get_object (value));
164       break;
165 
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168     }
169 }
170 
171 static void
dspy_name_row_class_init(DspyNameRowClass * klass)172 dspy_name_row_class_init (DspyNameRowClass *klass)
173 {
174   GObjectClass *object_class = G_OBJECT_CLASS (klass);
175   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
176 
177   object_class->finalize = dspy_name_row_finalize;
178   object_class->get_property = dspy_name_row_get_property;
179   object_class->set_property = dspy_name_row_set_property;
180 
181   properties [PROP_NAME] =
182     g_param_spec_object ("name",
183                          "Name",
184                          "The DspyName for the row",
185                          DSPY_TYPE_NAME,
186                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
187 
188   g_object_class_install_properties (object_class, N_PROPS, properties);
189 
190   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/dspy/dspy-name-row.ui");
191   gtk_widget_class_bind_template_child (widget_class, DspyNameRow, subtitle);
192   gtk_widget_class_bind_template_child (widget_class, DspyNameRow, title);
193 }
194 
195 static void
dspy_name_row_init(DspyNameRow * self)196 dspy_name_row_init (DspyNameRow *self)
197 {
198   gtk_widget_init_template (GTK_WIDGET (self));
199 }
200 
201 /**
202  * dspy_name_row_get_name:
203  *
204  * Gets the #DspyName for the row.
205  *
206  * Returns: (transfer none): a #DspyName
207  */
208 DspyName *
dspy_name_row_get_name(DspyNameRow * self)209 dspy_name_row_get_name (DspyNameRow *self)
210 {
211   g_return_val_if_fail (DSPY_IS_NAME_ROW (self), NULL);
212 
213   return self->name;
214 }
215