1 /* ide-environment.c
2  *
3  * Copyright 2016-2019 Christian Hergert <christian@hergert.me>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "ide-environment"
22 
23 #include "config.h"
24 
25 #include <libide-core.h>
26 
27 #include "ide-environment.h"
28 #include "ide-environment-variable.h"
29 
30 struct _IdeEnvironment
31 {
32   GObject    parent_instance;
33   GPtrArray *variables;
34 };
35 
36 static void list_model_iface_init (GListModelInterface *iface);
37 
38 G_DEFINE_TYPE_EXTENDED (IdeEnvironment, ide_environment, G_TYPE_OBJECT, G_TYPE_FLAG_FINAL,
39                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
40 
41 enum {
42   CHANGED,
43   LAST_SIGNAL
44 };
45 
46 static guint signals [LAST_SIGNAL];
47 
48 static void
ide_environment_finalize(GObject * object)49 ide_environment_finalize (GObject *object)
50 {
51   IdeEnvironment *self = (IdeEnvironment *)object;
52 
53   g_clear_pointer (&self->variables, g_ptr_array_unref);
54 
55   G_OBJECT_CLASS (ide_environment_parent_class)->finalize (object);
56 }
57 
58 static void
ide_environment_class_init(IdeEnvironmentClass * klass)59 ide_environment_class_init (IdeEnvironmentClass *klass)
60 {
61   GObjectClass *object_class = G_OBJECT_CLASS (klass);
62 
63   object_class->finalize = ide_environment_finalize;
64 
65   signals [CHANGED] =
66     g_signal_new ("changed",
67                   G_TYPE_FROM_CLASS (klass),
68                   G_SIGNAL_RUN_LAST,
69                   0, NULL, NULL,
70                   g_cclosure_marshal_VOID__VOID,
71                   G_TYPE_NONE, 0);
72   g_signal_set_va_marshaller (signals [CHANGED],
73                               G_TYPE_FROM_CLASS (klass),
74                               g_cclosure_marshal_VOID__VOIDv);
75 }
76 
77 static void
ide_environment_items_changed(IdeEnvironment * self)78 ide_environment_items_changed (IdeEnvironment *self)
79 {
80   g_assert (IDE_IS_ENVIRONMENT (self));
81 
82   g_signal_emit (self, signals [CHANGED], 0);
83 }
84 
85 static void
ide_environment_init(IdeEnvironment * self)86 ide_environment_init (IdeEnvironment *self)
87 {
88   self->variables = g_ptr_array_new_with_free_func (g_object_unref);
89 
90   g_signal_connect (self,
91                     "items-changed",
92                     G_CALLBACK (ide_environment_items_changed),
93                     NULL);
94 }
95 
96 static GType
ide_environment_get_item_type(GListModel * model)97 ide_environment_get_item_type (GListModel *model)
98 {
99   return IDE_TYPE_ENVIRONMENT_VARIABLE;
100 }
101 
102 static gpointer
ide_environment_get_item(GListModel * model,guint position)103 ide_environment_get_item (GListModel *model,
104                           guint       position)
105 {
106   IdeEnvironment *self = (IdeEnvironment *)model;
107 
108   g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
109   g_return_val_if_fail (position < self->variables->len, NULL);
110 
111   return g_object_ref (g_ptr_array_index (self->variables, position));
112 }
113 
114 static guint
ide_environment_get_n_items(GListModel * model)115 ide_environment_get_n_items (GListModel *model)
116 {
117   IdeEnvironment *self = (IdeEnvironment *)model;
118 
119   g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), 0);
120 
121   return self->variables->len;
122 }
123 
124 static void
list_model_iface_init(GListModelInterface * iface)125 list_model_iface_init (GListModelInterface *iface)
126 {
127   iface->get_n_items = ide_environment_get_n_items;
128   iface->get_item = ide_environment_get_item;
129   iface->get_item_type = ide_environment_get_item_type;
130 }
131 
132 static void
ide_environment_variable_notify(IdeEnvironment * self,GParamSpec * pspec,IdeEnvironmentVariable * variable)133 ide_environment_variable_notify (IdeEnvironment         *self,
134                                  GParamSpec             *pspec,
135                                  IdeEnvironmentVariable *variable)
136 {
137   g_assert (IDE_IS_ENVIRONMENT (self));
138 
139   g_signal_emit (self, signals [CHANGED], 0);
140 }
141 
142 void
ide_environment_setenv(IdeEnvironment * self,const gchar * key,const gchar * value)143 ide_environment_setenv (IdeEnvironment *self,
144                         const gchar    *key,
145                         const gchar    *value)
146 {
147   guint i;
148 
149   g_return_if_fail (IDE_IS_ENVIRONMENT (self));
150   g_return_if_fail (key != NULL);
151 
152   for (i = 0; i < self->variables->len; i++)
153     {
154       IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
155       const gchar *var_key = ide_environment_variable_get_key (var);
156 
157       if (g_strcmp0 (key, var_key) == 0)
158         {
159           if (value == NULL)
160             {
161               g_ptr_array_remove_index (self->variables, i);
162               g_list_model_items_changed (G_LIST_MODEL (self), i, 1, 0);
163               return;
164             }
165 
166           ide_environment_variable_set_value (var, value);
167           return;
168         }
169     }
170 
171   if (value != NULL)
172     {
173       IdeEnvironmentVariable *var;
174       guint position = self->variables->len;
175 
176       var = g_object_new (IDE_TYPE_ENVIRONMENT_VARIABLE,
177                           "key", key,
178                           "value", value,
179                           NULL);
180       g_signal_connect_object (var,
181                                "notify",
182                                G_CALLBACK (ide_environment_variable_notify),
183                                self,
184                                G_CONNECT_SWAPPED);
185       g_ptr_array_add (self->variables, var);
186       g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
187     }
188 }
189 
190 const gchar *
ide_environment_getenv(IdeEnvironment * self,const gchar * key)191 ide_environment_getenv (IdeEnvironment *self,
192                         const gchar    *key)
193 {
194   guint i;
195 
196   g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
197   g_return_val_if_fail (key != NULL, NULL);
198 
199   for (i = 0; i < self->variables->len; i++)
200     {
201       IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
202       const gchar *var_key = ide_environment_variable_get_key (var);
203 
204       if (g_strcmp0 (key, var_key) == 0)
205         return ide_environment_variable_get_value (var);
206     }
207 
208   return NULL;
209 }
210 
211 /**
212  * ide_environment_get_environ:
213  * @self: An #IdeEnvironment
214  *
215  * Gets the environment as a set of key=value pairs, suitable for use
216  * in various GLib process functions.
217  *
218  * Returns: (transfer full): A newly allocated string array.
219  *
220  * Since: 3.32
221  */
222 gchar **
ide_environment_get_environ(IdeEnvironment * self)223 ide_environment_get_environ (IdeEnvironment *self)
224 {
225   GPtrArray *ar;
226   guint i;
227 
228   g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
229 
230   ar = g_ptr_array_new ();
231 
232   for (i = 0; i < self->variables->len; i++)
233     {
234       IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
235       const gchar *key = ide_environment_variable_get_key (var);
236       const gchar *value = ide_environment_variable_get_value (var);
237 
238       if (value == NULL)
239         value = "";
240 
241       if (key != NULL)
242         g_ptr_array_add (ar, g_strdup_printf ("%s=%s", key, value));
243     }
244 
245   g_ptr_array_add (ar, NULL);
246 
247   return (gchar **)g_ptr_array_free (ar, FALSE);
248 }
249 
250 void
ide_environment_set_environ(IdeEnvironment * self,const gchar * const * env)251 ide_environment_set_environ (IdeEnvironment      *self,
252                              const gchar * const *env)
253 {
254   guint len;
255 
256   g_return_if_fail (IDE_IS_ENVIRONMENT (self));
257 
258   len = self->variables->len;
259 
260   if (len > 0)
261     {
262       g_ptr_array_remove_range (self->variables, 0, len);
263       g_list_model_items_changed (G_LIST_MODEL (self), 0, len, 0);
264     }
265 
266   if (env != NULL)
267     {
268       for (guint i = 0; env[i]; i++)
269         {
270           g_autofree gchar *key = NULL;
271           g_autofree gchar *val = NULL;
272 
273           if (ide_environ_parse (env[i], &key, &val))
274             ide_environment_setenv (self, key, val);
275         }
276     }
277 }
278 
279 IdeEnvironment *
ide_environment_new(void)280 ide_environment_new (void)
281 {
282   return g_object_new (IDE_TYPE_ENVIRONMENT, NULL);
283 }
284 
285 void
ide_environment_remove(IdeEnvironment * self,IdeEnvironmentVariable * variable)286 ide_environment_remove (IdeEnvironment         *self,
287                         IdeEnvironmentVariable *variable)
288 {
289   guint i;
290 
291   g_return_if_fail (IDE_IS_ENVIRONMENT (self));
292   g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (variable));
293 
294   for (i = 0; i < self->variables->len; i++)
295     {
296       IdeEnvironmentVariable *item = g_ptr_array_index (self->variables, i);
297 
298       if (item == variable)
299         {
300           g_ptr_array_remove_index (self->variables, i);
301           g_list_model_items_changed (G_LIST_MODEL (self), i, 1, 0);
302           break;
303         }
304     }
305 }
306 
307 void
ide_environment_append(IdeEnvironment * self,IdeEnvironmentVariable * variable)308 ide_environment_append (IdeEnvironment         *self,
309                         IdeEnvironmentVariable *variable)
310 {
311   guint position;
312 
313   g_return_if_fail (IDE_IS_ENVIRONMENT (self));
314   g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (variable));
315 
316   position = self->variables->len;
317 
318   g_signal_connect_object (variable,
319                            "notify",
320                            G_CALLBACK (ide_environment_variable_notify),
321                            self,
322                            G_CONNECT_SWAPPED);
323   g_ptr_array_add (self->variables, g_object_ref (variable));
324   g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
325 }
326 
327 /**
328  * ide_environment_copy:
329  * @self: An #IdeEnvironment
330  *
331  * Copies the contents of #IdeEnvironment into a newly allocated #IdeEnvironment.
332  *
333  * Returns: (transfer full): An #IdeEnvironment.
334  *
335  * Since: 3.32
336  */
337 IdeEnvironment *
ide_environment_copy(IdeEnvironment * self)338 ide_environment_copy (IdeEnvironment *self)
339 {
340   g_autoptr(IdeEnvironment) copy = NULL;
341 
342   g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
343 
344   copy = ide_environment_new ();
345   ide_environment_copy_into (self, copy, TRUE);
346 
347   return g_steal_pointer (&copy);
348 }
349 
350 void
ide_environment_copy_into(IdeEnvironment * self,IdeEnvironment * dest,gboolean replace)351 ide_environment_copy_into (IdeEnvironment *self,
352                            IdeEnvironment *dest,
353                            gboolean        replace)
354 {
355   g_return_if_fail (IDE_IS_ENVIRONMENT (self));
356   g_return_if_fail (IDE_IS_ENVIRONMENT (dest));
357 
358   for (guint i = 0; i < self->variables->len; i++)
359     {
360       IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
361       const gchar *key = ide_environment_variable_get_key (var);
362       const gchar *value = ide_environment_variable_get_value (var);
363 
364       if (replace || ide_environment_getenv (dest, key) == NULL)
365         ide_environment_setenv (dest, key, value);
366     }
367 }
368 
369 /**
370  * ide_environ_parse:
371  * @pair: the KEY=VALUE pair
372  * @key: (out) (optional): a location for a @key
373  * @value: (out) (optional): a location for a @value
374  *
375  * Parses a KEY=VALUE style key-pair into @key and @value.
376  *
377  * Returns: %TRUE if @pair was successfully parsed
378  *
379  * Since: 3.32
380  */
381 gboolean
ide_environ_parse(const gchar * pair,gchar ** key,gchar ** value)382 ide_environ_parse (const gchar  *pair,
383                    gchar       **key,
384                    gchar       **value)
385 {
386   const gchar *eq;
387 
388   g_return_val_if_fail (pair != NULL, FALSE);
389 
390   if (key != NULL)
391     *key = NULL;
392 
393   if (value != NULL)
394     *value = NULL;
395 
396   if ((eq = strchr (pair, '=')))
397     {
398       if (key != NULL)
399         *key = g_strndup (pair, eq - pair);
400 
401       if (value != NULL)
402         *value = g_strdup (eq + 1);
403 
404       return TRUE;
405     }
406 
407   return FALSE;
408 }
409