4 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
5 * @author Renato Filho <renato.filho@indt.org.br>
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.
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.
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
29 #include <gmyth/gmyth.h>
30 #include <dbus/dbus-glib-bindings.h>
33 #include "gmyth-dbus-common.h"
34 #include "gmyth-dbus-server.h"
36 #define MYTH_DEFAULT_DB "mythconverg"
38 typedef struct _GMythDbusServerPrivate GMythDbusServerPrivate;
40 struct _GMythDbusServerPrivate
42 GMythBackendInfo *myth_backend;
44 GMythScheduler *myth_scheduler;
47 #define GMYTH_DBUS_SERVER_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GMYTH_DBUS_SERVER_TYPE, GMythDbusServerPrivate))
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);
56 static gboolean gmyth_dbus_server_connect (GObject *obj,
60 const gchar *password,
63 static gboolean gmyth_dbus_server_get_channel_list (GObject *obj,
66 static gboolean gmyth_dbus_server_get_channel_info (GObject *obj,
70 static gboolean gmyth_dbus_server_file_exists (GObject *obj,
71 const gchar *file_name,
74 static gboolean gmyth_dbus_server_get_recorded_list (GObject *obj,
77 static gboolean gmyth_dbus_server_get_recorded_info (GObject *obj,
78 const gchar *basename,
81 static gboolean gmyth_dbus_server_get_program_list (GObject *obj,
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);
89 static gboolean gmyth_dbus_server_connected (GObject *obj,
92 static gboolean gmyth_dbus_server_disconnect (GObject *obj,
94 static gboolean gmyth_dbus_server_get_server_info (GObject *obj,
99 static gboolean gmyth_dbus_server_get_thumbnail (GObject *obj,
103 static gboolean gmyth_dbus_server_get_channel_icon (GObject *obj,
107 static gboolean gmyth_dbus_server_stop_recording (GObject *obj,
111 static gboolean gmyth_dbus_server_add_schedule (GObject *obj,
114 const gchar *start_time,
115 const gchar *end_time,
117 const gchar *description,
120 static gboolean gmyth_dbus_server_add_exception (GObject *obj,
124 const gchar *start_time,
125 const gchar *end_time,
126 const gchar *description,
128 static gboolean gmyth_dbus_server_remove_schedule (GObject *obj,
133 #include "gmyth-dbus-server-glue.h"
136 G_DEFINE_TYPE (GMythDbusServer, gmyth_dbus_server, G_TYPE_OBJECT);
139 gmyth_dbus_server_class_init (GMythDbusServerClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
143 g_type_class_add_private (klass, sizeof (GMythDbusServerPrivate));
145 object_class->dispose = gmyth_dbus_server_dispose;
146 object_class->finalize = gmyth_dbus_server_finalize;
148 dbus_g_object_type_install_info (GMYTH_DBUS_SERVER_TYPE,
149 &dbus_glib_gmyth_dbus_server_object_info);
153 gmyth_dbus_server_init (GMythDbusServer *self)
158 gmyth_dbus_server_dispose (GObject *object)
160 G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->dispose (object);
164 gmyth_dbus_server_finalize (GObject *object)
166 G_OBJECT_CLASS (gmyth_dbus_server_parent_class)->finalize (object);
170 gmyth_dbus_server_connect_epg (GMythDbusServer *server)
172 GMythDbusServerPrivate *priv;
173 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
177 priv->myth_epg = gmyth_epg_new();
178 if (!gmyth_epg_connect (priv->myth_epg, priv->myth_backend))
180 g_object_unref (priv->myth_epg);
181 priv->myth_epg = NULL;
190 gmyth_dbus_server_connect_scheduler (GMythDbusServer *server)
192 GMythDbusServerPrivate *priv;
193 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (server);
195 if (!priv->myth_scheduler)
197 priv->myth_scheduler = gmyth_scheduler_new ();
198 if (!gmyth_scheduler_connect (priv->myth_scheduler,
201 g_object_unref (priv->myth_scheduler);
202 priv->myth_scheduler = NULL;
211 gmyth_dbus_server_connect (GObject *obj,
215 const gchar *password,
219 GMythDbusServerPrivate *priv;
220 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
222 if (priv->myth_backend)
225 priv->myth_backend = gmyth_backend_info_new_full (host,
235 gmyth_dbus_server_connected (GObject *obj,
239 GMythDbusServerPrivate *priv;
240 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
242 if (priv->myth_backend)
250 gmyth_dbus_server_disconnect (GObject *obj,
253 GMythDbusServerPrivate *priv;
254 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
258 g_object_unref (priv->myth_epg);
259 priv->myth_epg = NULL;
263 if (priv->myth_backend)
265 g_object_unref (priv->myth_backend);
266 priv->myth_backend = NULL;
269 if (priv->myth_scheduler)
271 g_object_unref (priv->myth_scheduler);
272 priv->myth_scheduler = NULL;
280 gmyth_dbus_server_get_server_info (GObject *obj,
281 guint64 *total_space,
286 GMythBackendDetails *details;
287 GMythDbusServerPrivate *priv;
288 gboolean ret = FALSE;
291 g_debug ("%s:%d", __FUNCTION__, __LINE__);
292 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
294 g_return_val_if_fail (priv->myth_backend != NULL, FALSE);
296 socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
299 gmyth_util_get_backend_details (socket,
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);
311 g_object_unref (socket);
318 gmyth_dbus_server_parse_channel_info (GMythChannelInfo *info,
321 dbus_g_type_struct_set (val,
323 1, info->channel_num->str,
324 2, info->channel_name->str,
325 3, info->channel_icon->str,
330 gmyth_dbus_server_get_channel_info (GObject *obj,
336 GMythChannelInfo *ch_info;
337 GMythDbusServerPrivate *priv;
339 g_debug ("%s:%d", __FUNCTION__, __LINE__);
340 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
345 ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
347 ch_info = gmyth_epg_get_channel_info (priv->myth_epg, channel_id);
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);
355 *info = g_value_get_boxed (&v);
364 gmyth_dbus_server_get_channel_list (GObject *obj,
365 GPtrArray **channels,
372 GMythDbusServerPrivate *priv;
374 g_debug ("%s:%d", __FUNCTION__, __LINE__);
375 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
381 len = gmyth_epg_get_channel_list (priv->myth_epg, &lst);
383 *channels = g_ptr_array_sized_new (len);
384 ch_type = GMYTH_DBUS_CHANNEL_G_TYPE;
386 for (walk = lst; walk != NULL; walk = walk->next)
389 GMythChannelInfo *data;
391 data = (GMythChannelInfo *) walk->data;
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));
399 gmyth_free_channel_list (lst);
404 gmyth_dbus_server_file_exists (GObject *obj,
405 const gchar *file_name,
409 GMythDbusServerPrivate *priv;
410 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
412 g_return_val_if_fail (priv->myth_backend, FALSE);
414 *exists = gmyth_util_file_exists (priv->myth_backend, file_name);
420 gmyth_dbus_server_get_program_list (GObject *obj,
422 const gchar *start_time,
423 const gchar *end_time,
424 GPtrArray **programs)
430 GTimeVal start_time_val;
431 GTimeVal end_time_val;
432 GMythDbusServerPrivate *priv;
434 g_debug ("%s:%d", __FUNCTION__, __LINE__);
435 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
441 g_time_val_from_iso8601 (start_time, &start_time_val);
442 g_time_val_from_iso8601 (end_time, &end_time_val);
445 len = gmyth_epg_get_program_list (priv->myth_epg,
451 *programs = g_ptr_array_sized_new (len);
452 program_type = GMYTH_DBUS_PROGRAM_G_TYPE;
454 for (walk = list; walk != NULL; walk = walk->next)
456 GValue program = { 0, };
459 GMythProgramInfo *data;
461 data = (GMythProgramInfo *) walk->data;
463 g_value_init (&program, program_type);
464 g_value_take_boxed (&program,
465 dbus_g_type_specialized_construct (program_type));
467 start_str = g_time_val_to_iso8601 (data->startts);
468 end_str = g_time_val_to_iso8601 (data->endts);
470 dbus_g_type_struct_set (&program,
471 0, data->chanid->str,
475 4, data->subtitle->str,
476 5, data->description->str,
477 6, data->category->str,
480 g_ptr_array_add (*programs, g_value_get_boxed (&program));
485 gmyth_free_program_list (list);
490 gmyth_dbus_server_parse_recorded_info (RecordedInfo *info,
496 start_str = g_time_val_to_iso8601 (info->start_time);
497 end_str = g_time_val_to_iso8601 (info->end_time);
499 dbus_g_type_struct_set (val,
506 6, info->subtitle->str,
507 7, info->description->str,
508 8, info->category->str,
509 9, info->basename->str,
517 gmyth_dbus_server_get_recorded_info (GObject *obj,
518 const gchar *basename,
523 GMythDbusServerPrivate *priv;
524 RecordedInfo *record_info;
527 g_debug ("%s:%d", __FUNCTION__, __LINE__);
528 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
533 record_type = GMYTH_DBUS_RECORD_G_TYPE;
535 record_info = gmyth_scheduler_get_recorded_info (priv->myth_scheduler,
542 g_value_init (&r, record_type);
543 g_value_take_boxed (&r,
544 dbus_g_type_specialized_construct (record_type));
546 gmyth_dbus_server_parse_recorded_info (record_info, &r);
547 gmyth_recorded_info_free (record_info);
549 *info = g_value_get_boxed (&r);
559 gmyth_dbus_server_get_recorded_list (GObject *obj,
567 GMythDbusServerPrivate *priv;
569 g_debug ("%s:%d", __FUNCTION__, __LINE__);
570 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
576 len = gmyth_scheduler_get_recorded_list (priv->myth_scheduler,
579 record_type = GMYTH_DBUS_RECORD_G_TYPE;
580 *records = g_ptr_array_sized_new (len);
582 for (walk = list; walk != NULL; walk = walk->next)
584 GValue record = { 0, };
587 data = (RecordedInfo *) walk->data;
589 g_value_init (&record, record_type);
590 g_value_take_boxed (&record,
591 dbus_g_type_specialized_construct (record_type));
593 gmyth_dbus_server_parse_recorded_info (data, &record);
595 g_ptr_array_add (*records, g_value_get_boxed (&record));
598 gmyth_recorded_info_list_free (list);
605 gmyth_dbus_server_get_schedule_list (GObject *obj,
606 GPtrArray **schedules)
612 GMythDbusServerPrivate *priv;
614 g_debug ("%s:%d", __FUNCTION__, __LINE__);
615 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
621 len = gmyth_scheduler_get_schedule_list (priv->myth_scheduler,
624 *schedules = g_ptr_array_sized_new (len);
625 schedule_type = GMYTH_DBUS_SCHEDULE_G_TYPE;
627 for (walk = list; walk != NULL; walk = walk->next)
629 GValue schedule = { 0, };
631 gchar *start_str_time;
634 data = (ScheduleInfo *) walk->data;
636 g_value_init (&schedule, schedule_type);
637 g_value_take_boxed (&schedule,
638 dbus_g_type_specialized_construct (schedule_type));
640 start_str_time = g_time_val_to_iso8601 (data->start_time);
641 end_str_time = g_time_val_to_iso8601 (data->end_time);
643 dbus_g_type_struct_set (&schedule,
644 0, data->schedule_id,
650 6, data->subtitle->str,
651 7, data->description->str,
652 8, data->category->str,
656 g_ptr_array_add (*schedules, g_value_get_boxed (&schedule));
658 g_free (start_str_time);
659 g_free (end_str_time);
662 gmyth_schedule_info_list_free (list);
669 gmyth_dbus_server_get_thumbnail (GObject *obj,
674 GMythFileTransfer *file_transfer;
676 GMythFileReadResult result;
677 GMythDbusServerPrivate *priv;
679 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
681 g_debug ("getting icon: %s", uri);
683 g_return_val_if_fail (priv->myth_backend, FALSE);
685 if (!gmyth_util_file_exists (priv->myth_backend, uri))
688 file_transfer = gmyth_file_transfer_new (priv->myth_backend);
690 if (!gmyth_file_transfer_open (file_transfer, uri))
693 filesize = gmyth_file_transfer_get_filesize (file_transfer);
697 *image = g_byte_array_new ();
698 result = gmyth_file_transfer_read (file_transfer, *image, filesize, FALSE);
699 if (result == GMYTH_FILE_READ_ERROR)
702 gmyth_file_transfer_close (file_transfer);
703 g_object_unref (file_transfer);
705 if (filesize > (*image)->len)
711 g_object_unref(file_transfer);
716 gmyth_dbus_server_get_channel_icon (GObject *obj,
721 GMythChannelInfo *channel = NULL;
724 GMythDbusServerPrivate *priv;
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);
730 channel = gmyth_epg_get_channel_info (priv->myth_epg,
738 if (!gmyth_epg_channel_has_icon(priv->myth_epg, channel))
740 gmyth_channel_info_free (channel);
741 g_debug("Channel does not have icon available");
747 if (!gmyth_epg_channel_get_icon (priv->myth_epg,
752 gmyth_channel_info_free (channel);
753 g_warning("Could not get channel icon for channel id = %u", channel_id);
757 *icon = g_byte_array_sized_new (icon_length);
758 *icon = g_byte_array_append (*icon, icon_data, icon_length);
761 gmyth_channel_info_free(channel);
767 gmyth_dbus_server_stop_recording (GObject *obj,
772 gboolean ret = FALSE;
773 GMythDbusServerPrivate *priv;
775 g_debug ("%s:%d", __FUNCTION__, __LINE__);
776 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
781 ret = gmyth_scheduler_stop_recording (priv->myth_scheduler,
788 gmyth_dbus_server_new_schedule_info (const gchar* description,
791 GTimeVal *start_vtime,
794 ScheduleInfo *new_sched_info;
796 new_sched_info = g_new0(ScheduleInfo, 1);
798 /* record_id == -1 for generating a new id */
799 new_sched_info->schedule_id = -1;
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;
808 /* TODO: there is no frequency field */
809 /*new_sched_info->frequency = -1;*/
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);
817 return new_sched_info;
821 gmyth_dbus_server_add_schedule (GObject *obj,
824 const gchar *start_time,
825 const gchar *end_time,
827 const gchar *description,
831 ScheduleInfo *sch_info;
832 GTimeVal start_vtime;
834 GMythDbusServerPrivate *priv;
836 g_debug ("%s:%d", __FUNCTION__, __LINE__);
837 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
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,
852 if (sch_info != NULL) {
853 GMythScheduleType type;
858 GMYTH_SCHEDULE_ALL_OCCURRENCES :
859 GMYTH_SCHEDULE_ONE_OCCURRENCE);
861 g_get_current_time (&t_now);
863 has_record = gmyth_scheduler_was_recorded_before (priv->myth_scheduler,
865 (time_t) start_vtime.tv_sec);
868 if ((t_now.tv_sec >= start_vtime.tv_sec)
869 && (t_now.tv_sec <= end_vtime.tv_sec) && has_record)
872 gboolean res = FALSE;
874 socket = gmyth_backend_info_get_connected_socket (priv->myth_backend);
875 res = gmyth_scheduler_reactivate_schedule(priv->myth_scheduler,
877 (time_t) start_vtime.tv_sec);
879 GMythStringList *slist = gmyth_string_list_new();
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);
887 g_object_unref(socket);
892 if (!gmyth_scheduler_add_schedule_full (priv->myth_scheduler,
896 g_warning("Could not add schedule entry");
900 (*schedule_id) = sch_info->schedule_id;
901 gmyth_schedule_info_free (sch_info);
909 gmyth_dbus_server_add_exception (GObject *obj,
913 const gchar *start_time,
914 const gchar *end_time,
915 const gchar *description,
918 ScheduleInfo *sch_info;
919 GTimeVal start_vtime;
921 GMythDbusServerPrivate *priv;
923 g_debug ("%s:%d", __FUNCTION__, __LINE__);
924 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
929 g_time_val_from_iso8601 (start_time, &start_vtime);
930 g_time_val_from_iso8601 (end_time, &end_vtime);
932 sch_info = gmyth_dbus_server_new_schedule_info (description,
937 if (sch_info != NULL)
939 if (!gmyth_scheduler_add_exception (priv->myth_scheduler,
943 g_warning ("Could not add schedule exception");
944 gmyth_schedule_info_free (sch_info);
948 gmyth_schedule_info_free (sch_info);
955 gmyth_dbus_server_remove_schedule (GObject *obj,
959 GMythDbusServerPrivate *priv;
961 priv = GMYTH_DBUS_SERVER_GET_PRIVATE (obj);
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);
966 return gmyth_scheduler_delete_schedule (priv->myth_scheduler, schedule_id);
970 gmyth_dbus_server_start_dbus_service (void)
972 GError *error = NULL;
974 DBusGConnection *bus;
976 GMythDbusServer *self;
978 self = g_object_new (GMYTH_DBUS_SERVER_TYPE, NULL);
979 g_return_val_if_fail (self, FALSE);
981 /* TODO: should verify if this service was already started */
983 /* connect to session bus */
984 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
987 g_warning ("Could not connect to dbus: %s", error->message);
988 g_error_free (error);
992 /* register dbus object */
993 dbus_g_connection_register_g_object (bus,
994 GMYTH_DBUS_SERVER_PATH, G_OBJECT (self));
996 proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS,
997 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
999 /* registering download manager service */
1000 if (!org_freedesktop_DBus_request_name (proxy, GMYTH_DBUS_SERVER_IFACE,
1001 0, &request_ret, &error))
1003 g_warning ("Unable to register dbus service: %d %s",
1004 error->code, error->message);
1005 g_error_free (error);
1009 if (request_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
1011 g_warning ("Got result code %u from requesting name", request_ret);
1018 g_object_unref (self);