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