| Directory: | src/ |
|---|---|
| File: | src/backend/modules/config-gnome.c |
| Date: | 2023-01-04 17:35:37 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 15 | 45 | 33.3% |
| Branches: | 6 | 21 | 28.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* config-gnome.c | ||
| 2 | * | ||
| 3 | * Copyright 2022-2023 Jan-Michael Brummer | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library 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 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 library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | * | ||
| 19 | * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <glib-object.h> | ||
| 23 | |||
| 24 | #include "config-gnome.h" | ||
| 25 | #include "px-module.h" | ||
| 26 | #include "px-config-module.h" | ||
| 27 | |||
| 28 | struct _PxGnomeModule { | ||
| 29 | GObject parent_instance; | ||
| 30 | GSettings *proxy_settings; | ||
| 31 | }; | ||
| 32 | |||
| 33 | enum { | ||
| 34 | GNOME_PROXY_MODE_AUTO = 2 | ||
| 35 | }; | ||
| 36 | |||
| 37 | static void module_interface_init (gpointer g_iface, | ||
| 38 | gpointer data); | ||
| 39 | |||
| 40 |
6/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 8 times.
|
28 | G_DEFINE_TYPE_WITH_CODE (PxGnomeModule, px_gnome_module, |
| 41 | PX_TYPE_MODULE, | ||
| 42 | G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG_MODULE, module_interface_init); | ||
| 43 | ); | ||
| 44 | |||
| 45 | static void | ||
| 46 | ✗ | on_proxy_settings_changed (GSettings *self, | |
| 47 | gchar *key, | ||
| 48 | gpointer user_data) | ||
| 49 | { | ||
| 50 | int mode; | ||
| 51 | char *server; | ||
| 52 | |||
| 53 | ✗ | mode = g_settings_get_enum (self, "mode"); | |
| 54 | |||
| 55 | /* Automatic */ | ||
| 56 | ✗ | if (mode == 2) { | |
| 57 | ✗ | server = g_settings_get_string (self, "autoconfig-url"); | |
| 58 | } else { | ||
| 59 | ✗ | server = g_strdup ("http://moep"); | |
| 60 | } | ||
| 61 | |||
| 62 | ✗ | g_print ("Server: %s\n", server); | |
| 63 | } | ||
| 64 | |||
| 65 | static void | ||
| 66 | 10 | px_gnome_module_init (PxGnomeModule *self) | |
| 67 | { | ||
| 68 | 10 | } | |
| 69 | |||
| 70 | static void | ||
| 71 | 2 | px_gnome_module_class_init (PxGnomeModuleClass *klass) | |
| 72 | { | ||
| 73 | 2 | } | |
| 74 | |||
| 75 | static gboolean | ||
| 76 | ✗ | gnome_check_available (void) | |
| 77 | { | ||
| 78 | ✗ | return (g_getenv ("GNOME_DESKTOP_SESSION_ID") || | |
| 79 | ✗ | (g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "gnome") == 0) || | |
| 80 | ✗ | (g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "mate") == 0)); | |
| 81 | } | ||
| 82 | |||
| 83 | static GStrv | ||
| 84 | ✗ | gnome_get_config (PxModule *module, | |
| 85 | GUri *uri, | ||
| 86 | GError **error) | ||
| 87 | { | ||
| 88 | ✗ | PxGnomeModule *self = PX_GNOME_MODULE (module); | |
| 89 | ✗ | GStrv ret = NULL; | |
| 90 | int mode; | ||
| 91 | ✗ | g_autofree char *server = NULL; | |
| 92 | |||
| 93 | ✗ | if (!self->proxy_settings) { | |
| 94 | ✗ | self->proxy_settings = g_settings_new ("org.gnome.system.proxy"); | |
| 95 | ✗ | g_signal_connect (self->proxy_settings, "changed", G_CALLBACK (on_proxy_settings_changed), NULL); | |
| 96 | ✗ | on_proxy_settings_changed (self->proxy_settings, NULL, self); | |
| 97 | } | ||
| 98 | |||
| 99 | ✗ | mode = g_settings_get_enum (self->proxy_settings, "mode"); | |
| 100 | ✗ | if (mode == GNOME_PROXY_MODE_AUTO) { | |
| 101 | ✗ | char *autoconfig_url = g_settings_get_string (self->proxy_settings, "autoconfig-url"); | |
| 102 | |||
| 103 | ✗ | if (strlen (autoconfig_url) != 0) { | |
| 104 | ✗ | server = g_strdup_printf ("pac+%s", autoconfig_url); | |
| 105 | } else { | ||
| 106 | ✗ | server = g_strdup ("wpad://"); | |
| 107 | } | ||
| 108 | } else { | ||
| 109 | ✗ | server = g_strdup ("direct://"); | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | ret = g_malloc0 (sizeof (char *) * 2); | |
| 113 | ✗ | ret[0] = g_strdup (server); | |
| 114 | |||
| 115 | ✗ | return ret; | |
| 116 | } | ||
| 117 | |||
| 118 | static char * | ||
| 119 | ✗ | gnome_get_ignore (PxModule *module, | |
| 120 | GUri *uri, | ||
| 121 | GError **error) | ||
| 122 | { | ||
| 123 | ✗ | return g_strdup (""); | |
| 124 | } | ||
| 125 | |||
| 126 | static void | ||
| 127 | 2 | module_interface_init (gpointer g_iface, | |
| 128 | gpointer data) | ||
| 129 | { | ||
| 130 | 2 | PxConfigModuleInterface *iface = g_iface; | |
| 131 | |||
| 132 | 2 | iface->name = "GNOME"; | |
| 133 | 2 | iface->version = 1; | |
| 134 | 2 | iface->check_available = gnome_check_available; | |
| 135 | 2 | iface->get_config = gnome_get_config; | |
| 136 | 2 | iface->get_ignore = gnome_get_ignore; | |
| 137 | 2 | } | |
| 138 | |||
| 139 | PxModule * | ||
| 140 | 10 | px_module_create (void) | |
| 141 | { | ||
| 142 | 10 | return g_object_new (PX_TYPE_GNOME_MODULE, NULL); | |
| 143 | } | ||
| 144 |