Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 2a6150d | 2017-07-19 18:24:47 -0400 | [diff] [blame] | 2 | * Copyright (C) 2016-2017 Savoir-faire Linux Inc. |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 4 | * Author: Alexandre Viau <alexandre.viau@savoirfairelinux.com> |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include "chatview.h" |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 22 | #include "utils/accounts.h" |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 23 | |
| 24 | #include <gtk/gtk.h> |
| 25 | #include <call.h> |
| 26 | #include <callmodel.h> |
| 27 | #include <contactmethod.h> |
| 28 | #include <person.h> |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 29 | #include <media/text.h> |
| 30 | #include <media/textrecording.h> |
| 31 | #include "ringnotify.h" |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 32 | #include "profilemodel.h" |
| 33 | #include "profile.h" |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 34 | #include "numbercategory.h" |
Stepan Salenikovich | 1b7100a | 2016-02-19 11:50:46 -0500 | [diff] [blame] | 35 | #include <QtCore/QDateTime> |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 36 | #include "utils/calling.h" |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 37 | #include "utils/files.h" |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 38 | #include "webkitchatcontainer.h" |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 39 | |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 40 | // LRC |
| 41 | #include <account.h> |
| 42 | |
| 43 | |
Stepan Salenikovich | ce06adb | 2016-02-19 12:53:53 -0500 | [diff] [blame] | 44 | static constexpr GdkRGBA RING_BLUE = {0.0508, 0.594, 0.676, 1.0}; // outgoing msg color: (13, 152, 173) |
| 45 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 46 | struct _ChatView |
| 47 | { |
| 48 | GtkBox parent; |
| 49 | }; |
| 50 | |
| 51 | struct _ChatViewClass |
| 52 | { |
| 53 | GtkBoxClass parent_class; |
| 54 | }; |
| 55 | |
| 56 | typedef struct _ChatViewPrivate ChatViewPrivate; |
| 57 | |
| 58 | struct _ChatViewPrivate |
| 59 | { |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 60 | GtkWidget *box_webkit_chat_container; |
| 61 | GtkWidget *webkit_chat_container; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 62 | GtkWidget *scrolledwindow_chat; |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 63 | GtkWidget *hbox_chat_info; |
| 64 | GtkWidget *label_peer; |
| 65 | GtkWidget *combobox_cm; |
AmarOk | cba0395 | 2017-07-17 10:13:49 -0400 | [diff] [blame] | 66 | GtkWidget *label_cm; |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 67 | GtkWidget *button_close_chatview; |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 68 | GtkWidget *button_placecall; |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 69 | GtkWidget *button_send_invitation; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 70 | |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 71 | GSettings *settings; |
| 72 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 73 | /* only one of the three following pointers should be non void; |
| 74 | * either this is an in-call chat (and so the in-call chat APIs will be used) |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 75 | * or it is an out of call chat (and so the account chat APIs will be used) */ |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 76 | Call *call; |
| 77 | Person *person; |
| 78 | ContactMethod *cm; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 79 | |
| 80 | QMetaObject::Connection new_message_connection; |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 81 | QMetaObject::Connection message_changed_connection; |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 82 | QMetaObject::Connection update_name; |
| 83 | QMetaObject::Connection update_send_invitation; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 84 | |
| 85 | gulong webkit_ready; |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 86 | gulong webkit_send_text; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | G_DEFINE_TYPE_WITH_PRIVATE(ChatView, chat_view, GTK_TYPE_BOX); |
| 90 | |
| 91 | #define CHAT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAT_VIEW_TYPE, ChatViewPrivate)) |
| 92 | |
| 93 | enum { |
| 94 | NEW_MESSAGES_DISPLAYED, |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 95 | HIDE_VIEW_CLICKED, |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 96 | LAST_SIGNAL |
| 97 | }; |
| 98 | |
| 99 | static guint chat_view_signals[LAST_SIGNAL] = { 0 }; |
| 100 | |
| 101 | static void |
| 102 | chat_view_dispose(GObject *object) |
| 103 | { |
| 104 | ChatView *view; |
| 105 | ChatViewPrivate *priv; |
| 106 | |
| 107 | view = CHAT_VIEW(object); |
| 108 | priv = CHAT_VIEW_GET_PRIVATE(view); |
| 109 | |
| 110 | QObject::disconnect(priv->new_message_connection); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 111 | QObject::disconnect(priv->message_changed_connection); |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 112 | QObject::disconnect(priv->update_name); |
| 113 | QObject::disconnect(priv->update_send_invitation); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 114 | |
| 115 | /* Destroying the box will also destroy its children, and we wouldn't |
| 116 | * want that. So we remove the webkit_chat_container from the box. */ |
| 117 | if (priv->webkit_chat_container) { |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 118 | /* disconnect for webkit signals */ |
| 119 | g_signal_handler_disconnect(priv->webkit_chat_container, priv->webkit_ready); |
| 120 | priv->webkit_ready = 0; |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 121 | g_signal_handler_disconnect(priv->webkit_chat_container, priv->webkit_send_text); |
| 122 | priv->webkit_send_text = 0; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 123 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 124 | gtk_container_remove( |
| 125 | GTK_CONTAINER(priv->box_webkit_chat_container), |
| 126 | GTK_WIDGET(priv->webkit_chat_container) |
| 127 | ); |
| 128 | priv->webkit_chat_container = nullptr; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 129 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 130 | } |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 131 | |
| 132 | G_OBJECT_CLASS(chat_view_parent_class)->dispose(object); |
| 133 | } |
| 134 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 135 | static void |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 136 | hide_chat_view(G_GNUC_UNUSED GtkWidget *widget, ChatView *self) |
| 137 | { |
| 138 | g_signal_emit(G_OBJECT(self), chat_view_signals[HIDE_VIEW_CLICKED], 0); |
| 139 | } |
| 140 | |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 141 | ContactMethod* |
| 142 | get_active_contactmethod(ChatView *self) |
| 143 | { |
| 144 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 145 | |
| 146 | if (priv->cm) { |
| 147 | return priv->cm; |
| 148 | } else if (priv->person) { |
| 149 | auto cms = priv->person->phoneNumbers(); |
| 150 | if (cms.size() == 1) { |
| 151 | return cms.first(); |
| 152 | } else if (cms.size() > 1) { |
| 153 | auto active = gtk_combo_box_get_active(GTK_COMBO_BOX(priv->combobox_cm)); |
| 154 | if (active >= 0 && active < cms.size()) { |
| 155 | return cms.at(active); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 163 | static void |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 164 | display_links_toggled(ChatView *self) |
| 165 | { |
| 166 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 167 | if (priv->webkit_chat_container) { |
| 168 | webkit_chat_container_set_display_links( |
| 169 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 170 | g_settings_get_boolean(priv->settings, "enable-display-links") |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 176 | placecall_clicked(ChatView *self) |
| 177 | { |
| 178 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 179 | |
| 180 | if (priv->person) { |
| 181 | // get the chosen cm |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 182 | auto cm = get_active_contactmethod(self); |
| 183 | if (cm) { |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 184 | place_new_call(cm); |
| 185 | } else { |
| 186 | g_warning("no ContactMethod chosen; cannot place call"); |
| 187 | } |
| 188 | } else if (priv->cm) { |
| 189 | place_new_call(priv->cm); |
| 190 | } else { |
| 191 | g_warning("no Person or ContactMethod set; cannot place call"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 196 | button_send_invitation_clicked(ChatView *self) |
| 197 | { |
| 198 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 199 | |
Guillaume Roguez | cea60ca | 2017-06-02 16:38:25 -0400 | [diff] [blame] | 200 | if (priv->person) { |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 201 | priv->cm = get_active_contactmethod(self); |
Guillaume Roguez | cea60ca | 2017-06-02 16:38:25 -0400 | [diff] [blame] | 202 | } |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 203 | |
| 204 | if (!priv->cm) { |
| 205 | g_warning("invalid contact, cannot send invitation!"); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // try first to use the account associated to the contact method |
| 210 | auto account = priv->cm->account(); |
| 211 | if (not account) { |
| 212 | |
| 213 | // get the choosen account |
| 214 | account = get_active_ring_account(); |
| 215 | |
| 216 | if (not account) { |
| 217 | g_warning("invalid account, cannot send invitation!"); |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // perform the request |
| 223 | if (not account->sendContactRequest(priv->cm)) |
| 224 | g_warning("contact request not forwarded, cannot send invitation!"); |
| 225 | |
| 226 | // TODO : add an entry in the conversation to tell the user an invitation was sent. |
| 227 | } |
| 228 | |
| 229 | static void |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 230 | webkit_chat_container_send_text(G_GNUC_UNUSED GtkWidget* webview, gchar *message, ChatView* self) |
| 231 | { |
| 232 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 233 | |
| 234 | /* make sure there is more than just whitespace, but if so, send the original text */ |
Sébastien Blin | f4f9028 | 2017-10-03 14:07:16 -0400 | [diff] [blame] | 235 | auto text = QString(message); |
| 236 | // NOTE for now, because we only have a send text action from the webkit. |
| 237 | if (text.startsWith("SEND:")) |
| 238 | text.remove(0, 5); |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 239 | if (!text.trimmed().isEmpty()) { |
| 240 | QMap<QString, QString> messages; |
| 241 | messages["text/plain"] = text; |
| 242 | |
| 243 | if (priv->call) { |
| 244 | // in call message |
| 245 | priv->call->addOutgoingMedia<Media::Text>()->send(messages); |
| 246 | } else if (priv->person) { |
| 247 | // get the chosen cm |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 248 | auto cm = get_active_contactmethod(self); |
| 249 | if (cm) { |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 250 | if (!cm->sendOfflineTextMessage(messages)) |
| 251 | g_warning("message failed to send"); // TODO: warn the user about this in the UI |
| 252 | } else { |
| 253 | g_warning("no ContactMethod chosen; message not sent"); |
| 254 | } |
| 255 | } else if (priv->cm) { |
| 256 | if (!priv->cm->sendOfflineTextMessage(messages)) |
| 257 | g_warning("message failed to send"); // TODO: warn the user about this in the UI |
| 258 | } else { |
| 259 | g_warning("no Call, Person, or ContactMethod set; message not sent"); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 265 | chat_view_init(ChatView *view) |
| 266 | { |
| 267 | gtk_widget_init_template(GTK_WIDGET(view)); |
| 268 | |
| 269 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(view); |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 270 | priv->settings = g_settings_new_full(get_ring_schema(), NULL, NULL); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 271 | |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 272 | g_signal_connect(priv->button_close_chatview, "clicked", G_CALLBACK(hide_chat_view), view); |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 273 | g_signal_connect_swapped(priv->button_placecall, "clicked", G_CALLBACK(placecall_clicked), view); |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 274 | g_signal_connect_swapped(priv->button_send_invitation, "clicked", G_CALLBACK(button_send_invitation_clicked), view); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static void |
| 278 | chat_view_class_init(ChatViewClass *klass) |
| 279 | { |
| 280 | G_OBJECT_CLASS(klass)->dispose = chat_view_dispose; |
| 281 | |
| 282 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 283 | "/cx/ring/RingGnome/chatview.ui"); |
| 284 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 285 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, box_webkit_chat_container); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 286 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, scrolledwindow_chat); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 287 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, hbox_chat_info); |
| 288 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, label_peer); |
AmarOk | cba0395 | 2017-07-17 10:13:49 -0400 | [diff] [blame] | 289 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, label_cm); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 290 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, combobox_cm); |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 291 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_close_chatview); |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 292 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_placecall); |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 293 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_send_invitation); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 294 | |
| 295 | chat_view_signals[NEW_MESSAGES_DISPLAYED] = g_signal_new ( |
| 296 | "new-messages-displayed", |
| 297 | G_TYPE_FROM_CLASS(klass), |
| 298 | (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 299 | 0, |
| 300 | nullptr, |
| 301 | nullptr, |
| 302 | g_cclosure_marshal_VOID__VOID, |
| 303 | G_TYPE_NONE, 0); |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 304 | |
| 305 | chat_view_signals[HIDE_VIEW_CLICKED] = g_signal_new ( |
| 306 | "hide-view-clicked", |
| 307 | G_TYPE_FROM_CLASS(klass), |
| 308 | (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 309 | 0, |
| 310 | nullptr, |
| 311 | nullptr, |
| 312 | g_cclosure_marshal_VOID__VOID, |
| 313 | G_TYPE_NONE, 0); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 314 | } |
| 315 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 316 | |
| 317 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 318 | static void |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 319 | print_message_to_buffer(ChatView* self, const QModelIndex &idx) |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 320 | { |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 321 | if (!idx.isValid()) { |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 322 | g_warning("QModelIndex in im model is not valid"); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 323 | return; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 324 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 325 | |
| 326 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 327 | |
| 328 | webkit_chat_container_print_new_message( |
| 329 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 330 | idx |
| 331 | ); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 332 | } |
| 333 | |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 334 | static void |
| 335 | set_participant_images(ChatView* self) |
| 336 | { |
| 337 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 338 | |
| 339 | webkit_chat_container_clear_sender_images( |
| 340 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container) |
| 341 | ); |
| 342 | |
| 343 | /* Set the sender image for the peer */ |
| 344 | ContactMethod* sender_contact_method_peer; |
| 345 | QVariant photo_variant_peer; |
| 346 | |
| 347 | if (priv->person) |
| 348 | { |
| 349 | photo_variant_peer = priv->person->photo(); |
| 350 | sender_contact_method_peer = get_active_contactmethod(self); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | if (priv->cm) |
| 355 | { |
| 356 | sender_contact_method_peer = priv->cm; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | sender_contact_method_peer = priv->call->peerContactMethod(); |
| 361 | } |
| 362 | photo_variant_peer = sender_contact_method_peer->roleData((int) Call::Role::Photo); |
| 363 | } |
| 364 | |
| 365 | if (photo_variant_peer.isValid()) |
| 366 | { |
| 367 | webkit_chat_container_set_sender_image( |
| 368 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 369 | sender_contact_method_peer, |
| 370 | photo_variant_peer |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | /* set sender image for "ME" */ |
| 375 | auto profile = ProfileModel::instance().selectedProfile(); |
| 376 | if (profile) |
| 377 | { |
| 378 | auto person = profile->person(); |
| 379 | if (person) |
| 380 | { |
| 381 | auto photo_variant_me = person->photo(); |
| 382 | if (photo_variant_me.isValid()) |
| 383 | { |
| 384 | webkit_chat_container_set_sender_image( |
| 385 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 386 | nullptr, |
| 387 | photo_variant_me |
| 388 | ); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 394 | static void |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 395 | print_text_recording(Media::TextRecording *recording, ChatView *self) |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 396 | { |
| 397 | g_return_if_fail(IS_CHAT_VIEW(self)); |
| 398 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 399 | |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 400 | /* set the photos of the chat participants */ |
| 401 | set_participant_images(self); |
| 402 | |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 403 | /* only text messages are supported for now */ |
| 404 | auto model = recording->instantTextMessagingModel(); |
| 405 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 406 | /* new model, disconnect from the old model updates and clear the text buffer */ |
| 407 | QObject::disconnect(priv->new_message_connection); |
| 408 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 409 | /* put all the messages in the im model into the text view */ |
| 410 | for (int row = 0; row < model->rowCount(); ++row) { |
| 411 | QModelIndex idx = model->index(row, 0); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 412 | print_message_to_buffer(self, idx); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 413 | } |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 414 | /* mark all messages as read */ |
| 415 | recording->setAllRead(); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 416 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 417 | |
| 418 | /* messages modified */ |
| 419 | /* connecting on instantMessagingModel and not textMessagingModel */ |
| 420 | priv->message_changed_connection = QObject::connect( |
| 421 | model, |
| 422 | &QAbstractItemModel::dataChanged, |
| 423 | [self, priv] (const QModelIndex & topLeft, G_GNUC_UNUSED const QModelIndex & bottomRight) |
| 424 | { |
| 425 | webkit_chat_container_update_message( |
| 426 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 427 | topLeft |
| 428 | ); |
| 429 | } |
| 430 | ); |
| 431 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 432 | /* append new messages */ |
| 433 | priv->new_message_connection = QObject::connect( |
| 434 | model, |
| 435 | &QAbstractItemModel::rowsInserted, |
| 436 | [self, priv, model] (const QModelIndex &parent, int first, int last) { |
| 437 | for (int row = first; row <= last; ++row) { |
| 438 | QModelIndex idx = model->index(row, 0, parent); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 439 | print_message_to_buffer(self, idx); |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 440 | /* make sure these messages are marked as read */ |
| 441 | model->setData(idx, true, static_cast<int>(Media::TextRecording::Role::IsRead)); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 442 | g_signal_emit(G_OBJECT(self), chat_view_signals[NEW_MESSAGES_DISPLAYED], 0); |
| 443 | } |
| 444 | } |
| 445 | ); |
| 446 | } |
| 447 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 448 | static void |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 449 | update_send_invitation(ChatView *self) |
| 450 | { |
| 451 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 452 | |
| 453 | auto cm = get_active_contactmethod(self); |
| 454 | |
| 455 | if (cm && cm->isConfirmed()) { |
| 456 | gtk_widget_hide(priv->button_send_invitation); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static void |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 461 | selected_cm_changed(ChatView *self) |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 462 | { |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 463 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 464 | |
| 465 | /* make sure the webkit view is ready, in case we get this signal before it is */ |
| 466 | if (!webkit_chat_container_is_ready(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container))) |
| 467 | return; |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 468 | |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 469 | auto cm = get_active_contactmethod(self); |
| 470 | if (cm){ |
| 471 | print_text_recording(cm->textRecording(), self); |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 472 | |
| 473 | QObject::disconnect(priv->update_send_invitation); |
| 474 | priv->update_send_invitation = QObject::connect( |
| 475 | get_active_contactmethod(self), |
| 476 | &ContactMethod::changed, |
| 477 | [self] () { update_send_invitation(self); } |
| 478 | ); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 479 | } else { |
| 480 | g_warning("no valid ContactMethod selected to display chat conversation"); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 485 | render_contact_method(G_GNUC_UNUSED GtkCellLayout *cell_layout, |
| 486 | GtkCellRenderer *cell, |
| 487 | GtkTreeModel *model, |
| 488 | GtkTreeIter *iter, |
| 489 | G_GNUC_UNUSED gpointer data) |
| 490 | { |
| 491 | GValue value = G_VALUE_INIT; |
| 492 | gtk_tree_model_get_value(model, iter, 0, &value); |
| 493 | auto cm = (ContactMethod *)g_value_get_pointer(&value); |
| 494 | |
| 495 | gchar *number = nullptr; |
| 496 | if (cm && cm->category()) { |
| 497 | // try to get the number category, eg: "home" |
| 498 | number = g_strdup_printf("(%s) %s", cm->category()->name().toUtf8().constData(), |
Houmin | beab3c9 | 2017-05-16 11:12:16 +0800 | [diff] [blame] | 499 | cm->bestId().toUtf8().constData()); |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 500 | } else if (cm) { |
Houmin | beab3c9 | 2017-05-16 11:12:16 +0800 | [diff] [blame] | 501 | number = g_strdup_printf("%s", cm->bestId().toUtf8().constData()); |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | g_object_set(G_OBJECT(cell), "text", number, NULL); |
| 505 | g_free(number); |
| 506 | } |
| 507 | |
| 508 | static void |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 509 | update_contact_methods(ChatView *self) |
| 510 | { |
| 511 | g_return_if_fail(IS_CHAT_VIEW(self)); |
| 512 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 513 | |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 514 | g_return_if_fail(priv->call || priv->person || priv->cm); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 515 | |
| 516 | /* model for the combobox for the choice of ContactMethods */ |
| 517 | auto cm_model = gtk_list_store_new( |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 518 | 1, G_TYPE_POINTER |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 519 | ); |
| 520 | |
Stepan Salenikovich | bbfa440 | 2016-05-04 15:07:29 -0400 | [diff] [blame] | 521 | Person::ContactMethods cms; |
| 522 | |
| 523 | if (priv->person) |
| 524 | cms = priv->person->phoneNumbers(); |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 525 | else if (priv->cm) |
Stepan Salenikovich | bbfa440 | 2016-05-04 15:07:29 -0400 | [diff] [blame] | 526 | cms << priv->cm; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 527 | else if (priv->call) |
| 528 | cms << priv->call->peerContactMethod(); |
Stepan Salenikovich | bbfa440 | 2016-05-04 15:07:29 -0400 | [diff] [blame] | 529 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 530 | for (int i = 0; i < cms.size(); ++i) { |
| 531 | GtkTreeIter iter; |
| 532 | gtk_list_store_append(cm_model, &iter); |
| 533 | gtk_list_store_set(cm_model, &iter, |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 534 | 0, cms.at(i), |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 535 | -1); |
| 536 | } |
| 537 | |
| 538 | gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_cm), GTK_TREE_MODEL(cm_model)); |
| 539 | g_object_unref(cm_model); |
| 540 | |
| 541 | auto renderer = gtk_cell_renderer_text_new(); |
| 542 | g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); |
| 543 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_cm), renderer, FALSE); |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 544 | gtk_cell_layout_set_cell_data_func( |
| 545 | GTK_CELL_LAYOUT(priv->combobox_cm), |
| 546 | renderer, |
| 547 | (GtkCellLayoutDataFunc)render_contact_method, |
| 548 | nullptr, nullptr); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 549 | |
| 550 | /* select the last used cm */ |
| 551 | if (!cms.isEmpty()) { |
| 552 | auto last_used_cm = cms.at(0); |
| 553 | int last_used_cm_idx = 0; |
| 554 | for (int i = 1; i < cms.size(); ++i) { |
| 555 | auto new_cm = cms.at(i); |
| 556 | if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0) { |
| 557 | last_used_cm = new_cm; |
| 558 | last_used_cm_idx = i; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_cm), last_used_cm_idx); |
Guillaume Roguez | d35d649 | 2017-06-26 16:40:09 -0400 | [diff] [blame] | 563 | |
| 564 | if (last_used_cm->isConfirmed()) |
| 565 | gtk_widget_hide(priv->button_send_invitation); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Stepan Salenikovich | bbfa440 | 2016-05-04 15:07:29 -0400 | [diff] [blame] | 568 | /* if there is only one CM, make the combo box insensitive */ |
AmarOk | cba0395 | 2017-07-17 10:13:49 -0400 | [diff] [blame] | 569 | if (cms.size() < 2) { |
| 570 | auto active = cms.at(gtk_combo_box_get_active(GTK_COMBO_BOX(priv->combobox_cm))); |
| 571 | std::string ring_id = active->roleData(static_cast<int>(Ring::Role::Number)).toString().toUtf8().constData(); |
| 572 | std::string displayed = gtk_label_get_text(GTK_LABEL(priv->label_peer)); |
| 573 | gtk_label_set_text(GTK_LABEL(priv->label_cm), ring_id.c_str()); |
| 574 | gtk_widget_hide(priv->combobox_cm); |
| 575 | if (displayed == ring_id) { |
| 576 | gtk_widget_hide(priv->label_cm); |
| 577 | } else { |
| 578 | gtk_widget_show(priv->label_cm); |
| 579 | } |
| 580 | } else { |
| 581 | gtk_widget_show(priv->combobox_cm); |
| 582 | gtk_widget_hide(priv->label_cm); |
| 583 | } |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 584 | |
| 585 | /* if no CMs make the call button insensitive */ |
| 586 | gtk_widget_set_sensitive(priv->button_placecall, !cms.isEmpty()); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static void |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 590 | update_name(ChatView *self) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 591 | { |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 592 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 593 | |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 594 | g_return_if_fail(priv->person || priv->cm || priv->call); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 595 | |
| 596 | QString name; |
| 597 | if (priv->person) { |
| 598 | name = priv->person->roleData(static_cast<int>(Ring::Role::Name)).toString(); |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 599 | } else if (priv->cm) { |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 600 | name = priv->cm->roleData(static_cast<int>(Ring::Role::Name)).toString(); |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 601 | } else if (priv->call) { |
| 602 | name = priv->call->peerContactMethod()->roleData(static_cast<int>(Ring::Role::Name)).toString(); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 603 | } |
| 604 | gtk_label_set_text(GTK_LABEL(priv->label_peer), name.toUtf8().constData()); |
| 605 | } |
| 606 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 607 | static void |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 608 | webkit_chat_container_ready(ChatView* self) |
| 609 | { |
| 610 | /* The webkit chat container has loaded the javascript libraries, we can |
| 611 | * now use it. */ |
| 612 | |
| 613 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 614 | |
| 615 | webkit_chat_container_clear( |
| 616 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container) |
| 617 | ); |
| 618 | |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 619 | display_links_toggled(self); |
| 620 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 621 | /* print the text recordings */ |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 622 | if (priv->call) { |
| 623 | print_text_recording(priv->call->peerContactMethod()->textRecording(), self); |
| 624 | } else if (priv->cm) { |
| 625 | print_text_recording(priv->cm->textRecording(), self); |
| 626 | } else if (priv->person) { |
| 627 | /* get the selected cm and print the text recording */ |
| 628 | selected_cm_changed(self); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 629 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static void |
| 633 | build_chat_view(ChatView* self) |
| 634 | { |
| 635 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 636 | |
| 637 | gtk_container_add(GTK_CONTAINER(priv->box_webkit_chat_container), priv->webkit_chat_container); |
| 638 | gtk_widget_show(priv->webkit_chat_container); |
| 639 | |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 640 | /* keep name updated */ |
| 641 | if (priv->call) { |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 642 | priv->update_name = QObject::connect( |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 643 | priv->call, |
| 644 | &Call::changed, |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 645 | [self] () { update_name(self); } |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 646 | ); |
| 647 | } else if (priv->cm) { |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 648 | priv->update_name = QObject::connect( |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 649 | priv->cm, |
| 650 | &ContactMethod::changed, |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 651 | [self] () { update_name(self); } |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 652 | ); |
| 653 | } else if (priv->person) { |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 654 | priv->update_name = QObject::connect( |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 655 | priv->person, |
| 656 | &Person::changed, |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 657 | [self] () { update_name(self); } |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 658 | ); |
| 659 | } |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 660 | |
| 661 | priv->update_send_invitation = QObject::connect( |
| 662 | get_active_contactmethod(self), |
| 663 | &ContactMethod::changed, |
| 664 | [self] () { update_send_invitation(self); } |
| 665 | ); |
| 666 | |
| 667 | update_name(self); |
| 668 | update_send_invitation(self); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 669 | |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 670 | /* keep selected cm updated */ |
| 671 | update_contact_methods(self); |
| 672 | g_signal_connect_swapped(priv->combobox_cm, "changed", G_CALLBACK(selected_cm_changed), self); |
| 673 | |
| 674 | priv->webkit_ready = g_signal_connect_swapped( |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 675 | priv->webkit_chat_container, |
| 676 | "ready", |
| 677 | G_CALLBACK(webkit_chat_container_ready), |
| 678 | self |
| 679 | ); |
| 680 | |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 681 | priv->webkit_send_text = g_signal_connect(priv->webkit_chat_container, |
| 682 | "send-message", |
| 683 | G_CALLBACK(webkit_chat_container_send_text), |
| 684 | self); |
| 685 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 686 | if (webkit_chat_container_is_ready(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container))) |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 687 | webkit_chat_container_ready(self); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 688 | |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 689 | /* we only show the chat info in the case of cm / person */ |
| 690 | gtk_widget_set_visible(priv->hbox_chat_info, (priv->cm || priv->person)); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 693 | GtkWidget * |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 694 | chat_view_new_call(WebKitChatContainer *webkit_chat_container, Call *call) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 695 | { |
| 696 | g_return_val_if_fail(call, nullptr); |
| 697 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 698 | ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL)); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 699 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 700 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 701 | priv->webkit_chat_container = GTK_WIDGET(webkit_chat_container); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 702 | priv->call = call; |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 703 | |
| 704 | build_chat_view(self); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 705 | |
| 706 | return (GtkWidget *)self; |
| 707 | } |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 708 | |
| 709 | GtkWidget * |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 710 | chat_view_new_cm(WebKitChatContainer *webkit_chat_container, ContactMethod *cm) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 711 | { |
| 712 | g_return_val_if_fail(cm, nullptr); |
| 713 | |
| 714 | ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL)); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 715 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 716 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 717 | priv->webkit_chat_container = GTK_WIDGET(webkit_chat_container); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 718 | priv->cm = cm; |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 719 | |
| 720 | build_chat_view(self); |
| 721 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 722 | return (GtkWidget *)self; |
| 723 | } |
| 724 | |
| 725 | GtkWidget * |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 726 | chat_view_new_person(WebKitChatContainer *webkit_chat_container, Person *p) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 727 | { |
| 728 | g_return_val_if_fail(p, nullptr); |
| 729 | |
| 730 | ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL)); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 731 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 732 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 733 | priv->webkit_chat_container = GTK_WIDGET(webkit_chat_container); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 734 | priv->person = p; |
| 735 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 736 | build_chat_view(self); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 737 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 738 | return (GtkWidget *)self; |
| 739 | } |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 740 | |
| 741 | Call* |
| 742 | chat_view_get_call(ChatView *self) |
| 743 | { |
| 744 | g_return_val_if_fail(IS_CHAT_VIEW(self), nullptr); |
| 745 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 746 | |
| 747 | return priv->call; |
| 748 | } |
| 749 | |
| 750 | ContactMethod* |
| 751 | chat_view_get_cm(ChatView *self) |
| 752 | { |
| 753 | g_return_val_if_fail(IS_CHAT_VIEW(self), nullptr); |
| 754 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 755 | |
| 756 | return priv->cm; |
| 757 | } |
| 758 | |
| 759 | Person* |
| 760 | chat_view_get_person(ChatView *self) |
| 761 | { |
| 762 | g_return_val_if_fail(IS_CHAT_VIEW(self), nullptr); |
| 763 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 764 | |
| 765 | return priv->person; |
| 766 | } |
Stepan Salenikovich | daf3cb3 | 2016-10-12 16:39:42 -0400 | [diff] [blame] | 767 | |
| 768 | void |
| 769 | chat_view_set_header_visible(ChatView *self, gboolean visible) |
| 770 | { |
| 771 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 772 | |
| 773 | gtk_widget_set_visible(priv->hbox_chat_info, visible); |
| 774 | } |