1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-11-04 23:51:17 +00:00
Purism-Calls/src/calls-application.c

609 lines
15 KiB
C
Raw Normal View History

2018-09-15 10:33:36 +00:00
/* calls-application.c
*
* Copyright (C) 2018, 2019 Purism SPC
2018-09-15 10:33:36 +00:00
* 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"
2018-11-09 16:30:40 +00:00
#include "calls-ringer.h"
#include "calls-notifier.h"
#include "calls-record-store.h"
2018-09-15 10:33:36 +00:00
#include "calls-call-window.h"
#include "calls-main-window.h"
#include "calls-manager.h"
2018-09-15 10:33:36 +00:00
#include "calls-application.h"
#include <glib/gi18n.h>
#include <handy.h>
#include <libcallaudio.h>
#include <libebook-contacts/libebook-contacts.h>
2018-09-15 10:33:36 +00:00
/**
* SECTION: calls-application
* @title: CallsApplication
* @short_description: Base Application class
* @include: "calls-application.h"
*/
#define DEFAULT_PROVIDER_PLUGIN "mm"
2018-09-15 10:33:36 +00:00
struct _CallsApplication
{
GtkApplication parent_instance;
gboolean daemon;
CallsManager *manager;
CallsRinger *ringer;
CallsNotifier *notifier;
CallsRecordStore *record_store;
CallsMainWindow *main_window;
CallsCallWindow *call_window;
char *uri;
2018-09-15 10:33:36 +00:00
};
G_DEFINE_TYPE (CallsApplication, calls_application, GTK_TYPE_APPLICATION);
static gboolean start_proper (CallsApplication *self);
2018-09-15 10:33:36 +00:00
static gint
handle_local_options (GApplication *application,
GVariantDict *options)
{
gboolean ok;
g_autoptr (GError) error = NULL;
const gchar *arg;
g_debug ("Registering application");
ok = g_application_register (application, NULL, &error);
if (!ok)
{
g_error ("Error registering application: %s",
error->message);
}
ok = g_variant_dict_lookup (options, "provider", "&s", &arg);
if (ok)
{
g_action_group_activate_action (G_ACTION_GROUP (application),
"set-provider-name",
g_variant_new_string (arg));
}
else
{
g_action_group_activate_action (G_ACTION_GROUP (application),
"set-provider-name",
g_variant_new_string (DEFAULT_PROVIDER_PLUGIN));
}
ok = g_variant_dict_contains (options, "daemon");
if (ok)
{
g_action_group_activate_action (G_ACTION_GROUP (application),
"set-daemon",
NULL);
}
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));
}
return -1; // Continue processing signal
}
2018-09-15 10:33:36 +00:00
static void
set_provider_name_action (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
2018-09-15 10:33:36 +00:00
{
const gchar *name;
2018-09-15 10:33:36 +00:00
name = g_variant_get_string (parameter, NULL);
g_return_if_fail (name != NULL);
2018-09-15 10:33:36 +00:00
/* 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)
{
g_warning ("Cannot set provider name to `%s'"
" because provider is already created",
name);
return;
}
g_debug ("Start loading provider `%s'", name);
calls_manager_set_provider (calls_manager_get_default (), name);
2018-09-15 10:33:36 +00:00
}
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");
}
#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)
{
g_autoptr (GError) error = NULL;
2021-02-10 23:32:25 +00:00
g_autoptr (GRegex) reject = g_regex_new (REJECT_RE, 0, 0, &error);
gboolean matches;
if (!reject)
{
g_warning ("Could not compile regex for"
" dial number checking: %s",
error->message);
return FALSE;
}
matches = g_regex_match (reject, number, 0, NULL);
return !matches;
}
static gchar *
extract_dial_string (const gchar *number)
{
g_autoptr (GError) error = NULL;
2021-02-10 23:32:25 +00:00
g_autoptr (GRegex) replace_visual = g_regex_new (VISUAL_RE, 0, 0, &error);
gchar *dial_string;
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;
g_autofree gchar *dial_string = NULL;
number = g_variant_get_string (parameter, NULL);
g_return_if_fail (number != NULL);
2021-03-09 16:22:12 +00:00
if (g_str_has_prefix (number, "sip:") ||
g_str_has_prefix (number, "sips:")) {
dial_string = g_strdup (number);
goto proper;
}
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);
2021-03-09 16:22:12 +00:00
proper:
start_proper (self);
calls_main_window_dial (self->main_window,
dial_string);
}
2021-01-19 15:03:28 +00:00
static void
copy_number (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
const gchar *number = g_variant_get_string (parameter, NULL);
GtkClipboard *clipboard =
gtk_clipboard_get_default (gdk_display_get_default ());
gtk_clipboard_set_text (clipboard, number, -1);
g_debug ("Copied `%s' to clipboard", number);
}
static void
manager_state_changed_cb (GApplication *application)
{
GAction* dial_action = g_action_map_lookup_action (G_ACTION_MAP (application), "dial");
CallsManagerState state = calls_manager_get_state (calls_manager_get_default ());
g_simple_action_set_enabled (G_SIMPLE_ACTION (dial_action), state == CALLS_MANAGER_STATE_READY);
}
static const GActionEntry actions[] =
{
{ "set-provider-name", set_provider_name_action, "s" },
{ "set-daemon", set_daemon_action, NULL },
{ "dial", dial_action, "s" },
2021-01-19 15:03:28 +00:00
{ "copy-number", copy_number, "s"},
};
2018-09-15 10:33:36 +00:00
static void
startup (GApplication *application)
{
g_autoptr (GtkCssProvider) provider = NULL;
g_autoptr (GError) error = NULL;
2018-09-15 10:33:36 +00:00
G_APPLICATION_CLASS (calls_application_parent_class)->startup (application);
hdy_init ();
if (!call_audio_init (&error))
{
g_warning ("Failed to init libcallaudio: %s", error->message);
}
2018-11-09 16:30:40 +00:00
g_set_prgname (APP_ID);
g_set_application_name (_("Calls"));
g_action_map_add_action_entries (G_ACTION_MAP (application),
actions,
G_N_ELEMENTS (actions),
application);
g_signal_connect_swapped (calls_manager_get_default (),
"notify::state",
G_CALLBACK (manager_state_changed_cb),
application);
manager_state_changed_cb (application);
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/sm/puri/calls/style.css");
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
static void
notify_window_visible_cb (GtkWidget *window,
GParamSpec *pspec,
CallsApplication *application)
{
CallsManager *manager = calls_manager_get_default ();
g_return_if_fail (CALLS_IS_APPLICATION (application));
g_return_if_fail (CALLS_IS_CALL_WINDOW (window));
/* The UI is being closed, hang up active calls */
if (!gtk_widget_is_visible (window))
calls_manager_hang_up_all_calls (manager);
}
static gboolean
start_proper (CallsApplication *self)
{
GtkApplication *gtk_app;
if (self->main_window)
{
return TRUE;
}
gtk_app = GTK_APPLICATION (self);
2020-03-18 11:53:13 +00:00
self->ringer = calls_ringer_new ();
g_assert (self->ringer != NULL);
2020-03-18 12:58:13 +00:00
self->record_store = calls_record_store_new ();
g_assert (self->record_store != NULL);
self->notifier = calls_notifier_new ();
g_assert (CALLS_IS_NOTIFIER (self->notifier));
self->main_window = calls_main_window_new
(gtk_app,
G_LIST_MODEL (self->record_store));
g_assert (self->main_window != NULL);
self->call_window = calls_call_window_new (gtk_app);
g_assert (self->call_window != NULL);
g_signal_connect (self->call_window,
"notify::visible",
G_CALLBACK (notify_window_visible_cb),
self);
return TRUE;
}
2021-03-09 16:22:12 +00:00
static void
open_sip_uri (CallsApplication *self,
const gchar *uri)
{
gchar **tokens = NULL;
g_assert (uri);
tokens = g_strsplit (uri, "///", 2);
if (tokens) {
/* Remove "///" from "sip:///user@host" */
g_autofree gchar *dial_string = g_strconcat (tokens[0], tokens[1], NULL);
calls_main_window_dial (self->main_window, dial_string);
g_strfreev (tokens);
}
else {
/* Dial the uri as it is */
calls_main_window_dial (self->main_window, uri);
}
}
static void
open_tel_uri (CallsApplication *self,
const gchar *uri)
{
g_autoptr (EPhoneNumber) number = NULL;
g_autoptr (GError) error = NULL;
g_autofree gchar *dial_str = NULL;
2021-03-25 19:28:37 +00:00
g_autofree gchar *country_code = NULL;
g_object_get (calls_manager_get_default (),
"country-code", &country_code,
NULL);
g_debug ("Opening tel URI `%s'", uri);
2021-03-25 19:28:37 +00:00
number = e_phone_number_from_string (uri, country_code, &error);
if (!number)
2018-09-15 10:33:36 +00:00
{
g_autofree gchar *msg =
g_strdup_printf (_("Tried dialing unparsable tel URI `%s'"), uri);
g_signal_emit_by_name (calls_manager_get_default (),
"error",
msg);
g_warning ("Ignoring unparsable tel URI `%s': %s",
uri, error->message);
return;
}
dial_str = e_phone_number_to_string
(number, E_PHONE_NUMBER_FORMAT_E164);
calls_main_window_dial (self->main_window,
dial_str);
}
static void
activate (GApplication *application)
{
CallsApplication *self = CALLS_APPLICATION (application);
gboolean present;
g_debug ("Activated");
if (self->main_window)
{
present = TRUE;
}
else
{
gboolean ok = start_proper (self);
if (!ok)
{
return;
}
present = !self->daemon;
}
if (present || self->uri)
{
gtk_window_present (GTK_WINDOW (self->main_window));
}
if (self->uri)
2021-03-09 16:22:12 +00:00
{
if (g_str_has_prefix (self->uri, "tel:"))
open_tel_uri (self, self->uri);
else if (g_str_has_prefix (self->uri, "sip:") ||
g_str_has_prefix (self->uri, "sips:"))
open_sip_uri (self, self->uri);
}
g_clear_pointer (&self->uri, g_free);
}
static void
app_open (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint)
{
CallsApplication *self = CALLS_APPLICATION (application);
g_assert (n_files > 0);
if (n_files > 1)
g_warning ("Calls can handle only one call a time. %u items provided", n_files);
2021-03-09 16:22:12 +00:00
if (g_file_has_uri_scheme (files[0], "tel") ||
g_file_has_uri_scheme (files[0], "sip") ||
g_file_has_uri_scheme (files[0], "sips"))
{
g_free (self->uri);
self->uri = g_file_get_uri (files[0]);
g_debug ("Opening %s", self->uri);
g_application_activate (application);
}
else
{
g_autofree char *msg = NULL;
g_autofree char *uri = NULL;
uri = g_file_get_parse_name (files[0]);
g_warning ("Don't know how to"
" open file `%s', ignoring",
uri);
msg = g_strdup_printf (_("Don't know how to open `%s'"), uri);
g_signal_emit_by_name (calls_manager_get_default (),
"error", msg);
2018-09-15 10:33:36 +00:00
}
}
static void
finalize (GObject *object)
{
CallsApplication *self = (CallsApplication *)object;
g_clear_object (&self->call_window);
g_clear_object (&self->main_window);
g_clear_object (&self->record_store);
g_clear_object (&self->ringer);
g_clear_object (&self->notifier);
g_free (self->uri);
G_OBJECT_CLASS (calls_application_parent_class)->finalize (object);
}
2018-09-15 10:33:36 +00:00
static void
calls_application_class_init (CallsApplicationClass *klass)
{
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = finalize;
2018-09-15 10:33:36 +00:00
application_class->handle_local_options = handle_local_options;
2018-09-15 10:33:36 +00:00
application_class->startup = startup;
application_class->activate = activate;
application_class->open = app_open;
2018-09-15 10:33:36 +00:00
g_type_ensure (CALLS_TYPE_ENCRYPTION_INDICATOR);
g_type_ensure (CALLS_TYPE_HISTORY_BOX);
g_type_ensure (CALLS_TYPE_NEW_CALL_BOX);
}
2018-09-15 10:33:36 +00:00
static void
calls_application_init (CallsApplication *self)
{
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")
},
{
"daemon", 'd', G_OPTION_FLAG_NONE,
G_OPTION_ARG_NONE, NULL,
_("Whether to present the main window on startup"),
NULL
},
{
"dial", 'l', G_OPTION_FLAG_NONE,
G_OPTION_ARG_STRING, NULL,
_("Dial a number"),
_("NUMBER")
},
{
NULL
}
};
g_application_add_main_option_entries (G_APPLICATION (self), options);
2018-09-15 10:33:36 +00:00
}
2018-09-15 10:33:36 +00:00
CallsApplication *
calls_application_new (void)
{
return g_object_new (CALLS_TYPE_APPLICATION,
"application-id", APP_ID,
"flags", G_APPLICATION_HANDLES_OPEN,
"register-session", TRUE,
2018-09-15 10:33:36 +00:00
NULL);
}