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
,
120 static const GtkTargetEntry targets_album
[] = {
121 { "text/x-rhythmbox-album", 0, TARGET_ALBUMS
},
122 { "application/x-rhythmbox-entry", 0, TARGET_ENTRIES
},
123 { "text/uri-list", 0, TARGET_URIS
},
125 static const GtkTargetEntry targets_genre
[] = {
126 { "text/x-rhythmbox-genre", 0, TARGET_GENRE
},
127 { "application/x-rhythmbox-entry", 0, TARGET_ENTRIES
},
128 { "text/uri-list", 0, TARGET_URIS
},
130 static const GtkTargetEntry targets_artist
[] = {
131 { "text/x-rhythmbox-artist", 0, TARGET_ARTISTS
},
132 { "application/x-rhythmbox-entry", 0, TARGET_ENTRIES
},
133 { "text/uri-list", 0, TARGET_URIS
},
135 static const GtkTargetEntry targets_location
[] = {
136 { "text/x-rhythmbox-location", 0, TARGET_LOCATION
},
137 { "application/x-rhythmbox-entry", 0, TARGET_ENTRIES
},
138 { "text/uri-list", 0, TARGET_URIS
},
141 static GtkTargetList
*rhythmdb_property_model_album_drag_target_list
= NULL
;
142 static GtkTargetList
*rhythmdb_property_model_artist_drag_target_list
= NULL
;
143 static GtkTargetList
*rhythmdb_property_model_genre_drag_target_list
= NULL
;
144 static GtkTargetList
*rhythmdb_property_model_location_drag_target_list
= NULL
;
146 struct RhythmDBPropertyModelPrivate
150 RhythmDBQueryModel
*query_model
;
153 RhythmDBPropType propid
;
154 RhythmDBPropType sort_propid
;
158 GSequence
*properties
;
159 GHashTable
*reverse_map
;
161 RhythmDBPropertyModelEntry
*all
;
166 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
182 static guint rhythmdb_property_model_signals
[LAST_SIGNAL
] = { 0 };
185 rhythmdb_property_model_class_init (RhythmDBPropertyModelClass
*klass
)
187 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
189 if (!rhythmdb_property_model_artist_drag_target_list
)
190 rhythmdb_property_model_artist_drag_target_list
=
191 gtk_target_list_new (targets_artist
,
192 G_N_ELEMENTS (targets_artist
));
193 if (!rhythmdb_property_model_album_drag_target_list
)
194 rhythmdb_property_model_album_drag_target_list
=
195 gtk_target_list_new (targets_album
,
196 G_N_ELEMENTS (targets_album
));
197 if (!rhythmdb_property_model_genre_drag_target_list
)
198 rhythmdb_property_model_genre_drag_target_list
=
199 gtk_target_list_new (targets_genre
,
200 G_N_ELEMENTS (targets_genre
));
201 if (!rhythmdb_property_model_location_drag_target_list
)
202 rhythmdb_property_model_location_drag_target_list
=
203 gtk_target_list_new (targets_location
,
204 G_N_ELEMENTS (targets_location
));
206 object_class
->set_property
= rhythmdb_property_model_set_property
;
207 object_class
->get_property
= rhythmdb_property_model_get_property
;
209 object_class
->finalize
= rhythmdb_property_model_finalize
;
211 rhythmdb_property_model_signals
[PRE_ROW_DELETION
] =
212 g_signal_new ("pre-row-deletion",
213 G_OBJECT_CLASS_TYPE (object_class
),
215 G_STRUCT_OFFSET (RhythmDBPropertyModelClass
, pre_row_deletion
),
217 g_cclosure_marshal_VOID__VOID
,
221 g_object_class_install_property (object_class
,
223 g_param_spec_object ("db",
227 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
229 g_object_class_install_property (object_class
,
231 g_param_spec_int ("prop",
234 0, RHYTHMDB_NUM_PROPERTIES
,
236 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
237 g_object_class_install_property (object_class
,
239 g_param_spec_object ("query-model",
240 "RhythmDBQueryModel",
241 "RhythmDBQueryModel object ",
242 RHYTHMDB_TYPE_QUERY_MODEL
,
245 g_type_class_add_private (klass
, sizeof (RhythmDBPropertyModelPrivate
));
249 rhythmdb_property_model_tree_model_init (GtkTreeModelIface
*iface
)
251 iface
->get_flags
= rhythmdb_property_model_get_flags
;
252 iface
->get_n_columns
= rhythmdb_property_model_get_n_columns
;
253 iface
->get_column_type
= rhythmdb_property_model_get_column_type
;
254 iface
->get_iter
= rhythmdb_property_model_get_iter
;
255 iface
->get_path
= rhythmdb_property_model_get_path
;
256 iface
->get_value
= rhythmdb_property_model_get_value
;
257 iface
->iter_next
= rhythmdb_property_model_iter_next
;
258 iface
->iter_children
= rhythmdb_property_model_iter_children
;
259 iface
->iter_has_child
= rhythmdb_property_model_iter_has_child
;
260 iface
->iter_n_children
= rhythmdb_property_model_iter_n_children
;
261 iface
->iter_nth_child
= rhythmdb_property_model_iter_nth_child
;
262 iface
->iter_parent
= rhythmdb_property_model_iter_parent
;
266 rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface
*iface
)
268 iface
->rb_row_draggable
= rhythmdb_property_model_row_draggable
;
269 iface
->rb_drag_data_delete
= rhythmdb_property_model_drag_data_delete
;
270 iface
->rb_drag_data_get
= rhythmdb_property_model_drag_data_get
;
274 _remove_entry_cb (GtkTreeModel
*model
,
277 RhythmDBPropertyModel
*propmodel
)
279 RhythmDBEntry
*entry
;
281 entry
= rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model
), iter
);
282 rhythmdb_property_model_entry_removed_cb (RHYTHMDB_QUERY_MODEL (model
),
289 _add_entry_cb (GtkTreeModel
*model
,
292 RhythmDBPropertyModel
*propmodel
)
294 rhythmdb_property_model_row_inserted_cb (model
, path
, iter
, propmodel
);
299 rhythmdb_property_model_set_query_model_internal (RhythmDBPropertyModel
*model
,
300 RhythmDBQueryModel
*query_model
)
302 if (model
->priv
->query_model
!= NULL
) {
303 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
304 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
306 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
307 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
309 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
310 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
313 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
314 (GtkTreeModelForeachFunc
)_remove_entry_cb
,
317 g_object_unref (model
->priv
->query_model
);
320 model
->priv
->query_model
= query_model
;
321 g_assert (rhythmdb_property_model_iter_n_children (GTK_TREE_MODEL (model
), NULL
) == 1);
323 if (model
->priv
->query_model
!= NULL
) {
324 g_object_ref (model
->priv
->query_model
);
326 g_signal_connect_object (model
->priv
->query_model
,
328 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
331 g_signal_connect_object (model
->priv
->query_model
,
333 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
336 g_signal_connect_object (model
->priv
->query_model
,
337 "entry-prop-changed",
338 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
341 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
342 (GtkTreeModelForeachFunc
)_add_entry_cb
,
348 rhythmdb_property_model_set_property (GObject
*object
,
353 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
357 model
->priv
->db
= g_value_get_object (value
);
360 model
->priv
->propid
= g_value_get_int (value
);
361 switch (model
->priv
->propid
) {
362 case RHYTHMDB_PROP_GENRE
:
363 model
->priv
->sort_propid
= RHYTHMDB_PROP_GENRE
;
365 case RHYTHMDB_PROP_ARTIST
:
366 model
->priv
->sort_propid
= RHYTHMDB_PROP_ARTIST
;
368 case RHYTHMDB_PROP_ALBUM
:
369 model
->priv
->sort_propid
= RHYTHMDB_PROP_ALBUM
;
371 case RHYTHMDB_PROP_SUBTITLE
:
372 model
->priv
->sort_propid
= RHYTHMDB_PROP_SUBTITLE
;
374 case RHYTHMDB_PROP_TITLE
:
375 case RHYTHMDB_PROP_LOCATION
:
376 model
->priv
->sort_propid
= RHYTHMDB_PROP_TITLE
;
379 g_assert_not_reached ();
383 case PROP_QUERY_MODEL
:
384 rhythmdb_property_model_set_query_model_internal (model
, g_value_get_object (value
));
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
393 rhythmdb_property_model_get_property (GObject
*object
,
398 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
402 g_value_set_object (value
, model
->priv
->db
);
405 g_value_set_int (value
, model
->priv
->propid
);
407 case PROP_QUERY_MODEL
:
408 g_value_set_object (value
, model
->priv
->query_model
);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
417 rhythmdb_property_model_init (RhythmDBPropertyModel
*model
)
419 model
->priv
= RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE (model
);
421 model
->priv
->stamp
= g_random_int ();
423 model
->priv
->properties
= g_sequence_new (NULL
);
424 model
->priv
->reverse_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
425 model
->priv
->entries
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
427 model
->priv
->all
= g_new0 (RhythmDBPropertyModelEntry
, 1);
428 model
->priv
->all
->string
= rb_refstring_new (_("All"));
432 rhythmdb_property_model_finalize (GObject
*object
)
434 RhythmDBPropertyModel
*model
;
436 GSequencePtr end_ptr
;
438 g_return_if_fail (object
!= NULL
);
439 g_return_if_fail (RHYTHMDB_IS_PROPERTY_MODEL (object
));
441 model
= RHYTHMDB_PROPERTY_MODEL (object
);
443 rb_debug ("finalizing property model %p", model
);
445 g_return_if_fail (model
->priv
!= NULL
);
447 if (model
->priv
->syncing_id
!= 0)
448 g_source_remove (model
->priv
->syncing_id
);
450 g_hash_table_destroy (model
->priv
->reverse_map
);
452 end_ptr
= g_sequence_get_end_ptr (model
->priv
->properties
);
453 for (ptr
= g_sequence_get_begin_ptr (model
->priv
->properties
); ptr
!= end_ptr
;
454 ptr
= g_sequence_ptr_next (ptr
)) {
455 RhythmDBPropertyModelEntry
*prop
= g_sequence_ptr_get_data (ptr
);
456 rb_refstring_unref (prop
->string
);
457 rb_refstring_unref (prop
->sort_string
);
460 g_sequence_free (model
->priv
->properties
);
462 g_hash_table_destroy (model
->priv
->entries
);
464 if (model
->priv
->query_model
!= NULL
) {
465 g_object_unref (model
->priv
->query_model
);
468 G_OBJECT_CLASS (rhythmdb_property_model_parent_class
)->finalize (object
);
471 RhythmDBPropertyModel
*
472 rhythmdb_property_model_new (RhythmDB
*db
,
473 RhythmDBPropType propid
)
475 return g_object_new (RHYTHMDB_TYPE_PROPERTY_MODEL
, "db", db
, "prop", propid
, NULL
);
479 rhythmdb_property_model_row_inserted_cb (GtkTreeModel
*model
,
482 RhythmDBPropertyModel
*propmodel
)
484 RhythmDBEntry
*entry
;
485 RhythmDBPropertyModelEntry
*prop
;
487 entry
= rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model
), iter
);
489 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
490 rhythmdb_property_model_sync (propmodel
);
492 rhythmdb_entry_unref (entry
);
496 rhythmdb_property_model_prop_changed_cb (RhythmDB
*db
,
497 RhythmDBEntry
*entry
,
498 RhythmDBPropType propid
,
501 RhythmDBPropertyModel
*propmodel
)
503 if (propid
== RHYTHMDB_PROP_HIDDEN
) {
504 gboolean old_val
= g_value_get_boolean (old
);
505 gboolean new_val
= g_value_get_boolean (new);
507 if (old_val
!= new_val
) {
508 if (new_val
== FALSE
) {
509 g_assert (g_hash_table_remove (propmodel
->priv
->entries
, entry
));
510 rhythmdb_property_model_insert (propmodel
, entry
);
512 g_assert (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
);
514 rhythmdb_property_model_delete (propmodel
, entry
);
515 g_hash_table_insert (propmodel
->priv
->entries
, entry
, GINT_TO_POINTER (1));
519 RhythmDBPropertyModelEntry
*prop
;
521 if (propid
!= propmodel
->priv
->propid
)
524 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) != NULL
)
527 rhythmdb_property_model_delete_prop (propmodel
, g_value_get_string (old
));
528 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
531 rhythmdb_property_model_sync (propmodel
);
535 rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel
*model
,
536 RhythmDBEntry
*entry
,
537 RhythmDBPropertyModel
*propmodel
)
539 if (g_hash_table_remove (propmodel
->priv
->entries
, entry
))
542 rhythmdb_property_model_delete (propmodel
, entry
);
543 rhythmdb_property_model_sync (propmodel
);
547 rhythmdb_property_model_compare (RhythmDBPropertyModelEntry
*a
,
548 RhythmDBPropertyModelEntry
*b
,
549 RhythmDBPropertyModel
*model
)
551 const char *a_str
, *b_str
;
553 a_str
= rb_refstring_get_sort_key (a
->sort_string
);
554 b_str
= rb_refstring_get_sort_key (b
->sort_string
);
556 return strcmp (a_str
, b_str
);
559 static RhythmDBPropertyModelEntry
*
560 rhythmdb_property_model_insert (RhythmDBPropertyModel
*model
,
561 RhythmDBEntry
*entry
)
563 RhythmDBPropertyModelEntry
*prop
;
569 propstr
= rhythmdb_entry_get_string (entry
, model
->priv
->propid
);
571 model
->priv
->all
->refcount
++;
573 if ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
))) {
574 prop
= g_sequence_ptr_get_data (ptr
);
576 rb_debug ("adding \"%s\": refcount %d", propstr
, prop
->refcount
);
579 rb_debug ("adding new property \"%s\"", propstr
);
581 prop
= g_new0 (RhythmDBPropertyModelEntry
, 1);
582 prop
->string
= rb_refstring_new (propstr
);
583 prop
->sort_string
= rb_refstring_new (rhythmdb_entry_get_string (entry
, model
->priv
->sort_propid
));
586 iter
.stamp
= model
->priv
->stamp
;
587 ptr
= g_sequence_insert_sorted (model
->priv
->properties
, prop
,
588 (GCompareDataFunc
) rhythmdb_property_model_compare
,
590 g_hash_table_insert (model
->priv
->reverse_map
,
591 (gpointer
)rb_refstring_get (prop
->string
),
594 iter
.user_data
= ptr
;
595 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
596 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model
), path
, &iter
);
597 gtk_tree_path_free (path
);
603 rhythmdb_property_model_delete (RhythmDBPropertyModel
*model
,
604 RhythmDBEntry
*entry
)
608 if (g_hash_table_lookup (model
->priv
->entries
, entry
))
611 propstr
= rhythmdb_entry_get_string (entry
, model
->priv
->propid
);
612 rhythmdb_property_model_delete_prop (model
, propstr
);
616 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel
*model
,
620 RhythmDBPropertyModelEntry
*prop
;
624 g_assert ((ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, propstr
)));
626 model
->priv
->all
->refcount
--;
628 prop
= g_sequence_ptr_get_data (ptr
);
629 rb_debug ("deleting \"%s\": refcount: %d", propstr
, prop
->refcount
);
631 if (prop
->refcount
> 0)
634 iter
.stamp
= model
->priv
->stamp
;
635 iter
.user_data
= ptr
;
637 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
638 g_signal_emit (G_OBJECT (model
), rhythmdb_property_model_signals
[PRE_ROW_DELETION
], 0);
639 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model
), path
);
640 gtk_tree_path_free (path
);
641 g_sequence_remove (ptr
);
642 g_hash_table_remove (model
->priv
->reverse_map
, propstr
);
643 rb_refstring_unref (prop
->string
);
644 rb_refstring_unref (prop
->sort_string
);
650 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel
*model
,
658 iter
->stamp
= model
->priv
->stamp
;
659 iter
->user_data
= model
->priv
->all
;
664 ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, name
);
669 iter
->stamp
= model
->priv
->stamp
;
670 iter
->user_data
= ptr
;
676 static GtkTreeModelFlags
677 rhythmdb_property_model_get_flags (GtkTreeModel
*model
)
679 return GTK_TREE_MODEL_ITERS_PERSIST
| GTK_TREE_MODEL_LIST_ONLY
;
683 rhythmdb_property_model_get_n_columns (GtkTreeModel
*tree_model
)
685 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST
;
689 rhythmdb_property_model_get_column_type (GtkTreeModel
*tree_model
,
693 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
694 return G_TYPE_STRING
;
695 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
696 return G_TYPE_BOOLEAN
;
697 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
700 g_assert_not_reached ();
701 return G_TYPE_INVALID
;
706 rhythmdb_property_model_get_iter (GtkTreeModel
*tree_model
,
710 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
714 index
= gtk_tree_path_get_indices (path
)[0];
717 iter
->stamp
= model
->priv
->stamp
;
718 iter
->user_data
= model
->priv
->all
;
723 if (index
>= g_sequence_get_length (model
->priv
->properties
))
726 ptr
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, index
);
728 iter
->stamp
= model
->priv
->stamp
;
729 iter
->user_data
= ptr
;
735 rhythmdb_property_model_get_path (GtkTreeModel
*tree_model
,
738 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
741 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, NULL
);
743 if (iter
->user_data
== model
->priv
->all
) {
744 return gtk_tree_path_new_first ();
747 if (g_sequence_ptr_is_end (iter
->user_data
))
750 path
= gtk_tree_path_new ();
751 if (iter
->user_data
== model
->priv
->all
)
752 gtk_tree_path_append_index (path
, 0);
754 gtk_tree_path_append_index (path
, g_sequence_ptr_get_position (iter
->user_data
) + 1);
759 rhythmdb_property_model_get_value (GtkTreeModel
*tree_model
,
764 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
766 g_return_if_fail (model
->priv
->stamp
== iter
->stamp
);
768 if (iter
->user_data
== model
->priv
->all
) {
770 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
771 g_value_init (value
, G_TYPE_STRING
);
772 g_value_set_string (value
, rb_refstring_get (model
->priv
->all
->string
));
774 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
775 g_value_init (value
, G_TYPE_BOOLEAN
);
776 g_value_set_boolean (value
, TRUE
);
778 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
779 g_value_init (value
, G_TYPE_UINT
);
780 g_value_set_uint (value
, model
->priv
->all
->refcount
);
783 g_assert_not_reached ();
786 RhythmDBPropertyModelEntry
*prop
;
788 g_return_if_fail (!g_sequence_ptr_is_end (iter
->user_data
));
790 prop
= g_sequence_ptr_get_data (iter
->user_data
);
792 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
:
793 g_value_init (value
, G_TYPE_STRING
);
794 g_value_set_string (value
, rb_refstring_get (prop
->string
));
796 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
797 g_value_init (value
, G_TYPE_BOOLEAN
);
798 g_value_set_boolean (value
, prop
== model
->priv
->all
);
800 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
801 g_value_init (value
, G_TYPE_UINT
);
802 g_value_set_uint (value
, prop
->refcount
);
805 g_assert_not_reached ();
811 rhythmdb_property_model_iter_next (GtkTreeModel
*tree_model
,
814 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
816 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, FALSE
);
818 if (iter
->user_data
== model
->priv
->all
) {
819 iter
->user_data
= g_sequence_get_begin_ptr (model
->priv
->properties
);
821 g_return_val_if_fail (!g_sequence_ptr_is_end (iter
->user_data
), FALSE
);
822 iter
->user_data
= g_sequence_ptr_next (iter
->user_data
);
825 return !g_sequence_ptr_is_end (iter
->user_data
);
829 rhythmdb_property_model_iter_children (GtkTreeModel
*tree_model
,
833 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
838 iter
->stamp
= model
->priv
->stamp
;
839 iter
->user_data
= model
->priv
->all
;
845 rhythmdb_property_model_iter_has_child (GtkTreeModel
*tree_model
,
852 rhythmdb_property_model_iter_n_children (GtkTreeModel
*tree_model
,
855 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
858 g_return_val_if_fail (model
->priv
->stamp
== iter
->stamp
, -1);
861 return 1 + g_sequence_get_length (model
->priv
->properties
);
867 rhythmdb_property_model_iter_nth_child (GtkTreeModel
*tree_model
,
872 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
879 child
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, n
);
881 if (g_sequence_ptr_is_end (child
))
883 iter
->user_data
= child
;
885 iter
->user_data
= model
->priv
->all
;
888 iter
->stamp
= model
->priv
->stamp
;
894 rhythmdb_property_model_iter_parent (GtkTreeModel
*tree_model
,
902 rhythmdb_property_model_row_draggable (RbTreeDragSource
*dragsource
,
909 rhythmdb_property_model_drag_data_delete (RbTreeDragSource
*dragsource
,
916 /*Going through hoops to avoid nested functions*/
917 struct QueryModelCbStruct
{
924 query_model_cb (GtkTreeModel
*query_model
,
927 struct QueryModelCbStruct
*data
)
929 RhythmDBEntry
*entry
;
931 gtk_tree_model_get (query_model
, iter
, 0, &entry
, -1);
932 if (data
->target
== TARGET_ENTRIES
) {
933 g_string_append_printf (data
->reply
,
935 rhythmdb_entry_get_ulong (entry
, RHYTHMDB_PROP_ENTRY_ID
));
936 } else if (data
->target
== TARGET_URIS
) {
938 uri
= rhythmdb_entry_get_string (entry
, RHYTHMDB_PROP_LOCATION
);
939 g_string_append (data
->reply
, uri
);
941 g_string_append (data
->reply
, "\r\n");
943 rhythmdb_entry_unref (entry
);
948 rhythmdb_property_model_drag_data_get (RbTreeDragSource
*dragsource
,
950 GtkSelectionData
*selection_data
)
952 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (dragsource
);
954 GtkTargetList
*drag_target_list
;
956 switch (model
->priv
->propid
) {
957 case RHYTHMDB_PROP_GENRE
:
958 drag_target_list
= rhythmdb_property_model_genre_drag_target_list
;
960 case RHYTHMDB_PROP_ALBUM
:
961 drag_target_list
= rhythmdb_property_model_album_drag_target_list
;
963 case RHYTHMDB_PROP_ARTIST
:
964 drag_target_list
= rhythmdb_property_model_artist_drag_target_list
;
966 case RHYTHMDB_PROP_LOCATION
:
967 drag_target_list
= rhythmdb_property_model_location_drag_target_list
;
970 g_assert_not_reached ();
973 if (!gtk_target_list_find (drag_target_list
,
974 selection_data
->target
,
979 if (target
== TARGET_URIS
|| target
== TARGET_ENTRIES
) {
980 RhythmDB
*db
= model
->priv
->db
;
981 RhythmDBQueryModel
*query_model
;
982 GString
* reply
= g_string_new ("");
984 gboolean is_all
= FALSE
;
985 struct QueryModelCbStruct tmp
;
987 GCompareDataFunc sort_func
= NULL
;
989 gboolean sort_reverse
;
991 query_model
= rhythmdb_query_model_new_empty (db
);
992 /* FIXME the sort order on the query model at this point is usually
993 * not the user's selected sort order.
995 g_object_get (G_OBJECT (model
->priv
->query_model
),
996 "sort-func", &sort_func
,
997 "sort-data", &sort_data
,
998 "sort-reverse", &sort_reverse
,
1000 rhythmdb_query_model_set_sort_order (RHYTHMDB_QUERY_MODEL (query_model
),
1001 sort_func
, GUINT_TO_POINTER (sort_data
), NULL
, sort_reverse
);
1003 rb_debug ("getting drag data as uri list");
1004 /* check if first selected row is 'All' */
1005 path
= gtk_tree_row_reference_get_path (paths
->data
);
1006 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
))
1007 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1008 RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
,
1010 gtk_tree_path_free (path
);
1012 g_object_set (query_model
,
1013 "base-model", model
->priv
->query_model
,
1017 GPtrArray
*subquery
= g_ptr_array_new ();
1019 for (row
= paths
; row
; row
= row
->next
) {
1021 path
= gtk_tree_row_reference_get_path (row
->data
);
1022 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
)) {
1023 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1024 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
,
1027 rhythmdb_query_append (db
, subquery
,
1028 RHYTHMDB_QUERY_PROP_EQUALS
,
1029 model
->priv
->propid
, name
,
1030 RHYTHMDB_QUERY_END
);
1032 rhythmdb_query_append (db
, subquery
,
1033 RHYTHMDB_QUERY_DISJUNCTION
,
1034 RHYTHMDB_QUERY_PROP_EQUALS
,
1035 model
->priv
->propid
, name
,
1036 RHYTHMDB_QUERY_END
);
1040 gtk_tree_path_free (path
);
1044 g_object_set (query_model
,
1046 "base-model", model
->priv
->query_model
,
1048 rhythmdb_query_free (subquery
);
1053 tmp
.target
= target
;
1054 /* Too bad that we're on the main thread. Why doesn't gtk call us async?
1055 * How does file-roller manage? - it seems it refuses the drop when it isn't
1056 * done unpacking. In which case, we should tweak the drop acknowledgement,
1057 * and prepare the query using do_full_query_async. The query would be
1058 * hooked to the drag context.
1060 gtk_tree_model_foreach (GTK_TREE_MODEL (query_model
),
1061 (GtkTreeModelForeachFunc
) query_model_cb
,
1064 g_object_unref (query_model
);
1066 gtk_selection_data_set (selection_data
,
1067 selection_data
->target
,
1068 8, (guchar
*)reply
->str
,
1070 g_string_free (reply
, TRUE
);
1075 GString
* reply
= g_string_new ("");
1077 rb_debug ("getting drag data as list of property values");
1079 for (p
= paths
; p
; p
= p
->next
) {
1083 path
= gtk_tree_row_reference_get_path (p
->data
);
1084 if (path
&& gtk_tree_model_get_iter (GTK_TREE_MODEL (model
), &iter
, path
)) {
1085 gtk_tree_model_get (GTK_TREE_MODEL (model
), &iter
,
1086 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, &title
, -1);
1087 g_string_append (reply
, title
);
1089 g_string_append (reply
, "\r\n");
1092 gtk_tree_path_free (path
);
1094 gtk_selection_data_set (selection_data
,
1095 selection_data
->target
,
1096 8, (guchar
*)reply
->str
,
1098 g_string_free (reply
, TRUE
);
1105 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel
*model
,
1108 const GtkTargetEntry
*targets
;
1111 switch (model
->priv
->propid
) {
1112 case RHYTHMDB_PROP_GENRE
:
1113 targets
= targets_genre
;
1114 n_elements
= G_N_ELEMENTS (targets_genre
);
1116 case RHYTHMDB_PROP_ALBUM
:
1117 targets
= targets_album
;
1118 n_elements
= G_N_ELEMENTS (targets_album
);
1120 case RHYTHMDB_PROP_ARTIST
:
1121 targets
= targets_artist
;
1122 n_elements
= G_N_ELEMENTS (targets_artist
);
1124 case RHYTHMDB_PROP_LOCATION
:
1125 targets
= targets_location
;
1126 n_elements
= G_N_ELEMENTS (targets_location
);
1129 g_assert_not_reached ();
1132 rb_tree_dnd_add_drag_source_support (view
,
1134 targets
, n_elements
,
1139 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel
*model
)
1144 GDK_THREADS_ENTER ();
1146 iter
.stamp
= model
->priv
->stamp
;
1147 iter
.user_data
= model
->priv
->all
;
1148 path
= rhythmdb_property_model_get_path (GTK_TREE_MODEL (model
), &iter
);
1149 gtk_tree_model_row_changed (GTK_TREE_MODEL (model
), path
, &iter
);
1150 gtk_tree_path_free (path
);
1152 model
->priv
->syncing_id
= 0;
1153 GDK_THREADS_LEAVE ();
1158 rhythmdb_property_model_sync (RhythmDBPropertyModel
*model
)
1160 if (model
->priv
->syncing_id
!= 0)
1163 model
->priv
->syncing_id
= g_idle_add ((GSourceFunc
)rhythmdb_property_model_perform_sync
, model
);
1166 /* This should really be standard. */
1167 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1170 rhythmdb_property_model_column_get_type (void)
1172 static GType etype
= 0;
1175 static const GEnumValue values
[] = {
1176 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE
, "Property title"),
1177 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
, "Value priority"),
1178 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
, "Track count"),
1182 etype
= g_enum_register_static ("RhythmDBPropertyModelColumn", values
);