blob: 886fc75411813500828e57850ba43f666f34171c [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 Salenikovich1b7100a2016-02-19 11:50:46 -050032#include <QtCore/QDateTime>
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050033
Stepan Salenikovichce06adb2016-02-19 12:53:53 -050034static constexpr GdkRGBA RING_BLUE = {0.0508, 0.594, 0.676, 1.0}; // outgoing msg color: (13, 152, 173)
35
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050036struct _ChatView
37{
38 GtkBox parent;
39};
40
41struct _ChatViewClass
42{
43 GtkBoxClass parent_class;
44};
45
46typedef struct _ChatViewPrivate ChatViewPrivate;
47
48struct _ChatViewPrivate
49{
50 GtkWidget *textview_chat;
51 GtkWidget *button_chat_input;
52 GtkWidget *entry_chat_input;
53 GtkWidget *scrolledwindow_chat;
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050054 GtkWidget *hbox_chat_info;
55 GtkWidget *label_peer;
56 GtkWidget *combobox_cm;
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050057
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050058 /* only one of the three following pointers should be non void;
59 * either this is an in-call chat (and so the in-call chat APIs will be used)
60 * or it is an out of call chat (and so the account chat APIs will be used)
61 */
62 Call *call;
63 Person *person;
64 ContactMethod *cm;
Stepan Salenikovichd2cad062016-01-08 13:43:49 -050065
66 QMetaObject::Connection new_message_connection;
67};
68
69G_DEFINE_TYPE_WITH_PRIVATE(ChatView, chat_view, GTK_TYPE_BOX);
70
71#define CHAT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAT_VIEW_TYPE, ChatViewPrivate))
72
73enum {
74 NEW_MESSAGES_DISPLAYED,
75 LAST_SIGNAL
76};
77
78static guint chat_view_signals[LAST_SIGNAL] = { 0 };
79
80static void
81chat_view_dispose(GObject *object)
82{
83 ChatView *view;
84 ChatViewPrivate *priv;
85
86 view = CHAT_VIEW(object);
87 priv = CHAT_VIEW_GET_PRIVATE(view);
88
89 QObject::disconnect(priv->new_message_connection);
90
91 G_OBJECT_CLASS(chat_view_parent_class)->dispose(object);
92}
93
94
95static void
96send_chat(G_GNUC_UNUSED GtkWidget *widget, ChatView *self)
97{
98 g_return_if_fail(IS_CHAT_VIEW(self));
99 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
100
101 /* make sure there is text to send */
102 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry_chat_input));
103 if (text && strlen(text) > 0) {
104 QMap<QString, QString> messages;
105 messages["text/plain"] = text;
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500106
107 if (priv->call) {
108 // in call message
109 priv->call->addOutgoingMedia<Media::Text>()->send(messages);
110 } else if (priv->person) {
111 // get the chosen cm
112 auto active = gtk_combo_box_get_active(GTK_COMBO_BOX(priv->combobox_cm));
113 if (active >= 0) {
114 auto cm = priv->person->phoneNumbers().at(active);
115 if (!cm->sendOfflineTextMessage(messages))
116 g_warning("message failed to send"); // TODO: warn the user about this in the UI
117 } else {
118 g_warning("no ContactMethod chosen; message not esnt");
119 }
120 } else if (priv->cm) {
121 if (!priv->cm->sendOfflineTextMessage(messages))
122 g_warning("message failed to send"); // TODO: warn the user about this in the UI
123 } else {
124 g_warning("no Call, Person, or ContactMethod set; message not sent");
125 }
126
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500127 /* clear the entry */
128 gtk_entry_set_text(GTK_ENTRY(priv->entry_chat_input), "");
129 }
130}
131
132static void
133scroll_to_bottom(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
134{
135 gtk_adjustment_set_value(adjustment,
136 gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment));
137}
138
139static void
140chat_view_init(ChatView *view)
141{
142 gtk_widget_init_template(GTK_WIDGET(view));
143
144 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(view);
145
146 g_signal_connect(priv->button_chat_input, "clicked", G_CALLBACK(send_chat), view);
147 g_signal_connect(priv->entry_chat_input, "activate", G_CALLBACK(send_chat), view);
148
149 /* the adjustment params will change only when the model is created and when
150 * new messages are added; in these cases we want to scroll to the bottom of
151 * the chat treeview */
152 GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(priv->scrolledwindow_chat));
153 g_signal_connect(adjustment, "changed", G_CALLBACK(scroll_to_bottom), NULL);
154}
155
156static void
157chat_view_class_init(ChatViewClass *klass)
158{
159 G_OBJECT_CLASS(klass)->dispose = chat_view_dispose;
160
161 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
162 "/cx/ring/RingGnome/chatview.ui");
163
164 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, textview_chat);
165 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, button_chat_input);
166 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, entry_chat_input);
167 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, scrolledwindow_chat);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500168 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, hbox_chat_info);
169 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, label_peer);
170 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), ChatView, combobox_cm);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500171
172 chat_view_signals[NEW_MESSAGES_DISPLAYED] = g_signal_new (
173 "new-messages-displayed",
174 G_TYPE_FROM_CLASS(klass),
175 (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
176 0,
177 nullptr,
178 nullptr,
179 g_cclosure_marshal_VOID__VOID,
180 G_TYPE_NONE, 0);
181}
182
183static void
184print_message_to_buffer(const QModelIndex &idx, GtkTextBuffer *buffer)
185{
186 if (idx.isValid()) {
187 auto message = idx.data().value<QString>().toUtf8();
188 auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>().toUtf8();
Stepan Salenikovich1b7100a2016-02-19 11:50:46 -0500189 auto timestamp = idx.data(static_cast<int>(Media::TextRecording::Role::Timestamp)).value<time_t>();
190 auto datetime = QDateTime::fromTime_t(timestamp);
Stepan Salenikovichce06adb2016-02-19 12:53:53 -0500191 auto direction = idx.data(static_cast<int>(Media::TextRecording::Role::Direction)).value<Media::Media::Direction>();
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500192
193 GtkTextIter iter;
194
195 /* unless its the very first message, insert a new line */
196 if (idx.row() != 0) {
197 gtk_text_buffer_get_end_iter(buffer, &iter);
198 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
199 }
200
Stepan Salenikovich1b7100a2016-02-19 11:50:46 -0500201 /* if it is the very first row, we print the current date;
202 * otherwise we print the date every time it is different from the previous message */
203 auto date = datetime.date();
204 gchar* new_date = nullptr;
205 if (idx.row() == 0) {
206 new_date = g_strconcat("-- ", date.toString().toUtf8().constData(), " --\n", NULL);
207 } else {
208 auto prev_timestamp = idx.sibling(idx.row() - 1, 0).data(static_cast<int>(Media::TextRecording::Role::Timestamp)).value<time_t>();
209 auto prev_date = QDateTime::fromTime_t(prev_timestamp).date();
210 if (date != prev_date) {
211 new_date = g_strconcat("-- ", date.toString().toUtf8().constData(), " --\n", NULL);
212 }
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500213 }
214
Stepan Salenikovich1b7100a2016-02-19 11:50:46 -0500215 if (new_date) {
216 gtk_text_buffer_get_end_iter(buffer, &iter);
217 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, new_date, -1, "center", NULL);
218 }
219
220 /* insert time */
221 gtk_text_buffer_get_end_iter(buffer, &iter);
222 gtk_text_buffer_insert(buffer, &iter, datetime.time().toString().toUtf8().constData(), -1);
223
224 /* insert sender */
225 auto format_sender = g_strconcat(" ", sender.constData(), ": ", NULL);
226 gtk_text_buffer_get_end_iter(buffer, &iter);
Stepan Salenikovichce06adb2016-02-19 12:53:53 -0500227 if (direction == Media::Media::Direction::OUT)
228 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, format_sender, -1, "bold-blue", NULL);
229 else
230 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, format_sender, -1, "bold", NULL);
Stepan Salenikovich1b7100a2016-02-19 11:50:46 -0500231 g_free(format_sender);
232
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500233 gtk_text_buffer_get_end_iter(buffer, &iter);
Stepan Salenikovichce06adb2016-02-19 12:53:53 -0500234 if (direction == Media::Media::Direction::OUT)
235 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, message.constData(), -1, "blue", NULL);
236 else
237 gtk_text_buffer_insert(buffer, &iter, message.constData(), -1);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500238
239 } else {
240 g_warning("QModelIndex in im model is not valid");
241 }
242}
243
244static void
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500245print_text_recording(Media::TextRecording *recording, ChatView *self)
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500246{
247 g_return_if_fail(IS_CHAT_VIEW(self));
248 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
249
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500250 /* only text messages are supported for now */
251 auto model = recording->instantTextMessagingModel();
252
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500253 /* new model, disconnect from the old model updates and clear the text buffer */
254 QObject::disconnect(priv->new_message_connection);
255
256 GtkTextBuffer *new_buffer = gtk_text_buffer_new(NULL);
257 gtk_text_view_set_buffer(GTK_TEXT_VIEW(priv->textview_chat), new_buffer);
258
259 /* add tags to the buffer */
260 gtk_text_buffer_create_tag(new_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
Stepan Salenikovich1b7100a2016-02-19 11:50:46 -0500261 gtk_text_buffer_create_tag(new_buffer, "center", "justification", GTK_JUSTIFY_CENTER, NULL);
Stepan Salenikovichce06adb2016-02-19 12:53:53 -0500262 gtk_text_buffer_create_tag(new_buffer, "bold-blue", "weight", PANGO_WEIGHT_BOLD, "foreground-rgba", &RING_BLUE, NULL);
263 gtk_text_buffer_create_tag(new_buffer, "blue", "foreground-rgba", &RING_BLUE, NULL);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500264
265 g_object_unref(new_buffer);
266
267 /* put all the messages in the im model into the text view */
268 for (int row = 0; row < model->rowCount(); ++row) {
269 QModelIndex idx = model->index(row, 0);
270 print_message_to_buffer(idx, new_buffer);
271 }
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500272 /* mark all messages as read */
273 recording->setAllRead();
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500274
275 /* append new messages */
276 priv->new_message_connection = QObject::connect(
277 model,
278 &QAbstractItemModel::rowsInserted,
279 [self, priv, model] (const QModelIndex &parent, int first, int last) {
280 for (int row = first; row <= last; ++row) {
281 QModelIndex idx = model->index(row, 0, parent);
282 print_message_to_buffer(idx, gtk_text_view_get_buffer(GTK_TEXT_VIEW(priv->textview_chat)));
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500283 /* make sure these messages are marked as read */
284 model->setData(idx, true, static_cast<int>(Media::TextRecording::Role::IsRead));
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500285 g_signal_emit(G_OBJECT(self), chat_view_signals[NEW_MESSAGES_DISPLAYED], 0);
286 }
287 }
288 );
289}
290
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500291static void
292selected_cm_changed(GtkComboBox *box, ChatView *self)
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500293{
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500294 g_return_if_fail(IS_CHAT_VIEW(self));
295 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
296
297 auto cms = priv->person->phoneNumbers();
298 auto active = gtk_combo_box_get_active(box);
299 if (active >= 0 && active < cms.size()) {
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500300 print_text_recording(cms.at(active)->textRecording(), self);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500301 } else {
302 g_warning("no valid ContactMethod selected to display chat conversation");
303 }
304}
305
306static void
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500307render_contact_method(G_GNUC_UNUSED GtkCellLayout *cell_layout,
308 GtkCellRenderer *cell,
309 GtkTreeModel *model,
310 GtkTreeIter *iter,
311 G_GNUC_UNUSED gpointer data)
312{
313 GValue value = G_VALUE_INIT;
314 gtk_tree_model_get_value(model, iter, 0, &value);
315 auto cm = (ContactMethod *)g_value_get_pointer(&value);
316
317 gchar *number = nullptr;
318 if (cm && cm->category()) {
319 // try to get the number category, eg: "home"
320 number = g_strdup_printf("(%s) %s", cm->category()->name().toUtf8().constData(),
321 cm->uri().toUtf8().constData());
322 } else if (cm) {
323 number = g_strdup_printf("%s", cm->uri().toUtf8().constData());
324 }
325
326 g_object_set(G_OBJECT(cell), "text", number, NULL);
327 g_free(number);
328}
329
330static void
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500331update_contact_methods(ChatView *self)
332{
333 g_return_if_fail(IS_CHAT_VIEW(self));
334 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
335
336 g_return_if_fail(priv->person);
337
338 /* model for the combobox for the choice of ContactMethods */
339 auto cm_model = gtk_list_store_new(
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500340 1, G_TYPE_POINTER
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500341 );
342
343 auto cms = priv->person->phoneNumbers();
344 for (int i = 0; i < cms.size(); ++i) {
345 GtkTreeIter iter;
346 gtk_list_store_append(cm_model, &iter);
347 gtk_list_store_set(cm_model, &iter,
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500348 0, cms.at(i),
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500349 -1);
350 }
351
352 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_cm), GTK_TREE_MODEL(cm_model));
353 g_object_unref(cm_model);
354
355 auto renderer = gtk_cell_renderer_text_new();
356 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
357 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_cm), renderer, FALSE);
Stepan Salenikovich5039c9b2016-02-12 14:09:51 -0500358 gtk_cell_layout_set_cell_data_func(
359 GTK_CELL_LAYOUT(priv->combobox_cm),
360 renderer,
361 (GtkCellLayoutDataFunc)render_contact_method,
362 nullptr, nullptr);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500363
364 /* select the last used cm */
365 if (!cms.isEmpty()) {
366 auto last_used_cm = cms.at(0);
367 int last_used_cm_idx = 0;
368 for (int i = 1; i < cms.size(); ++i) {
369 auto new_cm = cms.at(i);
370 if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0) {
371 last_used_cm = new_cm;
372 last_used_cm_idx = i;
373 }
374 }
375
376 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_cm), last_used_cm_idx);
377 }
378
379 /* show the combo box if there is more than one cm to choose from */
380 if (cms.size() > 1)
381 gtk_widget_show_all(priv->combobox_cm);
382 else
383 gtk_widget_hide(priv->combobox_cm);
384}
385
386static void
387update_name(ChatView *self)
388{
389 g_return_if_fail(IS_CHAT_VIEW(self));
390 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
391
392 g_return_if_fail(priv->person || priv->cm);
393
394 QString name;
395 if (priv->person) {
396 name = priv->person->roleData(static_cast<int>(Ring::Role::Name)).toString();
397 } else {
398 name = priv->cm->roleData(static_cast<int>(Ring::Role::Name)).toString();
399 }
400 gtk_label_set_text(GTK_LABEL(priv->label_peer), name.toUtf8().constData());
401}
402
403GtkWidget *
404chat_view_new_call(Call *call)
405{
406 g_return_val_if_fail(call, nullptr);
407
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500408 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
409 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
410
411 priv->call = call;
412 auto cm = priv->call->peerContactMethod();
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500413 print_text_recording(cm->textRecording(), self);
Stepan Salenikovichd2cad062016-01-08 13:43:49 -0500414
415 return (GtkWidget *)self;
416}
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500417
418GtkWidget *
419chat_view_new_cm(ContactMethod *cm)
420{
421 g_return_val_if_fail(cm, nullptr);
422
423 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
424 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
425
426 priv->cm = cm;
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500427 print_text_recording(priv->cm->textRecording(), self);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500428 update_name(self);
429
430 gtk_widget_show(priv->hbox_chat_info);
431
432 return (GtkWidget *)self;
433}
434
435GtkWidget *
436chat_view_new_person(Person *p)
437{
438 g_return_val_if_fail(p, nullptr);
439
440 ChatView *self = CHAT_VIEW(g_object_new(CHAT_VIEW_TYPE, NULL));
441 ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self);
442
443 priv->person = p;
444
445 /* connect to the changed signal before setting the cm combo box, so that the correct
446 * conversation will get displayed */
447 g_signal_connect(priv->combobox_cm, "changed", G_CALLBACK(selected_cm_changed), self);
448 update_contact_methods(self);
449 update_name(self);
450
451 gtk_widget_show(priv->hbox_chat_info);
452
453 return (GtkWidget *)self;
454}