~seb128/indicator-power/use-universe-translations

« back to all changes in this revision

Viewing changes to src/notifier.c

  • Committer: CI Train Bot
  • Author(s): Charles Kerr, charles kerr
  • Date: 2016-01-05 14:37:48 UTC
  • mfrom: (290.1.24 lp-1470767-low-battery-sound)
  • Revision ID: ci-train-bot@canonical.com-20160105143748-8vkmb6yhgz603fus
Play a 'low battery' sound when the low battery notification is shown.
 Fixes: #1470767
Approved by: Ted Gould, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright 2014 Canonical Ltd.
 
2
 * Copyright 2014-2016 Canonical Ltd.
3
3
 *
4
4
 * This program is free software: you can redistribute it and/or modify it
5
5
 * under the terms of the GNU General Public License version 3, as published
17
17
 *   Charles Kerr <charles.kerr@canonical.com>
18
18
 */
19
19
 
 
20
#include "datafiles.h"
 
21
#include "dbus-accounts-sound.h"
20
22
#include "dbus-battery.h"
21
23
#include "dbus-shared.h"
22
24
#include "notifier.h"
 
25
#include "sound-player.h"
23
26
 
24
27
#include <url-dispatcher.h>
25
28
 
46
49
{
47
50
  PROP_0,
48
51
  PROP_BATTERY,
 
52
  PROP_SOUND_PLAYER,
49
53
  LAST_PROP
50
54
};
51
55
 
52
56
#define PROP_BATTERY_NAME "battery"
 
57
#define PROP_SOUND_PLAYER_NAME "sound-player"
53
58
 
54
59
static GParamSpec * properties[LAST_PROP];
55
60
 
77
82
 
78
83
  gboolean caps_queried;
79
84
  gboolean actions_supported;
 
85
 
 
86
  IndicatorPowerSoundPlayer * sound_player;
 
87
 
 
88
  GCancellable * cancellable;
 
89
  DbusAccountsServiceSound * accounts_service_sound_proxy;
 
90
  gboolean accounts_service_sound_proxy_pending;
80
91
}
81
92
IndicatorPowerNotifierPrivate;
82
93
 
131
142
}
132
143
 
133
144
/***
 
145
****  Sounds
 
146
***/
 
147
 
 
148
static void
 
149
on_sound_proxy_ready (GObject      * source_object G_GNUC_UNUSED,
 
150
                      GAsyncResult * res,
 
151
                      gpointer       gself)
 
152
{
 
153
  GError * error;
 
154
  DbusAccountsServiceSound * proxy;
 
155
 
 
156
  error = NULL;
 
157
  proxy = dbus_accounts_service_sound_proxy_new_for_bus_finish (res, &error);
 
158
  if (error != NULL)
 
159
    {
 
160
      if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
 
161
        {
 
162
          get_priv(gself)->accounts_service_sound_proxy_pending = FALSE;
 
163
          g_debug("%s Couldn't find accounts service sound proxy: %s", G_STRLOC, error->message);
 
164
        }
 
165
 
 
166
      g_clear_error(&error);
 
167
    }
 
168
  else
 
169
    {
 
170
      IndicatorPowerNotifier * const self = INDICATOR_POWER_NOTIFIER(gself);
 
171
      priv_t * const p = get_priv (self);
 
172
      g_clear_object (&p->accounts_service_sound_proxy);
 
173
      p->accounts_service_sound_proxy = proxy;
 
174
      p->accounts_service_sound_proxy_pending = FALSE;
 
175
    }
 
176
}
 
177
 
 
178
static gboolean
 
179
silent_mode (IndicatorPowerNotifier * self)
 
180
{
 
181
  priv_t * const p = get_priv (self);
 
182
 
 
183
  /* if we don't have a proxy yet, assume we're in silent mode
 
184
     as a "do no harm" level of response */
 
185
  if (p->accounts_service_sound_proxy_pending)
 
186
    return TRUE;
 
187
 
 
188
  return (p->accounts_service_sound_proxy != NULL)
 
189
      && dbus_accounts_service_sound_get_silent_mode(p->accounts_service_sound_proxy);
 
190
}
 
191
 
 
192
static void
 
193
play_low_battery_sound (IndicatorPowerNotifier * self)
 
194
{
 
195
  const gchar * const key = LOW_BATTERY_SOUND;
 
196
  gchar * filename;
 
197
  priv_t * const p = get_priv(self);
 
198
 
 
199
  /* can't play? */
 
200
  g_return_if_fail (p->sound_player != NULL);
 
201
 
 
202
  /* won't play? */
 
203
  if (silent_mode(self))
 
204
    return;
 
205
 
 
206
  filename = datafile_find(DATAFILE_TYPE_SOUND, key);
 
207
  if (filename != NULL)
 
208
    {
 
209
      gchar * uri = g_filename_to_uri(filename, NULL, NULL);
 
210
      indicator_power_sound_player_play_uri (p->sound_player, uri);
 
211
      g_free(uri);
 
212
      g_free(filename);
 
213
    }
 
214
  else
 
215
    {
 
216
      g_warning("Unable to find '%s' in XDG data dirs", key);
 
217
    }
 
218
}
 
219
 
 
220
/***
134
221
****  Notifications
135
222
***/
136
223
 
300
387
      ((new_power_level != POWER_LEVEL_OK) && new_discharging && !old_discharging))
301
388
    {
302
389
      notification_show (self);
 
390
      play_low_battery_sound (self);
303
391
    }
304
392
  else if (!new_discharging || (new_power_level == POWER_LEVEL_OK))
305
393
    {
330
418
        g_value_set_object (value, p->battery);
331
419
        break;
332
420
 
 
421
      case PROP_SOUND_PLAYER:
 
422
        g_value_set_object (value, p->sound_player);
 
423
        break;
 
424
 
333
425
      default:
334
426
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
335
427
    }
349
441
        indicator_power_notifier_set_battery (self, g_value_get_object(value));
350
442
        break;
351
443
 
 
444
      case PROP_SOUND_PLAYER:
 
445
        indicator_power_notifier_set_sound_player (self, g_value_get_object(value));
 
446
        break;
 
447
 
352
448
      default:
353
449
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
354
450
    }
360
456
  IndicatorPowerNotifier * const self = INDICATOR_POWER_NOTIFIER(o);
361
457
  priv_t * const p = get_priv (self);
362
458
 
 
459
  if (p->cancellable != NULL)
 
460
    {
 
461
      g_cancellable_cancel(p->cancellable);
 
462
      g_clear_object(&p->cancellable);
 
463
    }
 
464
 
363
465
  indicator_power_notifier_set_bus (self, NULL);
 
466
  indicator_power_notifier_set_sound_player (self, NULL);
364
467
  notification_clear (self);
365
468
  indicator_power_notifier_set_battery (self, NULL);
366
469
  g_clear_object (&p->dbus_battery);
 
470
  g_clear_object (&p->accounts_service_sound_proxy);
367
471
 
368
472
  G_OBJECT_CLASS (indicator_power_notifier_parent_class)->dispose (o);
369
473
}
382
486
****  Instantiation
383
487
***/
384
488
 
385
 
 
386
489
static void
387
490
indicator_power_notifier_init (IndicatorPowerNotifier * self)
388
491
{
394
497
 
395
498
  p->power_level = POWER_LEVEL_OK;
396
499
 
 
500
  p->cancellable = g_cancellable_new();
 
501
 
397
502
  if (!instance_count++ && !notify_init("indicator-power-service"))
398
503
    g_critical("Unable to initialize libnotify! Notifications might not be shown.");
 
504
 
 
505
  p->accounts_service_sound_proxy_pending = TRUE;
 
506
  gchar* object_path = g_strdup_printf("/org/freedesktop/Accounts/User%lu", (gulong)getuid());
 
507
  dbus_accounts_service_sound_proxy_new_for_bus(
 
508
    G_BUS_TYPE_SYSTEM,
 
509
    G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
510
    "org.freedesktop.Accounts",
 
511
    object_path,
 
512
    p->cancellable,
 
513
    on_sound_proxy_ready,
 
514
    self);
 
515
  g_clear_pointer(&object_path, g_free);
399
516
}
400
517
 
401
518
static void
415
532
    G_TYPE_OBJECT,
416
533
    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
417
534
 
 
535
  properties[PROP_SOUND_PLAYER] = g_param_spec_object (
 
536
    PROP_SOUND_PLAYER_NAME,
 
537
    "Sound Player",
 
538
    "The current sound player",
 
539
    G_TYPE_OBJECT,
 
540
    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
541
 
418
542
  g_object_class_install_properties (object_class, LAST_PROP, properties);
419
543
}
420
544
 
423
547
***/
424
548
 
425
549
IndicatorPowerNotifier *
426
 
indicator_power_notifier_new (void)
 
550
indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player)
427
551
{
428
 
  GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, NULL);
 
552
  GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER,
 
553
                              PROP_SOUND_PLAYER_NAME, sound_player,
 
554
                              NULL);
429
555
 
430
556
  return INDICATOR_POWER_NOTIFIER (o);
431
557
}
465
591
}
466
592
 
467
593
void
 
594
indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self,
 
595
                                           IndicatorPowerSoundPlayer * sound_player)
 
596
{
 
597
  priv_t * p;
 
598
 
 
599
  g_return_if_fail(INDICATOR_IS_POWER_NOTIFIER(self));
 
600
  g_return_if_fail((sound_player == NULL) || INDICATOR_IS_POWER_SOUND_PLAYER(sound_player));
 
601
 
 
602
  p = get_priv (self);
 
603
 
 
604
  if (p->sound_player == sound_player)
 
605
    return;
 
606
 
 
607
  g_clear_object(&p->sound_player);
 
608
 
 
609
  if (sound_player != NULL)
 
610
      p->sound_player = g_object_ref(sound_player);
 
611
}
 
612
 
 
613
void
468
614
indicator_power_notifier_set_bus (IndicatorPowerNotifier * self,
469
615
                                  GDBusConnection        * bus)
470
616
{