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
;
155 GSequence
*properties
;
156 GHashTable
*reverse_map
;
158 RhythmDBPropertyModelEntry
*all
;
164 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
180 static guint rhythmdb_property_model_signals
[LAST_SIGNAL
] = { 0 };
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
),
213 G_STRUCT_OFFSET (RhythmDBPropertyModelClass
, pre_row_deletion
),
215 g_cclosure_marshal_VOID__VOID
,
219 g_object_class_install_property (object_class
,
221 g_param_spec_object ("db",
225 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
227 g_object_class_install_property (object_class
,
229 g_param_spec_int ("prop",
232 0, RHYTHMDB_NUM_PROPERTIES
,
234 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
235 g_object_class_install_property (object_class
,
237 g_param_spec_object ("query-model",
238 "RhythmDBQueryModel",
239 "RhythmDBQueryModel object ",
240 RHYTHMDB_TYPE_QUERY_MODEL
,
243 g_type_class_add_private (klass
, sizeof (RhythmDBPropertyModelPrivate
));
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
;
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
;
272 _remove_entry_cb (RhythmDBEntry
*entry
,
274 RhythmDBPropertyModel
*model
)
276 rhythmdb_property_model_delete (model
, entry
);
281 _add_entry_cb (GtkTreeModel
*model
,
284 RhythmDBPropertyModel
*propmodel
)
286 rhythmdb_property_model_row_inserted_cb (model
, path
, iter
, propmodel
);
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
),
298 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
299 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
301 g_signal_handlers_disconnect_by_func (model
->priv
->query_model
,
302 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
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
,
317 G_CALLBACK (rhythmdb_property_model_row_inserted_cb
),
320 g_signal_connect_object (model
->priv
->query_model
,
322 G_CALLBACK (rhythmdb_property_model_entry_removed_cb
),
325 g_signal_connect_object (model
->priv
->query_model
,
326 "entry-prop-changed",
327 G_CALLBACK (rhythmdb_property_model_prop_changed_cb
),
330 gtk_tree_model_foreach (GTK_TREE_MODEL (model
->priv
->query_model
),
331 (GtkTreeModelForeachFunc
)_add_entry_cb
,
337 rhythmdb_property_model_set_property (GObject
*object
,
342 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
346 model
->priv
->db
= g_value_get_object (value
);
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
;
354 case RHYTHMDB_PROP_ARTIST
:
355 model
->priv
->sort_propid
= RHYTHMDB_PROP_ARTIST
;
357 case RHYTHMDB_PROP_ALBUM
:
358 model
->priv
->sort_propid
= RHYTHMDB_PROP_ALBUM
;
360 case RHYTHMDB_PROP_SUBTITLE
:
361 model
->priv
->sort_propid
= RHYTHMDB_PROP_SUBTITLE
;
363 case RHYTHMDB_PROP_TITLE
:
364 case RHYTHMDB_PROP_LOCATION
:
365 model
->priv
->sort_propid
= RHYTHMDB_PROP_TITLE
;
368 g_assert_not_reached ();
372 case PROP_QUERY_MODEL
:
373 rhythmdb_property_model_set_query_model_internal (model
, g_value_get_object (value
));
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
382 rhythmdb_property_model_get_property (GObject
*object
,
387 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (object
);
391 g_value_set_object (value
, model
->priv
->db
);
394 g_value_set_int (value
, model
->priv
->propid
);
396 case PROP_QUERY_MODEL
:
397 g_value_set_object (value
, model
->priv
->query_model
);
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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"));
421 rhythmdb_property_model_finalize (GObject
*object
)
423 RhythmDBPropertyModel
*model
;
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
);
467 rhythmdb_property_model_row_inserted_cb (GtkTreeModel
*model
,
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
)) {
481 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
482 g_hash_table_insert (propmodel
->priv
->entries
, entry
, prop
);
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 RhythmDBPropertyModelEntry
*prop
;
505 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) != NULL
)
508 prop
= rhythmdb_property_model_insert (propmodel
, entry
);
509 g_hash_table_insert (propmodel
->priv
->entries
, entry
, prop
);
511 if (g_hash_table_lookup (propmodel
->priv
->entries
, entry
) == NULL
)
514 rhythmdb_property_model_delete (propmodel
, entry
);
515 g_hash_table_remove (propmodel
->priv
->entries
, entry
);
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 (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
);
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
)
543 rhythmdb_property_model_delete (propmodel
, entry
);
544 g_hash_table_remove (propmodel
->priv
->entries
, entry
);
545 rhythmdb_property_model_sync (propmodel
);
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
;
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
);
578 rb_debug ("adding \"%s\": refcount %d", propstr
, prop
->refcount
);
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
));
588 iter
.stamp
= model
->priv
->stamp
;
589 ptr
= g_sequence_insert_sorted (model
->priv
->properties
, prop
,
590 (GCompareDataFunc
) rhythmdb_property_model_compare
,
592 g_hash_table_insert (model
->priv
->reverse_map
,
593 (gpointer
)rb_refstring_get (prop
->string
),
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
);
605 rhythmdb_property_model_delete (RhythmDBPropertyModel
*model
,
606 RhythmDBEntry
*entry
)
608 RhythmDBPropertyModelEntry
*prop
;
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
);
617 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel
*model
,
621 RhythmDBPropertyModelEntry
*prop
;
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
);
632 if (prop
->refcount
> 0)
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
);
651 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel
*model
,
659 iter
->stamp
= model
->priv
->stamp
;
660 iter
->user_data
= model
->priv
->all
;
665 ptr
= g_hash_table_lookup (model
->priv
->reverse_map
, name
);
670 iter
->stamp
= model
->priv
->stamp
;
671 iter
->user_data
= ptr
;
677 static GtkTreeModelFlags
678 rhythmdb_property_model_get_flags (GtkTreeModel
*model
)
680 return GTK_TREE_MODEL_ITERS_PERSIST
| GTK_TREE_MODEL_LIST_ONLY
;
684 rhythmdb_property_model_get_n_columns (GtkTreeModel
*tree_model
)
686 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST
;
690 rhythmdb_property_model_get_column_type (GtkTreeModel
*tree_model
,
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
:
701 g_assert_not_reached ();
702 return G_TYPE_INVALID
;
707 rhythmdb_property_model_get_iter (GtkTreeModel
*tree_model
,
711 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
715 index
= gtk_tree_path_get_indices (path
)[0];
718 iter
->stamp
= model
->priv
->stamp
;
719 iter
->user_data
= model
->priv
->all
;
724 if (index
>= g_sequence_get_length (model
->priv
->properties
))
727 ptr
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, index
);
729 iter
->stamp
= model
->priv
->stamp
;
730 iter
->user_data
= ptr
;
736 rhythmdb_property_model_get_path (GtkTreeModel
*tree_model
,
739 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
742 g_return_val_if_fail (iter
->stamp
== model
->priv
->stamp
, NULL
);
744 if (g_sequence_ptr_is_end (iter
->user_data
))
747 path
= gtk_tree_path_new ();
748 if (iter
->user_data
== model
->priv
->all
)
749 gtk_tree_path_append_index (path
, 0);
751 gtk_tree_path_append_index (path
, g_sequence_ptr_get_position (iter
->user_data
)+1);
756 rhythmdb_property_model_get_value (GtkTreeModel
*tree_model
,
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
) {
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
));
772 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY
:
773 g_value_init (value
, G_TYPE_BOOLEAN
);
774 g_value_set_boolean (value
, TRUE
);
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
);
781 g_assert_not_reached ();
784 RhythmDBPropertyModelEntry
*prop
= g_sequence_ptr_get_data (iter
->user_data
);
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
));
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
);
795 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER
:
796 g_value_init (value
, G_TYPE_UINT
);
797 g_value_set_uint (value
, prop
->refcount
);
800 g_assert_not_reached ();
806 rhythmdb_property_model_iter_next (GtkTreeModel
*tree_model
,
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
);
816 iter
->user_data
= g_sequence_ptr_next (iter
->user_data
);
818 return !g_sequence_ptr_is_end (iter
->user_data
);
822 rhythmdb_property_model_iter_children (GtkTreeModel
*tree_model
,
826 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
831 iter
->stamp
= model
->priv
->stamp
;
832 iter
->user_data
= model
->priv
->all
;
838 rhythmdb_property_model_iter_has_child (GtkTreeModel
*tree_model
,
845 rhythmdb_property_model_iter_n_children (GtkTreeModel
*tree_model
,
848 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
851 g_return_val_if_fail (model
->priv
->stamp
== iter
->stamp
, -1);
854 return 1 + g_sequence_get_length (model
->priv
->properties
);
860 rhythmdb_property_model_iter_nth_child (GtkTreeModel
*tree_model
,
865 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (tree_model
);
872 child
= g_sequence_get_ptr_at_pos (model
->priv
->properties
, n
);
874 if (g_sequence_ptr_is_end (child
))
876 iter
->user_data
= child
;
878 iter
->user_data
= model
->priv
->all
;
881 iter
->stamp
= model
->priv
->stamp
;
887 rhythmdb_property_model_iter_parent (GtkTreeModel
*tree_model
,
895 rhythmdb_property_model_row_draggable (RbTreeDragSource
*dragsource
,
902 rhythmdb_property_model_drag_data_delete (RbTreeDragSource
*dragsource
,
909 /*Going through hoops to avoid nested functions*/
910 struct QueryModelCbStruct
{
916 query_model_cb (GtkTreeModel
*query_model
,
919 struct QueryModelCbStruct
*data
)
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
);
934 rhythmdb_property_model_drag_data_get (RbTreeDragSource
*dragsource
,
936 GtkSelectionData
*selection_data
)
938 RhythmDBPropertyModel
*model
= RHYTHMDB_PROPERTY_MODEL (dragsource
);
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
;
946 case RHYTHMDB_PROP_ALBUM
:
947 drag_target_list
= rhythmdb_property_model_album_drag_target_list
;
949 case RHYTHMDB_PROP_ARTIST
:
950 drag_target_list
= rhythmdb_property_model_artist_drag_target_list
;
952 case RHYTHMDB_PROP_LOCATION
:
953 drag_target_list
= rhythmdb_property_model_location_drag_target_list
;
956 g_assert_not_reached ();
959 if (!gtk_target_list_find (drag_target_list
,
960 selection_data
->target
,
965 if (target
== TARGET_URIS
) {
966 RhythmDB
*db
= model
->priv
->db
;
967 RhythmDBQueryModel
*query_model
;
968 GString
* reply
= g_string_new ("");
970 gboolean is_all
= FALSE
;
971 struct QueryModelCbStruct tmp
;
973 GCompareDataFunc sort_func
= NULL
;
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
,
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
,
996 gtk_tree_path_free (path
);
998 g_object_set (query_model
,
999 "base-model", model
->priv
->query_model
,
1003 GPtrArray
*subquery
= g_ptr_array_new ();
1005 for (row
= paths
; row
; row
= row
->next
) {
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
,
1013 rhythmdb_query_append (db
, subquery
,
1014 RHYTHMDB_QUERY_PROP_EQUALS
,
1015 model
->priv
->propid
, name
,
1016 RHYTHMDB_QUERY_END
);
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
);
1030 g_object_set (query_model
,
1032 "base-model", model
->priv
->query_model
,
1034 rhythmdb_query_free (subquery
);
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
,
1049 g_object_unref (query_model
);
1051 gtk_selection_data_set (selection_data
,
1052 selection_data
->target
,
1053 8, (guchar
*)reply
->str
,
1055 g_string_free (reply
, TRUE
);
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
) {
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
);
1074 g_string_append (reply
, "\r\n");
1077 gtk_tree_path_free (path
);
1079 gtk_selection_data_set (selection_data
,
1080 selection_data
->target
,
1081 8, (guchar
*)reply
->str
,
1083 g_string_free (reply
, TRUE
);
1090 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel
*model
,
1093 const GtkTargetEntry
*targets
;
1096 switch (model
->priv
->propid
) {
1097 case RHYTHMDB_PROP_GENRE
:
1098 targets
= targets_genre
;
1099 n_elements
= G_N_ELEMENTS (targets_genre
);
1101 case RHYTHMDB_PROP_ALBUM
:
1102 targets
= targets_album
;
1103 n_elements
= G_N_ELEMENTS (targets_album
);
1105 case RHYTHMDB_PROP_ARTIST
:
1106 targets
= targets_artist
;
1107 n_elements
= G_N_ELEMENTS (targets_artist
);
1109 case RHYTHMDB_PROP_LOCATION
:
1110 targets
= targets_location
;
1111 n_elements
= G_N_ELEMENTS (targets_location
);
1114 g_assert_not_reached ();
1117 rb_tree_dnd_add_drag_source_support (view
,
1119 targets
, n_elements
,
1124 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel
*model
)
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;
1140 rhythmdb_property_model_sync (RhythmDBPropertyModel
*model
)
1142 if (model
->priv
->syncing_id
!= 0)
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 }
1152 rhythmdb_property_model_column_get_type (void)
1154 static GType 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"),
1164 etype
= g_enum_register_static ("RhythmDBPropertyModelColumn", values
);