calls-history-box.c 5.56 KB
Newer Older
Adrien Plazas's avatar
Adrien Plazas committed
1
/*
2
 * Copyright (C) 2018, 2019 Purism SPC
Adrien Plazas's avatar
Adrien Plazas committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * 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>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "calls-history-box.h"
26
27
#include "calls-call-record.h"
#include "calls-call-record-row.h"
Adrien Plazas's avatar
Adrien Plazas committed
28
29
30
31
32
33
34
35
36
37
38
#include "util.h"

#include <glib/gi18n.h>
#include <glib-object.h>

#define HANDY_USE_UNSTABLE_API
#include <handy.h>


struct _CallsHistoryBox
{
Mohammed Sadiq's avatar
Mohammed Sadiq committed
39
  GtkStack parent_instance;
Adrien Plazas's avatar
Adrien Plazas committed
40

41
42
43
44
45
  GtkListBox *history;

  GListModel *model;
  gulong model_changed_handler_id;

46
  CallsContacts *contacts;
Adrien Plazas's avatar
Adrien Plazas committed
47
48
};

49
50
51
52
53
54
G_DEFINE_TYPE (CallsHistoryBox, calls_history_box, GTK_TYPE_STACK);


enum {
  PROP_0,
  PROP_MODEL,
55
  PROP_CONTACTS,
56
57
58
  PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];
Adrien Plazas's avatar
Adrien Plazas committed
59
60
61


static void
62
update (CallsHistoryBox *self)
Adrien Plazas's avatar
Adrien Plazas committed
63
{
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  gchar *child_name;

  if (g_list_model_get_n_items (self->model) == 0)
    {
      child_name = "empty";
    }
  else
    {
      child_name = "history";

      /* Transition should only ever be from empty to non-empty */
      if (self->model_changed_handler_id != 0)
        {
          calls_clear_signal (self->model,
                              &self->model_changed_handler_id);
        }
    }

  gtk_stack_set_visible_child_name (GTK_STACK (self), child_name);
}


static void
header_cb (GtkListBoxRow   *row,
           GtkListBoxRow   *before,
           CallsHistoryBox *self)
{
  if (!before)
    {
      return;
    }

  if (!gtk_list_box_row_get_header (row))
    {
      GtkWidget *header =
        gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);

      gtk_list_box_row_set_header (row, header);
    }
}


static GtkWidget *
create_row_cb (CallsCallRecord *record,
               CallsHistoryBox *self)
{
110
  return GTK_WIDGET (calls_call_record_row_new (record,
111
                                                self->contacts));
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
}


static void
set_property (GObject      *object,
              guint         property_id,
              const GValue *value,
              GParamSpec   *pspec)
{
  CallsHistoryBox *self = CALLS_HISTORY_BOX (object);

  switch (property_id)
    {
      case PROP_MODEL:
        g_set_object (&self->model,
                      G_LIST_MODEL (g_value_get_object (value)));
        break;

130
131
132
133
134
      case PROP_CONTACTS:
        g_set_object (&self->contacts,
                      CALLS_CONTACTS (g_value_get_object (value)));
        break;

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}


static void
constructed (GObject *object)
{
  CallsHistoryBox *self = CALLS_HISTORY_BOX (object);

  g_assert (self->model != NULL);

  self->model_changed_handler_id =
    g_signal_connect_swapped
    (self->model, "items-changed", G_CALLBACK (update), self);
  g_assert (self->model_changed_handler_id != 0);

  gtk_list_box_set_header_func (self->history,
                                (GtkListBoxUpdateHeaderFunc)header_cb,
                                self,
                                NULL);

  gtk_list_box_bind_model (self->history,
                           self->model,
                           (GtkListBoxCreateWidgetFunc)create_row_cb,
                           self,
                           NULL);

  update (self);

167
  G_OBJECT_CLASS (calls_history_box_parent_class)->constructed (object);
168
169
170
171
172
173
174
175
}


static void
dispose (GObject *object)
{
  CallsHistoryBox *self = CALLS_HISTORY_BOX (object);

176
  g_clear_object (&self->contacts);
177
178
  g_clear_object (&self->model);

179
  G_OBJECT_CLASS (calls_history_box_parent_class)->dispose (object);
Adrien Plazas's avatar
Adrien Plazas committed
180
181
182
183
184
185
}


static void
calls_history_box_class_init (CallsHistoryBoxClass *klass)
{
186
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Adrien Plazas's avatar
Adrien Plazas committed
187
188
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

189
190
191
192
193
194
195
196
197
198
199
  object_class->set_property = set_property;
  object_class->constructed = constructed;
  object_class->dispose = dispose;

  props[PROP_MODEL] =
    g_param_spec_object ("model",
                         _("model"),
                         _("The data store containing call records"),
                         G_TYPE_LIST_MODEL,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

200
201
202
203
204
205
206
  props[PROP_CONTACTS] =
    g_param_spec_object ("contacts",
                         _("Contacts"),
                         _("Interface for libfolks"),
                         CALLS_TYPE_CONTACTS,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

207
208
  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);

Adrien Plazas's avatar
Adrien Plazas committed
209
210

  gtk_widget_class_set_template_from_resource (widget_class, "/sm/puri/calls/ui/history-box.ui");
211
212
213
214
215
216
217
218
219
220
221
222
223
  gtk_widget_class_bind_template_child (widget_class, CallsHistoryBox, history);
}


static void
calls_history_box_init (CallsHistoryBox *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}


CallsHistoryBox *
calls_history_box_new (GListModel      *model,
224
                       CallsContacts   *contacts)
225
226
227
{
  return g_object_new (CALLS_TYPE_HISTORY_BOX,
                       "model", model,
228
                       "contacts", contacts,
229
                       NULL);
Adrien Plazas's avatar
Adrien Plazas committed
230
}