1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  *  Implmentation of DAAP (iTunes Music Sharing) sharing
4  *
5  *  Copyright (C) 2005 Charles Schmidt <cschmidt2@emich.edu>
6  *
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.
11  *
12  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
13  *  GStreamer plugins to be used and distributed together with GStreamer
14  *  and Rhythmbox. This permission is above and beyond the permissions granted
15  *  by the GPL license by which Rhythmbox is covered. If you modify this code
16  *  you may extend this exception to your version of the code, but you are not
17  *  obligated to do so. If you do not wish to do so, delete this exception
18  *  statement from your version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
28  *
29  */
30 
31 #include "config.h"
32 
33 #include <string.h>
34 
35 #include <gio/gio.h>
36 #include <glib/gi18n.h>
37 #include <glib/gprintf.h>
38 
39 #include "rb-daap-sharing.h"
40 #include "rb-rhythmdb-dmap-db-adapter.h"
41 #include "rb-dmap-container-db-adapter.h"
42 #include "rb-debug.h"
43 #include "rb-dialog.h"
44 #include "rb-playlist-manager.h"
45 
46 #include <libdmapsharing/dmap.h>
47 
48 static DAAPShare *share = NULL;
49 static GSettings *settings = NULL;
50 
51 char *
rb_daap_sharing_default_share_name()52 rb_daap_sharing_default_share_name ()
53 {
54 	const gchar *real_name;
55 
56 	real_name = g_get_real_name ();
57 	if (strcmp (real_name, "Unknown") == 0) {
58 		real_name = g_get_user_name ();
59 	}
60 
61 	return g_strdup_printf (_("%s's Music"), real_name);
62 }
63 
64 static gboolean
share_name_get_mapping(GValue * value,GVariant * variant,gpointer data)65 share_name_get_mapping (GValue *value, GVariant *variant, gpointer data)
66 {
67 	const char *name;
68 	name = g_variant_get_string (variant, NULL);
69 
70 	if (name != NULL || name[0] != '\0') {
71 		g_value_set_string (value, name);
72 	} else {
73 		g_value_take_string (value, rb_daap_sharing_default_share_name ());
74 	}
75 	return TRUE;
76 }
77 
78 static void
create_share(RBShell * shell)79 create_share (RBShell *shell)
80 {
81 	RhythmDB *rdb;
82 	DMAPDb *db;
83 	DMAPContainerDb *container_db;
84 	RBPlaylistManager *playlist_manager;
85 	char *name;
86 	char *password;
87 	gboolean require_password;
88 
89 	g_assert (share == NULL);
90 	rb_debug ("initialize daap sharing");
91 
92 	name = g_settings_get_string (settings, "share-name");
93 	if (name == NULL || *name == '\0') {
94 		g_free (name);
95 		name = rb_daap_sharing_default_share_name ();
96 	}
97 
98 	g_object_get (shell,
99 		      "db", &rdb,
100 		      "playlist-manager", &playlist_manager, NULL);
101 	db = DMAP_DB (rb_rhythmdb_dmap_db_adapter_new (rdb, RHYTHMDB_ENTRY_TYPE_SONG));
102 	container_db = DMAP_CONTAINER_DB (rb_dmap_container_db_adapter_new (playlist_manager));
103 
104 	require_password = g_settings_get_boolean (settings, "require-password");
105 	if (require_password) {
106 		password = g_settings_get_string (settings, "share-password");
107 	} else {
108 		password = NULL;
109 	}
110 
111 	share = daap_share_new (name, password, db, container_db, NULL);
112 
113 	g_settings_bind_with_mapping (settings, "share-name",
114 				      share, "name",
115 				      G_SETTINGS_BIND_GET,
116 				      share_name_get_mapping, NULL,
117 				      NULL, NULL);
118 	if (g_settings_get_boolean (settings, "require-password")) {
119 		g_settings_bind (settings, "share-password", share, "password", G_SETTINGS_BIND_DEFAULT);
120 	}
121 
122 	g_object_unref (db);
123 	g_object_unref (container_db);
124 	g_object_unref (rdb);
125 	g_object_unref (playlist_manager);
126 
127 	g_free (name);
128 	g_free (password);
129 }
130 
131 static void
sharing_settings_changed_cb(GSettings * settings,const char * key,RBShell * shell)132 sharing_settings_changed_cb (GSettings *settings, const char *key, RBShell *shell)
133 {
134 	if (g_strcmp0 (key, "enable-sharing") == 0) {
135 		gboolean enabled;
136 
137 		enabled = g_settings_get_boolean (settings, key);
138 		if (enabled) {
139 			if (share == NULL) {
140 				create_share (shell);
141 			}
142 		} else {
143 			if (share != NULL) {
144 				rb_debug ("shutting down daap share");
145 				g_object_unref (share);
146 				share = NULL;
147 			}
148 		}
149 	} else if (g_strcmp0 (key, "require-password") == 0) {
150 
151 		if (share != NULL) {
152 			if (g_settings_get_boolean (settings, key)) {
153 				g_settings_bind (settings, "share-password", share, "password", G_SETTINGS_BIND_DEFAULT);
154 			} else {
155 				g_settings_unbind (share, "password");
156 				g_object_set (share, "password", NULL, NULL);
157 			}
158 		}
159 	}
160 }
161 
162 void
rb_daap_sharing_init(RBShell * shell)163 rb_daap_sharing_init (RBShell *shell)
164 {
165 	g_object_ref (shell);
166 
167 	settings = g_settings_new ("org.gnome.rhythmbox.sharing");
168 
169 	if (g_settings_get_boolean (settings, "enable-sharing")) {
170 		create_share (shell);
171 	}
172 
173 	g_signal_connect_object (settings, "changed", G_CALLBACK (sharing_settings_changed_cb), shell, 0);
174 }
175 
176 void
rb_daap_sharing_shutdown(RBShell * shell)177 rb_daap_sharing_shutdown (RBShell *shell)
178 {
179 	if (share) {
180 		rb_debug ("shutdown daap sharing");
181 
182 		g_object_unref (share);
183 		share = NULL;
184 	}
185 
186 	if (settings) {
187 		g_object_unref (settings);
188 		settings = NULL;
189 	}
190 
191 	g_object_unref (shell);
192 }
193