calls-call-record-row.c 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (C) 2018, 2019 Purism SPC
 *
 * 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: Bob Ham <bob.ham@puri.sm>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "calls-call-record-row.h"
Bob Ham's avatar
Bob Ham committed
26
#include "calls-best-match.h"
Julian Sparber's avatar
Julian Sparber committed
27
#include "contrib/hdy-avatar.h"
28 29 30 31 32 33 34 35 36 37 38 39
#include "util.h"

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

#include <sys/time.h>
#include <errno.h>


struct _CallsCallRecordRow
{
40
  GtkListBoxRow parent_instance;
41

Julian Sparber's avatar
Julian Sparber committed
42
  GtkWidget *avatar;
43 44 45
  GtkImage *type;
  GtkLabel *target;
  GtkLabel *time;
46
  GtkButton *button;
47 48 49 50 51 52

  CallsCallRecord *record;
  gulong answered_notify_handler_id;
  gulong end_notify_handler_id;
  guint date_change_timeout;

53
  CallsContacts *contacts;
Bob Ham's avatar
Bob Ham committed
54
  CallsBestMatch *contact;
55 56
};

57
G_DEFINE_TYPE (CallsCallRecordRow, calls_call_record_row, GTK_TYPE_LIST_BOX_ROW)
58 59 60 61 62


enum {
  PROP_0,
  PROP_RECORD,
63
  PROP_CONTACTS,
64 65 66 67 68
  PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];


69 70 71 72
static gboolean
string_to_variant (GBinding *binding,
                   const GValue *from_value,
                   GValue *to_value)
73
{
74 75
  const gchar *target = g_value_get_string (from_value);
  GVariant *variant = g_variant_new_string (target);
76

77 78
  g_value_take_variant (to_value, variant);
  return TRUE;
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}


static void
nice_time (GDateTime *t,
           gchar **nice,
           gboolean *final)
{
  GDateTime *now = g_date_time_new_now_local ();
  const gboolean today =
    calls_date_time_is_same_day (now, t);
  const gboolean yesterday =
    (!today && calls_date_time_is_yesterday (now, t));

  g_assert (nice != NULL);
  g_assert (final != NULL);

  if (today || yesterday)
    {
      gchar *n = g_date_time_format (t, "%R");

      if (yesterday)
        {
          gchar *s;
          s = g_strdup_printf (_("%s\nyesterday"), n);
          g_free (n);
          n = s;
        }

      *nice = n;
      *final = FALSE;
    }
  else if (calls_date_time_is_same_year (now, t))
    {
      *nice = g_date_time_format (t, "%b %-d");
      *final = FALSE;
    }
  else
    {
      *nice = g_date_time_format (t, "%Y");
      *final = TRUE;
    }

  g_date_time_unref (now);
}


static void
127 128 129
update_time_text (CallsCallRecordRow *self,
                  GDateTime          *end,
                  gboolean           *final)
130 131 132 133 134 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
  gchar *nice;
  nice_time (end, &nice, final);
  gtk_label_set_text (self->time, nice);
  g_free (nice);
}


static gboolean date_change_cb (CallsCallRecordRow *self);


static void
setup_date_change_timeout (CallsCallRecordRow *self)
{
  GDateTime *gnow, *gnextday, *gtomorrow;
  struct timeval now, tomorrow, delta;
  int err;
  guint interval;

  // Get the time now
  gnow = g_date_time_new_now_local ();

  // Get the next day
  gnextday = g_date_time_add_days (gnow, 1);
  g_date_time_unref (gnow);

  // Get the start of the next day
  gtomorrow =
    g_date_time_new (g_date_time_get_timezone (gnextday),
                     g_date_time_get_year (gnextday),
                     g_date_time_get_month (gnextday),
                     g_date_time_get_day_of_month (gnextday),
                     0,
                     0,
                     0.0);
  g_date_time_unref (gnextday);

  // Convert to a timeval
  tomorrow.tv_sec  = g_date_time_to_unix (gtomorrow);
  tomorrow.tv_usec = 0;
  g_date_time_unref (gtomorrow);

  // Get the precise time now
  err = gettimeofday (&now, NULL);
  if (err == -1)
    {
      g_warning ("Error getting time to set date change timeout: %s",
                 g_strerror (errno));
      return;
    }

  // Find how long from now until the start of the next day
  timersub (&tomorrow, &now, &delta);

  // Convert to milliseconds
  interval =
    (delta.tv_sec  * 1000)
    +
    (delta.tv_usec / 1000);

  // Add the timeout
  self->date_change_timeout =
    g_timeout_add (interval,
                   (GSourceFunc)date_change_cb,
                   self);
}


static gboolean
date_change_cb (CallsCallRecordRow *self)
{
  GDateTime *end;
  gboolean final;

  g_object_get (G_OBJECT (self->record),
                "end", &end,
                NULL);
  g_assert (end != NULL);

209
  update_time_text (self, end, &final);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  g_date_time_unref (end);

  if (final)
    {
      self->date_change_timeout = 0;
    }
  else
    {
      setup_date_change_timeout (self);
    }

  return FALSE;
}


static void
226 227 228 229
update_time (CallsCallRecordRow *self,
             gboolean            inbound,
             GDateTime          *answered,
             GDateTime          *end)
230 231 232 233 234 235 236 237
{
  gboolean missed = FALSE;
  gchar *type_icon_name;

  if (end)
    {
      gboolean time_final;

238
      update_time_text (self, end, &time_final);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

      if (!time_final && !self->date_change_timeout)
        {
          setup_date_change_timeout (self);
        }

      if (!answered)
        {
          missed = TRUE;
        }
    }

  type_icon_name = g_strdup_printf
    ("call-arrow-%s%s-symbolic",
     inbound ? "incoming" : "outgoing",
     missed  ? "-missed"  : "");
  gtk_image_set_from_icon_name (self->type, type_icon_name,
                                GTK_ICON_SIZE_MENU);

  g_free (type_icon_name);
}


static void
263 264 265
notify_time_cb (CallsCallRecordRow *self,
                GParamSpec         *pspec,
                CallsCallRecord    *record)
266 267 268 269 270 271 272 273 274 275 276
{
  gboolean inbound;
  GDateTime *answered;
  GDateTime *end;

  g_object_get (G_OBJECT (self->record),
                "inbound", &inbound,
                "answered", &answered,
                "end", &end,
                NULL);

277
  update_time (self, inbound, answered, end);
278 279 280 281 282 283 284 285 286 287 288 289 290 291

  if (answered)
    {
      g_date_time_unref (answered);
      calls_clear_signal (record, &self->answered_notify_handler_id);
    }
  if (end)
    {
      g_date_time_unref (end);
      calls_clear_signal (record, &self->end_notify_handler_id);
    }
}


292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static void
setup_time (CallsCallRecordRow *self,
            gboolean            inbound,
            GDateTime          *answered,
            GDateTime          *end)
{
  if (!end)
    {
      self->end_notify_handler_id =
        g_signal_connect_swapped (self->record,
                                  "notify::end",
                                  G_CALLBACK (notify_time_cb),
                                  self);

      if (!answered)
        {
          self->answered_notify_handler_id =
            g_signal_connect_swapped (self->record,
                                      "notify::answered",
                                      G_CALLBACK (notify_time_cb),
                                      self);
        }
    }

  update_time (self, inbound, answered, end);
}


static void
Bob Ham's avatar
Bob Ham committed
321
contact_name_cb (CallsCallRecordRow *self)
322
{
Bob Ham's avatar
Bob Ham committed
323 324
  const gchar *name = NULL;

325 326
  if (self->contact)
    {
Bob Ham's avatar
Bob Ham committed
327 328
      name = calls_best_match_get_name (self->contact);
    }
329

Bob Ham's avatar
Bob Ham committed
330 331
  if (name)
    {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
      gtk_label_set_text (self->target, name);
    }
  else
    {
      gchar *target;

      g_object_get (G_OBJECT (self->record),
                    "target", &target,
                    NULL);

      gtk_label_set_text (self->target, target);

      g_free (target);
    }
}

static void
Julian Sparber's avatar
Julian Sparber committed
349
avatar_text_changed_cb (HdyAvatar *avatar)
350
{
Julian Sparber's avatar
Julian Sparber committed
351 352
  const gchar *text = hdy_avatar_get_text (avatar);
  hdy_avatar_set_show_initials (avatar, !g_ascii_isdigit (*text) && !strchr("#*+", *text));
353 354 355 356
}


static void
Bob Ham's avatar
Bob Ham committed
357
setup_contact (CallsCallRecordRow *self)
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
{
  g_autofree gchar *target = NULL;
  EPhoneNumber *phone_number;
  GError *error = NULL;

  // Get the target number
  g_object_get (G_OBJECT (self->record),
                "target", &target,
                NULL);
  g_assert (target != NULL);

  // Parse it
  phone_number = e_phone_number_from_string
    (target, NULL, &error);
  if (!phone_number)
    {
      g_warning ("Error parsing phone number `%s': %s",
                 target, error->message);
      g_error_free (error);
      return;
    }

Bob Ham's avatar
Bob Ham committed
380 381
  // Look up the best match object
  self->contact = calls_contacts_lookup_phone_number
382
    (self->contacts, phone_number);
Bob Ham's avatar
Bob Ham committed
383
  g_assert (self->contact != NULL);
384
  g_object_ref (self->contact);
385 386 387
  g_clear_object (&self->contacts);
  e_phone_number_free (phone_number);

Bob Ham's avatar
Bob Ham committed
388 389 390 391
  g_signal_connect_swapped (self->contact,
                            "notify::name",
                            G_CALLBACK (contact_name_cb),
                            self);
392 393 394
}


395 396 397 398 399 400 401 402 403 404 405 406 407 408
static void
set_property (GObject      *object,
              guint         property_id,
              const GValue *value,
              GParamSpec   *pspec)
{
  CallsCallRecordRow *self = CALLS_CALL_RECORD_ROW (object);

  switch (property_id) {
  case PROP_RECORD:
    g_set_object (&self->record,
                  CALLS_CALL_RECORD (g_value_get_object (value)));
    break;

409 410 411 412 413
  case PROP_CONTACTS:
    g_set_object (&self->contacts,
                  CALLS_CONTACTS (g_value_get_object (value)));
    break;

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
constructed (GObject *object)
{
  CallsCallRecordRow *self = CALLS_CALL_RECORD_ROW (object);
  gboolean inbound;
  GDateTime *answered;
  GDateTime *end;

  g_object_get (G_OBJECT (self->record),
                "inbound", &inbound,
                "answered", &answered,
                "end", &end,
                NULL);

435 436 437 438 439 440 441 442 443 444
  g_object_bind_property_full (self->record,
                               "target",
                               self->button,
                               "action-target",
                               G_BINDING_SYNC_CREATE,
                               (GBindingTransformFunc) string_to_variant,
                               NULL,
                               NULL,
                               NULL);

445
  setup_time (self, inbound, answered, end);
446 447 448
  calls_date_time_unref (answered);
  calls_date_time_unref (end);

Bob Ham's avatar
Bob Ham committed
449 450
  setup_contact (self);
  contact_name_cb (self);
451

452
  G_OBJECT_CLASS (calls_call_record_row_parent_class)->constructed (object);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
}


static void
get_property (GObject      *object,
              guint         property_id,
              GValue       *value,
              GParamSpec   *pspec)
{
  CallsCallRecordRow *self = CALLS_CALL_RECORD_ROW (object);

  switch (property_id) {
  case PROP_RECORD:
    g_value_set_object (value, self->record);
    break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
dispose (GObject *object)
{
  CallsCallRecordRow *self = CALLS_CALL_RECORD_ROW (object);

Bob Ham's avatar
Bob Ham committed
481
  g_clear_object (&self->contact);
482 483
  g_clear_object (&self->contacts);

484 485 486 487 488
  calls_clear_source (&self->date_change_timeout);
  calls_clear_signal (self->record, &self->answered_notify_handler_id);
  calls_clear_signal (self->record, &self->end_notify_handler_id);
  g_clear_object (&self->record);

489
  G_OBJECT_CLASS (calls_call_record_row_parent_class)->dispose (object);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
}


static void
calls_call_record_row_class_init (CallsCallRecordRowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->set_property = set_property;
  object_class->constructed = constructed;
  object_class->get_property = get_property;
  object_class->dispose = dispose;

  props[PROP_RECORD] =
    g_param_spec_object ("record",
506 507
                         "Record",
                         "The call record for this row",
508 509 510
                         CALLS_TYPE_CALL_RECORD,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

511 512
  props[PROP_CONTACTS] =
    g_param_spec_object ("contacts",
513 514
                         "Contacts",
                         "Interface for libfolks",
515 516 517
                         CALLS_TYPE_CONTACTS,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

518 519 520 521 522 523 524 525
  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);


  gtk_widget_class_set_template_from_resource (widget_class, "/sm/puri/calls/ui/call-record-row.ui");
  gtk_widget_class_bind_template_child (widget_class, CallsCallRecordRow, avatar);
  gtk_widget_class_bind_template_child (widget_class, CallsCallRecordRow, type);
  gtk_widget_class_bind_template_child (widget_class, CallsCallRecordRow, target);
  gtk_widget_class_bind_template_child (widget_class, CallsCallRecordRow, time);
526
  gtk_widget_class_bind_template_child (widget_class, CallsCallRecordRow, button);
527 528 529 530 531 532 533
}


static void
calls_call_record_row_init (CallsCallRecordRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
Julian Sparber's avatar
Julian Sparber committed
534 535 536 537 538

  g_signal_connect (self->avatar,
                    "notify::text",
                    G_CALLBACK (avatar_text_changed_cb),
                    NULL);
539 540 541 542 543
}


CallsCallRecordRow *
calls_call_record_row_new (CallsCallRecord *record,
544
                           CallsContacts   *contacts)
545 546 547
{
  return g_object_new (CALLS_TYPE_CALL_RECORD_ROW,
                       "record", record,
548
                       "contacts", contacts,
549 550 551 552 553 554 555 556 557
                       NULL);
}


CallsCallRecord *
calls_call_record_row_get_record (CallsCallRecordRow *self)
{
  return self->record;
}