2006-12-14 Francisco Javier F. Serrador <serrador@openshine.com>
[rhythmbox.git] / sources / rb-missing-files-source.c
blob3d2f9042b9465bad0b506e9c37ea143ff5734c94
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2006 Jonathan Matthew <jonathan@kaolin.wh9.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 * This source lists files rhythmbox cannot find, and maybe tries to stop
23 * you from trying to play them.
26 #include "config.h"
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
31 #include "rb-entry-view.h"
32 #include "rb-missing-files-source.h"
33 #include "rb-song-info.h"
34 #include "rb-util.h"
35 #include "rb-debug.h"
37 static void rb_missing_files_source_class_init (RBMissingFilesSourceClass *klass);
38 static void rb_missing_files_source_init (RBMissingFilesSource *source);
39 static GObject *rb_missing_files_source_constructor (GType type, guint n_construct_properties,
40 GObjectConstructParam *construct_properties);
41 static void rb_missing_files_source_dispose (GObject *object);
42 static void rb_missing_files_source_set_property (GObject *object,
43 guint prop_id,
44 const GValue *value,
45 GParamSpec *pspec);
46 static void rb_missing_files_source_get_property (GObject *object,
47 guint prop_id,
48 GValue *value,
49 GParamSpec *pspec);
51 static RBEntryView *impl_get_entry_view (RBSource *source);
52 static void impl_song_properties (RBSource *source);
53 static void impl_delete (RBSource *source);
54 static void impl_get_status (RBSource *source, char **text, char **progress_text, float *progress);
56 static void rb_missing_files_source_songs_show_popup_cb (RBEntryView *view,
57 gboolean over_entry,
58 RBMissingFilesSource *source);
59 static void rb_missing_files_source_songs_sort_order_changed_cb (RBEntryView *view,
60 RBMissingFilesSource *source);
62 #define MISSING_FILES_SOURCE_SONGS_POPUP_PATH "/MissingFilesViewPopup"
64 struct RBMissingFilesSourcePrivate
66 RhythmDB *db;
67 RBEntryView *view;
70 G_DEFINE_TYPE (RBMissingFilesSource, rb_missing_files_source, RB_TYPE_SOURCE);
72 static void
73 rb_missing_files_source_class_init (RBMissingFilesSourceClass *klass)
75 GObjectClass *object_class = G_OBJECT_CLASS (klass);
76 RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
78 object_class->dispose = rb_missing_files_source_dispose;
79 object_class->constructor = rb_missing_files_source_constructor;
81 object_class->set_property = rb_missing_files_source_set_property;
82 object_class->get_property = rb_missing_files_source_get_property;
84 source_class->impl_can_browse = (RBSourceFeatureFunc) rb_false_function;
85 source_class->impl_get_entry_view = impl_get_entry_view;
86 source_class->impl_can_rename = (RBSourceFeatureFunc) rb_false_function;
87 source_class->impl_can_search = (RBSourceFeatureFunc) rb_false_function;
89 source_class->impl_can_cut = (RBSourceFeatureFunc) rb_false_function;
90 source_class->impl_can_delete = (RBSourceFeatureFunc) rb_true_function;
91 source_class->impl_can_move_to_trash = (RBSourceFeatureFunc) rb_false_function;
92 source_class->impl_can_copy = (RBSourceFeatureFunc) rb_false_function;
93 source_class->impl_can_add_to_queue = (RBSourceFeatureFunc) rb_false_function;
95 source_class->impl_delete = impl_delete;
97 source_class->impl_song_properties = impl_song_properties;
98 source_class->impl_try_playlist = (RBSourceFeatureFunc) rb_false_function;
99 source_class->impl_can_pause = (RBSourceFeatureFunc) rb_false_function;
101 source_class->impl_get_status = impl_get_status;
103 g_type_class_add_private (klass, sizeof (RBMissingFilesSourcePrivate));
106 static void
107 rb_missing_files_source_init (RBMissingFilesSource *source)
109 gint size;
110 GdkPixbuf *pixbuf;
112 source->priv = G_TYPE_INSTANCE_GET_PRIVATE (source, RB_TYPE_MISSING_FILES_SOURCE, RBMissingFilesSourcePrivate);
114 gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &size, NULL);
115 pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
116 GTK_STOCK_MISSING_IMAGE,
117 size,
118 0, NULL);
119 rb_source_set_pixbuf (RB_SOURCE (source), pixbuf);
120 if (pixbuf != NULL) {
121 g_object_unref (pixbuf);
125 static GObject *
126 rb_missing_files_source_constructor (GType type, guint n_construct_properties,
127 GObjectConstructParam *construct_properties)
129 GObject *shell_player;
130 RBMissingFilesSource *source;
131 RBMissingFilesSourceClass *klass;
132 RBShell *shell;
133 GPtrArray *query;
134 RhythmDBQueryModel *model;
135 RhythmDBEntryType entry_type;
137 klass = RB_MISSING_FILES_SOURCE_CLASS (g_type_class_peek (RB_TYPE_MISSING_FILES_SOURCE));
139 source = RB_MISSING_FILES_SOURCE (G_OBJECT_CLASS (rb_missing_files_source_parent_class)->
140 constructor (type, n_construct_properties, construct_properties));
142 g_object_get (source,
143 "shell", &shell,
144 "entry-type", &entry_type,
145 NULL);
146 g_object_get (shell, "db", &source->priv->db, NULL);
147 shell_player = rb_shell_get_player (shell);
148 g_object_unref (shell);
150 /* construct real query */
151 query = rhythmdb_query_parse (source->priv->db,
152 RHYTHMDB_QUERY_PROP_EQUALS,
153 RHYTHMDB_PROP_TYPE,
154 entry_type,
155 RHYTHMDB_QUERY_PROP_EQUALS,
156 RHYTHMDB_PROP_HIDDEN,
157 TRUE,
158 RHYTHMDB_QUERY_END);
159 g_boxed_free (RHYTHMDB_TYPE_ENTRY_TYPE, entry_type);
161 model = rhythmdb_query_model_new (source->priv->db, query,
162 NULL, NULL, NULL, FALSE);
164 rhythmdb_query_free (query);
166 g_object_set (model, "show-hidden", TRUE, NULL);
168 /* set up entry view */
169 source->priv->view = rb_entry_view_new (source->priv->db, shell_player,
170 NULL, FALSE, FALSE);
172 rb_entry_view_set_model (source->priv->view, model);
174 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_TRACK_NUMBER, FALSE);
175 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_TITLE, TRUE);
176 /* rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_GENRE, FALSE); */
177 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_ARTIST, FALSE);
178 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_ALBUM, FALSE);
179 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_LOCATION, TRUE);
180 rb_entry_view_append_column (source->priv->view, RB_ENTRY_VIEW_COL_LAST_SEEN, TRUE);
182 rb_entry_view_set_columns_clickable (source->priv->view, TRUE);
184 gtk_container_add (GTK_CONTAINER (source), GTK_WIDGET (source->priv->view));
185 g_signal_connect_object (source->priv->view, "show_popup",
186 G_CALLBACK (rb_missing_files_source_songs_show_popup_cb), source, 0);
187 g_signal_connect_object (source->priv->view, "sort-order-changed",
188 G_CALLBACK (rb_missing_files_source_songs_sort_order_changed_cb), source, 0);
190 gtk_widget_show_all (GTK_WIDGET (source));
192 g_object_set (source, "query-model", model, NULL);
193 g_object_unref (model);
195 return G_OBJECT (source);
198 static void
199 rb_missing_files_source_dispose (GObject *object)
201 RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (object);
203 if (source->priv->db != NULL) {
204 g_object_unref (source->priv->db);
205 source->priv->db = NULL;
208 G_OBJECT_CLASS (rb_missing_files_source_parent_class)->dispose (object);
211 static RBEntryView *
212 impl_get_entry_view (RBSource *asource)
214 RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (asource);
215 return source->priv->view;
218 static void
219 rb_missing_files_source_set_property (GObject *object,
220 guint prop_id,
221 const GValue *value,
222 GParamSpec *pspec)
224 /*RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (object);*/
226 switch (prop_id)
228 default:
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 break;
234 static void
235 rb_missing_files_source_get_property (GObject *object,
236 guint prop_id,
237 GValue *value,
238 GParamSpec *pspec)
240 /*RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (object);*/
242 switch (prop_id)
244 default:
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 break;
250 RBSource *
251 rb_missing_files_source_new (RBShell *shell,
252 RBLibrarySource *library)
254 RBSource *source;
255 RhythmDBEntryType entry_type;
257 g_object_get (library, "entry-type", &entry_type, NULL);
258 source = RB_SOURCE (g_object_new (RB_TYPE_MISSING_FILES_SOURCE,
259 "name", _("Missing Files"),
260 "entry-type", entry_type,
261 "shell", shell,
262 "visibility", FALSE,
263 "hidden-when-empty", TRUE,
264 NULL));
265 g_boxed_free (RHYTHMDB_TYPE_ENTRY_TYPE, entry_type);
266 return source;
269 static void
270 rb_missing_files_source_songs_show_popup_cb (RBEntryView *view,
271 gboolean over_entry,
272 RBMissingFilesSource *source)
274 if (over_entry)
275 _rb_source_show_popup (RB_SOURCE (source), MISSING_FILES_SOURCE_SONGS_POPUP_PATH);
278 static void
279 impl_song_properties (RBSource *asource)
281 RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (asource);
282 GtkWidget *song_info = NULL;
284 g_return_if_fail (source->priv->view != NULL);
286 song_info = rb_song_info_new (asource, NULL);
287 if (song_info)
288 gtk_widget_show_all (song_info);
289 else
290 rb_debug ("failed to create dialog, or no selection!");
293 static void
294 impl_delete (RBSource *asource)
296 RBMissingFilesSource *source = RB_MISSING_FILES_SOURCE (asource);
297 GList *sel, *tem;
299 sel = rb_entry_view_get_selected_entries (source->priv->view);
300 for (tem = sel; tem != NULL; tem = tem->next) {
301 rhythmdb_entry_delete (source->priv->db, tem->data);
302 rhythmdb_commit (source->priv->db);
305 g_list_foreach (sel, (GFunc)rhythmdb_entry_unref, NULL);
306 g_list_free (sel);
309 static void
310 rb_missing_files_source_songs_sort_order_changed_cb (RBEntryView *view,
311 RBMissingFilesSource *source)
313 rb_entry_view_resort_model (view);
316 static void
317 impl_get_status (RBSource *asource, char **text, char **progress_text, float *progress)
319 RhythmDBQueryModel *model;
320 gint count;
322 g_object_get (asource, "query-model", &model, NULL);
323 count = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL);
324 g_object_unref (model);
326 *text = g_strdup_printf (ngettext ("%d missing file", "%d missing files", count),
327 count);