Updated Finnish translation
[rhythmbox.git] / rhythmdb / rhythmdb-property-model.c
blob05c1e2b2eb39f3f50a37c92e8ff07863442e89a3
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.
23 #include "config.h"
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib/gi18n.h>
30 #include "rhythmdb-property-model.h"
31 #include "rb-debug.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))
45 typedef struct {
46 RBRefString *string;
47 RBRefString *sort_string;
48 guint refcount;
49 } 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 },
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
143 RhythmDB *db;
145 RhythmDBQueryModel *query_model;
146 GHashTable *entries;
148 RhythmDBPropType propid;
149 RhythmDBPropType sort_propid;
151 guint stamp;
153 GSequence *properties;
154 GHashTable *reverse_map;
156 RhythmDBPropertyModelEntry *all;
158 guint syncing_id;
161 #define RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE_PROPERTY_MODEL, RhythmDBPropertyModelPrivate))
163 enum
165 PRE_ROW_DELETION,
166 LAST_SIGNAL
169 enum
171 PROP_0,
172 PROP_RHYTHMDB,
173 PROP_PROP,
174 PROP_QUERY_MODEL,
177 static guint rhythmdb_property_model_signals[LAST_SIGNAL] = { 0 };
179 static void
180 rhythmdb_property_model_class_init (RhythmDBPropertyModelClass *klass)
182 GObjectClass *object_class = G_OBJECT_CLASS (klass);
184 if (!rhythmdb_property_model_artist_drag_target_list)
185 rhythmdb_property_model_artist_drag_target_list =
186 gtk_target_list_new (targets_artist,
187 G_N_ELEMENTS (targets_artist));
188 if (!rhythmdb_property_model_album_drag_target_list)
189 rhythmdb_property_model_album_drag_target_list =
190 gtk_target_list_new (targets_album,
191 G_N_ELEMENTS (targets_album));
192 if (!rhythmdb_property_model_genre_drag_target_list)
193 rhythmdb_property_model_genre_drag_target_list =
194 gtk_target_list_new (targets_genre,
195 G_N_ELEMENTS (targets_genre));
196 if (!rhythmdb_property_model_location_drag_target_list)
197 rhythmdb_property_model_location_drag_target_list =
198 gtk_target_list_new (targets_location,
199 G_N_ELEMENTS (targets_location));
201 object_class->set_property = rhythmdb_property_model_set_property;
202 object_class->get_property = rhythmdb_property_model_get_property;
204 object_class->finalize = rhythmdb_property_model_finalize;
206 rhythmdb_property_model_signals[PRE_ROW_DELETION] =
207 g_signal_new ("pre-row-deletion",
208 G_OBJECT_CLASS_TYPE (object_class),
209 G_SIGNAL_RUN_LAST,
210 G_STRUCT_OFFSET (RhythmDBPropertyModelClass, pre_row_deletion),
211 NULL, NULL,
212 g_cclosure_marshal_VOID__VOID,
213 G_TYPE_NONE,
216 g_object_class_install_property (object_class,
217 PROP_RHYTHMDB,
218 g_param_spec_object ("db",
219 "RhythmDB",
220 "RhythmDB object",
221 RHYTHMDB_TYPE,
222 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
224 g_object_class_install_property (object_class,
225 PROP_PROP,
226 g_param_spec_int ("prop",
227 "propid",
228 "Property id",
229 0, RHYTHMDB_NUM_PROPERTIES,
230 RHYTHMDB_PROP_TYPE,
231 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
232 g_object_class_install_property (object_class,
233 PROP_QUERY_MODEL,
234 g_param_spec_object ("query-model",
235 "RhythmDBQueryModel",
236 "RhythmDBQueryModel object ",
237 RHYTHMDB_TYPE_QUERY_MODEL,
238 G_PARAM_READWRITE));
240 g_type_class_add_private (klass, sizeof (RhythmDBPropertyModelPrivate));
243 static void
244 rhythmdb_property_model_tree_model_init (GtkTreeModelIface *iface)
246 iface->get_flags = rhythmdb_property_model_get_flags;
247 iface->get_n_columns = rhythmdb_property_model_get_n_columns;
248 iface->get_column_type = rhythmdb_property_model_get_column_type;
249 iface->get_iter = rhythmdb_property_model_get_iter;
250 iface->get_path = rhythmdb_property_model_get_path;
251 iface->get_value = rhythmdb_property_model_get_value;
252 iface->iter_next = rhythmdb_property_model_iter_next;
253 iface->iter_children = rhythmdb_property_model_iter_children;
254 iface->iter_has_child = rhythmdb_property_model_iter_has_child;
255 iface->iter_n_children = rhythmdb_property_model_iter_n_children;
256 iface->iter_nth_child = rhythmdb_property_model_iter_nth_child;
257 iface->iter_parent = rhythmdb_property_model_iter_parent;
260 static void
261 rhythmdb_property_model_drag_source_init (RbTreeDragSourceIface *iface)
263 iface->row_draggable = rhythmdb_property_model_row_draggable;
264 iface->drag_data_delete = rhythmdb_property_model_drag_data_delete;
265 iface->drag_data_get = rhythmdb_property_model_drag_data_get;
268 static gboolean
269 _remove_entry_cb (GtkTreeModel *model,
270 GtkTreePath *path,
271 GtkTreeIter *iter,
272 RhythmDBPropertyModel *propmodel)
274 RhythmDBEntry *entry;
276 entry = rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model), iter);
277 rhythmdb_property_model_entry_removed_cb (RHYTHMDB_QUERY_MODEL (model),
278 entry,
279 propmodel);
280 return FALSE;
283 static gboolean
284 _add_entry_cb (GtkTreeModel *model,
285 GtkTreePath *path,
286 GtkTreeIter *iter,
287 RhythmDBPropertyModel *propmodel)
289 rhythmdb_property_model_row_inserted_cb (model, path, iter, propmodel);
290 return FALSE;
293 static void
294 rhythmdb_property_model_set_query_model_internal (RhythmDBPropertyModel *model,
295 RhythmDBQueryModel *query_model)
297 if (model->priv->query_model != NULL) {
298 g_signal_handlers_disconnect_by_func (model->priv->query_model,
299 G_CALLBACK (rhythmdb_property_model_row_inserted_cb),
300 model);
301 g_signal_handlers_disconnect_by_func (model->priv->query_model,
302 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
303 model);
304 g_signal_handlers_disconnect_by_func (model->priv->query_model,
305 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
306 model);
308 gtk_tree_model_foreach (GTK_TREE_MODEL (model->priv->query_model),
309 (GtkTreeModelForeachFunc)_remove_entry_cb,
310 model);
312 g_object_unref (model->priv->query_model);
315 model->priv->query_model = query_model;
316 g_assert (rhythmdb_property_model_iter_n_children (GTK_TREE_MODEL (model), NULL) == 1);
318 if (model->priv->query_model != NULL) {
319 g_object_ref (model->priv->query_model);
321 g_signal_connect_object (model->priv->query_model,
322 "row_inserted",
323 G_CALLBACK (rhythmdb_property_model_row_inserted_cb),
324 model,
326 g_signal_connect_object (model->priv->query_model,
327 "post-entry-delete",
328 G_CALLBACK (rhythmdb_property_model_entry_removed_cb),
329 model,
331 g_signal_connect_object (model->priv->query_model,
332 "entry-prop-changed",
333 G_CALLBACK (rhythmdb_property_model_prop_changed_cb),
334 model,
336 gtk_tree_model_foreach (GTK_TREE_MODEL (model->priv->query_model),
337 (GtkTreeModelForeachFunc)_add_entry_cb,
338 model);
342 static void
343 rhythmdb_property_model_set_property (GObject *object,
344 guint prop_id,
345 const GValue *value,
346 GParamSpec *pspec)
348 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
350 switch (prop_id) {
351 case PROP_RHYTHMDB:
352 model->priv->db = g_value_get_object (value);
353 break;
354 case PROP_PROP:
355 model->priv->propid = g_value_get_int (value);
356 switch (model->priv->propid) {
357 case RHYTHMDB_PROP_GENRE:
358 model->priv->sort_propid = RHYTHMDB_PROP_GENRE;
359 break;
360 case RHYTHMDB_PROP_ARTIST:
361 model->priv->sort_propid = RHYTHMDB_PROP_ARTIST;
362 break;
363 case RHYTHMDB_PROP_ALBUM:
364 model->priv->sort_propid = RHYTHMDB_PROP_ALBUM;
365 break;
366 case RHYTHMDB_PROP_SUBTITLE:
367 model->priv->sort_propid = RHYTHMDB_PROP_SUBTITLE;
368 break;
369 case RHYTHMDB_PROP_TITLE:
370 case RHYTHMDB_PROP_LOCATION:
371 model->priv->sort_propid = RHYTHMDB_PROP_TITLE;
372 break;
373 default:
374 g_assert_not_reached ();
375 break;
377 break;
378 case PROP_QUERY_MODEL:
379 rhythmdb_property_model_set_query_model_internal (model, g_value_get_object (value));
380 break;
381 default:
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383 break;
387 static void
388 rhythmdb_property_model_get_property (GObject *object,
389 guint prop_id,
390 GValue *value,
391 GParamSpec *pspec)
393 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (object);
395 switch (prop_id) {
396 case PROP_RHYTHMDB:
397 g_value_set_object (value, model->priv->db);
398 break;
399 case PROP_PROP:
400 g_value_set_int (value, model->priv->propid);
401 break;
402 case PROP_QUERY_MODEL:
403 g_value_set_object (value, model->priv->query_model);
404 break;
405 default:
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
407 break;
411 static void
412 rhythmdb_property_model_init (RhythmDBPropertyModel *model)
414 model->priv = RHYTHMDB_PROPERTY_MODEL_GET_PRIVATE (model);
416 model->priv->stamp = g_random_int ();
418 model->priv->properties = g_sequence_new (NULL);
419 model->priv->reverse_map = g_hash_table_new (g_str_hash, g_str_equal);
420 model->priv->entries = g_hash_table_new (g_direct_hash, g_direct_equal);
422 model->priv->all = g_new0 (RhythmDBPropertyModelEntry, 1);
423 model->priv->all->string = rb_refstring_new (_("All"));
426 static void
427 rhythmdb_property_model_finalize (GObject *object)
429 RhythmDBPropertyModel *model;
430 GSequencePtr ptr;
431 GSequencePtr end_ptr;
433 g_return_if_fail (object != NULL);
434 g_return_if_fail (RHYTHMDB_IS_PROPERTY_MODEL (object));
436 model = RHYTHMDB_PROPERTY_MODEL (object);
438 rb_debug ("finalizing property model %p", model);
440 g_return_if_fail (model->priv != NULL);
442 if (model->priv->syncing_id != 0)
443 g_source_remove (model->priv->syncing_id);
445 end_ptr = g_sequence_get_end_ptr (model->priv->properties);
446 for (ptr = g_sequence_get_begin_ptr (model->priv->properties); ptr != end_ptr;
447 ptr = g_sequence_ptr_next (ptr)) {
448 RhythmDBPropertyModelEntry *prop = g_sequence_ptr_get_data (ptr);
449 rb_refstring_unref (prop->string);
450 rb_refstring_unref (prop->sort_string);
453 g_hash_table_destroy (model->priv->reverse_map);
454 g_sequence_free (model->priv->properties);
456 g_hash_table_destroy (model->priv->entries);
458 if (model->priv->query_model != NULL) {
459 g_object_unref (model->priv->query_model);
462 G_OBJECT_CLASS (rhythmdb_property_model_parent_class)->finalize (object);
465 RhythmDBPropertyModel *
466 rhythmdb_property_model_new (RhythmDB *db,
467 RhythmDBPropType propid)
469 return g_object_new (RHYTHMDB_TYPE_PROPERTY_MODEL, "db", db, "prop", propid, NULL);
472 static void
473 rhythmdb_property_model_row_inserted_cb (GtkTreeModel *model,
474 GtkTreePath *path,
475 GtkTreeIter *iter,
476 RhythmDBPropertyModel *propmodel)
478 RhythmDBEntry *entry;
479 RhythmDBPropertyModelEntry *prop;
481 entry = rhythmdb_query_model_iter_to_entry (RHYTHMDB_QUERY_MODEL (model), iter);
483 prop = rhythmdb_property_model_insert (propmodel, entry);
484 rhythmdb_property_model_sync (propmodel);
486 rhythmdb_entry_unref (entry);
489 static void
490 rhythmdb_property_model_prop_changed_cb (RhythmDB *db,
491 RhythmDBEntry *entry,
492 RhythmDBPropType propid,
493 const GValue *old,
494 const GValue *new,
495 RhythmDBPropertyModel *propmodel)
497 if (propid == RHYTHMDB_PROP_HIDDEN) {
498 gboolean old_val = g_value_get_boolean (old);
499 gboolean new_val = g_value_get_boolean (new);
501 if (old_val != new_val) {
502 if (new_val == FALSE) {
503 g_assert (g_hash_table_remove (propmodel->priv->entries, entry));
504 rhythmdb_property_model_insert (propmodel, entry);
505 } else {
506 g_assert (g_hash_table_lookup (propmodel->priv->entries, entry) == NULL);
508 rhythmdb_property_model_delete (propmodel, entry);
509 g_hash_table_insert (propmodel->priv->entries, entry, GINT_TO_POINTER (1));
512 } else {
513 RhythmDBPropertyModelEntry *prop;
515 if (propid != propmodel->priv->propid)
516 return;
518 if (g_hash_table_lookup (propmodel->priv->entries, entry) != NULL)
519 return;
521 rhythmdb_property_model_delete_prop (propmodel, g_value_get_string (old));
522 prop = rhythmdb_property_model_insert (propmodel, entry);
525 rhythmdb_property_model_sync (propmodel);
528 static void
529 rhythmdb_property_model_entry_removed_cb (RhythmDBQueryModel *model,
530 RhythmDBEntry *entry,
531 RhythmDBPropertyModel *propmodel)
533 if (g_hash_table_remove (propmodel->priv->entries, entry))
534 return;
536 rhythmdb_property_model_delete (propmodel, entry);
537 rhythmdb_property_model_sync (propmodel);
540 static gint
541 rhythmdb_property_model_compare (RhythmDBPropertyModelEntry *a,
542 RhythmDBPropertyModelEntry *b,
543 RhythmDBPropertyModel *model)
545 const char *a_str, *b_str;
547 a_str = rb_refstring_get_sort_key (a->sort_string);
548 b_str = rb_refstring_get_sort_key (b->sort_string);
550 return strcmp (a_str, b_str);
553 static RhythmDBPropertyModelEntry *
554 rhythmdb_property_model_insert (RhythmDBPropertyModel *model,
555 RhythmDBEntry *entry)
557 RhythmDBPropertyModelEntry *prop;
558 GtkTreeIter iter;
559 GtkTreePath *path;
560 GSequencePtr ptr;
561 const char *propstr;
563 propstr = rhythmdb_entry_get_string (entry, model->priv->propid);
565 model->priv->all->refcount++;
567 if ((ptr = g_hash_table_lookup (model->priv->reverse_map, propstr))) {
568 prop = g_sequence_ptr_get_data (ptr);
569 prop->refcount++;
570 rb_debug ("adding \"%s\": refcount %d", propstr, prop->refcount);
571 return prop;
573 rb_debug ("adding new property \"%s\"", propstr);
575 prop = g_new0 (RhythmDBPropertyModelEntry, 1);
576 prop->string = rb_refstring_new (propstr);
577 prop->sort_string = rb_refstring_new (rhythmdb_entry_get_string (entry, model->priv->sort_propid));
578 prop->refcount = 1;
580 iter.stamp = model->priv->stamp;
581 ptr = g_sequence_insert_sorted (model->priv->properties, prop,
582 (GCompareDataFunc) rhythmdb_property_model_compare,
583 model);
584 g_hash_table_insert (model->priv->reverse_map,
585 (gpointer)rb_refstring_get (prop->string),
586 ptr);
588 iter.user_data = ptr;
589 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
590 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
591 gtk_tree_path_free (path);
593 return prop;
596 static void
597 rhythmdb_property_model_delete (RhythmDBPropertyModel *model,
598 RhythmDBEntry *entry)
600 const char *propstr;
602 if (g_hash_table_lookup (model->priv->entries, entry))
603 return;
605 propstr = rhythmdb_entry_get_string (entry, model->priv->propid);
606 rhythmdb_property_model_delete_prop (model, propstr);
609 static void
610 rhythmdb_property_model_delete_prop (RhythmDBPropertyModel *model,
611 const char *propstr)
613 GSequencePtr ptr;
614 RhythmDBPropertyModelEntry *prop;
615 GtkTreePath *path;
616 GtkTreeIter iter;
618 g_assert ((ptr = g_hash_table_lookup (model->priv->reverse_map, propstr)));
620 model->priv->all->refcount--;
622 prop = g_sequence_ptr_get_data (ptr);
623 rb_debug ("deleting \"%s\": refcount: %d", propstr, prop->refcount);
624 prop->refcount--;
625 if (prop->refcount > 0)
626 return;
628 iter.stamp = model->priv->stamp;
629 iter.user_data = ptr;
631 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
632 g_signal_emit (G_OBJECT (model), rhythmdb_property_model_signals[PRE_ROW_DELETION], 0);
633 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
634 gtk_tree_path_free (path);
635 g_sequence_remove (ptr);
636 g_hash_table_remove (model->priv->reverse_map, propstr);
637 rb_refstring_unref (prop->string);
638 rb_refstring_unref (prop->sort_string);
639 g_free (prop);
640 return;
643 gboolean
644 rhythmdb_property_model_iter_from_string (RhythmDBPropertyModel *model,
645 const char *name,
646 GtkTreeIter *iter)
648 GSequencePtr ptr;
650 if (name == NULL) {
651 if (iter) {
652 iter->stamp = model->priv->stamp;
653 iter->user_data = model->priv->all;
655 return TRUE;
658 ptr = g_hash_table_lookup (model->priv->reverse_map, name);
659 if (!ptr)
660 return FALSE;
662 if (iter) {
663 iter->stamp = model->priv->stamp;
664 iter->user_data = ptr;
667 return TRUE;
670 static GtkTreeModelFlags
671 rhythmdb_property_model_get_flags (GtkTreeModel *model)
673 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
676 static gint
677 rhythmdb_property_model_get_n_columns (GtkTreeModel *tree_model)
679 return RHYTHMDB_PROPERTY_MODEL_COLUMN_LAST;
682 static GType
683 rhythmdb_property_model_get_column_type (GtkTreeModel *tree_model,
684 int index)
686 switch (index) {
687 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
688 return G_TYPE_STRING;
689 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
690 return G_TYPE_BOOLEAN;
691 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
692 return G_TYPE_UINT;
693 default:
694 g_assert_not_reached ();
695 return G_TYPE_INVALID;
699 static gboolean
700 rhythmdb_property_model_get_iter (GtkTreeModel *tree_model,
701 GtkTreeIter *iter,
702 GtkTreePath *path)
704 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
705 guint index;
706 GSequencePtr ptr;
708 index = gtk_tree_path_get_indices (path)[0];
710 if (index == 0) {
711 iter->stamp = model->priv->stamp;
712 iter->user_data = model->priv->all;
713 return TRUE;
716 index--;
717 if (index >= g_sequence_get_length (model->priv->properties))
718 return FALSE;
720 ptr = g_sequence_get_ptr_at_pos (model->priv->properties, index);
722 iter->stamp = model->priv->stamp;
723 iter->user_data = ptr;
725 return TRUE;
728 static GtkTreePath *
729 rhythmdb_property_model_get_path (GtkTreeModel *tree_model,
730 GtkTreeIter *iter)
732 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
733 GtkTreePath *path;
735 g_return_val_if_fail (iter->stamp == model->priv->stamp, NULL);
737 if (iter->user_data == model->priv->all) {
738 return gtk_tree_path_new_first ();
741 if (g_sequence_ptr_is_end (iter->user_data))
742 return NULL;
744 path = gtk_tree_path_new ();
745 if (iter->user_data == model->priv->all)
746 gtk_tree_path_append_index (path, 0);
747 else
748 gtk_tree_path_append_index (path, g_sequence_ptr_get_position (iter->user_data) + 1);
749 return path;
752 static void
753 rhythmdb_property_model_get_value (GtkTreeModel *tree_model,
754 GtkTreeIter *iter,
755 gint column,
756 GValue *value)
758 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
760 g_return_if_fail (model->priv->stamp == iter->stamp);
762 if (iter->user_data == model->priv->all) {
763 switch (column) {
764 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
765 g_value_init (value, G_TYPE_STRING);
766 g_value_set_string (value, rb_refstring_get (model->priv->all->string));
767 break;
768 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
769 g_value_init (value, G_TYPE_BOOLEAN);
770 g_value_set_boolean (value, TRUE);
771 break;
772 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
773 g_value_init (value, G_TYPE_UINT);
774 g_value_set_uint (value, model->priv->all->refcount);
775 break;
776 default:
777 g_assert_not_reached ();
779 } else {
780 RhythmDBPropertyModelEntry *prop;
782 g_return_if_fail (!g_sequence_ptr_is_end (iter->user_data));
784 prop = g_sequence_ptr_get_data (iter->user_data);
785 switch (column) {
786 case RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE:
787 g_value_init (value, G_TYPE_STRING);
788 g_value_set_string (value, rb_refstring_get (prop->string));
789 break;
790 case RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY:
791 g_value_init (value, G_TYPE_BOOLEAN);
792 g_value_set_boolean (value, prop == model->priv->all);
793 break;
794 case RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER:
795 g_value_init (value, G_TYPE_UINT);
796 g_value_set_uint (value, prop->refcount);
797 break;
798 default:
799 g_assert_not_reached ();
804 static gboolean
805 rhythmdb_property_model_iter_next (GtkTreeModel *tree_model,
806 GtkTreeIter *iter)
808 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
810 g_return_val_if_fail (iter->stamp == model->priv->stamp, FALSE);
812 if (iter->user_data == model->priv->all) {
813 iter->user_data = g_sequence_get_begin_ptr (model->priv->properties);
814 } else {
815 g_return_val_if_fail (!g_sequence_ptr_is_end (iter->user_data), FALSE);
816 iter->user_data = g_sequence_ptr_next (iter->user_data);
819 return !g_sequence_ptr_is_end (iter->user_data);
822 static gboolean
823 rhythmdb_property_model_iter_children (GtkTreeModel *tree_model,
824 GtkTreeIter *iter,
825 GtkTreeIter *parent)
827 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
829 if (parent != NULL)
830 return FALSE;
832 iter->stamp = model->priv->stamp;
833 iter->user_data = model->priv->all;
835 return TRUE;
838 static gboolean
839 rhythmdb_property_model_iter_has_child (GtkTreeModel *tree_model,
840 GtkTreeIter *iter)
842 return FALSE;
845 static gint
846 rhythmdb_property_model_iter_n_children (GtkTreeModel *tree_model,
847 GtkTreeIter *iter)
849 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
851 if (iter)
852 g_return_val_if_fail (model->priv->stamp == iter->stamp, -1);
854 if (iter == NULL)
855 return 1 + g_sequence_get_length (model->priv->properties);
857 return 0;
860 static gboolean
861 rhythmdb_property_model_iter_nth_child (GtkTreeModel *tree_model,
862 GtkTreeIter *iter,
863 GtkTreeIter *parent,
864 gint n)
866 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (tree_model);
867 GSequencePtr child;
869 if (parent)
870 return FALSE;
872 if (n != 0) {
873 child = g_sequence_get_ptr_at_pos (model->priv->properties, n);
875 if (g_sequence_ptr_is_end (child))
876 return FALSE;
877 iter->user_data = child;
878 } else {
879 iter->user_data = model->priv->all;
882 iter->stamp = model->priv->stamp;
884 return TRUE;
887 static gboolean
888 rhythmdb_property_model_iter_parent (GtkTreeModel *tree_model,
889 GtkTreeIter *iter,
890 GtkTreeIter *child)
892 return FALSE;
895 static gboolean
896 rhythmdb_property_model_row_draggable (RbTreeDragSource *dragsource,
897 GList *paths)
899 return TRUE;
902 static gboolean
903 rhythmdb_property_model_drag_data_delete (RbTreeDragSource *dragsource,
904 GList *paths)
906 /* not supported */
907 return TRUE;
910 /*Going through hoops to avoid nested functions*/
911 struct QueryModelCbStruct {
912 RhythmDB *db;
913 GString *reply;
916 static gboolean
917 query_model_cb (GtkTreeModel *query_model,
918 GtkTreePath *path,
919 GtkTreeIter *iter,
920 struct QueryModelCbStruct *data)
922 const char *uri;
923 RhythmDBEntry *entry;
925 gtk_tree_model_get (query_model, iter, 0, &entry, -1);
926 uri = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION);
927 g_string_append (data->reply, uri);
928 g_string_append (data->reply, "\r\n");
930 rhythmdb_entry_unref (entry);
931 return FALSE;
934 static gboolean
935 rhythmdb_property_model_drag_data_get (RbTreeDragSource *dragsource,
936 GList *paths,
937 GtkSelectionData *selection_data)
939 RhythmDBPropertyModel *model = RHYTHMDB_PROPERTY_MODEL (dragsource);
940 guint target;
941 GtkTargetList *drag_target_list;
943 switch (model->priv->propid) {
944 case RHYTHMDB_PROP_GENRE:
945 drag_target_list = rhythmdb_property_model_genre_drag_target_list;
946 break;
947 case RHYTHMDB_PROP_ALBUM:
948 drag_target_list = rhythmdb_property_model_album_drag_target_list;
949 break;
950 case RHYTHMDB_PROP_ARTIST:
951 drag_target_list = rhythmdb_property_model_artist_drag_target_list;
952 break;
953 case RHYTHMDB_PROP_LOCATION:
954 drag_target_list = rhythmdb_property_model_location_drag_target_list;
955 break;
956 default:
957 g_assert_not_reached ();
960 if (!gtk_target_list_find (drag_target_list,
961 selection_data->target,
962 &target)) {
963 return FALSE;
966 if (target == TARGET_URIS) {
967 RhythmDB *db = model->priv->db;
968 RhythmDBQueryModel *query_model;
969 GString* reply = g_string_new ("");
970 GtkTreeIter iter;
971 gboolean is_all = FALSE;
972 struct QueryModelCbStruct tmp;
973 GtkTreePath *path;
974 GCompareDataFunc sort_func = NULL;
975 gpointer sort_data;
976 gboolean sort_reverse;
978 query_model = rhythmdb_query_model_new_empty (db);
979 /* FIXME the sort order on the query model at this point is usually
980 * not the user's selected sort order.
982 g_object_get (G_OBJECT (model->priv->query_model),
983 "sort-func", &sort_func,
984 "sort-data", &sort_data,
985 "sort-reverse", &sort_reverse,
986 NULL);
987 rhythmdb_query_model_set_sort_order (RHYTHMDB_QUERY_MODEL (query_model),
988 sort_func, GUINT_TO_POINTER (sort_data), NULL, sort_reverse);
990 rb_debug ("getting drag data as uri list");
991 /* check if first selected row is 'All' */
992 path = gtk_tree_row_reference_get_path (paths->data);
993 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
994 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
995 RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY,
996 &is_all, -1);
997 gtk_tree_path_free (path);
998 if (is_all) {
999 g_object_set (query_model,
1000 "base-model", model->priv->query_model,
1001 NULL);
1002 } else {
1003 GList *row;
1004 GPtrArray *subquery = g_ptr_array_new ();
1006 for (row = paths; row; row = row->next) {
1007 char* name;
1008 path = gtk_tree_row_reference_get_path (row->data);
1009 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path)) {
1010 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
1011 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE,
1012 &name, -1);
1013 if (row == paths) {
1014 rhythmdb_query_append (db, subquery,
1015 RHYTHMDB_QUERY_PROP_EQUALS,
1016 model->priv->propid, name,
1017 RHYTHMDB_QUERY_END);
1018 } else {
1019 rhythmdb_query_append (db, subquery,
1020 RHYTHMDB_QUERY_DISJUNCTION,
1021 RHYTHMDB_QUERY_PROP_EQUALS,
1022 model->priv->propid, name,
1023 RHYTHMDB_QUERY_END);
1027 gtk_tree_path_free (path);
1028 g_free (name);
1031 g_object_set (query_model,
1032 "query", subquery,
1033 "base-model", model->priv->query_model,
1034 NULL);
1035 rhythmdb_query_free (subquery);
1038 tmp.db = db;
1039 tmp.reply = reply;
1040 /* Too bad that we're on the main thread. Why doesn't gtk call us async?
1041 * How does file-roller manage? - it seems it refuses the drop when it isn't
1042 * done unpacking. In which case, we should tweak the drop acknowledgement,
1043 * and prepare the query using do_full_query_async. The query would be
1044 * hooked to the drag context.
1046 gtk_tree_model_foreach (GTK_TREE_MODEL (query_model),
1047 (GtkTreeModelForeachFunc) query_model_cb,
1048 &tmp);
1050 g_object_unref (query_model);
1052 gtk_selection_data_set (selection_data,
1053 selection_data->target,
1054 8, (guchar *)reply->str,
1055 reply->len);
1056 g_string_free (reply, TRUE);
1058 } else {
1059 char* title;
1060 GList *p;
1061 GString* reply = g_string_new ("");
1063 rb_debug ("getting drag data as list of property values");
1065 for (p = paths; p; p = p->next) {
1066 GtkTreeIter iter;
1067 GtkTreePath *path;
1069 path = gtk_tree_row_reference_get_path (p->data);
1070 if (path && gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path)) {
1071 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
1072 RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE, &title, -1);
1073 g_string_append (reply, title);
1074 if (p->next)
1075 g_string_append (reply, "\r\n");
1076 g_free (title);
1078 gtk_tree_path_free (path);
1080 gtk_selection_data_set (selection_data,
1081 selection_data->target,
1082 8, (guchar *)reply->str,
1083 reply->len);
1084 g_string_free (reply, TRUE);
1087 return TRUE;
1090 void
1091 rhythmdb_property_model_enable_drag (RhythmDBPropertyModel *model,
1092 GtkTreeView *view)
1094 const GtkTargetEntry *targets;
1095 gint n_elements;
1097 switch (model->priv->propid) {
1098 case RHYTHMDB_PROP_GENRE:
1099 targets = targets_genre;
1100 n_elements = G_N_ELEMENTS (targets_genre);
1101 break;
1102 case RHYTHMDB_PROP_ALBUM:
1103 targets = targets_album;
1104 n_elements = G_N_ELEMENTS (targets_album);
1105 break;
1106 case RHYTHMDB_PROP_ARTIST:
1107 targets = targets_artist;
1108 n_elements = G_N_ELEMENTS (targets_artist);
1109 break;
1110 case RHYTHMDB_PROP_LOCATION:
1111 targets = targets_location;
1112 n_elements = G_N_ELEMENTS (targets_location);
1113 break;
1114 default:
1115 g_assert_not_reached ();
1118 rb_tree_dnd_add_drag_source_support (view,
1119 GDK_BUTTON1_MASK,
1120 targets, n_elements,
1121 GDK_ACTION_COPY);
1124 static gboolean
1125 rhythmdb_property_model_perform_sync (RhythmDBPropertyModel *model)
1127 GtkTreeIter iter;
1128 GtkTreePath *path;
1130 GDK_THREADS_ENTER ();
1132 iter.stamp = model->priv->stamp;
1133 iter.user_data = model->priv->all;
1134 path = rhythmdb_property_model_get_path (GTK_TREE_MODEL (model), &iter);
1135 gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
1136 gtk_tree_path_free (path);
1138 model->priv->syncing_id = 0;
1139 GDK_THREADS_LEAVE ();
1140 return FALSE;
1143 static void
1144 rhythmdb_property_model_sync (RhythmDBPropertyModel *model)
1146 if (model->priv->syncing_id != 0)
1147 return;
1149 model->priv->syncing_id = g_idle_add ((GSourceFunc)rhythmdb_property_model_perform_sync, model);
1152 /* This should really be standard. */
1153 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1155 GType
1156 rhythmdb_property_model_column_get_type (void)
1158 static GType etype = 0;
1160 if (etype == 0) {
1161 static const GEnumValue values[] = {
1162 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_TITLE, "Property title"),
1163 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_PRIORITY, "Value priority"),
1164 ENUM_ENTRY (RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER, "Track count"),
1165 { 0, 0, 0 }
1168 etype = g_enum_register_static ("RhythmDBPropertyModelColumn", values);
1171 return etype;