2006-06-18 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / rhythmdb / rhythmdb-property-model.c
blobb964924d501bd8ac41c7c7cff430d4304664561f
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * arch-tag: Implementation of RhythmDB property GtkTreeModel
5 * Copyright (C) 2003 Colin Walters <walters@gnome.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <config.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
29 #include "rhythmdb-property-model.h"
30 #include "rb-debug.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))
44 typedef struct {
45 RBRefString *string;
46 RBRefString *sort_string;
47 guint refcount;
48 } RhythmDBPropertyModelEntry;
51 static void rhythmdb_property_model_finalize (GObject *object);
52 static void rhythmdb_property_model_set_property (GObject *object,
53 guint prop_id,
54 const GValue *value,
55 GParamSpec *pspec);
56 static void rhythmdb_property_model_get_property (GObject *object,
57 guint prop_id,
58 GValue *value,
59 GParamSpec *pspec);
60 static void rhythmdb_property_model_sync (RhythmDBPropertyModel *model);
61 static void rhythmdb_property_model_row_inserted_cb (GtkTreeModel *model,
62 GtkTreePath *path,
63 GtkTreeIter *iter,
64 RhythmDBPropertyModel *propmodel);
65 static void rhythmdb_property_model_prop_changed_cb (RhythmDB *db, RhythmDBEntry *entry,
66 RhythmDBPropType prop, const GValue *old,
67 const GValue *new,
68 RhythmDBPropertyModel *propmodel);
69 static void rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel *model,
70 RhythmDBEntry *entry,
71 RhythmDBPropertyModel *propmodel);
72 static RhythmDBPropertyModelEntry* rhythmdb_property_model_insert (RhythmDBPropertyModel *model,
73 RhythmDBEntry *entry);
74 static void rhythmdb_property_model_delete (RhythmDBPropertyModel *model,
75 RhythmDBEntry *entry);
76 static void rhythmdb_property_model_delete_prop (RhythmDBPropertyModel *model,
77 const char *propstr);
78 static GtkTreeModelFlags rhythmdb_property_model_get_flags (GtkTreeModel *model);
79 static gint rhythmdb_property_model_get_n_columns (GtkTreeModel *tree_model);
80 static GType rhythmdb_property_model_get_column_type (GtkTreeModel *tree_model, int index);
81 static gboolean rhythmdb_property_model_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter,
82 GtkTreePath *path);
83 static GtkTreePath * rhythmdb_property_model_get_path (GtkTreeModel *tree_model,
84 GtkTreeIter *iter);
85 static void rhythmdb_property_model_get_value (GtkTreeModel *tree_model, GtkTreeIter *iter,
86 gint column, GValue *value);
87 static gboolean rhythmdb_property_model_iter_next (GtkTreeModel *tree_model,
88 GtkTreeIter *iter);
89 static gboolean rhythmdb_property_model_iter_children (GtkTreeModel *tree_model,
90 GtkTreeIter *iter,
91 GtkTreeIter *parent);
92 static gboolean rhythmdb_property_model_iter_has_child (GtkTreeModel *tree_model,
93 GtkTreeIter *iter);
94 static gint rhythmdb_property_model_iter_n_children (GtkTreeModel *tree_model,
95 GtkTreeIter *iter);
96 static gboolean rhythmdb_property_model_iter_nth_child (GtkTreeModel *tree_model,
97 GtkTreeIter *iter, GtkTreeIter *parent,
98 gint n);
99 static gboolean rhythmdb_property_model_iter_parent (GtkTreeModel *tree_model,
100 GtkTreeIter *iter,
101 GtkTreeIter *child);
103 static gboolean rhythmdb_property_model_drag_data_get (RbTreeDragSource *dragsource,
104 GList *paths,
105 GtkSelectionData *selection_data);
106 static gboolean rhythmdb_property_model_drag_data_delete (RbTreeDragSource *dragsource,
107 GList *paths);
108 static gboolean rhythmdb_property_model_row_draggable (RbTreeDragSource *dragsource,
109 GList *paths);
111 enum {
112 TARGET_ALBUMS,
113 TARGET_GENRE,
114 TARGET_ARTISTS,
115 TARGET_LOCATION,
116 TARGET_URIS,
119 static const GtkTargetEntry targets_album [] = {
120 { "text/x-rhythmbox-album", 0, TARGET_ALBUMS },
121 { "text/uri-list", 0, TARGET_URIS },
123 static const GtkTargetEntry targets_genre [] = {
124 { "text/x-rhythmbox-genre", 0, TARGET_GENRE },
125 { "text/uri-list", 0, TARGET_URIS },
127 static const GtkTargetEntry targets_artist [] = {
128 { "text/x-rhythmbox-artist", 0, TARGET_ARTISTS },
129 { "text/uri-list", 0, TARGET_URIS },
131 static const GtkTargetEntry targets_location [] = {
132 { "text/x-rhythmbox-location", 0, TARGET_LOCATION },
133 { "text/uri-list", 0, TARGET_URIS },
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
144 RhythmDB *db;
146 RhythmDBQueryModel *query_model;
147 GHashTable *entries;
149 RhythmDBPropType propid;
150 RhythmDBPropType sort_propid;
152 guint stamp;
154 GPtrArray *query;
156 GSequence *properties;
157 GHashTable *reverse_map;
159 RhythmDBPropertyModelEntry *all;
161 gboolean complete;
162 guint syncing_id;
165 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
167 enum
169 PRE_ROW_DELETION,
170 LAST_SIGNAL
173 enum
175 PROP_0,
176 PROP_RHYTHMDB,
177 PROP_PROP,
178 PROP_QUERY_MODEL,
181 static guint rhythmdb_property_model_signals[LAST_SIGNAL] = { 0 };
183 static void
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),
213 G_SIGNAL_RUN_LAST,
214 G_STRUCT_OFFSET (RhythmDBPropertyModelClass, pre_row_deletion),
215 NULL, NULL,
216 g_cclosure_marshal_VOID__VOID,
217 G_TYPE_NONE,
220 g_object_class_install_property (object_class,
221 PROP_RHYTHMDB,
222 g_param_spec_object ("db",
223 "RhythmDB",
224 "RhythmDB object",
225 RHYTHMDB_TYPE,
226 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
228 g_object_class_install_property (object_class,
229 PROP_PROP,
230 g_param_spec_int ("prop",
231 "propid",
232 "Property id",
233 0, RHYTHMDB_NUM_PROPERTIES,
234 RHYTHMDB_PROP_TYPE,
235 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
236 g_object_class_install_property (object_class,
237 PROP_QUERY_MODEL,
238 g_param_spec_object ("query-model",
239 "RhythmDBQueryModel",
240 "RhythmDBQueryModel object ",
241 RHYTHMDB_TYPE_QUERY_MODEL,
242 G_PARAM_READWRITE));
244 g_type_class_add_private (klass, sizeof (RhythmDBPropertyModelPrivate));
247 static void
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;
264 static void
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;
272 static gboolean
273 _remove_entry_cb (RhythmDBEntry *entry,
274 gpointer unused,
275 RhythmDBPropertyModel *model)
277 rhythmdb_property_model_delete (model, entry);
278 return TRUE;
281 static gboolean
282 _add_entry_cb (GtkTreeModel *model,
283 GtkTreePath *path,
284 GtkTreeIter *iter,
285 RhythmDBPropertyModel *propmodel)
287 rhythmdb_property_model_row_inserted_cb (model, path, iter, propmodel);
288 return FALSE;
291 static void
292 rhythmdb_property_model_set_property (GObject *object,
293 guint prop_id,
294 const GValue *value,
295 GParamSpec *pspec)
297 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
299 switch (prop_id) {
300 case PROP_RHYTHMDB:
302 model->priv->db = g_value_get_object (value);
303 break;
305 case PROP_PROP:
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;
310 break;
311 case RHYTHMDB_PROP_ARTIST:
312 model->priv->sort_propid = RHYTHMDB_PROP_ARTIST;
313 break;
314 case RHYTHMDB_PROP_ALBUM:
315 model->priv->sort_propid = RHYTHMDB_PROP_ALBUM;
316 break;
317 case RHYTHMDB_PROP_SUBTITLE:
318 model->priv->sort_propid = RHYTHMDB_PROP_SUBTITLE;
319 break;
320 case RHYTHMDB_PROP_TITLE:
321 case RHYTHMDB_PROP_LOCATION:
322 model->priv->sort_propid = RHYTHMDB_PROP_TITLE;
323 break;
324 default:
325 g_assert_not_reached ();
326 break;
328 break;
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),
334 model);
335 g_signal_handlers_disconnect_by_func (G_OBJECT (model->priv->query_model),
336 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
337 model);
338 g_signal_handlers_disconnect_by_func (G_OBJECT (model->priv->query_model),
339 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
340 model);
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),
351 "row_inserted",
352 G_CALLBACK (rhythmdb_property_model_row_inserted_cb),
353 model,
355 g_signal_connect_object (G_OBJECT (model->priv->query_model),
356 "entry-removed",
357 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
358 model,
360 g_signal_connect_object (G_OBJECT (model->priv->query_model),
361 "entry-prop-changed",
362 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
363 model,
365 gtk_tree_model_foreach (GTK_TREE_MODEL (model->priv->query_model),
366 (GtkTreeModelForeachFunc)_add_entry_cb,
367 model);
369 break;
371 default:
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 break;
377 static void
378 rhythmdb_property_model_get_property (GObject *object,
379 guint prop_id,
380 GValue *value,
381 GParamSpec *pspec)
383 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
385 switch (prop_id) {
386 case PROP_RHYTHMDB:
387 g_value_set_object (value, model->priv->db);
388 break;
389 case PROP_PROP:
390 g_value_set_int (value, model->priv->propid);
391 break;
392 case PROP_QUERY_MODEL:
393 g_value_set_object (value, model->priv->query_model);
394 break;
395 default:
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 break;
401 static void
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"));
416 static void
417 rhythmdb_property_model_finalize (GObject *object)
419 RhythmDBPropertyModel *model;
420 GSequencePtr ptr;
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);
459 static void
460 rhythmdb_property_model_row_inserted_cb (GtkTreeModel *model,
461 GtkTreePath *path,
462 GtkTreeIter *iter,
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))
469 return;
472 prop = rhythmdb_property_model_insert (propmodel, entry);
473 g_hash_table_insert (propmodel->priv->entries, entry, prop);
475 rhythmdb_property_model_sync (propmodel);
478 static void
479 rhythmdb_property_model_prop_changed_cb (RhythmDB *db, RhythmDBEntry *entry,
480 RhythmDBPropType propid, const GValue *old,
481 const GValue *new,
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)
493 return;
495 prop = rhythmdb_property_model_insert (propmodel, entry);
496 g_hash_table_insert (propmodel->priv->entries, entry, prop);
497 } else {
498 if (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL)
499 return;
501 rhythmdb_property_model_delete (propmodel, entry);
502 g_hash_table_remove (propmodel->priv->entries, entry);
505 } else {
506 RhythmDBPropertyModelEntry *prop;
508 if (propid != propmodel->priv->propid)
509 return;
511 if (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL)
512 return;
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);
522 static void
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)
528 return;
530 rhythmdb_property_model_delete (propmodel, entry);
531 g_hash_table_remove (propmodel->priv->entries, entry);
532 rhythmdb_property_model_sync (propmodel);
535 static gint
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;
550 GtkTreeIter iter;
551 GtkTreePath *path;
552 GSequencePtr ptr;
553 const char *propstr;
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);
561 prop->refcount++;
562 rb_debug ("adding \"%s\": refcount %d", propstr, prop->refcount);
563 return prop;
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));
570 prop->refcount = 1;
572 iter.stamp = model->priv->stamp;
573 ptr = g_sequence_insert_sorted (model->priv->properties, prop,
574 (GCompareDataFunc) rhythmdb_property_model_compare,
575 model);
576 g_hash_table_insert (model->priv->reverse_map,
577 (gpointer)rb_refstring_get (prop->string),
578 ptr);
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);
585 return prop;
588 static void
589 rhythmdb_property_model_delete (RhythmDBPropertyModel *model,
590 RhythmDBEntry *entry)
592 RhythmDBPropertyModelEntry *prop;
593 const char *propstr;
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);
600 static void
601 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel *model,
602 const char *propstr)
604 GSequencePtr ptr;
605 RhythmDBPropertyModelEntry *prop;
606 GtkTreePath *path;
607 GtkTreeIter iter;
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);
615 prop->refcount--;
616 if (prop->refcount > 0)
617 return;
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);
630 g_free (prop);
631 return;
634 gboolean
635 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel *model,
636 const char *name, GtkTreeIter *iter)
638 GSequencePtr ptr;
640 if (name == NULL) {
641 if (iter) {
642 iter->stamp = model->priv->stamp;
643 iter->user_data = model->priv->all;
645 return TRUE;
648 ptr = g_hash_table_lookup (model->priv->reverse_map, name);
649 if (!ptr)
650 return FALSE;
652 if (iter) {
653 iter->stamp = model->priv->stamp;
654 iter->user_data = ptr;
657 return TRUE;
660 static GtkTreeModelFlags
661 rhythmdb_property_model_get_flags (GtkTreeModel *model)
663 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
666 static gint
667 rhythmdb_property_model_get_n_columns (GtkTreeModel *tree_model)
669 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST;
672 static GType
673 rhythmdb_property_model_get_column_type (GtkTreeModel *tree_model, int index)
675 switch (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:
681 return G_TYPE_UINT;
682 default:
683 g_assert_not_reached ();
684 return G_TYPE_INVALID;
688 static gboolean
689 rhythmdb_property_model_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter,
690 GtkTreePath *path)
692 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
693 guint index;
694 GSequencePtr ptr;
696 index = gtk_tree_path_get_indices (path)[0];
698 if (index == 0) {
699 iter->stamp = model->priv->stamp;
700 iter->user_data = model->priv->all;
701 return TRUE;
704 index--;
705 if (index >= g_sequence_get_length (model->priv->properties))
706 return FALSE;
708 ptr = g_sequence_get_ptr_at_pos (model->priv->properties, index);
710 iter->stamp = model->priv->stamp;
711 iter->user_data = ptr;
713 return TRUE;
716 static GtkTreePath *
717 rhythmdb_property_model_get_path (GtkTreeModel *tree_model,
718 GtkTreeIter *iter)
720 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
721 GtkTreePath *path;
723 g_return_val_if_fail (iter->stamp == model->priv->stamp, NULL);
725 if (g_sequence_ptr_is_end (iter->user_data))
726 return NULL;
728 path = gtk_tree_path_new ();
729 if (iter->user_data == model->priv->all)
730 gtk_tree_path_append_index (path, 0);
731 else
732 gtk_tree_path_append_index (path, g_sequence_ptr_get_position (iter->user_data)+1);
733 return path;
736 static void
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) {
746 switch (column) {
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));
750 break;
751 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
752 g_value_init (value, G_TYPE_BOOLEAN);
753 g_value_set_boolean (value, TRUE);
754 break;
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);
758 break;
759 default:
760 g_assert_not_reached ();
762 } else {
763 RhythmDBPropertyModelEntry *prop = g_sequence_ptr_get_data (iter->user_data);
765 switch (column) {
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));
769 break;
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);
773 break;
774 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
775 g_value_init (value, G_TYPE_UINT);
776 g_value_set_uint (value, prop->refcount);
777 break;
778 default:
779 g_assert_not_reached ();
784 static gboolean
785 rhythmdb_property_model_iter_next (GtkTreeModel *tree_model,
786 GtkTreeIter *iter)
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);
794 else
795 iter->user_data = g_sequence_ptr_next (iter->user_data);
797 return !g_sequence_ptr_is_end (iter->user_data);
800 static gboolean
801 rhythmdb_property_model_iter_children (GtkTreeModel *tree_model,
802 GtkTreeIter *iter,
803 GtkTreeIter *parent)
805 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
807 if (parent != NULL)
808 return FALSE;
810 iter->stamp = model->priv->stamp;
811 iter->user_data = model->priv->all;
813 return TRUE;
816 static gboolean
817 rhythmdb_property_model_iter_has_child (GtkTreeModel *tree_model,
818 GtkTreeIter *iter)
820 return FALSE;
823 static gint
824 rhythmdb_property_model_iter_n_children (GtkTreeModel *tree_model,
825 GtkTreeIter *iter)
827 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
829 if (iter)
830 g_return_val_if_fail (model->priv->stamp == iter->stamp, -1);
832 if (iter == NULL)
833 return 1 + g_sequence_get_length (model->priv->properties);
835 return 0;
838 static gboolean
839 rhythmdb_property_model_iter_nth_child (GtkTreeModel *tree_model,
840 GtkTreeIter *iter, GtkTreeIter *parent,
841 gint n)
843 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
844 GSequencePtr child;
846 if (parent)
847 return FALSE;
849 if (n != 0) {
850 child = g_sequence_get_ptr_at_pos (model->priv->properties, n);
852 if (g_sequence_ptr_is_end (child))
853 return FALSE;
854 iter->user_data = child;
855 } else {
856 iter->user_data = model->priv->all;
859 iter->stamp = model->priv->stamp;
861 return TRUE;
864 static gboolean
865 rhythmdb_property_model_iter_parent (GtkTreeModel *tree_model,
866 GtkTreeIter *iter,
867 GtkTreeIter *child)
869 return FALSE;
872 static gboolean
873 rhythmdb_property_model_row_draggable (RbTreeDragSource *dragsource,
874 GList *paths)
876 return TRUE;
879 static gboolean
880 rhythmdb_property_model_drag_data_delete (RbTreeDragSource *dragsource,
881 GList *paths)
883 /* not supported */
884 return TRUE;
887 /*Going through hoops to avoid nested functions*/
888 struct QueryModelCbStruct {
889 RhythmDB* db;
890 GString* reply;
893 static gboolean
894 query_model_cb (GtkTreeModel *query_model,
895 GtkTreePath *path, GtkTreeIter *iter,
896 struct QueryModelCbStruct *data)
898 const char* uri;
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);
907 return FALSE;
910 static gboolean
911 rhythmdb_property_model_drag_data_get (RbTreeDragSource *dragsource,
912 GList *paths,
913 GtkSelectionData *selection_data)
915 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (dragsource);
916 guint target;
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;
922 break;
923 case RHYTHMDB_PROP_ALBUM:
924 drag_target_list = rhythmdb_property_model_album_drag_target_list;
925 break;
926 case RHYTHMDB_PROP_ARTIST:
927 drag_target_list = rhythmdb_property_model_artist_drag_target_list;
928 break;
929 case RHYTHMDB_PROP_LOCATION:
930 drag_target_list = rhythmdb_property_model_location_drag_target_list;
931 break;
932 default:
933 g_assert_not_reached ();
936 if (!gtk_target_list_find (drag_target_list,
937 selection_data->target,
938 &target)) {
939 return FALSE;
942 if (target == TARGET_URIS) {
943 RhythmDB *db = model->priv->db;
944 RhythmDBQueryModel *query_model;
945 GString* reply = g_string_new ("");
946 GtkTreeIter iter;
947 gboolean is_all;
948 struct QueryModelCbStruct tmp;
949 GtkTreePath *path;
950 GCompareDataFunc sort_func = NULL;
951 gpointer sort_data;
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,
962 NULL);
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,
972 &is_all, -1);
973 gtk_tree_path_free (path);
974 if (is_all) {
975 g_object_set (G_OBJECT (query_model),
976 "base-model", model->priv->query_model,
977 NULL);
978 } else {
979 GList *row;
980 GPtrArray *query;
981 GPtrArray *subquery = g_ptr_array_new ();
983 for (row = paths; row; row = row->next) {
984 char* name;
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,
989 &name, -1);
990 if (row == paths) {
991 rhythmdb_query_append (db, subquery,
992 RHYTHMDB_QUERY_PROP_EQUALS,
993 model->priv->propid, name,
994 RHYTHMDB_QUERY_END);
995 } else {
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);
1004 g_free (name);
1006 query = rhythmdb_query_parse (db,
1007 RHYTHMDB_QUERY_SUBQUERY, subquery,
1008 RHYTHMDB_QUERY_END);
1010 g_object_set (G_OBJECT (query_model),
1011 "query", query,
1012 "base-model", model->priv->query_model,
1013 NULL);
1014 rhythmdb_query_free (query);
1017 tmp.db = db;
1018 tmp.reply = reply;
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,
1027 &tmp);
1029 g_object_unref (query_model);
1031 gtk_selection_data_set (selection_data,
1032 selection_data->target,
1033 8, (guchar *)reply->str,
1034 reply->len);
1035 g_string_free (reply, TRUE);
1037 } else {
1038 char* title;
1039 GList *p;
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) {
1045 GtkTreeIter iter;
1046 GtkTreePath *path;
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);
1053 if (p->next)
1054 g_string_append (reply, "\r\n");
1055 g_free (title);
1056 gtk_tree_path_free (path);
1058 gtk_selection_data_set (selection_data,
1059 selection_data->target,
1060 8, (guchar *)reply->str,
1061 reply->len);
1062 g_string_free (reply, TRUE);
1065 return TRUE;
1068 void
1069 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel *model, GtkTreeView *view)
1071 const GtkTargetEntry *targets;
1072 gint n_elements;
1074 switch (model->priv->propid) {
1075 case RHYTHMDB_PROP_GENRE:
1076 targets = targets_genre;
1077 n_elements = G_N_ELEMENTS (targets_genre);
1078 break;
1079 case RHYTHMDB_PROP_ALBUM:
1080 targets = targets_album;
1081 n_elements = G_N_ELEMENTS (targets_album);
1082 break;
1083 case RHYTHMDB_PROP_ARTIST:
1084 targets = targets_artist;
1085 n_elements = G_N_ELEMENTS (targets_artist);
1086 break;
1087 case RHYTHMDB_PROP_LOCATION:
1088 targets = targets_location;
1089 n_elements = G_N_ELEMENTS (targets_location);
1090 break;
1091 default:
1092 g_assert_not_reached ();
1095 rb_tree_dnd_add_drag_source_support (view,
1096 GDK_BUTTON1_MASK,
1097 targets, n_elements,
1098 GDK_ACTION_COPY);
1101 static gboolean
1102 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel *model)
1104 GtkTreeIter iter;
1105 GtkTreePath *path;
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;
1114 return FALSE;
1117 static void
1118 rhythmdb_property_model_sync (RhythmDBPropertyModel *model)
1120 if (model->priv->syncing_id != 0)
1121 return;
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 }
1129 GType
1130 rhythmdb_property_model_column_get_type (void)
1132 static GType etype = 0;
1134 if (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"),
1139 { 0, 0, 0 }
1142 etype = g_enum_register_static ("RhythmDBPropertyModelColumn", values);
1145 return etype;