Updated Finnish translation
[rhythmbox.git] / daapsharing / rb-daap-sharing.c
blob043592fce7b46e238cce7a95e234df33e1086c68
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 (shell,
73 "db", &db,
74 "playlist-manager", &playlist_manager, NULL);
76 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
77 if (require_password) {
78 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
79 } else {
80 password = NULL;
83 share = rb_daap_share_new (name, password, db, playlist_manager);
85 g_object_unref (db);
86 g_object_unref (playlist_manager);
88 g_free (name);
89 g_free (password);
92 static void
93 enable_sharing_changed_cb (GConfClient *client,
94 guint cnxn_id,
95 GConfEntry *entry,
96 RBShell *shell)
98 gboolean enabled;
100 enabled = eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING);
102 if (enabled) {
103 if (share == NULL) {
104 create_share (shell);
106 } else {
107 rb_debug ("shutdown daap sharing");
109 if (share) {
110 g_object_unref (share);
112 share = NULL;
116 static void
117 require_password_changed_cb (GConfClient *client,
118 guint cnxn_id,
119 GConfEntry *entry,
120 RBShell *shell)
122 gboolean required;
123 char *password;
125 if (share == NULL) {
126 return;
129 required = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
131 if (required) {
132 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
133 } else {
134 password = NULL;
137 g_object_set (G_OBJECT (share), "password", password, NULL);
138 g_free (password);
141 static void
142 share_name_changed_cb (GConfClient *client,
143 guint cnxn_id,
144 GConfEntry *entry,
145 RBShell *shell)
147 char *name;
149 if (share == NULL) {
150 return;
153 name = eel_gconf_get_string (CONF_DAAP_SHARE_NAME);
154 g_object_set (G_OBJECT (share), "name", name, NULL);
155 g_free (name);
158 static void
159 share_password_changed_cb (GConfClient *client,
160 guint cnxn_id,
161 GConfEntry *entry,
162 RBShell *shell)
164 gboolean require_password;
165 char *password;
167 if (share == NULL) {
168 return;
171 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
173 /* Don't do anything unless we require a password */
174 if (! require_password) {
175 return;
178 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
179 g_object_set (G_OBJECT (share), "password", password, NULL);
180 g_free (password);
183 void
184 rb_daap_sharing_init (RBShell *shell)
186 g_object_ref (shell);
188 if (eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING)) {
189 create_share (shell);
192 enable_sharing_notify_id =
193 eel_gconf_notification_add (CONF_DAAP_ENABLE_SHARING,
194 (GConfClientNotifyFunc) enable_sharing_changed_cb,
195 shell);
196 require_password_notify_id =
197 eel_gconf_notification_add (CONF_DAAP_REQUIRE_PASSWORD,
198 (GConfClientNotifyFunc) require_password_changed_cb,
199 shell);
200 share_name_notify_id =
201 eel_gconf_notification_add (CONF_DAAP_SHARE_NAME,
202 (GConfClientNotifyFunc) share_name_changed_cb,
203 shell);
204 share_password_notify_id =
205 eel_gconf_notification_add (CONF_DAAP_SHARE_PASSWORD,
206 (GConfClientNotifyFunc) share_password_changed_cb,
207 shell);
210 void
211 rb_daap_sharing_shutdown (RBShell *shell)
213 if (share) {
214 rb_debug ("shutdown daap sharing");
216 g_object_unref (share);
217 share = NULL;
220 if (enable_sharing_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
221 eel_gconf_notification_remove (enable_sharing_notify_id);
222 enable_sharing_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
224 if (require_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
225 eel_gconf_notification_remove (require_password_notify_id);
226 require_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
228 if (share_name_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
229 eel_gconf_notification_remove (share_name_notify_id);
230 share_name_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
232 if (share_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
233 eel_gconf_notification_remove (share_password_notify_id);
234 share_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
237 g_object_unref (shell);