1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2009-2011 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
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.
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.
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.
24
#include <glib-object.h>
32
static void gcm_dmi_finalize (GObject *object);
34
#define GCM_DMI_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_DMI, GcmDmiPrivate))
43
static gpointer gcm_dmi_object = NULL;
45
G_DEFINE_TYPE (GcmDmi, gcm_dmi, G_TYPE_OBJECT)
48
gcm_dmi_get_from_filename (const gchar *filename)
54
/* get the contents */
55
ret = g_file_get_contents (filename, &data, NULL, &error);
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);
62
/* process the random chars and trailing spaces */
64
g_strdelimit (data, "\t_", ' ');
65
g_strdelimit (data, "\n\r", '\0');
69
/* don't return an empty string */
70
if (data != NULL && data[0] == '\0') {
79
gcm_dmi_get_from_filenames (const gchar * const * filenames)
84
/* try each one in preference order */
85
for (i = 0; filenames[i] != NULL; i++) {
86
tmp = gcm_dmi_get_from_filename (filenames[i]);
94
gcm_dmi_get_name (GcmDmi *dmi)
96
g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
97
return dmi->priv->name;
101
gcm_dmi_get_version (GcmDmi *dmi)
103
g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
104
return dmi->priv->version;
108
gcm_dmi_get_vendor (GcmDmi *dmi)
110
g_return_val_if_fail (GCM_IS_DMI (dmi), NULL);
111
return dmi->priv->vendor;
115
gcm_dmi_class_init (GcmDmiClass *klass)
117
GObjectClass *object_class = G_OBJECT_CLASS (klass);
118
object_class->finalize = gcm_dmi_finalize;
119
g_type_class_add_private (klass, sizeof (GcmDmiPrivate));
123
gcm_dmi_init (GcmDmi *dmi)
125
#if defined(__linux__)
126
const gchar *sysfs_name[] = {
127
"/sys/class/dmi/id/product_name",
128
"/sys/class/dmi/id/board_name",
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",
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",
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 };
147
dmi->priv = GCM_DMI_GET_PRIVATE (dmi);
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);
156
gcm_dmi_finalize (GObject *object)
158
GcmDmi *dmi = GCM_DMI (object);
160
g_free (dmi->priv->name);
161
g_free (dmi->priv->version);
162
g_free (dmi->priv->vendor);
164
G_OBJECT_CLASS (gcm_dmi_parent_class)->finalize (object);
170
if (gcm_dmi_object != NULL) {
171
g_object_ref (gcm_dmi_object);
173
gcm_dmi_object = g_object_new (GCM_TYPE_DMI, NULL);
174
g_object_add_weak_pointer (gcm_dmi_object, &gcm_dmi_object);
176
return GCM_DMI (gcm_dmi_object);