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.
28 #include <glib/gi18n.h>
30 #include "rhythmdb-property-model.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
))
47 RBRefString
*sort_string
;
49 } RhythmDBPropertyModelEntry
;
51 static void rhythmdb_property_model_finalize (GObject
*object
);
52 static void rhythmdb_property_model_set_property (GObject
*object
,
56 static void rhythmdb_property_model_get_property (GObject
*object
,
60 static void rhythmdb_property_model_sync (RhythmDBPropertyModel
*model
);
61 static void rhythmdb_property_model_row_inserted_cb (GtkTreeModel
*model
,
64 RhythmDBPropertyModel
*propmodel
);
65 static void rhythmdb_property_model_prop_changed_cb (RhythmDB
*db
, RhythmDBEntry
*entry
,
66 RhythmDBPropType prop
, const GValue
*old
,
68 RhythmDBPropertyModel
*propmodel
);
69 static void rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel
*model
,
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
,
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
,
83 static GtkTreePath
* rhythmdb_property_model_get_path (GtkTreeModel
*tree_model
,
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
,
89 static gboolean
rhythmdb_property_model_iter_children (GtkTreeModel
*tree_model
,
92 static gboolean
rhythmdb_property_model_iter_has_child (GtkTreeModel
*tree_model
,
94 static gint
rhythmdb_property_model_iter_n_children (GtkTreeModel
*tree_model
,
96 static gboolean
rhythmdb_property_model_iter_nth_child (GtkTreeModel
*tree_model
,
97 GtkTreeIter
*iter
, GtkTreeIter
*parent
,
99 static gboolean
rhythmdb_property_model_iter_parent (GtkTreeModel
*tree_model
,
103 static gboolean
rhythmdb_property_model_drag_data_get (RbTreeDragSource
*dragsource
,
105 GtkSelectionData
*selection_data
);
106 static gboolean
rhythmdb_property_model_drag_data_delete (RbTreeDragSource
*dragsource
,
108 static gboolean
rhythmdb_property_model_row_draggable (RbTreeDragSource
*dragsource
,
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
145 RhythmDBQueryModel
*query_model
;
148 RhythmDBPropType propid
;
149 RhythmDBPropType sort_propid
;
153 GSequence
*properties
;
154 GHashTable
*reverse_map
;
156 RhythmDBPropertyModelEntry
*all
;
161 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
177 static guint rhythmdb_property_model_signals
[LAST_SIGNAL
] = { 0 };
180 rhythmdb_property_model_class_init (RhythmDBPropertyModelClass
*klass
)
182 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
184 if (!rhythmdb_property_model_artist_drag_target_list
)
185 rhythmdb_property_model_artist_drag_target_list
=
186 gtk_target_list_new (targets_artist
,
187 G_N_ELEMENTS (targets_artist
));
188 if (!rhythmdb_property_model_album_drag_target_list
)
189 rhythmdb_property_model_album_drag_target_list
=
190 gtk_target_list_new (targets_album
,
191 G_N_ELEMENTS (targets_album
));
192 if (!rhythmdb_property_model_genre_drag_target_list
)
193 rhythmdb_property_model_genre_drag_target_list
=
194 gtk_target_list_new (targets_genre
,
195 G_N_ELEMENTS (targets_genre
));
196 if (!rhythmdb_property_model_location_drag_target_list
)
197 rhythmdb_property_model_location_drag_target_list
=
198 gtk_target_list_new (targets_location
,
199 G_N_ELEMENTS (targets_location
));
201 object_class
->set_property
= rhythmdb_property_model_set_property
;
202 object_class
->get_property
= rhythmdb_property_model_get_property
;
204 object_class
->finalize
= rhythmdb_property_model_finalize
;
206 rhythmdb_property_model_signals
[PRE_ROW_DELETION
] =
207 g_signal_new ("pre-row-deletion",
208 G_OBJECT_CLASS_TYPE (object_class
),
210 G_STRUCT_OFFSET (RhythmDBPropertyModelClass
, pre_row_deletion
),
212 g_cclosure_marshal_VOID__VOID
,
216 g_object_class_install_property (object_class
,
218 g_param_spec_object ("db",
222 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
224 g_object_class_install_property (object_class
,
226 g_param_spec_int ("prop",
229 0, RHYTHMDB_NUM_PROPERTIES
,
231 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
232 g_object_class_install_property (object_class
,
234 g_param_spec_object ("query-model",
235 "RhythmDBQueryModel",
236 "RhythmDBQueryModel object ",
237 RHYTHMDB_TYPE_QUERY_MODEL
,
240 g_type_class_add_private (klass
, sizeof (RhythmDBPropertyModelPrivate
));
244 rhythmdb_property_model_tree_model_init (GtkTreeModelIface
*iface
)
246 iface
->get_flags
= rhythmdb_property_model_get_flags
;
247 iface
->get_n_columns
= rhythmdb_property_model_get_n_columns
;
248 iface
->get_column_type
= rhythmdb_property_model_get_column_type
;
249 iface
->get_iter
= rhythmdb_property_model_get_iter
;
250 iface
->get_path
= rhythmdb_property_model_get_path
;
251 iface
->get_value
= rhythmdb_property_model_get_value
;
252 iface
->iter_next
= rhythmdb_property_model_iter_next
;
253 iface
->iter_children
= rhythmdb_property_model_iter_children
;
254 iface
->iter_has_child
= rhythmdb_property_model_iter_has_child
;
255 iface
->iter_n_children
= rhythmdb_property_model_iter_n_children
;
256 iface
->iter_nth_child
= rhythmdb_property_model_iter_nth_child
;
257 iface
->iter_parent
= rhythmdb_property_model_iter_parent
;
261 rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface
*iface
)
263 iface
->row_draggable
= rhythmdb_property_model_row_draggable
;
264 iface
->drag_data_delete
= rhythmdb_property_model_drag_data_delete
;
265 iface
->drag_data_get
= rhythmdb_property_model_drag_data_get
;
269 _remove_entry_cb (GtkTreeModel
*model
,
272 RhythmDBPropertyModel
*propmodel
)
274 RhythmDBEntry
*entry
;
276 entry
= rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model
), iter
);
277 rhythmdb_property_model_entry_removed_cb (RHYTHMDB_QUERY_MODEL (model
),
284 _add_entry_cb (GtkTreeModel
*model
,
287 RhythmDBPropertyModel
*propmodel
)
289 rhythmdb_property_model_row_inserted_cb (model
, path
, iter
, propmodel
);
294 rhythmdb_property_model_set_query_model_internal (RhythmDBPropertyModel
*model
,
295 RhythmDBQueryModel
*query_model
)
297 if (model
->priv
->query_model
!= NULL
) {
298 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
299 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
301 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
302 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
304 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
305 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
308 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
309 (GtkTreeModelForeachFunc
)_remove_entry_cb
,
312 g_object_unref (model
->priv
->query_model
);
315 model
->priv
->query_model
= query_model
;
316 g_assert (rhythmdb_property_model_iter_n_children (GTK_TREE_MODEL (model
), NULL
) == 1);
318 if (model
->priv
->query_model
!= NULL
) {
319 g_object_ref (model
->priv
->query_model
);
321 g_signal_connect_object (model
->priv
->query_model
,
323 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
326 g_signal_connect_object (model
->priv
->query_model
,
328 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
331 g_signal_connect_object (model
->priv
->query_model
,
332 "entry-prop-changed",
333 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
336 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
337 (GtkTreeModelForeachFunc
)_add_entry_cb
,
343 rhythmdb_property_model_set_property (GObject
*object
,
348 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
352 model
->priv
->db
= g_value_get_object (value
);
355 model
->priv
->propid
= g_value_get_int (value
);
356 switch (model
->priv
->propid
) {
357 case RHYTHMDB_PROP_GENRE
:
358 model
->priv
->sort_propid
= RHYTHMDB_PROP_GENRE
;
360 case RHYTHMDB_PROP_ARTIST
:
361 model
->priv
->sort_propid
= RHYTHMDB_PROP_ARTIST
;
363 case RHYTHMDB_PROP_ALBUM
:
364 model
->priv
->sort_propid
= RHYTHMDB_PROP_ALBUM
;
366 case RHYTHMDB_PROP_SUBTITLE
:
367 model
->priv
->sort_propid
= RHYTHMDB_PROP_SUBTITLE
;
369 case RHYTHMDB_PROP_TITLE
:
370 case RHYTHMDB_PROP_LOCATION
:
371 model
->priv
->sort_propid
= RHYTHMDB_PROP_TITLE
;
374 g_assert_not_reached ();
378 case PROP_QUERY_MODEL
:
379 rhythmdb_property_model_set_query_model_internal (model
, g_value_get_object (value
));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
388 rhythmdb_property_model_get_property (GObject
*object
,
393 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
397 g_value_set_object (value
, model
->priv
->db
);
400 g_value_set_int (value
, model
->priv
->propid
);
402 case PROP_QUERY_MODEL
:
403 g_value_set_object (value
, model
->priv
->query_model
);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
412 rhythmdb_property_model_init (RhythmDBPropertyModel
*model
)
414 model
->priv
= RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE (model
);
416 model
->priv
->stamp
= g_random_int ();
418 model
->priv
->properties
= g_sequence_new (NULL
);
419 model
->priv
->reverse_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
420 model
->priv
->entries
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
422 model
->priv
->all
= g_new0 (RhythmDBPropertyModelEntry
, 1);
423 model
->priv
->all
->string
= rb_refstring_new (_("All"));
427 rhythmdb_property_model_finalize (GObject
*object
)
429 RhythmDBPropertyModel
*model
;
431 GSequencePtr end_ptr
;
433 g_return_if_fail (object
!= NULL
);
434 g_return_if_fail (RHYTHMDB_IS_PROPERTY_MODEL (object
));
436 model
= RHYTHMDB_PROPERTY_MODEL (object
);
438 rb_debug ("finalizing property model %p", model
);
440 g_return_if_fail (model
->priv
!= NULL
);
442 if (model
->priv
->syncing_id
!= 0)
443 g_source_remove (model
->priv
->syncing_id
);
445 end_ptr
= g_sequence_get_end_ptr (model
->priv
->properties
);
446 for (ptr
= g_sequence_get_begin_ptr (model
->priv
->properties
); ptr
!= end_ptr
;
447 ptr
= g_sequence_ptr_next (ptr
)) {
448 RhythmDBPropertyModelEntry
*prop
= g_sequence_ptr_get_data (ptr
);
449 rb_refstring_unref (prop
->string
);
450 rb_refstring_unref (prop
->sort_string
);
453 g_hash_table_destroy (model
->priv
->reverse_map
);
454 g_sequence_free (model
->priv
->properties
);
456 g_hash_table_destroy (model
->priv
->entries
);
458 if (model
->priv
->query_model
!= NULL
) {
459 g_object_unref (model
->priv
->query_model
);
462 G_OBJECT_CLASS (rhythmdb_property_model_parent_class
)->finalize (object
);
465 RhythmDBPropertyModel
*
466 rhythmdb_property_model_new (RhythmDB
*db
,
467 RhythmDBPropType propid
)
469 return g_object_new (RHYTHMDB_TYPE_PROPERTY_MODEL
, "db", db
, "prop", propid
, NULL
);
473 rhythmdb_property_model_row_inserted_cb (GtkTreeModel
*model
,
476 RhythmDBPropertyModel
*propmodel
)
478 RhythmDBEntry
*entry
;
479 RhythmDBPropertyModelEntry
*prop
;
481 entry
= rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model
), iter
);
483 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
484 rhythmdb_property_model_sync (propmodel
);
486 rhythmdb_entry_unref (entry
);
490 rhythmdb_property_model_prop_changed_cb (RhythmDB
*db
,
491 RhythmDBEntry
*entry
,
492 RhythmDBPropType propid
,
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 g_assert (g_hash_table_remove (propmodel
->priv
->entries
, entry
));
504 rhythmdb_property_model_insert (propmodel
, entry
);
506 g_assert (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
);
508 rhythmdb_property_model_delete (propmodel
, entry
);
509 g_hash_table_insert (propmodel
->priv
->entries
, entry
, GINT_TO_POINTER (1));
513 RhythmDBPropertyModelEntry
*prop
;
515 if (propid
!= propmodel
->priv
->propid
)
518 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) != NULL
)
521 rhythmdb_property_model_delete_prop (propmodel
, g_value_get_string (old
));
522 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
525 rhythmdb_property_model_sync (propmodel
);
529 rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel
*model
,
530 RhythmDBEntry
*entry
,
531 RhythmDBPropertyModel
*propmodel
)
533 if (g_hash_table_remove (propmodel
->priv
->entries
, entry
))
536 rhythmdb_property_model_delete (propmodel
, entry
);
537 rhythmdb_property_model_sync (propmodel
);
541 rhythmdb_property_model_compare (RhythmDBPropertyModelEntry
*a
,
542 RhythmDBPropertyModelEntry
*b
,
543 RhythmDBPropertyModel
*model
)
545 const char *a_str
, *b_str
;
547 a_str
= rb_refstring_get_sort_key (a
->sort_string
);
548 b_str
= rb_refstring_get_sort_key (b
->sort_string
);
550 return strcmp (a_str
, b_str
);
553 static RhythmDBPropertyModelEntry
*
554 rhythmdb_property_model_insert (RhythmDBPropertyModel
*model
,
555 RhythmDBEntry
*entry
)
557 RhythmDBPropertyModelEntry
*prop
;
563 propstr
= rhythmdb_entry_get_string (entry
, model
->priv
->propid
);
565 model
->priv
->all
->refcount
++;
567 if ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
))) {
568 prop
= g_sequence_ptr_get_data (ptr
);
570 rb_debug ("adding \"%s\": refcount %d", propstr
, prop
->refcount
);
573 rb_debug ("adding new property \"%s\"", propstr
);
575 prop
= g_new0 (RhythmDBPropertyModelEntry
, 1);
576 prop
->string
= rb_refstring_new (propstr
);
577 prop
->sort_string
= rb_refstring_new (rhythmdb_entry_get_string (entry
, model
->priv
->sort_propid
));
580 iter
.stamp
= model
->priv
->stamp
;
581 ptr
= g_sequence_insert_sorted (model
->priv
->properties
, prop
,
582 (GCompareDataFunc
) rhythmdb_property_model_compare
,
584 g_hash_table_insert (model
->priv
->reverse_map
,
585 (gpointer
)rb_refstring_get (prop
->string
),
588 iter
.user_data
= ptr
;
589 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
590 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model
), path
, &iter
);
591 gtk_tree_path_free (path
);
597 rhythmdb_property_model_delete (RhythmDBPropertyModel
*model
,
598 RhythmDBEntry
*entry
)
602 if (g_hash_table_lookup (model
->priv
->entries
, entry
))
605 propstr
= rhythmdb_entry_get_string (entry
, model
->priv
->propid
);
606 rhythmdb_property_model_delete_prop (model
, propstr
);
610 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel
*model
,
614 RhythmDBPropertyModelEntry
*prop
;
618 g_assert ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
)));
620 model
->priv
->all
->refcount
--;
622 prop
= g_sequence_ptr_get_data (ptr
);
623 rb_debug ("deleting \"%s\": refcount: %d", propstr
, prop
->refcount
);
625 if (prop
->refcount
> 0)
628 iter
.stamp
= model
->priv
->stamp
;
629 iter
.user_data
= ptr
;
631 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
632 g_signal_emit (G_OBJECT (model
), rhythmdb_property_model_signals
[PRE_ROW_DELETION
], 0);
633 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model
), path
);
634 gtk_tree_path_free (path
);
635 g_sequence_remove (ptr
);
636 g_hash_table_remove (model
->priv
->reverse_map
, propstr
);
637 rb_refstring_unref (prop
->string
);
638 rb_refstring_unref (prop
->sort_string
);
644 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel
*model
,
652 iter
->stamp
= model
->priv
->stamp
;
653 iter
->user_data
= model
->priv
->all
;
658 ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, name
);
663 iter
->stamp
= model
->priv
->stamp
;
664 iter
->user_data
= ptr
;
670 static GtkTreeModelFlags
671 rhythmdb_property_model_get_flags (GtkTreeModel
*model
)
673 return GTK_TREE_MODEL_ITERS_PERSIST
| GTK_TREE_MODEL_LIST_ONLY
;
677 rhythmdb_property_model_get_n_columns (GtkTreeModel
*tree_model
)
679 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST
;
683 rhythmdb_property_model_get_column_type (GtkTreeModel
*tree_model
,
687 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
688 return G_TYPE_STRING
;
689 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
690 return G_TYPE_BOOLEAN
;
691 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
694 g_assert_not_reached ();
695 return G_TYPE_INVALID
;
700 rhythmdb_property_model_get_iter (GtkTreeModel
*tree_model
,
704 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
708 index
= gtk_tree_path_get_indices (path
)[0];
711 iter
->stamp
= model
->priv
->stamp
;
712 iter
->user_data
= model
->priv
->all
;
717 if (index
>= g_sequence_get_length (model
->priv
->properties
))
720 ptr
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, index
);
722 iter
->stamp
= model
->priv
->stamp
;
723 iter
->user_data
= ptr
;
729 rhythmdb_property_model_get_path (GtkTreeModel
*tree_model
,
732 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
735 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, NULL
);
737 if (iter
->user_data
== model
->priv
->all
) {
738 return gtk_tree_path_new_first ();
741 if (g_sequence_ptr_is_end (iter
->user_data
))
744 path
= gtk_tree_path_new ();
745 if (iter
->user_data
== model
->priv
->all
)
746 gtk_tree_path_append_index (path
, 0);
748 gtk_tree_path_append_index (path
, g_sequence_ptr_get_position (iter
->user_data
) + 1);
753 rhythmdb_property_model_get_value (GtkTreeModel
*tree_model
,
758 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
760 g_return_if_fail (model
->priv
->stamp
== iter
->stamp
);
762 if (iter
->user_data
== model
->priv
->all
) {
764 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
765 g_value_init (value
, G_TYPE_STRING
);
766 g_value_set_string (value
, rb_refstring_get (model
->priv
->all
->string
));
768 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
769 g_value_init (value
, G_TYPE_BOOLEAN
);
770 g_value_set_boolean (value
, TRUE
);
772 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
773 g_value_init (value
, G_TYPE_UINT
);
774 g_value_set_uint (value
, model
->priv
->all
->refcount
);
777 g_assert_not_reached ();
780 RhythmDBPropertyModelEntry
*prop
;
782 g_return_if_fail (!g_sequence_ptr_is_end (iter
->user_data
));
784 prop
= g_sequence_ptr_get_data (iter
->user_data
);
786 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
787 g_value_init (value
, G_TYPE_STRING
);
788 g_value_set_string (value
, rb_refstring_get (prop
->string
));
790 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
791 g_value_init (value
, G_TYPE_BOOLEAN
);
792 g_value_set_boolean (value
, prop
== model
->priv
->all
);
794 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
795 g_value_init (value
, G_TYPE_UINT
);
796 g_value_set_uint (value
, prop
->refcount
);
799 g_assert_not_reached ();
805 rhythmdb_property_model_iter_next (GtkTreeModel
*tree_model
,
808 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
810 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, FALSE
);
812 if (iter
->user_data
== model
->priv
->all
) {
813 iter
->user_data
= g_sequence_get_begin_ptr (model
->priv
->properties
);
815 g_return_val_if_fail (!g_sequence_ptr_is_end (iter
->user_data
), FALSE
);
816 iter
->user_data
= g_sequence_ptr_next (iter
->user_data
);
819 return !g_sequence_ptr_is_end (iter
->user_data
);
823 rhythmdb_property_model_iter_children (GtkTreeModel
*tree_model
,
827 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
832 iter
->stamp
= model
->priv
->stamp
;
833 iter
->user_data
= model
->priv
->all
;
839 rhythmdb_property_model_iter_has_child (GtkTreeModel
*tree_model
,
846 rhythmdb_property_model_iter_n_children (GtkTreeModel
*tree_model
,
849 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
852 g_return_val_if_fail (model
->priv
->stamp
== iter
->stamp
, -1);
855 return 1 + g_sequence_get_length (model
->priv
->properties
);
861 rhythmdb_property_model_iter_nth_child (GtkTreeModel
*tree_model
,
866 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
873 child
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, n
);
875 if (g_sequence_ptr_is_end (child
))
877 iter
->user_data
= child
;
879 iter
->user_data
= model
->priv
->all
;
882 iter
->stamp
= model
->priv
->stamp
;
888 rhythmdb_property_model_iter_parent (GtkTreeModel
*tree_model
,
896 rhythmdb_property_model_row_draggable (RbTreeDragSource
*dragsource
,
903 rhythmdb_property_model_drag_data_delete (RbTreeDragSource
*dragsource
,
910 /*Going through hoops to avoid nested functions*/
911 struct QueryModelCbStruct
{
917 query_model_cb (GtkTreeModel
*query_model
,
920 struct QueryModelCbStruct
*data
)
923 RhythmDBEntry
*entry
;
925 gtk_tree_model_get (query_model
, iter
, 0, &entry
, -1);
926 uri
= rhythmdb_entry_get_string (entry
, RHYTHMDB_PROP_LOCATION
);
927 g_string_append (data
->reply
, uri
);
928 g_string_append (data
->reply
, "\r\n");
930 rhythmdb_entry_unref (entry
);
935 rhythmdb_property_model_drag_data_get (RbTreeDragSource
*dragsource
,
937 GtkSelectionData
*selection_data
)
939 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (dragsource
);
941 GtkTargetList
*drag_target_list
;
943 switch (model
->priv
->propid
) {
944 case RHYTHMDB_PROP_GENRE
:
945 drag_target_list
= rhythmdb_property_model_genre_drag_target_list
;
947 case RHYTHMDB_PROP_ALBUM
:
948 drag_target_list
= rhythmdb_property_model_album_drag_target_list
;
950 case RHYTHMDB_PROP_ARTIST
:
951 drag_target_list
= rhythmdb_property_model_artist_drag_target_list
;
953 case RHYTHMDB_PROP_LOCATION
:
954 drag_target_list
= rhythmdb_property_model_location_drag_target_list
;
957 g_assert_not_reached ();
960 if (!gtk_target_list_find (drag_target_list
,
961 selection_data
->target
,
966 if (target
== TARGET_URIS
) {
967 RhythmDB
*db
= model
->priv
->db
;
968 RhythmDBQueryModel
*query_model
;
969 GString
* reply
= g_string_new ("");
971 gboolean is_all
= FALSE
;
972 struct QueryModelCbStruct tmp
;
974 GCompareDataFunc sort_func
= NULL
;
976 gboolean sort_reverse
;
978 query_model
= rhythmdb_query_model_new_empty (db
);
979 /* FIXME the sort order on the query model at this point is usually
980 * not the user's selected sort order.
982 g_object_get (G_OBJECT (model
->priv
->query_model
),
983 "sort-func", &sort_func
,
984 "sort-data", &sort_data
,
985 "sort-reverse", &sort_reverse
,
987 rhythmdb_query_model_set_sort_order (RHYTHMDB_QUERY_MODEL (query_model
),
988 sort_func
, GUINT_TO_POINTER (sort_data
), NULL
, sort_reverse
);
990 rb_debug ("getting drag data as uri list");
991 /* check if first selected row is 'All' */
992 path
= gtk_tree_row_reference_get_path (paths
->data
);
993 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
))
994 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
995 RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
,
997 gtk_tree_path_free (path
);
999 g_object_set (query_model
,
1000 "base-model", model
->priv
->query_model
,
1004 GPtrArray
*subquery
= g_ptr_array_new ();
1006 for (row
= paths
; row
; row
= row
->next
) {
1008 path
= gtk_tree_row_reference_get_path (row
->data
);
1009 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
)) {
1010 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1011 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
,
1014 rhythmdb_query_append (db
, subquery
,
1015 RHYTHMDB_QUERY_PROP_EQUALS
,
1016 model
->priv
->propid
, name
,
1017 RHYTHMDB_QUERY_END
);
1019 rhythmdb_query_append (db
, subquery
,
1020 RHYTHMDB_QUERY_DISJUNCTION
,
1021 RHYTHMDB_QUERY_PROP_EQUALS
,
1022 model
->priv
->propid
, name
,
1023 RHYTHMDB_QUERY_END
);
1027 gtk_tree_path_free (path
);
1031 g_object_set (query_model
,
1033 "base-model", model
->priv
->query_model
,
1035 rhythmdb_query_free (subquery
);
1040 /* Too bad that we're on the main thread. Why doesn't gtk call us async?
1041 * How does file-roller manage? - it seems it refuses the drop when it isn't
1042 * done unpacking. In which case, we should tweak the drop acknowledgement,
1043 * and prepare the query using do_full_query_async. The query would be
1044 * hooked to the drag context.
1046 gtk_tree_model_foreach (GTK_TREE_MODEL (query_model
),
1047 (GtkTreeModelForeachFunc
) query_model_cb
,
1050 g_object_unref (query_model
);
1052 gtk_selection_data_set (selection_data
,
1053 selection_data
->target
,
1054 8, (guchar
*)reply
->str
,
1056 g_string_free (reply
, TRUE
);
1061 GString
* reply
= g_string_new ("");
1063 rb_debug ("getting drag data as list of property values");
1065 for (p
= paths
; p
; p
= p
->next
) {
1069 path
= gtk_tree_row_reference_get_path (p
->data
);
1070 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
)) {
1071 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1072 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, &title
, -1);
1073 g_string_append (reply
, title
);
1075 g_string_append (reply
, "\r\n");
1078 gtk_tree_path_free (path
);
1080 gtk_selection_data_set (selection_data
,
1081 selection_data
->target
,
1082 8, (guchar
*)reply
->str
,
1084 g_string_free (reply
, TRUE
);
1091 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel
*model
,
1094 const GtkTargetEntry
*targets
;
1097 switch (model
->priv
->propid
) {
1098 case RHYTHMDB_PROP_GENRE
:
1099 targets
= targets_genre
;
1100 n_elements
= G_N_ELEMENTS (targets_genre
);
1102 case RHYTHMDB_PROP_ALBUM
:
1103 targets
= targets_album
;
1104 n_elements
= G_N_ELEMENTS (targets_album
);
1106 case RHYTHMDB_PROP_ARTIST
:
1107 targets
= targets_artist
;
1108 n_elements
= G_N_ELEMENTS (targets_artist
);
1110 case RHYTHMDB_PROP_LOCATION
:
1111 targets
= targets_location
;
1112 n_elements
= G_N_ELEMENTS (targets_location
);
1115 g_assert_not_reached ();
1118 rb_tree_dnd_add_drag_source_support (view
,
1120 targets
, n_elements
,
1125 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel
*model
)
1130 GDK_THREADS_ENTER ();
1132 iter
.stamp
= model
->priv
->stamp
;
1133 iter
.user_data
= model
->priv
->all
;
1134 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
1135 gtk_tree_model_row_changed (GTK_TREE_MODEL (model
), path
, &iter
);
1136 gtk_tree_path_free (path
);
1138 model
->priv
->syncing_id
= 0;
1139 GDK_THREADS_LEAVE ();
1144 rhythmdb_property_model_sync (RhythmDBPropertyModel
*model
)
1146 if (model
->priv
->syncing_id
!= 0)
1149 model
->priv
->syncing_id
= g_idle_add ((GSourceFunc
)rhythmdb_property_model_perform_sync
, model
);
1152 /* This should really be standard. */
1153 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1156 rhythmdb_property_model_column_get_type (void)
1158 static GType etype
= 0;
1161 static const GEnumValue values
[] = {
1162 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, "Property title"),
1163 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
, "Value priority"),
1164 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
, "Track count"),
1168 etype
= g_enum_register_static ("RhythmDBPropertyModelColumn", values
);