Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 77c579d | 2018-01-30 15:54:02 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Savoir-faire Linux Inc. |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program 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 |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "ringnotify.h" |
| 21 | #include "config.h" |
Stepan Salenikovich | 5834f83 | 2016-05-20 15:32:29 -0400 | [diff] [blame] | 22 | #include "ring_client.h" |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 23 | |
Nicolas Jager | 655b8db | 2017-12-15 14:04:32 -0500 | [diff] [blame] | 24 | #if USE_CANBERRA |
| 25 | #include <canberra-gtk.h> |
| 26 | #endif // USE_CANBERRA |
| 27 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 28 | #if USE_LIBNOTIFY |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 29 | #include <glib/gi18n.h> |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 30 | #include <libnotify/notify.h> |
| 31 | #include <memory> |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 32 | #include <globalinstances.h> |
| 33 | #include "native/pixbufmanipulator.h" |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 34 | #include <call.h> |
| 35 | #include <QtCore/QSize> |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 36 | #include <media/text.h> |
| 37 | #include <callmodel.h> |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 38 | #include <media/textrecording.h> |
| 39 | #include <media/recordingmodel.h> |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 40 | #include <recentmodel.h> |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 41 | #endif |
| 42 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 43 | #if USE_LIBNOTIFY |
| 44 | |
| 45 | static constexpr int MAX_NOTIFICATIONS = 10; // max unread chat msgs to display from the same contact |
| 46 | static constexpr const char* SERVER_NOTIFY_OSD = "notify-osd"; |
Nicolas Jager | 655b8db | 2017-12-15 14:04:32 -0500 | [diff] [blame] | 47 | static constexpr const char* NOTIFICATION_FILE = SOUNDSDIR "/ringtone_notify.wav"; |
| 48 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 49 | |
| 50 | /* struct to store the parsed list of the notify server capabilities */ |
| 51 | struct RingNotifyServerInfo |
| 52 | { |
| 53 | /* info */ |
| 54 | char *name; |
| 55 | char *vendor; |
| 56 | char *version; |
| 57 | char *spec; |
| 58 | |
| 59 | /* capabilities */ |
| 60 | gboolean append; |
| 61 | gboolean actions; |
| 62 | |
| 63 | /* the info strings must be freed */ |
| 64 | ~RingNotifyServerInfo() { |
| 65 | g_free(name); |
| 66 | g_free(vendor); |
| 67 | g_free(version); |
| 68 | g_free(spec); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | static struct RingNotifyServerInfo server_info; |
| 73 | #endif |
| 74 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 75 | void |
| 76 | ring_notify_init() |
| 77 | { |
| 78 | #if USE_LIBNOTIFY |
| 79 | notify_init("Ring"); |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 80 | |
| 81 | /* get notify server info */ |
| 82 | if (notify_get_server_info(&server_info.name, |
| 83 | &server_info.vendor, |
| 84 | &server_info.version, |
| 85 | &server_info.spec)) { |
| 86 | g_debug("notify server name: %s, vendor: %s, version: %s, spec: %s", |
| 87 | server_info.name, server_info.vendor, server_info.version, server_info.spec); |
| 88 | } |
| 89 | |
| 90 | /* check notify server capabilities */ |
| 91 | auto list = notify_get_server_caps(); |
| 92 | while (list) { |
| 93 | if (g_strcmp0((const char *)list->data, "append") == 0 || |
| 94 | g_strcmp0((const char *)list->data, "x-canonical-append") == 0) { |
| 95 | server_info.append = TRUE; |
| 96 | } |
| 97 | if (g_strcmp0((const char *)list->data, "actions") == 0) { |
| 98 | server_info.actions = TRUE; |
| 99 | } |
| 100 | |
| 101 | list = g_list_next(list); |
| 102 | } |
| 103 | |
| 104 | g_list_free_full(list, g_free); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 105 | #endif |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | ring_notify_uninit() |
| 110 | { |
| 111 | #if USE_LIBNOTIFY |
| 112 | if (notify_is_initted()) |
| 113 | notify_uninit(); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | gboolean |
| 118 | ring_notify_is_initted() |
| 119 | { |
| 120 | #if USE_LIBNOTIFY |
| 121 | return notify_is_initted(); |
| 122 | #else |
| 123 | return FALSE; |
| 124 | #endif |
| 125 | } |
| 126 | |
Rafał Babski | b210553 | 2017-07-26 00:10:36 +0200 | [diff] [blame] | 127 | #if USE_LIBNOTIFY |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 128 | static void |
| 129 | ring_notify_show_cm(NotifyNotification*, char *, ContactMethod *cm) |
| 130 | { |
| 131 | /* show the main window in case its hidden */ |
| 132 | if (auto action = g_action_map_lookup_action(G_ACTION_MAP(g_application_get_default()), "show-main-window")) { |
| 133 | g_action_change_state(action, g_variant_new_boolean(TRUE)); |
| 134 | } |
| 135 | /* select the relevant cm */ |
| 136 | auto idx = RecentModel::instance().getIndex(cm); |
| 137 | if (idx.isValid()) { |
| 138 | RecentModel::instance().selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect); |
| 139 | } |
| 140 | } |
Rafał Babski | b210553 | 2017-07-26 00:10:36 +0200 | [diff] [blame] | 141 | #endif |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 142 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 143 | gboolean |
| 144 | ring_notify_incoming_call( |
| 145 | #if !USE_LIBNOTIFY |
| 146 | G_GNUC_UNUSED |
| 147 | #endif |
| 148 | Call* call) |
| 149 | { |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 150 | gboolean success = FALSE; |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 151 | #if USE_LIBNOTIFY |
| 152 | g_return_val_if_fail(call, FALSE); |
| 153 | |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 154 | gchar *body = g_markup_escape_text(call->formattedName().toUtf8().constData(), -1); |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 155 | std::shared_ptr<NotifyNotification> notification( |
Stepan Salenikovich | a1b8cb3 | 2015-09-11 14:58:35 -0400 | [diff] [blame] | 156 | notify_notification_new(_("Incoming call"), body, NULL), g_object_unref); |
Stepan Salenikovich | c2e4426 | 2015-06-16 12:46:33 -0400 | [diff] [blame] | 157 | g_free(body); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 158 | |
| 159 | /* get photo */ |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 160 | QVariant var_p = GlobalInstances::pixmapManipulator().callPhoto( |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 161 | call->peerContactMethod(), QSize(50, 50), false); |
| 162 | std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>(); |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 163 | notify_notification_set_image_from_pixbuf(notification.get(), photo.get()); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 164 | |
| 165 | /* calls have highest urgency */ |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 166 | notify_notification_set_urgency(notification.get(), NOTIFY_URGENCY_CRITICAL); |
| 167 | notify_notification_set_timeout(notification.get(), NOTIFY_EXPIRES_DEFAULT); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 168 | |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 169 | /* if the notification server supports actions, make the default action to show the call */ |
| 170 | if (server_info.actions) { |
| 171 | notify_notification_add_action(notification.get(), |
| 172 | "default", |
| 173 | C_("notification action name", "Show"), |
| 174 | (NotifyActionCallback)ring_notify_show_cm, |
| 175 | call->peerContactMethod(), |
| 176 | nullptr); |
| 177 | } |
| 178 | |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 179 | GError *error = NULL; |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 180 | success = notify_notification_show(notification.get(), &error); |
| 181 | |
| 182 | if (success) { |
| 183 | /* monitor the life cycle of the call and try to close the notification |
| 184 | * once the call has been aswered */ |
| 185 | auto state_changed_conn = std::make_shared<QMetaObject::Connection>(); |
| 186 | *state_changed_conn = QObject::connect( |
| 187 | call, |
| 188 | &Call::lifeCycleStateChanged, |
| 189 | [notification, state_changed_conn] (Call::LifeCycleState newState, G_GNUC_UNUSED Call::LifeCycleState previousState) |
| 190 | { |
| 191 | g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification.get())); |
| 192 | if (newState > Call::LifeCycleState::INITIALIZATION) { |
| 193 | /* note: not all systems will actually close the notification |
| 194 | * even if the above function returns as true */ |
| 195 | if (!notify_notification_close(notification.get(), NULL)) |
| 196 | g_warning("could not close notification"); |
| 197 | |
| 198 | /* once we (try to) close the notification, we can |
| 199 | * disconnect from this signal; this should also destroy |
| 200 | * the notification shared_ptr as its ref count will |
| 201 | * drop to 0 */ |
| 202 | QObject::disconnect(*state_changed_conn); |
| 203 | } |
| 204 | } |
| 205 | ); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 206 | } else { |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 207 | g_warning("failed to show notification: %s", error->message); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 208 | g_clear_error(&error); |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 209 | } |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 210 | #endif |
Stepan Salenikovich | 9675db3 | 2015-06-16 14:36:54 -0400 | [diff] [blame] | 211 | return success; |
Stepan Salenikovich | 76c33e6 | 2015-05-22 12:24:07 -0400 | [diff] [blame] | 212 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 213 | |
| 214 | #if USE_LIBNOTIFY |
| 215 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 216 | static void |
| 217 | ring_notify_free_list(gpointer, GList *value, gpointer) |
| 218 | { |
| 219 | if (value) { |
| 220 | g_object_unref(G_OBJECT(value->data)); |
| 221 | g_list_free(value); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | ring_notify_free_chat_table(GHashTable *table) { |
| 227 | if (table) { |
| 228 | g_hash_table_foreach(table, (GHFunc)ring_notify_free_list, nullptr); |
| 229 | g_hash_table_destroy(table); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns a pointer to a GHashTable which contains key,value pairs where a ContactMethod pointer |
| 235 | * is the key and a GList of notifications for that CM is the vlue. |
| 236 | */ |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 237 | GHashTable * |
| 238 | ring_notify_get_chat_table() |
| 239 | { |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 240 | static std::unique_ptr<GHashTable, decltype(ring_notify_free_chat_table)&> chat_table( |
| 241 | nullptr, ring_notify_free_chat_table); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 242 | |
| 243 | if (chat_table.get() == nullptr) |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 244 | chat_table.reset(g_hash_table_new(NULL, NULL)); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 245 | |
| 246 | return chat_table.get(); |
| 247 | } |
| 248 | |
| 249 | static void |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 250 | notification_closed(NotifyNotification *notification, ContactMethod *cm) |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 251 | { |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 252 | g_return_if_fail(cm); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 253 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 254 | /* remove from the list */ |
| 255 | auto chat_table = ring_notify_get_chat_table(); |
| 256 | if (auto list = (GList *)g_hash_table_lookup(chat_table, cm)) { |
| 257 | list = g_list_remove(list, notification); |
| 258 | if (list) { |
| 259 | // the head of the list may have changed |
| 260 | g_hash_table_replace(chat_table, cm, list); |
| 261 | } else { |
| 262 | g_hash_table_remove(chat_table, cm); |
| 263 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 264 | } |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 265 | |
| 266 | g_object_unref(notification); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static gboolean |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 270 | ring_notify_show_text_message(ContactMethod *cm, const QModelIndex& idx) |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 271 | { |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 272 | g_return_val_if_fail(idx.isValid() && cm, FALSE); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 273 | gboolean success = FALSE; |
| 274 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 275 | auto title = g_markup_printf_escaped(C_("Text message notification", "%s says:"), idx.data(static_cast<int>(Ring::Role::Name)).toString().toUtf8().constData()); |
| 276 | auto body = g_markup_escape_text(idx.data(Qt::DisplayRole).toString().toUtf8().constData(), -1); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 277 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 278 | NotifyNotification *notification_new = nullptr; |
| 279 | NotifyNotification *notification_old = nullptr; |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 280 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 281 | /* try to get the previous notification */ |
| 282 | auto chat_table = ring_notify_get_chat_table(); |
| 283 | auto list = (GList *)g_hash_table_lookup(chat_table, cm); |
| 284 | if (list) |
| 285 | notification_old = (NotifyNotification *)list->data; |
| 286 | |
| 287 | /* we display chat notifications in different ways to suit different notification servers and |
| 288 | * their capabilities: |
| 289 | * 1. if the server doesn't support appending (eg: Notification Daemon) then we update the |
| 290 | * previous notification (if exists) with new text; otherwise it takes we have many |
| 291 | * notifications from the same person... we don't concatinate the old messages because |
| 292 | * servers which don't support append usually don't support multi line bodies |
| 293 | * 2. the notify-osd server supports appending; however it doesn't clear the old notifications |
| 294 | * on demand, which means in our case that chat messages which have already been read could |
| 295 | * still be displayed when a new notification is appended, thus in this case, we update |
| 296 | * the old notification body manually to only contain the unread messages |
| 297 | * 3. the 3rd case is that the server supports append but is not notify-osd, then we simply use |
| 298 | * the append feature |
| 299 | */ |
| 300 | |
| 301 | if (notification_old && !server_info.append) { |
| 302 | /* case 1 */ |
| 303 | notify_notification_update(notification_old, title, body, nullptr); |
| 304 | notification_new = notification_old; |
| 305 | } else if (notification_old && g_strcmp0(server_info.name, SERVER_NOTIFY_OSD) == 0) { |
| 306 | /* case 2 */ |
| 307 | /* print up to MAX_NOTIFICATIONS unread messages */ |
| 308 | int msg_count = 0; |
| 309 | auto idx_next = idx.sibling(idx.row() - 1, idx.column()); |
| 310 | auto read = idx_next.data(static_cast<int>(Media::TextRecording::Role::IsRead)).toBool(); |
| 311 | while (idx_next.isValid() && !read && msg_count < MAX_NOTIFICATIONS) { |
| 312 | |
| 313 | auto body_prev = body; |
| 314 | body = g_markup_printf_escaped("%s\n%s", body_prev, idx_next.data(Qt::DisplayRole).toString().toUtf8().constData()); |
| 315 | g_free(body_prev); |
| 316 | |
| 317 | idx_next = idx_next.sibling(idx_next.row() - 1, idx_next.column()); |
| 318 | read = idx_next.data(static_cast<int>(Media::TextRecording::Role::IsRead)).toBool(); |
| 319 | ++msg_count; |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 320 | } |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 321 | |
| 322 | notify_notification_update(notification_old, title, body, nullptr); |
| 323 | |
| 324 | notification_new = notification_old; |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 325 | } else { |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 326 | /* need new notification for case 1, 2, or 3 */ |
| 327 | notification_new = notify_notification_new(title, body, nullptr); |
| 328 | |
| 329 | /* track in hash table */ |
| 330 | auto list = (GList *)g_hash_table_lookup(chat_table, cm); |
| 331 | list = g_list_append(list, notification_new); |
| 332 | g_hash_table_replace(chat_table, cm, list); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 333 | |
| 334 | /* get photo */ |
Stepan Salenikovich | bbd6c13 | 2015-08-20 15:21:48 -0400 | [diff] [blame] | 335 | QVariant var_p = GlobalInstances::pixmapManipulator().callPhoto( |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 336 | cm, QSize(50, 50), false); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 337 | std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>(); |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 338 | notify_notification_set_image_from_pixbuf(notification_new, photo.get()); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 339 | |
| 340 | /* normal priority for messages */ |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 341 | notify_notification_set_urgency(notification_new, NOTIFY_URGENCY_NORMAL); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 342 | |
| 343 | /* remove the key and value from the hash table once the notification is |
| 344 | * closed; note that this will also unref the notification */ |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 345 | g_signal_connect(notification_new, "closed", G_CALLBACK(notification_closed), cm); |
Stepan Salenikovich | f8e78cb | 2016-09-12 17:14:51 -0400 | [diff] [blame] | 346 | |
| 347 | /* if the notification server supports actions, make the default action to show the chat view */ |
| 348 | if (server_info.actions) { |
| 349 | notify_notification_add_action(notification_new, |
| 350 | "default", |
| 351 | C_("notification action name", "Show"), |
| 352 | (NotifyActionCallback)ring_notify_show_cm, |
| 353 | cm, |
| 354 | nullptr); |
| 355 | } |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | GError *error = nullptr; |
| 359 | success = notify_notification_show(notification_new, &error); |
Nicolas Jager | 655b8db | 2017-12-15 14:04:32 -0500 | [diff] [blame] | 360 | |
| 361 | #if USE_CANBERRA |
| 362 | auto status = ca_context_play(ca_gtk_context_get(), |
| 363 | 0, |
| 364 | CA_PROP_MEDIA_FILENAME, |
| 365 | NOTIFICATION_FILE, |
| 366 | NULL); |
| 367 | if (status != 0) |
| 368 | g_warning("ca_context_play: %s", ca_strerror(status)); |
| 369 | #endif // USE_CANBERRA |
| 370 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 371 | if (!success) { |
| 372 | g_warning("failed to show notification: %s", error->message); |
| 373 | g_clear_error(&error); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | g_free(title); |
| 377 | g_free(body); |
| 378 | |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 379 | return success; |
| 380 | } |
| 381 | |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 382 | static gboolean |
| 383 | show_message_if_unread(const QModelIndex *idx) |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 384 | { |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 385 | g_return_val_if_fail(idx && idx->isValid(), G_SOURCE_REMOVE); |
| 386 | |
| 387 | if (!idx->data(static_cast<int>(Media::TextRecording::Role::IsRead)).toBool()) { |
| 388 | auto cm = idx->data(static_cast<int>(Media::TextRecording::Role::ContactMethod)).value<ContactMethod *>(); |
| 389 | ring_notify_show_text_message(cm, *idx); |
| 390 | } |
| 391 | |
| 392 | return G_SOURCE_REMOVE; |
| 393 | } |
| 394 | |
| 395 | static void |
| 396 | delete_idx(QModelIndex *idx) |
| 397 | { |
Nicolas Jager | fad674f | 2017-10-19 11:35:36 -0400 | [diff] [blame] | 398 | if (idx) { |
| 399 | delete idx; |
| 400 | idx = nullptr; |
| 401 | } |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 404 | #endif |
| 405 | |
| 406 | void |
| 407 | ring_notify_message( |
| 408 | #if !USE_LIBNOTIFY |
Nicolas Jager | cfeffc3 | 2018-01-12 10:58:55 -0500 | [diff] [blame] | 409 | ContactMethod*, Media::TextRecording*) |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 410 | #else |
Nicolas Jager | cfeffc3 | 2018-01-12 10:58:55 -0500 | [diff] [blame] | 411 | ContactMethod *cm, Media::TextRecording *t) |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 412 | #endif |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 413 | { |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 414 | |
| 415 | #if USE_LIBNOTIFY |
Nicolas Jager | cfeffc3 | 2018-01-12 10:58:55 -0500 | [diff] [blame] | 416 | g_return_if_fail(cm && t); |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 417 | |
| 418 | // get the message |
| 419 | auto model = t->instantMessagingModel(); |
| 420 | auto msg_idx = model->index(model->rowCount()-1, 0); |
| 421 | |
Nicolas Jager | cfeffc3 | 2018-01-12 10:58:55 -0500 | [diff] [blame] | 422 | ring_notify_show_text_message(cm, msg_idx); |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 423 | |
Nicolas Jager | cfeffc3 | 2018-01-12 10:58:55 -0500 | [diff] [blame] | 424 | return; |
Stepan Salenikovich | 5a12767 | 2016-09-13 11:19:50 -0400 | [diff] [blame] | 425 | #endif |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 426 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 427 | |
| 428 | gboolean |
| 429 | ring_notify_close_chat_notification( |
| 430 | #if !USE_LIBNOTIFY |
| 431 | G_GNUC_UNUSED |
| 432 | #endif |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 433 | ContactMethod *cm) |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 434 | { |
| 435 | gboolean notification_existed = FALSE; |
| 436 | |
| 437 | #if USE_LIBNOTIFY |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 438 | /* checks if there exists a chat notification associated with the given ContactMethod |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 439 | * and tries to close it; if it did exist, then the function returns TRUE */ |
Stepan Salenikovich | 26cd160 | 2016-01-20 13:43:17 -0500 | [diff] [blame] | 440 | g_return_val_if_fail(cm, FALSE); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 441 | |
| 442 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 443 | auto chat_table = ring_notify_get_chat_table(); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 444 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 445 | if (auto list = (GList *)g_hash_table_lookup(chat_table, cm)) { |
| 446 | while (list) { |
| 447 | notification_existed = TRUE; |
| 448 | auto notification = (NotifyNotification *)list->data; |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 449 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 450 | GError *error = NULL; |
| 451 | if (!notify_notification_close(notification, &error)) { |
| 452 | g_warning("could not close notification: %s", error->message); |
| 453 | g_clear_error(&error); |
| 454 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 455 | |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 456 | list = g_list_next(list); |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 457 | } |
| 458 | } |
Stepan Salenikovich | fc79fc1 | 2016-09-12 15:29:38 -0400 | [diff] [blame] | 459 | |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 460 | #endif |
| 461 | |
| 462 | return notification_existed; |
| 463 | } |