hythmbox-2.98/plugins/daap/rb-dmap-container-db-adapter.c

No issues found

  1 /*
  2  *  Container / Playlist database adapter class for DMAP sharing
  3  *
  4  *  Copyright (C) 2008 W. Michael Petullo <mike@flyn.org>
  5  *
  6  *  This program is free software; you can redistribute it and/or modify
  7  *  it under the terms of the GNU General Public License as published by
  8  *  the Free Software Foundation; either version 2 of the License, or
  9  *  (at your option) any later version.
 10  *
 11  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 12  *  GStreamer plugins to be used and distributed together with GStreamer
 13  *  and Rhythmbox. This permission is above and beyond the permissions granted
 14  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 15  *  you may extend this exception to your version of the code, but you are not
 16  *  obligated to do so. If you do not wish to do so, delete this exception
 17  *  statement from your version.
 18  *
 19  *  This program is distributed in the hope that it will be useful,
 20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22  *  GNU General Public License for more details.
 23  *
 24  *  You should have received a copy of the GNU General Public License
 25  *  along with this program; if not, write to the Free Software
 26  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 27  *
 28  */
 29 
 30 #include "rb-playlist-manager.h"
 31 #include "rb-playlist-source.h"
 32 #include "rb-dmap-container-db-adapter.h"
 33 #include "rb-daap-container-record.h"
 34 
 35 #include <libdmapsharing/dmap.h>
 36 
 37 static guint next_playlist_id = 2;
 38 
 39 struct RBDMAPContainerDbAdapterPrivate {
 40 	RBPlaylistManager *playlist_manager;
 41 };
 42 
 43 typedef struct ForeachAdapterData {
 44 	gpointer data;
 45 	GHFunc func;
 46 } ForeachAdapterData;
 47 
 48 static guint find_by_id (gconstpointer a, gconstpointer b)
 49 {
 50 	return GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (a), "daap_id")) != GPOINTER_TO_UINT (b);
 51 }
 52 
 53 static DMAPContainerRecord *
 54 rb_dmap_container_db_adapter_lookup_by_id (DMAPContainerDb *db, guint id)
 55 {
 56 	gchar *name;
 57 	GList *playlists;
 58 	DMAPContainerRecord *fnval = NULL;
 59 
 60 	playlists = rb_playlist_manager_get_playlists (RB_DMAP_CONTAINER_DB_ADAPTER (db)->priv->playlist_manager);
 61 
 62 	if (playlists != NULL && playlists->data != NULL) {
 63 		GList *result;
 64 		result = g_list_find_custom (playlists, GINT_TO_POINTER (id), (GCompareFunc) find_by_id);
 65 		if (result != NULL && result->data != NULL) {
 66 			RBPlaylistSource *source;
 67 			source = RB_PLAYLIST_SOURCE (result->data);
 68 			g_object_get (source, "name", &name, NULL);
 69 			fnval = DMAP_CONTAINER_RECORD (rb_daap_container_record_new (name, source));
 70 		}
 71 	}
 72 
 73 	g_list_free (playlists);
 74 
 75 	return fnval;
 76 }
 77 
 78 static void
 79 foreach_adapter (RBPlaylistSource *entry, gpointer data)
 80 {
 81 	gchar *name;
 82 	DMAPContainerRecord *record;
 83 	ForeachAdapterData *foreach_adapter_data;
 84 
 85 	foreach_adapter_data = data;
 86 	g_object_get (entry, "name", &name, NULL);
 87 	record = DMAP_CONTAINER_RECORD (rb_daap_container_record_new (name, entry));
 88 
 89 	foreach_adapter_data->func (GINT_TO_POINTER (rb_daap_container_record_get_id (record)),
 90 				    record,
 91 				    foreach_adapter_data->data);
 92 
 93 	g_object_unref (record);
 94 }
 95 
 96 static void
 97 rb_dmap_container_db_adapter_foreach	(DMAPContainerDb *db,
 98 					 GHFunc func,
 99 				         gpointer data)
100 {
101 	ForeachAdapterData *foreach_adapter_data;
102 	GList *playlists;
103 
104 	playlists = rb_playlist_manager_get_playlists (RB_DMAP_CONTAINER_DB_ADAPTER (db)->priv->playlist_manager);
105 
106 	foreach_adapter_data = g_new (ForeachAdapterData, 1);
107 	foreach_adapter_data->data = data;
108 	foreach_adapter_data->func = func;
109 	g_list_foreach (playlists, (GFunc) foreach_adapter, foreach_adapter_data);
110 
111 	g_list_free (playlists);
112 	g_free (foreach_adapter_data);
113 }
114 
115 static gint64
116 rb_dmap_container_db_adapter_count (DMAPContainerDb *db)
117 {
118 	gint64 count = 0;
119 	GList *playlists = rb_playlist_manager_get_playlists (
120 		RB_DMAP_CONTAINER_DB_ADAPTER (db)->priv->playlist_manager);
121 	count = g_list_length (playlists);
122 	g_list_free (playlists);
123 	return count;
124 }
125 
126 static void
127 rb_dmap_container_db_adapter_init (RBDMAPContainerDbAdapter *db)
128 {
129 	db->priv = RB_DMAP_CONTAINER_DB_ADAPTER_GET_PRIVATE (db);
130 }
131 
132 static void
133 rb_dmap_container_db_adapter_class_init (RBDMAPContainerDbAdapterClass *klass)
134 {
135 	g_type_class_add_private (klass, sizeof (RBDMAPContainerDbAdapterPrivate));
136 }
137 
138 static void
139 rb_dmap_container_db_adapter_class_finalize (RBDMAPContainerDbAdapterClass *klass)
140 {
141 }
142 
143 static void
144 rb_dmap_container_db_adapter_interface_init (gpointer iface, gpointer data)
145 {
146 	DMAPContainerDbIface *dmap_db = iface;
147 
148 	g_assert (G_TYPE_FROM_INTERFACE (dmap_db) == DMAP_TYPE_CONTAINER_DB);
149 
150 	dmap_db->lookup_by_id = rb_dmap_container_db_adapter_lookup_by_id;
151 	dmap_db->foreach = rb_dmap_container_db_adapter_foreach;
152 	dmap_db->count = rb_dmap_container_db_adapter_count;
153 }
154 
155 G_DEFINE_DYNAMIC_TYPE_EXTENDED (RBDMAPContainerDbAdapter,
156 				rb_dmap_container_db_adapter,
157 				G_TYPE_OBJECT,
158 				0,
159 				G_IMPLEMENT_INTERFACE_DYNAMIC (DMAP_TYPE_CONTAINER_DB,
160 							       rb_dmap_container_db_adapter_interface_init))
161 
162 static void
163 assign_id (RBPlaylistManager *mgr,
164 	   RBSource *source)
165 {
166 	if (g_object_get_data (G_OBJECT (source), "daap_id") == NULL)
167 		g_object_set_data (G_OBJECT (source), "daap_id", GUINT_TO_POINTER (next_playlist_id++));
168 }
169 
170 RBDMAPContainerDbAdapter *
171 rb_dmap_container_db_adapter_new (RBPlaylistManager *playlist_manager)
172 {
173 	RBDMAPContainerDbAdapter *db;
174 	GList *playlists;
175 	
176 	playlists = rb_playlist_manager_get_playlists (playlist_manager);
177 
178 	/* These IDs are DAAP-specific, so they are not a part of the
179 	 * general-purpose RBPlaylistSource class:
180 	 */
181 	if (playlists != NULL && playlists->data != NULL) {
182 		GList *l;
183 		for (l = playlists; l != NULL; l = l->next) {
184 			assign_id (playlist_manager, RB_SOURCE (l->data));
185 		}
186 	}
187 	
188 	g_signal_connect (G_OBJECT (playlist_manager),
189 			 "playlist_created",
190 			  G_CALLBACK (assign_id),
191 			  NULL);
192 	
193 	g_signal_connect (G_OBJECT (playlist_manager),
194 			 "playlist_added",
195 			  G_CALLBACK (assign_id),
196 			  NULL);
197 
198 	db = RB_DMAP_CONTAINER_DB_ADAPTER (g_object_new (RB_TYPE_DMAP_CONTAINER_DB_ADAPTER,
199 					       NULL));
200 
201 	db->priv->playlist_manager = playlist_manager;
202 
203 	return db;
204 }
205 
206 void
207 _rb_dmap_container_db_adapter_register_type (GTypeModule *module)
208 {
209 	rb_dmap_container_db_adapter_register_type (module);
210 }