2018-08-01 09:41:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 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: Adrien Plazas <adrien.plazas@puri.sm>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "calls-new-call-box.h"
|
2020-07-07 12:42:23 +00:00
|
|
|
#include "calls-ussd.h"
|
|
|
|
#include "calls-main-window.h"
|
2020-03-18 10:29:41 +00:00
|
|
|
#include "calls-manager.h"
|
2018-08-01 09:41:11 +00:00
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <handy.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct _CallsNewCallBox
|
|
|
|
{
|
|
|
|
GtkBox parent_instance;
|
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
GtkListStore *origin_store;
|
2018-08-01 09:41:11 +00:00
|
|
|
GtkComboBox *origin_box;
|
2019-08-22 15:01:10 +00:00
|
|
|
GtkButton *backspace;
|
2019-09-17 12:09:33 +00:00
|
|
|
HdyKeypad *keypad;
|
2018-10-19 10:19:41 +00:00
|
|
|
GtkButton *dial;
|
2020-12-27 11:01:35 +00:00
|
|
|
GtkGestureLongPress *long_press_back_gesture;
|
2019-08-05 10:25:45 +00:00
|
|
|
|
|
|
|
GList *dial_queue;
|
2018-08-01 09:41:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (CallsNewCallBox, calls_new_call_box, GTK_TYPE_BOX);
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ORIGIN_STORE_COLUMN_NAME,
|
|
|
|
ORIGIN_STORE_COLUMN_ORIGIN
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
static CallsOrigin *
|
|
|
|
get_origin (CallsNewCallBox *self)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean ok;
|
|
|
|
CallsOrigin *origin;
|
|
|
|
|
|
|
|
ok = gtk_combo_box_get_active_iter (self->origin_box, &iter);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_model_get (GTK_TREE_MODEL (self->origin_store),
|
|
|
|
&iter,
|
|
|
|
ORIGIN_STORE_COLUMN_ORIGIN, &origin,
|
|
|
|
-1);
|
|
|
|
g_assert (CALLS_IS_ORIGIN (origin));
|
|
|
|
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-27 11:01:35 +00:00
|
|
|
static void
|
|
|
|
long_press_back_cb (CallsNewCallBox *self)
|
|
|
|
{
|
|
|
|
GtkEntry *entry = hdy_keypad_get_entry (self->keypad);
|
|
|
|
gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:41:11 +00:00
|
|
|
static void
|
2019-08-22 15:01:10 +00:00
|
|
|
backspace_clicked_cb (CallsNewCallBox *self)
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2020-09-29 10:22:47 +00:00
|
|
|
GtkEntry *entry = hdy_keypad_get_entry (self->keypad);
|
2019-09-17 12:09:33 +00:00
|
|
|
g_signal_emit_by_name (entry, "backspace", NULL);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:42:23 +00:00
|
|
|
static void
|
|
|
|
ussd_send_cb (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
CallsNewCallBox *self;
|
|
|
|
CallsUssd *ussd = (CallsUssd *)object;
|
|
|
|
g_autoptr(GTask) task = user_data;
|
|
|
|
GError *error = NULL;
|
|
|
|
char *response;
|
|
|
|
|
|
|
|
g_assert (G_IS_TASK (task));
|
|
|
|
self = g_task_get_source_object (task);
|
|
|
|
|
|
|
|
g_assert (CALLS_IS_NEW_CALL_BOX (self));
|
|
|
|
g_assert (CALLS_IS_USSD (ussd));
|
|
|
|
|
|
|
|
response = calls_ussd_initiate_finish (ussd, result, &error);
|
|
|
|
g_task_set_task_data (task, g_object_ref (ussd), g_object_unref);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
else
|
|
|
|
g_task_return_pointer (task, response, g_free);
|
|
|
|
}
|
2018-08-01 09:41:11 +00:00
|
|
|
|
|
|
|
static void
|
2019-08-01 13:25:53 +00:00
|
|
|
dial_clicked_cb (CallsNewCallBox *self)
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2020-09-29 10:22:47 +00:00
|
|
|
GtkEntry *entry = hdy_keypad_get_entry (self->keypad);
|
2020-07-07 12:42:23 +00:00
|
|
|
GtkWidget *window;
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
window = gtk_widget_get_toplevel (GTK_WIDGET (self));
|
|
|
|
text = gtk_entry_get_text (entry);
|
|
|
|
|
|
|
|
if (CALLS_IS_MAIN_WINDOW (window))
|
|
|
|
calls_main_window_dial (CALLS_MAIN_WINDOW (window), text);
|
|
|
|
else
|
|
|
|
calls_new_call_box_dial (self, text);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
static void
|
|
|
|
dial_queued_cb (gchar *target,
|
|
|
|
CallsOrigin *origin)
|
|
|
|
{
|
|
|
|
g_debug ("Dialing queued target `%s'", target);
|
|
|
|
calls_origin_dial (origin, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_dial_queue (CallsNewCallBox *self)
|
|
|
|
{
|
|
|
|
g_list_free_full (self->dial_queue, g_free);
|
|
|
|
self->dial_queue = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dial_queued (CallsNewCallBox *self)
|
|
|
|
{
|
|
|
|
CallsOrigin *origin;
|
|
|
|
|
|
|
|
if (!self->dial_queue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_debug ("Dialing %u queued targets",
|
|
|
|
g_list_length (self->dial_queue));
|
|
|
|
|
|
|
|
origin = get_origin (self);
|
|
|
|
g_assert (origin != NULL);
|
|
|
|
|
|
|
|
g_list_foreach (self->dial_queue,
|
|
|
|
(GFunc)dial_queued_cb,
|
|
|
|
origin);
|
|
|
|
g_object_unref (origin);
|
|
|
|
|
|
|
|
clear_dial_queue (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-01 09:41:11 +00:00
|
|
|
void
|
|
|
|
update_origin_box (CallsNewCallBox *self)
|
|
|
|
{
|
2018-10-05 11:27:55 +00:00
|
|
|
GtkTreeModel *origin_store = GTK_TREE_MODEL (self->origin_store);
|
2018-08-01 09:41:11 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
if (!gtk_tree_model_get_iter_first (origin_store, &iter))
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
|
|
|
gtk_widget_hide (GTK_WIDGET (self->origin_box));
|
2018-10-19 10:19:41 +00:00
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (self->dial), FALSE);
|
2018-08-01 09:41:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
/* We know there is at least one origin. */
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2018-10-19 10:19:41 +00:00
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (self->dial), TRUE);
|
|
|
|
|
2018-08-01 09:41:11 +00:00
|
|
|
if (!gtk_tree_model_iter_next (origin_store, &iter))
|
|
|
|
{
|
|
|
|
gtk_combo_box_set_active (self->origin_box, 0);
|
|
|
|
gtk_widget_hide (GTK_WIDGET (self->origin_box));
|
|
|
|
}
|
2019-08-05 10:25:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We know there are multiple origins. */
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
if (gtk_combo_box_get_active (self->origin_box) < 0)
|
|
|
|
{
|
|
|
|
gtk_combo_box_set_active (self->origin_box, 0);
|
|
|
|
}
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
/* We know there are multiple origins and one is selected. */
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
gtk_widget_show (GTK_WIDGET (self->origin_box));
|
|
|
|
}
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
dial_queued (self);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2018-10-05 11:27:55 +00:00
|
|
|
add_origin (CallsNewCallBox *self, CallsOrigin *origin)
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2018-10-05 11:27:55 +00:00
|
|
|
GtkTreeIter iter;
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
gtk_list_store_append (self->origin_store, &iter);
|
|
|
|
gtk_list_store_set (self->origin_store, &iter,
|
|
|
|
ORIGIN_STORE_COLUMN_NAME, calls_origin_get_name (origin),
|
|
|
|
ORIGIN_STORE_COLUMN_ORIGIN, G_OBJECT (origin),
|
|
|
|
-1);
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
update_origin_box (self);
|
|
|
|
}
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
remove_origin (CallsNewCallBox *self, CallsOrigin *origin)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
ok = calls_list_store_find (self->origin_store, origin,
|
|
|
|
ORIGIN_STORE_COLUMN_ORIGIN, &iter);
|
|
|
|
g_return_if_fail (ok);
|
|
|
|
|
|
|
|
gtk_list_store_remove (self->origin_store, &iter);
|
|
|
|
|
|
|
|
update_origin_box (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_origins (CallsNewCallBox *self)
|
|
|
|
{
|
|
|
|
GtkTreeModel *model = GTK_TREE_MODEL (self->origin_store);
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
while (gtk_tree_model_get_iter_first (model, &iter))
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2018-10-05 11:27:55 +00:00
|
|
|
gtk_list_store_remove (self->origin_store, &iter);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
2018-10-05 11:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2020-03-18 10:29:41 +00:00
|
|
|
calls_new_call_box_init (CallsNewCallBox *self)
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2020-03-18 10:29:41 +00:00
|
|
|
g_autoptr (GList) origins = NULL;
|
|
|
|
GList *o;
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2020-03-18 10:29:41 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2020-03-18 10:29:41 +00:00
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
|
|
|
"origin-add",
|
|
|
|
G_CALLBACK (add_origin),
|
|
|
|
self);
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2020-03-18 10:29:41 +00:00
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
|
|
|
"origin-remove",
|
|
|
|
G_CALLBACK (remove_origin),
|
|
|
|
self);
|
2018-08-01 09:41:11 +00:00
|
|
|
|
2020-03-18 10:29:41 +00:00
|
|
|
origins = calls_manager_get_origins (calls_manager_get_default ());
|
|
|
|
for (o = origins; o != NULL; o = o->next) {
|
|
|
|
add_origin (self, o->data);
|
|
|
|
}
|
2018-10-05 11:27:55 +00:00
|
|
|
|
|
|
|
update_origin_box (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dispose (GObject *object)
|
|
|
|
{
|
2018-08-01 09:41:11 +00:00
|
|
|
CallsNewCallBox *self = CALLS_NEW_CALL_BOX (object);
|
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
clear_dial_queue (self);
|
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
if (self->origin_store)
|
2018-08-01 09:41:11 +00:00
|
|
|
{
|
2018-10-05 11:27:55 +00:00
|
|
|
remove_origins (self);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
2018-10-05 11:27:55 +00:00
|
|
|
|
2020-12-27 11:01:35 +00:00
|
|
|
if (self->long_press_back_gesture != NULL)
|
|
|
|
g_object_unref (self->long_press_back_gesture);
|
|
|
|
|
2020-02-18 15:01:22 +00:00
|
|
|
G_OBJECT_CLASS (calls_new_call_box_parent_class)->dispose (object);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_new_call_box_class_init (CallsNewCallBoxClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2018-10-05 11:27:55 +00:00
|
|
|
object_class->dispose = dispose;
|
2018-08-01 09:41:11 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/sm/puri/calls/ui/new-call-box.ui");
|
2018-10-05 11:27:55 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, origin_store);
|
2018-08-01 09:41:11 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, origin_box);
|
2019-08-22 15:01:10 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, backspace);
|
2020-12-27 11:01:35 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, long_press_back_gesture);
|
2019-09-17 12:09:33 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, keypad);
|
2018-10-19 10:19:41 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, CallsNewCallBox, dial);
|
2018-08-01 09:41:11 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, dial_clicked_cb);
|
2019-08-22 15:01:10 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, backspace_clicked_cb);
|
2020-12-27 11:01:35 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, long_press_back_cb);
|
2018-08-01 09:41:11 +00:00
|
|
|
}
|
2018-10-05 11:27:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
CallsNewCallBox *
|
2020-03-18 10:29:41 +00:00
|
|
|
calls_new_call_box_new (void)
|
2018-10-05 11:27:55 +00:00
|
|
|
{
|
2020-03-18 10:29:41 +00:00
|
|
|
return g_object_new (CALLS_TYPE_NEW_CALL_BOX, NULL);
|
2018-10-05 11:27:55 +00:00
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
void
|
|
|
|
calls_new_call_box_dial (CallsNewCallBox *self,
|
|
|
|
const gchar *target)
|
|
|
|
{
|
|
|
|
CallsOrigin *origin;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_NEW_CALL_BOX (self));
|
|
|
|
g_return_if_fail (target != NULL);
|
|
|
|
|
2019-08-05 10:25:45 +00:00
|
|
|
origin = get_origin (self);
|
|
|
|
if (!origin)
|
2019-08-01 13:25:53 +00:00
|
|
|
{
|
2019-08-05 10:25:45 +00:00
|
|
|
// Queue for dialing when an origin appears
|
|
|
|
g_debug ("Can't submit call with no origin, queuing for later");
|
|
|
|
self->dial_queue = g_list_append (self->dial_queue,
|
|
|
|
g_strdup (target));
|
2019-08-01 13:25:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
calls_origin_dial (origin, target);
|
2019-08-05 10:25:45 +00:00
|
|
|
g_object_unref (origin);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
2020-07-07 12:42:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
calls_new_call_box_send_ussd_async (CallsNewCallBox *self,
|
|
|
|
const char *target,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_autoptr(CallsOrigin) origin = NULL;
|
|
|
|
g_autoptr(GTask) task = NULL;
|
|
|
|
GtkEntry *entry;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_NEW_CALL_BOX (self));
|
|
|
|
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
|
|
|
|
g_return_if_fail (target && *target);
|
|
|
|
|
|
|
|
origin = get_origin (self);
|
|
|
|
|
|
|
|
task = g_task_new (self, cancellable, callback, user_data);
|
|
|
|
|
|
|
|
if (!CALLS_IS_USSD (origin))
|
|
|
|
{
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"No origin with USSD available");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!calls_number_is_ussd (target))
|
|
|
|
{
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"%s is not a valid USSD code", target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
calls_ussd_initiate_async (CALLS_USSD (origin), target, cancellable,
|
|
|
|
ussd_send_cb, g_steal_pointer (&task));
|
|
|
|
|
|
|
|
entry = hdy_keypad_get_entry (self->keypad);
|
|
|
|
gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
calls_new_call_box_send_ussd_finish (CallsNewCallBox *self,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_NEW_CALL_BOX (self), NULL);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), NULL);
|
|
|
|
|
|
|
|
return g_task_propagate_pointer (G_TASK (result), error);
|
|
|
|
}
|