summaryrefslogtreecommitdiff
path: root/ohmd/ohm-manager.c
blob: 79d7fdadd2bb1c07ae0cf9b32fef3f99d858d1d6 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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 Lesser General Public
 * License along with this library; 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

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>

#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "ohm-debug.h"
#include "ohm-common.h"
#include "ohm-manager.h"
#include "ohm-conf.h"
#include "ohm-keystore.h"
#include "ohm-module.h"
#include "ohm-dbus-keystore.h"

static void     ohm_manager_class_init	(OhmManagerClass *klass);
static void     ohm_manager_init	(OhmManager      *manager);
static void     ohm_manager_finalize	(GObject	 *object);

#define OHM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OHM_TYPE_MANAGER, OhmManagerPrivate))

struct OhmManagerPrivate
{
	OhmConf			*conf;
	OhmModule		*module;
	OhmKeystore		*keystore;
};

enum {
	ON_AC_CHANGED,
	LAST_SIGNAL
};

static guint	     signals [LAST_SIGNAL] = { 0, };

G_DEFINE_TYPE (OhmManager, ohm_manager, G_TYPE_OBJECT)

/**
 * ohm_manager_error_quark:
 * Return value: Our personal error quark.
 **/
GQuark
ohm_manager_error_quark (void)
{
	static GQuark quark = 0;
	if (!quark) {
		quark = g_quark_from_static_string ("ohm_manager_error");
	}
	return quark;
}

/**
 * ohm_manager_get_on_ac:
 * @manager: This class instance
 * @retval: TRUE if we are on AC power
 **/
gboolean
ohm_manager_get_on_ac (OhmManager  *manager,
		       gboolean   *retval,
		       GError    **error)
{
	g_return_val_if_fail (OHM_IS_MANAGER (manager), FALSE);

	if (retval == NULL) {
		return FALSE;
	}

	*retval = TRUE;

	return TRUE;
}

/**
 * ohm_manager_get_low_power_mode:
 * @manager: This class instance
 * @retval: TRUE if we are on low power mode
 **/
gboolean
ohm_manager_get_low_power_mode (OhmManager  *manager,
				gboolean    *retval,
				GError     **error)
{
	g_return_val_if_fail (OHM_IS_MANAGER (manager), FALSE);

	if (retval == NULL) {
		return FALSE;
	}

	*retval = TRUE;

	return TRUE;
}

/**
 * ohm_manager_class_init:
 * @klass: The OhmManagerClass
 **/
static void
ohm_manager_class_init (OhmManagerClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize	   = ohm_manager_finalize;

	signals [ON_AC_CHANGED] =
		g_signal_new ("on-ac-changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (OhmManagerClass, on_ac_changed),
			      NULL,
			      NULL,
			      g_cclosure_marshal_VOID__BOOLEAN,
			      G_TYPE_NONE,
			      1, G_TYPE_BOOLEAN);

	g_type_class_add_private (klass, sizeof (OhmManagerPrivate));
}

/**
 * ohm_manager_init:
 * @manager: This class instance
 **/
static void
ohm_manager_init (OhmManager *manager)
{
	GError *error = NULL;
	DBusGConnection *connection;
	manager->priv = OHM_MANAGER_GET_PRIVATE (manager);

	/* get system bus connection */
	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	/* FIXME: check error */

	manager->priv->conf = ohm_conf_new ();
	manager->priv->module = ohm_module_new ();

	/* add the keystore and the DBUS interface */
	manager->priv->keystore = ohm_keystore_new ();
	dbus_g_object_type_install_info (OHM_TYPE_KEYSTORE, &dbus_glib_ohm_keystore_object_info);
	dbus_g_connection_register_g_object (connection, OHM_DBUS_PATH_KEYSTORE, G_OBJECT (manager->priv->keystore));

	/* set some predefined keys */
	ohm_conf_set_key_internal (manager->priv->conf, "manager.version.major", 0, TRUE, NULL);
	ohm_conf_set_key_internal (manager->priv->conf, "manager.version.minor", 0, TRUE, NULL);
	ohm_conf_set_key_internal (manager->priv->conf, "manager.version.patch", 1, TRUE, NULL);

	ohm_conf_print_all (manager->priv->conf);
}

/**
 * ohm_manager_finalize:
 * @object: The object to finalize
 *
 * Finalise the manager, by unref'ing all the depending modules.
 **/
static void
ohm_manager_finalize (GObject *object)
{
	OhmManager *manager;

	g_return_if_fail (object != NULL);
	g_return_if_fail (OHM_IS_MANAGER (object));
	manager = OHM_MANAGER (object);
	g_return_if_fail (manager->priv != NULL);

	g_object_unref (manager->priv->module);
	g_object_unref (manager->priv->keystore);
	g_object_unref (manager->priv->conf);

	G_OBJECT_CLASS (ohm_manager_parent_class)->finalize (object);
}

/**
 * ohm_manager_new:
 *
 * Return value: a new OhmManager object.
 **/
OhmManager *
ohm_manager_new (void)
{
	OhmManager *manager;

	manager = g_object_new (OHM_TYPE_MANAGER, NULL);

	return OHM_MANAGER (manager);
}