aboutsummaryrefslogtreecommitdiff
path: root/src/snap.cpp
blob: 37ce741ccf3c0b5ba7ff507ddcc802cc76df0dd6 (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
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
/*
 * Copyright 2014 Canonical Ltd.
 * Copyright 2021 Robert Tari
 *
 * 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>
 *   Robert Tari <robert@tari.in>
 */

#ifdef HAS_LOMIRI_ACCTSERVICE_SOUND
#include "dbus-accounts-sound.h"

#include <datetime/snap.h>
#include <datetime/utils.h> // is_locale_12h()

#include <notifications/awake.h>
#include <notifications/haptic.h>
#include <notifications/sound.h>

#include <gst/gst.h>

#include <glib/gi18n.h>

#include <chrono>
#include <set>
#include <string>

#include <unistd.h> // getuid()
#include <sys/types.h> // getuid()

namespace ain = ayatana::indicator::notifications;

namespace ayatana {
namespace indicator {
namespace datetime {

/***
****
***/

class Snap::Impl
{
public:

    Impl(const std::shared_ptr<ayatana::indicator::notifications::Engine>& engine,
         const std::shared_ptr<ayatana::indicator::notifications::SoundBuilder>& sound_builder,
         const std::shared_ptr<const Settings>& settings,
         GDBusConnection* system_bus):
      m_engine(engine),
      m_sound_builder(sound_builder),
      m_settings(settings),
      m_cancellable(g_cancellable_new()),
      m_system_bus{G_DBUS_CONNECTION(g_object_ref(system_bus))}
    {
        auto object_path = g_strdup_printf("/org/freedesktop/Accounts/User%lu", (gulong)getuid());


        accounts_service_sound_proxy_new(m_system_bus,
                                         G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
                                         "org.freedesktop.Accounts",
                                         object_path,
                                         m_cancellable,
                                         on_sound_proxy_ready,
                                         this);
        g_free(object_path);
    }

    ~Impl()
    {
        g_cancellable_cancel(m_cancellable);
        g_clear_object(&m_cancellable);
        g_clear_object(&m_accounts_service_sound_proxy);
        g_clear_object(&m_system_bus);

        for (const auto& key : m_notifications)
            m_engine->close (key);
    }

    void operator()(const Appointment& appointment,
                    const Alarm& alarm,
                    response_func on_response)
    {
        // If calendar notifications are disabled, don't show them
        if (!appointment.is_alarm() && !calendar_notifications_are_enabled()) {
            g_debug("Skipping disabled calendar event '%s' notification", appointment.summary.c_str());
            return;
        }

        /* Alarms and calendar events are treated differently.
           Alarms should require manual intervention to dismiss.
           Calendar events are less urgent and shouldn't require manual
           intervention and shouldn't loop the sound. */
        const bool interactive = appointment.is_alarm() && m_engine->supports_actions();

        // force the system to stay awake
        std::shared_ptr<ain::Awake> awake;
        if (appointment.is_alarm() || calendar_bubbles_enabled() || calendar_list_enabled()) {
            awake = std::make_shared<ain::Awake>(m_system_bus, m_engine->app_name());
        }

        // calendar events are muted in silent mode; alarm clocks never are
        std::shared_ptr<ain::Sound> sound;
        if (appointment.is_alarm() || (calendar_sounds_enabled() && !silent_mode())) {
            // create the sound.
            const auto role = appointment.is_alarm() ? "alarm" : "alert";
            const auto uri = get_alarm_uri(appointment, alarm, m_settings);
            const auto volume = m_settings->alarm_volume.get();
            const bool loop = interactive;
            sound = m_sound_builder->create(role, uri, volume, loop);
        }

        // create the haptic feedback...
        std::shared_ptr<ain::Haptic> haptic;
        if (should_vibrate() && (appointment.is_alarm() || calendar_vibrations_enabled())) {
            // when in silent mode should only vibrate if user defined so
            if (!silent_mode() || vibrate_in_silent_mode_enabled()) {
                const auto haptic_mode = m_settings->alarm_haptic.get();
                if (haptic_mode == "pulse")
                    haptic = std::make_shared<ain::Haptic>(ain::Haptic::MODE_PULSE, appointment.is_alarm());
            }
        }

        // show a notification...
        const auto minutes = std::chrono::minutes(m_settings->alarm_duration.get());
        ain::Builder b;
        b.set_body (appointment.summary);
        b.set_icon_name (appointment.is_alarm() ? "alarm-clock" : "calendar-app");
        b.add_hint (ain::Builder::HINT_NONSHAPED_ICON);
        b.set_start_time (appointment.begin.to_unix());

        const char * timefmt;
        if (is_locale_12h()) {
            /** strftime(3) format for abbreviated weekday,
                hours, minutes in a 12h locale; e.g. Wed, 2:00 PM */
            timefmt = _("%a, %l:%M %p");
        } else {
            /** A strftime(3) format for abbreviated weekday,
                hours, minutes in a 24h locale; e.g. Wed, 14:00 */
            timefmt = _("%a, %H:%M");
        }
        const auto timestr = appointment.begin.format(timefmt);

        const char * titlefmt;
        if (appointment.is_alarm()) {
            titlefmt = _("Alarm %s");
        } else {
            titlefmt = _("Event %s");
        }
        auto title = g_strdup_printf(titlefmt, timestr.c_str());
        b.set_title (title);
        g_free (title);
        b.set_timeout (std::chrono::duration_cast<std::chrono::seconds>(minutes));
        if (interactive) {
            b.add_hint (ain::Builder::HINT_SNAP);
            b.add_hint (ain::Builder::HINT_AFFIRMATIVE_HINT);
            b.add_action (ACTION_NONE, _("OK"));
            b.add_action (ACTION_SNOOZE, _("Snooze"));
        } else {
            b.add_hint (ain::Builder::HINT_INTERACTIVE);
            b.add_action (ACTION_SHOW_APP, _("OK"));
        }

        // add 'sound', 'haptic', and 'awake' objects to the capture so
        // they stay alive until the closed callback is called; i.e.,
        // for the lifespan of the notficiation
        b.set_closed_callback([appointment, alarm, on_response, sound, awake, haptic]
                              (const std::string& action){
            Snap::Response response;
            if ((action == ACTION_SNOOZE) || (appointment.is_alarm() && action.empty()))
                response = Snap::Response::Snooze;
            else if (action == ACTION_SHOW_APP)
                response = Snap::Response::ShowApp;
            else
                response = Snap::Response::None;

            on_response(appointment, alarm, response);
        });

        //TODO: we need to extend it to support alarms appointments
        if (!appointment.is_alarm()) {
            b.set_timeout_callback([appointment, alarm, on_response](){
                on_response(appointment, alarm, Snap::Response::ShowApp);
            });
        }

        b.set_show_notification_bubble(appointment.is_alarm() || calendar_bubbles_enabled());
        b.set_post_to_messaging_menu(appointment.is_alarm() || calendar_list_enabled());

        const auto key = m_engine->show(b);
        if (key)
            m_notifications.insert (key);
    }

private:

    bool calendar_notifications_are_enabled() const
    {
        return m_settings->cal_notification_enabled.get();
    }

    bool calendar_sounds_enabled() const
    {
        return m_settings->cal_notification_sounds.get();
    }

    bool calendar_vibrations_enabled() const
    {
        return m_settings->cal_notification_vibrations.get();
    }

    bool calendar_bubbles_enabled() const
    {
        return m_settings->cal_notification_bubbles.get();
    }

    bool calendar_list_enabled() const
    {
        return m_settings->cal_notification_list.get();
    }

    bool vibrate_in_silent_mode_enabled() const
    {
        return m_settings->vibrate_silent_mode.get();
    }

    static void on_sound_proxy_ready(GObject* /*source_object*/, GAsyncResult* res, gpointer gself)
    {
        GError * error;

        error = nullptr;
        auto proxy = accounts_service_sound_proxy_new_finish (res, &error);
        if (error != nullptr)
        {
            if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                g_warning("%s Couldn't find accounts service sound proxy: %s", G_STRLOC, error->message);

            g_clear_error(&error);
        }
        else
        {
            static_cast<Impl*>(gself)->m_accounts_service_sound_proxy = proxy;
        }
    }

    bool silent_mode() const
    {
        return (m_accounts_service_sound_proxy != nullptr)
            && (accounts_service_sound_get_silent_mode(m_accounts_service_sound_proxy));
    }

    bool should_vibrate() const
    {
        return (m_accounts_service_sound_proxy != nullptr)
            && (accounts_service_sound_get_other_vibrate(m_accounts_service_sound_proxy));
    }

    std::string get_alarm_uri(const Appointment& appointment,
                              const Alarm& alarm,
                              const std::shared_ptr<const Settings>& settings) const
    {
        const auto is_alarm = appointment.is_alarm();
        const std::string candidates[] = {
            alarm.audio_url,
            is_alarm ? settings->alarm_sound.get() : settings->calendar_sound.get(),
            is_alarm ? ALARM_DEFAULT_SOUND : CALENDAR_DEFAULT_SOUND
        };

        std::string uri;

        for(const auto& candidate : candidates)
        {
            if (gst_uri_is_valid (candidate.c_str()))
            {
                uri = candidate;
                break;
            }
            else if (g_file_test(candidate.c_str(), G_FILE_TEST_EXISTS))
            {
                gchar* tmp = gst_filename_to_uri(candidate.c_str(), nullptr);
                if (tmp != nullptr)
                {
                    uri = tmp;
                    g_free (tmp);
                    break;
                }
            }
        }

        return uri;
    }

    const std::shared_ptr<ayatana::indicator::notifications::Engine> m_engine;
    const std::shared_ptr<ayatana::indicator::notifications::SoundBuilder> m_sound_builder;
    const std::shared_ptr<const Settings> m_settings;
    std::set<int> m_notifications;
    GCancellable * m_cancellable {nullptr};
    AccountsServiceSound * m_accounts_service_sound_proxy {nullptr};
    GDBusConnection * m_system_bus {nullptr};

    static constexpr char const * ACTION_NONE {"none"};
    static constexpr char const * ACTION_SNOOZE {"snooze"};
    static constexpr char const * ACTION_SHOW_APP {"show-app"};
};

/***
****
***/

Snap::Snap(const std::shared_ptr<ayatana::indicator::notifications::Engine>& engine,
           const std::shared_ptr<ayatana::indicator::notifications::SoundBuilder>& sound_builder,
           const std::shared_ptr<const Settings>& settings,
           GDBusConnection* system_bus):
  impl(new Impl(engine, sound_builder, settings, system_bus))
{
}

Snap::~Snap()
{
}

void
Snap::operator()(const Appointment& appointment,
                 const Alarm& alarm,
                 response_func on_response)
{
  (*impl)(appointment, alarm, on_response);
}

/***
****
***/

} // namespace datetime
} // namespace indicator
} // namespace ayatana

#endif