watc-movie-service: Added WATC movie service
[maevies] / src / mvs-watc-movie-service.c
1 /*
2  * mvs-watc-movie-service.c
3  *
4  * This file is part of maevies
5  * Copyright (C) 2010 Simón Pena <spenap@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  */
18
19 #include <dbus/dbus-glib-bindings.h>
20
21 #include "mvs-watc-movie-service.h"
22
23 #define WATC_MOVIE_SERVICE_OBJECT_PATH "/WATCMovie"
24 #define WATC_MOVIE_SERVICE_NAME "com.simonpena.maevies.watcmovie"
25
26 G_DEFINE_TYPE (MvsWatcMovieService, mvs_watc_movie_service, G_TYPE_OBJECT)
27
28 enum {
29         PROP_0,
30         PROP_ID,
31         PROP_DBUSGCONN,
32 };
33
34 #define GET_PRIVATE(o) \
35         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MVS_TYPE_WATC_MOVIE_SERVICE, MvsWatcMovieServicePrivate))
36
37 struct _MvsWatcMovieServicePrivate {
38         MvsWatcMovie *movie;
39         DBusGConnection *connection;
40         gchar *suffix;
41 };
42
43 gboolean
44 mvs_watc_movie_service_get_name (MvsWatcMovieService *self, gchar **title,
45                                   GError **error)
46 {
47         *title = g_strdup (mvs_watc_movie_get_name (self->priv->movie));
48         return *title != NULL;
49 }
50
51 gboolean
52 mvs_watc_movie_service_get_year (MvsWatcMovieService *self, gchar **year,
53                                  GError **error)
54 {
55         *year = g_strdup (mvs_watc_movie_get_year(self->priv->movie));
56         return *year != NULL;
57 }
58
59 gboolean
60 mvs_watc_movie_service_get_stingers (MvsWatcMovieService *self, int *stingers,
61                                      GError **error)
62 {
63         *stingers = mvs_watc_movie_get_stingers (self->priv->movie);
64         return TRUE;
65 }
66
67 #include "mvs-watc-movie-service-glue.h"
68
69 static void
70 setup_dbus (MvsWatcMovieService *self)
71 {
72         DBusGProxy *proxy;
73         guint request_name_result;
74         GError *error = NULL;
75         gchar *object_path = NULL;
76
77         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
78                                            DBUS_SERVICE_DBUS,
79                                            DBUS_PATH_DBUS,
80                                            DBUS_INTERFACE_DBUS);
81
82         if (!org_freedesktop_DBus_request_name (proxy,
83                                                 WATC_MOVIE_SERVICE_NAME,
84                                                 0, &request_name_result,
85                                                 &error)) {
86                 g_warning ("Unable to register service: %s", error->message);
87                 g_error_free (error);
88         }
89
90         object_path = g_strdup_printf (WATC_MOVIE_SERVICE_OBJECT_PATH "/%s",
91                         self->priv->suffix);
92
93         dbus_g_connection_register_g_object (self->priv->connection,
94                                              object_path,
95                                              G_OBJECT (self));
96
97         g_free (object_path);
98         g_object_unref (proxy);
99 }
100
101 static void
102 mvs_watc_movie_service_set_property (GObject *object, guint property_id,
103                                   const GValue *value, GParamSpec *pspec)
104 {
105         MvsWatcMovieService *self = MVS_WATC_MOVIE_SERVICE (object);
106
107         switch (property_id) {
108         case PROP_DBUSGCONN:
109                 if (!self->priv->connection) {
110                         DBusGConnection *tmp = g_value_get_pointer (value);
111                         if (tmp) {
112                                 self->priv->connection =
113                                         dbus_g_connection_ref (tmp);
114                                 setup_dbus (self);
115                         }
116                 }
117                 g_assert (self->priv->connection);
118                 break;
119         default:
120                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
121         }
122 }
123
124 static void
125 mvs_watc_movie_service_finalize (GObject *object)
126 {
127         MvsWatcMovieService *self = MVS_WATC_MOVIE_SERVICE (object);
128
129         if (self->priv->connection) {
130                 dbus_g_connection_unref (self->priv->connection);
131         }
132         g_free (self->priv->suffix);
133         g_object_unref (self->priv->movie);
134         G_OBJECT_CLASS (mvs_watc_movie_service_parent_class)->finalize (object);
135 }
136
137 static void
138 mvs_watc_movie_service_class_init (MvsWatcMovieServiceClass *klass)
139 {
140         GObjectClass *object_class = G_OBJECT_CLASS (klass);
141
142         g_type_class_add_private (klass, sizeof (MvsWatcMovieServicePrivate));
143
144         object_class->set_property = mvs_watc_movie_service_set_property;
145         object_class->finalize = mvs_watc_movie_service_finalize;
146
147         g_object_class_install_property
148                 (object_class, PROP_DBUSGCONN,
149                  g_param_spec_pointer ("connection", "DBusGConnection",
150                                        "DBus GConnection",
151                                        G_PARAM_WRITABLE));
152
153          dbus_g_object_type_install_info (MVS_TYPE_WATC_MOVIE_SERVICE,
154                                           &dbus_glib_mvs_watc_movie_service_object_info);
155 }
156
157 static void
158 mvs_watc_movie_service_init (MvsWatcMovieService *self)
159 {
160         self->priv = GET_PRIVATE (self);
161         self->priv->movie = NULL;
162         self->priv->connection = NULL;
163         self->priv->suffix = NULL;
164 }
165
166 MvsWatcMovieService*
167 mvs_watc_movie_service_new (DBusGConnection *connection,
168                 MvsWatcMovie *movie, const gchar *suffix)
169 {
170         MvsWatcMovieService *instance = g_object_new (MVS_TYPE_WATC_MOVIE_SERVICE, NULL);
171         instance->priv->suffix = g_strdup(suffix);
172         g_object_set (instance, "connection", connection, NULL);
173         instance->priv->movie = movie;
174         return instance;
175 }