9c2bfa6d251ef031663308cfd6c735bdc9466929
[maevies] / src / mvs-tmdb-movie-service.c
1 /*
2  * mvs-tmdb-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-tmdb-movie-service.h"
22
23 #define TMDB_MOVIE_SERVICE_OBJECT_PATH "/TMDBMovie"
24 #define TMDB_MOVIE_SERVICE_NAME "com.simonpena.maevies.tmdbmovie"
25
26 G_DEFINE_TYPE (MvsTMDBMovieService, mvs_tmdb_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_TMDB_MOVIE_SERVICE, MvsTMDBMovieServicePrivate))
36
37 struct _MvsTMDBMovieServicePrivate {
38         MvsTmdbMovie *movie;
39         DBusGConnection *connection;
40         gchar *suffix;
41 };
42
43 gboolean
44 mvs_tmdb_movie_service_get_title (MvsTMDBMovieService *self, gchar **title,
45                                   GError **error)
46 {
47         *title = g_strdup (mvs_tmdb_movie_get_name (self->priv->movie));
48         return *title != NULL;
49 }
50
51 #include "mvs-tmdb-movie-service-glue.h"
52
53 static void
54 setup_dbus (MvsTMDBMovieService *self)
55 {
56         DBusGProxy *proxy;
57         guint request_name_result;
58         GError *error = NULL;
59         gchar *object_path = NULL;
60
61         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
62                                            DBUS_SERVICE_DBUS,
63                                            DBUS_PATH_DBUS,
64                                            DBUS_INTERFACE_DBUS);
65
66         if (!org_freedesktop_DBus_request_name (proxy,
67                                                 TMDB_MOVIE_SERVICE_NAME,
68                                                 0, &request_name_result,
69                                                 &error)) {
70                 g_warning ("Unable to register service: %s", error->message);
71                 g_error_free (error);
72         }
73
74         object_path = g_strdup_printf (TMDB_MOVIE_SERVICE_OBJECT_PATH "/%s",
75                         self->priv->suffix);
76
77         dbus_g_connection_register_g_object (self->priv->connection,
78                                              object_path,
79                                              G_OBJECT (self));
80
81         g_free (object_path);
82         g_object_unref (proxy);
83 }
84
85 static void
86 mvs_tmdb_movie_service_set_property (GObject *object, guint property_id,
87                                   const GValue *value, GParamSpec *pspec)
88 {
89         MvsTMDBMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
90
91         switch (property_id) {
92         case PROP_DBUSGCONN:
93                 if (!self->priv->connection) {
94                         DBusGConnection *tmp = g_value_get_pointer (value);
95                         if (tmp) {
96                                 self->priv->connection =
97                                         dbus_g_connection_ref (tmp);
98                                 setup_dbus (self);
99                         }
100                 }
101                 g_assert (self->priv->connection);
102                 break;
103         default:
104                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105         }
106 }
107
108 static void
109 mvs_tmdb_movie_service_finalize (GObject *object)
110 {
111         MvsTMDBMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
112
113         if (self->priv->connection) {
114                 dbus_g_connection_unref (self->priv->connection);
115         }
116         g_free (self->priv->suffix);
117         g_object_unref (self->priv->movie);
118         G_OBJECT_CLASS (mvs_tmdb_movie_service_parent_class)->finalize (object);
119 }
120
121 static void
122 mvs_tmdb_movie_service_class_init (MvsTMDBMovieServiceClass *klass)
123 {
124         GObjectClass *object_class = G_OBJECT_CLASS (klass);
125
126         g_type_class_add_private (klass, sizeof (MvsTMDBMovieServicePrivate));
127
128         object_class->set_property = mvs_tmdb_movie_service_set_property;
129         object_class->finalize = mvs_tmdb_movie_service_finalize;
130
131         g_object_class_install_property
132                 (object_class, PROP_DBUSGCONN,
133                  g_param_spec_pointer ("connection", "DBusGConnection",
134                                        "DBus GConnection",
135                                        G_PARAM_WRITABLE));
136
137          dbus_g_object_type_install_info (MVS_TYPE_TMDB_MOVIE_SERVICE,
138                                           &dbus_glib_mvs_tmdb_movie_service_object_info);
139 }
140
141 static void
142 mvs_tmdb_movie_service_init (MvsTMDBMovieService *self)
143 {
144         self->priv = GET_PRIVATE (self);
145         self->priv->movie = NULL;
146         self->priv->connection = NULL;
147         self->priv->suffix = NULL;
148 }
149
150 MvsTMDBMovieService*
151 mvs_tmdb_movie_service_new (DBusGConnection *connection,
152                 MvsTmdbMovie *movie, const gchar *suffix)
153 {
154         MvsTMDBMovieService *instance = g_object_new (MVS_TYPE_TMDB_MOVIE_SERVICE, NULL);
155         instance->priv->suffix = g_strdup(suffix);
156         g_object_set (instance, "connection", connection, NULL);
157         instance->priv->movie = movie;
158         return instance;
159 }