tracker-0.16.2/src/miners/fs/tracker-power-upower.c

No issues found

  1 /*
  2  * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
  3  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  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
 17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18  * Boston, MA  02110-1301, USA.
 19  */
 20 
 21 #include "config.h"
 22 
 23 #ifdef HAVE_UPOWER
 24 
 25 #include <upower.h>
 26 
 27 #include "tracker-power.h"
 28 
 29 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_POWER, TrackerPowerPriv))
 30 
 31 typedef struct {
 32 	UpClient  *client;
 33 	gboolean   on_battery;
 34 	gboolean   on_low_battery;
 35 } TrackerPowerPriv;
 36 
 37 static void     tracker_power_finalize          (GObject         *object);
 38 static void     tracker_power_get_property      (GObject         *object,
 39                                                  guint            param_id,
 40                                                  GValue                  *value,
 41                                                  GParamSpec      *pspec);
 42 static void     tracker_power_client_changed_cb (UpClient        *client,
 43                                                  TrackerPower    *power);
 44 
 45 enum {
 46 	PROP_0,
 47 	PROP_ON_BATTERY,
 48 	PROP_ON_LOW_BATTERY
 49 };
 50 
 51 G_DEFINE_TYPE (TrackerPower, tracker_power, G_TYPE_OBJECT);
 52 
 53 static void
 54 tracker_power_class_init (TrackerPowerClass *klass)
 55 {
 56 	GObjectClass *object_class;
 57 
 58 	object_class = G_OBJECT_CLASS (klass);
 59 
 60 	object_class->finalize     = tracker_power_finalize;
 61 	object_class->get_property = tracker_power_get_property;
 62 
 63 	g_object_class_install_property (object_class,
 64 	                                 PROP_ON_BATTERY,
 65 	                                 g_param_spec_boolean ("on-battery",
 66 	                                                       "Battery in use",
 67 	                                                       "Whether the battery is being used",
 68 	                                                       FALSE,
 69 	                                                       G_PARAM_READABLE));
 70 
 71 	g_object_class_install_property (object_class,
 72 	                                 PROP_ON_LOW_BATTERY,
 73 	                                 g_param_spec_boolean ("on-low-battery",
 74 	                                                       "Battery low",
 75 	                                                       "Whether the battery is low",
 76 	                                                       FALSE,
 77 	                                                       G_PARAM_READABLE));
 78 
 79 	g_type_class_add_private (object_class, sizeof (TrackerPowerPriv));
 80 }
 81 
 82 static void
 83 tracker_power_init (TrackerPower *power)
 84 {
 85 	TrackerPowerPriv *priv;
 86 
 87 	g_message ("Initializing UPower...");
 88 
 89 	priv = GET_PRIV (power);
 90 
 91 	/* connect to a UPower instance */
 92 	priv->client = up_client_new ();
 93 	g_signal_connect (priv->client, "changed",
 94 	                  G_CALLBACK (tracker_power_client_changed_cb), power);
 95 
 96 	/* coldplug */
 97 	priv->on_battery = up_client_get_on_battery (priv->client);
 98 	priv->on_low_battery = up_client_get_on_low_battery (priv->client);
 99 }
100 
101 static void
102 tracker_power_finalize (GObject *object)
103 {
104 	TrackerPowerPriv *priv;
105 
106 	priv = GET_PRIV (object);
107 
108 	g_object_unref (priv->client);
109 
110 	(G_OBJECT_CLASS (tracker_power_parent_class)->finalize) (object);
111 }
112 
113 static void
114 tracker_power_get_property (GObject    *object,
115                             guint       param_id,
116                             GValue     *value,
117                             GParamSpec *pspec)
118 {
119 	TrackerPowerPriv *priv;
120 
121 	priv = GET_PRIV (object);
122 
123 	switch (param_id) {
124 	case PROP_ON_BATTERY:
125 		g_value_set_boolean (value, priv->on_battery);
126 		break;
127 	case PROP_ON_LOW_BATTERY:
128 		g_value_set_boolean (value, priv->on_low_battery);
129 		break;
130 	default:
131 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
132 		break;
133 	};
134 }
135 
136 /**
137  * tracker_power_client_changed_cb:
138  **/
139 static void
140 tracker_power_client_changed_cb (UpClient *client, TrackerPower *power)
141 {
142 	gboolean on_battery;
143 	gboolean on_low_battery;
144 	TrackerPowerPriv *priv;
145 
146 	priv = GET_PRIV (power);
147 
148 	/* get the on-battery state */
149 	on_battery = up_client_get_on_battery (priv->client);
150 	if (on_battery != priv->on_battery) {
151 		priv->on_battery = on_battery;
152 		g_object_notify (G_OBJECT (power), "on-battery");
153 	}
154 
155 	/* get the on-low-battery state */
156 	on_low_battery = up_client_get_on_low_battery (priv->client);
157 	if (on_low_battery != priv->on_low_battery) {
158 		priv->on_low_battery = on_low_battery;
159 		g_object_notify (G_OBJECT (power), "on-low-battery");
160 	}
161 }
162 
163 /**
164  * tracker_power_new:
165  *
166  * Creates a new instance of #TrackerPower.
167  *
168  * Returns: The newly created #TrackerPower.
169  **/
170 TrackerPower *
171 tracker_power_new ()
172 {
173 	return g_object_new (TRACKER_TYPE_POWER, NULL);
174 }
175 
176 /**
177  * tracker_power_get_on_battery:
178  * @power: A #TrackerPower.
179  *
180  * Returns whether the computer battery (if any) is currently in use.
181  *
182  * Returns: #TRUE if the computer is running on battery power.
183  **/
184 gboolean
185 tracker_power_get_on_battery (TrackerPower *power)
186 {
187 	TrackerPowerPriv *priv;
188 
189 	g_return_val_if_fail (TRACKER_IS_POWER (power), TRUE);
190 
191 	priv = GET_PRIV (power);
192 
193 	return priv->on_battery;
194 }
195 
196 /**
197  * tracker_power_get_on_low_battery:
198  * @power: A #TrackerPower
199  *
200  * Returns whether the computer has batteries.
201  *
202  * Returns: #TRUE if the computer has batteries available.
203  **/
204 gboolean
205 tracker_power_get_on_low_battery (TrackerPower *power)
206 {
207 	TrackerPowerPriv *priv;
208 
209 	g_return_val_if_fail (TRACKER_IS_POWER (power), TRUE);
210 
211 	priv = GET_PRIV (power);
212 
213 	return priv->on_low_battery;
214 }
215 
216 #endif /* HAVE_UPOWER */