2019-07-22 10:52:46 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-06-18 09:10:36 +00:00
|
|
|
#define G_LOG_DOMAIN "CallsRecordStore"
|
|
|
|
|
2022-12-18 10:18:10 +00:00
|
|
|
#include "calls-config.h"
|
2022-02-04 03:03:58 +00:00
|
|
|
|
2019-07-22 10:52:46 +00:00
|
|
|
#include "calls-call-record.h"
|
2020-03-18 12:58:13 +00:00
|
|
|
#include "calls-manager.h"
|
2022-02-04 03:31:23 +00:00
|
|
|
#include "calls-ui-call-data.h"
|
2022-02-04 03:03:58 +00:00
|
|
|
#include "calls-record-store.h"
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
#include <gom/gom.h>
|
|
|
|
#include <glib/gi18n.h>
|
2021-01-23 21:05:18 +00:00
|
|
|
#include <glib/gstdio.h>
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define RECORD_STORE_FILENAME "records.db"
|
2021-04-12 09:44:30 +00:00
|
|
|
#define RECORD_STORE_VERSION 2
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2023-01-22 13:45:17 +00:00
|
|
|
enum {
|
|
|
|
SIGNAL_DB_DONE,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
static guint signals[N_SIGNALS];
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2023-01-17 14:13:55 +00:00
|
|
|
typedef enum {
|
2019-07-22 10:52:46 +00:00
|
|
|
STARTED,
|
|
|
|
ANSWERED,
|
|
|
|
ENDED
|
|
|
|
} CallsCallRecordState;
|
|
|
|
|
|
|
|
|
|
|
|
static CallsCallRecordState
|
2022-02-04 03:31:23 +00:00
|
|
|
state_to_record_state (CuiCallState call_state)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
2021-06-02 18:39:23 +00:00
|
|
|
switch (call_state) {
|
2022-02-04 03:31:23 +00:00
|
|
|
case CUI_CALL_STATE_CALLING:
|
|
|
|
case CUI_CALL_STATE_INCOMING:
|
2021-06-02 18:39:23 +00:00
|
|
|
return STARTED;
|
|
|
|
|
2022-02-04 03:31:23 +00:00
|
|
|
case CUI_CALL_STATE_ACTIVE:
|
|
|
|
case CUI_CALL_STATE_HELD:
|
2021-06-02 18:39:23 +00:00
|
|
|
return ANSWERED;
|
|
|
|
|
2022-02-04 03:31:23 +00:00
|
|
|
case CUI_CALL_STATE_DISCONNECTED:
|
2021-06-02 18:39:23 +00:00
|
|
|
return ENDED;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2021-06-02 18:42:07 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-18 17:41:15 +00:00
|
|
|
static const char *
|
|
|
|
record_state_to_string (CallsCallRecordState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case STARTED:
|
|
|
|
return "started";
|
|
|
|
case ANSWERED:
|
|
|
|
return "answered";
|
|
|
|
case ENDED:
|
|
|
|
return "ended";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
struct _CallsRecordStore {
|
2019-07-22 10:52:46 +00:00
|
|
|
GtkApplicationWindow parent_instance;
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
gchar *filename;
|
|
|
|
GomAdapter *adapter;
|
|
|
|
GomRepository *repository;
|
2019-07-22 10:52:46 +00:00
|
|
|
};
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
G_DEFINE_TYPE (CallsRecordStore, calls_record_store, G_TYPE_LIST_STORE);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
|
2020-06-08 13:09:57 +00:00
|
|
|
static void
|
|
|
|
delete_record_cb (GomResource *resource,
|
|
|
|
GAsyncResult *res,
|
|
|
|
CallsRecordStore *self)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
gboolean ok;
|
|
|
|
guint id;
|
|
|
|
|
|
|
|
ok = gom_resource_delete_finish (resource,
|
|
|
|
res,
|
|
|
|
&error);
|
|
|
|
|
|
|
|
g_object_get (G_OBJECT (resource),
|
|
|
|
"id",
|
|
|
|
&id,
|
|
|
|
NULL);
|
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
|
|
|
if (error) {
|
|
|
|
g_warning ("Error deleting call record with id %u from database %s",
|
|
|
|
id, error->message);
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
g_warning ("Unknown error deleting call record with id %u from database",
|
|
|
|
id);
|
2020-06-08 13:09:57 +00:00
|
|
|
}
|
2021-06-02 18:39:23 +00:00
|
|
|
|
|
|
|
} else {
|
2020-06-08 13:09:57 +00:00
|
|
|
g_debug ("Successfully deleted call record with id %u from database",
|
|
|
|
id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-04-24 10:24:55 +00:00
|
|
|
delete_call_cb (CallsCallRecord *record,
|
|
|
|
CallsRecordStore *self)
|
2020-06-08 13:09:57 +00:00
|
|
|
{
|
|
|
|
gom_resource_delete_async (GOM_RESOURCE (record),
|
|
|
|
(GAsyncReadyCallback) delete_record_cb,
|
|
|
|
self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
static void
|
|
|
|
load_calls_fetch_cb (GomResourceGroup *group,
|
|
|
|
GAsyncResult *res,
|
|
|
|
CallsRecordStore *self)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2022-04-24 10:24:55 +00:00
|
|
|
gboolean ok;
|
2019-08-01 13:25:53 +00:00
|
|
|
guint count, i;
|
|
|
|
gpointer *records;
|
|
|
|
|
|
|
|
ok = gom_resource_group_fetch_finish (group,
|
|
|
|
res,
|
|
|
|
&error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (error) {
|
|
|
|
g_debug ("Error fetching call records: %s",
|
|
|
|
error->message);
|
2023-01-16 08:09:40 +00:00
|
|
|
goto exit;
|
2021-06-02 18:39:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
g_assert (ok);
|
|
|
|
|
|
|
|
count = gom_resource_group_get_count (group);
|
|
|
|
g_debug ("Fetched %u call records from database `%s'",
|
|
|
|
count, self->filename);
|
|
|
|
records = g_new (gpointer, count);
|
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
GomResource *resource;
|
|
|
|
CallsCallRecord *record;
|
|
|
|
|
|
|
|
resource = gom_resource_group_get_index (group, i);
|
|
|
|
g_assert (resource != NULL);
|
|
|
|
g_assert (CALLS_IS_CALL_RECORD (resource));
|
|
|
|
record = CALLS_CALL_RECORD (resource);
|
|
|
|
|
|
|
|
records[i] = record;
|
|
|
|
|
|
|
|
g_signal_connect (record,
|
|
|
|
"call-delete",
|
|
|
|
G_CALLBACK (delete_call_cb),
|
|
|
|
self);
|
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
g_list_store_splice (G_LIST_STORE (self),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
records,
|
|
|
|
count);
|
|
|
|
|
|
|
|
g_free (records);
|
|
|
|
g_object_unref (group);
|
2023-01-16 08:09:40 +00:00
|
|
|
exit:
|
|
|
|
g_object_unref (self);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_calls_find_cb (GomRepository *repository,
|
|
|
|
GAsyncResult *res,
|
|
|
|
CallsRecordStore *self)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2022-04-24 10:24:55 +00:00
|
|
|
GomResourceGroup *group;
|
2019-08-01 13:25:53 +00:00
|
|
|
guint count;
|
|
|
|
|
|
|
|
group = gom_repository_find_finish (repository,
|
|
|
|
res,
|
|
|
|
&error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (error) {
|
|
|
|
g_debug ("Error finding call records in database `%s': %s",
|
|
|
|
self->filename, error->message);
|
2023-01-16 08:09:40 +00:00
|
|
|
goto exit;
|
2021-06-02 18:39:23 +00:00
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
g_assert (group != NULL);
|
|
|
|
|
|
|
|
count = gom_resource_group_get_count (group);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (count == 0) {
|
|
|
|
g_debug ("No call records found in database `%s'",
|
|
|
|
self->filename);
|
2023-01-16 08:09:40 +00:00
|
|
|
goto exit;
|
2021-06-02 18:39:23 +00:00
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
g_debug ("Found %u call records in database `%s', fetching",
|
|
|
|
count, self->filename);
|
2022-04-24 10:24:55 +00:00
|
|
|
gom_resource_group_fetch_async (group,
|
|
|
|
0,
|
|
|
|
count,
|
|
|
|
(GAsyncReadyCallback) load_calls_fetch_cb,
|
2023-01-16 08:09:40 +00:00
|
|
|
g_object_ref (self));
|
|
|
|
exit:
|
|
|
|
g_object_unref (self);
|
2019-08-01 13:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_calls (CallsRecordStore *self)
|
|
|
|
{
|
|
|
|
GomFilter *filter;
|
|
|
|
GomSorting *sorting;
|
|
|
|
|
|
|
|
filter = gom_filter_new_is_not_null
|
2022-04-24 10:24:55 +00:00
|
|
|
(CALLS_TYPE_CALL_RECORD, "start");
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
sorting = gom_sorting_new (CALLS_TYPE_CALL_RECORD,
|
|
|
|
"start",
|
|
|
|
GOM_SORTING_DESCENDING,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_debug ("Finding records in call record database `%s'",
|
|
|
|
self->filename);
|
|
|
|
gom_repository_find_sorted_async (self->repository,
|
|
|
|
CALLS_TYPE_CALL_RECORD,
|
|
|
|
filter,
|
|
|
|
sorting,
|
2022-04-24 10:24:55 +00:00
|
|
|
(GAsyncReadyCallback) load_calls_find_cb,
|
2023-01-16 08:09:40 +00:00
|
|
|
g_object_ref (self));
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
g_object_unref (G_OBJECT (filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-22 10:52:46 +00:00
|
|
|
static void
|
2022-04-24 10:24:55 +00:00
|
|
|
set_up_repo_migrate_cb (GomRepository *repo,
|
|
|
|
GAsyncResult *res,
|
2019-07-22 10:52:46 +00:00
|
|
|
CallsRecordStore *self)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
ok = gom_repository_automatic_migrate_finish (repo, res, &error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
2022-04-24 10:24:55 +00:00
|
|
|
if (error)
|
|
|
|
g_warning ("Error migrating call record database `%s': %s",
|
|
|
|
self->filename, error->message);
|
|
|
|
else
|
|
|
|
g_warning ("Unknown error migrating call record database `%s'",
|
|
|
|
self->filename);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
g_clear_object (&self->repository);
|
|
|
|
g_clear_object (&self->adapter);
|
|
|
|
} else {
|
2021-06-02 18:39:23 +00:00
|
|
|
g_debug ("Successfully migrated call record database `%s'",
|
|
|
|
self->filename);
|
|
|
|
load_calls (self);
|
|
|
|
}
|
2023-01-16 08:09:40 +00:00
|
|
|
|
2023-01-22 13:45:17 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_DB_DONE], 0, ok);
|
2023-01-16 08:09:40 +00:00
|
|
|
g_object_unref (self);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_up_repo (CallsRecordStore *self)
|
|
|
|
{
|
|
|
|
GomRepository *repo;
|
|
|
|
GList *types = NULL;
|
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
if (self->repository) {
|
|
|
|
g_warning ("Opened call record database `%s'"
|
|
|
|
" while repository exists",
|
|
|
|
self->filename);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
repo = gom_repository_new (self->adapter);
|
|
|
|
|
|
|
|
g_debug ("Attempting migration of call"
|
|
|
|
" record database `%s'",
|
|
|
|
self->filename);
|
2022-04-24 10:24:55 +00:00
|
|
|
types = g_list_append (types, (gpointer) CALLS_TYPE_CALL_RECORD);
|
2019-07-22 10:52:46 +00:00
|
|
|
gom_repository_automatic_migrate_async
|
|
|
|
(repo,
|
2022-04-24 10:24:55 +00:00
|
|
|
RECORD_STORE_VERSION,
|
|
|
|
types,
|
|
|
|
(GAsyncReadyCallback) set_up_repo_migrate_cb,
|
2023-01-16 08:09:40 +00:00
|
|
|
g_object_ref (self));
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
self->repository = repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_adapter (CallsRecordStore *self)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
gboolean ok;
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
if (!self->adapter) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
ok = gom_adapter_close_sync (self->adapter, &error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
2022-04-24 10:24:55 +00:00
|
|
|
if (error)
|
|
|
|
g_warning ("Error closing call record database `%s': %s",
|
|
|
|
self->filename, error->message);
|
|
|
|
else
|
|
|
|
g_warning ("Unknown error closing call record database `%s'",
|
|
|
|
self->filename);
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
g_clear_object (&self->adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-04-24 10:24:55 +00:00
|
|
|
open_repo_adapter_open_cb (GomAdapter *adapter,
|
|
|
|
GAsyncResult *res,
|
2019-07-22 10:52:46 +00:00
|
|
|
CallsRecordStore *self)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
ok = gom_adapter_open_finish (adapter, res, &error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
|
|
|
if (error)
|
|
|
|
g_warning ("Error opening call record database `%s': %s",
|
|
|
|
self->filename, error->message);
|
|
|
|
else
|
|
|
|
g_warning ("Unknown error opening call record database `%s'",
|
|
|
|
self->filename);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
close_adapter (self);
|
2023-01-22 13:45:17 +00:00
|
|
|
g_signal_emit (self, signals[SIGNAL_DB_DONE], 0, FALSE);
|
2021-06-02 18:39:23 +00:00
|
|
|
} else {
|
|
|
|
g_debug ("Successfully opened call record database `%s'",
|
|
|
|
self->filename);
|
|
|
|
set_up_repo (self);
|
|
|
|
}
|
2023-01-16 08:09:40 +00:00
|
|
|
|
|
|
|
g_object_unref (self);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_repo (CallsRecordStore *self)
|
|
|
|
{
|
|
|
|
gchar *dir;
|
|
|
|
gint err;
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
if (self->adapter)
|
2021-06-02 18:39:23 +00:00
|
|
|
return;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
dir = g_path_get_dirname (self->filename);
|
|
|
|
err = g_mkdir_with_parents (dir, 0755);
|
|
|
|
if (err)
|
2021-06-02 18:39:23 +00:00
|
|
|
g_warning ("Could not create Calls data directory `%s': %s",
|
|
|
|
dir, g_strerror (errno));
|
2019-07-22 10:52:46 +00:00
|
|
|
g_free (dir);
|
|
|
|
|
|
|
|
|
|
|
|
uri = g_strdup_printf ("file:%s", self->filename);
|
|
|
|
g_debug ("Opening call record database using URI `%s'", uri);
|
|
|
|
self->adapter = gom_adapter_new ();
|
|
|
|
gom_adapter_open_async
|
|
|
|
(self->adapter,
|
2022-04-24 10:24:55 +00:00
|
|
|
uri,
|
|
|
|
(GAsyncReadyCallback) open_repo_adapter_open_cb,
|
2023-01-16 08:09:40 +00:00
|
|
|
g_object_ref (self));
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
struct CallsRecordCallData {
|
2019-08-01 13:25:53 +00:00
|
|
|
CallsRecordStore *self;
|
2022-04-24 10:24:55 +00:00
|
|
|
CallsUiCallData *call;
|
2019-08-01 13:25:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-07-22 10:52:46 +00:00
|
|
|
static void
|
2019-08-01 13:25:53 +00:00
|
|
|
record_call_save_cb (GomResource *resource,
|
|
|
|
GAsyncResult *res,
|
|
|
|
struct CallsRecordCallData *data)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
2019-08-01 13:25:53 +00:00
|
|
|
GObject * const call_obj = G_OBJECT (data->call);
|
2022-04-24 10:24:55 +00:00
|
|
|
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
ok = gom_resource_save_finish (resource, res, &error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
|
|
|
if (error)
|
|
|
|
g_warning ("Error saving call record to database: %s",
|
|
|
|
error->message);
|
|
|
|
else
|
|
|
|
g_warning ("Unknown error saving call record to database");
|
|
|
|
|
|
|
|
g_object_set_data (call_obj, "calls-call-record", NULL);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
g_debug ("Successfully saved new call record to database");
|
|
|
|
g_list_store_insert (G_LIST_STORE (data->self),
|
|
|
|
0,
|
|
|
|
CALLS_CALL_RECORD (resource));
|
|
|
|
|
|
|
|
g_object_set_data (call_obj, "calls-call-start", NULL);
|
|
|
|
}
|
2019-08-01 13:25:53 +00:00
|
|
|
|
|
|
|
g_object_unref (data->call);
|
|
|
|
g_object_unref (data->self);
|
|
|
|
g_free (data);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
record_call (CallsRecordStore *self,
|
2022-02-04 03:31:23 +00:00
|
|
|
CallsUiCallData *call)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
|
|
|
GObject * const call_obj = G_OBJECT (call);
|
|
|
|
GDateTime *start;
|
|
|
|
CallsCallRecord *record;
|
2019-08-01 13:25:53 +00:00
|
|
|
struct CallsRecordCallData *data;
|
2022-02-04 03:31:23 +00:00
|
|
|
gboolean inbound;
|
|
|
|
g_autofree char *protocol = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
g_assert (g_object_get_data (call_obj, "calls-call-record") == NULL);
|
|
|
|
|
|
|
|
start = g_object_get_data (call_obj, "calls-call-start");
|
|
|
|
g_assert (start != NULL);
|
|
|
|
|
2022-02-04 03:31:23 +00:00
|
|
|
g_object_get (call, "inbound", &inbound, "protocol", &protocol, NULL);
|
|
|
|
|
2019-07-22 10:52:46 +00:00
|
|
|
record = g_object_new (CALLS_TYPE_CALL_RECORD,
|
|
|
|
"repository", self->repository,
|
2022-02-04 03:31:23 +00:00
|
|
|
"target", cui_call_get_id (CUI_CALL (call)),
|
|
|
|
"inbound", inbound,
|
|
|
|
"protocol", protocol,
|
2019-07-22 10:52:46 +00:00
|
|
|
"start", start,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_set_data_full (call_obj, "calls-call-record",
|
|
|
|
record, g_object_unref);
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
data = g_new (struct CallsRecordCallData, 1);
|
|
|
|
g_object_ref (self);
|
|
|
|
g_object_ref (call);
|
|
|
|
data->self = self;
|
|
|
|
data->call = call;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
gom_resource_save_async (GOM_RESOURCE (record),
|
2022-04-24 10:24:55 +00:00
|
|
|
(GAsyncReadyCallback) record_call_save_cb,
|
2019-08-01 13:25:53 +00:00
|
|
|
data);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_cb (GomResource *resource,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer *unused)
|
|
|
|
{
|
2020-06-05 11:12:22 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2019-07-22 10:52:46 +00:00
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
ok = gom_resource_save_finish (resource, res, &error);
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!ok) {
|
|
|
|
if (error)
|
|
|
|
g_warning ("Error updating call record in database: %s",
|
|
|
|
error->message);
|
|
|
|
else
|
|
|
|
g_warning ("Unknown error updating call record in database");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
g_debug ("Successfully updated call record in database");
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-04-24 10:24:55 +00:00
|
|
|
stamp_call (CallsCallRecord *record,
|
|
|
|
const gchar *stamp_name)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
|
|
|
GObject *record_obj = G_OBJECT (record);
|
|
|
|
GDateTime *stamp = NULL;
|
|
|
|
|
|
|
|
/* Check the call has not already been stamped */
|
|
|
|
g_object_get (record_obj,
|
|
|
|
stamp_name, &stamp,
|
|
|
|
NULL);
|
|
|
|
if (stamp)
|
2021-06-02 18:39:23 +00:00
|
|
|
return;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
g_debug ("Stamping call `%s'", stamp_name);
|
2021-10-15 16:01:47 +00:00
|
|
|
stamp = g_date_time_new_now_utc ();
|
2019-07-22 10:52:46 +00:00
|
|
|
g_object_set (record_obj,
|
|
|
|
stamp_name, stamp,
|
|
|
|
NULL);
|
|
|
|
g_date_time_unref (stamp);
|
|
|
|
|
|
|
|
gom_resource_save_async (GOM_RESOURCE (record),
|
2022-04-24 10:24:55 +00:00
|
|
|
(GAsyncReadyCallback) update_cb,
|
2019-07-22 10:52:46 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_changed_cb (CallsRecordStore *self,
|
2022-02-04 03:31:23 +00:00
|
|
|
CuiCallState new_state,
|
|
|
|
CuiCallState old_state,
|
|
|
|
CallsUiCallData *call)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
|
|
|
GObject *call_obj = G_OBJECT (call);
|
|
|
|
CallsCallRecord *record =
|
|
|
|
g_object_get_data (call_obj, "calls-call-record");
|
|
|
|
CallsCallRecordState new_rec_state, old_rec_state;
|
|
|
|
|
2023-01-18 17:41:15 +00:00
|
|
|
g_debug ("Call state changed from %s (%d) to %s (%d)",
|
|
|
|
cui_call_state_to_string (old_state), old_state,
|
|
|
|
cui_call_state_to_string (new_state), new_state);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
/* Check whether the call is recorded */
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!record) {
|
|
|
|
/* Try to record the call again */
|
|
|
|
if (g_object_get_data (call_obj, "calls-call-start") != NULL)
|
|
|
|
record_call (self, call);
|
|
|
|
else
|
|
|
|
g_warning ("Record store received state change"
|
|
|
|
" for non-started call");
|
|
|
|
return;
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
|
|
|
new_rec_state = state_to_record_state (new_state);
|
|
|
|
old_rec_state = state_to_record_state (old_state);
|
|
|
|
|
|
|
|
if (new_rec_state == old_rec_state)
|
2021-06-02 18:39:23 +00:00
|
|
|
return;
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
switch (old_rec_state) {
|
|
|
|
case STARTED:
|
|
|
|
switch (new_rec_state) {
|
|
|
|
case ANSWERED:
|
|
|
|
stamp_call (record, "answered");
|
2019-07-22 10:52:46 +00:00
|
|
|
break;
|
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
case ENDED:
|
|
|
|
stamp_call (record, "end");
|
|
|
|
break;
|
|
|
|
|
2021-06-02 18:42:07 +00:00
|
|
|
case STARTED:
|
2021-06-02 18:39:23 +00:00
|
|
|
default:
|
2023-01-18 17:41:15 +00:00
|
|
|
goto critical_exit;
|
2021-06-02 18:39:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANSWERED:
|
|
|
|
switch (new_rec_state) {
|
|
|
|
case ENDED:
|
|
|
|
stamp_call (record, "end");
|
2019-07-22 10:52:46 +00:00
|
|
|
break;
|
|
|
|
|
2021-06-02 18:42:07 +00:00
|
|
|
case STARTED:
|
|
|
|
case ANSWERED:
|
2019-07-22 10:52:46 +00:00
|
|
|
default:
|
2023-01-18 17:41:15 +00:00
|
|
|
goto critical_exit;
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
2021-06-02 18:39:23 +00:00
|
|
|
break;
|
|
|
|
|
2021-06-02 18:42:07 +00:00
|
|
|
case ENDED:
|
2021-06-02 18:39:23 +00:00
|
|
|
default:
|
2023-01-18 17:41:15 +00:00
|
|
|
goto critical_exit;
|
2021-06-02 18:39:23 +00:00
|
|
|
}
|
2023-01-18 17:41:15 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
critical_exit:
|
|
|
|
/* XXX Modem's may be buggy; let's print as much information as possible */
|
|
|
|
g_critical ("Unexpected state change occurred!\n"
|
|
|
|
"From %s (%s) to %s (%s)",
|
|
|
|
cui_call_state_to_string (old_state), record_state_to_string (old_rec_state),
|
|
|
|
cui_call_state_to_string (new_state), record_state_to_string (new_rec_state));
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2020-03-18 12:58:13 +00:00
|
|
|
call_added_cb (CallsRecordStore *self,
|
2022-02-04 03:31:23 +00:00
|
|
|
CallsUiCallData *call)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
2020-03-18 12:58:13 +00:00
|
|
|
GObject * const call_obj = G_OBJECT (call);
|
|
|
|
GDateTime *start;
|
|
|
|
|
|
|
|
g_assert (g_object_get_data (call_obj, "calls-call-start") == NULL);
|
2021-10-15 16:01:47 +00:00
|
|
|
start = g_date_time_new_now_utc ();
|
2020-03-18 12:58:13 +00:00
|
|
|
g_object_set_data_full (call_obj, "calls-call-start",
|
2022-04-24 10:24:55 +00:00
|
|
|
start, (GDestroyNotify) g_date_time_unref);
|
2020-03-18 12:58:13 +00:00
|
|
|
|
2021-06-02 18:39:23 +00:00
|
|
|
if (!self->repository) {
|
|
|
|
open_repo (self);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-03-18 12:58:13 +00:00
|
|
|
record_call (self, call);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-03-18 12:58:13 +00:00
|
|
|
g_signal_connect_swapped (call,
|
|
|
|
"state-changed",
|
|
|
|
G_CALLBACK (state_changed_cb),
|
|
|
|
self);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2020-03-18 12:58:13 +00:00
|
|
|
call_removed_cb (CallsRecordStore *self,
|
2022-02-04 03:31:23 +00:00
|
|
|
CallsUiCallData *call,
|
2020-03-18 12:58:13 +00:00
|
|
|
const gchar *reason)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
2020-03-18 12:58:13 +00:00
|
|
|
/* Stamp the call as ended if it hasn't already been done */
|
|
|
|
CallsCallRecord *record =
|
|
|
|
g_object_get_data (G_OBJECT (call), "calls-call-record");
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-03-18 12:58:13 +00:00
|
|
|
if (record)
|
2021-06-02 18:39:23 +00:00
|
|
|
stamp_call (record, "end");
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-03-18 12:58:13 +00:00
|
|
|
g_signal_handlers_disconnect_by_data (call, self);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
constructed (GObject *object)
|
|
|
|
{
|
2020-03-18 12:58:13 +00:00
|
|
|
g_autoptr (GList) calls = NULL;
|
|
|
|
GList *c;
|
2019-07-22 10:52:46 +00:00
|
|
|
CallsRecordStore *self = CALLS_RECORD_STORE (object);
|
|
|
|
|
|
|
|
open_repo (self);
|
2020-03-18 12:58:13 +00:00
|
|
|
|
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
2022-02-04 03:31:23 +00:00
|
|
|
"ui-call-added",
|
2020-03-18 12:58:13 +00:00
|
|
|
G_CALLBACK (call_added_cb),
|
|
|
|
self);
|
|
|
|
|
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
2022-02-04 03:31:23 +00:00
|
|
|
"ui-call-removed",
|
2020-03-18 12:58:13 +00:00
|
|
|
G_CALLBACK (call_removed_cb),
|
|
|
|
self);
|
|
|
|
|
|
|
|
calls = calls_manager_get_calls (calls_manager_get_default ());
|
|
|
|
for (c = calls; c != NULL; c = c->next) {
|
|
|
|
call_added_cb (self, c->data);
|
|
|
|
}
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-02-18 15:01:22 +00:00
|
|
|
G_OBJECT_CLASS (calls_record_store_parent_class)->constructed (object);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dispose (GObject *object)
|
|
|
|
{
|
|
|
|
CallsRecordStore *self = CALLS_RECORD_STORE (object);
|
|
|
|
|
2019-08-01 13:25:53 +00:00
|
|
|
g_list_store_remove_all (G_LIST_STORE (self));
|
|
|
|
|
2019-07-22 10:52:46 +00:00
|
|
|
g_clear_object (&self->repository);
|
|
|
|
|
2020-02-18 15:01:22 +00:00
|
|
|
G_OBJECT_CLASS (calls_record_store_parent_class)->dispose (object);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
finalize (GObject *object)
|
|
|
|
{
|
|
|
|
CallsRecordStore *self = CALLS_RECORD_STORE (object);
|
|
|
|
|
|
|
|
g_free (self->filename);
|
2022-11-11 09:01:44 +00:00
|
|
|
close_adapter (self);
|
2019-07-22 10:52:46 +00:00
|
|
|
|
2020-02-18 15:01:22 +00:00
|
|
|
G_OBJECT_CLASS (calls_record_store_parent_class)->finalize (object);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_record_store_class_init (CallsRecordStoreClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructed = constructed;
|
|
|
|
object_class->dispose = dispose;
|
|
|
|
object_class->finalize = finalize;
|
2023-01-22 13:45:17 +00:00
|
|
|
|
|
|
|
signals[SIGNAL_DB_DONE] = g_signal_new ("db-done",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
|
|
|
NULL,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1,
|
|
|
|
G_TYPE_BOOLEAN);
|
2019-07-22 10:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_record_store_init (CallsRecordStore *self)
|
|
|
|
{
|
2023-01-17 14:13:55 +00:00
|
|
|
g_autofree char *old_dir = g_build_filename (g_get_user_data_dir (),
|
|
|
|
"calls",
|
|
|
|
NULL);
|
|
|
|
g_autofree char *new_dir = g_build_filename (g_get_user_data_dir (),
|
|
|
|
APP_DATA_NAME,
|
|
|
|
NULL);
|
2023-01-18 07:48:38 +00:00
|
|
|
const char *env_dir = g_getenv ("CALLS_RECORD_DIR");
|
2023-01-17 14:13:55 +00:00
|
|
|
char *used_dir = NULL;
|
|
|
|
gboolean exist_old;
|
|
|
|
gboolean exist_new;
|
|
|
|
gboolean new_is_dir;
|
2021-01-23 21:05:18 +00:00
|
|
|
|
|
|
|
exist_old = g_file_test (old_dir, G_FILE_TEST_EXISTS);
|
|
|
|
exist_new = g_file_test (new_dir, G_FILE_TEST_EXISTS);
|
|
|
|
new_is_dir = g_file_test (new_dir, G_FILE_TEST_IS_DIR);
|
|
|
|
|
2023-01-18 07:48:38 +00:00
|
|
|
if (env_dir) {
|
|
|
|
used_dir = (char *) env_dir;
|
|
|
|
} else if (exist_old && !exist_new) {
|
2021-01-23 21:05:18 +00:00
|
|
|
g_debug ("Trying to move database from `%s' to `%s'", old_dir, new_dir);
|
|
|
|
|
|
|
|
if (g_rename (old_dir, new_dir) == 0) {
|
|
|
|
used_dir = new_dir;
|
|
|
|
} else {
|
|
|
|
g_warning ("Moving folders to new location failed!");
|
|
|
|
g_debug ("Continuing to use old location");
|
|
|
|
used_dir = old_dir;
|
|
|
|
}
|
2023-01-17 14:13:55 +00:00
|
|
|
} else if (exist_new && new_is_dir) {
|
2021-01-23 21:05:18 +00:00
|
|
|
used_dir = new_dir;
|
2023-01-17 14:13:55 +00:00
|
|
|
} else {
|
2021-01-23 21:05:18 +00:00
|
|
|
used_dir = old_dir;
|
2023-01-17 14:13:55 +00:00
|
|
|
}
|
2021-01-23 21:05:18 +00:00
|
|
|
|
|
|
|
g_assert (used_dir);
|
|
|
|
|
|
|
|
self->filename = g_build_filename (used_dir,
|
2019-07-22 10:52:46 +00:00
|
|
|
RECORD_STORE_FILENAME,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CallsRecordStore *
|
2021-06-02 18:18:50 +00:00
|
|
|
calls_record_store_new (void)
|
2019-07-22 10:52:46 +00:00
|
|
|
{
|
|
|
|
return g_object_new (CALLS_TYPE_RECORD_STORE,
|
2019-08-01 13:25:53 +00:00
|
|
|
"item-type", CALLS_TYPE_CALL_RECORD,
|
2019-07-22 10:52:46 +00:00
|
|
|
NULL);
|
|
|
|
}
|