blob: 51459df66fe3f74c6c6bfe4215400b6d7133850b [file] [log] [blame]
Stepan Salenikovich9816a942015-04-22 17:49:16 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich9816a942015-04-22 17:49:16 -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.
Stepan Salenikovich9816a942015-04-22 17:49:16 -040018 */
19
20#include "historyview.h"
21
22#include <gtk/gtk.h>
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040023#include <glib/gi18n.h>
Stepan Salenikovichf6078222016-10-03 17:31:16 -040024#include "models/gtkqtreemodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040025#include <categorizedhistorymodel.h>
26#include <QtCore/QSortFilterProxyModel>
27#include <personmodel.h>
28#include "utils/calling.h"
29#include <memory>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040030#include <globalinstances.h>
31#include "native/pixbufmanipulator.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040032#include "defines.h"
33#include "utils/models.h"
34#include <contactmethod.h>
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -040035#include <QtCore/QDateTime> // for date time formatting
Stepan Salenikovich9d294492015-05-14 16:34:24 -040036#include <QtCore/QItemSelectionModel>
Stepan Salenikovichf2d76c52015-07-17 17:54:56 -040037#include "utils/menus.h"
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040038#include "contactpopupmenu.h"
Julien Baron865676b2016-03-04 20:38:59 +010039
Stepan Salenikovich9816a942015-04-22 17:49:16 -040040struct _HistoryView
41{
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -040042 GtkTreeView parent;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040043};
44
45struct _HistoryViewClass
46{
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -040047 GtkTreeViewClass parent_class;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040048};
49
50typedef struct _HistoryViewPrivate HistoryViewPrivate;
51
52struct _HistoryViewPrivate
53{
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -040054 GtkWidget *popup_menu;
55
Stepan Salenikovich9d294492015-05-14 16:34:24 -040056 CategorizedHistoryModel::SortedProxy *q_sorted_proxy;
57 QMetaObject::Connection category_changed;
Stepan Salenikovich9816a942015-04-22 17:49:16 -040058};
59
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -040060G_DEFINE_TYPE_WITH_PRIVATE(HistoryView, history_view, GTK_TYPE_TREE_VIEW);
Stepan Salenikovich9816a942015-04-22 17:49:16 -040061
62#define HISTORY_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HISTORY_VIEW_TYPE, HistoryViewPrivate))
63
64static void
65render_call_direction(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
66 GtkCellRenderer *cell,
67 GtkTreeModel *tree_model,
68 GtkTreeIter *iter,
69 G_GNUC_UNUSED gpointer data)
70{
71 /* check if this is a top level item (the fuzzy date item),
72 * in this case we don't want to show a call direction */
73 gchar *render_direction = NULL;
74 GtkTreeIter parent;
75 if (gtk_tree_model_iter_parent(tree_model, &parent, iter)) {
76 /* get direction and missed values */
77 GValue value = G_VALUE_INIT;
78 gtk_tree_model_get_value(tree_model, iter, 3, &value);
79 Call::Direction direction = (Call::Direction)g_value_get_int(&value);
80 g_value_unset(&value);
81
82 gtk_tree_model_get_value(tree_model, iter, 4, &value);
83 gboolean missed = g_value_get_boolean(&value);
84 g_value_unset(&value);
85
86 switch (direction) {
87 case Call::Direction::INCOMING:
88 if (missed)
89 render_direction = g_strdup_printf("<span fgcolor=\"red\" font=\"monospace\">&#8601;</span>");
90 else
91 render_direction = g_strdup_printf("<span fgcolor=\"green\" font=\"monospace\">&#8601;</span>");
92 break;
93 case Call::Direction::OUTGOING:
94 if (missed)
95 render_direction = g_strdup_printf("<span fgcolor=\"red\" font=\"monospace\">&#8599;</span>");
96 else
97 render_direction = g_strdup_printf("<span fgcolor=\"green\" font=\"monospace\">&#8599;</span>");
98 break;
99 }
100 }
101 g_object_set(G_OBJECT(cell), "markup", render_direction, NULL);
102 g_free(render_direction);
103}
104
105static void
106activate_history_item(GtkTreeView *tree_view,
107 GtkTreePath *path,
108 G_GNUC_UNUSED GtkTreeViewColumn *column,
109 G_GNUC_UNUSED gpointer user_data)
110{
111 GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
112
113 /* expand / collapse row */
114 if (gtk_tree_view_row_expanded(tree_view, path))
115 gtk_tree_view_collapse_row(tree_view, path);
116 else
117 gtk_tree_view_expand_row(tree_view, path, FALSE);
118
119 /* get iter */
120 GtkTreeIter iter;
121 if (gtk_tree_model_get_iter(model, &iter, path)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400122 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400123
124 QVariant contact_method = idx.data(static_cast<int>(Call::Role::ContactMethod));
125 /* create new call */
126 if (contact_method.value<ContactMethod*>()) {
127 place_new_call(contact_method.value<ContactMethod*>());
128 }
129 }
130}
131
132static void
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400133render_call_photo(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
134 GtkCellRenderer *cell,
135 GtkTreeModel *tree_model,
136 GtkTreeIter *iter,
137 G_GNUC_UNUSED gpointer data)
138{
139 /* check if this is a top level item (category),
140 * in this case we don't want to show a photo */
141 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
142 int depth = gtk_tree_path_get_depth(path);
143 gtk_tree_path_free(path);
144 if (depth == 2) {
145 /* get person */
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400146 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400147 if (idx.isValid()) {
148 QVariant var_c = idx.data(static_cast<int>(Call::Role::Object));
149 Call *c = var_c.value<Call *>();
150 /* get photo */
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -0400151 QVariant var_p = GlobalInstances::pixmapManipulator().callPhoto(c, QSize(50, 50), false);
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400152 std::shared_ptr<GdkPixbuf> photo = var_p.value<std::shared_ptr<GdkPixbuf>>();
153 g_object_set(G_OBJECT(cell), "pixbuf", photo.get(), NULL);
154 return;
155 }
156 }
157
158 /* otherwise, make sure its an empty pixbuf */
159 g_object_set(G_OBJECT(cell), "pixbuf", NULL, NULL);
160}
161
162static void
163render_name_and_contact_method(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
164 GtkCellRenderer *cell,
165 GtkTreeModel *tree_model,
166 GtkTreeIter *iter,
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400167 GtkTreeView *treeview)
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400168{
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400169 // check if this iter is selected
170 gboolean is_selected = FALSE;
171 if (GTK_IS_TREE_VIEW(treeview)) {
172 auto selection = gtk_tree_view_get_selection(treeview);
173 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
174 }
175
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400176 /**
177 * If call, show the name and the contact method (number) underneath;
178 * otherwise just display the category.
179 */
180 GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
181 int depth = gtk_tree_path_get_depth(path);
182 gtk_tree_path_free(path);
183
184 gchar *text = NULL;
185
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400186 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400187 if (idx.isValid()) {
188 QVariant var = idx.data(Qt::DisplayRole);
189 if (depth == 1) {
190 /* category */
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400191 text = g_markup_printf_escaped("<b>%s</b>", var.value<QString>().toUtf8().constData());
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400192 } else if (depth == 2) {
193 /* call item */
194 QVariant var_name = idx.data(static_cast<int>(Call::Role::Name));
195 QVariant var_number = idx.data(static_cast<int>(Call::Role::Number));
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400196
197 /* we want the color of the status text to be the default color if this iter is
198 * selected so that the treeview is able to invert it against the selection color */
199 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400200 text = g_markup_printf_escaped("%s\n %s",
201 var_name.value<QString>().toUtf8().constData(),
202 var_number.value<QString>().toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400203 } else {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400204 text = g_markup_printf_escaped("%s\n <span fgcolor=\"gray\">%s</span>",
205 var_name.value<QString>().toUtf8().constData(),
206 var_number.value<QString>().toUtf8().constData());
Stepan Salenikoviche4981b22015-10-22 15:22:59 -0400207 }
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400208 }
209 }
210
211 g_object_set(G_OBJECT(cell), "markup", text, NULL);
212 g_free(text);
213}
214
215static void
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500216render_date_time(G_GNUC_UNUSED GtkTreeViewColumn *tree_column,
217 GtkCellRenderer *cell,
218 GtkTreeModel *tree_model,
219 GtkTreeIter *iter,
220 GtkTreeView *treeview)
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400221{
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500222 // check if this iter is selected
223 gboolean is_selected = FALSE;
224 if (GTK_IS_TREE_VIEW(treeview)) {
225 auto selection = gtk_tree_view_get_selection(treeview);
226 is_selected = gtk_tree_selection_iter_is_selected(selection, iter);
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400227 }
228
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400229 gchar *text = NULL;
230
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400231 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500232 QVariant var_d = idx.data(static_cast<int>(Call::Role::DateTime));
233 if (idx.isValid() && var_d.isValid()) {
Stepan Salenikovich803562b2015-12-23 10:51:01 -0500234 QDateTime date_time = var_d.value<QDateTime>();
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500235
236 /* we want the color of the text to be the default color if this iter is
237 * selected so that the treeview is able to invert it against the selection color */
238 if (is_selected) {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400239 text = g_markup_printf_escaped("%s\n%s",
240 date_time.time().toString().toUtf8().constData(),
241 date_time.date().toString().toUtf8().constData()
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500242 );
243 } else {
Stepan Salenikovich0731de02016-05-17 17:47:16 -0400244 text = g_markup_printf_escaped("%s\n<span fgcolor=\"gray\">%s</span>",
245 date_time.time().toString().toUtf8().constData(),
246 date_time.date().toString().toUtf8().constData()
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500247 );
248 }
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400249 }
250
251 g_object_set(G_OBJECT(cell), "markup", text, NULL);
252 g_free(text);
253}
254
255static void
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400256history_view_init(HistoryView *self)
257{
258 HistoryViewPrivate *priv = HISTORY_VIEW_GET_PRIVATE(self);
259
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400260 /* make headers visible to allow column resizing */
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400261 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self), TRUE);
262 /* disable default search, we will handle it ourselves;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400263 * otherwise the search steals input focus on key presses */
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400264 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(self), FALSE);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400265
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400266 /* instantiate history proxy model */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400267 priv->q_sorted_proxy = &CategorizedHistoryModel::SortedProxy::instance();
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400268
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400269 /* select default category (the first one, which is by date) */
270 priv->q_sorted_proxy->categorySelectionModel()->setCurrentIndex(
271 priv->q_sorted_proxy->categoryModel()->index(0, 0),
272 QItemSelectionModel::ClearAndSelect);
Stepan Salenikovich803562b2015-12-23 10:51:01 -0500273 /* make sure it is sorted so that newest calls are at the top */
274 priv->q_sorted_proxy->model()->sort(0, Qt::AscendingOrder);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400275
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400276 GtkQTreeModel *history_model = gtk_q_tree_model_new(
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400277 priv->q_sorted_proxy->model(),
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400278 5,
aviau271bcc22016-05-27 17:25:19 -0400279 0, Qt::DisplayRole, G_TYPE_STRING,
280 0, Call::Role::Number, G_TYPE_STRING,
281 0, Call::Role::FormattedDate, G_TYPE_STRING,
282 0, Call::Role::Direction, G_TYPE_INT,
283 0, Call::Role::Missed, G_TYPE_BOOLEAN);
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400284 gtk_tree_view_set_model( GTK_TREE_VIEW(self), GTK_TREE_MODEL(history_model) );
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400285
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400286 /* call direction, photo, name/number column */
287 GtkCellArea *area = gtk_cell_area_box_new();
288 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400289 gtk_tree_view_column_set_title(column, C_("Call history", "Call"));
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400290
291 /* call direction */
292 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400293 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400294
295 /* display the call direction with arrows */
296 gtk_tree_view_column_set_cell_data_func(
297 column,
298 renderer,
299 (GtkTreeCellDataFunc)render_call_direction,
300 NULL,
301 NULL);
302
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400303 /* photo renderer */
304 renderer = gtk_cell_renderer_pixbuf_new();
305 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400306
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400307 /* get the photo */
308 gtk_tree_view_column_set_cell_data_func(
309 column,
310 renderer,
311 (GtkTreeCellDataFunc)render_call_photo,
312 NULL,
313 NULL);
314
315 /* name and contact method renderer */
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400316 renderer = gtk_cell_renderer_text_new();
317 g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400318 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
319
320 gtk_tree_view_column_set_cell_data_func(
321 column,
322 renderer,
323 (GtkTreeCellDataFunc)render_name_and_contact_method,
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400324 self,
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400325 NULL);
326
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400327 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400328 gtk_tree_view_column_set_resizable(column, TRUE);
Stepan Salenikoviche7c4e282015-06-11 17:22:08 -0400329 gtk_tree_view_column_set_expand(column, TRUE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400330
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500331 /* date time column */
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400332 area = gtk_cell_area_box_new();
333 column = gtk_tree_view_column_new_with_area(area);
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -0400334 gtk_tree_view_column_set_title(column, C_("Call history", "Date"));
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400335
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500336 /* date time renderer */
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400337 renderer = gtk_cell_renderer_text_new ();
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400338 gtk_cell_area_box_pack_start(GTK_CELL_AREA_BOX(area), renderer, FALSE, FALSE, FALSE);
339 /* format the time*/
340 gtk_tree_view_column_set_cell_data_func(
341 column,
342 renderer,
Stepan Salenikovich501a7f82015-12-23 11:06:47 -0500343 (GtkTreeCellDataFunc)render_date_time,
344 self,
Stepan Salenikovich82b1acf2015-05-12 12:33:51 -0400345 NULL);
346
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400347 gtk_tree_view_append_column(GTK_TREE_VIEW(self), column);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400348 gtk_tree_view_column_set_resizable(column, TRUE);
Stepan Salenikoviche7c4e282015-06-11 17:22:08 -0400349 gtk_tree_view_column_set_expand(column, FALSE);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400350
Stepan Salenikovich4a5dd7f2015-10-29 16:35:02 -0400351 g_signal_connect(self, "row-activated", G_CALLBACK(activate_history_item), NULL);
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400352
353 /* init popup menu */
354 priv->popup_menu = contact_popup_menu_new(GTK_TREE_VIEW(self));
355 g_signal_connect_swapped(self, "button-press-event", G_CALLBACK(contact_popup_menu_show), priv->popup_menu);
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400356
357 gtk_widget_show_all(GTK_WIDGET(self));
358}
359
360static void
361history_view_dispose(GObject *object)
362{
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400363 HistoryViewPrivate *priv = HISTORY_VIEW_GET_PRIVATE(object);
364
365 QObject::disconnect(priv->category_changed);
Stepan Salenikovich8eaa13e2016-08-26 16:51:48 -0400366 gtk_widget_destroy(priv->popup_menu);
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400367
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400368 G_OBJECT_CLASS(history_view_parent_class)->dispose(object);
369}
370
371static void
372history_view_finalize(GObject *object)
373{
Stepan Salenikovich9816a942015-04-22 17:49:16 -0400374 G_OBJECT_CLASS(history_view_parent_class)->finalize(object);
375}
376
377static void
378history_view_class_init(HistoryViewClass *klass)
379{
380 G_OBJECT_CLASS(klass)->finalize = history_view_finalize;
381 G_OBJECT_CLASS(klass)->dispose = history_view_dispose;
382}
383
384GtkWidget *
385history_view_new()
386{
387 gpointer self = g_object_new(HISTORY_VIEW_TYPE, NULL);
388
389 return (GtkWidget *)self;
Stepan Salenikovich9d294492015-05-14 16:34:24 -0400390}