udapted vi.po
[rhythmbox.git] / daapsharing / rb-daap-sharing.c
blob5bc361d3378d6bfbadb495dc9ab7b682fd893cef
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 char *
45 rb_daap_sharing_default_share_name ()
47 const gchar *real_name;
49 real_name = g_get_real_name ();
50 if (strcmp (real_name, "Unknown") == 0) {
51 real_name = g_get_user_name ();
54 return g_strdup_printf (_("%s's Music"), real_name);
57 static void
58 create_share (RBShell *shell)
60 RhythmDB *db;
61 RBPlaylistManager *playlist_manager;
62 char *name;
63 char *password;
64 gboolean require_password;
66 g_assert (share == NULL);
67 rb_debug ("initialize daap sharing");
69 name = eel_gconf_get_string (CONF_DAAP_SHARE_NAME);
70 if (name == NULL || *name == '\0') {
71 g_free (name);
72 name = rb_daap_sharing_default_share_name ();
75 g_object_get (shell,
76 "db", &db,
77 "playlist-manager", &playlist_manager, NULL);
79 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
80 if (require_password) {
81 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
82 } else {
83 password = NULL;
86 share = rb_daap_share_new (name, password, db, RHYTHMDB_ENTRY_TYPE_SONG, playlist_manager);
88 g_object_unref (db);
89 g_object_unref (playlist_manager);
91 g_free (name);
92 g_free (password);
95 static void
96 enable_sharing_changed_cb (GConfClient *client,
97 guint cnxn_id,
98 GConfEntry *entry,
99 RBShell *shell)
101 gboolean enabled;
103 enabled = eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING);
105 if (enabled) {
106 if (share == NULL) {
107 create_share (shell);
109 } else {
110 rb_debug ("shutdown daap sharing");
112 if (share) {
113 g_object_unref (share);
115 share = NULL;
119 static void
120 require_password_changed_cb (GConfClient *client,
121 guint cnxn_id,
122 GConfEntry *entry,
123 RBShell *shell)
125 gboolean required;
126 char *password;
128 if (share == NULL) {
129 return;
132 required = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
134 if (required) {
135 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
136 } else {
137 password = NULL;
140 g_object_set (G_OBJECT (share), "password", password, NULL);
141 g_free (password);
144 static void
145 share_name_changed_cb (GConfClient *client,
146 guint cnxn_id,
147 GConfEntry *entry,
148 RBShell *shell)
150 char *name;
152 if (share == NULL) {
153 return;
156 name = eel_gconf_get_string (CONF_DAAP_SHARE_NAME);
157 if (name == NULL || name[0] == '\0') {
158 g_free (name);
159 name = rb_daap_sharing_default_share_name ();
162 g_object_set (G_OBJECT (share), "name", name, NULL);
163 g_free (name);
166 static void
167 share_password_changed_cb (GConfClient *client,
168 guint cnxn_id,
169 GConfEntry *entry,
170 RBShell *shell)
172 gboolean require_password;
173 char *password;
175 if (share == NULL) {
176 return;
179 require_password = eel_gconf_get_boolean (CONF_DAAP_REQUIRE_PASSWORD);
181 /* Don't do anything unless we require a password */
182 if (! require_password) {
183 return;
186 password = eel_gconf_get_string (CONF_DAAP_SHARE_PASSWORD);
187 g_object_set (G_OBJECT (share), "password", password, NULL);
188 g_free (password);
191 void
192 rb_daap_sharing_init (RBShell *shell)
194 g_object_ref (shell);
196 if (eel_gconf_get_boolean (CONF_DAAP_ENABLE_SHARING)) {
197 create_share (shell);
200 enable_sharing_notify_id =
201 eel_gconf_notification_add (CONF_DAAP_ENABLE_SHARING,
202 (GConfClientNotifyFunc) enable_sharing_changed_cb,
203 shell);
204 require_password_notify_id =
205 eel_gconf_notification_add (CONF_DAAP_REQUIRE_PASSWORD,
206 (GConfClientNotifyFunc) require_password_changed_cb,
207 shell);
208 share_name_notify_id =
209 eel_gconf_notification_add (CONF_DAAP_SHARE_NAME,
210 (GConfClientNotifyFunc) share_name_changed_cb,
211 shell);
212 share_password_notify_id =
213 eel_gconf_notification_add (CONF_DAAP_SHARE_PASSWORD,
214 (GConfClientNotifyFunc) share_password_changed_cb,
215 shell);
218 void
219 rb_daap_sharing_shutdown (RBShell *shell)
221 if (share) {
222 rb_debug ("shutdown daap sharing");
224 g_object_unref (share);
225 share = NULL;
228 if (enable_sharing_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
229 eel_gconf_notification_remove (enable_sharing_notify_id);
230 enable_sharing_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
232 if (require_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
233 eel_gconf_notification_remove (require_password_notify_id);
234 require_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
236 if (share_name_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
237 eel_gconf_notification_remove (share_name_notify_id);
238 share_name_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
240 if (share_password_notify_id != EEL_GCONF_UNDEFINED_CONNECTION) {
241 eel_gconf_notification_remove (share_password_notify_id);
242 share_password_notify_id = EEL_GCONF_UNDEFINED_CONNECTION;
245 g_object_unref (shell);