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