Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 77c579d | 2018-01-30 15:54:02 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2018 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> |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 5 | * Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> |
| 6 | * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "chatview.h" |
| 24 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 25 | // std |
| 26 | #include <algorithm> |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 27 | |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 28 | // GTK |
| 29 | #include <glib/gi18n.h> |
| 30 | |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 31 | // LRC |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 32 | #include <api/contactmodel.h> |
| 33 | #include <api/conversationmodel.h> |
| 34 | #include <api/contact.h> |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 35 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 36 | // Client |
| 37 | #include "utils/files.h" |
Stepan Salenikovich | ce06adb | 2016-02-19 12:53:53 -0500 | [diff] [blame] | 38 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 39 | struct _ChatView |
| 40 | { |
| 41 | GtkBox parent; |
| 42 | }; |
| 43 | |
| 44 | struct _ChatViewClass |
| 45 | { |
| 46 | GtkBoxClass parent_class; |
| 47 | }; |
| 48 | |
| 49 | typedef struct _ChatViewPrivate ChatViewPrivate; |
| 50 | |
| 51 | struct _ChatViewPrivate |
| 52 | { |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 53 | GtkWidget *box_webkit_chat_container; |
| 54 | GtkWidget *webkit_chat_container; |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 55 | GtkWidget *hbox_chat_info; |
| 56 | GtkWidget *label_peer; |
AmarOk | cba0395 | 2017-07-17 10:13:49 -0400 | [diff] [blame] | 57 | GtkWidget *label_cm; |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 58 | GtkWidget *button_close_chatview; |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 59 | GtkWidget *button_placecall; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 60 | GtkWidget *button_add_to_conversations; |
Nicolas Jager | d29ae95 | 2017-12-04 16:00:12 -0500 | [diff] [blame] | 61 | GtkWidget *button_place_audio_call; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 62 | |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 63 | GSettings *settings; |
| 64 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 65 | lrc::api::conversation::Info* conversation_; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 66 | AccountInfoPointer const * accountInfo_; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 67 | bool isTemporary_; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 68 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 69 | QMetaObject::Connection new_interaction_connection; |
| 70 | QMetaObject::Connection update_interaction_connection; |
| 71 | QMetaObject::Connection update_add_to_conversations; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 72 | |
| 73 | gulong webkit_ready; |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 74 | gulong webkit_send_text; |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | G_DEFINE_TYPE_WITH_PRIVATE(ChatView, chat_view, GTK_TYPE_BOX); |
| 78 | |
| 79 | #define CHAT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAT_VIEW_TYPE, ChatViewPrivate)) |
| 80 | |
| 81 | enum { |
| 82 | NEW_MESSAGES_DISPLAYED, |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 83 | HIDE_VIEW_CLICKED, |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 84 | LAST_SIGNAL |
| 85 | }; |
| 86 | |
| 87 | static guint chat_view_signals[LAST_SIGNAL] = { 0 }; |
| 88 | |
| 89 | static void |
| 90 | chat_view_dispose(GObject *object) |
| 91 | { |
| 92 | ChatView *view; |
| 93 | ChatViewPrivate *priv; |
| 94 | |
| 95 | view = CHAT_VIEW(object); |
| 96 | priv = CHAT_VIEW_GET_PRIVATE(view); |
| 97 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 98 | QObject::disconnect(priv->new_interaction_connection); |
| 99 | QObject::disconnect(priv->update_interaction_connection); |
| 100 | QObject::disconnect(priv->update_add_to_conversations); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 101 | |
| 102 | /* Destroying the box will also destroy its children, and we wouldn't |
| 103 | * want that. So we remove the webkit_chat_container from the box. */ |
| 104 | if (priv->webkit_chat_container) { |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 105 | /* disconnect for webkit signals */ |
| 106 | g_signal_handler_disconnect(priv->webkit_chat_container, priv->webkit_ready); |
| 107 | priv->webkit_ready = 0; |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 108 | g_signal_handler_disconnect(priv->webkit_chat_container, priv->webkit_send_text); |
| 109 | priv->webkit_send_text = 0; |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 110 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 111 | gtk_container_remove( |
| 112 | GTK_CONTAINER(priv->box_webkit_chat_container), |
| 113 | GTK_WIDGET(priv->webkit_chat_container) |
| 114 | ); |
| 115 | priv->webkit_chat_container = nullptr; |
| 116 | } |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 117 | |
| 118 | G_OBJECT_CLASS(chat_view_parent_class)->dispose(object); |
| 119 | } |
| 120 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 121 | static void |
Stepan Salenikovich | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 122 | hide_chat_view(G_GNUC_UNUSED GtkWidget *widget, ChatView *self) |
| 123 | { |
| 124 | g_signal_emit(G_OBJECT(self), chat_view_signals[HIDE_VIEW_CLICKED], 0); |
| 125 | } |
| 126 | |
| 127 | static void |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 128 | display_links_toggled(ChatView *self) |
| 129 | { |
| 130 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 131 | if (priv->webkit_chat_container) { |
| 132 | webkit_chat_container_set_display_links( |
| 133 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 134 | g_settings_get_boolean(priv->settings, "enable-display-links") |
| 135 | ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 140 | placecall_clicked(ChatView *self) |
| 141 | { |
| 142 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 143 | if (!priv->conversation_) return; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 144 | (*priv->accountInfo_)->conversationModel->placeCall(priv->conversation_->uid); |
Stepan Salenikovich | e993324 | 2016-06-21 18:08:48 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void |
Nicolas Jager | d29ae95 | 2017-12-04 16:00:12 -0500 | [diff] [blame] | 148 | place_audio_call_clicked(ChatView *self) |
| 149 | { |
| 150 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 151 | |
| 152 | if (!priv->conversation_) |
| 153 | return; |
| 154 | |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 155 | (*priv->accountInfo_)->conversationModel->placeAudioOnlyCall(priv->conversation_->uid); |
Nicolas Jager | d29ae95 | 2017-12-04 16:00:12 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 159 | button_add_to_conversations_clicked(ChatView *self) |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 160 | { |
| 161 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 162 | if (!priv->conversation_) return; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 163 | (*priv->accountInfo_)->conversationModel->makePermanent(priv->conversation_->uid); |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 166 | static gchar* |
Sébastien Blin | 8fca81d | 2018-03-02 14:55:25 -0500 | [diff] [blame] | 167 | file_to_manipulate(GtkWindow* top_window, bool send) |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 168 | { |
| 169 | GtkWidget* dialog; |
| 170 | GtkFileChooserAction action = send? GTK_FILE_CHOOSER_ACTION_OPEN : GTK_FILE_CHOOSER_ACTION_SAVE; |
| 171 | gint res; |
| 172 | gchar* filename = nullptr; |
| 173 | |
| 174 | dialog = gtk_file_chooser_dialog_new(send? _("Send File") : _("Save File"), |
| 175 | top_window, |
| 176 | action, |
| 177 | _("_Cancel"), |
| 178 | GTK_RESPONSE_CANCEL, |
| 179 | send? _("_Open"): _("_Save"), |
| 180 | GTK_RESPONSE_ACCEPT, |
| 181 | nullptr); |
| 182 | |
| 183 | res = gtk_dialog_run (GTK_DIALOG(dialog)); |
| 184 | |
| 185 | if (res == GTK_RESPONSE_ACCEPT) { |
| 186 | auto chooser = GTK_FILE_CHOOSER(dialog); |
| 187 | filename = gtk_file_chooser_get_filename(chooser); |
| 188 | } |
| 189 | gtk_widget_destroy (dialog); |
| 190 | |
| 191 | return filename; |
| 192 | } |
| 193 | |
Nicolas Jager | 759faea | 2017-03-22 16:22:00 -0400 | [diff] [blame] | 194 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 195 | webkit_chat_container_script_dialog(G_GNUC_UNUSED GtkWidget* webview, gchar *interaction, ChatView* self) |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 196 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 197 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 198 | auto order = std::string(interaction); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 199 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 200 | if (order == "ACCEPT") { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 201 | (*priv->accountInfo_)->conversationModel->makePermanent(priv->conversation_->uid); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 202 | } else if (order == "REFUSE") { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 203 | (*priv->accountInfo_)->conversationModel->removeConversation(priv->conversation_->uid); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 204 | } else if (order == "BLOCK") { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 205 | (*priv->accountInfo_)->conversationModel->removeConversation(priv->conversation_->uid, true); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 206 | } else if (order.find("SEND:") == 0) { |
| 207 | // Get text body |
| 208 | auto toSend = order.substr(std::string("SEND:").size()); |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 209 | (*priv->accountInfo_)->conversationModel->sendMessage(priv->conversation_->uid, toSend); |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 210 | } else if (order.find("SEND_FILE") == 0) { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 211 | if (auto model = (*priv->accountInfo_)->conversationModel.get()) { |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 212 | if (auto filename = file_to_manipulate(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(self))), true)) |
| 213 | model->sendFile(priv->conversation_->uid, filename, g_path_get_basename(filename)); |
| 214 | } |
| 215 | } else if (order.find("ACCEPT_FILE:") == 0) { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 216 | if (auto model = (*priv->accountInfo_)->conversationModel.get()) { |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 217 | try { |
| 218 | auto interactionId = std::stoull(order.substr(std::string("ACCEPT_FILE:").size())); |
Sébastien Blin | afbc959 | 2018-02-28 13:52:20 -0500 | [diff] [blame] | 219 | |
| 220 | lrc::api::datatransfer::Info info = {}; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 221 | (*priv->accountInfo_)->conversationModel->getTransferInfo(interactionId, info); |
Sébastien Blin | afbc959 | 2018-02-28 13:52:20 -0500 | [diff] [blame] | 222 | |
Sébastien Blin | 8fca81d | 2018-03-02 14:55:25 -0500 | [diff] [blame] | 223 | // get prefered directory destination. |
| 224 | auto* download_directory_variant = g_settings_get_value(priv->settings, "download-folder"); |
| 225 | char* download_directory_value; |
| 226 | g_variant_get(download_directory_variant, "&s", &download_directory_value); |
| 227 | std::string default_download_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD); |
| 228 | auto current_value = std::string(download_directory_value); |
| 229 | if (current_value.empty()) { |
| 230 | g_settings_set_value(priv->settings, "download-folder", g_variant_new("s", default_download_dir.c_str())); |
| 231 | } |
| 232 | // get full path |
| 233 | std::string filename = current_value.empty()? default_download_dir.c_str() : download_directory_value; |
| 234 | if (!filename.empty() && filename.back() != '/') filename += "/"; |
| 235 | filename += info.displayName; |
| 236 | |
| 237 | model->acceptTransfer(priv->conversation_->uid, interactionId, filename); |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 238 | } catch (...) { |
| 239 | // ignore |
| 240 | } |
| 241 | } |
| 242 | } else if (order.find("REFUSE_FILE:") == 0) { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 243 | if (auto model = (*priv->accountInfo_)->conversationModel.get()) { |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 244 | try { |
| 245 | auto interactionId = std::stoull(order.substr(std::string("REFUSE_FILE:").size())); |
| 246 | model->cancelTransfer(priv->conversation_->uid, interactionId); |
| 247 | } catch (...) { |
| 248 | // ignore |
| 249 | } |
| 250 | } |
| 251 | } else if (order.find("OPEN_FILE:") == 0) { |
| 252 | // Get text body |
| 253 | auto filename {"file://" + order.substr(std::string("OPEN_FILE:").size())}; |
| 254 | filename.erase(std::find_if(filename.rbegin(), filename.rend(), |
| 255 | std::not1(std::ptr_fun<int, int>(std::isspace))).base(), filename.end()); |
| 256 | GError* error = nullptr; |
| 257 | if (!gtk_show_uri(nullptr, filename.c_str(), GDK_CURRENT_TIME, &error)) { |
| 258 | g_debug("Could not open file: %s", error->message); |
| 259 | g_error_free(error); |
| 260 | } |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 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); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 274 | g_signal_connect_swapped(priv->button_add_to_conversations, "clicked", G_CALLBACK(button_add_to_conversations_clicked), view); |
Nicolas Jager | d29ae95 | 2017-12-04 16:00:12 -0500 | [diff] [blame] | 275 | g_signal_connect_swapped(priv->button_place_audio_call, "clicked", G_CALLBACK(place_audio_call_clicked), view); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static void |
| 279 | chat_view_class_init(ChatViewClass *klass) |
| 280 | { |
| 281 | G_OBJECT_CLASS(klass)->dispose = chat_view_dispose; |
| 282 | |
| 283 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 284 | "/cx/ring/RingGnome/chatview.ui"); |
| 285 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 286 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, box_webkit_chat_container); |
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 | 8043a56 | 2016-03-18 13:56:40 -0400 | [diff] [blame] | 290 | 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] | 291 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_placecall); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 292 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_add_to_conversations); |
Nicolas Jager | d29ae95 | 2017-12-04 16:00:12 -0500 | [diff] [blame] | 293 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_place_audio_call); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 294 | |
| 295 | chat_view_signals[NEW_MESSAGES_DISPLAYED] = g_signal_new ( |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 296 | "new-interactions-displayed", |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 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 | |
| 316 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 317 | print_interaction_to_buffer(ChatView* self, uint64_t interactionId, const lrc::api::interaction::Info& interaction) |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 318 | { |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 319 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 320 | |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 321 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 322 | if (interaction.status == lrc::api::interaction::Status::UNREAD) |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 323 | (*priv->accountInfo_)->conversationModel->setInteractionRead(priv->conversation_->uid, interactionId); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 324 | |
| 325 | webkit_chat_container_print_new_interaction( |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 326 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 327 | *(*priv->accountInfo_)->conversationModel, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 328 | interactionId, |
| 329 | interaction |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 330 | ); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 331 | } |
| 332 | |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 333 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 334 | update_interaction(ChatView* self, uint64_t interactionId, const lrc::api::interaction::Info& interaction) |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 335 | { |
| 336 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 337 | webkit_chat_container_update_interaction( |
| 338 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 339 | *(*priv->accountInfo_)->conversationModel, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 340 | interactionId, |
| 341 | interaction |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 342 | ); |
aviau | fc21355 | 2016-11-01 12:39:39 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 345 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 346 | load_participants_images(ChatView *self) |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 347 | { |
| 348 | g_return_if_fail(IS_CHAT_VIEW(self)); |
| 349 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 350 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 351 | // Contact |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 352 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 353 | auto contactUri = priv->conversation_->participants.front(); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 354 | try{ |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 355 | auto& contact = (*priv->accountInfo_)->contactModel->getContact(contactUri); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 356 | if (!contact.profileInfo.avatar.empty()) { |
| 357 | webkit_chat_container_set_sender_image( |
| 358 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 359 | (*priv->accountInfo_)->contactModel->getContactProfileId(contactUri), |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 360 | contact.profileInfo.avatar |
| 361 | ); |
| 362 | } |
| 363 | } catch (const std::out_of_range&) { |
| 364 | // ContactModel::getContact() exception |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 367 | // For this account |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 368 | if (!(*priv->accountInfo_)->profileInfo.avatar.empty()) { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 369 | webkit_chat_container_set_sender_image( |
| 370 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 371 | (*priv->accountInfo_)->contactModel->getContactProfileId((*priv->accountInfo_)->profileInfo.uri), |
| 372 | (*priv->accountInfo_)->profileInfo.avatar |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 373 | ); |
| 374 | } |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 377 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 378 | print_text_recording(ChatView *self) |
| 379 | { |
| 380 | g_return_if_fail(IS_CHAT_VIEW(self)); |
| 381 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 382 | |
Sébastien Blin | 162eb0f | 2018-02-27 10:14:53 -0500 | [diff] [blame] | 383 | // Read interactions |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 384 | if (!priv->conversation_) return; |
Sébastien Blin | 162eb0f | 2018-02-27 10:14:53 -0500 | [diff] [blame] | 385 | for (const auto& it: priv->conversation_->interactions) { |
| 386 | if (it.second.status == lrc::api::interaction::Status::UNREAD) |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 387 | (*priv->accountInfo_)->conversationModel->setInteractionRead(priv->conversation_->uid, it.first); |
Sébastien Blin | 162eb0f | 2018-02-27 10:14:53 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Sébastien Blin | 05317a7 | 2018-02-21 11:09:16 -0500 | [diff] [blame] | 390 | webkit_chat_container_print_history( |
| 391 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 392 | *(*priv->accountInfo_)->conversationModel, |
Sébastien Blin | 05317a7 | 2018-02-21 11:09:16 -0500 | [diff] [blame] | 393 | priv->conversation_->interactions |
| 394 | ); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 395 | |
| 396 | QObject::disconnect(priv->new_interaction_connection); |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | update_add_to_conversations(ChatView *self) |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 401 | { |
| 402 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 403 | |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 404 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 405 | auto participant = priv->conversation_->participants[0]; |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 406 | try { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 407 | auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(participant); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 408 | if(contactInfo.profileInfo.type != lrc::api::profile::Type::TEMPORARY |
| 409 | && contactInfo.profileInfo.type != lrc::api::profile::Type::PENDING) |
| 410 | gtk_widget_hide(priv->button_add_to_conversations); |
| 411 | } catch (const std::out_of_range&) { |
| 412 | // ContactModel::getContact() exception |
| 413 | } |
Stepan Salenikovich | 5039c9b | 2016-02-12 14:09:51 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 417 | update_contact_methods(ChatView *self) |
| 418 | { |
| 419 | g_return_if_fail(IS_CHAT_VIEW(self)); |
| 420 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 421 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 422 | auto contactUri = priv->conversation_->participants.front(); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 423 | try { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 424 | auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(contactUri); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 425 | auto bestId = std::string(contactInfo.registeredName).empty() ? contactInfo.profileInfo.uri : contactInfo.registeredName; |
| 426 | if (contactInfo.profileInfo.alias == bestId) { |
| 427 | gtk_widget_hide(priv->label_cm); |
| 428 | } else { |
| 429 | gtk_label_set_text(GTK_LABEL(priv->label_cm), bestId.c_str()); |
| 430 | gtk_widget_show(priv->label_cm); |
| 431 | } |
| 432 | } catch (const std::out_of_range&) { |
| 433 | // ContactModel::getContact() exception |
AmarOk | cba0395 | 2017-07-17 10:13:49 -0400 | [diff] [blame] | 434 | } |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 438 | update_name(ChatView *self) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 439 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 440 | g_return_if_fail(IS_CHAT_VIEW(self)); |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 441 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 442 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 443 | auto contactUri = priv->conversation_->participants.front(); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 444 | try { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 445 | auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(contactUri); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 446 | auto alias = contactInfo.profileInfo.alias; |
| 447 | alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end()); |
| 448 | gtk_label_set_text(GTK_LABEL(priv->label_peer), alias.c_str()); |
| 449 | } catch (const std::out_of_range&) { |
| 450 | // ContactModel::getContact() exception |
| 451 | } |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 452 | } |
| 453 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 454 | static void |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 455 | webkit_chat_container_ready(ChatView* self) |
| 456 | { |
| 457 | /* The webkit chat container has loaded the javascript libraries, we can |
| 458 | * now use it. */ |
| 459 | |
| 460 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 461 | |
| 462 | webkit_chat_container_clear( |
| 463 | WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container) |
| 464 | ); |
| 465 | |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 466 | display_links_toggled(self); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 467 | print_text_recording(self); |
| 468 | load_participants_images(self); |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 469 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 470 | priv->new_interaction_connection = QObject::connect( |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 471 | &*(*priv->accountInfo_)->conversationModel, &lrc::api::ConversationModel::newInteraction, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 472 | [self, priv](const std::string& uid, uint64_t interactionId, lrc::api::interaction::Info interaction) { |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 473 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 474 | if(uid == priv->conversation_->uid) { |
| 475 | print_interaction_to_buffer(self, interactionId, interaction); |
| 476 | } |
| 477 | }); |
| 478 | |
| 479 | priv->update_interaction_connection = QObject::connect( |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 480 | &*(*priv->accountInfo_)->conversationModel, &lrc::api::ConversationModel::interactionStatusUpdated, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 481 | [self, priv](const std::string& uid, uint64_t msgId, lrc::api::interaction::Info msg) { |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 482 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 483 | if(uid == priv->conversation_->uid) { |
| 484 | update_interaction(self, msgId, msg); |
| 485 | } |
| 486 | }); |
| 487 | |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 488 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 489 | auto contactUri = priv->conversation_->participants.front(); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 490 | try { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 491 | auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(contactUri); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 492 | priv->isTemporary_ = contactInfo.profileInfo.type == lrc::api::profile::Type::TEMPORARY |
| 493 | || contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING; |
| 494 | webkit_chat_container_set_temporary(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), priv->isTemporary_); |
| 495 | auto bestName = contactInfo.profileInfo.alias; |
| 496 | if (bestName.empty()) |
| 497 | bestName = contactInfo.registeredName; |
| 498 | if (bestName.empty()) |
| 499 | bestName = contactInfo.profileInfo.uri; |
| 500 | bestName.erase(std::remove(bestName.begin(), bestName.end(), '\r'), bestName.end()); |
| 501 | webkit_chat_container_set_invitation(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 502 | (contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING), |
| 503 | bestName); |
| 504 | webkit_chat_disable_send_interaction(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
| 505 | (contactInfo.profileInfo.type == lrc::api::profile::Type::SIP) |
| 506 | && priv->conversation_->callId.empty()); |
| 507 | } catch (const std::out_of_range&) { |
| 508 | // ContactModel::getContact() exception |
| 509 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static void |
| 513 | build_chat_view(ChatView* self) |
| 514 | { |
| 515 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 516 | |
| 517 | gtk_container_add(GTK_CONTAINER(priv->box_webkit_chat_container), priv->webkit_chat_container); |
| 518 | gtk_widget_show(priv->webkit_chat_container); |
| 519 | |
Sébastien Blin | f3681aa | 2017-07-19 16:53:49 -0400 | [diff] [blame] | 520 | update_name(self); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 521 | update_add_to_conversations(self); |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 522 | update_contact_methods(self); |
Stepan Salenikovich | 1f73121 | 2016-11-10 11:55:10 -0500 | [diff] [blame] | 523 | |
| 524 | priv->webkit_ready = g_signal_connect_swapped( |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 525 | priv->webkit_chat_container, |
| 526 | "ready", |
| 527 | G_CALLBACK(webkit_chat_container_ready), |
| 528 | self |
| 529 | ); |
| 530 | |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 531 | priv->webkit_send_text = g_signal_connect(priv->webkit_chat_container, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 532 | "script-dialog", |
| 533 | G_CALLBACK(webkit_chat_container_script_dialog), |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 534 | self); |
| 535 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 536 | if (webkit_chat_container_is_ready(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container))) |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 537 | webkit_chat_container_ready(self); |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 538 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 539 | gtk_widget_set_visible(priv->hbox_chat_info, TRUE); |
| 540 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 543 | GtkWidget * |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 544 | chat_view_new (WebKitChatContainer* webkit_chat_container, |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 545 | AccountInfoPointer const & accountInfo, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 546 | lrc::api::conversation::Info* conversation) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 547 | { |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 548 | ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL)); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 549 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 550 | ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); |
| 551 | priv->webkit_chat_container = GTK_WIDGET(webkit_chat_container); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 552 | priv->conversation_ = conversation; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 553 | priv->accountInfo_ = &accountInfo; |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 554 | |
| 555 | build_chat_view(self); |
Stepan Salenikovich | d2cad06 | 2016-01-08 13:43:49 -0500 | [diff] [blame] | 556 | return (GtkWidget *)self; |
| 557 | } |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 558 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 559 | void |
Sébastien Blin | 8ab4a81 | 2018-02-27 13:24:17 -0500 | [diff] [blame] | 560 | chat_view_update_temporary(ChatView* self, bool showAddButton, bool showInvitation) |
Stepan Salenikovich | c6a3b98 | 2016-01-11 18:11:39 -0500 | [diff] [blame] | 561 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 562 | g_return_if_fail(IS_CHAT_VIEW(self)); |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 563 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
| 564 | |
Sébastien Blin | 8ab4a81 | 2018-02-27 13:24:17 -0500 | [diff] [blame] | 565 | priv->isTemporary_ = showAddButton; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 566 | if (!priv->isTemporary_) { |
| 567 | gtk_widget_hide(priv->button_add_to_conversations); |
| 568 | } |
| 569 | webkit_chat_container_set_temporary(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), priv->isTemporary_); |
Sébastien Blin | 5851aaf | 2017-11-06 16:02:08 -0500 | [diff] [blame] | 570 | if (!priv->conversation_) return; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 571 | auto contactUri = priv->conversation_->participants.front(); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 572 | try { |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame^] | 573 | auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(contactUri); |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 574 | auto bestName = contactInfo.profileInfo.alias; |
| 575 | if (bestName.empty()) |
| 576 | bestName = contactInfo.registeredName; |
| 577 | if (bestName.empty()) |
| 578 | bestName = contactInfo.profileInfo.uri; |
| 579 | webkit_chat_container_set_invitation(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container), |
Sébastien Blin | 8ab4a81 | 2018-02-27 13:24:17 -0500 | [diff] [blame] | 580 | showInvitation, |
Guillaume Roguez | c209592 | 2017-12-14 14:07:10 -0500 | [diff] [blame] | 581 | bestName); |
| 582 | } catch (const std::out_of_range&) { |
| 583 | // ContactModel::getContact() exception |
| 584 | } |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 585 | } |
| 586 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 587 | bool |
| 588 | chat_view_get_temporary(ChatView *self) |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 589 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 590 | g_return_val_if_fail(IS_CHAT_VIEW(self), false); |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 591 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 592 | return priv->isTemporary_; |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 595 | lrc::api::conversation::Info |
| 596 | chat_view_get_conversation(ChatView *self) |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 597 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 598 | g_return_val_if_fail(IS_CHAT_VIEW(self), lrc::api::conversation::Info()); |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 599 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 600 | return *priv->conversation_; |
Stepan Salenikovich | 09e0b78 | 2016-09-07 16:28:50 -0400 | [diff] [blame] | 601 | } |
Stepan Salenikovich | daf3cb3 | 2016-10-12 16:39:42 -0400 | [diff] [blame] | 602 | |
| 603 | void |
| 604 | chat_view_set_header_visible(ChatView *self, gboolean visible) |
| 605 | { |
| 606 | auto priv = CHAT_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | daf3cb3 | 2016-10-12 16:39:42 -0400 | [diff] [blame] | 607 | gtk_widget_set_visible(priv->hbox_chat_info, visible); |
| 608 | } |