~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to sources/rb-play-queue-source.c

Tags: upstream-0.9.2cvs20060102
ImportĀ upstreamĀ versionĀ 0.9.2cvs20060102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 *  Copyright (C) 2005 Jonathan Matthew <jonathan@kaolin.hn.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <gtk/gtk.h>
 
23
#include <libgnome/gnome-i18n.h>
 
24
#include <libgnomevfs/gnome-vfs-uri.h>
 
25
#include <libxml/tree.h>
 
26
 
 
27
#include "rb-play-queue-source.h"
 
28
#include "rb-song-info.h"
 
29
#include "rb-util.h"
 
30
#include "rb-debug.h"
 
31
 
 
32
static GObject *rb_play_queue_source_constructor (GType type, guint n_construct_properties,
 
33
                                                  GObjectConstructParam *construct_properties);
 
34
static void rb_play_queue_source_get_property (GObject *object,
 
35
                                               guint prop_id,
 
36
                                               GValue *value,
 
37
                                               GParamSpec *pspec);
 
38
static void rb_play_queue_source_track_info_cell_data_func (GtkTreeViewColumn *column,
 
39
                                                            GtkCellRenderer *renderer,
 
40
                                                            GtkTreeModel *tree_model,
 
41
                                                            GtkTreeIter *iter,
 
42
                                                            RBPlaylistSource *source);
 
43
static void rb_play_queue_sync_playing_state (GObject *entry_view,
 
44
                                              GParamSpec *pspec,
 
45
                                              RBPlayQueueSource *source);
 
46
static void rb_play_queue_source_row_inserted_cb (GtkTreeModel *model,
 
47
                                                  GtkTreePath *path,
 
48
                                                  GtkTreeIter *iter,
 
49
                                                  RBPlayQueueSource *source);
 
50
static void rb_play_queue_source_row_deleted_cb (GtkTreeModel *model,
 
51
                                                 GtkTreePath *path,
 
52
                                                 RBPlayQueueSource *source);
 
53
static void rb_play_queue_source_update_count (RBPlayQueueSource *source,
 
54
                                               GtkTreeModel *model,
 
55
                                               gint offset);
 
56
static void impl_show_entry_view_popup (RBPlaylistSource *source,
 
57
                                        RBEntryView *view);
 
58
 
 
59
#define PLAY_QUEUE_SOURCE_SONGS_POPUP_PATH "/QueuePlaylistViewPopup"
 
60
#define PLAY_QUEUE_SOURCE_SIDEBAR_POPUP_PATH "/QueueSidebarViewPopup"
 
61
 
 
62
typedef struct _RBPlayQueueSourcePrivate RBPlayQueueSourcePrivate;
 
63
 
 
64
struct _RBPlayQueueSourcePrivate
 
65
{
 
66
        RBEntryView *sidebar;
 
67
};
 
68
 
 
69
enum
 
70
{
 
71
        PROP_0,
 
72
        PROP_SIDEBAR
 
73
};
 
74
 
 
75
G_DEFINE_TYPE (RBPlayQueueSource, rb_play_queue_source, RB_TYPE_STATIC_PLAYLIST_SOURCE)
 
76
#define RB_PLAY_QUEUE_SOURCE_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), RB_TYPE_PLAY_QUEUE_SOURCE, RBPlayQueueSourcePrivate))
 
77
 
 
78
static void
 
79
rb_play_queue_source_class_init (RBPlayQueueSourceClass *klass)
 
80
{
 
81
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
82
        RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
 
83
        RBPlaylistSourceClass *playlist_class = RB_PLAYLIST_SOURCE_CLASS (klass);
 
84
 
 
85
        object_class->constructor = rb_play_queue_source_constructor;
 
86
        object_class->get_property = rb_play_queue_source_get_property;
 
87
 
 
88
        source_class->impl_can_add_to_queue = (RBSourceFeatureFunc) rb_false_function;
 
89
        source_class->impl_can_rename = (RBSourceFeatureFunc) rb_false_function;
 
90
 
 
91
        playlist_class->impl_show_entry_view_popup = impl_show_entry_view_popup;
 
92
 
 
93
        g_object_class_install_property (object_class,
 
94
                                         PROP_SIDEBAR,
 
95
                                         g_param_spec_object ("sidebar",
 
96
                                                              "sidebar",
 
97
                                                              "queue sidebar entry view",
 
98
                                                              RB_TYPE_ENTRY_VIEW,
 
99
                                                              G_PARAM_READABLE));
 
100
 
 
101
        g_type_class_add_private (klass, sizeof (RBPlayQueueSourcePrivate));
 
102
}
 
103
 
 
104
static void
 
105
rb_play_queue_source_init (RBPlayQueueSource *source)
 
106
{
 
107
}
 
108
 
 
109
static GObject *
 
110
rb_play_queue_source_constructor (GType type, guint n_construct_properties,
 
111
                                  GObjectConstructParam *construct_properties)
 
112
{
 
113
        GObjectClass *parent_class = G_OBJECT_CLASS (rb_play_queue_source_parent_class);
 
114
        RBPlayQueueSource *source = RB_PLAY_QUEUE_SOURCE (
 
115
                        parent_class->constructor (type, n_construct_properties, construct_properties));
 
116
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (source); 
 
117
        GObject *shell_player;
 
118
        RBShell *shell;
 
119
        RhythmDB *db = rb_playlist_source_get_db (RB_PLAYLIST_SOURCE (source));
 
120
        GtkTreeViewColumn *column;
 
121
        GtkCellRenderer *renderer;
 
122
        RhythmDBQueryModel *model;
 
123
 
 
124
        g_object_get (G_OBJECT (source), "shell", &shell, NULL);
 
125
        shell_player = rb_shell_get_player (shell);
 
126
        g_object_unref (G_OBJECT (shell));
 
127
                
 
128
        priv->sidebar = rb_entry_view_new (db, shell_player, NULL, TRUE, TRUE);
 
129
 
 
130
        g_object_set (G_OBJECT (priv->sidebar), "vscrollbar-policy", GTK_POLICY_AUTOMATIC, NULL);
 
131
 
 
132
        column = gtk_tree_view_column_new ();
 
133
        renderer = gtk_cell_renderer_text_new ();
 
134
        gtk_tree_view_column_pack_start (column, renderer, TRUE);
 
135
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
 
136
        gtk_tree_view_column_set_expand (column, TRUE);
 
137
        gtk_tree_view_column_set_clickable (column, TRUE);
 
138
        gtk_tree_view_column_set_cell_data_func (column, renderer,
 
139
                                                 (GtkTreeCellDataFunc)
 
140
                                                 rb_play_queue_source_track_info_cell_data_func,
 
141
                                                 source, NULL);
 
142
        rb_entry_view_append_column_custom (priv->sidebar, column,
 
143
                                            _("Queued songs"), "Title", NULL, NULL);
 
144
        rb_entry_view_set_columns_clickable (priv->sidebar, FALSE);
 
145
        rb_playlist_source_setup_entry_view (RB_PLAYLIST_SOURCE (source), priv->sidebar);
 
146
 
 
147
        model = rb_playlist_source_get_query_model (RB_PLAYLIST_SOURCE (source));
 
148
        rb_entry_view_set_model (priv->sidebar, model);
 
149
 
 
150
        /* sync the state of the main entry view and the sidebar */
 
151
        g_signal_connect_object (G_OBJECT (rb_source_get_entry_view (RB_SOURCE (source))),
 
152
                                 "notify::playing-state",
 
153
                                 G_CALLBACK (rb_play_queue_sync_playing_state),
 
154
                                 source, 0);
 
155
 
 
156
        /* update the queued song count when the query model changes */
 
157
        g_signal_connect_object (G_OBJECT (model), "row-inserted",
 
158
                                 G_CALLBACK (rb_play_queue_source_row_inserted_cb),
 
159
                                 source, 0);
 
160
        g_signal_connect_object (G_OBJECT (model), "row-deleted",
 
161
                                 G_CALLBACK (rb_play_queue_source_row_deleted_cb),
 
162
                                 source, 0);
 
163
 
 
164
        rb_play_queue_source_update_count (source, GTK_TREE_MODEL (model), 0);
 
165
        
 
166
        return G_OBJECT (source);
 
167
}
 
168
 
 
169
static void
 
170
rb_play_queue_source_get_property (GObject *object,
 
171
                                   guint prop_id,
 
172
                                   GValue *value,
 
173
                                   GParamSpec *pspec)
 
174
{
 
175
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (object);
 
176
 
 
177
        switch (prop_id)
 
178
        {
 
179
        case PROP_SIDEBAR:
 
180
                g_value_set_object (value, priv->sidebar);
 
181
                break;
 
182
        default:
 
183
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
184
                break;
 
185
        }
 
186
}
 
187
 
 
188
RBSource *
 
189
rb_play_queue_source_new (RBShell *shell)
 
190
{
 
191
        return RB_SOURCE (g_object_new (RB_TYPE_PLAY_QUEUE_SOURCE,
 
192
                                        "name", _("Queued songs"),
 
193
                                        "shell", shell,
 
194
                                        "is-local", TRUE,
 
195
                                        NULL));
 
196
}
 
197
 
 
198
void
 
199
rb_play_queue_source_sidebar_song_info (RBPlayQueueSource *source)
 
200
{
 
201
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (source);
 
202
        GtkWidget *song_info = NULL;
 
203
 
 
204
        g_return_if_fail (priv->sidebar != NULL);
 
205
 
 
206
        song_info = rb_song_info_new (RB_SOURCE (source), priv->sidebar);
 
207
        if (song_info)
 
208
                gtk_widget_show_all (song_info);
 
209
        else
 
210
                rb_debug ("failed to create dialog, or no selection!");
 
211
}
 
212
 
 
213
void
 
214
rb_play_queue_source_sidebar_delete (RBPlayQueueSource *source)
 
215
{
 
216
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (source);
 
217
        RBEntryView *sidebar = priv->sidebar;
 
218
        GList *sel, *tem;
 
219
 
 
220
        sel = rb_entry_view_get_selected_entries (sidebar);
 
221
        for (tem = sel; tem != NULL; tem = tem->next)
 
222
                rb_static_playlist_source_remove_entry (RB_STATIC_PLAYLIST_SOURCE (source),
 
223
                                                        (RhythmDBEntry *) tem->data);
 
224
        g_list_free (sel);
 
225
}
 
226
 
 
227
static void
 
228
impl_show_entry_view_popup (RBPlaylistSource *source, RBEntryView *view)
 
229
{
 
230
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (source);
 
231
        const char *popup = PLAY_QUEUE_SOURCE_SONGS_POPUP_PATH;
 
232
        if (view == priv->sidebar)
 
233
                popup = PLAY_QUEUE_SOURCE_SIDEBAR_POPUP_PATH;
 
234
        _rb_source_show_popup (RB_SOURCE (source), popup);
 
235
}
 
236
 
 
237
static void
 
238
rb_play_queue_source_track_info_cell_data_func (GtkTreeViewColumn *column,
 
239
                                                GtkCellRenderer *renderer,
 
240
                                                GtkTreeModel *tree_model,
 
241
                                                GtkTreeIter *iter,
 
242
                                                RBPlaylistSource *source)
 
243
{
 
244
        RhythmDBEntry *entry;
 
245
        gtk_tree_model_get (tree_model, iter, 0, &entry, -1);
 
246
        const char *title;
 
247
        const char *artist;
 
248
        const char *album;
 
249
        char *markup;
 
250
 
 
251
        title = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE);
 
252
        artist = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ARTIST);
 
253
        album = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ALBUM);
 
254
 
 
255
        /* Translators: format is "<title> from <album> by <artist>" */
 
256
        markup = g_markup_printf_escaped("%s\n<span size=\"smaller\">%s <u>%s</u>\n%s <u>%s</u></span>",
 
257
                                         title, _("from"), album, _("by"), artist);
 
258
 
 
259
        g_object_set (G_OBJECT (renderer), "markup", markup, NULL);
 
260
        g_free (markup);
 
261
}
 
262
 
 
263
static void 
 
264
rb_play_queue_sync_playing_state (GObject *entry_view,
 
265
                                  GParamSpec *pspec,
 
266
                                  RBPlayQueueSource *source)
 
267
{
 
268
        int state;
 
269
        RBPlayQueueSourcePrivate *priv = RB_PLAY_QUEUE_SOURCE_GET_PRIVATE (source);
 
270
        g_object_get (entry_view, "playing-state", &state, NULL);
 
271
        rb_entry_view_set_state (priv->sidebar, state);
 
272
}
 
273
 
 
274
static void 
 
275
rb_play_queue_source_row_inserted_cb (GtkTreeModel *model,
 
276
                                      GtkTreePath *path,
 
277
                                      GtkTreeIter *iter,
 
278
                                      RBPlayQueueSource *source)
 
279
{
 
280
        rb_play_queue_source_update_count (source, model, 0);
 
281
}
 
282
 
 
283
static void 
 
284
rb_play_queue_source_row_deleted_cb (GtkTreeModel *model,
 
285
                                     GtkTreePath *path,
 
286
                                     RBPlayQueueSource *source)
 
287
{
 
288
        rb_play_queue_source_update_count (source, model, -1);
 
289
}
 
290
 
 
291
static void 
 
292
rb_play_queue_source_update_count (RBPlayQueueSource *source,
 
293
                                   GtkTreeModel *model,
 
294
                                   gint offset)
 
295
{
 
296
        gint count = gtk_tree_model_iter_n_children (model, NULL) + offset;
 
297
        char *name = _("Queued songs");
 
298
 
 
299
        if (count > 0)
 
300
                name = g_strdup_printf ("%s (%d)", name, count);
 
301
 
 
302
        g_object_set (G_OBJECT (source), "name", name, NULL);
 
303
 
 
304
        if (count > 0)
 
305
                g_free (name);
 
306
}