~ubuntu-branches/ubuntu/precise/xfce4-power-manager/precise

« back to all changes in this revision

Viewing changes to libxfpm/hal-battery.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2009-10-02 20:15:41 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091002201541-ognqyxyov51epj84
Tags: 0.8.4-1ubuntu1
* New upstream bugfix release (LP: #438754), merge from Debian unstable,
  remaining Ubuntu changes:
  - debian/patches:
    + xubuntu-use-notification-icons.patch: use our notification-* icons for
      notification bubbles instead of the normal ones LP: #437374
    + 10_pc.patch: added patched files to POTFILES.skip
    + series: created, with these two patches
  - debian/rules: add --with quilt
  - debian/control: build-depends on quilt (>= 0.46-7).

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
#define HAL_BATTERY_GET_PRIVATE(o) \
44
44
(G_TYPE_INSTANCE_GET_PRIVATE((o), HAL_TYPE_BATTERY, HalBatteryPrivate))
45
45
 
 
46
#define FREE_STR_PROP(str)                          \
 
47
    if ( str )                                      \
 
48
        g_free (str);                               \
 
49
    str = NULL;
 
50
 
46
51
struct HalBatteryPrivate
47
52
{
48
53
    /* Properties read-only */
52
57
    gboolean  is_charging;
53
58
    gboolean  is_discharging;
54
59
    
55
 
    gchar    *unit;
56
 
    gchar    *technology;
57
 
    gchar    *vendor;
58
 
    gchar    *model;
59
60
    guint     percentage;
60
61
 
61
62
    guint32   current_charge;
97
98
 
98
99
G_DEFINE_TYPE(HalBattery, hal_battery, HAL_TYPE_DEVICE)
99
100
 
 
101
static gchar *
 
102
hal_battery_get_info_string (HalBattery *battery, const gchar *key)
 
103
{
 
104
    gchar *val = NULL;
 
105
    
 
106
    if ( hal_device_has_key (HAL_DEVICE (battery), key) )
 
107
    {
 
108
        val = hal_device_get_property_string (HAL_DEVICE(battery), key);
 
109
    }
 
110
    
 
111
    return val;
 
112
}
 
113
 
100
114
static void
101
115
hal_battery_class_init(HalBatteryClass *klass)
102
116
{
231
245
    battery->priv->is_charging     = FALSE;
232
246
    battery->priv->is_discharging  = FALSE;
233
247
 
234
 
    battery->priv->unit            = NULL;
235
 
    battery->priv->vendor          = NULL;
236
 
    battery->priv->technology      = NULL;
237
 
    battery->priv->model           = NULL;
238
248
    battery->priv->type            = HAL_DEVICE_TYPE_UNKNOWN;
239
249
    
240
250
    battery->priv->percentage      = 0;
245
255
    battery->priv->reporting_last_full = 0;
246
256
}
247
257
 
 
258
static const gchar * G_GNUC_PURE
 
259
_translate_technology (const gchar *tech)
 
260
{
 
261
    if ( !g_strcmp0 (tech, "lithium-ion") )
 
262
    {
 
263
        return _("Lithium ion");
 
264
    }
 
265
    else if ( !g_strcmp0 (tech, "lead-acid") )
 
266
    {
 
267
        return _("Lead acid");
 
268
    }
 
269
    else if ( !g_strcmp0 (tech, "lithium-polymer") )
 
270
    {
 
271
        return _("Lithium polymer");
 
272
    }
 
273
    else if ( !g_strcmp0 (tech, "nickel-metal-hydride") )
 
274
    {
 
275
        return _("Nickel metal hydride");
 
276
    }
 
277
    
 
278
    return _("Unknown");
 
279
}
 
280
 
 
281
static const gchar * G_GNUC_PURE
 
282
_translate_unit (const gchar *unit)
 
283
{
 
284
    if ( !g_strcmp0 (unit, "mWh") )
 
285
    {
 
286
        return _("mWh");
 
287
    }
 
288
    else if ( !g_strcmp0 (unit, "mAh") )
 
289
    {
 
290
        return _("mAh");
 
291
    }
 
292
    
 
293
    return _("Unknown unit");
 
294
}
 
295
 
248
296
static void hal_battery_get_property(GObject *object,
249
297
                                    guint prop_id,
250
298
                                    GValue *value,
267
315
        case PROP_IS_DISCHARGING:
268
316
                g_value_set_boolean (value, battery->priv->is_discharging);
269
317
                break;
 
318
                
270
319
        case PROP_UNIT:
271
 
                g_value_set_string (value, battery->priv->unit);
 
320
        {
 
321
                gchar *unit = NULL;
 
322
                gchar *val;
 
323
                val = hal_battery_get_info_string (battery, "battery.reporting.unit");
 
324
                if ( val )
 
325
                {
 
326
                    unit = g_strdup(_translate_unit (val));
 
327
                    g_free (val);
 
328
                }
 
329
                g_value_set_string (value, unit);
272
330
                break;
 
331
        }
273
332
        case PROP_TECHNOLOGY:
274
 
                g_value_set_string (value, battery->priv->technology);
 
333
        {
 
334
                gchar *val;
 
335
                gchar *technology = NULL;
 
336
                val = hal_battery_get_info_string (battery, "battery.technology");
 
337
                if ( val )
 
338
                {
 
339
                    technology = g_strdup (_translate_technology (val));
 
340
                    g_free (val);
 
341
                }
 
342
                
 
343
                g_value_set_string (value, technology);
 
344
                g_free (technology);
275
345
                break;
 
346
        }
276
347
        case PROP_VENDOR:
277
 
                g_value_set_string (value, battery->priv->vendor);
 
348
        {
 
349
                gchar *vendor = NULL;
 
350
                vendor = hal_battery_get_info_string (battery, "battery.vendor");
 
351
                g_value_set_string (value, vendor);
 
352
                g_free (vendor);
278
353
                break;
 
354
        }
279
355
        case PROP_MODEL:
280
 
                g_value_set_string (value, battery->priv->model);
 
356
        {
 
357
                gchar *model = NULL;
 
358
                model = hal_battery_get_info_string (battery, "battery.model");
 
359
                g_value_set_string (value, model);
 
360
                g_free (model);
281
361
                break;
 
362
        }
282
363
        case PROP_PERCENTAGE:
283
364
                g_value_set_uint (value, battery->priv->percentage);
284
365
                break;
310
391
 
311
392
    battery = HAL_BATTERY(object);
312
393
    
313
 
    if ( battery->priv->technology )
314
 
        g_free (battery->priv->technology);
315
 
        
316
 
    if ( battery->priv->vendor )
317
 
        g_free (battery->priv->vendor);
318
 
        
319
 
    if ( battery->priv->model )
320
 
        g_free (battery->priv->model);
321
 
        
322
 
    if ( battery->priv->unit )
323
 
        g_free (battery->priv->unit);
324
 
 
325
394
    G_OBJECT_CLASS(hal_battery_parent_class)->finalize(object);
326
395
}
327
396
 
426
495
    else
427
496
        battery->priv->time = 0;
428
497
    
429
 
    //FIXME: calculate the percentage if it is not found on HAL
430
498
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.charge_level.percentage") )
431
499
        battery->priv->percentage = 
432
500
                hal_device_get_property_int(HAL_DEVICE(battery), "battery.charge_level.percentage");
435
503
    if ( hal_device_has_key(HAL_DEVICE(battery), "battery.reporting.last_full") )
436
504
        battery->priv->reporting_last_full = 
437
505
                hal_device_get_property_int(HAL_DEVICE(battery), "battery.reporting.last_full");
438
 
                
439
 
}
440
 
 
441
 
static const gchar * G_GNUC_PURE
442
 
_translate_technology (const gchar *tech)
443
 
{
444
 
    if ( !g_strcmp0 (tech, "lithium-ion") )
445
 
    {
446
 
        return _("Lithium ion");
447
 
    }
448
 
    else if ( !g_strcmp0 (tech, "lead-acid") )
449
 
    {
450
 
        return _("Lead acid");
451
 
    }
452
 
    else if ( !g_strcmp0 (tech, "lithium-polymer") )
453
 
    {
454
 
        return _("Lithium polymer");
455
 
    }
456
 
    else if ( !g_strcmp0 (tech, "nickel-metal-hydride") )
457
 
    {
458
 
        return _("Nickel metal hydride");
459
 
    }
460
 
    
461
 
    return _("Unknown");
462
 
}
463
 
 
464
 
static const gchar * G_GNUC_PURE
465
 
_translate_unit (const gchar *unit)
466
 
{
467
 
    if ( !g_strcmp0 (unit, "mWh") )
468
 
    {
469
 
        return _("mWh");
470
 
    }
471
 
    else if ( !g_strcmp0 (unit, "mAh") )
472
 
    {
473
 
        return _("mAh");
474
 
    }
475
 
    
476
 
    return _("Unknown unit");
477
 
}
478
 
 
479
 
static void
480
 
hal_battery_get_battery_info (HalBattery *battery)
481
 
{
482
 
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.technology") )
483
 
    {
484
 
        gchar *tech = hal_device_get_property_string (HAL_DEVICE(battery), "battery.technology");
485
 
        if ( tech )
486
 
        {
487
 
            battery->priv->technology = g_strdup (_translate_technology (tech));
488
 
            g_free (tech);
489
 
        }
490
 
    }
491
 
    
492
 
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.vendor") )
493
 
    {
494
 
        gchar *vendor = hal_device_get_property_string (HAL_DEVICE(battery), "battery.vendor");
495
 
        if ( vendor )
496
 
        {
497
 
            battery->priv->vendor = g_strdup ( vendor);
498
 
            g_free (vendor);
499
 
        }
500
 
    }
501
 
    
502
 
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.model") )
503
 
    {
504
 
        gchar *model = hal_device_get_property_string (HAL_DEVICE(battery), "battery.model");
505
 
        if ( model )
506
 
        {
507
 
            battery->priv->model = g_strdup (model);
508
 
            g_free (model);
509
 
        }
510
 
    }
511
 
    
 
506
 
512
507
    battery->priv->reporting_design = hal_device_get_property_int (HAL_DEVICE(battery), 
513
 
                                                                           "battery.reporting.design");
514
 
                                                              
515
 
    if ( hal_device_has_key (HAL_DEVICE(battery), "battery.reporting.unit") )
516
 
    {
517
 
        gchar *unit = hal_device_get_property_string (HAL_DEVICE(battery), "battery.reporting.unit");
518
 
        
519
 
        if ( unit )
520
 
        {
521
 
            battery->priv->unit = g_strdup(_translate_unit(unit));
522
 
            g_free(unit);           
523
 
        }
524
 
    }
 
508
                                                                   "battery.reporting.design");
525
509
}
526
510
 
527
511
static void
537
521
        hal_battery_refresh_all (battery);
538
522
        g_signal_emit (G_OBJECT (battery), signals[BATTERY_CHANGED], 0);
539
523
    }
540
 
    
541
524
}
542
525
 
543
 
 
544
526
static void
545
527
hal_battery_property_modified_cb(HalBattery *battery, 
546
528
                                 const gchar *udi,
563
545
    battery->priv->type = hal_battery_get_device_type (battery);
564
546
   
565
547
    hal_battery_refresh_all (battery);
566
 
    hal_battery_get_battery_info (battery);
567
548
        
568
549
    hal_device_watch (HAL_DEVICE(battery));
569
550