2018-08-01 08:15:36 +00:00
|
|
|
/*
|
2022-07-09 11:20:34 +00:00
|
|
|
* Copyright (C) 2018, 2019, 2022 Purism SPC
|
2018-08-01 08:15:36 +00:00
|
|
|
*
|
|
|
|
* This file is part of Calls.
|
|
|
|
*
|
|
|
|
* Calls is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calls is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calls. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Adrien Plazas <adrien.plazas@puri.sm>
|
2022-07-09 11:20:34 +00:00
|
|
|
* Evangelos Ribeiro Tzaras <devrtz@fortysixandtwo.eu>
|
2018-08-01 08:15:36 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
*/
|
2022-07-09 11:20:34 +00:00
|
|
|
#define G_LOG_DOMAIN "CallsHistoryBox"
|
2018-08-01 08:15:36 +00:00
|
|
|
|
|
|
|
#include "calls-history-box.h"
|
2019-08-01 13:25:53 +00:00
|
|
|
#include "calls-call-record.h"
|
|
|
|
#include "calls-call-record-row.h"
|
2023-01-21 15:42:39 +00:00
|
|
|
#include "calls-util.h"
|
2018-08-01 08:15:36 +00:00
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
#define CALLS_HISTORY_SIZE_INITIAL 75
|
|
|
|
#define CALLS_HISTORY_SIZE_INCREMENTS 50
|
|
|
|
#define CALLS_HISTORY_RESET_SIZE_POSITION_THRESHOLD 500
|
|
|
|
#define CALLS_HISTORY_INCREASE_N_PAGES_THRESHOLD 2
|
2018-08-01 08:15:36 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
struct _CallsHistoryBox {
|
2023-12-13 20:25:46 +00:00
|
|
|
GtkWidget parent_instance;
|
2018-08-01 08:15:36 +00:00
|
|
|
|
2023-08-27 01:49:13 +00:00
|
|
|
GtkStack *stack;
|
2022-07-09 11:20:34 +00:00
|
|
|
GtkListBox *history;
|
|
|
|
GtkScrolledWindow *scrolled_window;
|
|
|
|
GtkAdjustment *scroll_adjustment;
|
2019-08-01 13:25:53 +00:00
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
GListModel *model;
|
2022-07-09 10:49:08 +00:00
|
|
|
GtkSliceListModel *slice_model;
|
|
|
|
|
2022-07-27 14:54:26 +00:00
|
|
|
gsize n_items;
|
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
gulong model_changed_handler_id;
|
2019-08-01 13:25:53 +00:00
|
|
|
|
2018-08-01 08:15:36 +00:00
|
|
|
};
|
|
|
|
|
2023-12-13 20:25:46 +00:00
|
|
|
G_DEFINE_TYPE (CallsHistoryBox, calls_history_box, GTK_TYPE_WIDGET);
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_MODEL,
|
|
|
|
PROP_LAST_PROP,
|
|
|
|
};
|
|
|
|
static GParamSpec *props[PROP_LAST_PROP];
|
2018-08-01 08:15:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-07-27 14:54:26 +00:00
|
|
|
on_model_changed (GListModel *model,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
CallsHistoryBox *self)
|
2018-08-01 08:15:36 +00:00
|
|
|
{
|
2019-08-01 13:25:53 +00:00
|
|
|
gchar *child_name;
|
|
|
|
|
2022-07-27 14:54:26 +00:00
|
|
|
self->n_items = self->n_items + added - removed;
|
|
|
|
if (self->n_items == 0)
|
2021-12-01 06:03:09 +00:00
|
|
|
child_name = "empty";
|
2022-07-27 14:45:42 +00:00
|
|
|
else
|
2021-12-01 06:03:09 +00:00
|
|
|
child_name = "history";
|
|
|
|
|
2023-08-27 01:49:13 +00:00
|
|
|
gtk_stack_set_visible_child_name (self->stack, child_name);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 13:09:57 +00:00
|
|
|
static void
|
2022-04-24 10:24:55 +00:00
|
|
|
delete_call_cb (CallsCallRecord *record,
|
|
|
|
CallsHistoryBox *self)
|
2020-06-08 13:09:57 +00:00
|
|
|
{
|
|
|
|
guint position;
|
|
|
|
guint id;
|
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_CALL_RECORD (record));
|
|
|
|
|
2022-10-10 06:39:42 +00:00
|
|
|
ok = calls_find_in_model (self->model,
|
2022-04-24 10:24:55 +00:00
|
|
|
record,
|
|
|
|
&position);
|
2020-06-08 13:09:57 +00:00
|
|
|
|
|
|
|
g_object_get (G_OBJECT (record),
|
|
|
|
"id",
|
|
|
|
&id,
|
|
|
|
NULL);
|
|
|
|
|
2021-12-01 06:03:09 +00:00
|
|
|
if (!ok) {
|
|
|
|
g_warning ("Could not find record with id %u in model",
|
|
|
|
id);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-08 13:09:57 +00:00
|
|
|
|
|
|
|
g_list_store_remove ((GListStore *) self->model, position);
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
create_row_cb (CallsCallRecord *record,
|
|
|
|
CallsHistoryBox *self)
|
|
|
|
{
|
2020-06-08 13:09:57 +00:00
|
|
|
GtkWidget *row_widget;
|
2022-04-24 10:24:55 +00:00
|
|
|
|
2020-06-17 11:44:11 +00:00
|
|
|
row_widget = GTK_WIDGET (calls_call_record_row_new (record));
|
2020-06-08 13:09:57 +00:00
|
|
|
|
|
|
|
g_signal_connect (record,
|
|
|
|
"call-delete",
|
|
|
|
G_CALLBACK (delete_call_cb),
|
|
|
|
self);
|
|
|
|
return row_widget;
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
static void
|
|
|
|
on_adjustment_position_changed (GtkAdjustment *adjustment,
|
|
|
|
CallsHistoryBox *self)
|
|
|
|
{
|
|
|
|
double position;
|
|
|
|
double upper_limit;
|
|
|
|
double page_size;
|
|
|
|
guint old_size;
|
|
|
|
|
|
|
|
g_assert (CALLS_IS_HISTORY_BOX (self));
|
|
|
|
|
|
|
|
position = gtk_adjustment_get_value (adjustment);
|
|
|
|
page_size = gtk_adjustment_get_page_size (adjustment);
|
|
|
|
old_size = gtk_slice_list_model_get_size (self->slice_model);
|
|
|
|
|
|
|
|
if (position < CALLS_HISTORY_RESET_SIZE_POSITION_THRESHOLD) {
|
|
|
|
if (old_size == CALLS_HISTORY_SIZE_INITIAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_debug ("Resetting to initial size: %u",
|
|
|
|
CALLS_HISTORY_SIZE_INITIAL);
|
|
|
|
gtk_slice_list_model_set_size (self->slice_model, CALLS_HISTORY_SIZE_INITIAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
upper_limit = gtk_adjustment_get_upper (adjustment);
|
|
|
|
|
|
|
|
if (position > upper_limit - CALLS_HISTORY_INCREASE_N_PAGES_THRESHOLD * page_size) {
|
|
|
|
guint new_size = old_size + CALLS_HISTORY_SIZE_INCREMENTS;
|
|
|
|
|
2022-07-27 14:54:26 +00:00
|
|
|
new_size = MIN (new_size, self->n_items);
|
|
|
|
|
|
|
|
if (old_size == new_size)
|
|
|
|
return;
|
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
g_debug ("Increasing history slice from %u to %u",
|
|
|
|
old_size, new_size);
|
|
|
|
gtk_slice_list_model_set_size (self->slice_model, new_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
static void
|
|
|
|
set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
CallsHistoryBox *self = CALLS_HISTORY_BOX (object);
|
|
|
|
|
2021-12-01 06:03:09 +00:00
|
|
|
switch (property_id) {
|
|
|
|
case PROP_MODEL:
|
|
|
|
g_set_object (&self->model,
|
|
|
|
G_LIST_MODEL (g_value_get_object (value)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
constructed (GObject *object)
|
|
|
|
{
|
|
|
|
CallsHistoryBox *self = CALLS_HISTORY_BOX (object);
|
|
|
|
|
|
|
|
g_assert (self->model != NULL);
|
|
|
|
|
2022-07-27 14:25:16 +00:00
|
|
|
G_OBJECT_CLASS (calls_history_box_parent_class)->constructed (object);
|
|
|
|
|
2022-07-09 11:20:34 +00:00
|
|
|
self->slice_model = gtk_slice_list_model_new (self->model,
|
|
|
|
0,
|
|
|
|
CALLS_HISTORY_SIZE_INITIAL);
|
2022-07-09 10:49:08 +00:00
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
self->model_changed_handler_id =
|
2022-07-27 14:54:26 +00:00
|
|
|
g_signal_connect (self->model,
|
|
|
|
"items-changed",
|
|
|
|
G_CALLBACK (on_model_changed),
|
|
|
|
self);
|
2019-08-01 13:25:53 +00:00
|
|
|
g_assert (self->model_changed_handler_id != 0);
|
|
|
|
|
|
|
|
gtk_list_box_bind_model (self->history,
|
2022-07-09 10:49:08 +00:00
|
|
|
G_LIST_MODEL (self->slice_model),
|
2022-04-24 10:24:55 +00:00
|
|
|
(GtkListBoxCreateWidgetFunc) create_row_cb,
|
2019-08-01 13:25:53 +00:00
|
|
|
self,
|
|
|
|
NULL);
|
|
|
|
|
2022-07-27 14:54:26 +00:00
|
|
|
on_model_changed (self->model, 0, 0, g_list_model_get_n_items (self->model), self);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dispose (GObject *object)
|
|
|
|
{
|
|
|
|
CallsHistoryBox *self = CALLS_HISTORY_BOX (object);
|
2023-12-16 22:08:56 +00:00
|
|
|
GtkWidget *stack = GTK_WIDGET (self->stack);
|
2019-08-01 13:25:53 +00:00
|
|
|
|
2023-01-11 17:14:36 +00:00
|
|
|
g_clear_signal_handler (&self->model_changed_handler_id, self->model);
|
2022-07-09 10:49:08 +00:00
|
|
|
g_clear_object (&self->slice_model);
|
2019-08-01 13:25:53 +00:00
|
|
|
g_clear_object (&self->model);
|
|
|
|
|
2023-12-16 22:08:56 +00:00
|
|
|
g_clear_pointer (&stack, gtk_widget_unparent);
|
|
|
|
|
2020-02-18 15:01:22 +00:00
|
|
|
G_OBJECT_CLASS (calls_history_box_parent_class)->dispose (object);
|
2018-08-01 08:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_history_box_class_init (CallsHistoryBoxClass *klass)
|
|
|
|
{
|
2019-08-01 13:25:53 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2018-08-01 08:15:36 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
object_class->set_property = set_property;
|
|
|
|
object_class->constructed = constructed;
|
|
|
|
object_class->dispose = dispose;
|
|
|
|
|
|
|
|
props[PROP_MODEL] =
|
|
|
|
g_param_spec_object ("model",
|
2020-05-29 05:38:11 +00:00
|
|
|
"model",
|
|
|
|
"The data store containing call records",
|
2019-08-01 13:25:53 +00:00
|
|
|
G_TYPE_LIST_MODEL,
|
|
|
|
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
|
|
|
|
|
|
|
|
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
|
|
|
|
|
2018-08-01 08:15:36 +00:00
|
|
|
|
2021-07-10 02:15:22 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Calls/ui/history-box.ui");
|
2023-08-27 01:49:13 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsHistoryBox, stack);
|
2019-08-01 13:25:53 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsHistoryBox, history);
|
2022-07-09 11:20:34 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsHistoryBox, scrolled_window);
|
2023-12-16 22:08:56 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_history_box_init (CallsHistoryBox *self)
|
|
|
|
{
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2022-07-09 11:20:34 +00:00
|
|
|
|
|
|
|
self->scroll_adjustment = gtk_scrolled_window_get_vadjustment (self->scrolled_window);
|
|
|
|
|
|
|
|
g_signal_connect (self->scroll_adjustment,
|
|
|
|
"value-changed",
|
|
|
|
G_CALLBACK (on_adjustment_position_changed),
|
|
|
|
self);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CallsHistoryBox *
|
2022-04-24 10:24:55 +00:00
|
|
|
calls_history_box_new (GListModel *model)
|
2019-08-01 13:25:53 +00:00
|
|
|
{
|
|
|
|
return g_object_new (CALLS_TYPE_HISTORY_BOX,
|
|
|
|
"model", model,
|
|
|
|
NULL);
|
2018-08-01 08:15:36 +00:00
|
|
|
}
|