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.
27 #include <glib/gi18n.h>
29 #include "rhythmdb-property-model.h"
31 #include "gsequence.h"
32 #include "rb-refstring.h"
33 #include "rb-tree-dnd.h"
35 static void rhythmdb_property_model_tree_model_init (GtkTreeModelIface
*iface
);
36 static void rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface
*iface
);
38 G_DEFINE_TYPE_WITH_CODE(RhythmDBPropertyModel
, rhythmdb_property_model
, G_TYPE_OBJECT
,
39 G_IMPLEMENT_INTERFACE(GTK_TYPE_TREE_MODEL
,
40 rhythmdb_property_model_tree_model_init
)
41 G_IMPLEMENT_INTERFACE(RB_TYPE_TREE_DRAG_SOURCE
,
42 rhythmdb_property_model_drag_source_init
))
46 RBRefString
*sort_string
;
48 } 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
},
137 static GtkTargetList
*rhythmdb_property_model_album_drag_target_list
= NULL
;
138 static GtkTargetList
*rhythmdb_property_model_artist_drag_target_list
= NULL
;
139 static GtkTargetList
*rhythmdb_property_model_genre_drag_target_list
= NULL
;
140 static GtkTargetList
*rhythmdb_property_model_location_drag_target_list
= NULL
;
142 struct RhythmDBPropertyModelPrivate
146 RhythmDBQueryModel
*query_model
;
149 RhythmDBPropType propid
;
150 RhythmDBPropType sort_propid
;
156 GSequence
*properties
;
157 GHashTable
*reverse_map
;
159 RhythmDBPropertyModelEntry
*all
;
165 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
181 static guint rhythmdb_property_model_signals
[LAST_SIGNAL
] = { 0 };
184 rhythmdb_property_model_class_init (RhythmDBPropertyModelClass
*klass
)
186 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
188 if (!rhythmdb_property_model_artist_drag_target_list
)
189 rhythmdb_property_model_artist_drag_target_list
=
190 gtk_target_list_new (targets_artist
,
191 G_N_ELEMENTS (targets_artist
));
192 if (!rhythmdb_property_model_album_drag_target_list
)
193 rhythmdb_property_model_album_drag_target_list
=
194 gtk_target_list_new (targets_album
,
195 G_N_ELEMENTS (targets_album
));
196 if (!rhythmdb_property_model_genre_drag_target_list
)
197 rhythmdb_property_model_genre_drag_target_list
=
198 gtk_target_list_new (targets_genre
,
199 G_N_ELEMENTS (targets_genre
));
200 if (!rhythmdb_property_model_location_drag_target_list
)
201 rhythmdb_property_model_location_drag_target_list
=
202 gtk_target_list_new (targets_location
,
203 G_N_ELEMENTS (targets_location
));
205 object_class
->set_property
= rhythmdb_property_model_set_property
;
206 object_class
->get_property
= rhythmdb_property_model_get_property
;
208 object_class
->finalize
= rhythmdb_property_model_finalize
;
210 rhythmdb_property_model_signals
[PRE_ROW_DELETION
] =
211 g_signal_new ("pre-row-deletion",
212 G_OBJECT_CLASS_TYPE (object_class
),
214 G_STRUCT_OFFSET (RhythmDBPropertyModelClass
, pre_row_deletion
),
216 g_cclosure_marshal_VOID__VOID
,
220 g_object_class_install_property (object_class
,
222 g_param_spec_object ("db",
226 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
228 g_object_class_install_property (object_class
,
230 g_param_spec_int ("prop",
233 0, RHYTHMDB_NUM_PROPERTIES
,
235 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
236 g_object_class_install_property (object_class
,
238 g_param_spec_object ("query-model",
239 "RhythmDBQueryModel",
240 "RhythmDBQueryModel object ",
241 RHYTHMDB_TYPE_QUERY_MODEL
,
244 g_type_class_add_private (klass
, sizeof (RhythmDBPropertyModelPrivate
));
248 rhythmdb_property_model_tree_model_init (GtkTreeModelIface
*iface
)
250 iface
->get_flags
= rhythmdb_property_model_get_flags
;
251 iface
->get_n_columns
= rhythmdb_property_model_get_n_columns
;
252 iface
->get_column_type
= rhythmdb_property_model_get_column_type
;
253 iface
->get_iter
= rhythmdb_property_model_get_iter
;
254 iface
->get_path
= rhythmdb_property_model_get_path
;
255 iface
->get_value
= rhythmdb_property_model_get_value
;
256 iface
->iter_next
= rhythmdb_property_model_iter_next
;
257 iface
->iter_children
= rhythmdb_property_model_iter_children
;
258 iface
->iter_has_child
= rhythmdb_property_model_iter_has_child
;
259 iface
->iter_n_children
= rhythmdb_property_model_iter_n_children
;
260 iface
->iter_nth_child
= rhythmdb_property_model_iter_nth_child
;
261 iface
->iter_parent
= rhythmdb_property_model_iter_parent
;
265 rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface
*iface
)
267 iface
->row_draggable
= rhythmdb_property_model_row_draggable
;
268 iface
->drag_data_delete
= rhythmdb_property_model_drag_data_delete
;
269 iface
->drag_data_get
= rhythmdb_property_model_drag_data_get
;
273 _remove_entry_cb (RhythmDBEntry
*entry
,
275 RhythmDBPropertyModel
*model
)
277 rhythmdb_property_model_delete (model
, entry
);
282 _add_entry_cb (GtkTreeModel
*model
,
285 RhythmDBPropertyModel
*propmodel
)
287 rhythmdb_property_model_row_inserted_cb (model
, path
, iter
, propmodel
);
292 rhythmdb_property_model_set_property (GObject
*object
,
297 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
302 model
->priv
->db
= g_value_get_object (value
);
306 model
->priv
->propid
= g_value_get_int (value
);
307 switch (model
->priv
->propid
) {
308 case RHYTHMDB_PROP_GENRE
:
309 model
->priv
->sort_propid
= RHYTHMDB_PROP_GENRE
;
311 case RHYTHMDB_PROP_ARTIST
:
312 model
->priv
->sort_propid
= RHYTHMDB_PROP_ARTIST
;
314 case RHYTHMDB_PROP_ALBUM
:
315 model
->priv
->sort_propid
= RHYTHMDB_PROP_ALBUM
;
317 case RHYTHMDB_PROP_SUBTITLE
:
318 model
->priv
->sort_propid
= RHYTHMDB_PROP_SUBTITLE
;
320 case RHYTHMDB_PROP_TITLE
:
321 case RHYTHMDB_PROP_LOCATION
:
322 model
->priv
->sort_propid
= RHYTHMDB_PROP_TITLE
;
325 g_assert_not_reached ();
329 case PROP_QUERY_MODEL
:
331 if (model
->priv
->query_model
) {
332 g_signal_handlers_disconnect_by_func (G_OBJECT (model
->priv
->query_model
),
333 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
335 g_signal_handlers_disconnect_by_func (G_OBJECT (model
->priv
->query_model
),
336 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
338 g_signal_handlers_disconnect_by_func (G_OBJECT (model
->priv
->query_model
),
339 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
341 g_hash_table_foreach_remove (model
->priv
->entries
, (GHRFunc
)_remove_entry_cb
, model
);
342 g_object_unref (G_OBJECT (model
->priv
->query_model
));
345 model
->priv
->query_model
= g_value_get_object (value
);
346 g_assert (rhythmdb_property_model_iter_n_children (GTK_TREE_MODEL (model
), NULL
) == 1);
348 if (model
->priv
->query_model
) {
349 g_object_ref (G_OBJECT (model
->priv
->query_model
));
350 g_signal_connect_object (G_OBJECT (model
->priv
->query_model
),
352 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
355 g_signal_connect_object (G_OBJECT (model
->priv
->query_model
),
357 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
360 g_signal_connect_object (G_OBJECT (model
->priv
->query_model
),
361 "entry-prop-changed",
362 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
365 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
366 (GtkTreeModelForeachFunc
)_add_entry_cb
,
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
378 rhythmdb_property_model_get_property (GObject
*object
,
383 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
387 g_value_set_object (value
, model
->priv
->db
);
390 g_value_set_int (value
, model
->priv
->propid
);
392 case PROP_QUERY_MODEL
:
393 g_value_set_object (value
, model
->priv
->query_model
);
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
402 rhythmdb_property_model_init (RhythmDBPropertyModel
*model
)
404 model
->priv
= RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE (model
);
406 model
->priv
->stamp
= g_random_int ();
408 model
->priv
->properties
= g_sequence_new (NULL
);
409 model
->priv
->reverse_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
410 model
->priv
->entries
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
412 model
->priv
->all
= g_new0 (RhythmDBPropertyModelEntry
, 1);
413 model
->priv
->all
->string
= rb_refstring_new (_("All"));
417 rhythmdb_property_model_finalize (GObject
*object
)
419 RhythmDBPropertyModel
*model
;
421 GSequencePtr end_ptr
;
423 g_return_if_fail (object
!= NULL
);
424 g_return_if_fail (RHYTHMDB_IS_PROPERTY_MODEL (object
));
426 model
= RHYTHMDB_PROPERTY_MODEL (object
);
428 rb_debug ("finalizing property model %p", model
);
430 g_return_if_fail (model
->priv
!= NULL
);
432 if (model
->priv
->syncing_id
!= 0)
433 g_source_remove (model
->priv
->syncing_id
);
435 end_ptr
= g_sequence_get_end_ptr (model
->priv
->properties
);
436 for (ptr
= g_sequence_get_begin_ptr (model
->priv
->properties
); ptr
!= end_ptr
;
437 ptr
= g_sequence_ptr_next (ptr
)) {
438 RhythmDBPropertyModelEntry
*prop
= g_sequence_ptr_get_data (ptr
);
439 rb_refstring_unref (prop
->string
);
440 rb_refstring_unref (prop
->sort_string
);
443 g_hash_table_destroy (model
->priv
->reverse_map
);
444 g_sequence_free (model
->priv
->properties
);
445 g_hash_table_destroy (model
->priv
->entries
);
447 if (model
->priv
->query_model
)
448 g_object_unref (G_OBJECT (model
->priv
->query_model
));
450 G_OBJECT_CLASS (rhythmdb_property_model_parent_class
)->finalize (object
);
453 RhythmDBPropertyModel
*
454 rhythmdb_property_model_new (RhythmDB
*db
, RhythmDBPropType propid
)
456 return g_object_new (RHYTHMDB_TYPE_PROPERTY_MODEL
, "db", db
, "prop", propid
, NULL
);
460 rhythmdb_property_model_row_inserted_cb (GtkTreeModel
*model
,
463 RhythmDBPropertyModel
*propmodel
)
465 RhythmDBEntry
*entry
= rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model
), iter
);
466 RhythmDBPropertyModelEntry
*prop
;
468 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
))
472 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
473 g_hash_table_insert (propmodel
->priv
->entries
, entry
, prop
);
475 rhythmdb_property_model_sync (propmodel
);
479 rhythmdb_property_model_prop_changed_cb (RhythmDB
*db
, RhythmDBEntry
*entry
,
480 RhythmDBPropType propid
, const GValue
*old
,
482 RhythmDBPropertyModel
*propmodel
)
484 if (propid
== RHYTHMDB_PROP_HIDDEN
) {
485 gboolean old_val
= g_value_get_boolean (old
);
486 gboolean new_val
= g_value_get_boolean (new);
488 if (old_val
!= new_val
) {
489 if (new_val
== FALSE
) {
490 RhythmDBPropertyModelEntry
*prop
;
492 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) != NULL
)
495 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
496 g_hash_table_insert (propmodel
->priv
->entries
, entry
, prop
);
498 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
)
501 rhythmdb_property_model_delete (propmodel
, entry
);
502 g_hash_table_remove (propmodel
->priv
->entries
, entry
);
506 RhythmDBPropertyModelEntry
*prop
;
508 if (propid
!= propmodel
->priv
->propid
)
511 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
)
514 rhythmdb_property_model_delete (propmodel
, entry
);
515 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
516 g_hash_table_insert (propmodel
->priv
->entries
, entry
, prop
);
519 rhythmdb_property_model_sync (propmodel
);
523 rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel
*model
,
524 RhythmDBEntry
*entry
,
525 RhythmDBPropertyModel
*propmodel
)
527 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
)
530 rhythmdb_property_model_delete (propmodel
, entry
);
531 g_hash_table_remove (propmodel
->priv
->entries
, entry
);
532 rhythmdb_property_model_sync (propmodel
);
536 rhythmdb_property_model_compare (RhythmDBPropertyModelEntry
*a
, RhythmDBPropertyModelEntry
*b
, RhythmDBPropertyModel
*model
)
538 const char *a_str
, *b_str
;
540 a_str
= rb_refstring_get_sort_key (a
->sort_string
);
541 b_str
= rb_refstring_get_sort_key (b
->sort_string
);
543 return strcmp (a_str
, b_str
);
546 static RhythmDBPropertyModelEntry
*
547 rhythmdb_property_model_insert (RhythmDBPropertyModel
*model
, RhythmDBEntry
*entry
)
549 RhythmDBPropertyModelEntry
*prop
;
555 propstr
= rhythmdb_entry_get_string (entry
, model
->priv
->propid
);
557 model
->priv
->all
->refcount
++;
559 if ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
))) {
560 prop
= g_sequence_ptr_get_data (ptr
);
562 rb_debug ("adding \"%s\": refcount %d", propstr
, prop
->refcount
);
565 rb_debug ("adding new property \"%s\"", propstr
);
567 prop
= g_new0 (RhythmDBPropertyModelEntry
, 1);
568 prop
->string
= rb_refstring_new (propstr
);
569 prop
->sort_string
= rb_refstring_new (rhythmdb_entry_get_string (entry
, model
->priv
->sort_propid
));
572 iter
.stamp
= model
->priv
->stamp
;
573 ptr
= g_sequence_insert_sorted (model
->priv
->properties
, prop
,
574 (GCompareDataFunc
) rhythmdb_property_model_compare
,
576 g_hash_table_insert (model
->priv
->reverse_map
,
577 (gpointer
)rb_refstring_get (prop
->string
),
580 iter
.user_data
= ptr
;
581 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
582 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model
), path
, &iter
);
583 gtk_tree_path_free (path
);
589 rhythmdb_property_model_delete (RhythmDBPropertyModel
*model
,
590 RhythmDBEntry
*entry
)
592 RhythmDBPropertyModelEntry
*prop
;
595 prop
= g_hash_table_lookup (model
->priv
->entries
, entry
);
596 propstr
= rb_refstring_get (prop
->string
);
597 rhythmdb_property_model_delete_prop (model
, propstr
);
601 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel
*model
,
605 RhythmDBPropertyModelEntry
*prop
;
609 g_assert ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
)));
611 model
->priv
->all
->refcount
--;
613 prop
= g_sequence_ptr_get_data (ptr
);
614 rb_debug ("deleting \"%s\": refcount: %d", propstr
, prop
->refcount
);
616 if (prop
->refcount
> 0)
619 iter
.stamp
= model
->priv
->stamp
;
620 iter
.user_data
= ptr
;
622 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
623 g_signal_emit (G_OBJECT (model
), rhythmdb_property_model_signals
[PRE_ROW_DELETION
], 0);
624 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model
), path
);
625 gtk_tree_path_free (path
);
626 g_sequence_remove (ptr
);
627 g_hash_table_remove (model
->priv
->reverse_map
, propstr
);
628 rb_refstring_unref (prop
->string
);
629 rb_refstring_unref (prop
->sort_string
);
635 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel
*model
,
636 const char *name
, GtkTreeIter
*iter
)
642 iter
->stamp
= model
->priv
->stamp
;
643 iter
->user_data
= model
->priv
->all
;
648 ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, name
);
653 iter
->stamp
= model
->priv
->stamp
;
654 iter
->user_data
= ptr
;
660 static GtkTreeModelFlags
661 rhythmdb_property_model_get_flags (GtkTreeModel
*model
)
663 return GTK_TREE_MODEL_ITERS_PERSIST
| GTK_TREE_MODEL_LIST_ONLY
;
667 rhythmdb_property_model_get_n_columns (GtkTreeModel
*tree_model
)
669 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST
;
673 rhythmdb_property_model_get_column_type (GtkTreeModel
*tree_model
, int index
)
676 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
677 return G_TYPE_STRING
;
678 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
679 return G_TYPE_BOOLEAN
;
680 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
683 g_assert_not_reached ();
684 return G_TYPE_INVALID
;
689 rhythmdb_property_model_get_iter (GtkTreeModel
*tree_model
, GtkTreeIter
*iter
,
692 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
696 index
= gtk_tree_path_get_indices (path
)[0];
699 iter
->stamp
= model
->priv
->stamp
;
700 iter
->user_data
= model
->priv
->all
;
705 if (index
>= g_sequence_get_length (model
->priv
->properties
))
708 ptr
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, index
);
710 iter
->stamp
= model
->priv
->stamp
;
711 iter
->user_data
= ptr
;
717 rhythmdb_property_model_get_path (GtkTreeModel
*tree_model
,
720 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
723 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, NULL
);
725 if (g_sequence_ptr_is_end (iter
->user_data
))
728 path
= gtk_tree_path_new ();
729 if (iter
->user_data
== model
->priv
->all
)
730 gtk_tree_path_append_index (path
, 0);
732 gtk_tree_path_append_index (path
, g_sequence_ptr_get_position (iter
->user_data
)+1);
737 rhythmdb_property_model_get_value (GtkTreeModel
*tree_model
, GtkTreeIter
*iter
,
738 gint column
, GValue
*value
)
740 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
742 g_return_if_fail (!g_sequence_ptr_is_end (iter
->user_data
));
743 g_return_if_fail (model
->priv
->stamp
== iter
->stamp
);
745 if (iter
->user_data
== model
->priv
->all
) {
747 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
748 g_value_init (value
, G_TYPE_STRING
);
749 g_value_set_string (value
, rb_refstring_get (model
->priv
->all
->string
));
751 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
752 g_value_init (value
, G_TYPE_BOOLEAN
);
753 g_value_set_boolean (value
, TRUE
);
755 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
756 g_value_init (value
, G_TYPE_UINT
);
757 g_value_set_uint (value
, model
->priv
->all
->refcount
);
760 g_assert_not_reached ();
763 RhythmDBPropertyModelEntry
*prop
= g_sequence_ptr_get_data (iter
->user_data
);
766 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
767 g_value_init (value
, G_TYPE_STRING
);
768 g_value_set_string (value
, rb_refstring_get (prop
->string
));
770 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
771 g_value_init (value
, G_TYPE_BOOLEAN
);
772 g_value_set_boolean (value
, prop
== model
->priv
->all
);
774 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
775 g_value_init (value
, G_TYPE_UINT
);
776 g_value_set_uint (value
, prop
->refcount
);
779 g_assert_not_reached ();
785 rhythmdb_property_model_iter_next (GtkTreeModel
*tree_model
,
788 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
790 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, FALSE
);
792 if (iter
->user_data
== model
->priv
->all
)
793 iter
->user_data
= g_sequence_get_begin_ptr (model
->priv
->properties
);
795 iter
->user_data
= g_sequence_ptr_next (iter
->user_data
);
797 return !g_sequence_ptr_is_end (iter
->user_data
);
801 rhythmdb_property_model_iter_children (GtkTreeModel
*tree_model
,
805 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
810 iter
->stamp
= model
->priv
->stamp
;
811 iter
->user_data
= model
->priv
->all
;
817 rhythmdb_property_model_iter_has_child (GtkTreeModel
*tree_model
,
824 rhythmdb_property_model_iter_n_children (GtkTreeModel
*tree_model
,
827 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
830 g_return_val_if_fail (model
->priv
->stamp
== iter
->stamp
, -1);
833 return 1 + g_sequence_get_length (model
->priv
->properties
);
839 rhythmdb_property_model_iter_nth_child (GtkTreeModel
*tree_model
,
840 GtkTreeIter
*iter
, GtkTreeIter
*parent
,
843 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
850 child
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, n
);
852 if (g_sequence_ptr_is_end (child
))
854 iter
->user_data
= child
;
856 iter
->user_data
= model
->priv
->all
;
859 iter
->stamp
= model
->priv
->stamp
;
865 rhythmdb_property_model_iter_parent (GtkTreeModel
*tree_model
,
873 rhythmdb_property_model_row_draggable (RbTreeDragSource
*dragsource
,
880 rhythmdb_property_model_drag_data_delete (RbTreeDragSource
*dragsource
,
887 /*Going through hoops to avoid nested functions*/
888 struct QueryModelCbStruct
{
894 query_model_cb (GtkTreeModel
*query_model
,
895 GtkTreePath
*path
, GtkTreeIter
*iter
,
896 struct QueryModelCbStruct
*data
)
899 RhythmDBEntry
* entry
;
901 gtk_tree_model_get (query_model
, iter
, 0, &entry
, -1);
902 uri
= rhythmdb_entry_get_string (entry
, RHYTHMDB_PROP_LOCATION
);
903 g_string_append (data
->reply
, uri
);
904 g_string_append (data
->reply
, "\r\n");
906 rhythmdb_entry_unref (data
->db
, entry
);
911 rhythmdb_property_model_drag_data_get (RbTreeDragSource
*dragsource
,
913 GtkSelectionData
*selection_data
)
915 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (dragsource
);
917 GtkTargetList
*drag_target_list
;
919 switch (model
->priv
->propid
) {
920 case RHYTHMDB_PROP_GENRE
:
921 drag_target_list
= rhythmdb_property_model_genre_drag_target_list
;
923 case RHYTHMDB_PROP_ALBUM
:
924 drag_target_list
= rhythmdb_property_model_album_drag_target_list
;
926 case RHYTHMDB_PROP_ARTIST
:
927 drag_target_list
= rhythmdb_property_model_artist_drag_target_list
;
929 case RHYTHMDB_PROP_LOCATION
:
930 drag_target_list
= rhythmdb_property_model_location_drag_target_list
;
933 g_assert_not_reached ();
936 if (!gtk_target_list_find (drag_target_list
,
937 selection_data
->target
,
942 if (target
== TARGET_URIS
) {
943 RhythmDB
*db
= model
->priv
->db
;
944 RhythmDBQueryModel
*query_model
;
945 GString
* reply
= g_string_new ("");
948 struct QueryModelCbStruct tmp
;
950 GCompareDataFunc sort_func
= NULL
;
952 gboolean sort_reverse
;
954 query_model
= rhythmdb_query_model_new_empty (db
);
955 /* FIXME the sort order on the query model at this point is usually
956 * not the user's selected sort order.
958 g_object_get (G_OBJECT (model
->priv
->query_model
),
959 "sort-func", &sort_func
,
960 "sort-data", &sort_data
,
961 "sort-reverse", &sort_reverse
,
963 rhythmdb_query_model_set_sort_order (RHYTHMDB_QUERY_MODEL (query_model
),
964 sort_func
, GUINT_TO_POINTER (sort_data
), NULL
, sort_reverse
);
966 rb_debug ("getting drag data as uri list");
967 /* check if first selected row is 'All' */
968 path
= gtk_tree_row_reference_get_path (paths
->data
);
969 gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
);
970 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
971 RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
,
973 gtk_tree_path_free (path
);
975 g_object_set (G_OBJECT (query_model
),
976 "base-model", model
->priv
->query_model
,
981 GPtrArray
*subquery
= g_ptr_array_new ();
983 for (row
= paths
; row
; row
= row
->next
) {
985 path
= gtk_tree_row_reference_get_path (row
->data
);
986 gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
);
987 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
988 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
,
991 rhythmdb_query_append (db
, subquery
,
992 RHYTHMDB_QUERY_PROP_EQUALS
,
993 model
->priv
->propid
, name
,
996 rhythmdb_query_append (db
, subquery
,
997 RHYTHMDB_QUERY_DISJUNCTION
,
998 RHYTHMDB_QUERY_PROP_EQUALS
,
999 model
->priv
->propid
, name
,
1000 RHYTHMDB_QUERY_END
);
1003 gtk_tree_path_free (path
);
1006 query
= rhythmdb_query_parse (db
,
1007 RHYTHMDB_QUERY_SUBQUERY
, subquery
,
1008 RHYTHMDB_QUERY_END
);
1010 g_object_set (G_OBJECT (query_model
),
1012 "base-model", model
->priv
->query_model
,
1014 rhythmdb_query_free (query
);
1019 /* Too bad that we're on the main thread. Why doesn't gtk call us async?
1020 * How does file-roller manage? - it seems it refuses the drop when it isn't
1021 * done unpacking. In which case, we should tweak the drop acknowledgement,
1022 * and prepare the query using do_full_query_async. The query would be
1023 * hooked to the drag context.
1025 gtk_tree_model_foreach (GTK_TREE_MODEL (query_model
),
1026 (GtkTreeModelForeachFunc
) query_model_cb
,
1029 g_object_unref (query_model
);
1031 gtk_selection_data_set (selection_data
,
1032 selection_data
->target
,
1033 8, (guchar
*)reply
->str
,
1035 g_string_free (reply
, TRUE
);
1040 GString
* reply
= g_string_new ("");
1042 rb_debug ("getting drag data as list of property values");
1044 for (p
= paths
; p
; p
= p
->next
) {
1048 path
= gtk_tree_row_reference_get_path (p
->data
);
1049 gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
);
1050 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1051 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, &title
, -1);
1052 g_string_append (reply
, title
);
1054 g_string_append (reply
, "\r\n");
1056 gtk_tree_path_free (path
);
1058 gtk_selection_data_set (selection_data
,
1059 selection_data
->target
,
1060 8, (guchar
*)reply
->str
,
1062 g_string_free (reply
, TRUE
);
1069 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel
*model
, GtkTreeView
*view
)
1071 const GtkTargetEntry
*targets
;
1074 switch (model
->priv
->propid
) {
1075 case RHYTHMDB_PROP_GENRE
:
1076 targets
= targets_genre
;
1077 n_elements
= G_N_ELEMENTS (targets_genre
);
1079 case RHYTHMDB_PROP_ALBUM
:
1080 targets
= targets_album
;
1081 n_elements
= G_N_ELEMENTS (targets_album
);
1083 case RHYTHMDB_PROP_ARTIST
:
1084 targets
= targets_artist
;
1085 n_elements
= G_N_ELEMENTS (targets_artist
);
1087 case RHYTHMDB_PROP_LOCATION
:
1088 targets
= targets_location
;
1089 n_elements
= G_N_ELEMENTS (targets_location
);
1092 g_assert_not_reached ();
1095 rb_tree_dnd_add_drag_source_support (view
,
1097 targets
, n_elements
,
1102 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel
*model
)
1107 iter
.stamp
= model
->priv
->stamp
;
1108 iter
.user_data
= model
->priv
->all
;
1109 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
1110 gtk_tree_model_row_changed (GTK_TREE_MODEL (model
), path
, &iter
);
1111 gtk_tree_path_free (path
);
1113 model
->priv
->syncing_id
= 0;
1118 rhythmdb_property_model_sync (RhythmDBPropertyModel
*model
)
1120 if (model
->priv
->syncing_id
!= 0)
1123 model
->priv
->syncing_id
= g_idle_add ((GSourceFunc
)rhythmdb_property_model_perform_sync
, model
);
1126 /* This should really be standard. */
1127 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1130 rhythmdb_property_model_column_get_type (void)
1132 static GType etype
= 0;
1135 static const GEnumValue values
[] = {
1136 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, "Property title"),
1137 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
, "Value priority"),
1138 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
, "Track count"),
1142 etype
= g_enum_register_static ("RhythmDBPropertyModelColumn", values
);