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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
* Copyright 2014 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#include <datetime/appointment.h>
#include <datetime/formatter.h>
#include <datetime/snap.h>
#include <core/signal.h>
#include <canberra.h>
#include <libnotify/notify.h>
#include <glib/gi18n.h>
#include <glib.h>
#include <set>
#include <string>
namespace unity {
namespace indicator {
namespace datetime {
/***
****
***/
namespace
{
/**
* Plays a sound, possibly looping.
*/
class Sound
{
typedef Sound Self;
public:
Sound(const std::shared_ptr<Clock>& clock,
const std::string& filename,
AlarmVolume volume,
int duration_minutes,
bool loop):
m_clock(clock),
m_filename(filename),
m_volume(volume),
m_loop(loop),
m_canberra_id(get_next_canberra_id()),
m_loop_end_time(clock->localtime().add_full(0, 0, 0, 0, duration_minutes, 0.0))
{
if (m_loop)
{
g_debug("Looping '%s' until cutoff time %s",
m_filename.c_str(),
m_loop_end_time.format("%F %T").c_str());
}
else
{
g_debug("Playing '%s' once", m_filename.c_str());
}
const auto rv = ca_context_create(&m_context);
if (rv == CA_SUCCESS)
{
play();
}
else
{
g_warning("Failed to create canberra context: %s", ca_strerror(rv));
m_context = nullptr;
}
}
~Sound()
{
stop();
g_clear_pointer(&m_context, ca_context_destroy);
}
private:
void stop()
{
if (m_context != nullptr)
{
const auto rv = ca_context_cancel(m_context, m_canberra_id);
if (rv != CA_SUCCESS)
g_warning("Failed to cancel alarm sound: %s", ca_strerror(rv));
}
if (m_loop_tag != 0)
{
g_source_remove(m_loop_tag);
m_loop_tag = 0;
}
}
void play()
{
auto context = m_context;
g_return_if_fail(context != nullptr);
const auto filename = m_filename.c_str();
const float gain = get_gain_level(m_volume);
ca_proplist* props = nullptr;
ca_proplist_create(&props);
ca_proplist_sets(props, CA_PROP_MEDIA_FILENAME, filename);
ca_proplist_setf(props, CA_PROP_CANBERRA_VOLUME, "%f", gain);
const auto rv = ca_context_play_full(context, m_canberra_id, props,
on_done_playing, this);
if (rv != CA_SUCCESS)
g_warning("Unable to play '%s': %s", filename, ca_strerror(rv));
g_clear_pointer(&props, ca_proplist_destroy);
}
static float get_gain_level(const AlarmVolume volume)
{
/* These values aren't set in stone --
arrived at from from manual tests on Nexus 4 */
switch (volume)
{
case ALARM_VOLUME_VERY_QUIET: return -8;
case ALARM_VOLUME_QUIET: return -4;
case ALARM_VOLUME_LOUD: return 4;
case ALARM_VOLUME_VERY_LOUD: return 8;
default: return 0;
}
}
static void on_done_playing(ca_context*, uint32_t, int rv, void* gself)
{
// if we still need to loop, wait a second, then play it again
if (rv == CA_SUCCESS)
{
auto self = static_cast<Self*>(gself);
if ((self->m_loop_tag == 0) &&
(self->m_loop) &&
(self->m_clock->localtime() < self->m_loop_end_time))
{
self->m_loop_tag = g_timeout_add_seconds(1, play_idle, self);
}
}
}
static gboolean play_idle(gpointer gself)
{
auto self = static_cast<Self*>(gself);
self->m_loop_tag = 0;
self->play();
return G_SOURCE_REMOVE;
}
/***
****
***/
static int32_t get_next_canberra_id()
{
static int32_t next_canberra_id = 1;
return next_canberra_id++;
}
const std::shared_ptr<Clock> m_clock;
const std::string m_filename;
const AlarmVolume m_volume;
const bool m_loop;
const int32_t m_canberra_id;
const DateTime m_loop_end_time;
ca_context* m_context = nullptr;
guint m_loop_tag = 0;
};
class SoundBuilder
{
public:
void set_clock(const std::shared_ptr<Clock>& c) {m_clock = c;}
void set_filename(const std::string& s) {m_filename = s;}
void set_volume(const AlarmVolume v) {m_volume = v;}
void set_duration_minutes(int i) {m_duration_minutes=i;}
void set_looping(bool b) {m_looping=b;}
Sound* operator()() {
return new Sound (m_clock,
m_filename,
m_volume,
m_duration_minutes,
m_looping);
}
private:
std::shared_ptr<Clock> m_clock;
std::string m_filename;
AlarmVolume m_volume = ALARM_VOLUME_NORMAL;
int m_duration_minutes = 30;
bool m_looping = true;
};
/**
* A popup notification (with optional sound)
* that emits a Response signal when done.
*/
class Popup
{
public:
Popup(const Appointment& appointment, const SoundBuilder& sound_builder):
m_appointment(appointment),
m_interactive(get_interactive()),
m_sound_builder(sound_builder)
{
// ensure notify_init() is called once
// before we start popping up dialogs
static bool m_nn_inited = false;
if (G_UNLIKELY(!m_nn_inited))
{
if(!notify_init("indicator-datetime-service"))
g_critical("libnotify initialization failed");
m_nn_inited = true;
}
show();
}
~Popup()
{
if (m_nn != nullptr)
{
notify_notification_clear_actions(m_nn);
g_signal_handlers_disconnect_by_data(m_nn, this);
g_clear_object(&m_nn);
}
}
typedef enum
{
RESPONSE_SHOW,
RESPONSE_DISMISS,
RESPONSE_CLOSE
}
Response;
core::Signal<Response>& response() { return m_response; }
private:
void show()
{
const Appointment& appointment = m_appointment;
/// strftime(3) format string for an alarm's snap decision
const auto timestr = appointment.begin.format(_("%a, %X"));
auto title = g_strdup_printf(_("Alarm %s"), timestr.c_str());
const auto body = appointment.summary;
const gchar* icon_name = "alarm-clock";
m_nn = notify_notification_new(title, body.c_str(), icon_name);
if (m_interactive)
{
notify_notification_set_hint_string(m_nn,
"x-canonical-snap-decisions",
"true");
notify_notification_set_hint_string(m_nn,
"x-canonical-private-button-tint",
"true");
/// alarm popup dialog's button to show the active alarm
notify_notification_add_action(m_nn, "show", _("Show"),
on_snap_show, this, nullptr);
/// alarm popup dialog's button to shut up the alarm
notify_notification_add_action(m_nn, "dismiss", _("Dismiss"),
on_snap_dismiss, this, nullptr);
g_signal_connect(m_nn, "closed", G_CALLBACK(on_snap_closed), this);
}
bool shown = true;
GError* error = nullptr;
notify_notification_show(m_nn, &error);
if (error != NULL)
{
g_critical("Unable to show snap decision for '%s': %s",
body.c_str(), error->message);
g_error_free(error);
shown = false;
}
// Loop the sound *only* if we're prompting the user for a response.
// Otherwise, just play the sound once.
m_sound_builder.set_looping (shown && m_interactive);
m_sound.reset (m_sound_builder());
// if showing the notification didn't work,
// treat it as if the user clicked the 'show' button
if (!shown)
{
on_snap_show(nullptr, nullptr, this);
on_snap_dismiss(nullptr, nullptr, this);
}
g_free(title);
}
// user clicked 'show'
static void on_snap_show(NotifyNotification*, gchar*, gpointer gself)
{
auto self = static_cast<Self*>(gself);
self->m_response_value = RESPONSE_SHOW;
self->m_sound.reset();
}
// user clicked 'dismiss'
static void on_snap_dismiss(NotifyNotification*, gchar*, gpointer gself)
{
auto self = static_cast<Self*>(gself);
self->m_response_value = RESPONSE_DISMISS;
self->m_sound.reset();
}
// the popup was closed
static void on_snap_closed(NotifyNotification*, gpointer gself)
{
auto self = static_cast<Self*>(gself);
self->m_sound.reset();
self->m_response(self->m_response_value);
}
/***
**** Interactive
***/
static std::set<std::string> get_server_caps()
{
std::set<std::string> caps_set;
auto caps_gl = notify_get_server_caps();
std::string caps_str;
for(auto l=caps_gl; l!=nullptr; l=l->next)
{
caps_set.insert((const char*)l->data);
caps_str += (const char*) l->data;;
if (l->next != nullptr)
caps_str += ", ";
}
g_debug("%s notify_get_server() returned [%s]", G_STRFUNC, caps_str.c_str());
g_list_free_full(caps_gl, g_free);
return caps_set;
}
static bool get_interactive()
{
static bool interactive;
static bool inited = false;
if (G_UNLIKELY(!inited))
{
interactive = get_server_caps().count("actions") != 0;
inited = true;
}
return interactive;
}
/***
****
***/
typedef Popup Self;
const Appointment m_appointment;
const bool m_interactive;
SoundBuilder m_sound_builder;
std::unique_ptr<Sound> m_sound;
core::Signal<Response> m_response;
Response m_response_value = RESPONSE_CLOSE;
NotifyNotification* m_nn = nullptr;
};
/**
*** libnotify -- snap decisions
**/
std::string get_local_filename (const std::string& str)
{
std::string ret;
if (!str.empty())
{
GFile* files[] = { g_file_new_for_path(str.c_str()),
g_file_new_for_uri(str.c_str()) };
for(auto& file : files)
{
if (g_file_is_native(file) && g_file_query_exists(file, nullptr))
{
char* tmp = g_file_get_path(file);
if (tmp != nullptr)
{
ret = tmp;
g_free(tmp);
break;
}
}
}
for(auto& file : files)
g_object_unref(file);
}
return ret;
}
std::string get_alarm_sound(const Appointment& appointment,
const std::shared_ptr<const Settings>& settings)
{
const char* FALLBACK {"/usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg"};
const std::string candidates[] = { appointment.audio_url,
settings->alarm_sound.get(),
FALLBACK };
std::string alarm_sound;
for(const auto& candidate : candidates)
{
alarm_sound = get_local_filename(candidate);
if (!alarm_sound.empty())
break;
}
g_debug("%s: Appointment \"%s\" using alarm sound \"%s\"",
G_STRFUNC, appointment.summary.c_str(), alarm_sound.c_str());
return alarm_sound;
}
} // unnamed namespace
/***
****
***/
Snap::Snap(const std::shared_ptr<Clock>& clock,
const std::shared_ptr<const Settings>& settings):
m_clock(clock),
m_settings(settings)
{
}
Snap::~Snap()
{
}
void Snap::operator()(const Appointment& appointment,
appointment_func show,
appointment_func dismiss)
{
if (!appointment.has_alarms)
{
dismiss(appointment);
return;
}
// create a popup...
SoundBuilder sound_builder;
sound_builder.set_filename(get_alarm_sound(appointment, m_settings));
sound_builder.set_volume(m_settings->alarm_volume.get());
sound_builder.set_clock(m_clock);
sound_builder.set_duration_minutes(m_settings->alarm_duration.get());
auto popup = new Popup(appointment, sound_builder);
// listen for it to finish...
popup->response().connect([appointment,
show,
dismiss,
popup](Popup::Response response){
// we can't delete the Popup inside its response() signal handler,
// so push that to an idle func
g_idle_add([](gpointer gdata){
delete static_cast<Popup*>(gdata);
return G_SOURCE_REMOVE;
}, popup);
// maybe notify the client code that the popup's done
if (response == Popup::RESPONSE_SHOW)
show(appointment);
else if (response == Popup::RESPONSE_DISMISS)
dismiss(appointment);
});
}
/***
****
***/
} // namespace datetime
} // namespace indicator
} // namespace unity
|