[gnome-session/benzea/systemd-condition-evaluator: 1/3] autostart: Move helpers for condition evaluation into new file



commit 8cc286bc0b8a3cf274532660a0b4b53eafbc9408
Author: Benjamin Berg <bberg redhat com>
Date:   Thu Apr 9 15:21:51 2020 +0200

    autostart: Move helpers for condition evaluation into new file
    
    This is in preparation for allowing their use from other tools.

 gnome-session/gsm-autostart-app.c       | 150 ++++----------------------------
 gnome-session/gsm-autostart-condition.c | 129 +++++++++++++++++++++++++++
 gnome-session/gsm-autostart-condition.h |  45 ++++++++++
 gnome-session/meson.build               |   1 +
 4 files changed, 191 insertions(+), 134 deletions(-)
---
diff --git a/gnome-session/gsm-autostart-app.c b/gnome-session/gsm-autostart-app.c
index 0893300b..0320851f 100644
--- a/gnome-session/gsm-autostart-app.c
+++ b/gnome-session/gsm-autostart-app.c
@@ -39,6 +39,7 @@
 #endif
 
 #include "gsm-autostart-app.h"
+#include "gsm-autostart-condition.h"
 #include "gsm-util.h"
 
 enum {
@@ -46,16 +47,6 @@ enum {
         AUTOSTART_LAUNCH_ACTIVATE
 };
 
-enum {
-        GSM_CONDITION_NONE           = 0,
-        GSM_CONDITION_IF_EXISTS      = 1,
-        GSM_CONDITION_UNLESS_EXISTS  = 2,
-        GSM_CONDITION_GSETTINGS      = 3,
-        GSM_CONDITION_IF_SESSION     = 4,
-        GSM_CONDITION_UNLESS_SESSION = 5,
-        GSM_CONDITION_UNKNOWN        = 6
-};
-
 #define GSM_SESSION_CLIENT_DBUS_INTERFACE "org.gnome.SessionClient"
 
 struct _GsmAutostartAppPrivate {
@@ -160,61 +151,6 @@ is_disabled (GsmApp *app)
         return FALSE;
 }
 
-static gboolean
-parse_condition_string (const char *condition_string,
-                        guint      *condition_kindp,
-                        char      **keyp)
-{
-        const char *space;
-        const char *key;
-        int         len;
-        guint       kind;
-
-        space = condition_string + strcspn (condition_string, " ");
-        len = space - condition_string;
-        key = space;
-        while (isspace ((unsigned char)*key)) {
-                key++;
-        }
-
-        kind = GSM_CONDITION_UNKNOWN;
-
-        if (!g_ascii_strncasecmp (condition_string, "if-exists", len) && key) {
-                kind = GSM_CONDITION_IF_EXISTS;
-        } else if (!g_ascii_strncasecmp (condition_string, "unless-exists", len) && key) {
-                kind = GSM_CONDITION_UNLESS_EXISTS;
-        } else if (!g_ascii_strncasecmp (condition_string, "GSettings", len)) {
-                kind = GSM_CONDITION_GSETTINGS;
-        } else if (!g_ascii_strncasecmp (condition_string, "GNOME3", len)) {
-                condition_string = key;
-                space = condition_string + strcspn (condition_string, " ");
-                len = space - condition_string;
-                key = space;
-                while (isspace ((unsigned char)*key)) {
-                        key++;
-                }
-                if (!g_ascii_strncasecmp (condition_string, "if-session", len) && key) {
-                        kind = GSM_CONDITION_IF_SESSION;
-                } else if (!g_ascii_strncasecmp (condition_string, "unless-session", len) && key) {
-                        kind = GSM_CONDITION_UNLESS_SESSION;
-                }
-        }
-
-        if (kind == GSM_CONDITION_UNKNOWN) {
-                key = NULL;
-        }
-
-        if (keyp != NULL) {
-                *keyp = g_strdup (key);
-        }
-
-        if (condition_kindp != NULL) {
-                *condition_kindp = kind;
-        }
-
-        return (kind != GSM_CONDITION_UNKNOWN);
-}
-
 static void
 if_exists_condition_cb (GFileMonitor     *monitor,
                         GFile            *file,
@@ -309,66 +245,20 @@ static gboolean
 setup_gsettings_condition_monitor (GsmAutostartApp *app,
                                    const char      *key)
 {
-        GSettingsSchemaSource *source;
-        GSettingsSchema *schema;
-        GSettings *settings;
-        GSettingsSchemaKey *schema_key;
-        const GVariantType *key_type;
-        char **elems;
-        gboolean retval = FALSE;
-        char *signal;
-
-        retval = FALSE;
-
-        schema = NULL;
+        g_autoptr(GSettings) settings = NULL;
+        g_autofree char *settings_key = NULL;
+        g_autofree char *signal = NULL;
 
-        elems = g_strsplit (key, " ", 2);
-
-        if (elems == NULL)
-                goto out;
-
-        if (elems[0] == NULL || elems[1] == NULL)
-                goto out;
-
-        source = g_settings_schema_source_get_default ();
-
-        schema = g_settings_schema_source_lookup (source, elems[0], TRUE);
-
-        if (schema == NULL)
-                goto out;
-
-        if (!g_settings_schema_has_key (schema, elems[1]))
-                goto out;
-
-        schema_key = g_settings_schema_get_key (schema, elems[1]);
-
-        g_assert (schema_key != NULL);
-
-        key_type = g_settings_schema_key_get_value_type (schema_key);
-
-        g_settings_schema_key_unref (schema_key);
-
-        g_assert (key_type != NULL);
-
-        if (!g_variant_type_equal (key_type, G_VARIANT_TYPE_BOOLEAN))
-                goto out;
-
-        settings = g_settings_new_full (schema, NULL, NULL);
-        retval = g_settings_get_boolean (settings, elems[1]);
+        if (!gsm_autostart_condition_resolve_settings (key, &settings, &settings_key))
+                return FALSE;
 
-        signal = g_strdup_printf ("changed::%s", elems[1]);
+        signal = g_strdup_printf ("changed::%s", settings_key);
         g_signal_connect (G_OBJECT (settings), signal,
                           G_CALLBACK (gsettings_condition_cb), app);
-        g_free (signal);
 
         app->priv->condition_settings = settings;
 
-out:
-        if (schema)
-                g_settings_schema_unref (schema);
-        g_strfreev (elems);
-
-        return retval;
+        return g_settings_get_boolean (settings, settings_key);
 }
 
 static void
@@ -388,7 +278,7 @@ if_session_condition_cb (GObject    *object,
 
         priv = GSM_AUTOSTART_APP (app)->priv;
 
-        parse_condition_string (priv->condition_string, NULL, &key);
+        gsm_autostart_condition_parse (priv->condition_string, NULL, &key);
 
         g_object_get (object, "session-name", &session_name, NULL);
         condition = strcmp (session_name, key) == 0;
@@ -424,7 +314,7 @@ unless_session_condition_cb (GObject    *object,
 
         priv = GSM_AUTOSTART_APP (app)->priv;
 
-        parse_condition_string (priv->condition_string, NULL, &key);
+        gsm_autostart_condition_parse (priv->condition_string, NULL, &key);
 
         g_object_get (object, "session-name", &session_name, NULL);
         condition = strcmp (session_name, key) != 0;
@@ -443,14 +333,6 @@ unless_session_condition_cb (GObject    *object,
         }
 }
 
-static char *
-resolve_conditional_file_path (const char *file)
-{
-        if (g_path_is_absolute (file))
-                return g_strdup (file);
-        return g_build_filename (g_get_user_config_dir (), file, NULL);
-}
-
 static void
 setup_condition_monitor (GsmAutostartApp *app)
 {
@@ -473,7 +355,7 @@ setup_condition_monitor (GsmAutostartApp *app)
         }
 
         key = NULL;
-        res = parse_condition_string (app->priv->condition_string, &kind, &key);
+        res = gsm_autostart_condition_parse (app->priv->condition_string, &kind, &key);
         if (! res) {
                 g_free (key);
                 return;
@@ -487,7 +369,7 @@ setup_condition_monitor (GsmAutostartApp *app)
                 char  *file_path;
                 GFile *file;
 
-                file_path = resolve_conditional_file_path (key);
+                file_path = gsm_autostart_condition_resolve_file_path (key);
                 disabled = !g_file_test (file_path, G_FILE_TEST_EXISTS);
 
                 file = g_file_new_for_path (file_path);
@@ -503,7 +385,7 @@ setup_condition_monitor (GsmAutostartApp *app)
                 char  *file_path;
                 GFile *file;
 
-                file_path = resolve_conditional_file_path (key);
+                file_path = gsm_autostart_condition_resolve_file_path (key);
                 disabled = g_file_test (file_path, G_FILE_TEST_EXISTS);
 
                 file = g_file_new_for_path (file_path);
@@ -774,7 +656,7 @@ is_conditionally_disabled (GsmApp *app)
         }
 
         key = NULL;
-        res = parse_condition_string (priv->condition_string, &kind, &key);
+        res = gsm_autostart_condition_parse (priv->condition_string, &kind, &key);
         if (! res) {
                 g_free (key);
                 return TRUE;
@@ -787,13 +669,13 @@ is_conditionally_disabled (GsmApp *app)
         if (kind == GSM_CONDITION_IF_EXISTS) {
                 char *file_path;
 
-                file_path = resolve_conditional_file_path (key);
+                file_path = gsm_autostart_condition_resolve_file_path (key);
                 disabled = !g_file_test (file_path, G_FILE_TEST_EXISTS);
                 g_free (file_path);
         } else if (kind == GSM_CONDITION_UNLESS_EXISTS) {
                 char *file_path;
 
-                file_path = resolve_conditional_file_path (key);
+                file_path = gsm_autostart_condition_resolve_file_path (key);
                 disabled = g_file_test (file_path, G_FILE_TEST_EXISTS);
                 g_free (file_path);
         } else if (kind == GSM_CONDITION_GSETTINGS &&
diff --git a/gnome-session/gsm-autostart-condition.c b/gnome-session/gsm-autostart-condition.c
new file mode 100644
index 00000000..a5b43674
--- /dev/null
+++ b/gnome-session/gsm-autostart-condition.c
@@ -0,0 +1,129 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Novell, Inc.
+ * Copyright (C) 2008 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
+ * Lesser 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 "gsm-autostart-condition.h"
+#include <ctype.h>
+
+char *
+gsm_autostart_condition_resolve_file_path (const char *file)
+{
+        if (g_path_is_absolute (file))
+                return g_strdup (file);
+        return g_build_filename (g_get_user_config_dir (), file, NULL);
+}
+
+
+gboolean
+gsm_autostart_condition_resolve_settings (const char *condition,
+                                          GSettings **settings,
+                                          char **settings_key)
+{
+        g_auto(GStrv) elems = NULL;
+        g_autoptr(GSettingsSchema) schema = NULL;
+        g_autoptr(GSettingsSchemaKey) schema_key = NULL;
+        GSettingsSchemaSource *source;
+        const GVariantType *key_type;
+
+        elems = g_strsplit (condition, " ", 2);
+        if (elems == NULL)
+                return FALSE;
+        if (elems[0] == NULL || elems[1] == NULL)
+                return FALSE;
+
+        source = g_settings_schema_source_get_default ();
+        schema = g_settings_schema_source_lookup (source, elems[0], TRUE);
+        if (schema == NULL)
+                return FALSE;
+
+        if (!g_settings_schema_has_key (schema, elems[1]))
+                return FALSE;
+
+        schema_key = g_settings_schema_get_key (schema, elems[1]);
+        g_assert (schema_key != NULL);
+
+        key_type = g_settings_schema_key_get_value_type (schema_key);
+
+        g_settings_schema_key_unref (schema_key);
+        g_assert (key_type != NULL);
+
+        if (!g_variant_type_equal (key_type, G_VARIANT_TYPE_BOOLEAN))
+                return FALSE;
+
+        *settings = g_settings_new_full (schema, NULL, NULL);
+        *settings_key = g_strdup(elems[1]);
+
+        return TRUE;
+}
+
+gboolean
+gsm_autostart_condition_parse (const char             *condition_string,
+                               GsmAutostartCondition  *condition_kindp,
+                               char                  **keyp)
+{
+        const char *space;
+        const char *key;
+        int         len;
+        guint       kind;
+
+        space = condition_string + strcspn (condition_string, " ");
+        len = space - condition_string;
+        key = space;
+        while (isspace ((unsigned char)*key)) {
+                key++;
+        }
+
+        kind = GSM_CONDITION_UNKNOWN;
+
+        if (!g_ascii_strncasecmp (condition_string, "if-exists", len) && key) {
+                kind = GSM_CONDITION_IF_EXISTS;
+        } else if (!g_ascii_strncasecmp (condition_string, "unless-exists", len) && key) {
+                kind = GSM_CONDITION_UNLESS_EXISTS;
+        } else if (!g_ascii_strncasecmp (condition_string, "GSettings", len)) {
+                kind = GSM_CONDITION_GSETTINGS;
+        } else if (!g_ascii_strncasecmp (condition_string, "GNOME3", len)) {
+                condition_string = key;
+                space = condition_string + strcspn (condition_string, " ");
+                len = space - condition_string;
+                key = space;
+                while (isspace ((unsigned char)*key)) {
+                        key++;
+                }
+                if (!g_ascii_strncasecmp (condition_string, "if-session", len) && key) {
+                        kind = GSM_CONDITION_IF_SESSION;
+                } else if (!g_ascii_strncasecmp (condition_string, "unless-session", len) && key) {
+                        kind = GSM_CONDITION_UNLESS_SESSION;
+                }
+        }
+
+        if (kind == GSM_CONDITION_UNKNOWN) {
+                key = NULL;
+        }
+
+        if (keyp != NULL) {
+                *keyp = g_strdup (key);
+        }
+
+        if (condition_kindp != NULL) {
+                *condition_kindp = kind;
+        }
+
+        return (kind != GSM_CONDITION_UNKNOWN);
+}
+
+
diff --git a/gnome-session/gsm-autostart-condition.h b/gnome-session/gsm-autostart-condition.h
new file mode 100644
index 00000000..c6b83ae2
--- /dev/null
+++ b/gnome-session/gsm-autostart-condition.h
@@ -0,0 +1,45 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Novell, Inc.
+ * Copyright (C) 2008 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
+ * Lesser 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/>.
+ */
+
+#ifndef __GSM_AUTOSTART_CONDITION_H__
+#define __GSM_AUTOSTART_CONDITION_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+typedef enum {
+        GSM_CONDITION_NONE           = 0,
+        GSM_CONDITION_IF_EXISTS      = 1,
+        GSM_CONDITION_UNLESS_EXISTS  = 2,
+        GSM_CONDITION_GSETTINGS      = 3,
+        GSM_CONDITION_IF_SESSION     = 4,
+        GSM_CONDITION_UNLESS_SESSION = 5,
+        GSM_CONDITION_UNKNOWN        = 6
+} GsmAutostartCondition;
+
+char *   gsm_autostart_condition_resolve_file_path (const char *file);
+gboolean gsm_autostart_condition_resolve_settings  (const char *condition,
+                                                    GSettings **settings,
+                                                    char **settings_key);
+
+gboolean gsm_autostart_condition_parse (const char             *condition_string,
+                                        GsmAutostartCondition  *condition_kindp,
+                                        char                  **keyp);
+
+#endif /* __GSM_AUTOSTART_CONDITION_H__ */
diff --git a/gnome-session/meson.build b/gnome-session/meson.build
index 7d2f3c04..7ae506f3 100644
--- a/gnome-session/meson.build
+++ b/gnome-session/meson.build
@@ -21,6 +21,7 @@ libgsmutil = static_library(
 sources = files(
   'gsm-app.c',
   'gsm-autostart-app.c',
+  'gsm-autostart-condition.c',
   'gsm-client.c',
   'gsm-dbus-client.c',
   'gsm-fail-whale.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]