blob: 54504ca3684260bf28f0dc087525b8cb32047290 [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;
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500124 gchar *uri;
Sébastien Bline72d43c2017-10-03 11:37:33 -0400125
126 gtk_tree_model_get (model, iter,
127 0 /* col# */, &uid /* data */,
128 1 /* col# */, &alias /* data */,
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500129 2 /* col# */, &uri /* data */,
130 3 /* col# */, &registeredName /* data */,
131 5 /* col# */, &lastInteraction /* data */,
Sébastien Bline72d43c2017-10-03 11:37:33 -0400132 -1);
133
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500134 auto bestId = std::string(registeredName).empty() ? uri: registeredName;
Sébastien Bline72d43c2017-10-03 11:37:33 -0400135 if (std::string(alias).empty()) {
136 // For conversations with contacts with no alias
137 text = g_markup_printf_escaped(
138 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500139 bestId,
Sébastien Bline72d43c2017-10-03 11:37:33 -0400140 lastInteraction
141 );
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500142 } else if (std::string(alias) == std::string(bestId)
143 || std::string(bestId).empty() || std::string(uid).empty()) {
Sébastien Bline72d43c2017-10-03 11:37:33 -0400144 // For temporary item
145 text = g_markup_printf_escaped(
146 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
147 alias,
148 lastInteraction
149 );
150 } else {
151 // For conversations with contacts with alias
152 text = g_markup_printf_escaped(
153 "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
154 alias,
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500155 bestId,
Sébastien Bline72d43c2017-10-03 11:37:33 -0400156 lastInteraction
157 );
158 }
159
160 g_object_set(G_OBJECT(cell), "markup", text, NULL);
161 g_free(uid);
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500162 g_free(uri);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400163 g_free(alias);
164 g_free(registeredName);
165}
166
167static void
168render_time(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
169 GtkCellRenderer *cell,
170 GtkTreeModel *model,
171 GtkTreeIter *iter,
172 G_GNUC_UNUSED GtkTreeView *treeview)
173{
174
175 // Get active conversation
176 auto path = gtk_tree_model_get_path(model, iter);
177 auto row = std::atoi(gtk_tree_path_to_string(path));
178 if (row == -1) return;
179 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(treeview);
180 if (!priv) return;
181
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400182 char empty[] = {'\0'};
183 gchar *text = empty;
Sébastien Bline72d43c2017-10-03 11:37:33 -0400184
185 try
186 {
187 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
188 auto callId = conversation.confId.empty() ? conversation.callId : conversation.confId;
189 if (!callId.empty()) {
190 auto call = priv->accountContainer_->info.callModel->getCall(callId);
191 text = g_markup_printf_escaped("%s",
192 lrc::api::call::to_string(call.status).c_str()
193 );
194 } else if (conversation.interactions.empty()) {
Sébastien Bline72d43c2017-10-03 11:37:33 -0400195 } else {
196 auto lastUid = conversation.lastMessageUid;
197 if (conversation.interactions.find(lastUid) == conversation.interactions.end()) {
Sébastien Bline72d43c2017-10-03 11:37:33 -0400198 } else {
199 std::time_t lastInteractionTimestamp = conversation.interactions[lastUid].timestamp;
200 std::time_t now = std::time(nullptr);
201 char interactionDay[100];
202 char nowDay[100];
203 std::strftime(interactionDay, sizeof(interactionDay), "%D", std::localtime(&lastInteractionTimestamp));
204 std::strftime(nowDay, sizeof(nowDay), "%D", std::localtime(&now));
205
206
207 if (std::string(interactionDay) == std::string(nowDay)) {
208 char interactionTime[100];
209 std::strftime(interactionTime, sizeof(interactionTime), "%R", std::localtime(&lastInteractionTimestamp));
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400210 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"#666\">%s</span>", &interactionTime[0]);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400211 } else {
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400212 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"#666\">%s</span>", &interactionDay[0]);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400213 }
214 }
215 }
216 }
217 catch (const std::exception&)
218 {
219 g_warning("Can't get conversation at row %i", row);
220 }
221
222 g_object_set(G_OBJECT(cell), "markup", text, NULL);
223}
224
225static GtkTreeModel*
226create_and_fill_model(ConversationsView *self)
227{
228 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500229 auto store = gtk_list_store_new (6 /* # of cols */ ,
230 G_TYPE_STRING,
Sébastien Bline72d43c2017-10-03 11:37:33 -0400231 G_TYPE_STRING,
232 G_TYPE_STRING,
233 G_TYPE_STRING,
234 G_TYPE_STRING,
235 G_TYPE_STRING,
236 G_TYPE_UINT);
237 if(!priv) GTK_TREE_MODEL (store);
238 GtkTreeIter iter;
239
240 for (auto conversation : priv->accountContainer_->info.conversationModel->allFilteredConversations()) {
241 if (conversation.participants.empty()) break; // Should not
242 auto contactUri = conversation.participants.front();
243 auto contactInfo = priv->accountContainer_->info.contactModel->getContact(contactUri);
244 auto lastMessage = conversation.interactions.empty() ? "" :
245 conversation.interactions.at(conversation.lastMessageUid).body;
246 std::replace(lastMessage.begin(), lastMessage.end(), '\n', ' ');
247 gtk_list_store_append (store, &iter);
248 auto alias = contactInfo.profileInfo.alias;
249 alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
250 gtk_list_store_set (store, &iter,
251 0 /* col # */ , conversation.uid.c_str() /* celldata */,
252 1 /* col # */ , alias.c_str() /* celldata */,
Sébastien Blinf11c9f32017-12-01 14:09:06 -0500253 2 /* col # */ , contactInfo.profileInfo.uri.c_str() /* celldata */,
254 3 /* col # */ , contactInfo.registeredName.c_str() /* celldata */,
255 4 /* col # */ , contactInfo.profileInfo.avatar.c_str() /* celldata */,
256 5 /* col # */ , lastMessage.c_str() /* celldata */,
Sébastien Bline72d43c2017-10-03 11:37:33 -0400257 -1 /* end */);
258 }
259
260 return GTK_TREE_MODEL (store);
261}
262
263static void
264call_conversation(GtkTreeView *self,
265 GtkTreePath *path,
266 G_GNUC_UNUSED GtkTreeViewColumn *column,
267 G_GNUC_UNUSED gpointer user_data)
268{
269 auto row = std::atoi(gtk_tree_path_to_string(path));
270 if (row == -1) return;
271 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
272 if (!priv) return;
273 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
274 priv->accountContainer_->info.conversationModel->placeCall(conversation.uid);
275}
276
277static void
278select_conversation(GtkTreeSelection *selection, ConversationsView *self)
279{
280 // Update popupMenu_
281 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
282 if (priv->popupMenu_) {
283 // Because popup menu is not up to date, we need to update it.
284 auto isVisible = gtk_widget_get_visible(priv->popupMenu_);
285 // Destroy the not up to date menu.
286 gtk_widget_hide(priv->popupMenu_);
287 gtk_widget_destroy(priv->popupMenu_);
288 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
289 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
290 auto nbItems = g_list_length(children);
291 // Show the new popupMenu_ should be visible
292 if (isVisible && nbItems > 0)
293 gtk_menu_popup(GTK_MENU(priv->popupMenu_), nullptr, nullptr, nullptr, nullptr, 0, gtk_get_current_event_time());
294 }
295 GtkTreeIter iter;
296 GtkTreeModel *model = nullptr;
297 gchar *conversationUid = nullptr;
298
299 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return;
300
301 gtk_tree_model_get(model, &iter,
302 0, &conversationUid,
303 -1);
304 priv->accountContainer_->info.conversationModel->selectConversation(std::string(conversationUid));
305}
306
307static void
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400308conversations_view_init(G_GNUC_UNUSED ConversationsView *self)
Sébastien Bline72d43c2017-10-03 11:37:33 -0400309{
310 // Nothing to do
311}
312
313static void
314show_popup_menu(ConversationsView *self, GdkEventButton *event)
315{
316 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
317 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
318 auto nbItems = g_list_length(children);
319 // Show the new popupMenu_ should be visible
320 if (nbItems > 0)
321 conversation_popup_menu_show(CONVERSATION_POPUP_MENU(priv->popupMenu_), event);
322}
323
324static void
325on_drag_data_get(GtkWidget *treeview,
326 G_GNUC_UNUSED GdkDragContext *context,
327 GtkSelectionData *data,
328 G_GNUC_UNUSED guint info,
329 G_GNUC_UNUSED guint time,
330 G_GNUC_UNUSED gpointer user_data)
331{
332 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
333
334 /* we always drag the selected row */
335 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
336 GtkTreeModel *model = NULL;
337 GtkTreeIter iter;
338
339 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
340 auto path_str = gtk_tree_model_get_string_from_iter(model, &iter);
341
342 gtk_selection_data_set(data,
343 gdk_atom_intern_static_string(CALL_TARGET),
344 8, /* bytes */
345 (guchar *)path_str,
346 strlen(path_str) + 1);
347
348 g_free(path_str);
349 } else {
350 g_warning("drag selection not valid");
351 }
352}
353
354static gboolean
355on_drag_drop(GtkWidget *treeview,
356 GdkDragContext *context,
357 gint x,
358 gint y,
359 guint time,
360 G_GNUC_UNUSED gpointer user_data)
361{
362 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
363
364 GtkTreePath *path = NULL;
365 GtkTreeViewDropPosition drop_pos;
366
367 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
368 x, y, &path, &drop_pos)) {
369
370 GdkAtom target_type = gtk_drag_dest_find_target(treeview, context, NULL);
371
372 if (target_type != GDK_NONE) {
373 g_debug("can drop");
374 gtk_drag_get_data(treeview, context, target_type, time);
375 return TRUE;
376 }
377
378 gtk_tree_path_free(path);
379 }
380
381 return FALSE;
382}
383
384static gboolean
385on_drag_motion(GtkWidget *treeview,
386 GdkDragContext *context,
387 gint x,
388 gint y,
389 guint time,
390 G_GNUC_UNUSED gpointer user_data)
391{
392 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
393
394 GtkTreePath *path = NULL;
395 GtkTreeViewDropPosition drop_pos;
396
397 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
398 x, y, &path, &drop_pos)) {
399 // we only want to drop on a row, not before or after
400 if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE) {
401 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
402 } else if (drop_pos == GTK_TREE_VIEW_DROP_AFTER) {
403 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
404 }
405 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
406 return TRUE;
407 } else {
408 // not a row in the treeview, so we cannot drop
409 return FALSE;
410 }
411}
412
413static void
414on_drag_data_received(GtkWidget *treeview,
415 GdkDragContext *context,
416 gint x,
417 gint y,
418 GtkSelectionData *data,
419 G_GNUC_UNUSED guint info,
420 guint time,
421 G_GNUC_UNUSED gpointer user_data)
422{
423 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
424 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(treeview);
425
426 gboolean success = FALSE;
427
428 /* get the source and destination calls */
429 auto path_str_source = (gchar *)gtk_selection_data_get_data(data);
430 auto type = gtk_selection_data_get_data_type(data);
431 g_debug("data type: %s", gdk_atom_name(type));
432 if (path_str_source && strlen(path_str_source) > 0) {
433 g_debug("source path: %s", path_str_source);
434
435 /* get the destination path */
436 GtkTreePath *dest_path = NULL;
437 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview), x, y, &dest_path, NULL)) {
438 auto model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
439
440 GtkTreeIter source, dest;
441 gtk_tree_model_get_iter_from_string(model, &source, path_str_source);
442 gtk_tree_model_get_iter(model, &dest, dest_path);
443
444 gchar *conversationUidSrc = nullptr;
445 gchar *conversationUidDest = nullptr;
446
447 gtk_tree_model_get(model, &source,
448 0, &conversationUidSrc,
449 -1);
450 gtk_tree_model_get(model, &dest,
451 0, &conversationUidDest,
452 -1);
453
454 priv->accountContainer_->info.conversationModel->joinConversations(
455 conversationUidSrc,
456 conversationUidDest
457 );
458
459 gtk_tree_path_free(dest_path);
460 }
461 }
462
463 gtk_drag_finish(context, success, FALSE, time);
464}
465
466static void
467build_conversations_view(ConversationsView *self)
468{
469 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
470 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
471
472 auto model = create_and_fill_model(self);
473 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
474 GTK_TREE_MODEL(model));
475
476 // ringId method column
477 auto area = gtk_cell_area_box_new();
478 auto column = gtk_tree_view_column_new_with_area(area);
479
480 // render the photo
481 auto renderer = gtk_cell_renderer_pixbuf_new();
482 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
483
484 gtk_tree_view_column_set_cell_data_func(
485 column,
486 renderer,
487 (GtkTreeCellDataFunc)render_contact_photo,
488 self,
489 NULL);
490
491 // render name and last interaction
492 renderer = gtk_cell_renderer_text_new();
493 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
494 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
495
496 gtk_tree_view_column_set_cell_data_func(
497 column,
498 renderer,
499 (GtkTreeCellDataFunc)render_name_and_last_interaction,
500 self,
501 NULL);
502
503 // render time of last interaction and number of unread
504 renderer = gtk_cell_renderer_text_new();
505 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
506 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
507 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
508
509 gtk_tree_view_column_set_cell_data_func(
510 column,
511 renderer,
512 (GtkTreeCellDataFunc)render_time,
513 self,
514 NULL);
515
516 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
517
518 // This view should be synchronized and redraw at each update.
519 priv->modelSortedConnection_ = QObject::connect(
520 &*priv->accountContainer_->info.conversationModel,
521 &lrc::api::ConversationModel::modelSorted,
522 [self] () {
523 auto model = create_and_fill_model(self);
524
525 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
526 GTK_TREE_MODEL(model));
527 });
528
529 priv->filterChangedConnection_ = QObject::connect(
530 &*priv->accountContainer_->info.conversationModel,
531 &lrc::api::ConversationModel::filterChanged,
532 [self] () {
533 auto model = create_and_fill_model(self);
534
535 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
536 GTK_TREE_MODEL(model));
537 });
538
539 gtk_widget_show_all(GTK_WIDGET(self));
540
541 auto selectionNew = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
542 // One left click to select the conversation
543 g_signal_connect(selectionNew, "changed", G_CALLBACK(select_conversation), self);
544 // Two clicks to placeCall
545 g_signal_connect(self, "row-activated", G_CALLBACK(call_conversation), NULL);
546
547 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
548 // Right click to show actions
549 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(show_popup_menu), self);
550
551 /* drag and drop */
552 static GtkTargetEntry targetentries[] = {
553 { (gchar *)CALL_TARGET, GTK_TARGET_SAME_WIDGET, CALL_TARGET_ID },
554 };
555
556 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(self),
557 GDK_BUTTON1_MASK, targetentries, 1, (GdkDragAction)(GDK_ACTION_DEFAULT | GDK_ACTION_MOVE));
558
559 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(self),
560 targetentries, 1, GDK_ACTION_DEFAULT);
561
562 g_signal_connect(self, "drag-data-get", G_CALLBACK(on_drag_data_get), nullptr);
563 g_signal_connect(self, "drag-drop", G_CALLBACK(on_drag_drop), nullptr);
564 g_signal_connect(self, "drag-motion", G_CALLBACK(on_drag_motion), nullptr);
565 g_signal_connect(self, "drag_data_received", G_CALLBACK(on_drag_data_received), nullptr);
566}
567
568static void
569conversations_view_dispose(GObject *object)
570{
571 auto self = CONVERSATIONS_VIEW(object);
572 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
573
574 QObject::disconnect(priv->selection_updated);
575 QObject::disconnect(priv->layout_changed);
576 QObject::disconnect(priv->modelSortedConnection_);
577 QObject::disconnect(priv->filterChangedConnection_);
578
579 gtk_widget_destroy(priv->popupMenu_);
580
581 G_OBJECT_CLASS(conversations_view_parent_class)->dispose(object);
582}
583
584static void
585conversations_view_finalize(GObject *object)
586{
587 G_OBJECT_CLASS(conversations_view_parent_class)->finalize(object);
588}
589
590static void
591conversations_view_class_init(ConversationsViewClass *klass)
592{
593 G_OBJECT_CLASS(klass)->finalize = conversations_view_finalize;
594 G_OBJECT_CLASS(klass)->dispose = conversations_view_dispose;
595}
596
597GtkWidget *
598conversations_view_new(AccountContainer* accountContainer)
599{
600 auto self = CONVERSATIONS_VIEW(g_object_new(CONVERSATIONS_VIEW_TYPE, NULL));
601 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
602
603 priv->accountContainer_ = accountContainer;
604
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400605 if (priv->accountContainer_)
606 build_conversations_view(self);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400607
608 return (GtkWidget *)self;
609}
610
611/**
612 * Select a conversation by uid (used to synchronize the selection)
613 * @param self
614 * @param uid of the conversation
615 */
616void
617conversations_view_select_conversation(ConversationsView *self, const std::string& uid)
618{
619 auto idx = 0;
620 auto model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
621 auto iterIsCorrect = true;
622 GtkTreeIter iter;
623
624 while(iterIsCorrect) {
625 iterIsCorrect = gtk_tree_model_iter_nth_child (model, &iter, nullptr, idx);
626 if (!iterIsCorrect)
627 break;
628 gchar *ringId;
629 gtk_tree_model_get (model, &iter,
630 0 /* col# */, &ringId /* data */,
631 -1);
632 if(std::string(ringId) == uid) {
633 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
634 gtk_tree_selection_select_iter(selection, &iter);
635 g_free(ringId);
636 return;
637 }
638 g_free(ringId);
639 idx++;
640 }
641}