File: display-profiles.c

package info (click to toggle)
xfce4-settings 4.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,244 kB
  • sloc: ansic: 28,113; sh: 4,365; makefile: 996; python: 111; xml: 43
file content (175 lines) | stat: -rw-r--r-- 5,572 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 *  Copyright (c) 2019 Simon SteinbeiƟ <simon@xfce.org>
 *
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <glib.h>
#include <display-profiles.h>

gint get_size (gchar **i);

gint
get_size (gchar **i) {
    gint num = 0;
    while (*i != NULL) {
        num++;
        i++;
    }
    return num;
}

gboolean
display_settings_profile_name_exists (XfconfChannel *channel, const gchar *new_profile_name)
{
    GHashTable *properties;
    GList *channel_contents, *current;

    properties = xfconf_channel_get_properties (channel, NULL);
    channel_contents = g_hash_table_get_keys (properties);

    /* get all profiles */
    current = g_list_first (channel_contents);
    while (current)
    {
        gchar **current_elements = g_strsplit (current->data, "/", -1);
        gchar *old_profile_name;

        if (get_size (current_elements) != 2)
        {
            g_strfreev (current_elements);
            current = g_list_next (current);
            continue;
        }

        old_profile_name = xfconf_channel_get_string (channel, current->data, NULL);
        if (g_strcmp0 (new_profile_name, old_profile_name) == 0)
        {
            g_free (old_profile_name);
            return FALSE;
        }
        g_free (old_profile_name);

        current = g_list_next (current);
    }
    g_list_free (channel_contents);
    g_hash_table_destroy (properties);
    return TRUE;
}

GList*
display_settings_get_profiles (gchar **display_infos, XfconfChannel *channel)
{
    GHashTable *properties;
    GList      *channel_contents;
    GList      *profiles = NULL;
    GList      *current;
    guint       m;
    guint       noutput;

    properties = xfconf_channel_get_properties (channel, NULL);
    channel_contents = g_hash_table_get_keys (properties);
    noutput = g_strv_length (display_infos);

    /* get all profiles */
    current = g_list_first (channel_contents);
    while (current)
    {
        GHashTable     *props;
        gchar          *property_profile;
        GHashTableIter  iter;
        gpointer        key, value;
        guint           profile_match = 0;
        guint           monitors = 0;
        gchar         **current_elements = g_strsplit (current->data, "/", -1);
        gchar          *profile_name;

        /* Only process the profiles and skip all other xfconf properties */
        /* If xfconf ever supports just getting the first-level children of a property
           we could replace this */
        if (get_size (current_elements) != 2)
        {
            g_strfreev (current_elements);
            current = g_list_next (current);
            continue;
        }

        profile_name = g_strdup_printf ("%s", *(current_elements + 1));
        g_strfreev (current_elements);

        /* Walk through the profile and check if every EDID referenced there is also currently available */
        property_profile = g_strdup_printf ("/%s", profile_name);
        props = xfconf_channel_get_properties (channel, property_profile);
        g_hash_table_iter_init (&iter, props);

        while (g_hash_table_iter_next (&iter, &key, &value))
        {
            gchar *property;
            gchar *current_edid;

            gchar ** property_elements = g_strsplit (key, "/", -1);
            if (get_size (property_elements) == 3) {
                monitors++;

                property = g_strdup_printf ("%s/EDID", (gchar*)key);
                current_edid = xfconf_channel_get_string (channel, property, NULL);

                if (current_edid) {

                    for (m = 0; m < noutput; ++m)
                    {
                        if (g_strcmp0 (display_infos[m], current_edid) == 0)
                        {
                            profile_match ++;
                        }
                    }
                }
                g_free (property);
                g_free (current_edid);
            }

            g_strfreev (property_elements);
        }
        g_free (property_profile);
        g_hash_table_destroy (props);

        /* filter the content of the combobox to only matching profiles and exclude "Notify", "Default" and "Schemes" */
        if (!g_list_find_custom (profiles, profile_name, (GCompareFunc) strcmp) &&
            strcmp (profile_name, "Notify") &&
            strcmp (profile_name, "Default") &&
            strcmp (profile_name, "Schemes") &&
            profile_match == monitors &&
            noutput == profile_match)
        {
            profiles = g_list_prepend (profiles, g_strdup (profile_name));
        }
        /* else don't add the profile to the list */
        current = g_list_next (current);
        g_free (profile_name);
    }

    g_list_free (channel_contents);
    g_hash_table_destroy (properties);

    return profiles;
}