blob: 51c788dcc29933c8b52ee05754c565ac38dac606 [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:
241 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500242 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400243 break;
244 }
245 }
246
247 g_object_set(G_OBJECT(cell), "markup", text, NULL);
248 g_free(text);
249}
250
251static void
252render_call_duration(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
253 GtkCellRenderer *cell,
254 GtkTreeModel *model,
255 GtkTreeIter *iter,
256 G_GNUC_UNUSED gpointer data)
257{
258 gchar *text = NULL;
259
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400260 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400261
262 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
263 if (idx.isValid() && type.isValid()) {
264 switch (type.value<Ring::ObjectType>()) {
265 case Ring::ObjectType::Person:
266 case Ring::ObjectType::ContactMethod:
267 {
268 // check if there are any children (calls); we need to convert to source model in
269 // case there is only one
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400270 auto idx_source = RecentModel::instance().peopleProxy()->mapToSource(idx);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400271 auto duration = idx.data(static_cast<int>(Ring::Role::Length));
272 if (idx_source.isValid()
273 && (idx_source.model()->rowCount(idx_source) == 1)
274 && duration.isValid())
275 {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400276 text = g_markup_escape_text(duration.value<QString>().toUtf8().constData(), -1);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400277 }
278 }
279 break;
280 case Ring::ObjectType::Call:
281 {
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500282 // do not display the duration if the call is part of a conference
283 auto parent_source = RecentModel::instance().peopleProxy()->mapToSource(idx.parent());
284 auto in_conference = RecentModel::instance().isConference(parent_source);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400285
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500286 if (!in_conference) {
287 auto duration = idx.data(static_cast<int>(Ring::Role::Length));
288
289 if (duration.isValid())
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400290 text = g_markup_escape_text(duration.value<QString>().toUtf8().constData(), -1);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500291 }
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400292 }
293 break;
294 case Ring::ObjectType::Media:
295 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500296 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400297 break;
298 }
299 }
300
301 g_object_set(G_OBJECT(cell), "markup", text, NULL);
302 g_free(text);
303}
304
305static void
306activate_item(GtkTreeView *tree_view,
307 GtkTreePath *path,
308 G_GNUC_UNUSED GtkTreeViewColumn *column,
309 G_GNUC_UNUSED gpointer user_data)
310{
311 auto model = gtk_tree_view_get_model(tree_view);
312 GtkTreeIter iter;
313 if (gtk_tree_model_get_iter(model, &iter, path)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400314 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400315 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
316 if (idx.isValid() && type.isValid()) {
317 switch (type.value<Ring::ObjectType>()) {
318 case Ring::ObjectType::Person:
319 {
320 // call the last used contact method
321 // TODO: if no contact methods have been used, offer a popup to Choose
322 auto p_var = idx.data(static_cast<int>(Ring::Role::Object));
323 if (p_var.isValid()) {
324 auto person = p_var.value<Person *>();
325 auto cms = person->phoneNumbers();
326
327 if (!cms.isEmpty()) {
328 auto last_used_cm = cms.at(0);
329 for (int i = 1; i < cms.size(); ++i) {
330 auto new_cm = cms.at(i);
331 if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0)
332 last_used_cm = new_cm;
333 }
334
335 place_new_call(last_used_cm);
336 }
337 }
338 }
339 break;
340 case Ring::ObjectType::ContactMethod:
341 {
342 // call the contact method
343 auto cm = idx.data(static_cast<int>(Ring::Role::Object));
344 if (cm.isValid())
345 place_new_call(cm.value<ContactMethod *>());
346 }
347 break;
348 case Ring::ObjectType::Call:
349 case Ring::ObjectType::Media:
350 // nothing to do for now
Stepan Salenikovich4e7ea712015-12-24 14:04:37 -0500351 case Ring::ObjectType::COUNT__:
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400352 break;
353 }
354 }
355 }
356}
357
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400358static void
359expand_if_child(G_GNUC_UNUSED GtkTreeModel *tree_model,
360 GtkTreePath *path,
361 G_GNUC_UNUSED GtkTreeIter *iter,
362 GtkTreeView *treeview)
363{
364 if (gtk_tree_path_get_depth(path) > 1)
365 gtk_tree_view_expand_to_path(treeview, path);
366}
367
368static void
Stepan Salenikovichec0862f2015-10-16 15:49:42 -0400369scroll_to_selection(GtkTreeSelection *selection)
370{
371 GtkTreeModel *model = nullptr;
372 GtkTreeIter iter;
373 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
374 auto path = gtk_tree_model_get_path(model, &iter);
375 auto treeview = gtk_tree_selection_get_tree_view(selection);
376 gtk_tree_view_scroll_to_cell(treeview, path, nullptr, FALSE, 0.0, 0.0);
377 }
378}
379
380static void
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500381on_drag_data_get(GtkWidget *treeview,
382 G_GNUC_UNUSED GdkDragContext *context,
383 GtkSelectionData *data,
384 G_GNUC_UNUSED guint info,
385 G_GNUC_UNUSED guint time,
386 G_GNUC_UNUSED gpointer user_data)
387{
388 g_return_if_fail(IS_RECENT_CONTACTS_VIEW(treeview));
389
390 /* we always drag the selected row */
391 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
392 GtkTreeModel *model = NULL;
393 GtkTreeIter iter;
394
395 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
396 auto path_str = gtk_tree_model_get_string_from_iter(model, &iter);
397
398 gtk_selection_data_set(data,
399 gdk_atom_intern_static_string(CALL_TARGET),
400 8, /* bytes */
401 (guchar *)path_str,
402 strlen(path_str) + 1);
403
404 g_free(path_str);
405 } else {
406 g_warning("drag selection not valid");
407 }
408}
409
410static gboolean
411on_drag_drop(GtkWidget *treeview,
412 GdkDragContext *context,
413 gint x,
414 gint y,
415 guint time,
416 G_GNUC_UNUSED gpointer user_data)
417{
418 g_return_val_if_fail(IS_RECENT_CONTACTS_VIEW(treeview), FALSE);
419
420 GtkTreePath *path = NULL;
421 GtkTreeViewDropPosition drop_pos;
422
423 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
424 x, y, &path, &drop_pos)) {
425
426 GdkAtom target_type = gtk_drag_dest_find_target(treeview, context, NULL);
427
428 if (target_type != GDK_NONE) {
429 g_debug("can drop");
430 gtk_drag_get_data(treeview, context, target_type, time);
431 return TRUE;
432 }
433
434 gtk_tree_path_free(path);
435 }
436
437 return FALSE;
438}
439
440static gboolean
441on_drag_motion(GtkWidget *treeview,
442 GdkDragContext *context,
443 gint x,
444 gint y,
445 guint time,
446 G_GNUC_UNUSED gpointer user_data)
447{
448 g_return_val_if_fail(IS_RECENT_CONTACTS_VIEW(treeview), FALSE);
449
450 GtkTreePath *path = NULL;
451 GtkTreeViewDropPosition drop_pos;
452
453 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview),
454 x, y, &path, &drop_pos)) {
455 // we only want to drop on a row, not before or after
456 if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE) {
457 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
458 } else if (drop_pos == GTK_TREE_VIEW_DROP_AFTER) {
459 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(treeview), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
460 }
461 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
462 return TRUE;
463 } else {
464 // not a row in the treeview, so we cannot drop
465 return FALSE;
466 }
467}
468
469static void
470on_drag_data_received(GtkWidget *treeview,
471 GdkDragContext *context,
472 gint x,
473 gint y,
474 GtkSelectionData *data,
475 G_GNUC_UNUSED guint info,
476 guint time,
477 G_GNUC_UNUSED gpointer user_data)
478{
479 g_return_if_fail(IS_RECENT_CONTACTS_VIEW(treeview));
480
481 gboolean success = FALSE;
482
483 /* get the source and destination calls */
484 auto path_str_source = (gchar *)gtk_selection_data_get_data(data);
485 auto type = gtk_selection_data_get_data_type(data);
486 g_debug("data type: %s", gdk_atom_name(type));
487 if (path_str_source && strlen(path_str_source) > 0) {
488 g_debug("source path: %s", path_str_source);
489
490 /* get the destination path */
491 GtkTreePath *dest_path = NULL;
492 if (gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(treeview), x, y, &dest_path, NULL)) {
493 auto model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
494
495 GtkTreeIter source;
496 gtk_tree_model_get_iter_from_string(model, &source, path_str_source);
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400497 auto idx_source_proxy = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &source);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500498
499 GtkTreeIter dest;
500 gtk_tree_model_get_iter(model, &dest, dest_path);
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400501 auto idx_dest_proxy = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &dest);
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500502
503 // get call objects and indeces from RecentModel indeces being drag and dropped
504 auto idx_source = RecentModel::instance().peopleProxy()->mapToSource(idx_source_proxy);
505 auto idx_dest = RecentModel::instance().peopleProxy()->mapToSource(idx_dest_proxy);
506 auto call_source = RecentModel::instance().getActiveCall(idx_source);
507 auto call_dest = RecentModel::instance().getActiveCall(idx_dest);
508 auto idx_call_source = CallModel::instance().getIndex(call_source);
509 auto idx_call_dest = CallModel::instance().getIndex(call_dest);
510
511 if (idx_call_source.isValid() && idx_call_dest.isValid()) {
512 QModelIndexList source_list;
513 source_list << idx_call_source;
514 auto mimeData = CallModel::instance().mimeData(source_list);
515 auto action = Call::DropAction::Conference;
516 mimeData->setProperty("dropAction", action);
517
518 if (CallModel::instance().dropMimeData(mimeData, Qt::MoveAction, idx_call_dest.row(), idx_call_dest.column(), idx_call_dest.parent())) {
519 success = TRUE;
520 } else {
521 g_warning("could not drop mime data");
522 }
523 } else {
524 g_warning("source or dest call not valid");
525 }
526
527 gtk_tree_path_free(dest_path);
528 }
529 }
530
531 gtk_drag_finish(context, success, FALSE, time);
532}
533
534static void
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400535recent_contacts_view_init(RecentContactsView *self)
536{
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400537 RecentContactsViewPrivate *priv = RECENT_CONTACTS_VIEW_GET_PRIVATE(self);
538
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400539 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), FALSE);
540 /* no need to show the expander since it will always be expanded */
541 gtk_tree_view_set_show_expanders(GTK_TREE_VIEW(self), FALSE);
542 /* disable default search, we will handle it ourselves via LRC;
543 * otherwise the search steals input focus on key presses */
544 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
545
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400546 GtkQTreeModel *recent_model = gtk_q_tree_model_new(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400547 RecentModel::instance().peopleProxy(),
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400548 1,
aviau271bcc22016-05-27 17:25:19 -0400549 0, Qt::DisplayRole, G_TYPE_STRING);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400550
551 gtk_tree_view_set_model(GTK_TREE_VIEW(self),
552 GTK_TREE_MODEL(recent_model));
553
554 /* photo and name/contact method column */
555 GtkCellArea *area = gtk_cell_area_box_new();
556 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
557
558 /* photo renderer */
559 GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();
560 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
561
562 /* get the photo */
563 gtk_tree_view_column_set_cell_data_func(
564 column,
565 renderer,
566 (GtkTreeCellDataFunc)render_contact_photo,
567 NULL,
568 NULL);
569
570 /* name/cm and status renderer */
571 renderer = gtk_cell_renderer_text_new();
572 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
573 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
574
575 gtk_tree_view_column_set_cell_data_func(
576 column,
577 renderer,
578 (GtkTreeCellDataFunc)render_name_and_info,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400579 self,
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400580 NULL);
581
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400582 /* call duration */
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400583 renderer = gtk_cell_renderer_text_new();
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400584 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
585 gtk_cell_area_box_pack_end(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400586 gtk_tree_view_column_set_cell_data_func(
587 column,
588 renderer,
589 (GtkTreeCellDataFunc)render_call_duration,
590 NULL,
591 NULL);
592
Stepan Salenikoviche8ef9352015-10-27 19:07:46 -0400593 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
594 gtk_tree_view_column_set_resizable(column, TRUE);
595 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400596 gtk_tree_view_expand_all(GTK_TREE_VIEW(self));
597
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400598 g_signal_connect(self, "row-activated", G_CALLBACK(activate_item), NULL);
599 g_signal_connect(recent_model, "row-inserted", G_CALLBACK(expand_if_child), self);
600
601 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500602 g_signal_connect(selection, "changed", G_CALLBACK(update_selection), NULL);
Stepan Salenikovichec0862f2015-10-16 15:49:42 -0400603 g_signal_connect(selection, "changed", G_CALLBACK(scroll_to_selection), NULL);
604 g_signal_connect_swapped(recent_model, "rows-reordered", G_CALLBACK(scroll_to_selection), selection);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400605
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500606 /* update the selection based on the RecentModel */
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400607 priv->selection_updated = QObject::connect(
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500608 RecentModel::instance().selectionModel(),
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400609 &QItemSelectionModel::currentChanged,
610 [self, recent_model](const QModelIndex current, G_GNUC_UNUSED const QModelIndex & previous) {
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500611 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
612
613 auto current_proxy = RecentModel::instance().peopleProxy()->mapFromSource(current);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400614
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400615 if (current.isValid()) {
616 /* select the current */
617 GtkTreeIter new_iter;
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400618 if (gtk_q_tree_model_source_index_to_iter(recent_model, current_proxy, &new_iter)) {
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400619 gtk_tree_selection_select_iter(selection, &new_iter);
620 }
621 } else {
622 gtk_tree_selection_unselect_all(selection);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400623 }
624 }
625 );
626
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400627 /* we may need to update the selection when the layout changes */
628 priv->layout_changed = QObject::connect(
629 RecentModel::instance().peopleProxy(),
630 &QAbstractItemModel::layoutChanged,
631 [self, recent_model]() {
632 auto idx = RecentModel::instance().selectionModel()->currentIndex();
633 auto selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
634
635 auto idx_proxy = RecentModel::instance().peopleProxy()->mapFromSource(idx);
636
637 if (idx_proxy.isValid()) {
638 /* select the current */
639 GtkTreeIter iter;
640 if (gtk_q_tree_model_source_index_to_iter(recent_model, idx_proxy, &iter)) {
641 gtk_tree_selection_select_iter(selection, &iter);
642 }
643 } else {
644 gtk_tree_selection_unselect_all(selection);
645 }
646 }
647 );
648
Stepan Salenikovich0c17cb62015-11-03 13:01:50 -0500649 /* drag and drop */
650 static GtkTargetEntry targetentries[] = {
651 { (gchar *)CALL_TARGET, GTK_TARGET_SAME_WIDGET, CALL_TARGET_ID },
652 };
653
654 gtk_tree_view_enable_model_drag_source(GTK_TREE_VIEW(self),
655 GDK_BUTTON1_MASK, targetentries, 1, (GdkDragAction)(GDK_ACTION_DEFAULT | GDK_ACTION_MOVE));
656
657 gtk_tree_view_enable_model_drag_dest(GTK_TREE_VIEW(self),
658 targetentries, 1, GDK_ACTION_DEFAULT);
659
660 g_signal_connect(self, "drag-data-get", G_CALLBACK(on_drag_data_get), nullptr);
661 g_signal_connect(self, "drag-drop", G_CALLBACK(on_drag_drop), nullptr);
662 g_signal_connect(self, "drag-motion", G_CALLBACK(on_drag_motion), nullptr);
663 g_signal_connect(self, "drag_data_received", G_CALLBACK(on_drag_data_received), nullptr);
664
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400665 /* init popup menu */
666 priv->popup_menu = contact_popup_menu_new(GTK_TREE_VIEW(self));
667 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(contact_popup_menu_show), priv->popup_menu);
668
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400669 gtk_widget_show_all(GTK_WIDGET(self));
670}
671
672static void
673recent_contacts_view_dispose(GObject *object)
674{
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400675 RecentContactsView *self = RECENT_CONTACTS_VIEW(object);
676 RecentContactsViewPrivate *priv = RECENT_CONTACTS_VIEW_GET_PRIVATE(self);
677
678 QObject::disconnect(priv->selection_updated);
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400679 QObject::disconnect(priv->layout_changed);
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400680 gtk_widget_destroy(priv->popup_menu);
Stepan Salenikoviche8fa6812015-10-16 15:34:59 -0400681
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400682 G_OBJECT_CLASS(recent_contacts_view_parent_class)->dispose(object);
683}
684
685static void
686recent_contacts_view_finalize(GObject *object)
687{
688 G_OBJECT_CLASS(recent_contacts_view_parent_class)->finalize(object);
689}
690
691static void
692recent_contacts_view_class_init(RecentContactsViewClass *klass)
693{
694 G_OBJECT_CLASS(klass)->finalize = recent_contacts_view_finalize;
695 G_OBJECT_CLASS(klass)->dispose = recent_contacts_view_dispose;
696}
697
698GtkWidget *
699recent_contacts_view_new()
700{
701 gpointer self = g_object_new(RECENT_CONTACTS_VIEW_TYPE, NULL);
702
703 return (GtkWidget *)self;
704}