calls-application.c 13.1 KB
Newer Older
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1 2
/* calls-application.c
 *
3
 * Copyright (C) 2018, 2019 Purism SPC
Mohammed Sadiq's avatar
Mohammed Sadiq committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * Copyright (C) 2018 Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * 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/>.
 *
 * Authors:
 *      Bob Ham <bob.ham@puri.sm>
 *      Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "config.h"
#include "calls-history-box.h"
#include "calls-new-call-box.h"
#include "calls-encryption-indicator.h"
Bob Ham's avatar
Bob Ham committed
32
#include "calls-ringer.h"
33
#include "calls-notifier.h"
34
#include "calls-record-store.h"
35
#include "calls-contacts.h"
Mohammed Sadiq's avatar
Mohammed Sadiq committed
36 37
#include "calls-call-window.h"
#include "calls-main-window.h"
38
#include "calls-manager.h"
Mohammed Sadiq's avatar
Mohammed Sadiq committed
39 40
#include "calls-application.h"

41
#include <glib/gi18n.h>
Adrien Plazas's avatar
Adrien Plazas committed
42
#include <handy.h>
43
#include <libcallaudio.h>
Bob Ham's avatar
Bob Ham committed
44
#include <libebook-contacts/libebook-contacts.h>
45

Mohammed Sadiq's avatar
Mohammed Sadiq committed
46 47 48 49 50 51 52
/**
 * SECTION: calls-application
 * @title: CallsApplication
 * @short_description: Base Application class
 * @include: "calls-application.h"
 */

53 54
#define DEFAULT_PROVIDER_PLUGIN "mm"

Mohammed Sadiq's avatar
Mohammed Sadiq committed
55 56 57 58
struct _CallsApplication
{
  GtkApplication parent_instance;

59
  gboolean          daemon;
60
  CallsManager     *manager;
61
  CallsRinger      *ringer;
62
  CallsNotifier    *notifier;
63
  CallsRecordStore *record_store;
64
  CallsContacts    *contacts;
Bob Ham's avatar
Bob Ham committed
65 66
  CallsMainWindow  *main_window;
  CallsCallWindow  *call_window;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
67 68
};

69 70 71 72
G_DEFINE_TYPE (CallsApplication, calls_application, GTK_TYPE_APPLICATION);


static gboolean start_proper (CallsApplication *self);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
73 74


75 76 77 78 79 80
static gint
handle_local_options (GApplication *application,
                      GVariantDict *options)
{
  gboolean ok;
  g_autoptr(GError) error = NULL;
81
  const gchar *arg;
82 83 84 85 86 87 88 89 90

  g_debug ("Registering application");
  ok = g_application_register (application, NULL, &error);
  if (!ok)
    {
      g_error ("Error registering application: %s",
               error->message);
    }

91
  ok = g_variant_dict_lookup (options, "provider", "&s", &arg);
92 93 94 95
  if (ok)
    {
      g_action_group_activate_action (G_ACTION_GROUP (application),
                                      "set-provider-name",
96
                                      g_variant_new_string (arg));
97
    }
98 99 100 101 102 103
  else
    {
      g_action_group_activate_action (G_ACTION_GROUP (application),
                                      "set-provider-name",
                                      g_variant_new_string (DEFAULT_PROVIDER_PLUGIN));
    }
104

105 106 107 108 109 110 111 112
  ok = g_variant_dict_contains (options, "daemon");
  if (ok)
    {
      g_action_group_activate_action (G_ACTION_GROUP (application),
                                      "set-daemon",
                                      NULL);
    }

113 114 115 116 117 118 119 120
  ok = g_variant_dict_lookup (options, "dial", "&s", &arg);
  if (ok)
    {
      g_action_group_activate_action (G_ACTION_GROUP (application),
                                      "dial",
                                      g_variant_new_string (arg));
    }

121 122 123 124
  return -1; // Continue processing signal
}


Mohammed Sadiq's avatar
Mohammed Sadiq committed
125
static void
126 127 128
set_provider_name_action (GSimpleAction *action,
                          GVariant      *parameter,
                          gpointer       user_data)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
129
{
130
  const gchar *name;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
131

132 133
  name = g_variant_get_string (parameter, NULL);
  g_return_if_fail (name != NULL);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
134

135 136 137
  /* FIXME: allow to set a new provider, we need to make sure that the
     provider is unloaded correctly from the CallsManager */
  if (calls_manager_get_provider (calls_manager_get_default ()) != NULL)
138 139 140 141 142 143 144
    {
      g_warning ("Cannot set provider name to `%s'"
                 " because provider is already created",
                 name);
      return;
    }

145 146
  g_debug ("Start loading provider `%s'", name);
  calls_manager_set_provider (calls_manager_get_default (), name);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
147 148
}

149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static void
set_daemon_action (GSimpleAction *action,
                   GVariant      *parameter,
                   gpointer       user_data)
{
  CallsApplication *self = CALLS_APPLICATION (user_data);

  if (self->main_window)
    {
      g_warning ("Cannot set application as a daemon"
                 " because application is already started");
      return;
    }

  self->daemon = TRUE;

  g_debug ("Application marked as daemon");
}


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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#define DIALLING    "0-9*#+ABCD"
#define SIGNALLING  ",TP!W@X"
#define VISUAL      "[:space:]\\-.()t/"
#define REJECT_RE   "[^" DIALLING SIGNALLING VISUAL "]"
#define VISUAL_RE   "[" VISUAL "]"

static gboolean
check_dial_number (const gchar *number)
{
  GError *error = NULL;
  GRegex *reject;
  gboolean matches;

  reject = g_regex_new (REJECT_RE, 0, 0, &error);
  if (!reject)
    {
      g_warning ("Could not compile regex for"
                 " dial number checking: %s",
                 error->message);
      g_error_free (error);
      return FALSE;
    }

  matches = g_regex_match (reject, number, 0, NULL);

  g_regex_unref (reject);

  return !matches;
}


static gchar *
extract_dial_string (const gchar *number)
{
  g_autoptr(GError) error = NULL;
  g_autoptr(GRegex) replace_visual;
  gchar *dial_string;

  replace_visual = g_regex_new (VISUAL_RE, 0, 0, &error);
  if (!replace_visual)
    {
      g_warning ("Could not compile regex for"
                 " dial number extracting: %s",
                 error->message);
      return NULL;
    }

  dial_string = g_regex_replace_literal
    (replace_visual, number, -1, 0, "", 0, &error);

  if (!dial_string)
    {
      g_warning ("Error replacing visual separators"
                 " in dial number: %s",
                 error->message);
      return NULL;
    }

  return dial_string;
}


static void
dial_action (GSimpleAction *action,
             GVariant      *parameter,
             gpointer       user_data)
{
  CallsApplication *self = CALLS_APPLICATION (user_data);
  const gchar *number;
  gboolean number_ok;
  gchar *dial_string;

  number = g_variant_get_string (parameter, NULL);
  g_return_if_fail (number != NULL);

  number_ok = check_dial_number (number);
  if (!number_ok)
    {
      g_warning ("Dial number `%s' is not a valid dial string",
                 number);
      return;
    }

  dial_string = extract_dial_string (number);
  if (!dial_string)
    {
      return;
    }

  g_debug ("Dialing dial string `%s' extracted from number `%s'",
           dial_string, number);


  start_proper (self);

  calls_main_window_dial (self->main_window,
                          dial_string);
  g_free (dial_string);
}


271 272 273
static const GActionEntry actions[] =
{
  { "set-provider-name", set_provider_name_action, "s" },
274
  { "set-daemon", set_daemon_action, NULL },
275
  { "dial", dial_action, "s" },
276 277 278
};


279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static void
css_setup ()
{
  GtkCssProvider *provider;
  GFile *file;
  GError *error = NULL;

  provider = gtk_css_provider_new ();
  file = g_file_new_for_uri ("resource:///sm/puri/calls/style.css");

  if (!gtk_css_provider_load_from_file (provider, file, &error)) {
    g_warning ("Failed to load CSS file: %s", error->message);
    g_clear_error (&error);
    g_object_unref (file);
    return;
  }
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             GTK_STYLE_PROVIDER (provider), 600);
  g_object_unref (file);
}


Mohammed Sadiq's avatar
Mohammed Sadiq committed
301 302 303
static void
startup (GApplication *application)
{
304
  GtkIconTheme *icon_theme;
305
  g_autoptr(GError) error = NULL;
306

Mohammed Sadiq's avatar
Mohammed Sadiq committed
307 308
  G_APPLICATION_CLASS (calls_application_parent_class)->startup (application);

Adrien Plazas's avatar
Adrien Plazas committed
309 310
  hdy_init ();

311 312 313 314 315
  if (!call_audio_init (&error))
    {
      g_warning ("Failed to init libcallaudio: %s", error->message);
    }

Bob Ham's avatar
Bob Ham committed
316 317 318
  g_set_prgname (APP_ID);
  g_set_application_name (_("Calls"));

319 320 321
  icon_theme = gtk_icon_theme_get_default ();
  gtk_icon_theme_add_resource_path (icon_theme, "/sm/puri/calls/");

322 323 324 325
  g_action_map_add_action_entries (G_ACTION_MAP (application),
                                   actions,
                                   G_N_ELEMENTS (actions),
                                   application);
326 327

  css_setup ();
328 329 330
}


Bob Ham's avatar
Bob Ham committed
331 332 333 334 335 336 337 338 339 340 341 342
static gboolean
start_proper (CallsApplication  *self)
{
  GtkApplication *gtk_app;

  if (self->main_window)
    {
      return TRUE;
    }

  gtk_app = GTK_APPLICATION (self);

Julian Sparber's avatar
Julian Sparber committed
343
  self->ringer = calls_ringer_new ();
Bob Ham's avatar
Bob Ham committed
344 345
  g_assert (self->ringer != NULL);

Julian Sparber's avatar
Julian Sparber committed
346
  self->record_store = calls_record_store_new ();
Bob Ham's avatar
Bob Ham committed
347 348
  g_assert (self->record_store != NULL);

349
  self->contacts = calls_contacts_get_default ();
350 351
  g_assert (self->contacts != NULL);

352
  self->notifier = calls_notifier_new ();
353 354
  g_assert (CALLS_IS_NOTIFIER (self->notifier));

Bob Ham's avatar
Bob Ham committed
355 356
  self->main_window = calls_main_window_new
    (gtk_app,
357
     G_LIST_MODEL (self->record_store));
Bob Ham's avatar
Bob Ham committed
358 359
  g_assert (self->main_window != NULL);

Julian Sparber's avatar
Julian Sparber committed
360
  self->call_window = calls_call_window_new (gtk_app);
Bob Ham's avatar
Bob Ham committed
361 362 363 364 365 366
  g_assert (self->call_window != NULL);

  return TRUE;
}


Mohammed Sadiq's avatar
Mohammed Sadiq committed
367 368 369
static void
activate (GApplication *application)
{
Bob Ham's avatar
Bob Ham committed
370
  CallsApplication *self = CALLS_APPLICATION (application);
371
  gboolean present;
Bob Ham's avatar
Bob Ham committed
372 373

  g_debug ("Activated");
Mohammed Sadiq's avatar
Mohammed Sadiq committed
374

375
  if (self->main_window)
Bob Ham's avatar
Bob Ham committed
376
    {
377 378 379 380 381 382 383 384 385 386 387
      present = TRUE;
    }
  else
    {
      gboolean ok = start_proper (self);
      if (!ok)
        {
          return;
        }

      present = !self->daemon;
Bob Ham's avatar
Bob Ham committed
388 389
    }

390 391 392 393
  if (present)
    {
      gtk_window_present (GTK_WINDOW (self->main_window));
    }
Bob Ham's avatar
Bob Ham committed
394
}
Mohammed Sadiq's avatar
Mohammed Sadiq committed
395 396


Bob Ham's avatar
Bob Ham committed
397 398 399 400 401 402 403 404 405 406 407 408
static void
open_tel_uri (CallsApplication *self,
              const gchar      *uri)
{
  EPhoneNumber *number;
  GError *error = NULL;
  gchar *dial_str;

  g_debug ("Opening tel URI `%s'", uri);

  number = e_phone_number_from_string (uri, NULL, &error);
  if (!number)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
409
    {
Bob Ham's avatar
Bob Ham committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423
      g_warning ("Ignoring unparsable tel URI `%s': %s",
                 uri, error->message);
      g_error_free (error);
      return;
    }

  dial_str = e_phone_number_to_string
    (number, E_PHONE_NUMBER_FORMAT_E164);
  e_phone_number_free (number);

  calls_main_window_dial (self->main_window,
                          dial_str);
  g_free (dial_str);
}
424

425

Bob Ham's avatar
Bob Ham committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
static void
app_open (GApplication  *application,
          GFile        **files,
          gint           n_files,
          const gchar   *hint)
{
  CallsApplication *self = CALLS_APPLICATION (application);
  gint i;

  g_assert (n_files > 0);

  g_debug ("Opened (%i files)", n_files);

  start_proper (self);

  for (i = 0; i < n_files; ++i)
    {
      gchar *uri;
      if (g_file_has_uri_scheme (files[i], "tel"))
        {
          uri = g_file_get_uri (files[i]);

          open_tel_uri (self, uri);
        }
      else
        {
          uri = g_file_get_parse_name (files[i]);
          g_warning ("Don't know how to"
                     " open file `%s', ignoring",
                     uri);
456 457
        }

Bob Ham's avatar
Bob Ham committed
458
      g_free (uri);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
459 460 461
    }
}

462 463 464 465 466 467 468 469 470 471 472 473

static void
constructed (GObject *object)
{
  CallsApplication *self = CALLS_APPLICATION (object);
  GSimpleActionGroup *action_group;

  action_group = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (action_group),
                                   actions, G_N_ELEMENTS (actions), self);
  g_object_unref (action_group);

474
  G_OBJECT_CLASS (calls_application_parent_class)->constructed (object);
475 476 477 478
}


static void
479
finalize (GObject *object)
480 481 482
{
  CallsApplication *self = (CallsApplication *)object;

Bob Ham's avatar
Bob Ham committed
483 484
  g_clear_object (&self->call_window);
  g_clear_object (&self->main_window);
485
  g_clear_object (&self->record_store);
486
  g_clear_object (&self->contacts);
487
  g_clear_object (&self->ringer);
488
  g_clear_object (&self->notifier);
489 490 491 492 493

  G_OBJECT_CLASS (calls_application_parent_class)->dispose (object);
}


Mohammed Sadiq's avatar
Mohammed Sadiq committed
494 495 496 497 498 499
static void
calls_application_class_init (CallsApplicationClass *klass)
{
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

500 501
  object_class->constructed = constructed;
  object_class->finalize = finalize;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
502

503
  application_class->handle_local_options = handle_local_options;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
504 505
  application_class->startup = startup;
  application_class->activate = activate;
Bob Ham's avatar
Bob Ham committed
506
  application_class->open = app_open;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
507 508 509 510 511 512

  g_type_ensure (CALLS_TYPE_ENCRYPTION_INDICATOR);
  g_type_ensure (CALLS_TYPE_HISTORY_BOX);
  g_type_ensure (CALLS_TYPE_NEW_CALL_BOX);
}

513

Mohammed Sadiq's avatar
Mohammed Sadiq committed
514 515 516
static void
calls_application_init (CallsApplication *self)
{
517 518 519 520 521 522 523
  const GOptionEntry options[] = {
    {
      "provider", 'p', G_OPTION_FLAG_NONE,
      G_OPTION_ARG_STRING, NULL,
      _("The name of the plugin to use for the call Provider"),
      _("PLUGIN")
    },
524 525 526 527 528 529
    {
      "daemon", 'd', G_OPTION_FLAG_NONE,
      G_OPTION_ARG_NONE, NULL,
      _("Whether to present the main window on startup"),
      NULL
    },
530 531 532 533 534 535
    {
      "dial", 'l', G_OPTION_FLAG_NONE,
      G_OPTION_ARG_STRING, NULL,
      _("Dial a number"),
      _("NUMBER")
    },
536 537 538 539 540 541
    {
      NULL
    }
  };

  g_application_add_main_option_entries (G_APPLICATION (self), options);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
542 543
}

544

Mohammed Sadiq's avatar
Mohammed Sadiq committed
545 546 547 548 549
CallsApplication *
calls_application_new (void)
{
  return g_object_new (CALLS_TYPE_APPLICATION,
                       "application-id", APP_ID,
Bob Ham's avatar
Bob Ham committed
550
                       "flags", G_APPLICATION_HANDLES_OPEN,
551
                       "register-session", TRUE,
Mohammed Sadiq's avatar
Mohammed Sadiq committed
552 553
                       NULL);
}