myth-dbus/src/gmyth-dbus-server.c
author renatofilho
Wed Oct 24 23:47:06 2007 +0100 (2007-10-24)
branchtrunk
changeset 866 640fc18c9f37
parent 865 3eeb6b565018
child 867 c6c57f6060bd
permissions -rw-r--r--
[svn r872] fixed valuearray types
     1 /**
     2  * GMyth Library
     3  *
     4  * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
     5  * @author Renato Filho  <renato.filho@indt.org.br>
     6  *
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU Lesser General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU Lesser General Public License
    19  * along with this program; if not, write to the Free Software
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21  */
    22 
    23 
    24 #ifdef HAVE_CONFIG_H
    25 #include "config.h"
    26 #endif
    27 
    28 
    29 #include <gmyth/gmyth.h>
    30 #include <dbus/dbus-glib-bindings.h>
    31 
    32 
    33 #include "gmyth-dbus-common.h"
    34 #include "gmyth-dbus-server.h"
    35 
    36 #define MYTH_DEFAULT_DB             "mythconverg"
    37 
    38 typedef struct _GMythDbusServerPrivate GMythDbusServerPrivate;
    39 
    40 struct _GMythDbusServerPrivate
    41 {
    42     GMythBackendInfo *myth_backend;
    43     GMythEPG         *myth_epg;
    44     GMythScheduler   *myth_scheduler;
    45 };
    46 
    47 #define GMYTH_DBUS_SERVER_GET_PRIVATE(o) \
    48     (G_TYPE_INSTANCE_GET_PRIVATE ((o), GMYTH_DBUS_SERVER_TYPE, GMythDbusServerPrivate))
    49 
    50 static void gmyth_dbus_server_class_init (GMythDbusServerClass *klass);
    51 static void gmyth_dbus_server_init       (GMythDbusServer *self);
    52 static void gmyth_dbus_server_dispose    (GObject *object);
    53 static void gmyth_dbus_server_finalize   (GObject *object);
    54 
    55 /* Dbus */
    56 static gboolean gmyth_dbus_server_connect           (GObject *obj,
    57                                                      const gchar *host,
    58                                                      guint port,
    59                                                      const gchar *user,
    60                                                      const gchar *password,
    61                                                      gboolean *result,
    62                                                      GError **error);
    63 static gboolean gmyth_dbus_server_get_channel_list  (GObject *obj,
    64                                                      GPtrArray **channels,
    65                                                      GError **error);
    66 static gboolean gmyth_dbus_server_get_channel_info  (GObject *obj,
    67                                                      gint channel_id,
    68                                                      GValueArray **info,
    69                                                      GError **error);
    70 static gboolean gmyth_dbus_server_file_exists       (GObject *obj,
    71                                                      const gchar *file_name,
    72                                                      gboolean *exists,
    73                                                      GError **error);
    74 static gboolean gmyth_dbus_server_get_recorded_list (GObject *obj,
    75                                                      GPtrArray **channels,
    76                                                      GError **error);
    77 static gboolean gmyth_dbus_server_get_recorded_info (GObject *obj,
    78                                                      const gchar *basename,
    79                                                      GValueArray **info,
    80                                                      GError **error);
    81 static gboolean gmyth_dbus_server_get_program_list  (GObject *obj,
    82                                                      gint channel_id,
    83                                                      const gchar *start_time,
    84                                                      const gchar *end_time,
    85                                                      GPtrArray **program_list);
    86 static gboolean gmyth_dbus_server_get_schedule_list (GObject *obj,
    87                                                      GPtrArray **schedule_list);
    88 
    89 static gboolean gmyth_dbus_server_connected         (GObject *obj,
    90                                                      gboolean *status,
    91                                                      GError **error);
    92 static gboolean gmyth_dbus_server_disconnect        (GObject *obj,
    93                                                      GError **error);
    94 static gboolean gmyth_dbus_server_get_server_info   (GObject *obj,
    95                                                      guint64 *total_space,
    96                                                      guint64 *used_space,
    97                                                      guint64 *free_space,
    98                                                      GError **error);
    99 static gboolean gmyth_dbus_server_get_thumbnail     (GObject *obj,
   100                                                      const gchar *uri,
   101                                                      GByteArray **image,
   102                                                      GError **error);
   103 static gboolean gmyth_dbus_server_get_channel_icon  (GObject *obj,
   104                                                      guint channel_id,
   105                                                      GByteArray **icon,
   106                                                      GError **error);
   107 static gboolean gmyth_dbus_server_stop_recording    (GObject *obj,
   108                                                      guint channel_id,
   109                                                      gboolean *result,
   110                                                      GError **error);
   111 static gboolean gmyth_dbus_server_add_schedule      (GObject *obj,
   112                                                      guint channel_id,
   113                                                      guint program_id,
   114                                                      const gchar *start_time,
   115                                                      const gchar *end_time,
   116                                                      gboolean recurring,
   117                                                      const gchar *description,
   118                                                      guint *schedule_id,
   119                                                      GError **error);
   120 static gboolean gmyth_dbus_server_add_exception     (GObject *obj,
   121                                                      guint schedule_id,
   122                                                      guint channel_id,
   123                                                      guint program_id,
   124                                                      const gchar *start_time,
   125                                                      const gchar *end_time,
   126                                                      const gchar *description,
   127                                                      GError **error);
   128 static gboolean gmyth_dbus_server_remove_schedule   (GObject *obj,
   129                                                      guint schedule_id,
   130                                                      GError **error);
   131 
   132 
   133 #include "gmyth-dbus-server-glue.h"
   134 
   135 
   136 G_DEFINE_TYPE (GMythDbusServer, gmyth_dbus_server, G_TYPE_OBJECT);
   137 
   138 static void
   139 gmyth_dbus_server_class_init (GMythDbusServerClass *klass)
   140 {
   141     GObjectClass *object_class = G_OBJECT_CLASS (klass);
   142 
   143     g_type_class_add_private (klass, sizeof (GMythDbusServerPrivate));
   144 
   145     object_class->dispose = gmyth_dbus_server_dispose;
   146     object_class->finalize = gmyth_dbus_server_finalize;
   147 
   148     dbus_g_object_type_install_info (GMYTH_DBUS_SERVER_TYPE,
   149                                      &dbus_glib_gmyth_dbus_server_object_info);
   150 }
   151 
   152 static void
   153 gmyth_dbus_server_init (GMythDbusServer *self)
   154 {
   155 }
   156 
   157 static void
   158 gmyth_dbus_server_dispose (GObject *object)
   159 {
   160     G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->dispose (object);
   161 }
   162 
   163 static void
   164 gmyth_dbus_server_finalize (GObject *object)
   165 {
   166     G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->finalize (object);
   167 }
   168 
   169 static gboolean
   170 gmyth_dbus_server_connect_epg (GMythDbusServer *server)
   171 {
   172     GMythDbusServerPrivate *priv;
   173     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
   174 
   175     if (!priv->myth_epg)
   176     {
   177         priv->myth_epg = gmyth_epg_new();
   178         if (!gmyth_epg_connect (priv->myth_epg, priv->myth_backend))
   179         {
   180             g_object_unref (priv->myth_epg);
   181             priv->myth_epg = NULL;
   182             return FALSE;
   183         }
   184     }
   185 
   186     return TRUE;
   187 }
   188 
   189 static gboolean
   190 gmyth_dbus_server_connect_scheduler (GMythDbusServer *server)
   191 {
   192     GMythDbusServerPrivate *priv;
   193     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
   194 
   195     if (!priv->myth_scheduler)
   196     {
   197         priv->myth_scheduler = gmyth_scheduler_new ();
   198         if (!gmyth_scheduler_connect (priv->myth_scheduler,
   199                                       priv->myth_backend))
   200         {
   201             g_object_unref (priv->myth_scheduler);
   202             priv->myth_scheduler = NULL;
   203             return FALSE;
   204         }
   205     }
   206 
   207     return TRUE;
   208 }
   209 
   210 static gboolean
   211 gmyth_dbus_server_connect  (GObject *obj,
   212                             const gchar *host,
   213                             guint port,
   214                             const gchar *user,
   215                             const gchar *password,
   216                             gboolean *result,
   217                             GError **error)
   218 {
   219     GMythDbusServerPrivate *priv;
   220     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   221 
   222     if (priv->myth_backend)
   223         return TRUE;
   224 
   225     priv->myth_backend = gmyth_backend_info_new_full (host,
   226                                                       user,
   227                                                       password,
   228                                                       MYTH_DEFAULT_DB,
   229                                                       port);
   230     *result = TRUE;
   231     return TRUE;
   232 }
   233 
   234 static gboolean
   235 gmyth_dbus_server_connected (GObject *obj,
   236                              gboolean *status,
   237                              GError **error)
   238 {
   239     GMythDbusServerPrivate *priv;
   240     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   241 
   242     if (priv->myth_backend)
   243         *status = TRUE;
   244     else
   245         *status = FALSE;
   246     return TRUE;
   247 }
   248 
   249 static gboolean
   250 gmyth_dbus_server_disconnect (GObject *obj,
   251                               GError **error)
   252 {
   253     GMythDbusServerPrivate *priv;
   254     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   255 
   256     if (priv->myth_epg)
   257     {
   258         g_object_unref (priv->myth_epg);
   259         priv->myth_epg = NULL;
   260     }
   261 
   262 
   263     if (priv->myth_backend)
   264     {
   265         g_object_unref (priv->myth_backend);
   266         priv->myth_backend = NULL;
   267     }
   268 
   269     if (priv->myth_scheduler)
   270     {
   271         g_object_unref (priv->myth_scheduler);
   272         priv->myth_scheduler = NULL;
   273     }
   274 
   275 
   276     return TRUE;
   277 }
   278 
   279 static gboolean
   280 gmyth_dbus_server_get_server_info (GObject *obj,
   281                                    guint64 *total_space,
   282                                    guint64 *used_space,
   283                                    guint64 *free_space,
   284                                    GError **error)
   285 {
   286     GMythBackendDetails *details;
   287     GMythDbusServerPrivate *priv;
   288     gboolean ret = FALSE;
   289     GMythSocket *socket;
   290 
   291     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   292     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   293 
   294     g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
   295 
   296     socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
   297 
   298     details = NULL;
   299     gmyth_util_get_backend_details (socket,
   300                                     &details);
   301     if (details)
   302     {
   303         *total_space = details->total_space;
   304         *used_space = details->used_space;
   305         *free_space = *total_space - *used_space;
   306         gmyth_util_backend_details_free (details);
   307 
   308         ret = TRUE;
   309     }
   310 
   311     g_object_unref (socket);
   312 
   313     return ret;
   314 }
   315 
   316 
   317 static void
   318 gmyth_dbus_server_parse_channel_info (GMythChannelInfo *info,
   319                                       GValue *val)
   320 {
   321     dbus_g_type_struct_set (val,
   322                             0, info->channel_ID,
   323                             1, info->channel_num->str,
   324                             2, info->channel_name->str,
   325                             3, info->channel_icon->str,
   326                             G_MAXUINT);
   327 }
   328 
   329 static gboolean 
   330 gmyth_dbus_server_get_channel_info  (GObject *obj,
   331                                      gint channel_id,
   332                                      GValueArray **info,
   333                                      GError **error)
   334 {
   335     GType ch_type;
   336     GMythChannelInfo *ch_info;
   337     GMythDbusServerPrivate *priv;
   338 
   339     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   340     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   341 
   342     g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
   343     g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
   344 
   345     ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
   346 
   347     ch_info = gmyth_epg_get_channel_info (priv->myth_epg, channel_id);
   348     if (ch_info)
   349     {
   350         GValue v = { 0, };
   351         g_value_init (&v, ch_type);
   352         g_value_take_boxed (&v, dbus_g_type_specialized_construct (ch_type));
   353         gmyth_dbus_server_parse_channel_info (ch_info, &v);
   354 
   355         *info = g_value_get_boxed (&v);
   356         return TRUE;
   357     }
   358 
   359     return FALSE;
   360 }
   361 
   362 
   363 static gboolean
   364 gmyth_dbus_server_get_channel_list (GObject *obj,
   365                                     GPtrArray **channels,
   366                                     GError **error)
   367 {
   368     GList *lst;
   369     GList *walk;
   370     int len;
   371     GType ch_type;
   372     GMythDbusServerPrivate *priv;
   373 
   374     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   375     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   376 
   377     g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
   378     g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
   379 
   380 
   381     len = gmyth_epg_get_channel_list (priv->myth_epg, &lst);
   382 
   383     *channels = g_ptr_array_sized_new (len);
   384     ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
   385 
   386     for (walk = lst; walk != NULL; walk = walk->next)
   387     {
   388         GValue ch = { 0, };
   389         GMythChannelInfo *data;
   390 
   391         data = (GMythChannelInfo *) walk->data;
   392 
   393         g_value_init (&ch, ch_type);
   394         g_value_take_boxed (&ch, dbus_g_type_specialized_construct (ch_type));
   395         gmyth_dbus_server_parse_channel_info (data, &ch);
   396         g_ptr_array_add (*channels, g_value_get_boxed (&ch));
   397     }
   398 
   399     gmyth_free_channel_list (lst);
   400     return TRUE;
   401 }
   402 
   403 static gboolean
   404 gmyth_dbus_server_file_exists (GObject *obj,
   405                                const gchar *file_name,
   406                                gboolean *exists,
   407                                GError **error)
   408 {
   409     GMythDbusServerPrivate *priv;
   410     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   411 
   412     g_return_val_if_fail (priv->myth_backend, FALSE);
   413 
   414     *exists = gmyth_util_file_exists (priv->myth_backend, file_name);
   415 
   416     return TRUE;
   417 }
   418 
   419 static gboolean
   420 gmyth_dbus_server_get_program_list (GObject *obj,
   421                                     gint channel_id,
   422                                     const gchar *start_time,
   423                                     const gchar *end_time,
   424                                     GPtrArray **programs)
   425 {
   426     GList *list;
   427     GList *walk;
   428     gint len;
   429     GType program_type;
   430     GTimeVal start_time_val;
   431     GTimeVal end_time_val;
   432     GMythDbusServerPrivate *priv;
   433 
   434     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   435     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   436 
   437     g_return_val_if_fail (priv->myth_backend, FALSE);
   438     g_return_val_if_fail (gmyth_dbus_server_connect_epg (GMYTH_DBUS_SERVER (obj)), FALSE);
   439 
   440 
   441     g_time_val_from_iso8601 (start_time, &start_time_val);
   442     g_time_val_from_iso8601 (end_time, &end_time_val);
   443 
   444     list = NULL;
   445     len = gmyth_epg_get_program_list (priv->myth_epg,
   446                                       &list,
   447                                       channel_id,
   448                                       &start_time_val,
   449                                       &end_time_val);
   450 
   451     *programs = g_ptr_array_sized_new (len);
   452     program_type = GMYTH_DBUS_PROGRAM_G_TYPE;
   453 
   454     for (walk = list; walk != NULL; walk = walk->next)
   455     {
   456         GValue program = { 0, };
   457         gchar *start_str;
   458         gchar *end_str;
   459         GMythProgramInfo *data;
   460 
   461         data = (GMythProgramInfo *) walk->data;
   462 
   463         g_value_init (&program, program_type);
   464         g_value_take_boxed (&program,
   465                             dbus_g_type_specialized_construct (program_type));
   466 
   467         start_str = g_time_val_to_iso8601 (data->startts);
   468         end_str = g_time_val_to_iso8601 (data->endts);
   469 
   470         dbus_g_type_struct_set (&program,
   471                                 0, data->chanid,
   472                                 1, start_str,
   473                                 2, end_str,
   474                                 3, data->title->str,
   475                                 4, data->subtitle->str,
   476                                 5, data->description->str,
   477                                 6, data->category->str,
   478                                 G_MAXUINT);
   479 
   480         g_ptr_array_add (*programs, g_value_get_boxed (&program));
   481         g_free (start_str);
   482         g_free (end_str);
   483     }
   484 
   485     gmyth_free_program_list (list);
   486     return TRUE;
   487 }
   488 
   489 static void
   490 gmyth_dbus_server_parse_recorded_info (RecordedInfo *info,
   491                                        GValue *val)
   492 {
   493     gchar *start_str;
   494     gchar *end_str;
   495 
   496     start_str = g_time_val_to_iso8601 (info->start_time);
   497     end_str = g_time_val_to_iso8601 (info->end_time);
   498 
   499     dbus_g_type_struct_set (val,
   500                             0, info->record_id,
   501                             1, info->program_id,
   502                             2, info->channel_id,
   503                             3, start_str,
   504                             4, end_str,
   505                             5, info->title->str,
   506                             6, info->subtitle->str,
   507                             7, info->description->str,
   508                             8, info->category->str,
   509                             9, info->basename->str,
   510                             10, info->filesize,
   511                             G_MAXUINT);
   512     g_free (start_str);
   513     g_free (end_str);
   514 }
   515 
   516 static gboolean
   517 gmyth_dbus_server_get_recorded_info (GObject *obj,
   518                                      const gchar *basename,
   519                                      GValueArray **info,
   520                                      GError **error)
   521 {
   522     GType record_type;
   523     GMythDbusServerPrivate *priv;
   524     RecordedInfo *record_info;
   525 
   526 
   527     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   528     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   529 
   530     g_return_val_if_fail (priv->myth_backend, FALSE);
   531     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   532 
   533     record_type = GMYTH_DBUS_RECORD_G_TYPE;
   534 
   535     record_info = gmyth_scheduler_get_recorded_info (priv->myth_scheduler,
   536                                                      basename);
   537 
   538     if (record_info)
   539     {
   540         GValue r = { 0, };
   541 
   542         g_value_init (&r, record_type);
   543         g_value_take_boxed (&r,
   544                             dbus_g_type_specialized_construct (record_type));
   545 
   546         gmyth_dbus_server_parse_recorded_info (record_info, &r);
   547         gmyth_recorded_info_free (record_info);
   548 
   549         *info = g_value_get_boxed (&r);
   550 
   551         return TRUE;
   552     }
   553 
   554     return FALSE;
   555 }
   556 
   557 
   558 static gboolean
   559 gmyth_dbus_server_get_recorded_list (GObject *obj,
   560                                      GPtrArray **records,
   561                                      GError **error)
   562 {
   563     GList *list;
   564     GList *walk;
   565     gint len;
   566     GType record_type;
   567     GMythDbusServerPrivate *priv;
   568 
   569     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   570     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   571 
   572     g_return_val_if_fail (priv->myth_backend, FALSE);
   573     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   574 
   575 
   576     len = gmyth_scheduler_get_recorded_list (priv->myth_scheduler,
   577                                              &list);
   578 
   579     record_type = GMYTH_DBUS_RECORD_G_TYPE;
   580     *records = g_ptr_array_sized_new (len);
   581 
   582     for (walk = list; walk != NULL; walk = walk->next)
   583     {
   584         GValue record = { 0, };
   585         RecordedInfo *data;
   586 
   587         data = (RecordedInfo *) walk->data;
   588 
   589         g_value_init (&record, record_type);
   590         g_value_take_boxed (&record,
   591                             dbus_g_type_specialized_construct (record_type));
   592 
   593         gmyth_dbus_server_parse_recorded_info (data, &record);
   594 
   595         g_ptr_array_add (*records, g_value_get_boxed (&record));
   596     }
   597 
   598     gmyth_recorded_info_list_free (list);
   599 
   600     return TRUE;
   601 
   602 }
   603 
   604 static gboolean
   605 gmyth_dbus_server_get_schedule_list (GObject *obj,
   606                                      GPtrArray **schedules)
   607 {
   608     GList *list;
   609     GList *walk;
   610     gint len;
   611     GType schedule_type;
   612     GMythDbusServerPrivate *priv;
   613 
   614     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   615     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   616 
   617     g_return_val_if_fail (priv->myth_backend, FALSE);
   618     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   619 
   620 
   621     len = gmyth_scheduler_get_schedule_list (priv->myth_scheduler,
   622                                              &list);
   623 
   624     *schedules = g_ptr_array_sized_new (len);
   625     schedule_type = GMYTH_DBUS_SCHEDULE_G_TYPE;
   626 
   627     for (walk = list; walk != NULL; walk = walk->next)
   628     {
   629         GValue schedule = { 0, };
   630         ScheduleInfo *data;
   631         gchar *start_str_time;
   632         gchar *end_str_time;
   633 
   634         data = (ScheduleInfo *) walk->data;
   635 
   636         g_value_init (&schedule, schedule_type);
   637         g_value_take_boxed (&schedule,
   638                             dbus_g_type_specialized_construct (schedule_type));
   639 
   640         start_str_time = g_time_val_to_iso8601 (data->start_time);
   641         end_str_time = g_time_val_to_iso8601 (data->end_time);
   642 
   643         dbus_g_type_struct_set (&schedule,
   644                             0, data->schedule_id,
   645                             1, data->program_id,
   646                             2, data->channel_id,
   647                             3, start_str_time,
   648                             4, end_str_time,
   649                             5, data->title->str,
   650                             6, data->subtitle->str,
   651                             7, data->description->str,
   652                             8, data->category->str,
   653                             9, data->type,
   654                             G_MAXUINT);
   655 
   656         g_ptr_array_add (*schedules, g_value_get_boxed (&schedule));
   657 
   658         g_free (start_str_time);
   659         g_free (end_str_time);
   660     }
   661 
   662     gmyth_schedule_info_list_free (list);
   663 
   664     return TRUE;
   665 }
   666 
   667 
   668 static gboolean
   669 gmyth_dbus_server_get_thumbnail (GObject *obj,
   670                                 const gchar *uri,
   671                                 GByteArray **image,
   672                                 GError **error)
   673 {
   674     GMythFileTransfer *file_transfer;
   675     glong filesize;
   676     GMythFileReadResult result;
   677     GMythDbusServerPrivate *priv;
   678 
   679     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   680 
   681     g_debug ("getting icon: %s", uri);
   682 
   683     g_return_val_if_fail (priv->myth_backend, FALSE);
   684 
   685     if (!gmyth_util_file_exists (priv->myth_backend, uri))
   686         goto fail;
   687 
   688     file_transfer = gmyth_file_transfer_new (priv->myth_backend);
   689 
   690     if (!gmyth_file_transfer_open (file_transfer, uri))
   691         goto fail;
   692 
   693     filesize = gmyth_file_transfer_get_filesize (file_transfer);
   694     if (filesize <= 0)
   695         goto fail;
   696 
   697     *image = g_byte_array_new ();
   698     result = gmyth_file_transfer_read (file_transfer, *image, filesize, FALSE);
   699     if (result == GMYTH_FILE_READ_ERROR)
   700        goto fail;
   701 
   702     gmyth_file_transfer_close (file_transfer);
   703     g_object_unref (file_transfer);
   704 
   705     if (filesize > (*image)->len)
   706         goto fail;
   707 
   708     return TRUE;
   709 
   710 fail:
   711     g_object_unref(file_transfer);
   712     return FALSE;
   713 }
   714 
   715 static gboolean
   716 gmyth_dbus_server_get_channel_icon (GObject *obj,
   717                                     guint channel_id,
   718                                     GByteArray **icon,
   719                                     GError **error)
   720 {
   721     GMythChannelInfo *channel = NULL;
   722     guint8 *icon_data;
   723     guint icon_length;
   724     GMythDbusServerPrivate *priv;
   725 
   726     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   727     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   728     g_return_val_if_fail (priv->myth_backend, FALSE);
   729 
   730     channel = gmyth_epg_get_channel_info (priv->myth_epg,
   731                                          (gint) channel_id);
   732 
   733     *icon = NULL;
   734 
   735     if (channel == NULL)
   736         return FALSE;
   737 
   738     if (!gmyth_epg_channel_has_icon(priv->myth_epg, channel))
   739     {
   740         gmyth_channel_info_free (channel);
   741         g_debug("Channel does not have icon available");
   742         return FALSE;
   743     }
   744 
   745     icon_data = NULL;
   746     icon_length = 0;
   747     if (!gmyth_epg_channel_get_icon (priv->myth_epg,
   748                                      channel,
   749                                      &icon_data,
   750                                      &icon_length)) 
   751     {
   752         gmyth_channel_info_free (channel);
   753         g_warning("Could not get channel icon for channel id = %u", channel_id);
   754         return FALSE;
   755     }
   756 
   757     *icon = g_byte_array_sized_new (icon_length);
   758     *icon = g_byte_array_append (*icon, icon_data, icon_length);
   759 
   760     g_free (icon_data);
   761     gmyth_channel_info_free(channel);
   762     return TRUE;
   763 }
   764 
   765 
   766 static gboolean
   767 gmyth_dbus_server_stop_recording (GObject *obj,
   768                                   guint channel_id,
   769                                   gboolean *result,
   770                                   GError **error)
   771 {
   772     gboolean ret = FALSE;
   773     GMythDbusServerPrivate *priv;
   774 
   775     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   776     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   777 
   778     g_return_val_if_fail (priv->myth_backend, FALSE);
   779     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   780 
   781     ret = gmyth_scheduler_stop_recording (priv->myth_scheduler,
   782                                           channel_id);
   783 
   784     return ret;
   785 }
   786 
   787 static ScheduleInfo*
   788 gmyth_dbus_server_new_schedule_info (const gchar* description,
   789                                      guint channel_id,
   790                                      guint program_id,
   791                                      GTimeVal *start_vtime,
   792                                      GTimeVal *end_vtime)
   793 {
   794     ScheduleInfo   *new_sched_info;
   795 
   796     new_sched_info = g_new0(ScheduleInfo, 1);
   797 
   798     /* record_id == -1 for generating a new id */
   799     new_sched_info->schedule_id = -1;
   800 
   801     new_sched_info->channel_id = channel_id;
   802     new_sched_info->program_id = program_id;
   803     new_sched_info->start_time = g_new0 (GTimeVal, 1);
   804     *new_sched_info->start_time = *start_vtime;
   805     new_sched_info->end_time = g_new0 (GTimeVal, 1);
   806     *new_sched_info->end_time = *end_vtime;
   807 
   808     /* TODO: there is no frequency field */
   809     /*new_sched_info->frequency = -1;*/
   810 
   811     if (description != NULL) {
   812         /* FIXME: description parameter is used as title and description */
   813         new_sched_info->title = g_string_new(description);
   814         new_sched_info->description = g_string_new(description);
   815     }
   816 
   817     return new_sched_info;
   818 }
   819 
   820 static gboolean
   821 gmyth_dbus_server_add_schedule (GObject *obj,
   822                                 guint channel_id,
   823                                 guint program_id,
   824                                 const gchar *start_time,
   825                                 const gchar *end_time,
   826                                 gboolean recurring,
   827                                 const gchar *description,
   828                                 guint *schedule_id,
   829                                 GError **error)
   830 {
   831     ScheduleInfo *sch_info;
   832     GTimeVal start_vtime;
   833     GTimeVal end_vtime;
   834     GMythDbusServerPrivate *priv;
   835 
   836     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   837     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   838 
   839     *schedule_id = 0;
   840 
   841     g_return_val_if_fail (priv->myth_backend, FALSE);
   842     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   843 
   844 
   845     g_time_val_from_iso8601 (start_time, &start_vtime);
   846     g_time_val_from_iso8601 (end_time, &end_vtime);
   847     sch_info = gmyth_dbus_server_new_schedule_info (description,
   848                                                     channel_id,
   849                                                     program_id,
   850                                                     &start_vtime,
   851                                                     &end_vtime);
   852     if (sch_info != NULL) {
   853         GMythScheduleType type;
   854         GTimeVal t_now;
   855         gboolean has_record;
   856 
   857         type = (recurring ?
   858                 GMYTH_SCHEDULE_ALL_OCCURRENCES :
   859                 GMYTH_SCHEDULE_ONE_OCCURRENCE);
   860 
   861         g_get_current_time (&t_now);
   862 
   863         has_record = gmyth_scheduler_was_recorded_before (priv->myth_scheduler, 
   864                                                           channel_id,
   865                                                           (time_t) start_vtime.tv_sec);
   866 
   867 
   868         if ((t_now.tv_sec >= start_vtime.tv_sec)
   869             && (t_now.tv_sec <= end_vtime.tv_sec) && has_record)
   870         {
   871             GMythSocket    *socket;
   872             gboolean        res = FALSE;
   873 
   874             socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
   875             res = gmyth_scheduler_reactivate_schedule(priv->myth_scheduler,
   876                                                       channel_id,
   877                                                       (time_t) start_vtime.tv_sec);
   878             if (res) {
   879                 GMythStringList *slist = gmyth_string_list_new();
   880 
   881                 gmyth_string_list_append_char_array(slist, "RESCHEDULE_RECORDINGS 0");
   882                 gmyth_socket_sendreceive_stringlist(socket, slist);
   883                 res = (gmyth_string_list_get_int(slist, 0) == 1);
   884                 g_object_unref(slist);
   885             }
   886 
   887             g_object_unref(socket);
   888             return res;
   889         }
   890         else
   891         {
   892             if (!gmyth_scheduler_add_schedule_full (priv->myth_scheduler,
   893                                                     sch_info,
   894                                                     type))
   895             {
   896                 g_warning("Could not add schedule entry");
   897                 return FALSE;
   898             }
   899 
   900             (*schedule_id) = sch_info->schedule_id;
   901             gmyth_schedule_info_free (sch_info);
   902             return TRUE;
   903         }
   904     }
   905     return FALSE;
   906 }
   907 
   908 static gboolean
   909 gmyth_dbus_server_add_exception (GObject *obj,
   910                                  guint schedule_id,
   911                                  guint channel_id,
   912                                  guint program_id,
   913                                  const gchar *start_time,
   914                                  const gchar *end_time,
   915                                  const gchar *description,
   916                                  GError **error)
   917 {
   918     ScheduleInfo *sch_info;
   919     GTimeVal start_vtime;
   920     GTimeVal end_vtime;
   921     GMythDbusServerPrivate *priv;
   922 
   923     g_debug ("%s:%d", __FUNCTION__, __LINE__);
   924     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   925 
   926     g_return_val_if_fail (priv->myth_backend, FALSE);
   927     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   928 
   929     g_time_val_from_iso8601 (start_time, &start_vtime);
   930     g_time_val_from_iso8601 (end_time, &end_vtime);
   931 
   932     sch_info = gmyth_dbus_server_new_schedule_info (description,
   933                                                    channel_id,
   934                                                    program_id,
   935                                                    &start_vtime,
   936                                                    &end_vtime);
   937     if (sch_info != NULL)
   938     {
   939        if (!gmyth_scheduler_add_exception (priv->myth_scheduler,
   940                                            schedule_id,
   941                                            sch_info))
   942        {
   943            g_warning ("Could not add schedule exception");
   944            gmyth_schedule_info_free (sch_info);
   945            return FALSE;
   946        }
   947 
   948        gmyth_schedule_info_free (sch_info);
   949        return TRUE;
   950     }
   951     return FALSE;
   952 }
   953 
   954 static gboolean
   955 gmyth_dbus_server_remove_schedule (GObject *obj,
   956                                    guint schedule_id,
   957                                    GError **error)
   958 {
   959     GMythDbusServerPrivate *priv;
   960 
   961     priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
   962 
   963     g_return_val_if_fail (priv->myth_backend, FALSE);
   964     g_return_val_if_fail (gmyth_dbus_server_connect_scheduler (GMYTH_DBUS_SERVER (obj)), FALSE);
   965 
   966     return gmyth_scheduler_delete_schedule (priv->myth_scheduler, schedule_id);
   967 }
   968 
   969 GMythDbusServer*
   970 gmyth_dbus_server_start_dbus_service (void)
   971 {
   972     GError *error = NULL;
   973     DBusGProxy *proxy;
   974     DBusGConnection *bus;
   975     guint request_ret;
   976     GMythDbusServer *self;
   977 
   978     self = g_object_new (GMYTH_DBUS_SERVER_TYPE, NULL);
   979     g_return_val_if_fail (self, FALSE);
   980 
   981     /* TODO: should verify if this service was already started */
   982 
   983     /* connect to session bus */
   984     bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
   985     if (bus == NULL) 
   986     {
   987         g_warning ("Could not connect to dbus: %s", error->message);
   988         g_error_free (error);
   989         goto fail;
   990     }
   991 
   992     /* register dbus object */
   993     dbus_g_connection_register_g_object (bus,
   994         GMYTH_DBUS_SERVER_PATH, G_OBJECT (self));
   995 
   996     proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS,
   997                                        DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
   998 
   999     /* registering download manager service */
  1000     if (!org_freedesktop_DBus_request_name (proxy, GMYTH_DBUS_SERVER_IFACE,
  1001                                             0, &request_ret, &error)) 
  1002     {
  1003         g_warning ("Unable to register dbus service: %d %s",
  1004                    error->code, error->message);
  1005         g_error_free (error);
  1006         goto fail;
  1007     }
  1008 
  1009     if (request_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) 
  1010     {
  1011         g_warning ("Got result code %u from requesting name", request_ret);
  1012         goto fail;
  1013     }
  1014 
  1015     return self;
  1016 
  1017 fail:
  1018     g_object_unref (self);
  1019     return NULL;
  1020 }
  1021