~ctf/unity-settings-daemon/bug1389099_mic_volume_icons

« back to all changes in this revision

Viewing changes to plugins/color/gcm-dmi.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-02-07 11:44:36 UTC
  • Revision ID: package-import@ubuntu.com-20140207114436-7t5u3yvwc4ul7w3e
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2009-2011 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib-object.h>
 
25
#include <math.h>
 
26
#include <string.h>
 
27
#include <gio/gio.h>
 
28
#include <stdlib.h>
 
29
 
 
30
#include "gcm-dmi.h"
 
31
 
 
32
static void     gcm_dmi_finalize        (GObject     *object);
 
33
 
 
34
#define GCM_DMI_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_DMI, GcmDmiPrivate))
 
35
 
 
36
struct _GcmDmiPrivate
 
37
{
 
38
        gchar                           *name;
 
39
        gchar                           *version;
 
40
        gchar                           *vendor;
 
41
};
 
42
 
 
43
static gpointer gcm_dmi_object = NULL;
 
44
 
 
45
G_DEFINE_TYPE (GcmDmi, gcm_dmi, G_TYPE_OBJECT)
 
46
 
 
47
static gchar *
 
48
gcm_dmi_get_from_filename (const gchar *filename)
 
49
{
 
50
        gboolean ret;
 
51
        GError *error = NULL;
 
52
        gchar *data = NULL;
 
53
 
 
54
        /* get the contents */
 
55
        ret = g_file_get_contents (filename, &data, NULL, &error);
 
56
        if (!ret) {
 
57
                if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
 
58
                        g_warning ("failed to get contents of %s: %s", filename, error->message);
 
59
                g_error_free (error);
 
60
        }
 
61
 
 
62
        /* process the random chars and trailing spaces */
 
63
        if (data != NULL) {
 
64
                g_strdelimit (data, "\t_", ' ');
 
65
                g_strdelimit (data, "\n\r", '\0');
 
66
                g_strchomp (data);
 
67
        }
 
68
 
 
69
        /* don't return an empty string */
 
70
        if (data != NULL && data[0] == '\0') {
 
71
                g_free (data);
 
72
                data = NULL;
 
73
        }
 
74
 
 
75
        return data;
 
76
}
 
77
 
 
78
static gchar *
 
79
gcm_dmi_get_from_filenames (const gchar * const * filenames)
 
80
{
 
81
        guint i;
 
82
        gchar *tmp = NULL;
 
83
 
 
84
        /* try each one in preference order */
 
85
        for (i = 0; filenames[i] != NULL; i++) {
 
86
                tmp = gcm_dmi_get_from_filename (filenames[i]);
 
87
                if (tmp != NULL)
 
88
                        break;
 
89
        }
 
90
        return tmp;
 
91
}
 
92
 
 
93
const gchar *
 
94
gcm_dmi_get_name (GcmDmi *dmi)
 
95
{
 
96
        g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
 
97
        return dmi->priv->name;
 
98
}
 
99
 
 
100
const gchar *
 
101
gcm_dmi_get_version (GcmDmi *dmi)
 
102
{
 
103
        g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
 
104
        return dmi->priv->version;
 
105
}
 
106
 
 
107
const gchar *
 
108
gcm_dmi_get_vendor (GcmDmi *dmi)
 
109
{
 
110
        g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
 
111
        return dmi->priv->vendor;
 
112
}
 
113
 
 
114
static void
 
115
gcm_dmi_class_init (GcmDmiClass *klass)
 
116
{
 
117
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
118
        object_class->finalize = gcm_dmi_finalize;
 
119
        g_type_class_add_private (klass, sizeof (GcmDmiPrivate));
 
120
}
 
121
 
 
122
static void
 
123
gcm_dmi_init (GcmDmi *dmi)
 
124
{
 
125
#if defined(__linux__)
 
126
        const gchar *sysfs_name[] = {
 
127
                "/sys/class/dmi/id/product_name",
 
128
                "/sys/class/dmi/id/board_name",
 
129
                NULL};
 
130
        const gchar *sysfs_version[] = {
 
131
                "/sys/class/dmi/id/product_version",
 
132
                "/sys/class/dmi/id/chassis_version",
 
133
                "/sys/class/dmi/id/board_version",
 
134
                NULL};
 
135
        const gchar *sysfs_vendor[] = {
 
136
                "/sys/class/dmi/id/sys_vendor",
 
137
                "/sys/class/dmi/id/chassis_vendor",
 
138
                "/sys/class/dmi/id/board_vendor",
 
139
                NULL};
 
140
#else
 
141
#warning Please add dmi support for your OS
 
142
        const gchar *sysfs_name[] = { NULL };
 
143
        const gchar *sysfs_version[] = { NULL };
 
144
        const gchar *sysfs_vendor[] = { NULL };
 
145
#endif
 
146
 
 
147
        dmi->priv = GCM_DMI_GET_PRIVATE (dmi);
 
148
 
 
149
        /* get all the possible data now */
 
150
        dmi->priv->name = gcm_dmi_get_from_filenames (sysfs_name);
 
151
        dmi->priv->version = gcm_dmi_get_from_filenames (sysfs_version);
 
152
        dmi->priv->vendor = gcm_dmi_get_from_filenames (sysfs_vendor);
 
153
}
 
154
 
 
155
static void
 
156
gcm_dmi_finalize (GObject *object)
 
157
{
 
158
        GcmDmi *dmi = GCM_DMI (object);
 
159
 
 
160
        g_free (dmi->priv->name);
 
161
        g_free (dmi->priv->version);
 
162
        g_free (dmi->priv->vendor);
 
163
 
 
164
        G_OBJECT_CLASS (gcm_dmi_parent_class)->finalize (object);
 
165
}
 
166
 
 
167
GcmDmi *
 
168
gcm_dmi_new (void)
 
169
{
 
170
        if (gcm_dmi_object != NULL) {
 
171
                g_object_ref (gcm_dmi_object);
 
172
        } else {
 
173
                gcm_dmi_object = g_object_new (GCM_TYPE_DMI, NULL);
 
174
                g_object_add_weak_pointer (gcm_dmi_object, &gcm_dmi_object);
 
175
        }
 
176
        return GCM_DMI (gcm_dmi_object);
 
177
}