blob: 83be3e40a83f00dfb9c1d3fdc88f819cd5480612 [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
Nicolas Jagerd64d88e2017-12-11 14:26:00 -050090 auto conversationInfo = priv->accountContainer_->info.conversationModel->filteredConversation(row);
91 auto contactInfo = priv->accountContainer_->info.contactModel->getContact(conversationInfo.participants.front());
Sébastien Bline72d43c2017-10-03 11:37:33 -040092 std::shared_ptr<GdkPixbuf> image;
93 auto var_photo = GlobalInstances::pixmapManipulator().conversationPhoto(
Nicolas Jagerd64d88e2017-12-11 14:26:00 -050094 conversationInfo,
Sébastien Bline72d43c2017-10-03 11:37:33 -040095 priv->accountContainer_->info,
96 QSize(50, 50),
Nicolas Jagerd64d88e2017-12-11 14:26:00 -050097 contactInfo.isPresent
Sébastien Bline72d43c2017-10-03 11:37:33 -040098 );
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();
Guillaume Roguezc2095922017-12-14 14:07:10 -0500243 try {
244 auto contactInfo = priv->accountContainer_->info.contactModel->getContact(contactUri);
245 auto lastMessage = conversation.interactions.empty() ? "" :
246 conversation.interactions.at(conversation.lastMessageUid).body;
247 std::replace(lastMessage.begin(), lastMessage.end(), '\n', ' ');
248 gtk_list_store_append (store, &iter);
249 auto alias = contactInfo.profileInfo.alias;
250 alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
251 gtk_list_store_set (store, &iter,
252 0 /* col # */ , conversation.uid.c_str() /* celldata */,
253 1 /* col # */ , alias.c_str() /* celldata */,
254 2 /* col # */ , contactInfo.profileInfo.uri.c_str() /* celldata */,
255 3 /* col # */ , contactInfo.registeredName.c_str() /* celldata */,
256 4 /* col # */ , contactInfo.profileInfo.avatar.c_str() /* celldata */,
257 5 /* col # */ , lastMessage.c_str() /* celldata */,
258 -1 /* end */);
259 } catch (const std::out_of_range&) {
260 // ContactModel::getContact() exception
261 }
Sébastien Bline72d43c2017-10-03 11:37:33 -0400262 }
263
264 return GTK_TREE_MODEL (store);
265}
266
267static void
268call_conversation(GtkTreeView *self,
269 GtkTreePath *path,
270 G_GNUC_UNUSED GtkTreeViewColumn *column,
271 G_GNUC_UNUSED gpointer user_data)
272{
273 auto row = std::atoi(gtk_tree_path_to_string(path));
274 if (row == -1) return;
275 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
276 if (!priv) return;
277 auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(row);
278 priv->accountContainer_->info.conversationModel->placeCall(conversation.uid);
279}
280
281static void
282select_conversation(GtkTreeSelection *selection, ConversationsView *self)
283{
284 // Update popupMenu_
285 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
286 if (priv->popupMenu_) {
287 // Because popup menu is not up to date, we need to update it.
288 auto isVisible = gtk_widget_get_visible(priv->popupMenu_);
289 // Destroy the not up to date menu.
290 gtk_widget_hide(priv->popupMenu_);
291 gtk_widget_destroy(priv->popupMenu_);
292 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
293 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
294 auto nbItems = g_list_length(children);
295 // Show the new popupMenu_ should be visible
296 if (isVisible && nbItems > 0)
297 gtk_menu_popup(GTK_MENU(priv->popupMenu_), nullptr, nullptr, nullptr, nullptr, 0, gtk_get_current_event_time());
298 }
299 GtkTreeIter iter;
300 GtkTreeModel *model = nullptr;
301 gchar *conversationUid = nullptr;
302
303 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return;
304
305 gtk_tree_model_get(model, &iter,
306 0, &conversationUid,
307 -1);
308 priv->accountContainer_->info.conversationModel->selectConversation(std::string(conversationUid));
309}
310
311static void
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400312conversations_view_init(G_GNUC_UNUSED ConversationsView *self)
Sébastien Bline72d43c2017-10-03 11:37:33 -0400313{
314 // Nothing to do
315}
316
317static void
318show_popup_menu(ConversationsView *self, GdkEventButton *event)
319{
320 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
321 auto children = gtk_container_get_children (GTK_CONTAINER(priv->popupMenu_));
322 auto nbItems = g_list_length(children);
323 // Show the new popupMenu_ should be visible
324 if (nbItems > 0)
325 conversation_popup_menu_show(CONVERSATION_POPUP_MENU(priv->popupMenu_), event);
326}
327
328static void
329on_drag_data_get(GtkWidget *treeview,
330 G_GNUC_UNUSED GdkDragContext *context,
331 GtkSelectionData *data,
332 G_GNUC_UNUSED guint info,
333 G_GNUC_UNUSED guint time,
334 G_GNUC_UNUSED gpointer user_data)
335{
336 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
337
338 /* we always drag the selected row */
339 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
340 GtkTreeModel *model = NULL;
341 GtkTreeIter iter;
342
343 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
344 auto path_str = gtk_tree_model_get_string_from_iter(model, &iter);
345
346 gtk_selection_data_set(data,
347 gdk_atom_intern_static_string(CALL_TARGET),
348 8, /* bytes */
349 (guchar *)path_str,
350 strlen(path_str) + 1);
351
352 g_free(path_str);
353 } else {
354 g_warning("drag selection not valid");
355 }
356}
357
358static gboolean
359on_drag_drop(GtkWidget *treeview,
360 GdkDragContext *context,
361 gint x,
362 gint y,
363 guint time,
364 G_GNUC_UNUSED gpointer user_data)
365{
366 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
367
368 GtkTreePath *path = NULL;
369 GtkTreeViewDropPosition drop_pos;
370
371 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
372 x, y, &path, &drop_pos)) {
373
374 GdkAtom target_type = gtk_drag_dest_find_target(treeview, context, NULL);
375
376 if (target_type != GDK_NONE) {
377 g_debug("can drop");
378 gtk_drag_get_data(treeview, context, target_type, time);
379 return TRUE;
380 }
381
382 gtk_tree_path_free(path);
383 }
384
385 return FALSE;
386}
387
388static gboolean
389on_drag_motion(GtkWidget *treeview,
390 GdkDragContext *context,
391 gint x,
392 gint y,
393 guint time,
394 G_GNUC_UNUSED gpointer user_data)
395{
396 g_return_val_if_fail(IS_CONVERSATIONS_VIEW(treeview), FALSE);
397
398 GtkTreePath *path = NULL;
399 GtkTreeViewDropPosition drop_pos;
400
401 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
402 x, y, &path, &drop_pos)) {
403 // we only want to drop on a row, not before or after
404 if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE) {
405 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
406 } else if (drop_pos == GTK_TREE_VIEW_DROP_AFTER) {
407 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
408 }
409 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
410 return TRUE;
411 } else {
412 // not a row in the treeview, so we cannot drop
413 return FALSE;
414 }
415}
416
417static void
418on_drag_data_received(GtkWidget *treeview,
419 GdkDragContext *context,
420 gint x,
421 gint y,
422 GtkSelectionData *data,
423 G_GNUC_UNUSED guint info,
424 guint time,
425 G_GNUC_UNUSED gpointer user_data)
426{
427 g_return_if_fail(IS_CONVERSATIONS_VIEW(treeview));
428 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(treeview);
429
430 gboolean success = FALSE;
431
432 /* get the source and destination calls */
433 auto path_str_source = (gchar *)gtk_selection_data_get_data(data);
434 auto type = gtk_selection_data_get_data_type(data);
435 g_debug("data type: %s", gdk_atom_name(type));
436 if (path_str_source && strlen(path_str_source) > 0) {
437 g_debug("source path: %s", path_str_source);
438
439 /* get the destination path */
440 GtkTreePath *dest_path = NULL;
441 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview), x, y, &dest_path, NULL)) {
442 auto model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
443
444 GtkTreeIter source, dest;
445 gtk_tree_model_get_iter_from_string(model, &source, path_str_source);
446 gtk_tree_model_get_iter(model, &dest, dest_path);
447
448 gchar *conversationUidSrc = nullptr;
449 gchar *conversationUidDest = nullptr;
450
451 gtk_tree_model_get(model, &source,
452 0, &conversationUidSrc,
453 -1);
454 gtk_tree_model_get(model, &dest,
455 0, &conversationUidDest,
456 -1);
457
458 priv->accountContainer_->info.conversationModel->joinConversations(
459 conversationUidSrc,
460 conversationUidDest
461 );
462
463 gtk_tree_path_free(dest_path);
464 }
465 }
466
467 gtk_drag_finish(context, success, FALSE, time);
468}
469
470static void
471build_conversations_view(ConversationsView *self)
472{
473 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
474 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
475
476 auto model = create_and_fill_model(self);
477 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
478 GTK_TREE_MODEL(model));
479
480 // ringId method column
481 auto area = gtk_cell_area_box_new();
482 auto column = gtk_tree_view_column_new_with_area(area);
483
484 // render the photo
485 auto renderer = gtk_cell_renderer_pixbuf_new();
486 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
487
488 gtk_tree_view_column_set_cell_data_func(
489 column,
490 renderer,
491 (GtkTreeCellDataFunc)render_contact_photo,
492 self,
493 NULL);
494
495 // render name and last interaction
496 renderer = gtk_cell_renderer_text_new();
497 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
498 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
499
500 gtk_tree_view_column_set_cell_data_func(
501 column,
502 renderer,
503 (GtkTreeCellDataFunc)render_name_and_last_interaction,
504 self,
505 NULL);
506
507 // render time of last interaction and number of unread
508 renderer = gtk_cell_renderer_text_new();
509 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
510 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
511 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
512
513 gtk_tree_view_column_set_cell_data_func(
514 column,
515 renderer,
516 (GtkTreeCellDataFunc)render_time,
517 self,
518 NULL);
519
520 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
521
522 // This view should be synchronized and redraw at each update.
523 priv->modelSortedConnection_ = QObject::connect(
524 &*priv->accountContainer_->info.conversationModel,
525 &lrc::api::ConversationModel::modelSorted,
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 priv->filterChangedConnection_ = QObject::connect(
534 &*priv->accountContainer_->info.conversationModel,
535 &lrc::api::ConversationModel::filterChanged,
536 [self] () {
537 auto model = create_and_fill_model(self);
538
539 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
540 GTK_TREE_MODEL(model));
541 });
542
543 gtk_widget_show_all(GTK_WIDGET(self));
544
545 auto selectionNew = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
546 // One left click to select the conversation
547 g_signal_connect(selectionNew, "changed", G_CALLBACK(select_conversation), self);
548 // Two clicks to placeCall
549 g_signal_connect(self, "row-activated", G_CALLBACK(call_conversation), NULL);
550
551 priv->popupMenu_ = conversation_popup_menu_new(GTK_TREE_VIEW(self), priv->accountContainer_);
552 // Right click to show actions
553 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(show_popup_menu), self);
554
555 /* drag and drop */
556 static GtkTargetEntry targetentries[] = {
557 { (gchar *)CALL_TARGET, GTK_TARGET_SAME_WIDGET, CALL_TARGET_ID },
558 };
559
560 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(self),
561 GDK_BUTTON1_MASK, targetentries, 1, (GdkDragAction)(GDK_ACTION_DEFAULT | GDK_ACTION_MOVE));
562
563 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(self),
564 targetentries, 1, GDK_ACTION_DEFAULT);
565
566 g_signal_connect(self, "drag-data-get", G_CALLBACK(on_drag_data_get), nullptr);
567 g_signal_connect(self, "drag-drop", G_CALLBACK(on_drag_drop), nullptr);
568 g_signal_connect(self, "drag-motion", G_CALLBACK(on_drag_motion), nullptr);
569 g_signal_connect(self, "drag_data_received", G_CALLBACK(on_drag_data_received), nullptr);
570}
571
572static void
573conversations_view_dispose(GObject *object)
574{
575 auto self = CONVERSATIONS_VIEW(object);
576 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
577
578 QObject::disconnect(priv->selection_updated);
579 QObject::disconnect(priv->layout_changed);
580 QObject::disconnect(priv->modelSortedConnection_);
581 QObject::disconnect(priv->filterChangedConnection_);
582
583 gtk_widget_destroy(priv->popupMenu_);
584
585 G_OBJECT_CLASS(conversations_view_parent_class)->dispose(object);
586}
587
588static void
589conversations_view_finalize(GObject *object)
590{
591 G_OBJECT_CLASS(conversations_view_parent_class)->finalize(object);
592}
593
594static void
595conversations_view_class_init(ConversationsViewClass *klass)
596{
597 G_OBJECT_CLASS(klass)->finalize = conversations_view_finalize;
598 G_OBJECT_CLASS(klass)->dispose = conversations_view_dispose;
599}
600
601GtkWidget *
602conversations_view_new(AccountContainer* accountContainer)
603{
604 auto self = CONVERSATIONS_VIEW(g_object_new(CONVERSATIONS_VIEW_TYPE, NULL));
605 auto priv = CONVERSATIONS_VIEW_GET_PRIVATE(self);
606
607 priv->accountContainer_ = accountContainer;
608
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400609 if (priv->accountContainer_)
610 build_conversations_view(self);
Sébastien Bline72d43c2017-10-03 11:37:33 -0400611
612 return (GtkWidget *)self;
613}
614
615/**
616 * Select a conversation by uid (used to synchronize the selection)
617 * @param self
618 * @param uid of the conversation
619 */
620void
621conversations_view_select_conversation(ConversationsView *self, const std::string& uid)
622{
623 auto idx = 0;
624 auto model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
625 auto iterIsCorrect = true;
626 GtkTreeIter iter;
627
628 while(iterIsCorrect) {
629 iterIsCorrect = gtk_tree_model_iter_nth_child (model, &iter, nullptr, idx);
630 if (!iterIsCorrect)
631 break;
632 gchar *ringId;
633 gtk_tree_model_get (model, &iter,
634 0 /* col# */, &ringId /* data */,
635 -1);
636 if(std::string(ringId) == uid) {
637 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
638 gtk_tree_selection_select_iter(selection, &iter);
639 g_free(ringId);
640 return;
641 }
642 g_free(ringId);
643 idx++;
644 }
645}