blob: b89d402db5bd891cf55ad5876b655b975a98736f [file] [log] [blame]
Sébastien Bline72d43c2017-10-03 11:37:33 -04001/****************************************************************************
2 * Copyright (C) 2017 Savoir-faire Linux *
3 * Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> *
4 * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
19
20#include "conversationsview.h"
21
22// std
23#include <algorithm>
24
25// GTK+ related
26#include <QSize>
27
28// LRC
29#include <globalinstances.h>
30#include <api/conversationmodel.h>
31#include <api/contactmodel.h>
32#include <api/call.h>
33#include <api/contact.h>
34#include <api/newcallmodel.h>
35
36// Gnome client
37#include "native/pixbufmanipulator.h"
38#include "conversationpopupmenu.h"
39
40
41static constexpr const char* CALL_TARGET = "CALL_TARGET";
42static constexpr int CALL_TARGET_ID = 0;
43
44struct _ConversationsView
45{
46 GtkTreeView parent;
47};
48
49struct _ConversationsViewClass
50{
51 GtkTreeViewClass parent_class;
52};
53
54typedef struct _ConversationsViewPrivate ConversationsViewPrivate;
55
56struct _ConversationsViewPrivate
57{
58 AccountContainer* accountContainer_;
59
60 GtkWidget* popupMenu_;
61
62 QMetaObject::Connection selection_updated;
63 QMetaObject::Connection layout_changed;
64 QMetaObject::Connection modelSortedConnection_;
65 QMetaObject::Connection filterChangedConnection_;
66
67};
68
69G_DEFINE_TYPE_WITH_PRIVATE(ConversationsView, conversations_view, GTK_TYPE_TREE_VIEW);
70
71#define CONVERSATIONS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONVERSATIONS_VIEW_TYPE, ConversationsViewPrivate))
72
73static void
74render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
75 GtkCellRenderer *cell,
76 GtkTreeModel *model,
77 GtkTreeIter *iter,
78 gpointer self)
79{
80 // Get active conversation
81 auto path = gtk_tree_model_get_path(model, iter);
82 auto row = std::atoi(gtk_tree_path_to_string(path));
83 if (row == -1) return;
84 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
85 if (!priv) return;
86 try
87 {
88 // Draw first contact.
89 // NOTE: We just draw the first contact, must change this for conferences when they will have their own object
90 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
91 auto contact = priv->accountContainer_->info.contactModel->getContact(conversation.participants.front());
92 std::shared_ptr<GdkPixbuf> image;
93 auto var_photo = GlobalInstances::pixmapManipulator().conversationPhoto(
94 conversation,
95 priv->accountContainer_->info,
96 QSize(50, 50),
97 contact.isPresent
98 );
99 image = var_photo.value<std::shared_ptr<GdkPixbuf>>();
100
101 // set the width of the cell rendered to the width of the photo
102 // so that the other renderers are shifted to the right
103 g_object_set(G_OBJECT(cell), "width", 50, NULL);
104 g_object_set(G_OBJECT(cell), "pixbuf", image.get(), NULL);
105 }
106 catch (const std::exception&)
107 {
108 g_warning("Can't get conversation at row %i", row);
109 }
110}
111
112static void
113render_name_and_last_interaction(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
114 GtkCellRenderer *cell,
115 GtkTreeModel *model,
116 GtkTreeIter *iter,
117 G_GNUC_UNUSED GtkTreeView *treeview)
118{
119 gchar *alias;
120 gchar *registeredName;
121 gchar *lastInteraction;
122 gchar *text;
123 gchar *uid;
124
125 gtk_tree_model_get (model, iter,
126 0 /* col# */, &uid /* data */,
127 1 /* col# */, &alias /* data */,
128 2 /* col# */, &registeredName /* data */,
129 4 /* col# */, &lastInteraction /* data */,
130 -1);
131
Sébastien Bline72d43c2017-10-03 11:37:33 -0400132 if (std::string(alias).empty()) {
133 // For conversations with contacts with no alias
134 text = g_markup_printf_escaped(
135 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
136 registeredName,
137 lastInteraction
138 );
139 } else if (std::string(alias) == std::string(registeredName)
140 || std::string(registeredName).empty() || std::string(uid).empty()) {
141 // For temporary item
142 text = g_markup_printf_escaped(
143 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
144 alias,
145 lastInteraction
146 );
147 } else {
148 // For conversations with contacts with alias
149 text = g_markup_printf_escaped(
150 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
151 alias,
152 registeredName,
153 lastInteraction
154 );
155 }
156
157 g_object_set(G_OBJECT(cell), "markup", text, NULL);
158 g_free(uid);
159 g_free(alias);
160 g_free(registeredName);
161}
162
163static void
164render_time(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
165 GtkCellRenderer *cell,
166 GtkTreeModel *model,
167 GtkTreeIter *iter,
168 G_GNUC_UNUSED GtkTreeView *treeview)
169{
170
171 // Get active conversation
172 auto path = gtk_tree_model_get_path(model, iter);
173 auto row = std::atoi(gtk_tree_path_to_string(path));
174 if (row == -1) return;
175 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(treeview);
176 if (!priv) return;
177
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400178 char empty[] = {'\0'};
179 gchar *text = empty;
Sébastien Bline72d43c2017-10-03 11:37:33 -0400180
181 try
182 {
183 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
184 auto callId = conversation.confId.empty() ? conversation.callId : conversation.confId;
185 if (!callId.empty()) {
186 auto call = priv->accountContainer_->info.callModel->getCall(callId);
187 text = g_markup_printf_escaped("%s",
188 lrc::api::call::to_string(call.status).c_str()
189 );
190 } else if (conversation.interactions.empty()) {
Sébastien Bline72d43c2017-10-03 11:37:33 -0400191 } else {
192 auto lastUid = conversation.lastMessageUid;
193 if (conversation.interactions.find(lastUid) == conversation.interactions.end()) {
Sébastien Bline72d43c2017-10-03 11:37:33 -0400194 } else {
195 std::time_t lastInteractionTimestamp = conversation.interactions[lastUid].timestamp;
196 std::time_t now = std::time(nullptr);
197 char interactionDay[100];
198 char nowDay[100];
199 std::strftime(interactionDay, sizeof(interactionDay), "%D", std::localtime(&lastInteractionTimestamp));
200 std::strftime(nowDay, sizeof(nowDay), "%D", std::localtime(&now));
201
202
203 if (std::string(interactionDay) == std::string(nowDay)) {
204 char interactionTime[100];
205 std::strftime(interactionTime, sizeof(interactionTime), "%R", std::localtime(&lastInteractionTimestamp));
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400206 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"#666\">%s</span>", &interactionTime[0]);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400207 } else {
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400208 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"#666\">%s</span>", &interactionDay[0]);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400209 }
210 }
211 }
212 }
213 catch (const std::exception&)
214 {
215 g_warning("Can't get conversation at row %i", row);
216 }
217
218 g_object_set(G_OBJECT(cell), "markup", text, NULL);
219}
220
221static GtkTreeModel*
222create_and_fill_model(ConversationsView *self)
223{
224 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
225 auto store = gtk_list_store_new (5 /* # of cols */ ,
226 G_TYPE_STRING,
227 G_TYPE_STRING,
228 G_TYPE_STRING,
229 G_TYPE_STRING,
230 G_TYPE_STRING,
231 G_TYPE_UINT);
232 if(!priv) GTK_TREE_MODEL (store);
233 GtkTreeIter iter;
234
235 for (auto conversation : priv->accountContainer_->info.conversationModel->allFilteredConversations()) {
236 if (conversation.participants.empty()) break; // Should not
237 auto contactUri = conversation.participants.front();
238 auto contactInfo = priv->accountContainer_->info.contactModel->getContact(contactUri);
239 auto lastMessage = conversation.interactions.empty() ? "" :
240 conversation.interactions.at(conversation.lastMessageUid).body;
241 std::replace(lastMessage.begin(), lastMessage.end(), '\n', ' ');
242 gtk_list_store_append (store, &iter);
243 auto alias = contactInfo.profileInfo.alias;
244 alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
245 gtk_list_store_set (store, &iter,
246 0 /* col # */ , conversation.uid.c_str() /* celldata */,
247 1 /* col # */ , alias.c_str() /* celldata */,
248 2 /* col # */ , contactInfo.registeredName.c_str() /* celldata */,
249 3 /* col # */ , contactInfo.profileInfo.avatar.c_str() /* celldata */,
250 4 /* col # */ , lastMessage.c_str() /* celldata */,
251 -1 /* end */);
252 }
253
254 return GTK_TREE_MODEL (store);
255}
256
257static void
258call_conversation(GtkTreeView *self,
259 GtkTreePath *path,
260 G_GNUC_UNUSED GtkTreeViewColumn *column,
261 G_GNUC_UNUSED gpointer user_data)
262{
263 auto row = std::atoi(gtk_tree_path_to_string(path));
264 if (row == -1) return;
265 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
266 if (!priv) return;
267 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
268 priv->accountContainer_->info.conversationModel->placeCall(conversation.uid);
269}
270
271static void
272select_conversation(GtkTreeSelection *selection, ConversationsView *self)
273{
274 // Update popupMenu_
275 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
276 if (priv->popupMenu_) {
277 // Because popup menu is not up to date, we need to update it.
278 auto isVisible = gtk_widget_get_visible(priv->popupMenu_);
279 // Destroy the not up to date menu.
280 gtk_widget_hide(priv->popupMenu_);
281 gtk_widget_destroy(priv->popupMenu_);
282 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
283 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
284 auto nbItems = g_list_length(children);
285 // Show the new popupMenu_ should be visible
286 if (isVisible && nbItems > 0)
287 gtk_menu_popup(GTK_MENU(priv->popupMenu_), nullptr, nullptr, nullptr, nullptr, 0, gtk_get_current_event_time());
288 }
289 GtkTreeIter iter;
290 GtkTreeModel *model = nullptr;
291 gchar *conversationUid = nullptr;
292
293 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return;
294
295 gtk_tree_model_get(model, &iter,
296 0, &conversationUid,
297 -1);
298 priv->accountContainer_->info.conversationModel->selectConversation(std::string(conversationUid));
299}
300
301static void
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400302conversations_view_init(G_GNUC_UNUSED ConversationsView *self)
Sébastien Bline72d43c2017-10-03 11:37:33 -0400303{
304 // Nothing to do
305}
306
307static void
308show_popup_menu(ConversationsView *self, GdkEventButton *event)
309{
310 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
311 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
312 auto nbItems = g_list_length(children);
313 // Show the new popupMenu_ should be visible
314 if (nbItems > 0)
315 conversation_popup_menu_show(CONVERSATION_POPUP_MENU(priv->popupMenu_), event);
316}
317
318static void
319on_drag_data_get(GtkWidget *treeview,
320 G_GNUC_UNUSED GdkDragContext *context,
321 GtkSelectionData *data,
322 G_GNUC_UNUSED guint info,
323 G_GNUC_UNUSED guint time,
324 G_GNUC_UNUSED gpointer user_data)
325{
326 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
327
328 /* we always drag the selected row */
329 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
330 GtkTreeModel *model = NULL;
331 GtkTreeIter iter;
332
333 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
334 auto path_str = gtk_tree_model_get_string_from_iter(model, &iter);
335
336 gtk_selection_data_set(data,
337 gdk_atom_intern_static_string(CALL_TARGET),
338 8, /* bytes */
339 (guchar *)path_str,
340 strlen(path_str) + 1);
341
342 g_free(path_str);
343 } else {
344 g_warning("drag selection not valid");
345 }
346}
347
348static gboolean
349on_drag_drop(GtkWidget *treeview,
350 GdkDragContext *context,
351 gint x,
352 gint y,
353 guint time,
354 G_GNUC_UNUSED gpointer user_data)
355{
356 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
357
358 GtkTreePath *path = NULL;
359 GtkTreeViewDropPosition drop_pos;
360
361 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
362 x, y, &path, &drop_pos)) {
363
364 GdkAtom target_type = gtk_drag_dest_find_target(treeview, context, NULL);
365
366 if (target_type != GDK_NONE) {
367 g_debug("can drop");
368 gtk_drag_get_data(treeview, context, target_type, time);
369 return TRUE;
370 }
371
372 gtk_tree_path_free(path);
373 }
374
375 return FALSE;
376}
377
378static gboolean
379on_drag_motion(GtkWidget *treeview,
380 GdkDragContext *context,
381 gint x,
382 gint y,
383 guint time,
384 G_GNUC_UNUSED gpointer user_data)
385{
386 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
387
388 GtkTreePath *path = NULL;
389 GtkTreeViewDropPosition drop_pos;
390
391 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
392 x, y, &path, &drop_pos)) {
393 // we only want to drop on a row, not before or after
394 if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE) {
395 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
396 } else if (drop_pos == GTK_TREE_VIEW_DROP_AFTER) {
397 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
398 }
399 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
400 return TRUE;
401 } else {
402 // not a row in the treeview, so we cannot drop
403 return FALSE;
404 }
405}
406
407static void
408on_drag_data_received(GtkWidget *treeview,
409 GdkDragContext *context,
410 gint x,
411 gint y,
412 GtkSelectionData *data,
413 G_GNUC_UNUSED guint info,
414 guint time,
415 G_GNUC_UNUSED gpointer user_data)
416{
417 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
418 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(treeview);
419
420 gboolean success = FALSE;
421
422 /* get the source and destination calls */
423 auto path_str_source = (gchar *)gtk_selection_data_get_data(data);
424 auto type = gtk_selection_data_get_data_type(data);
425 g_debug("data type: %s", gdk_atom_name(type));
426 if (path_str_source && strlen(path_str_source) > 0) {
427 g_debug("source path: %s", path_str_source);
428
429 /* get the destination path */
430 GtkTreePath *dest_path = NULL;
431 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview), x, y, &dest_path, NULL)) {
432 auto model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
433
434 GtkTreeIter source, dest;
435 gtk_tree_model_get_iter_from_string(model, &source, path_str_source);
436 gtk_tree_model_get_iter(model, &dest, dest_path);
437
438 gchar *conversationUidSrc = nullptr;
439 gchar *conversationUidDest = nullptr;
440
441 gtk_tree_model_get(model, &source,
442 0, &conversationUidSrc,
443 -1);
444 gtk_tree_model_get(model, &dest,
445 0, &conversationUidDest,
446 -1);
447
448 priv->accountContainer_->info.conversationModel->joinConversations(
449 conversationUidSrc,
450 conversationUidDest
451 );
452
453 gtk_tree_path_free(dest_path);
454 }
455 }
456
457 gtk_drag_finish(context, success, FALSE, time);
458}
459
460static void
461build_conversations_view(ConversationsView *self)
462{
463 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
464 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
465
466 auto model = create_and_fill_model(self);
467 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
468 GTK_TREE_MODEL(model));
469
470 // ringId method column
471 auto area = gtk_cell_area_box_new();
472 auto column = gtk_tree_view_column_new_with_area(area);
473
474 // render the photo
475 auto renderer = gtk_cell_renderer_pixbuf_new();
476 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
477
478 gtk_tree_view_column_set_cell_data_func(
479 column,
480 renderer,
481 (GtkTreeCellDataFunc)render_contact_photo,
482 self,
483 NULL);
484
485 // render name and last interaction
486 renderer = gtk_cell_renderer_text_new();
487 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
488 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
489
490 gtk_tree_view_column_set_cell_data_func(
491 column,
492 renderer,
493 (GtkTreeCellDataFunc)render_name_and_last_interaction,
494 self,
495 NULL);
496
497 // render time of last interaction and number of unread
498 renderer = gtk_cell_renderer_text_new();
499 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
500 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
501 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
502
503 gtk_tree_view_column_set_cell_data_func(
504 column,
505 renderer,
506 (GtkTreeCellDataFunc)render_time,
507 self,
508 NULL);
509
510 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
511
512 // This view should be synchronized and redraw at each update.
513 priv->modelSortedConnection_ = QObject::connect(
514 &*priv->accountContainer_->info.conversationModel,
515 &lrc::api::ConversationModel::modelSorted,
516 [self] () {
517 auto model = create_and_fill_model(self);
518
519 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
520 GTK_TREE_MODEL(model));
521 });
522
523 priv->filterChangedConnection_ = QObject::connect(
524 &*priv->accountContainer_->info.conversationModel,
525 &lrc::api::ConversationModel::filterChanged,
526 [self] () {
527 auto model = create_and_fill_model(self);
528
529 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
530 GTK_TREE_MODEL(model));
531 });
532
533 gtk_widget_show_all(GTK_WIDGET(self));
534
535 auto selectionNew = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
536 // One left click to select the conversation
537 g_signal_connect(selectionNew, "changed", G_CALLBACK(select_conversation), self);
538 // Two clicks to placeCall
539 g_signal_connect(self, "row-activated", G_CALLBACK(call_conversation), NULL);
540
541 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
542 // Right click to show actions
543 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(show_popup_menu), self);
544
545 /* drag and drop */
546 static GtkTargetEntry targetentries[] = {
547 { (gchar *)CALL_TARGET, GTK_TARGET_SAME_WIDGET, CALL_TARGET_ID },
548 };
549
550 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(self),
551 GDK_BUTTON1_MASK, targetentries, 1, (GdkDragAction)(GDK_ACTION_DEFAULT | GDK_ACTION_MOVE));
552
553 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(self),
554 targetentries, 1, GDK_ACTION_DEFAULT);
555
556 g_signal_connect(self, "drag-data-get", G_CALLBACK(on_drag_data_get), nullptr);
557 g_signal_connect(self, "drag-drop", G_CALLBACK(on_drag_drop), nullptr);
558 g_signal_connect(self, "drag-motion", G_CALLBACK(on_drag_motion), nullptr);
559 g_signal_connect(self, "drag_data_received", G_CALLBACK(on_drag_data_received), nullptr);
560}
561
562static void
563conversations_view_dispose(GObject *object)
564{
565 auto self = CONVERSATIONS_VIEW(object);
566 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
567
568 QObject::disconnect(priv->selection_updated);
569 QObject::disconnect(priv->layout_changed);
570 QObject::disconnect(priv->modelSortedConnection_);
571 QObject::disconnect(priv->filterChangedConnection_);
572
573 gtk_widget_destroy(priv->popupMenu_);
574
575 G_OBJECT_CLASS(conversations_view_parent_class)->dispose(object);
576}
577
578static void
579conversations_view_finalize(GObject *object)
580{
581 G_OBJECT_CLASS(conversations_view_parent_class)->finalize(object);
582}
583
584static void
585conversations_view_class_init(ConversationsViewClass *klass)
586{
587 G_OBJECT_CLASS(klass)->finalize = conversations_view_finalize;
588 G_OBJECT_CLASS(klass)->dispose = conversations_view_dispose;
589}
590
591GtkWidget *
592conversations_view_new(AccountContainer* accountContainer)
593{
594 auto self = CONVERSATIONS_VIEW(g_object_new(CONVERSATIONS_VIEW_TYPE, NULL));
595 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
596
597 priv->accountContainer_ = accountContainer;
598
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400599 if (priv->accountContainer_)
600 build_conversations_view(self);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400601
602 return (GtkWidget *)self;
603}
604
605/**
606 * Select a conversation by uid (used to synchronize the selection)
607 * @param self
608 * @param uid of the conversation
609 */
610void
611conversations_view_select_conversation(ConversationsView *self, const std::string& uid)
612{
613 auto idx = 0;
614 auto model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
615 auto iterIsCorrect = true;
616 GtkTreeIter iter;
617
618 while(iterIsCorrect) {
619 iterIsCorrect = gtk_tree_model_iter_nth_child (model, &iter, nullptr, idx);
620 if (!iterIsCorrect)
621 break;
622 gchar *ringId;
623 gtk_tree_model_get (model, &iter,
624 0 /* col# */, &ringId /* data */,
625 -1);
626 if(std::string(ringId) == uid) {
627 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
628 gtk_tree_selection_select_iter(selection, &iter);
629 g_free(ringId);
630 return;
631 }
632 g_free(ringId);
633 idx++;
634 }
635}