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