2006-06-07 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / daapsharing / rb-daap-sharing.c
blob0e00f276f12d520bcd89a14d093796ada2605138
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Implmentation of DAAP (iTunes Music Sharing) sharing
5 * Copyright (C) 2005 Charles Schmidt <cschmidt2@emich.edu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <string.h>
27 #include <glib/gi18n.h>
28 #include <glib/gprintf.h>
30 #include "rb-daap-sharing.h"
31 #include "rb-daap-share.h"
32 #include "rb-debug.h"
33 #include "rb-dialog.h"
34 #include "rb-playlist-manager.h"
35 #include "eel-gconf-extensions.h"
36 #include "rb-preferences.h"
38 static RBDAAPShare *share = NULL;
39 static guint enable_sharing_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
40 static guint require_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
41 static guint share_name_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
42 static guint share_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
44 static void
45 create_share (RBShell *shell)
47 RhythmDB *db;
48 RBPlaylistManager *playlist_manager;
49 char *name;
50 char *password;
51 gboolean require_password;
53 g_assert (share == NULL);
54 rb_debug ("initialize daap sharing\n");
56 name = eel_gconf_get_string (CONF_DAAP_SHARE_NAME);
58 if (name == NULL || *name == '\0') {
59 const gchar *real_name;
61 g_free (name);
63 real_name = g_get_real_name ();
64 if (strcmp (real_name, "Unknown") == 0) {
65 real_name = g_get_user_name ();
68 name = g_strdup_printf (_("%s's Music"), real_name);
69 eel_gconf_set_string (CONF_DAAP_SHARE_NAME, name);
72 g_object_get (G_OBJECT (shell), "db", &db, "playlist-manager", &playlist_manager, NULL);
74 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
75 if (require_password) {
76 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
77 } else {
78 password = NULL;
81 share = rb_daap_share_new (name, password, db, playlist_manager);
83 g_free (name);
84 g_free (password);
87 static void
88 enable_sharing_changed_cb (GConfClient *client,
89 guint cnxn_id,
90 GConfEntry *entry,
91 RBShell *shell)
93 gboolean enabled;
95 enabled = eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING);
97 if (enabled) {
98 if (share == NULL) {
99 create_share (shell);
101 } else {
102 rb_debug ("shutdown daap sharing");
104 if (share) {
105 g_object_unref (share);
107 share = NULL;
111 static void
112 require_password_changed_cb (GConfClient *client,
113 guint cnxn_id,
114 GConfEntry *entry,
115 RBShell *shell)
117 gboolean required;
118 char *password;
120 if (share == NULL) {
121 return;
124 required = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
126 if (required) {
127 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
128 } else {
129 password = NULL;
132 g_object_set (G_OBJECT (share), "password", password, NULL);
133 g_free (password);
136 static void
137 share_name_changed_cb (GConfClient *client,
138 guint cnxn_id,
139 GConfEntry *entry,
140 RBShell *shell)
142 char *name;
144 if (share == NULL) {
145 return;
148 name = eel_gconf_get_string (CONF_DAAP_SHARE_NAME);
149 g_object_set (G_OBJECT (share), "name", name, NULL);
150 g_free (name);
153 static void
154 share_password_changed_cb (GConfClient *client,
155 guint cnxn_id,
156 GConfEntry *entry,
157 RBShell *shell)
159 gboolean require_password;
160 char *password;
162 if (share == NULL) {
163 return;
166 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
168 /* Don't do anything unless we require a password */
169 if (! require_password) {
170 return;
173 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
174 g_object_set (G_OBJECT (share), "password", password, NULL);
175 g_free (password);
179 void
180 rb_daap_sharing_init (RBShell *shell)
182 g_object_ref (shell);
184 if (eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING)) {
185 create_share (shell);
188 enable_sharing_notify_id =
189 eel_gconf_notification_add (CONF_DAAP_ENABLE_SHARING,
190 (GConfClientNotifyFunc) enable_sharing_changed_cb,
191 shell);
192 require_password_notify_id =
193 eel_gconf_notification_add (CONF_DAAP_REQUIRE_PASSWORD,
194 (GConfClientNotifyFunc) require_password_changed_cb,
195 shell);
196 share_name_notify_id =
197 eel_gconf_notification_add (CONF_DAAP_SHARE_NAME,
198 (GConfClientNotifyFunc) share_name_changed_cb,
199 shell);
200 share_password_notify_id =
201 eel_gconf_notification_add (CONF_DAAP_SHARE_PASSWORD,
202 (GConfClientNotifyFunc) share_password_changed_cb,
203 shell);
206 void
207 rb_daap_sharing_shutdown (RBShell *shell)
209 if (share) {
210 rb_debug ("shutdown daap sharing");
212 g_object_unref (share);
213 share = NULL;
216 if (enable_sharing_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
217 eel_gconf_notification_remove (enable_sharing_notify_id);
218 enable_sharing_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
220 if (require_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
221 eel_gconf_notification_remove (require_password_notify_id);
222 require_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
224 if (share_name_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
225 eel_gconf_notification_remove (share_name_notify_id);
226 share_name_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
228 if (share_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
229 eel_gconf_notification_remove (share_password_notify_id);
230 share_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
233 g_object_unref (shell);