blob: 69589e3f4ef6c76d1ef1d84151cd5f43d6cb21dc [file] [log] [blame]
Stepan Salenikovichd2cad062016-01-08 13:43:49 -05001/*
2 * Copyright (C) 2016 Savoir-faire Linux Inc.
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.
18 */
19
20#include "chatview.h"
21
22#include <gtk/gtk.h>
23#include <call.h>
24#include <callmodel.h>
25#include <contactmethod.h>
26#include <person.h>
27#include <media/media.h>
28#include <media/text.h>
29#include <media/textrecording.h>
30#include "ringnotify.h"
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -050031#include "numbercategory.h"
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050032
33struct _ChatView
34{
35 GtkBox parent;
36};
37
38struct _ChatViewClass
39{
40 GtkBoxClass parent_class;
41};
42
43typedef struct _ChatViewPrivate ChatViewPrivate;
44
45struct _ChatViewPrivate
46{
47 GtkWidget *textview_chat;
48 GtkWidget *button_chat_input;
49 GtkWidget *entry_chat_input;
50 GtkWidget *scrolledwindow_chat;
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050051 GtkWidget *hbox_chat_info;
52 GtkWidget *label_peer;
53 GtkWidget *combobox_cm;
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050054
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050055 /* only one of the three following pointers should be non void;
56 * either this is an in-call chat (and so the in-call chat APIs will be used)
57 * or it is an out of call chat (and so the account chat APIs will be used)
58 */
59 Call *call;
60 Person *person;
61 ContactMethod *cm;
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050062
63 QMetaObject::Connection new_message_connection;
64};
65
66G_DEFINE_TYPE_WITH_PRIVATE(ChatView, chat_view, GTK_TYPE_BOX);
67
68#define CHAT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAT_VIEW_TYPE, ChatViewPrivate))
69
70enum {
71 NEW_MESSAGES_DISPLAYED,
72 LAST_SIGNAL
73};
74
75static guint chat_view_signals[LAST_SIGNAL] = { 0 };
76
77static void
78chat_view_dispose(GObject *object)
79{
80 ChatView *view;
81 ChatViewPrivate *priv;
82
83 view = CHAT_VIEW(object);
84 priv = CHAT_VIEW_GET_PRIVATE(view);
85
86 QObject::disconnect(priv->new_message_connection);
87
88 G_OBJECT_CLASS(chat_view_parent_class)->dispose(object);
89}
90
91
92static void
93send_chat(G_GNUC_UNUSED GtkWidget *widget, ChatView *self)
94{
95 g_return_if_fail(IS_CHAT_VIEW(self));
96 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
97
98 /* make sure there is text to send */
99 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry_chat_input));
100 if (text && strlen(text) > 0) {
101 QMap<QString, QString> messages;
102 messages["text/plain"] = text;
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500103
104 if (priv->call) {
105 // in call message
106 priv->call->addOutgoingMedia<Media::Text>()->send(messages);
107 } else if (priv->person) {
108 // get the chosen cm
109 auto active = gtk_combo_box_get_active(GTK_COMBO_BOX(priv->combobox_cm));
110 if (active >= 0) {
111 auto cm = priv->person->phoneNumbers().at(active);
112 if (!cm->sendOfflineTextMessage(messages))
113 g_warning("message failed to send"); // TODO: warn the user about this in the UI
114 } else {
115 g_warning("no ContactMethod chosen; message not esnt");
116 }
117 } else if (priv->cm) {
118 if (!priv->cm->sendOfflineTextMessage(messages))
119 g_warning("message failed to send"); // TODO: warn the user about this in the UI
120 } else {
121 g_warning("no Call, Person, or ContactMethod set; message not sent");
122 }
123
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500124 /* clear the entry */
125 gtk_entry_set_text(GTK_ENTRY(priv->entry_chat_input), "");
126 }
127}
128
129static void
130scroll_to_bottom(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
131{
132 gtk_adjustment_set_value(adjustment,
133 gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment));
134}
135
136static void
137chat_view_init(ChatView *view)
138{
139 gtk_widget_init_template(GTK_WIDGET(view));
140
141 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(view);
142
143 g_signal_connect(priv->button_chat_input, "clicked", G_CALLBACK(send_chat), view);
144 g_signal_connect(priv->entry_chat_input, "activate", G_CALLBACK(send_chat), view);
145
146 /* the adjustment params will change only when the model is created and when
147 * new messages are added; in these cases we want to scroll to the bottom of
148 * the chat treeview */
149 GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(priv->scrolledwindow_chat));
150 g_signal_connect(adjustment, "changed", G_CALLBACK(scroll_to_bottom), NULL);
151}
152
153static void
154chat_view_class_init(ChatViewClass *klass)
155{
156 G_OBJECT_CLASS(klass)->dispose = chat_view_dispose;
157
158 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
159 "/cx/ring/RingGnome/chatview.ui");
160
161 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, textview_chat);
162 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_chat_input);
163 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, entry_chat_input);
164 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, scrolledwindow_chat);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500165 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, hbox_chat_info);
166 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, label_peer);
167 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, combobox_cm);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500168
169 chat_view_signals[NEW_MESSAGES_DISPLAYED] = g_signal_new (
170 "new-messages-displayed",
171 G_TYPE_FROM_CLASS(klass),
172 (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
173 0,
174 nullptr,
175 nullptr,
176 g_cclosure_marshal_VOID__VOID,
177 G_TYPE_NONE, 0);
178}
179
180static void
181print_message_to_buffer(const QModelIndex &idx, GtkTextBuffer *buffer)
182{
183 if (idx.isValid()) {
184 auto message = idx.data().value<QString>().toUtf8();
185 auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>().toUtf8();
186
187 GtkTextIter iter;
188
189 /* unless its the very first message, insert a new line */
190 if (idx.row() != 0) {
191 gtk_text_buffer_get_end_iter(buffer, &iter);
192 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
193 }
194
195 auto format_sender = g_strconcat(sender.constData(), ": ", NULL);
196 gtk_text_buffer_get_end_iter(buffer, &iter);
197 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
198 format_sender, -1,
199 "bold", NULL);
200 g_free(format_sender);
201
202 /* if the sender name is too long, insert a new line after it */
203 if (sender.length() > 20) {
204 gtk_text_buffer_get_end_iter(buffer, &iter);
205 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
206 }
207
208 gtk_text_buffer_get_end_iter(buffer, &iter);
209 gtk_text_buffer_insert(buffer, &iter, message.constData(), -1);
210
211 } else {
212 g_warning("QModelIndex in im model is not valid");
213 }
214}
215
216static void
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500217print_text_recording(Media::TextRecording *recording, ChatView *self)
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500218{
219 g_return_if_fail(IS_CHAT_VIEW(self));
220 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
221
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500222 /* only text messages are supported for now */
223 auto model = recording->instantTextMessagingModel();
224
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500225 /* new model, disconnect from the old model updates and clear the text buffer */
226 QObject::disconnect(priv->new_message_connection);
227
228 GtkTextBuffer *new_buffer = gtk_text_buffer_new(NULL);
229 gtk_text_view_set_buffer(GTK_TEXT_VIEW(priv->textview_chat), new_buffer);
230
231 /* add tags to the buffer */
232 gtk_text_buffer_create_tag(new_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
233
234 g_object_unref(new_buffer);
235
236 /* put all the messages in the im model into the text view */
237 for (int row = 0; row < model->rowCount(); ++row) {
238 QModelIndex idx = model->index(row, 0);
239 print_message_to_buffer(idx, new_buffer);
240 }
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500241 /* mark all messages as read */
242 recording->setAllRead();
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500243
244 /* append new messages */
245 priv->new_message_connection = QObject::connect(
246 model,
247 &QAbstractItemModel::rowsInserted,
248 [self, priv, model] (const QModelIndex &parent, int first, int last) {
249 for (int row = first; row <= last; ++row) {
250 QModelIndex idx = model->index(row, 0, parent);
251 print_message_to_buffer(idx, gtk_text_view_get_buffer(GTK_TEXT_VIEW(priv->textview_chat)));
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500252 /* make sure these messages are marked as read */
253 model->setData(idx, true, static_cast<int>(Media::TextRecording::Role::IsRead));
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500254 g_signal_emit(G_OBJECT(self), chat_view_signals[NEW_MESSAGES_DISPLAYED], 0);
255 }
256 }
257 );
258}
259
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500260static void
261selected_cm_changed(GtkComboBox *box, ChatView *self)
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500262{
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500263 g_return_if_fail(IS_CHAT_VIEW(self));
264 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
265
266 auto cms = priv->person->phoneNumbers();
267 auto active = gtk_combo_box_get_active(box);
268 if (active >= 0 && active < cms.size()) {
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500269 print_text_recording(cms.at(active)->textRecording(), self);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500270 } else {
271 g_warning("no valid ContactMethod selected to display chat conversation");
272 }
273}
274
275static void
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500276render_contact_method(G_GNUC_UNUSED GtkCellLayout *cell_layout,
277 GtkCellRenderer *cell,
278 GtkTreeModel *model,
279 GtkTreeIter *iter,
280 G_GNUC_UNUSED gpointer data)
281{
282 GValue value = G_VALUE_INIT;
283 gtk_tree_model_get_value(model, iter, 0, &value);
284 auto cm = (ContactMethod *)g_value_get_pointer(&value);
285
286 gchar *number = nullptr;
287 if (cm && cm->category()) {
288 // try to get the number category, eg: "home"
289 number = g_strdup_printf("(%s) %s", cm->category()->name().toUtf8().constData(),
290 cm->uri().toUtf8().constData());
291 } else if (cm) {
292 number = g_strdup_printf("%s", cm->uri().toUtf8().constData());
293 }
294
295 g_object_set(G_OBJECT(cell), "text", number, NULL);
296 g_free(number);
297}
298
299static void
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500300update_contact_methods(ChatView *self)
301{
302 g_return_if_fail(IS_CHAT_VIEW(self));
303 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
304
305 g_return_if_fail(priv->person);
306
307 /* model for the combobox for the choice of ContactMethods */
308 auto cm_model = gtk_list_store_new(
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500309 1, G_TYPE_POINTER
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500310 );
311
312 auto cms = priv->person->phoneNumbers();
313 for (int i = 0; i < cms.size(); ++i) {
314 GtkTreeIter iter;
315 gtk_list_store_append(cm_model, &iter);
316 gtk_list_store_set(cm_model, &iter,
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500317 0, cms.at(i),
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500318 -1);
319 }
320
321 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_cm), GTK_TREE_MODEL(cm_model));
322 g_object_unref(cm_model);
323
324 auto renderer = gtk_cell_renderer_text_new();
325 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
326 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_cm), renderer, FALSE);
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500327 gtk_cell_layout_set_cell_data_func(
328 GTK_CELL_LAYOUT(priv->combobox_cm),
329 renderer,
330 (GtkCellLayoutDataFunc)render_contact_method,
331 nullptr, nullptr);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500332
333 /* select the last used cm */
334 if (!cms.isEmpty()) {
335 auto last_used_cm = cms.at(0);
336 int last_used_cm_idx = 0;
337 for (int i = 1; i < cms.size(); ++i) {
338 auto new_cm = cms.at(i);
339 if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0) {
340 last_used_cm = new_cm;
341 last_used_cm_idx = i;
342 }
343 }
344
345 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_cm), last_used_cm_idx);
346 }
347
348 /* show the combo box if there is more than one cm to choose from */
349 if (cms.size() > 1)
350 gtk_widget_show_all(priv->combobox_cm);
351 else
352 gtk_widget_hide(priv->combobox_cm);
353}
354
355static void
356update_name(ChatView *self)
357{
358 g_return_if_fail(IS_CHAT_VIEW(self));
359 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
360
361 g_return_if_fail(priv->person || priv->cm);
362
363 QString name;
364 if (priv->person) {
365 name = priv->person->roleData(static_cast<int>(Ring::Role::Name)).toString();
366 } else {
367 name = priv->cm->roleData(static_cast<int>(Ring::Role::Name)).toString();
368 }
369 gtk_label_set_text(GTK_LABEL(priv->label_peer), name.toUtf8().constData());
370}
371
372GtkWidget *
373chat_view_new_call(Call *call)
374{
375 g_return_val_if_fail(call, nullptr);
376
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500377 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
378 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
379
380 priv->call = call;
381 auto cm = priv->call->peerContactMethod();
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500382 print_text_recording(cm->textRecording(), self);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500383
384 return (GtkWidget *)self;
385}
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500386
387GtkWidget *
388chat_view_new_cm(ContactMethod *cm)
389{
390 g_return_val_if_fail(cm, nullptr);
391
392 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
393 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
394
395 priv->cm = cm;
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500396 print_text_recording(priv->cm->textRecording(), self);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500397 update_name(self);
398
399 gtk_widget_show(priv->hbox_chat_info);
400
401 return (GtkWidget *)self;
402}
403
404GtkWidget *
405chat_view_new_person(Person *p)
406{
407 g_return_val_if_fail(p, nullptr);
408
409 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
410 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
411
412 priv->person = p;
413
414 /* connect to the changed signal before setting the cm combo box, so that the correct
415 * conversation will get displayed */
416 g_signal_connect(priv->combobox_cm, "changed", G_CALLBACK(selected_cm_changed), self);
417 update_contact_methods(self);
418 update_name(self);
419
420 gtk_widget_show(priv->hbox_chat_info);
421
422 return (GtkWidget *)self;
423}