~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to sources/rb-static-playlist-source.c

Tags: upstream-0.9.2cvs20060102
ImportĀ upstreamĀ versionĀ 0.9.2cvs20060102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 *  Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
 
4
 *  Copyright (C) 2003 Colin Walters <walters@gnome.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
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include <gtk/gtk.h>
 
24
#include <libgnome/gnome-i18n.h>
 
25
#include <libgnomevfs/gnome-vfs-uri.h>
 
26
#include <libxml/tree.h>
 
27
 
 
28
#include "rb-static-playlist-source.h"
 
29
#include "rb-util.h"
 
30
#include "rb-debug.h"
 
31
#include "rb-stock-icons.h"
 
32
#include "rb-file-helpers.h"
 
33
#include "rb-playlist-xml.h"
 
34
 
 
35
static GObject *rb_static_playlist_source_constructor (GType type, guint n_construct_properties,
 
36
                                                       GObjectConstructParam *construct_properties);
 
37
 
 
38
/* source methods */
 
39
static GdkPixbuf *impl_get_pixbuf (RBSource *source);
 
40
static GList * impl_cut (RBSource *source);
 
41
static void impl_paste (RBSource *asource, GList *entries);
 
42
static void impl_delete (RBSource *source);
 
43
static gboolean impl_receive_drag (RBSource *source, GtkSelectionData *data);
 
44
 
 
45
/* playlist methods */
 
46
static void impl_save_contents_to_xml (RBPlaylistSource *source,
 
47
                                       xmlNodePtr node);
 
48
 
 
49
 
 
50
static void rb_static_playlist_source_add_list_uri (RBStaticPlaylistSource *source,
 
51
                                                    GList *list);
 
52
static void rb_static_playlist_source_row_inserted (GtkTreeModel *model,
 
53
                                                    GtkTreePath *path,
 
54
                                                    GtkTreeIter *iter,
 
55
                                                    RBStaticPlaylistSource *source);
 
56
static void rb_static_playlist_source_non_entry_dropped (GtkTreeModel *model,
 
57
                                                         const char *uri,
 
58
                                                         int position,
 
59
                                                         RBStaticPlaylistSource *source);
 
60
 
 
61
G_DEFINE_TYPE (RBStaticPlaylistSource, rb_static_playlist_source, RB_TYPE_PLAYLIST_SOURCE)
 
62
 
 
63
static void
 
64
rb_static_playlist_source_class_init (RBStaticPlaylistSourceClass *klass)
 
65
{
 
66
        gint size;
 
67
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
68
        RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
 
69
        RBPlaylistSourceClass *playlist_class = RB_PLAYLIST_SOURCE_CLASS (klass);
 
70
        
 
71
        object_class->constructor = rb_static_playlist_source_constructor;
 
72
 
 
73
        source_class->impl_can_cut = (RBSourceFeatureFunc) rb_true_function;
 
74
        source_class->impl_can_copy = (RBSourceFeatureFunc) rb_true_function;
 
75
        source_class->impl_can_delete = (RBSourceFeatureFunc) rb_true_function;
 
76
        source_class->impl_cut = impl_cut;
 
77
        source_class->impl_paste = impl_paste;
 
78
        source_class->impl_delete = impl_delete;
 
79
        source_class->impl_get_pixbuf = impl_get_pixbuf;
 
80
        source_class->impl_receive_drag = impl_receive_drag;
 
81
 
 
82
        playlist_class->impl_save_contents_to_xml = impl_save_contents_to_xml;
 
83
        
 
84
        gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &size, NULL);
 
85
        klass->pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
 
86
                                                  GNOME_MEDIA_PLAYLIST,
 
87
                                                  size,
 
88
                                                  0, NULL);
 
89
}
 
90
 
 
91
static void
 
92
rb_static_playlist_source_init (RBStaticPlaylistSource *source)
 
93
{
 
94
}
 
95
 
 
96
static GObject *
 
97
rb_static_playlist_source_constructor (GType type, guint n_construct_properties,
 
98
                                       GObjectConstructParam *construct_properties)
 
99
{
 
100
        GObjectClass *parent_class = G_OBJECT_CLASS (rb_static_playlist_source_parent_class);
 
101
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (
 
102
                        parent_class->constructor (type, n_construct_properties, construct_properties));
 
103
        RhythmDBQueryModel *model = rb_playlist_source_get_query_model (RB_PLAYLIST_SOURCE (source));
 
104
 
 
105
        /* watch these to find out when things are dropped into the entry view */
 
106
        g_signal_connect_object (G_OBJECT (model), "row-inserted",
 
107
                         G_CALLBACK (rb_static_playlist_source_row_inserted),
 
108
                         source, 0);
 
109
        g_signal_connect_object (G_OBJECT (model), "non-entry-dropped",
 
110
                         G_CALLBACK (rb_static_playlist_source_non_entry_dropped),
 
111
                         source, 0);
 
112
 
 
113
        return G_OBJECT (source);
 
114
}
 
115
 
 
116
RBSource *
 
117
rb_static_playlist_source_new (RBShell *shell, const char *name, gboolean local, RhythmDBEntryType entry_type)
 
118
{
 
119
        if (name == NULL)
 
120
                name = "";
 
121
 
 
122
        return RB_SOURCE (g_object_new (RB_TYPE_STATIC_PLAYLIST_SOURCE,
 
123
                                        "name", name,
 
124
                                        "shell", shell,
 
125
                                        "is-local", local,
 
126
                                        "entry-type", entry_type,
 
127
                                        NULL));
 
128
}
 
129
 
 
130
RBSource *      
 
131
rb_static_playlist_source_new_from_xml (RBShell *shell, xmlNodePtr node)
 
132
{
 
133
        RBSource *psource = rb_static_playlist_source_new (shell,
 
134
                                                           NULL,
 
135
                                                           TRUE,
 
136
                                                           RHYTHMDB_ENTRY_TYPE_SONG);
 
137
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (psource);
 
138
        xmlNodePtr child;
 
139
 
 
140
        for (child = node->children; child; child = child->next) {
 
141
                xmlChar *location;
 
142
 
 
143
                if (xmlNodeIsText (child))
 
144
                        continue;
 
145
        
 
146
                if (xmlStrcmp (child->name, RB_PLAYLIST_LOCATION))
 
147
                        continue;
 
148
        
 
149
                location = xmlNodeGetContent (child);
 
150
                rb_static_playlist_source_add_location (source,
 
151
                                                        (char *) location, -1);
 
152
        }
 
153
 
 
154
        return RB_SOURCE (source);
 
155
}
 
156
 
 
157
static GdkPixbuf *
 
158
impl_get_pixbuf (RBSource *asource)
 
159
{
 
160
        RBStaticPlaylistSourceClass *klass = RB_STATIC_PLAYLIST_SOURCE_GET_CLASS (asource);
 
161
        return klass->pixbuf;
 
162
}
 
163
 
 
164
 
 
165
static GList *
 
166
impl_cut (RBSource *asource)
 
167
{
 
168
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (asource);
 
169
        RBEntryView *songs = rb_source_get_entry_view (asource);
 
170
        GList *sel = rb_entry_view_get_selected_entries (songs);
 
171
        GList *tem;
 
172
 
 
173
        for (tem = sel; tem; tem = tem->next)
 
174
                rb_static_playlist_source_remove_entry (source, (RhythmDBEntry *) tem->data);
 
175
 
 
176
        return sel;
 
177
}
 
178
 
 
179
static void
 
180
impl_paste (RBSource *asource, GList *entries)
 
181
{
 
182
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (asource);
 
183
 
 
184
        for (; entries; entries = g_list_next (entries))
 
185
                rb_static_playlist_source_add_entry (source, entries->data, -1);
 
186
}
 
187
 
 
188
static void
 
189
impl_delete (RBSource *asource)
 
190
{
 
191
        RBEntryView *songs = rb_source_get_entry_view (asource);
 
192
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (asource);
 
193
        GList *sel, *tem;
 
194
 
 
195
        sel = rb_entry_view_get_selected_entries (songs);
 
196
        for (tem = sel; tem != NULL; tem = tem->next) {
 
197
                rb_static_playlist_source_remove_entry (source, (RhythmDBEntry *) tem->data);
 
198
        }
 
199
        g_list_free (sel);
 
200
}
 
201
 
 
202
static gboolean
 
203
impl_receive_drag (RBSource *asource, GtkSelectionData *data)
 
204
{
 
205
        GList *list;
 
206
        RBStaticPlaylistSource *source = RB_STATIC_PLAYLIST_SOURCE (asource);
 
207
 
 
208
        if (data->type == gdk_atom_intern ("text/uri-list", TRUE)) {
 
209
                list = gnome_vfs_uri_list_parse ((char *) data->data);
 
210
 
 
211
                if (list != NULL)
 
212
                        rb_static_playlist_source_add_list_uri (source, list);
 
213
                else
 
214
                        return FALSE;
 
215
        }
 
216
 
 
217
        return TRUE;
 
218
}
 
219
 
 
220
static void 
 
221
impl_save_contents_to_xml (RBPlaylistSource *source,
 
222
                           xmlNodePtr node)
 
223
{
 
224
        GtkTreeIter iter;
 
225
        RhythmDBQueryModel *model = rb_playlist_source_get_query_model (source);
 
226
 
 
227
        xmlSetProp (node, RB_PLAYLIST_TYPE, RB_PLAYLIST_STATIC);
 
228
        
 
229
        if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
 
230
                return;
 
231
 
 
232
        do { 
 
233
                xmlNodePtr child_node = xmlNewChild (node, NULL, RB_PLAYLIST_LOCATION, NULL);
 
234
                RhythmDBEntry *entry;
 
235
                xmlChar *encoded;
 
236
 
 
237
                gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 0, &entry, -1);
 
238
 
 
239
                encoded = xmlEncodeEntitiesReentrant (NULL, BAD_CAST entry->location);
 
240
 
 
241
                xmlNodeSetContent (child_node, encoded);
 
242
                g_free (encoded);
 
243
        } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
 
244
}
 
245
 
 
246
static void
 
247
rb_static_playlist_source_add_list_uri (RBStaticPlaylistSource *source,
 
248
                                        GList *list)
 
249
{
 
250
        GList *i, *uri_list = NULL;
 
251
        RBPlaylistSource *psource = RB_PLAYLIST_SOURCE (source);
 
252
 
 
253
        g_return_if_fail (list != NULL);
 
254
 
 
255
        for (i = list; i != NULL; i = g_list_next (i))
 
256
                uri_list = g_list_prepend (uri_list, gnome_vfs_uri_to_string ((const GnomeVFSURI *) i->data, 0));
 
257
        uri_list = g_list_reverse (uri_list);
 
258
 
 
259
        gnome_vfs_uri_list_free (list);
 
260
 
 
261
        if (uri_list == NULL)
 
262
                return;
 
263
 
 
264
        for (i = uri_list; i != NULL; i = i->next) {
 
265
                char *uri = i->data;
 
266
                if (uri != NULL) {
 
267
                        rhythmdb_add_uri (rb_playlist_source_get_db (psource), uri);
 
268
                        rb_static_playlist_source_add_location (source, uri, -1);
 
269
                }
 
270
 
 
271
                g_free (uri);
 
272
        }
 
273
        g_list_free (uri_list);
 
274
}
 
275
 
 
276
static void
 
277
rb_static_playlist_source_add_location_internal (RBStaticPlaylistSource *source,
 
278
                                                 const char *location,
 
279
                                                 gint index)
 
280
{
 
281
        RhythmDB *db;
 
282
        RhythmDBEntry *entry;
 
283
        RBPlaylistSource *psource = RB_PLAYLIST_SOURCE (source);
 
284
        if (rb_playlist_source_location_in_map (psource, location))
 
285
                return;
 
286
 
 
287
        db = rb_playlist_source_get_db (psource);
 
288
        entry = rhythmdb_entry_lookup_by_location (db, location);
 
289
        if (entry) {
 
290
                RhythmDBQueryModel *model = rb_playlist_source_get_query_model (psource);
 
291
                RhythmDBEntryType entry_type;
 
292
 
 
293
                g_object_get (G_OBJECT (source), "entry-type", &entry_type, NULL);
 
294
                if (entry_type != -1 &&
 
295
                    rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_TYPE) != entry_type) {
 
296
                        rb_debug ("attempting to add an entry of the wrong type to playlist");
 
297
                        return;
 
298
                }
 
299
 
 
300
                rhythmdb_entry_ref (db, entry);
 
301
                rhythmdb_query_model_add_entry (model, entry, index);
 
302
                rhythmdb_entry_unref (db, entry);
 
303
        }
 
304
 
 
305
        rb_playlist_source_add_to_map (psource, location);
 
306
 
 
307
        rb_playlist_source_mark_dirty (psource);
 
308
}
 
309
 
 
310
static void
 
311
rb_static_playlist_source_add_location_swapped (const char *uri, 
 
312
                                                RBStaticPlaylistSource *source)
 
313
{
 
314
        rb_static_playlist_source_add_location_internal (source, uri, -1);
 
315
}
 
316
 
 
317
 
 
318
void
 
319
rb_static_playlist_source_add_location (RBStaticPlaylistSource *source,
 
320
                                        const char *location,
 
321
                                        gint index)
 
322
{
 
323
        RhythmDB *db;
 
324
        RhythmDBEntry *entry;
 
325
 
 
326
        db = rb_playlist_source_get_db (RB_PLAYLIST_SOURCE (source));
 
327
        entry = rhythmdb_entry_lookup_by_location (db, location);
 
328
 
 
329
        /* if there is an entry, it won't be a directory */
 
330
        if (entry == NULL && rb_uri_is_directory (location))
 
331
                rb_uri_handle_recursively (location,
 
332
                                           (GFunc) rb_static_playlist_source_add_location_swapped,
 
333
                                           NULL,
 
334
                                           source);
 
335
        else
 
336
                rb_static_playlist_source_add_location_internal (source, location, index);
 
337
 
 
338
}
 
339
 
 
340
void
 
341
rb_static_playlist_source_add_locations (RBStaticPlaylistSource *source,
 
342
                                         GList *locations)
 
343
{
 
344
        GList *l;
 
345
 
 
346
        for (l = locations; l; l = l->next) {
 
347
                const gchar *uri = (const gchar *)l->data;
 
348
                rb_static_playlist_source_add_location (source, uri, -1);
 
349
        }
 
350
}
 
351
 
 
352
void
 
353
rb_static_playlist_source_remove_location (RBStaticPlaylistSource *source,
 
354
                                           const char *location)
 
355
{
 
356
        RBPlaylistSource *psource = RB_PLAYLIST_SOURCE (source);
 
357
        RhythmDB *db;
 
358
        RhythmDBEntry *entry;
 
359
 
 
360
        g_return_if_fail (rb_playlist_source_location_in_map (psource, location));
 
361
 
 
362
        db = rb_playlist_source_get_db (psource);
 
363
        entry = rhythmdb_entry_lookup_by_location (db, location);
 
364
 
 
365
        if (entry != NULL) {
 
366
                RhythmDBQueryModel *model = rb_playlist_source_get_query_model (psource);
 
367
 
 
368
                /* if this fails, the model and the playlist are out of sync */
 
369
                g_assert (rhythmdb_query_model_remove_entry (model, entry));
 
370
                rb_playlist_source_mark_dirty (psource);
 
371
        }
 
372
}
 
373
 
 
374
void
 
375
rb_static_playlist_source_add_entry (RBStaticPlaylistSource *source,
 
376
                                     RhythmDBEntry *entry,
 
377
                                     gint index)
 
378
{
 
379
        rb_static_playlist_source_add_location_internal (source, entry->location, index);
 
380
}
 
381
 
 
382
void
 
383
rb_static_playlist_source_remove_entry (RBStaticPlaylistSource *source,
 
384
                                        RhythmDBEntry *entry)
 
385
{
 
386
        rb_static_playlist_source_remove_location (source, entry->location);
 
387
}
 
388
 
 
389
void
 
390
rb_static_playlist_source_move_entry (RBStaticPlaylistSource *source,
 
391
                                      RhythmDBEntry *entry,
 
392
                                      gint index)
 
393
{
 
394
        RBPlaylistSource *psource = RB_PLAYLIST_SOURCE (source);
 
395
        RhythmDBQueryModel *model = rb_playlist_source_get_query_model (psource);
 
396
        rhythmdb_query_model_move_entry (model, entry, index);
 
397
 
 
398
        rb_playlist_source_mark_dirty (psource);
 
399
}
 
400
 
 
401
 
 
402
static void
 
403
rb_static_playlist_source_non_entry_dropped (GtkTreeModel *model,
 
404
                                             const char *uri,
 
405
                                             int position,
 
406
                                             RBStaticPlaylistSource *source)
 
407
{
 
408
        g_assert (g_utf8_strlen (uri, -1) > 0);
 
409
 
 
410
        rhythmdb_add_uri (rb_playlist_source_get_db (RB_PLAYLIST_SOURCE (source)), uri);
 
411
        rb_static_playlist_source_add_location (source, uri, position);
 
412
}
 
413
 
 
414
static void
 
415
rb_static_playlist_source_row_inserted (GtkTreeModel *model,
 
416
                                        GtkTreePath *path,
 
417
                                        GtkTreeIter *iter,
 
418
                                        RBStaticPlaylistSource *source)
 
419
{
 
420
        RhythmDBEntry *entry;
 
421
 
 
422
        gtk_tree_model_get (model, iter, 0, &entry, -1);
 
423
 
 
424
        rb_static_playlist_source_add_entry (source, entry, -1);
 
425
}
 
426