blob: e7797c75aa288f72d769473c720c39e518899cfa [file] [log] [blame]
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04003 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "recentcontactsview.h"
21
22#include <gtk/gtk.h>
23#include <glib/gi18n.h>
Stepan Salenikovichf6078222016-10-03 17:31:16 -040024#include "models/gtkqtreemodel.h"
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040025#include "utils/calling.h"
26#include <memory>
27#include <globalinstances.h>
28#include "native/pixbufmanipulator.h"
29#include <contactmethod.h>
30#include "defines.h"
31#include "utils/models.h"
32#include <recentmodel.h>
33#include <call.h>
34#include "utils/menus.h"
35#include <itemdataroles.h>
36#include <callmodel.h>
37#include <QtCore/QItemSelectionModel>
38#include <historytimecategorymodel.h>
39#include <QtCore/QDateTime>
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -050040#include <QtCore/QMimeData>
Stepan Salenikovichd8765072016-01-14 10:58:51 -050041#include "utils/drawing.h"
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040042#include <numbercategory.h>
43#include "contactpopupmenu.h"
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040044
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -050045static constexpr const char* CALL_TARGET = "CALL_TARGET";
46static constexpr int CALL_TARGET_ID = 0;
47
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040048struct _RecentContactsView
49{
50 GtkTreeView parent;
51};
52
53struct _RecentContactsViewClass
54{
55 GtkTreeViewClass parent_class;
56};
57
58typedef struct _RecentContactsViewPrivate RecentContactsViewPrivate;
59
60struct _RecentContactsViewPrivate
61{
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040062 GtkWidget *popup_menu;
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -040063
64 QMetaObject::Connection selection_updated;
Stepan Salenikovichf53128f2016-10-07 10:32:16 -040065 QMetaObject::Connection layout_changed;
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040066};
67
68G_DEFINE_TYPE_WITH_PRIVATE(RecentContactsView, recent_contacts_view, GTK_TYPE_TREE_VIEW);
69
70#define RECENT_CONTACTS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RECENT_CONTACTS_VIEW_TYPE, RecentContactsViewPrivate))
71
72static void
Stepan Salenikovichc1323422016-01-06 10:54:44 -050073update_selection(GtkTreeSelection *selection, G_GNUC_UNUSED gpointer user_data)
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040074{
75 auto current_proxy = get_index_from_selection(selection);
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040076 auto current = RecentModel::instance().peopleProxy()->mapToSource(current_proxy);
Stepan Salenikovichc1323422016-01-06 10:54:44 -050077
78 RecentModel::instance().selectionModel()->setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
79
80 // update the CallModel selection since we rely on the UserActionModel
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040081 if (auto call_to_select = RecentModel::instance().getActiveCall(current)) {
82 CallModel::instance().selectCall(call_to_select);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040083 } else {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040084 CallModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040085 }
86}
87
88static void
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040089render_contact_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
90 GtkCellRenderer *cell,
91 GtkTreeModel *model,
92 GtkTreeIter *iter,
93 G_GNUC_UNUSED gpointer data)
94{
Stepan Salenikovichf6078222016-10-03 17:31:16 -040095 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040096
Stepan Salenikovichd8765072016-01-14 10:58:51 -050097 std::shared_ptr<GdkPixbuf> image;
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040098 /* we only want to render a photo for the top nodes: Person, ContactMethod (, later Conference) */
99 QVariant object = idx.data(static_cast<int>(Ring::Role::Object));
100 if (idx.isValid() && object.isValid()) {
101 QVariant var_photo;
102 if (auto person = object.value<Person *>()) {
103 var_photo = GlobalInstances::pixmapManipulator().contactPhoto(person, QSize(50, 50), false);
104 } else if (auto cm = object.value<ContactMethod *>()) {
105 /* get photo, note that this should in all cases be the fallback avatar, since there
106 * shouldn't be a person associated with this contact method */
107 var_photo = GlobalInstances::pixmapManipulator().callPhoto(cm, QSize(50, 50), false);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500108 } else if (auto call = object.value<Call *>()) {
109 if (call->type() == Call::Type::CONFERENCE) {
110 var_photo = GlobalInstances::pixmapManipulator().callPhoto(call, QSize(50, 50), false);
111 }
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400112 }
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500113 if (var_photo.isValid()) {
114 std::shared_ptr<GdkPixbuf> photo = var_photo.value<std::shared_ptr<GdkPixbuf>>();
115
116 auto unread = idx.data(static_cast<int>(Ring::Role::UnreadTextMessageCount));
117
118 image.reset(ring_draw_unread_messages(photo.get(), unread.toInt()), g_object_unref);
119 } else {
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400120 // set the width of the cell rendered to the with of the photo
121 // so that the other renderers are shifted to the right
122 g_object_set(G_OBJECT(cell), "width", 50, NULL);
123 }
124 }
125
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500126 g_object_set(G_OBJECT(cell), "pixbuf", image.get(), NULL);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400127}
128
129static void
130render_name_and_info(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
131 GtkCellRenderer *cell,
132 GtkTreeModel *model,
133 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400134 GtkTreeView *treeview)
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400135{
136 gchar *text = NULL;
137
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400138 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400139
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400140 // check if this iter is selected
141 gboolean is_selected = FALSE;
142 if (GTK_IS_TREE_VIEW(treeview)) {
143 auto selection = gtk_tree_view_get_selection(treeview);
144 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
145 }
146
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400147 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
148 if (idx.isValid() && type.isValid()) {
149 switch (type.value<Ring::ObjectType>()) {
150 case Ring::ObjectType::Person:
151 case Ring::ObjectType::ContactMethod:
152 {
153 auto var_name = idx.data(static_cast<int>(Ring::Role::Name));
154 auto var_lastused = idx.data(static_cast<int>(Ring::Role::LastUsed));
155 auto var_status = idx.data(static_cast<int>(Ring::Role::FormattedState));
156
157 QString name, status;
158
159 if (var_name.isValid())
160 name = var_name.value<QString>();
161
162 // show the status if there is a call, otherwise the last used date/time
163 if (var_status.isValid()) {
164 status += var_status.value<QString>();
165 }else if (var_lastused.isValid()) {
166 auto date_time = var_lastused.value<QDateTime>();
167 auto category = HistoryTimeCategoryModel::timeToHistoryConst(date_time.toTime_t());
168
169 // if it is 'today', then we only want to show the time
170 if (category != HistoryTimeCategoryModel::HistoryConst::Today) {
171 status += HistoryTimeCategoryModel::timeToHistoryCategory(date_time.toTime_t());
172 }
173 // we only want to show the time if it is less than a week ago
174 if (category < HistoryTimeCategoryModel::HistoryConst::A_week_ago) {
175 if (!status.isEmpty())
176 status += ", ";
177 status += QLocale::system().toString(date_time.time(), QLocale::ShortFormat);
178 }
179 }
180
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400181 /* we want the color of the status text to be the default color if this iter is
182 * selected so that the treeview is able to invert it against the selection color */
183 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400184 text = g_markup_printf_escaped("%s\n<span size=\"smaller\">%s</span>",
185 name.toUtf8().constData(),
186 status.toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400187 } else {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400188 text = g_markup_printf_escaped("%s\n<span size=\"smaller\" color=\"gray\">%s</span>",
189 name.toUtf8().constData(),
190 status.toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400191 }
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400192 }
193 break;
194 case Ring::ObjectType::Call:
195 {
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500196 // check if it is a conference
197 auto idx_source = RecentModel::instance().peopleProxy()->mapToSource(idx);
198 auto is_conference = RecentModel::instance().isConference(idx_source);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400199
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500200 if (is_conference) {
201 auto var_name = idx.data(static_cast<int>(Ring::Role::Name));
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400202 text = g_markup_escape_text(var_name.value<QString>().toUtf8().constData(), -1);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500203 } else {
204 auto parent_source = RecentModel::instance().peopleProxy()->mapToSource(idx.parent());
205 if (RecentModel::instance().isConference(parent_source)) {
206 // part of conference, simply display the name
207 auto var_name = idx.data(static_cast<int>(Ring::Role::Name));
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400208
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500209 /* we want the color of the name text to be the default color if this iter is
210 * selected so that the treeview is able to invert it against the selection color */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400211 if (is_selected) {
212 text = g_markup_printf_escaped("<span size=\"smaller\">%s</span>",
213 var_name.value<QString>().toUtf8().constData());
214 } else {
215 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"gray\">%s</span>",
216 var_name.value<QString>().toUtf8().constData());
217 }
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500218 } else {
219 // just a call, so display the state
220 auto var_status = idx.data(static_cast<int>(Ring::Role::FormattedState));
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400221
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500222 QString status;
223
224 if (var_status.isValid())
225 status += var_status.value<QString>();
226
227 /* we want the color of the status text to be the default color if this iter is
228 * selected so that the treeview is able to invert it against the selection color */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400229 if (is_selected) {
230 text = g_markup_printf_escaped("<span size=\"smaller\">%s</span>",
231 status.toUtf8().constData());
232 } else {
233 text = g_markup_printf_escaped("<span size=\"smaller\" color=\"gray\">%s</span>",
234 status.toUtf8().constData());
235 }
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500236 }
237 }
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400238 }
239 break;
240 case Ring::ObjectType::Media:
aviau9a277472016-12-13 14:54:58 -0500241 case Ring::ObjectType::Certificate:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400242 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500243 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400244 break;
245 }
246 }
247
248 g_object_set(G_OBJECT(cell), "markup", text, NULL);
249 g_free(text);
250}
251
252static void
253render_call_duration(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
254 GtkCellRenderer *cell,
255 GtkTreeModel *model,
256 GtkTreeIter *iter,
257 G_GNUC_UNUSED gpointer data)
258{
259 gchar *text = NULL;
260
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400261 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400262
263 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
264 if (idx.isValid() && type.isValid()) {
265 switch (type.value<Ring::ObjectType>()) {
266 case Ring::ObjectType::Person:
267 case Ring::ObjectType::ContactMethod:
268 {
269 // check if there are any children (calls); we need to convert to source model in
270 // case there is only one
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400271 auto idx_source = RecentModel::instance().peopleProxy()->mapToSource(idx);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400272 auto duration = idx.data(static_cast<int>(Ring::Role::Length));
273 if (idx_source.isValid()
274 && (idx_source.model()->rowCount(idx_source) == 1)
275 && duration.isValid())
276 {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400277 text = g_markup_escape_text(duration.value<QString>().toUtf8().constData(), -1);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400278 }
279 }
280 break;
281 case Ring::ObjectType::Call:
282 {
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500283 // do not display the duration if the call is part of a conference
284 auto parent_source = RecentModel::instance().peopleProxy()->mapToSource(idx.parent());
285 auto in_conference = RecentModel::instance().isConference(parent_source);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400286
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500287 if (!in_conference) {
288 auto duration = idx.data(static_cast<int>(Ring::Role::Length));
289
290 if (duration.isValid())
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400291 text = g_markup_escape_text(duration.value<QString>().toUtf8().constData(), -1);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500292 }
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400293 }
294 break;
295 case Ring::ObjectType::Media:
aviau9a277472016-12-13 14:54:58 -0500296 case Ring::ObjectType::Certificate:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400297 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500298 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400299 break;
300 }
301 }
302
303 g_object_set(G_OBJECT(cell), "markup", text, NULL);
304 g_free(text);
305}
306
307static void
308activate_item(GtkTreeView *tree_view,
309 GtkTreePath *path,
310 G_GNUC_UNUSED GtkTreeViewColumn *column,
311 G_GNUC_UNUSED gpointer user_data)
312{
313 auto model = gtk_tree_view_get_model(tree_view);
314 GtkTreeIter iter;
315 if (gtk_tree_model_get_iter(model, &iter, path)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400316 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400317 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
318 if (idx.isValid() && type.isValid()) {
319 switch (type.value<Ring::ObjectType>()) {
320 case Ring::ObjectType::Person:
321 {
322 // call the last used contact method
323 // TODO: if no contact methods have been used, offer a popup to Choose
324 auto p_var = idx.data(static_cast<int>(Ring::Role::Object));
325 if (p_var.isValid()) {
326 auto person = p_var.value<Person *>();
327 auto cms = person->phoneNumbers();
328
329 if (!cms.isEmpty()) {
330 auto last_used_cm = cms.at(0);
331 for (int i = 1; i < cms.size(); ++i) {
332 auto new_cm = cms.at(i);
333 if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0)
334 last_used_cm = new_cm;
335 }
336
337 place_new_call(last_used_cm);
338 }
339 }
340 }
341 break;
342 case Ring::ObjectType::ContactMethod:
343 {
344 // call the contact method
345 auto cm = idx.data(static_cast<int>(Ring::Role::Object));
346 if (cm.isValid())
347 place_new_call(cm.value<ContactMethod *>());
348 }
349 break;
350 case Ring::ObjectType::Call:
351 case Ring::ObjectType::Media:
aviau9a277472016-12-13 14:54:58 -0500352 case Ring::ObjectType::Certificate:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400353 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500354 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400355 break;
356 }
357 }
358 }
359}
360
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400361static void
362expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
363 GtkTreePath *path,
364 G_GNUC_UNUSED GtkTreeIter *iter,
365 GtkTreeView *treeview)
366{
367 if (gtk_tree_path_get_depth(path) > 1)
368 gtk_tree_view_expand_to_path(treeview, path);
369}
370
371static void
Stepan Salenikovichec0862f2015-10-16 15:49:42 -0400372scroll_to_selection(GtkTreeSelection *selection)
373{
374 GtkTreeModel *model = nullptr;
375 GtkTreeIter iter;
376 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
377 auto path = gtk_tree_model_get_path(model, &iter);
378 auto treeview = gtk_tree_selection_get_tree_view(selection);
379 gtk_tree_view_scroll_to_cell(treeview, path, nullptr, FALSE, 0.0, 0.0);
380 }
381}
382
383static void
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500384on_drag_data_get(GtkWidget *treeview,
385 G_GNUC_UNUSED GdkDragContext *context,
386 GtkSelectionData *data,
387 G_GNUC_UNUSED guint info,
388 G_GNUC_UNUSED guint time,
389 G_GNUC_UNUSED gpointer user_data)
390{
391 g_return_if_fail(IS_RECENT_CONTACTS_VIEW(treeview));
392
393 /* we always drag the selected row */
394 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
395 GtkTreeModel *model = NULL;
396 GtkTreeIter iter;
397
398 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
399 auto path_str = gtk_tree_model_get_string_from_iter(model, &iter);
400
401 gtk_selection_data_set(data,
402 gdk_atom_intern_static_string(CALL_TARGET),
403 8, /* bytes */
404 (guchar *)path_str,
405 strlen(path_str) + 1);
406
407 g_free(path_str);
408 } else {
409 g_warning("drag selection not valid");
410 }
411}
412
413static gboolean
414on_drag_drop(GtkWidget *treeview,
415 GdkDragContext *context,
416 gint x,
417 gint y,
418 guint time,
419 G_GNUC_UNUSED gpointer user_data)
420{
421 g_return_val_if_fail(IS_RECENT_CONTACTS_VIEW(treeview), FALSE);
422
423 GtkTreePath *path = NULL;
424 GtkTreeViewDropPosition drop_pos;
425
426 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
427 x, y, &path, &drop_pos)) {
428
429 GdkAtom target_type = gtk_drag_dest_find_target(treeview, context, NULL);
430
431 if (target_type != GDK_NONE) {
432 g_debug("can drop");
433 gtk_drag_get_data(treeview, context, target_type, time);
434 return TRUE;
435 }
436
437 gtk_tree_path_free(path);
438 }
439
440 return FALSE;
441}
442
443static gboolean
444on_drag_motion(GtkWidget *treeview,
445 GdkDragContext *context,
446 gint x,
447 gint y,
448 guint time,
449 G_GNUC_UNUSED gpointer user_data)
450{
451 g_return_val_if_fail(IS_RECENT_CONTACTS_VIEW(treeview), FALSE);
452
453 GtkTreePath *path = NULL;
454 GtkTreeViewDropPosition drop_pos;
455
456 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
457 x, y, &path, &drop_pos)) {
458 // we only want to drop on a row, not before or after
459 if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE) {
460 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
461 } else if (drop_pos == GTK_TREE_VIEW_DROP_AFTER) {
462 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
463 }
464 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
465 return TRUE;
466 } else {
467 // not a row in the treeview, so we cannot drop
468 return FALSE;
469 }
470}
471
472static void
473on_drag_data_received(GtkWidget *treeview,
474 GdkDragContext *context,
475 gint x,
476 gint y,
477 GtkSelectionData *data,
478 G_GNUC_UNUSED guint info,
479 guint time,
480 G_GNUC_UNUSED gpointer user_data)
481{
482 g_return_if_fail(IS_RECENT_CONTACTS_VIEW(treeview));
483
484 gboolean success = FALSE;
485
486 /* get the source and destination calls */
487 auto path_str_source = (gchar *)gtk_selection_data_get_data(data);
488 auto type = gtk_selection_data_get_data_type(data);
489 g_debug("data type: %s", gdk_atom_name(type));
490 if (path_str_source && strlen(path_str_source) > 0) {
491 g_debug("source path: %s", path_str_source);
492
493 /* get the destination path */
494 GtkTreePath *dest_path = NULL;
495 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview), x, y, &dest_path, NULL)) {
496 auto model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
497
498 GtkTreeIter source;
499 gtk_tree_model_get_iter_from_string(model, &source, path_str_source);
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400500 auto idx_source_proxy = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &source);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500501
502 GtkTreeIter dest;
503 gtk_tree_model_get_iter(model, &dest, dest_path);
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400504 auto idx_dest_proxy = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &dest);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500505
506 // get call objects and indeces from RecentModel indeces being drag and dropped
507 auto idx_source = RecentModel::instance().peopleProxy()->mapToSource(idx_source_proxy);
508 auto idx_dest = RecentModel::instance().peopleProxy()->mapToSource(idx_dest_proxy);
509 auto call_source = RecentModel::instance().getActiveCall(idx_source);
510 auto call_dest = RecentModel::instance().getActiveCall(idx_dest);
511 auto idx_call_source = CallModel::instance().getIndex(call_source);
512 auto idx_call_dest = CallModel::instance().getIndex(call_dest);
513
514 if (idx_call_source.isValid() && idx_call_dest.isValid()) {
515 QModelIndexList source_list;
516 source_list << idx_call_source;
517 auto mimeData = CallModel::instance().mimeData(source_list);
518 auto action = Call::DropAction::Conference;
519 mimeData->setProperty("dropAction", action);
520
521 if (CallModel::instance().dropMimeData(mimeData, Qt::MoveAction, idx_call_dest.row(), idx_call_dest.column(), idx_call_dest.parent())) {
522 success = TRUE;
523 } else {
524 g_warning("could not drop mime data");
525 }
526 } else {
527 g_warning("source or dest call not valid");
528 }
529
530 gtk_tree_path_free(dest_path);
531 }
532 }
533
534 gtk_drag_finish(context, success, FALSE, time);
535}
536
537static void
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400538recent_contacts_view_init(RecentContactsView *self)
539{
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400540 RecentContactsViewPrivate *priv = RECENT_CONTACTS_VIEW_GET_PRIVATE(self);
541
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400542 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
543 /* no need to show the expander since it will always be expanded */
544 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(self), FALSE);
545 /* disable default search, we will handle it ourselves via LRC;
546 * otherwise the search steals input focus on key presses */
547 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
548
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400549 GtkQTreeModel *recent_model = gtk_q_tree_model_new(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400550 RecentModel::instance().peopleProxy(),
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400551 1,
aviau271bcc22016-05-27 17:25:19 -0400552 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400553
554 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
555 GTK_TREE_MODEL(recent_model));
556
557 /* photo and name/contact method column */
558 GtkCellArea *area = gtk_cell_area_box_new();
559 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
560
561 /* photo renderer */
562 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
563 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
564
565 /* get the photo */
566 gtk_tree_view_column_set_cell_data_func(
567 column,
568 renderer,
569 (GtkTreeCellDataFunc)render_contact_photo,
570 NULL,
571 NULL);
572
573 /* name/cm and status renderer */
574 renderer = gtk_cell_renderer_text_new();
575 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
576 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
577
578 gtk_tree_view_column_set_cell_data_func(
579 column,
580 renderer,
581 (GtkTreeCellDataFunc)render_name_and_info,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400582 self,
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400583 NULL);
584
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400585 /* call duration */
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400586 renderer = gtk_cell_renderer_text_new();
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400587 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
588 gtk_cell_area_box_pack_end(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400589 gtk_tree_view_column_set_cell_data_func(
590 column,
591 renderer,
592 (GtkTreeCellDataFunc)render_call_duration,
593 NULL,
594 NULL);
595
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400596 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
597 gtk_tree_view_column_set_resizable(column, TRUE);
598 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400599 gtk_tree_view_expand_all(GTK_TREE_VIEW(self));
600
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400601 g_signal_connect(self, "row-activated", G_CALLBACK(activate_item), NULL);
602 g_signal_connect(recent_model, "row-inserted", G_CALLBACK(expand_if_child), self);
603
604 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500605 g_signal_connect(selection, "changed", G_CALLBACK(update_selection), NULL);
Stepan Salenikovichec0862f2015-10-16 15:49:42 -0400606 g_signal_connect(selection, "changed", G_CALLBACK(scroll_to_selection), NULL);
607 g_signal_connect_swapped(recent_model, "rows-reordered", G_CALLBACK(scroll_to_selection), selection);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400608
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500609 /* update the selection based on the RecentModel */
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400610 priv->selection_updated = QObject::connect(
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500611 RecentModel::instance().selectionModel(),
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400612 &QItemSelectionModel::currentChanged,
613 [self, recent_model](const QModelIndex current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500614 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
615
616 auto current_proxy = RecentModel::instance().peopleProxy()->mapFromSource(current);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400617
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400618 if (current.isValid()) {
619 /* select the current */
620 GtkTreeIter new_iter;
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400621 if (gtk_q_tree_model_source_index_to_iter(recent_model, current_proxy, &new_iter)) {
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400622 gtk_tree_selection_select_iter(selection, &new_iter);
623 }
624 } else {
625 gtk_tree_selection_unselect_all(selection);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400626 }
627 }
628 );
629
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400630 /* we may need to update the selection when the layout changes */
631 priv->layout_changed = QObject::connect(
632 RecentModel::instance().peopleProxy(),
633 &QAbstractItemModel::layoutChanged,
634 [self, recent_model]() {
635 auto idx = RecentModel::instance().selectionModel()->currentIndex();
636 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
637
638 auto idx_proxy = RecentModel::instance().peopleProxy()->mapFromSource(idx);
639
640 if (idx_proxy.isValid()) {
641 /* select the current */
642 GtkTreeIter iter;
643 if (gtk_q_tree_model_source_index_to_iter(recent_model, idx_proxy, &iter)) {
644 gtk_tree_selection_select_iter(selection, &iter);
645 }
646 } else {
647 gtk_tree_selection_unselect_all(selection);
648 }
649 }
650 );
651
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500652 /* drag and drop */
653 static GtkTargetEntry targetentries[] = {
654 { (gchar *)CALL_TARGET, GTK_TARGET_SAME_WIDGET, CALL_TARGET_ID },
655 };
656
657 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(self),
658 GDK_BUTTON1_MASK, targetentries, 1, (GdkDragAction)(GDK_ACTION_DEFAULT | GDK_ACTION_MOVE));
659
660 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(self),
661 targetentries, 1, GDK_ACTION_DEFAULT);
662
663 g_signal_connect(self, "drag-data-get", G_CALLBACK(on_drag_data_get), nullptr);
664 g_signal_connect(self, "drag-drop", G_CALLBACK(on_drag_drop), nullptr);
665 g_signal_connect(self, "drag-motion", G_CALLBACK(on_drag_motion), nullptr);
666 g_signal_connect(self, "drag_data_received", G_CALLBACK(on_drag_data_received), nullptr);
667
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400668 /* init popup menu */
669 priv->popup_menu = contact_popup_menu_new(GTK_TREE_VIEW(self));
670 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(contact_popup_menu_show), priv->popup_menu);
671
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400672 gtk_widget_show_all(GTK_WIDGET(self));
673}
674
675static void
676recent_contacts_view_dispose(GObject *object)
677{
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400678 RecentContactsView *self = RECENT_CONTACTS_VIEW(object);
679 RecentContactsViewPrivate *priv = RECENT_CONTACTS_VIEW_GET_PRIVATE(self);
680
681 QObject::disconnect(priv->selection_updated);
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400682 QObject::disconnect(priv->layout_changed);
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400683 gtk_widget_destroy(priv->popup_menu);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400684
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400685 G_OBJECT_CLASS(recent_contacts_view_parent_class)->dispose(object);
686}
687
688static void
689recent_contacts_view_finalize(GObject *object)
690{
691 G_OBJECT_CLASS(recent_contacts_view_parent_class)->finalize(object);
692}
693
694static void
695recent_contacts_view_class_init(RecentContactsViewClass *klass)
696{
697 G_OBJECT_CLASS(klass)->finalize = recent_contacts_view_finalize;
698 G_OBJECT_CLASS(klass)->dispose = recent_contacts_view_dispose;
699}
700
701GtkWidget *
702recent_contacts_view_new()
703{
704 gpointer self = g_object_new(RECENT_CONTACTS_VIEW_TYPE, NULL);
705
706 return (GtkWidget *)self;
707}