2006-08-04 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / rhythmdb / rhythmdb-property-model.c
blob058061a27396ab55db579edde491a2f1215c936c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * arch-tag: Implementation of RhythmDB property GtkTreeModel
5 * Copyright (C) 2003 Colin Walters <walters@gnome.org>
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 <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib/gi18n.h>
30 #include "rhythmdb-property-model.h"
31 #include "rb-debug.h"
32 #include "gsequence.h"
33 #include "rb-refstring.h"
34 #include "rb-tree-dnd.h"
36 static void rhythmdb_property_model_tree_model_init (GtkTreeModelIface *iface);
37 static void rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface *iface);
39 G_DEFINE_TYPE_WITH_CODE(RhythmDBPropertyModel, rhythmdb_property_model, G_TYPE_OBJECT,
40 G_IMPLEMENT_INTERFACE(GTK_TYPE_TREE_MODEL,
41 rhythmdb_property_model_tree_model_init)
42 G_IMPLEMENT_INTERFACE(RB_TYPE_TREE_DRAG_SOURCE,
43 rhythmdb_property_model_drag_source_init))
45 typedef struct {
46 RBRefString *string;
47 RBRefString *sort_string;
48 guint refcount;
49 } RhythmDBPropertyModelEntry;
51 static void rhythmdb_property_model_finalize (GObject *object);
52 static void rhythmdb_property_model_set_property (GObject *object,
53 guint prop_id,
54 const GValue *value,
55 GParamSpec *pspec);
56 static void rhythmdb_property_model_get_property (GObject *object,
57 guint prop_id,
58 GValue *value,
59 GParamSpec *pspec);
60 static void rhythmdb_property_model_sync (RhythmDBPropertyModel *model);
61 static void rhythmdb_property_model_row_inserted_cb (GtkTreeModel *model,
62 GtkTreePath *path,
63 GtkTreeIter *iter,
64 RhythmDBPropertyModel *propmodel);
65 static void rhythmdb_property_model_prop_changed_cb (RhythmDB *db, RhythmDBEntry *entry,
66 RhythmDBPropType prop, const GValue *old,
67 const GValue *new,
68 RhythmDBPropertyModel *propmodel);
69 static void rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel *model,
70 RhythmDBEntry *entry,
71 RhythmDBPropertyModel *propmodel);
72 static RhythmDBPropertyModelEntry* rhythmdb_property_model_insert (RhythmDBPropertyModel *model,
73 RhythmDBEntry *entry);
74 static void rhythmdb_property_model_delete (RhythmDBPropertyModel *model,
75 RhythmDBEntry *entry);
76 static void rhythmdb_property_model_delete_prop (RhythmDBPropertyModel *model,
77 const char *propstr);
78 static GtkTreeModelFlags rhythmdb_property_model_get_flags (GtkTreeModel *model);
79 static gint rhythmdb_property_model_get_n_columns (GtkTreeModel *tree_model);
80 static GType rhythmdb_property_model_get_column_type (GtkTreeModel *tree_model, int index);
81 static gboolean rhythmdb_property_model_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter,
82 GtkTreePath *path);
83 static GtkTreePath * rhythmdb_property_model_get_path (GtkTreeModel *tree_model,
84 GtkTreeIter *iter);
85 static void rhythmdb_property_model_get_value (GtkTreeModel *tree_model, GtkTreeIter *iter,
86 gint column, GValue *value);
87 static gboolean rhythmdb_property_model_iter_next (GtkTreeModel *tree_model,
88 GtkTreeIter *iter);
89 static gboolean rhythmdb_property_model_iter_children (GtkTreeModel *tree_model,
90 GtkTreeIter *iter,
91 GtkTreeIter *parent);
92 static gboolean rhythmdb_property_model_iter_has_child (GtkTreeModel *tree_model,
93 GtkTreeIter *iter);
94 static gint rhythmdb_property_model_iter_n_children (GtkTreeModel *tree_model,
95 GtkTreeIter *iter);
96 static gboolean rhythmdb_property_model_iter_nth_child (GtkTreeModel *tree_model,
97 GtkTreeIter *iter, GtkTreeIter *parent,
98 gint n);
99 static gboolean rhythmdb_property_model_iter_parent (GtkTreeModel *tree_model,
100 GtkTreeIter *iter,
101 GtkTreeIter *child);
103 static gboolean rhythmdb_property_model_drag_data_get (RbTreeDragSource *dragsource,
104 GList *paths,
105 GtkSelectionData *selection_data);
106 static gboolean rhythmdb_property_model_drag_data_delete (RbTreeDragSource *dragsource,
107 GList *paths);
108 static gboolean rhythmdb_property_model_row_draggable (RbTreeDragSource *dragsource,
109 GList *paths);
111 enum {
112 TARGET_ALBUMS,
113 TARGET_GENRE,
114 TARGET_ARTISTS,
115 TARGET_LOCATION,
116 TARGET_URIS,
119 static const GtkTargetEntry targets_album [] = {
120 { "text/x-rhythmbox-album", 0, TARGET_ALBUMS },
121 { "text/uri-list", 0, TARGET_URIS },
123 static const GtkTargetEntry targets_genre [] = {
124 { "text/x-rhythmbox-genre", 0, TARGET_GENRE },
125 { "text/uri-list", 0, TARGET_URIS },
127 static const GtkTargetEntry targets_artist [] = {
128 { "text/x-rhythmbox-artist", 0, TARGET_ARTISTS },
129 { "text/uri-list", 0, TARGET_URIS },
131 static const GtkTargetEntry targets_location [] = {
132 { "text/x-rhythmbox-location", 0, TARGET_LOCATION },
133 { "text/uri-list", 0, TARGET_URIS },
136 static GtkTargetList *rhythmdb_property_model_album_drag_target_list = NULL;
137 static GtkTargetList *rhythmdb_property_model_artist_drag_target_list = NULL;
138 static GtkTargetList *rhythmdb_property_model_genre_drag_target_list = NULL;
139 static GtkTargetList *rhythmdb_property_model_location_drag_target_list = NULL;
141 struct RhythmDBPropertyModelPrivate
143 RhythmDB *db;
145 RhythmDBQueryModel *query_model;
146 GHashTable *entries;
148 RhythmDBPropType propid;
149 RhythmDBPropType sort_propid;
151 guint stamp;
153 GPtrArray *query;
155 GSequence *properties;
156 GHashTable *reverse_map;
158 RhythmDBPropertyModelEntry *all;
160 gboolean complete;
161 guint syncing_id;
164 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
166 enum
168 PRE_ROW_DELETION,
169 LAST_SIGNAL
172 enum
174 PROP_0,
175 PROP_RHYTHMDB,
176 PROP_PROP,
177 PROP_QUERY_MODEL,
180 static guint rhythmdb_property_model_signals[LAST_SIGNAL] = { 0 };
182 static void
183 rhythmdb_property_model_class_init (RhythmDBPropertyModelClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
187 if (!rhythmdb_property_model_artist_drag_target_list)
188 rhythmdb_property_model_artist_drag_target_list =
189 gtk_target_list_new (targets_artist,
190 G_N_ELEMENTS (targets_artist));
191 if (!rhythmdb_property_model_album_drag_target_list)
192 rhythmdb_property_model_album_drag_target_list =
193 gtk_target_list_new (targets_album,
194 G_N_ELEMENTS (targets_album));
195 if (!rhythmdb_property_model_genre_drag_target_list)
196 rhythmdb_property_model_genre_drag_target_list =
197 gtk_target_list_new (targets_genre,
198 G_N_ELEMENTS (targets_genre));
199 if (!rhythmdb_property_model_location_drag_target_list)
200 rhythmdb_property_model_location_drag_target_list =
201 gtk_target_list_new (targets_location,
202 G_N_ELEMENTS (targets_location));
204 object_class->set_property = rhythmdb_property_model_set_property;
205 object_class->get_property = rhythmdb_property_model_get_property;
207 object_class->finalize = rhythmdb_property_model_finalize;
209 rhythmdb_property_model_signals[PRE_ROW_DELETION] =
210 g_signal_new ("pre-row-deletion",
211 G_OBJECT_CLASS_TYPE (object_class),
212 G_SIGNAL_RUN_LAST,
213 G_STRUCT_OFFSET (RhythmDBPropertyModelClass, pre_row_deletion),
214 NULL, NULL,
215 g_cclosure_marshal_VOID__VOID,
216 G_TYPE_NONE,
219 g_object_class_install_property (object_class,
220 PROP_RHYTHMDB,
221 g_param_spec_object ("db",
222 "RhythmDB",
223 "RhythmDB object",
224 RHYTHMDB_TYPE,
225 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
227 g_object_class_install_property (object_class,
228 PROP_PROP,
229 g_param_spec_int ("prop",
230 "propid",
231 "Property id",
232 0, RHYTHMDB_NUM_PROPERTIES,
233 RHYTHMDB_PROP_TYPE,
234 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
235 g_object_class_install_property (object_class,
236 PROP_QUERY_MODEL,
237 g_param_spec_object ("query-model",
238 "RhythmDBQueryModel",
239 "RhythmDBQueryModel object ",
240 RHYTHMDB_TYPE_QUERY_MODEL,
241 G_PARAM_READWRITE));
243 g_type_class_add_private (klass, sizeof (RhythmDBPropertyModelPrivate));
246 static void
247 rhythmdb_property_model_tree_model_init (GtkTreeModelIface *iface)
249 iface->get_flags = rhythmdb_property_model_get_flags;
250 iface->get_n_columns = rhythmdb_property_model_get_n_columns;
251 iface->get_column_type = rhythmdb_property_model_get_column_type;
252 iface->get_iter = rhythmdb_property_model_get_iter;
253 iface->get_path = rhythmdb_property_model_get_path;
254 iface->get_value = rhythmdb_property_model_get_value;
255 iface->iter_next = rhythmdb_property_model_iter_next;
256 iface->iter_children = rhythmdb_property_model_iter_children;
257 iface->iter_has_child = rhythmdb_property_model_iter_has_child;
258 iface->iter_n_children = rhythmdb_property_model_iter_n_children;
259 iface->iter_nth_child = rhythmdb_property_model_iter_nth_child;
260 iface->iter_parent = rhythmdb_property_model_iter_parent;
263 static void
264 rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface *iface)
266 iface->row_draggable = rhythmdb_property_model_row_draggable;
267 iface->drag_data_delete = rhythmdb_property_model_drag_data_delete;
268 iface->drag_data_get = rhythmdb_property_model_drag_data_get;
271 static gboolean
272 _remove_entry_cb (RhythmDBEntry *entry,
273 gpointer unused,
274 RhythmDBPropertyModel *model)
276 rhythmdb_property_model_delete (model, entry);
277 return TRUE;
280 static gboolean
281 _add_entry_cb (GtkTreeModel *model,
282 GtkTreePath *path,
283 GtkTreeIter *iter,
284 RhythmDBPropertyModel *propmodel)
286 rhythmdb_property_model_row_inserted_cb (model, path, iter, propmodel);
287 return FALSE;
290 static void
291 rhythmdb_property_model_set_query_model_internal (RhythmDBPropertyModel *model,
292 RhythmDBQueryModel *query_model)
294 if (model->priv->query_model != NULL) {
295 g_signal_handlers_disconnect_by_func (model->priv->query_model,
296 G_CALLBACK (rhythmdb_property_model_row_inserted_cb),
297 model);
298 g_signal_handlers_disconnect_by_func (model->priv->query_model,
299 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
300 model);
301 g_signal_handlers_disconnect_by_func (model->priv->query_model,
302 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
303 model);
304 g_hash_table_foreach_remove (model->priv->entries, (GHRFunc)_remove_entry_cb, model);
306 g_object_unref (model->priv->query_model);
309 model->priv->query_model = query_model;
310 g_assert (rhythmdb_property_model_iter_n_children (GTK_TREE_MODEL (model), NULL) == 1);
312 if (model->priv->query_model != NULL) {
313 g_object_ref (model->priv->query_model);
315 g_signal_connect_object (model->priv->query_model,
316 "row_inserted",
317 G_CALLBACK (rhythmdb_property_model_row_inserted_cb),
318 model,
320 g_signal_connect_object (model->priv->query_model,
321 "post-entry-delete",
322 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
323 model,
325 g_signal_connect_object (model->priv->query_model,
326 "entry-prop-changed",
327 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
328 model,
330 gtk_tree_model_foreach (GTK_TREE_MODEL (model->priv->query_model),
331 (GtkTreeModelForeachFunc)_add_entry_cb,
332 model);
336 static void
337 rhythmdb_property_model_set_property (GObject *object,
338 guint prop_id,
339 const GValue *value,
340 GParamSpec *pspec)
342 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
344 switch (prop_id) {
345 case PROP_RHYTHMDB:
346 model->priv->db = g_value_get_object (value);
347 break;
348 case PROP_PROP:
349 model->priv->propid = g_value_get_int (value);
350 switch (model->priv->propid) {
351 case RHYTHMDB_PROP_GENRE:
352 model->priv->sort_propid = RHYTHMDB_PROP_GENRE;
353 break;
354 case RHYTHMDB_PROP_ARTIST:
355 model->priv->sort_propid = RHYTHMDB_PROP_ARTIST;
356 break;
357 case RHYTHMDB_PROP_ALBUM:
358 model->priv->sort_propid = RHYTHMDB_PROP_ALBUM;
359 break;
360 case RHYTHMDB_PROP_SUBTITLE:
361 model->priv->sort_propid = RHYTHMDB_PROP_SUBTITLE;
362 break;
363 case RHYTHMDB_PROP_TITLE:
364 case RHYTHMDB_PROP_LOCATION:
365 model->priv->sort_propid = RHYTHMDB_PROP_TITLE;
366 break;
367 default:
368 g_assert_not_reached ();
369 break;
371 break;
372 case PROP_QUERY_MODEL:
373 rhythmdb_property_model_set_query_model_internal (model, g_value_get_object (value));
374 break;
375 default:
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 break;
381 static void
382 rhythmdb_property_model_get_property (GObject *object,
383 guint prop_id,
384 GValue *value,
385 GParamSpec *pspec)
387 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
389 switch (prop_id) {
390 case PROP_RHYTHMDB:
391 g_value_set_object (value, model->priv->db);
392 break;
393 case PROP_PROP:
394 g_value_set_int (value, model->priv->propid);
395 break;
396 case PROP_QUERY_MODEL:
397 g_value_set_object (value, model->priv->query_model);
398 break;
399 default:
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 break;
405 static void
406 rhythmdb_property_model_init (RhythmDBPropertyModel *model)
408 model->priv = RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE (model);
410 model->priv->stamp = g_random_int ();
412 model->priv->properties = g_sequence_new (NULL);
413 model->priv->reverse_map = g_hash_table_new (g_str_hash, g_str_equal);
414 model->priv->entries = g_hash_table_new (g_direct_hash, g_direct_equal);
416 model->priv->all = g_new0 (RhythmDBPropertyModelEntry, 1);
417 model->priv->all->string = rb_refstring_new (_("All"));
420 static void
421 rhythmdb_property_model_finalize (GObject *object)
423 RhythmDBPropertyModel *model;
424 GSequencePtr ptr;
425 GSequencePtr end_ptr;
427 g_return_if_fail (object != NULL);
428 g_return_if_fail (RHYTHMDB_IS_PROPERTY_MODEL (object));
430 model = RHYTHMDB_PROPERTY_MODEL (object);
432 rb_debug ("finalizing property model %p", model);
434 g_return_if_fail (model->priv != NULL);
436 if (model->priv->syncing_id != 0)
437 g_source_remove (model->priv->syncing_id);
439 end_ptr = g_sequence_get_end_ptr (model->priv->properties);
440 for (ptr = g_sequence_get_begin_ptr (model->priv->properties); ptr != end_ptr;
441 ptr = g_sequence_ptr_next (ptr)) {
442 RhythmDBPropertyModelEntry *prop = g_sequence_ptr_get_data (ptr);
443 rb_refstring_unref (prop->string);
444 rb_refstring_unref (prop->sort_string);
447 g_hash_table_destroy (model->priv->reverse_map);
448 g_sequence_free (model->priv->properties);
450 g_hash_table_destroy (model->priv->entries);
452 if (model->priv->query_model != NULL) {
453 g_object_unref (model->priv->query_model);
456 G_OBJECT_CLASS (rhythmdb_property_model_parent_class)->finalize (object);
459 RhythmDBPropertyModel *
460 rhythmdb_property_model_new (RhythmDB *db,
461 RhythmDBPropType propid)
463 return g_object_new (RHYTHMDB_TYPE_PROPERTY_MODEL, "db", db, "prop", propid, NULL);
466 static void
467 rhythmdb_property_model_row_inserted_cb (GtkTreeModel *model,
468 GtkTreePath *path,
469 GtkTreeIter *iter,
470 RhythmDBPropertyModel *propmodel)
472 RhythmDBEntry *entry;
473 RhythmDBPropertyModelEntry *prop;
475 entry = rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model), iter);
477 if (g_hash_table_lookup (propmodel->priv->entries, entry)) {
478 goto out;
481 prop = rhythmdb_property_model_insert (propmodel, entry);
482 g_hash_table_insert (propmodel->priv->entries, entry, prop);
484 rhythmdb_property_model_sync (propmodel);
485 out:
486 rhythmdb_entry_unref (entry);
489 static void
490 rhythmdb_property_model_prop_changed_cb (RhythmDB *db,
491 RhythmDBEntry *entry,
492 RhythmDBPropType propid,
493 const GValue *old,
494 const GValue *new,
495 RhythmDBPropertyModel *propmodel)
497 if (propid == RHYTHMDB_PROP_HIDDEN) {
498 gboolean old_val = g_value_get_boolean (old);
499 gboolean new_val = g_value_get_boolean (new);
501 if (old_val != new_val) {
502 if (new_val == FALSE) {
503 RhythmDBPropertyModelEntry *prop;
505 if (g_hash_table_lookup (propmodel->priv->entries, entry) != NULL)
506 return;
508 prop = rhythmdb_property_model_insert (propmodel, entry);
509 g_hash_table_insert (propmodel->priv->entries, entry, prop);
510 } else {
511 if (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL)
512 return;
514 rhythmdb_property_model_delete (propmodel, entry);
515 g_hash_table_remove (propmodel->priv->entries, entry);
518 } else {
519 RhythmDBPropertyModelEntry *prop;
521 if (propid != propmodel->priv->propid)
522 return;
524 if (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL)
525 return;
527 rhythmdb_property_model_delete (propmodel, entry);
528 prop = rhythmdb_property_model_insert (propmodel, entry);
529 g_hash_table_insert (propmodel->priv->entries, entry, prop);
532 rhythmdb_property_model_sync (propmodel);
535 static void
536 rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel *model,
537 RhythmDBEntry *entry,
538 RhythmDBPropertyModel *propmodel)
540 if (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL)
541 return;
543 rhythmdb_property_model_delete (propmodel, entry);
544 g_hash_table_remove (propmodel->priv->entries, entry);
545 rhythmdb_property_model_sync (propmodel);
548 static gint
549 rhythmdb_property_model_compare (RhythmDBPropertyModelEntry *a,
550 RhythmDBPropertyModelEntry *b,
551 RhythmDBPropertyModel *model)
553 const char *a_str, *b_str;
555 a_str = rb_refstring_get_sort_key (a->sort_string);
556 b_str = rb_refstring_get_sort_key (b->sort_string);
558 return strcmp (a_str, b_str);
561 static RhythmDBPropertyModelEntry *
562 rhythmdb_property_model_insert (RhythmDBPropertyModel *model,
563 RhythmDBEntry *entry)
565 RhythmDBPropertyModelEntry *prop;
566 GtkTreeIter iter;
567 GtkTreePath *path;
568 GSequencePtr ptr;
569 const char *propstr;
571 propstr = rhythmdb_entry_get_string (entry, model->priv->propid);
573 model->priv->all->refcount++;
575 if ((ptr = g_hash_table_lookup (model->priv->reverse_map, propstr))) {
576 prop = g_sequence_ptr_get_data (ptr);
577 prop->refcount++;
578 rb_debug ("adding \"%s\": refcount %d", propstr, prop->refcount);
579 return prop;
581 rb_debug ("adding new property \"%s\"", propstr);
583 prop = g_new0 (RhythmDBPropertyModelEntry, 1);
584 prop->string = rb_refstring_new (propstr);
585 prop->sort_string = rb_refstring_new (rhythmdb_entry_get_string (entry, model->priv->sort_propid));
586 prop->refcount = 1;
588 iter.stamp = model->priv->stamp;
589 ptr = g_sequence_insert_sorted (model->priv->properties, prop,
590 (GCompareDataFunc) rhythmdb_property_model_compare,
591 model);
592 g_hash_table_insert (model->priv->reverse_map,
593 (gpointer)rb_refstring_get (prop->string),
594 ptr);
596 iter.user_data = ptr;
597 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
598 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
599 gtk_tree_path_free (path);
601 return prop;
604 static void
605 rhythmdb_property_model_delete (RhythmDBPropertyModel *model,
606 RhythmDBEntry *entry)
608 RhythmDBPropertyModelEntry *prop;
609 const char *propstr;
611 prop = g_hash_table_lookup (model->priv->entries, entry);
612 propstr = rb_refstring_get (prop->string);
613 rhythmdb_property_model_delete_prop (model, propstr);
616 static void
617 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel *model,
618 const char *propstr)
620 GSequencePtr ptr;
621 RhythmDBPropertyModelEntry *prop;
622 GtkTreePath *path;
623 GtkTreeIter iter;
625 g_assert ((ptr = g_hash_table_lookup (model->priv->reverse_map, propstr)));
627 model->priv->all->refcount--;
629 prop = g_sequence_ptr_get_data (ptr);
630 rb_debug ("deleting \"%s\": refcount: %d", propstr, prop->refcount);
631 prop->refcount--;
632 if (prop->refcount > 0)
633 return;
635 iter.stamp = model->priv->stamp;
636 iter.user_data = ptr;
638 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
639 g_signal_emit (G_OBJECT (model), rhythmdb_property_model_signals[PRE_ROW_DELETION], 0);
640 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
641 gtk_tree_path_free (path);
642 g_sequence_remove (ptr);
643 g_hash_table_remove (model->priv->reverse_map, propstr);
644 rb_refstring_unref (prop->string);
645 rb_refstring_unref (prop->sort_string);
646 g_free (prop);
647 return;
650 gboolean
651 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel *model,
652 const char *name,
653 GtkTreeIter *iter)
655 GSequencePtr ptr;
657 if (name == NULL) {
658 if (iter) {
659 iter->stamp = model->priv->stamp;
660 iter->user_data = model->priv->all;
662 return TRUE;
665 ptr = g_hash_table_lookup (model->priv->reverse_map, name);
666 if (!ptr)
667 return FALSE;
669 if (iter) {
670 iter->stamp = model->priv->stamp;
671 iter->user_data = ptr;
674 return TRUE;
677 static GtkTreeModelFlags
678 rhythmdb_property_model_get_flags (GtkTreeModel *model)
680 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
683 static gint
684 rhythmdb_property_model_get_n_columns (GtkTreeModel *tree_model)
686 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST;
689 static GType
690 rhythmdb_property_model_get_column_type (GtkTreeModel *tree_model,
691 int index)
693 switch (index) {
694 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
695 return G_TYPE_STRING;
696 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
697 return G_TYPE_BOOLEAN;
698 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
699 return G_TYPE_UINT;
700 default:
701 g_assert_not_reached ();
702 return G_TYPE_INVALID;
706 static gboolean
707 rhythmdb_property_model_get_iter (GtkTreeModel *tree_model,
708 GtkTreeIter *iter,
709 GtkTreePath *path)
711 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
712 guint index;
713 GSequencePtr ptr;
715 index = gtk_tree_path_get_indices (path)[0];
717 if (index == 0) {
718 iter->stamp = model->priv->stamp;
719 iter->user_data = model->priv->all;
720 return TRUE;
723 index--;
724 if (index >= g_sequence_get_length (model->priv->properties))
725 return FALSE;
727 ptr = g_sequence_get_ptr_at_pos (model->priv->properties, index);
729 iter->stamp = model->priv->stamp;
730 iter->user_data = ptr;
732 return TRUE;
735 static GtkTreePath *
736 rhythmdb_property_model_get_path (GtkTreeModel *tree_model,
737 GtkTreeIter *iter)
739 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
740 GtkTreePath *path;
742 g_return_val_if_fail (iter->stamp == model->priv->stamp, NULL);
744 if (g_sequence_ptr_is_end (iter->user_data))
745 return NULL;
747 path = gtk_tree_path_new ();
748 if (iter->user_data == model->priv->all)
749 gtk_tree_path_append_index (path, 0);
750 else
751 gtk_tree_path_append_index (path, g_sequence_ptr_get_position (iter->user_data)+1);
752 return path;
755 static void
756 rhythmdb_property_model_get_value (GtkTreeModel *tree_model,
757 GtkTreeIter *iter,
758 gint column,
759 GValue *value)
761 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
763 g_return_if_fail (!g_sequence_ptr_is_end (iter->user_data));
764 g_return_if_fail (model->priv->stamp == iter->stamp);
766 if (iter->user_data == model->priv->all) {
767 switch (column) {
768 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
769 g_value_init (value, G_TYPE_STRING);
770 g_value_set_string (value, rb_refstring_get (model->priv->all->string));
771 break;
772 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
773 g_value_init (value, G_TYPE_BOOLEAN);
774 g_value_set_boolean (value, TRUE);
775 break;
776 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
777 g_value_init (value, G_TYPE_UINT);
778 g_value_set_uint (value, model->priv->all->refcount);
779 break;
780 default:
781 g_assert_not_reached ();
783 } else {
784 RhythmDBPropertyModelEntry *prop = g_sequence_ptr_get_data (iter->user_data);
786 switch (column) {
787 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
788 g_value_init (value, G_TYPE_STRING);
789 g_value_set_string (value, rb_refstring_get (prop->string));
790 break;
791 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
792 g_value_init (value, G_TYPE_BOOLEAN);
793 g_value_set_boolean (value, prop == model->priv->all);
794 break;
795 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
796 g_value_init (value, G_TYPE_UINT);
797 g_value_set_uint (value, prop->refcount);
798 break;
799 default:
800 g_assert_not_reached ();
805 static gboolean
806 rhythmdb_property_model_iter_next (GtkTreeModel *tree_model,
807 GtkTreeIter *iter)
809 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
811 g_return_val_if_fail (iter->stamp == model->priv->stamp, FALSE);
813 if (iter->user_data == model->priv->all)
814 iter->user_data = g_sequence_get_begin_ptr (model->priv->properties);
815 else
816 iter->user_data = g_sequence_ptr_next (iter->user_data);
818 return !g_sequence_ptr_is_end (iter->user_data);
821 static gboolean
822 rhythmdb_property_model_iter_children (GtkTreeModel *tree_model,
823 GtkTreeIter *iter,
824 GtkTreeIter *parent)
826 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
828 if (parent != NULL)
829 return FALSE;
831 iter->stamp = model->priv->stamp;
832 iter->user_data = model->priv->all;
834 return TRUE;
837 static gboolean
838 rhythmdb_property_model_iter_has_child (GtkTreeModel *tree_model,
839 GtkTreeIter *iter)
841 return FALSE;
844 static gint
845 rhythmdb_property_model_iter_n_children (GtkTreeModel *tree_model,
846 GtkTreeIter *iter)
848 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
850 if (iter)
851 g_return_val_if_fail (model->priv->stamp == iter->stamp, -1);
853 if (iter == NULL)
854 return 1 + g_sequence_get_length (model->priv->properties);
856 return 0;
859 static gboolean
860 rhythmdb_property_model_iter_nth_child (GtkTreeModel *tree_model,
861 GtkTreeIter *iter,
862 GtkTreeIter *parent,
863 gint n)
865 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
866 GSequencePtr child;
868 if (parent)
869 return FALSE;
871 if (n != 0) {
872 child = g_sequence_get_ptr_at_pos (model->priv->properties, n);
874 if (g_sequence_ptr_is_end (child))
875 return FALSE;
876 iter->user_data = child;
877 } else {
878 iter->user_data = model->priv->all;
881 iter->stamp = model->priv->stamp;
883 return TRUE;
886 static gboolean
887 rhythmdb_property_model_iter_parent (GtkTreeModel *tree_model,
888 GtkTreeIter *iter,
889 GtkTreeIter *child)
891 return FALSE;
894 static gboolean
895 rhythmdb_property_model_row_draggable (RbTreeDragSource *dragsource,
896 GList *paths)
898 return TRUE;
901 static gboolean
902 rhythmdb_property_model_drag_data_delete (RbTreeDragSource *dragsource,
903 GList *paths)
905 /* not supported */
906 return TRUE;
909 /*Going through hoops to avoid nested functions*/
910 struct QueryModelCbStruct {
911 RhythmDB *db;
912 GString *reply;
915 static gboolean
916 query_model_cb (GtkTreeModel *query_model,
917 GtkTreePath *path,
918 GtkTreeIter *iter,
919 struct QueryModelCbStruct *data)
921 const char *uri;
922 RhythmDBEntry *entry;
924 gtk_tree_model_get (query_model, iter, 0, &entry, -1);
925 uri = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION);
926 g_string_append (data->reply, uri);
927 g_string_append (data->reply, "\r\n");
929 rhythmdb_entry_unref (entry);
930 return FALSE;
933 static gboolean
934 rhythmdb_property_model_drag_data_get (RbTreeDragSource *dragsource,
935 GList *paths,
936 GtkSelectionData *selection_data)
938 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (dragsource);
939 guint target;
940 GtkTargetList *drag_target_list;
942 switch (model->priv->propid) {
943 case RHYTHMDB_PROP_GENRE:
944 drag_target_list = rhythmdb_property_model_genre_drag_target_list;
945 break;
946 case RHYTHMDB_PROP_ALBUM:
947 drag_target_list = rhythmdb_property_model_album_drag_target_list;
948 break;
949 case RHYTHMDB_PROP_ARTIST:
950 drag_target_list = rhythmdb_property_model_artist_drag_target_list;
951 break;
952 case RHYTHMDB_PROP_LOCATION:
953 drag_target_list = rhythmdb_property_model_location_drag_target_list;
954 break;
955 default:
956 g_assert_not_reached ();
959 if (!gtk_target_list_find (drag_target_list,
960 selection_data->target,
961 &target)) {
962 return FALSE;
965 if (target == TARGET_URIS) {
966 RhythmDB *db = model->priv->db;
967 RhythmDBQueryModel *query_model;
968 GString* reply = g_string_new ("");
969 GtkTreeIter iter;
970 gboolean is_all = FALSE;
971 struct QueryModelCbStruct tmp;
972 GtkTreePath *path;
973 GCompareDataFunc sort_func = NULL;
974 gpointer sort_data;
975 gboolean sort_reverse;
977 query_model = rhythmdb_query_model_new_empty (db);
978 /* FIXME the sort order on the query model at this point is usually
979 * not the user's selected sort order.
981 g_object_get (G_OBJECT (model->priv->query_model),
982 "sort-func", &sort_func,
983 "sort-data", &sort_data,
984 "sort-reverse", &sort_reverse,
985 NULL);
986 rhythmdb_query_model_set_sort_order (RHYTHMDB_QUERY_MODEL (query_model),
987 sort_func, GUINT_TO_POINTER (sort_data), NULL, sort_reverse);
989 rb_debug ("getting drag data as uri list");
990 /* check if first selected row is 'All' */
991 path = gtk_tree_row_reference_get_path (paths->data);
992 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
993 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
994 RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY,
995 &is_all, -1);
996 gtk_tree_path_free (path);
997 if (is_all) {
998 g_object_set (query_model,
999 "base-model", model->priv->query_model,
1000 NULL);
1001 } else {
1002 GList *row;
1003 GPtrArray *subquery = g_ptr_array_new ();
1005 for (row = paths; row; row = row->next) {
1006 char* name;
1007 path = gtk_tree_row_reference_get_path (row->data);
1008 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path)) {
1009 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
1010 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE,
1011 &name, -1);
1012 if (row == paths) {
1013 rhythmdb_query_append (db, subquery,
1014 RHYTHMDB_QUERY_PROP_EQUALS,
1015 model->priv->propid, name,
1016 RHYTHMDB_QUERY_END);
1017 } else {
1018 rhythmdb_query_append (db, subquery,
1019 RHYTHMDB_QUERY_DISJUNCTION,
1020 RHYTHMDB_QUERY_PROP_EQUALS,
1021 model->priv->propid, name,
1022 RHYTHMDB_QUERY_END);
1026 gtk_tree_path_free (path);
1027 g_free (name);
1030 g_object_set (query_model,
1031 "query", subquery,
1032 "base-model", model->priv->query_model,
1033 NULL);
1034 rhythmdb_query_free (subquery);
1037 tmp.db = db;
1038 tmp.reply = reply;
1039 /* Too bad that we're on the main thread. Why doesn't gtk call us async?
1040 * How does file-roller manage? - it seems it refuses the drop when it isn't
1041 * done unpacking. In which case, we should tweak the drop acknowledgement,
1042 * and prepare the query using do_full_query_async. The query would be
1043 * hooked to the drag context.
1045 gtk_tree_model_foreach (GTK_TREE_MODEL (query_model),
1046 (GtkTreeModelForeachFunc) query_model_cb,
1047 &tmp);
1049 g_object_unref (query_model);
1051 gtk_selection_data_set (selection_data,
1052 selection_data->target,
1053 8, (guchar *)reply->str,
1054 reply->len);
1055 g_string_free (reply, TRUE);
1057 } else {
1058 char* title;
1059 GList *p;
1060 GString* reply = g_string_new ("");
1062 rb_debug ("getting drag data as list of property values");
1064 for (p = paths; p; p = p->next) {
1065 GtkTreeIter iter;
1066 GtkTreePath *path;
1068 path = gtk_tree_row_reference_get_path (p->data);
1069 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path)) {
1070 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
1071 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE, &title, -1);
1072 g_string_append (reply, title);
1073 if (p->next)
1074 g_string_append (reply, "\r\n");
1075 g_free (title);
1077 gtk_tree_path_free (path);
1079 gtk_selection_data_set (selection_data,
1080 selection_data->target,
1081 8, (guchar *)reply->str,
1082 reply->len);
1083 g_string_free (reply, TRUE);
1086 return TRUE;
1089 void
1090 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel *model,
1091 GtkTreeView *view)
1093 const GtkTargetEntry *targets;
1094 gint n_elements;
1096 switch (model->priv->propid) {
1097 case RHYTHMDB_PROP_GENRE:
1098 targets = targets_genre;
1099 n_elements = G_N_ELEMENTS (targets_genre);
1100 break;
1101 case RHYTHMDB_PROP_ALBUM:
1102 targets = targets_album;
1103 n_elements = G_N_ELEMENTS (targets_album);
1104 break;
1105 case RHYTHMDB_PROP_ARTIST:
1106 targets = targets_artist;
1107 n_elements = G_N_ELEMENTS (targets_artist);
1108 break;
1109 case RHYTHMDB_PROP_LOCATION:
1110 targets = targets_location;
1111 n_elements = G_N_ELEMENTS (targets_location);
1112 break;
1113 default:
1114 g_assert_not_reached ();
1117 rb_tree_dnd_add_drag_source_support (view,
1118 GDK_BUTTON1_MASK,
1119 targets, n_elements,
1120 GDK_ACTION_COPY);
1123 static gboolean
1124 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel *model)
1126 GtkTreeIter iter;
1127 GtkTreePath *path;
1129 iter.stamp = model->priv->stamp;
1130 iter.user_data = model->priv->all;
1131 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
1132 gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
1133 gtk_tree_path_free (path);
1135 model->priv->syncing_id = 0;
1136 return FALSE;
1139 static void
1140 rhythmdb_property_model_sync (RhythmDBPropertyModel *model)
1142 if (model->priv->syncing_id != 0)
1143 return;
1145 model->priv->syncing_id = g_idle_add ((GSourceFunc)rhythmdb_property_model_perform_sync, model);
1148 /* This should really be standard. */
1149 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1151 GType
1152 rhythmdb_property_model_column_get_type (void)
1154 static GType etype = 0;
1156 if (etype == 0) {
1157 static const GEnumValue values[] = {
1158 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE, "Property title"),
1159 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY, "Value priority"),
1160 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER, "Track count"),
1161 { 0, 0, 0 }
1164 etype = g_enum_register_static ("RhythmDBPropertyModelColumn", values);
1167 return etype;