1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/myth-dbus/src/gmyth-dbus-server.c Wed Oct 24 20:01:02 2007 +0100
1.3 @@ -0,0 +1,1005 @@
1.4 +/**
1.5 + * GMyth Library
1.6 + *
1.7 + * @file gmyth/gmyth_backend_info.c
1.8 + *
1.9 + * @brief <p> This component represents all the MythTV backend server
1.10 + * configuration information.
1.11 + *
1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
1.13 + * @author Renato Filho <renato.filho@indt.org.br>
1.14 + *
1.15 + *
1.16 + * This program is free software; you can redistribute it and/or modify
1.17 + * it under the terms of the GNU Lesser General Public License as published by
1.18 + * the Free Software Foundation; either version 2 of the License, or
1.19 + * (at your option) any later version.
1.20 + *
1.21 + * This program is distributed in the hope that it will be useful,
1.22 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.23 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.24 + * GNU General Public License for more details.
1.25 + *
1.26 + * You should have received a copy of the GNU Lesser General Public License
1.27 + * along with this program; if not, write to the Free Software
1.28 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.29 + */
1.30 +
1.31 +
1.32 +#ifdef HAVE_CONFIG_H
1.33 +#include "config.h"
1.34 +#endif
1.35 +
1.36 +
1.37 +#include <gmyth/gmyth.h>
1.38 +#include <dbus/dbus-glib-bindings.h>
1.39 +
1.40 +
1.41 +#include "gmyth-dbus-common.h"
1.42 +#include "gmyth-dbus-server.h"
1.43 +
1.44 +#define MYTH_DEFAULT_DB "mythconverg"
1.45 +
1.46 +
1.47 +typedef struct _GMythDbusServerPrivate GMythDbusServerPrivate;
1.48 +
1.49 +struct _GMythDbusServerPrivate
1.50 +{
1.51 + GMythBackendInfo *myth_backend;
1.52 + GMythEPG *myth_epg;
1.53 + GMythScheduler *myth_scheduler;
1.54 +};
1.55 +
1.56 +#define GMYTH_DBUS_SERVER_GET_PRIVATE(o) \
1.57 + (G_TYPE_INSTANCE_GET_PRIVATE ((o), GMYTH_DBUS_SERVER_TYPE, GMythDbusServerPrivate))
1.58 +
1.59 +static void gmyth_dbus_server_class_init (GMythDbusServerClass *klass);
1.60 +static void gmyth_dbus_server_init (GMythDbusServer *self);
1.61 +static void gmyth_dbus_server_dispose (GObject *object);
1.62 +static void gmyth_dbus_server_finalize (GObject *object);
1.63 +
1.64 +/* Dbus */
1.65 +static gboolean gmyth_dbus_server_connect (GObject *obj,
1.66 + const gchar *host,
1.67 + guint port,
1.68 + const gchar *user,
1.69 + const gchar *password,
1.70 + gboolean *result,
1.71 + GError **error);
1.72 +static gboolean gmyth_dbus_server_get_channel_list (GObject *obj,
1.73 + GPtrArray **channels,
1.74 + GError **error);
1.75 +static gboolean gmyth_dbus_server_get_channel_info (GObject *obj,
1.76 + gint channel_id,
1.77 + GValue **info,
1.78 + GError **error);
1.79 +static gboolean gmyth_dbus_server_file_exists (GObject *obj,
1.80 + const gchar *file_name,
1.81 + gboolean *exists,
1.82 + GError **error);
1.83 +static gboolean gmyth_dbus_server_get_recorded_list (GObject *obj,
1.84 + GPtrArray **channels,
1.85 + GError **error);
1.86 +static gboolean gmyth_dbus_server_get_recorded_info (GObject *obj,
1.87 + const gchar *basename,
1.88 + GValue **info,
1.89 + GError **error);
1.90 +static gboolean gmyth_dbus_server_get_program_list (GObject *obj,
1.91 + gint channel_id,
1.92 + const gchar *start_time,
1.93 + const gchar *end_time,
1.94 + GPtrArray **program_list);
1.95 +static gboolean gmyth_dbus_server_get_schedule_list (GObject *obj,
1.96 + GPtrArray **schedule_list);
1.97 +
1.98 +static gboolean gmyth_dbus_server_connected (GObject *obj,
1.99 + gboolean *status,
1.100 + GError **error);
1.101 +static gboolean gmyth_dbus_server_disconnect (GObject *obj,
1.102 + GError **error);
1.103 +static gboolean gmyth_dbus_server_get_server_info (GObject *obj,
1.104 + guint64 *total_space,
1.105 + guint64 *used_space,
1.106 + guint64 *free_space,
1.107 + GError **error);
1.108 +static gboolean gmyth_dbus_server_get_thumbnail (GObject *obj,
1.109 + const gchar *uri,
1.110 + GByteArray **image,
1.111 + GError **error);
1.112 +static gboolean gmyth_dbus_server_get_channel_icon (GObject *obj,
1.113 + guint channel_id,
1.114 + GByteArray **icon,
1.115 + GError **error);
1.116 +static gboolean gmyth_dbus_server_stop_recording (GObject *obj,
1.117 + guint channel_id,
1.118 + gboolean *result,
1.119 + GError **error);
1.120 +static gboolean gmyth_dbus_server_add_schedule (GObject *obj,
1.121 + guint channel_id,
1.122 + guint program_id,
1.123 + const gchar *start_time,
1.124 + const gchar *end_time,
1.125 + gboolean recurring,
1.126 + const gchar *description,
1.127 + guint *schedule_id,
1.128 + GError **error);
1.129 +static gboolean gmyth_dbus_server_add_exception (GObject *obj,
1.130 + guint schedule_id,
1.131 + guint channel_id,
1.132 + guint program_id,
1.133 + const gchar *start_time,
1.134 + const gchar *end_time,
1.135 + const gchar *description,
1.136 + GError **error);
1.137 +static gboolean gmyth_dbus_server_remove_schedule (GObject *obj,
1.138 + guint schedule_id,
1.139 + GError **error);
1.140 +
1.141 +
1.142 +#include "gmyth-dbus-server-glue.h"
1.143 +
1.144 +
1.145 +G_DEFINE_TYPE (GMythDbusServer, gmyth_dbus_server, G_TYPE_OBJECT);
1.146 +
1.147 +static void
1.148 +gmyth_dbus_server_class_init (GMythDbusServerClass *klass)
1.149 +{
1.150 + GObjectClass *object_class = G_OBJECT_CLASS (klass);
1.151 +
1.152 + g_type_class_add_private (klass, sizeof (GMythDbusServerPrivate));
1.153 +
1.154 + object_class->dispose = gmyth_dbus_server_dispose;
1.155 + object_class->finalize = gmyth_dbus_server_finalize;
1.156 +
1.157 + dbus_g_object_type_install_info (GMYTH_DBUS_SERVER_TYPE,
1.158 + &dbus_glib_gmyth_dbus_server_object_info);
1.159 +}
1.160 +
1.161 +static void
1.162 +gmyth_dbus_server_init (GMythDbusServer *self)
1.163 +{
1.164 +}
1.165 +
1.166 +static void
1.167 +gmyth_dbus_server_dispose (GObject *object)
1.168 +{
1.169 + G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->dispose (object);
1.170 +}
1.171 +
1.172 +static void
1.173 +gmyth_dbus_server_finalize (GObject *object)
1.174 +{
1.175 + G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->finalize (object);
1.176 +}
1.177 +
1.178 +static gboolean
1.179 +gmyth_dbus_server_connect_epg (GMythDbusServer *server)
1.180 +{
1.181 + GMythDbusServerPrivate *priv;
1.182 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
1.183 +
1.184 + if (!priv->myth_epg)
1.185 + {
1.186 + priv->myth_epg = gmyth_epg_new();
1.187 + if (!gmyth_epg_connect (priv->myth_epg, priv->myth_backend))
1.188 + {
1.189 + g_object_unref (priv->myth_epg);
1.190 + priv->myth_epg = NULL;
1.191 + return FALSE;
1.192 + }
1.193 + }
1.194 +
1.195 + return TRUE;
1.196 +}
1.197 +
1.198 +static gboolean
1.199 +gmyth_dbus_server_connect_scheduler (GMythDbusServer *server)
1.200 +{
1.201 + GMythDbusServerPrivate *priv;
1.202 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
1.203 +
1.204 + if (!priv->myth_scheduler)
1.205 + {
1.206 + priv->myth_scheduler = gmyth_scheduler_new ();
1.207 + if (!gmyth_scheduler_connect (priv->myth_scheduler,
1.208 + priv->myth_backend))
1.209 + {
1.210 + g_object_unref (priv->myth_scheduler);
1.211 + priv->myth_scheduler = NULL;
1.212 + return FALSE;
1.213 + }
1.214 + }
1.215 +
1.216 + return TRUE;
1.217 +}
1.218 +
1.219 +static gboolean
1.220 +gmyth_dbus_server_connect (GObject *obj,
1.221 + const gchar *host,
1.222 + guint port,
1.223 + const gchar *user,
1.224 + const gchar *password,
1.225 + gboolean *result,
1.226 + GError **error)
1.227 +{
1.228 + GMythDbusServerPrivate *priv;
1.229 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.230 +
1.231 + if (priv->myth_backend)
1.232 + return TRUE;
1.233 +
1.234 + priv->myth_backend = gmyth_backend_info_new_full (host,
1.235 + user,
1.236 + password,
1.237 + MYTH_DEFAULT_DB,
1.238 + port);
1.239 + *result = TRUE;
1.240 + return TRUE;
1.241 +}
1.242 +
1.243 +static gboolean
1.244 +gmyth_dbus_server_connected (GObject *obj,
1.245 + gboolean *status,
1.246 + GError **error)
1.247 +{
1.248 + GMythDbusServerPrivate *priv;
1.249 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.250 +
1.251 + if (priv->myth_backend)
1.252 + *status = TRUE;
1.253 + else
1.254 + *status = FALSE;
1.255 + return TRUE;
1.256 +}
1.257 +
1.258 +static gboolean
1.259 +gmyth_dbus_server_disconnect (GObject *obj,
1.260 + GError **error)
1.261 +{
1.262 + GMythDbusServerPrivate *priv;
1.263 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.264 +
1.265 + if (priv->myth_epg)
1.266 + {
1.267 + g_object_unref (priv->myth_epg);
1.268 + priv->myth_epg = NULL;
1.269 + }
1.270 +
1.271 +
1.272 + if (priv->myth_backend)
1.273 + {
1.274 + g_object_unref (priv->myth_backend);
1.275 + priv->myth_backend = NULL;
1.276 + }
1.277 +
1.278 + if (priv->myth_scheduler)
1.279 + {
1.280 + g_object_unref (priv->myth_scheduler);
1.281 + priv->myth_scheduler = NULL;
1.282 + }
1.283 +
1.284 +
1.285 + return TRUE;
1.286 +}
1.287 +
1.288 +static gboolean
1.289 +gmyth_dbus_server_get_server_info (GObject *obj,
1.290 + guint64 *total_space,
1.291 + guint64 *used_space,
1.292 + guint64 *free_space,
1.293 + GError **error)
1.294 +{
1.295 + GMythBackendDetails *details;
1.296 + GMythDbusServerPrivate *priv;
1.297 + gboolean ret = FALSE;
1.298 + GMythSocket *socket;
1.299 +
1.300 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.301 +
1.302 + g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
1.303 +
1.304 + socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
1.305 +
1.306 + details = NULL;
1.307 + gmyth_util_get_backend_details (socket,
1.308 + &details);
1.309 + if (details)
1.310 + {
1.311 + *total_space = details->total_space;
1.312 + *used_space = details->used_space;
1.313 + *free_space = *total_space - *used_space;
1.314 + gmyth_util_backend_details_free (details);
1.315 +
1.316 + ret = TRUE;
1.317 + }
1.318 +
1.319 + g_object_unref (socket);
1.320 +
1.321 + return ret;
1.322 +}
1.323 +
1.324 +
1.325 +static void
1.326 +gmyth_dbus_server_parse_channel_info (GMythChannelInfo *info,
1.327 + GValue *val)
1.328 +{
1.329 + dbus_g_type_struct_set (val,
1.330 + 0, info->channel_ID,
1.331 + 1, info->channel_num->str,
1.332 + 2, info->channel_name->str,
1.333 + 3, info->channel_icon->str,
1.334 + G_MAXUINT);
1.335 +}
1.336 +
1.337 +static gboolean
1.338 +gmyth_dbus_server_get_channel_info (GObject *obj,
1.339 + gint channel_id,
1.340 + GValue **info,
1.341 + GError **error)
1.342 +{
1.343 + GType ch_type;
1.344 + GMythChannelInfo *ch_info;
1.345 + GMythDbusServerPrivate *priv;
1.346 +
1.347 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.348 +
1.349 + g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
1.350 + g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
1.351 +
1.352 + ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
1.353 +
1.354 + ch_info = gmyth_epg_get_channel_info (priv->myth_epg, channel_id);
1.355 + if (ch_info)
1.356 + {
1.357 + *info = g_new0 (GValue, 1);
1.358 + g_value_init (*info, ch_type);
1.359 + g_value_set_static_boxed (*info, dbus_g_type_specialized_construct (ch_type));
1.360 + gmyth_dbus_server_parse_channel_info (ch_info, *info);
1.361 + return TRUE;
1.362 + }
1.363 +
1.364 + return FALSE;
1.365 +}
1.366 +
1.367 +
1.368 +static gboolean
1.369 +gmyth_dbus_server_get_channel_list (GObject *obj,
1.370 + GPtrArray **channels,
1.371 + GError **error)
1.372 +{
1.373 + GList *lst;
1.374 + GList *walk;
1.375 + int len;
1.376 + GType ch_type;
1.377 + GMythDbusServerPrivate *priv;
1.378 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.379 +
1.380 + g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
1.381 + g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
1.382 +
1.383 +
1.384 + len = gmyth_epg_get_channel_list (priv->myth_epg, &lst);
1.385 +
1.386 + *channels = g_ptr_array_sized_new (len);
1.387 + ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
1.388 +
1.389 + for (walk = lst; walk != NULL; walk = walk->next)
1.390 + {
1.391 + GValue ch = { 0, };
1.392 + GMythChannelInfo *data;
1.393 +
1.394 + data = (GMythChannelInfo *) walk->data;
1.395 +
1.396 + g_value_init (&ch, ch_type);
1.397 + g_value_set_static_boxed (&ch, dbus_g_type_specialized_construct (ch_type));
1.398 + gmyth_dbus_server_parse_channel_info (data, &ch);
1.399 + g_ptr_array_add (*channels, g_value_get_boxed (&ch));
1.400 + }
1.401 +
1.402 + gmyth_free_channel_list (lst);
1.403 + return TRUE;
1.404 +}
1.405 +
1.406 +static gboolean
1.407 +gmyth_dbus_server_file_exists (GObject *obj,
1.408 + const gchar *file_name,
1.409 + gboolean *exists,
1.410 + GError **error)
1.411 +{
1.412 + GMythDbusServerPrivate *priv;
1.413 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.414 +
1.415 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.416 +
1.417 + *exists = gmyth_util_file_exists (priv->myth_backend, file_name);
1.418 +
1.419 + return TRUE;
1.420 +}
1.421 +
1.422 +static gboolean
1.423 +gmyth_dbus_server_get_program_list (GObject *obj,
1.424 + gint program_id,
1.425 + const gchar *start_time,
1.426 + const gchar *end_time,
1.427 + GPtrArray **programs)
1.428 +{
1.429 + GList *list;
1.430 + GList *walk;
1.431 + gint len;
1.432 + GType program_type;
1.433 + GTimeVal start_time_val;
1.434 + GTimeVal end_time_val;
1.435 + GMythDbusServerPrivate *priv;
1.436 +
1.437 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.438 +
1.439 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.440 + g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
1.441 +
1.442 +
1.443 + g_time_val_from_iso8601 (start_time, &start_time_val);
1.444 + g_time_val_from_iso8601 (end_time, &end_time_val);
1.445 +
1.446 + len = gmyth_epg_get_program_list (priv->myth_epg,
1.447 + &list,
1.448 + program_id,
1.449 + &start_time_val,
1.450 + &end_time_val);
1.451 +
1.452 + *programs = g_ptr_array_sized_new (len);
1.453 + program_type = GMYTH_DBUS_PROGRAM_G_TYPE;
1.454 +
1.455 + for (walk = list; walk != NULL; walk = walk->next)
1.456 + {
1.457 + GValue program = { 0, };
1.458 + gchar *start_str;
1.459 + gchar *end_str;
1.460 + GMythProgramInfo *data;
1.461 +
1.462 + data = (GMythProgramInfo *) walk->data;
1.463 +
1.464 + g_value_init (&program, program_type);
1.465 + g_value_set_static_boxed (&program,
1.466 + dbus_g_type_specialized_construct (program_type));
1.467 +
1.468 + start_str = g_time_val_to_iso8601 (data->startts);
1.469 + end_str = g_time_val_to_iso8601 (data->endts);
1.470 +
1.471 + dbus_g_type_struct_set (&program,
1.472 + 0, data->chanid,
1.473 + 1, start_str,
1.474 + 2, end_str,
1.475 + 3, data->title->str,
1.476 + 4, data->subtitle->str,
1.477 + 5, data->description->str,
1.478 + 6, data->category->str,
1.479 + G_MAXUINT);
1.480 +
1.481 + g_ptr_array_add (*programs, g_value_get_boxed (&program));
1.482 + g_free (start_str);
1.483 + g_free (end_str);
1.484 + }
1.485 +
1.486 + gmyth_free_program_list (list);
1.487 +
1.488 + return TRUE;
1.489 +}
1.490 +
1.491 +static void
1.492 +gmyth_dbus_server_parse_recorded_info (RecordedInfo *info,
1.493 + GValue *val)
1.494 +{
1.495 + gchar *start_str;
1.496 + gchar *end_str;
1.497 +
1.498 + start_str = g_time_val_to_iso8601 (info->start_time);
1.499 + end_str = g_time_val_to_iso8601 (info->end_time);
1.500 + dbus_g_type_struct_set (val,
1.501 + 0, info->record_id,
1.502 + 1, info->program_id,
1.503 + 2, info->channel_id,
1.504 + 3, start_str,
1.505 + 4, end_str,
1.506 + 5, info->title->str,
1.507 + 6, info->subtitle->str,
1.508 + 7, info->description->str,
1.509 + 8, info->category->str,
1.510 + 9, info->basename->str,
1.511 + 10, info->filesize,
1.512 + G_MAXUINT);
1.513 +
1.514 + g_free (start_str);
1.515 + g_free (end_str);
1.516 +}
1.517 +
1.518 +static gboolean
1.519 +gmyth_dbus_server_get_recorded_info (GObject *obj,
1.520 + const gchar *basename,
1.521 + GValue **info,
1.522 + GError **error)
1.523 +{
1.524 + GType record_type;
1.525 + GMythDbusServerPrivate *priv;
1.526 + RecordedInfo *record_info;
1.527 +
1.528 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.529 +
1.530 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.531 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.532 +
1.533 + record_type = GMYTH_DBUS_RECORD_G_TYPE;
1.534 +
1.535 + record_info = gmyth_scheduler_get_recorded_info (priv->myth_scheduler,
1.536 + basename);
1.537 +
1.538 + if (record_info)
1.539 + {
1.540 + *info = g_new0 (GValue, 1);
1.541 + g_value_init (*info, record_type);
1.542 + g_value_set_static_boxed (*info,
1.543 + dbus_g_type_specialized_construct (record_type));
1.544 + gmyth_dbus_server_parse_recorded_info (record_info, *info);
1.545 +
1.546 + gmyth_recorded_info_free (record_info);
1.547 +
1.548 + return TRUE;
1.549 + }
1.550 +
1.551 + return FALSE;
1.552 +}
1.553 +
1.554 +
1.555 +static gboolean
1.556 +gmyth_dbus_server_get_recorded_list (GObject *obj,
1.557 + GPtrArray **records,
1.558 + GError **error)
1.559 +{
1.560 + GList *list;
1.561 + GList *walk;
1.562 + gint len;
1.563 + GType record_type;
1.564 + GMythDbusServerPrivate *priv;
1.565 +
1.566 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.567 +
1.568 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.569 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.570 +
1.571 +
1.572 + len = gmyth_scheduler_get_recorded_list (priv->myth_scheduler,
1.573 + &list);
1.574 +
1.575 + *records = g_ptr_array_sized_new (len);
1.576 + record_type = GMYTH_DBUS_RECORD_G_TYPE;
1.577 +
1.578 + for (walk = list; walk != NULL; walk = walk->next)
1.579 + {
1.580 + GValue record = { 0, };
1.581 + RecordedInfo *data;
1.582 +
1.583 + data = (RecordedInfo *) walk->data;
1.584 +
1.585 + g_value_init (&record, record_type);
1.586 + g_value_set_static_boxed (&record,
1.587 + dbus_g_type_specialized_construct (record_type));
1.588 + gmyth_dbus_server_parse_recorded_info (data, &record);
1.589 + g_ptr_array_add (*records, g_value_get_boxed (&record));
1.590 + }
1.591 +
1.592 + gmyth_recorded_info_list_free (list);
1.593 +
1.594 + return TRUE;
1.595 +
1.596 +}
1.597 +
1.598 +static gboolean
1.599 +gmyth_dbus_server_get_schedule_list (GObject *obj,
1.600 + GPtrArray **schedules)
1.601 +{
1.602 + GList *list;
1.603 + GList *walk;
1.604 + gint len;
1.605 + GType schedule_type;
1.606 + GMythDbusServerPrivate *priv;
1.607 +
1.608 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.609 +
1.610 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.611 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.612 +
1.613 +
1.614 + len = gmyth_scheduler_get_schedule_list (priv->myth_scheduler,
1.615 + &list);
1.616 +
1.617 + *schedules = g_ptr_array_sized_new (len);
1.618 + schedule_type = GMYTH_DBUS_SCHEDULE_G_TYPE;
1.619 +
1.620 + for (walk = list; walk != NULL; walk = walk->next)
1.621 + {
1.622 + GValue schedule = { 0, };
1.623 + ScheduleInfo *data;
1.624 + gchar *start_str_time;
1.625 + gchar *end_str_time;
1.626 +
1.627 + data = (ScheduleInfo *) walk->data;
1.628 +
1.629 + g_value_init (&schedule, schedule_type);
1.630 + g_value_set_static_boxed (&schedule,
1.631 + dbus_g_type_specialized_construct (schedule_type));
1.632 +
1.633 + start_str_time = g_time_val_to_iso8601 (data->start_time);
1.634 + end_str_time = g_time_val_to_iso8601 (data->end_time);
1.635 +
1.636 + dbus_g_type_struct_set (&schedule,
1.637 + 0, data->schedule_id,
1.638 + 1, data->program_id,
1.639 + 2, data->channel_id,
1.640 + 3, start_str_time,
1.641 + 4, end_str_time,
1.642 + 5, data->title->str,
1.643 + 6, data->subtitle->str,
1.644 + 7, data->description->str,
1.645 + 8, data->category->str,
1.646 + 9, data->type,
1.647 + G_MAXUINT);
1.648 +
1.649 + g_ptr_array_add (*schedules, g_value_get_boxed (&schedule));
1.650 +
1.651 + g_free (start_str_time);
1.652 + g_free (end_str_time);
1.653 + }
1.654 +
1.655 + gmyth_schedule_info_list_free (list);
1.656 +
1.657 + return TRUE;
1.658 +}
1.659 +
1.660 +
1.661 +static gboolean
1.662 +gmyth_dbus_server_get_thumbnail (GObject *obj,
1.663 + const gchar *uri,
1.664 + GByteArray **image,
1.665 + GError **error)
1.666 +{
1.667 + GMythFileTransfer *file_transfer;
1.668 + glong filesize;
1.669 + GMythFileReadResult result;
1.670 + GMythDbusServerPrivate *priv;
1.671 +
1.672 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.673 +
1.674 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.675 +
1.676 + if (!gmyth_util_file_exists (priv->myth_backend, uri))
1.677 + goto fail;
1.678 +
1.679 + file_transfer = gmyth_file_transfer_new (priv->myth_backend);
1.680 +
1.681 + if (!gmyth_file_transfer_open (file_transfer, uri))
1.682 + goto fail;
1.683 +
1.684 + filesize = gmyth_file_transfer_get_filesize (file_transfer);
1.685 + if (filesize <= 0)
1.686 + goto fail;
1.687 +
1.688 + *image = g_byte_array_new ();
1.689 + result = gmyth_file_transfer_read (file_transfer, *image, filesize, FALSE);
1.690 + if (result == GMYTH_FILE_READ_ERROR)
1.691 + goto fail;
1.692 +
1.693 + gmyth_file_transfer_close (file_transfer);
1.694 + g_object_unref (file_transfer);
1.695 +
1.696 + if (filesize > (*image)->len)
1.697 + goto fail;
1.698 +
1.699 + return TRUE;
1.700 +
1.701 +fail:
1.702 + g_object_unref(file_transfer);
1.703 + return FALSE;
1.704 +}
1.705 +
1.706 +static gboolean
1.707 +gmyth_dbus_server_get_channel_icon (GObject *obj,
1.708 + guint channel_id,
1.709 + GByteArray **icon,
1.710 + GError **error)
1.711 +{
1.712 + GMythChannelInfo *channel = NULL;
1.713 + guint8 *icon_data;
1.714 + guint icon_length;
1.715 + GMythDbusServerPrivate *priv;
1.716 +
1.717 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.718 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.719 +
1.720 + channel = gmyth_epg_get_channel_info (priv->myth_epg,
1.721 + (gint) channel_id);
1.722 +
1.723 + *icon = NULL;
1.724 +
1.725 + if (channel == NULL)
1.726 + return FALSE;
1.727 +
1.728 + if (!gmyth_epg_channel_has_icon(priv->myth_epg, channel))
1.729 + {
1.730 + gmyth_channel_info_free (channel);
1.731 + g_debug("Channel does not have icon available");
1.732 + return FALSE;
1.733 + }
1.734 +
1.735 + icon_data = NULL;
1.736 + icon_length = 0;
1.737 + if (!gmyth_epg_channel_get_icon (priv->myth_epg,
1.738 + channel,
1.739 + &icon_data,
1.740 + &icon_length))
1.741 + {
1.742 + gmyth_channel_info_free (channel);
1.743 + g_warning("Could not get channel icon for channel id = %u", channel_id);
1.744 + return FALSE;
1.745 + }
1.746 +
1.747 + *icon = g_byte_array_sized_new (icon_length);
1.748 + *icon = g_byte_array_append (*icon, icon_data, icon_length);
1.749 +
1.750 + g_free (icon_data);
1.751 + gmyth_channel_info_free(channel);
1.752 + return TRUE;
1.753 +}
1.754 +
1.755 +
1.756 +static gboolean
1.757 +gmyth_dbus_server_stop_recording (GObject *obj,
1.758 + guint channel_id,
1.759 + gboolean *result,
1.760 + GError **error)
1.761 +{
1.762 + gboolean ret = FALSE;
1.763 + GMythDbusServerPrivate *priv;
1.764 +
1.765 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.766 +
1.767 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.768 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.769 +
1.770 + ret = gmyth_scheduler_stop_recording (priv->myth_scheduler,
1.771 + channel_id);
1.772 +
1.773 + return ret;
1.774 +}
1.775 +
1.776 +static ScheduleInfo*
1.777 +gmyth_dbus_server_new_schedule_info (const gchar* description,
1.778 + guint channel_id,
1.779 + guint program_id,
1.780 + GTimeVal *start_vtime,
1.781 + GTimeVal *end_vtime)
1.782 +{
1.783 + ScheduleInfo *new_sched_info;
1.784 +
1.785 + new_sched_info = g_new0(ScheduleInfo, 1);
1.786 +
1.787 + /* record_id == -1 for generating a new id */
1.788 + new_sched_info->schedule_id = -1;
1.789 +
1.790 + new_sched_info->channel_id = channel_id;
1.791 + new_sched_info->program_id = program_id;
1.792 + new_sched_info->start_time = g_new0 (GTimeVal, 1);
1.793 + *new_sched_info->start_time = *start_vtime;
1.794 + new_sched_info->end_time = g_new0 (GTimeVal, 1);
1.795 + *new_sched_info->end_time = *end_vtime;
1.796 +
1.797 + /* TODO: there is no frequency field */
1.798 + /*new_sched_info->frequency = -1;*/
1.799 +
1.800 + if (description != NULL) {
1.801 + /* FIXME: description parameter is used as title and description */
1.802 + new_sched_info->title = g_string_new(description);
1.803 + new_sched_info->description = g_string_new(description);
1.804 + }
1.805 +
1.806 + return new_sched_info;
1.807 +}
1.808 +
1.809 +static gboolean
1.810 +gmyth_dbus_server_add_schedule (GObject *obj,
1.811 + guint channel_id,
1.812 + guint program_id,
1.813 + const gchar *start_time,
1.814 + const gchar *end_time,
1.815 + gboolean recurring,
1.816 + const gchar *description,
1.817 + guint *schedule_id,
1.818 + GError **error)
1.819 +{
1.820 + ScheduleInfo *sch_info;
1.821 + GTimeVal start_vtime;
1.822 + GTimeVal end_vtime;
1.823 + GMythDbusServerPrivate *priv;
1.824 +
1.825 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.826 +
1.827 + *schedule_id = 0;
1.828 +
1.829 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.830 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.831 +
1.832 +
1.833 + g_time_val_from_iso8601 (start_time, &start_vtime);
1.834 + g_time_val_from_iso8601 (end_time, &end_vtime);
1.835 + sch_info = gmyth_dbus_server_new_schedule_info (description,
1.836 + channel_id,
1.837 + program_id,
1.838 + &start_vtime,
1.839 + &end_vtime);
1.840 + if (sch_info != NULL) {
1.841 + GMythScheduleType type;
1.842 + GTimeVal t_now;
1.843 + gboolean has_record;
1.844 +
1.845 + type = (recurring ?
1.846 + GMYTH_SCHEDULE_ALL_OCCURRENCES :
1.847 + GMYTH_SCHEDULE_ONE_OCCURRENCE);
1.848 +
1.849 + g_get_current_time (&t_now);
1.850 +
1.851 + has_record = gmyth_scheduler_was_recorded_before (priv->myth_scheduler,
1.852 + channel_id,
1.853 + (time_t) start_vtime.tv_sec);
1.854 +
1.855 +
1.856 + if ((t_now.tv_sec >= start_vtime.tv_sec)
1.857 + && (t_now.tv_sec <= end_vtime.tv_sec) && has_record)
1.858 + {
1.859 + GMythSocket *socket;
1.860 + gboolean res = FALSE;
1.861 +
1.862 + socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
1.863 + res = gmyth_scheduler_reactivate_schedule(priv->myth_scheduler,
1.864 + channel_id,
1.865 + (time_t) start_vtime.tv_sec);
1.866 + if (res) {
1.867 + GMythStringList *slist = gmyth_string_list_new();
1.868 +
1.869 + gmyth_string_list_append_char_array(slist, "RESCHEDULE_RECORDINGS 0");
1.870 + gmyth_socket_sendreceive_stringlist(socket, slist);
1.871 + res = (gmyth_string_list_get_int(slist, 0) == 1);
1.872 + g_object_unref(slist);
1.873 + }
1.874 +
1.875 + g_object_unref(socket);
1.876 + return res;
1.877 + }
1.878 + else
1.879 + {
1.880 + if (!gmyth_scheduler_add_schedule_full (priv->myth_scheduler,
1.881 + sch_info,
1.882 + type))
1.883 + {
1.884 + g_warning("Could not add schedule entry");
1.885 + return FALSE;
1.886 + }
1.887 +
1.888 + (*schedule_id) = sch_info->schedule_id;
1.889 + gmyth_schedule_info_free (sch_info);
1.890 + return TRUE;
1.891 + }
1.892 + }
1.893 + return FALSE;
1.894 +}
1.895 +
1.896 +static gboolean
1.897 +gmyth_dbus_server_add_exception (GObject *obj,
1.898 + guint schedule_id,
1.899 + guint channel_id,
1.900 + guint program_id,
1.901 + const gchar *start_time,
1.902 + const gchar *end_time,
1.903 + const gchar *description,
1.904 + GError **error)
1.905 +{
1.906 + ScheduleInfo *sch_info;
1.907 + GTimeVal start_vtime;
1.908 + GTimeVal end_vtime;
1.909 + GMythDbusServerPrivate *priv;
1.910 +
1.911 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.912 +
1.913 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.914 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.915 +
1.916 + g_time_val_from_iso8601 (start_time, &start_vtime);
1.917 + g_time_val_from_iso8601 (end_time, &end_vtime);
1.918 +
1.919 + sch_info = gmyth_dbus_server_new_schedule_info (description,
1.920 + channel_id,
1.921 + program_id,
1.922 + &start_vtime,
1.923 + &end_vtime);
1.924 + if (sch_info != NULL)
1.925 + {
1.926 + if (!gmyth_scheduler_add_exception (priv->myth_scheduler,
1.927 + schedule_id,
1.928 + sch_info))
1.929 + {
1.930 + g_warning ("Could not add schedule exception");
1.931 + gmyth_schedule_info_free (sch_info);
1.932 + return FALSE;
1.933 + }
1.934 +
1.935 + gmyth_schedule_info_free (sch_info);
1.936 + return TRUE;
1.937 + }
1.938 + return FALSE;
1.939 +}
1.940 +
1.941 +static gboolean
1.942 +gmyth_dbus_server_remove_schedule (GObject *obj,
1.943 + guint schedule_id,
1.944 + GError **error)
1.945 +{
1.946 + GMythDbusServerPrivate *priv;
1.947 +
1.948 + priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
1.949 +
1.950 + g_return_val_if_fail (priv->myth_backend, FALSE);
1.951 + g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
1.952 +
1.953 + return gmyth_scheduler_delete_schedule (priv->myth_scheduler, schedule_id);
1.954 +}
1.955 +
1.956 +GMythDbusServer*
1.957 +gmyth_dbus_server_start_dbus_service (void)
1.958 +{
1.959 + GError *error = NULL;
1.960 + DBusGProxy *proxy;
1.961 + DBusGConnection *bus;
1.962 + guint request_ret;
1.963 + GMythDbusServer *self;
1.964 +
1.965 + self = g_object_new (GMYTH_DBUS_SERVER_TYPE, NULL);
1.966 + g_return_val_if_fail (self, FALSE);
1.967 +
1.968 + /* TODO: should verify if this service was already started */
1.969 +
1.970 + /* connect to session bus */
1.971 + bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
1.972 + if (bus == NULL)
1.973 + {
1.974 + g_warning ("Could not connect to dbus: %s", error->message);
1.975 + g_error_free (error);
1.976 + goto fail;
1.977 + }
1.978 +
1.979 + /* register dbus object */
1.980 + dbus_g_connection_register_g_object (bus,
1.981 + GMYTH_DBUS_SERVER_PATH, G_OBJECT (self));
1.982 +
1.983 + proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS,
1.984 + DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
1.985 +
1.986 + /* registering download manager service */
1.987 + if (!org_freedesktop_DBus_request_name (proxy, GMYTH_DBUS_SERVER_IFACE,
1.988 + 0, &request_ret, &error))
1.989 + {
1.990 + g_warning ("Unable to register dbus service: %d %s",
1.991 + error->code, error->message);
1.992 + g_error_free (error);
1.993 + goto fail;
1.994 + }
1.995 +
1.996 + if (request_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
1.997 + {
1.998 + g_warning ("Got result code %u from requesting name", request_ret);
1.999 + goto fail;
1.1000 + }
1.1001 +
1.1002 + return self;
1.1003 +
1.1004 +fail:
1.1005 + g_object_unref (self);
1.1006 + return NULL;
1.1007 +}
1.1008 +