2313
|
1 /* Audacious - Cross-platform multimedia player
|
|
2 * Copyright (C) 2005-2007 Audacious development team.
|
|
3 *
|
|
4 * This program is free software; you can redistribute it and/or modify
|
|
5 * it under the terms of the GNU General Public License as published by
|
|
6 * the Free Software Foundation; under version 2 of the License.
|
|
7 *
|
|
8 * This program is distributed in the hope that it will be useful,
|
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 * GNU General Public License for more details.
|
|
12 *
|
|
13 * You should have received a copy of the GNU General Public License
|
|
14 * along with this program; if not, write to the Free Software
|
|
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
16 */
|
|
17
|
|
18 #include "playlist_manager.h"
|
|
19 #include "ui_playlist.h"
|
|
20 #include "playlist.h"
|
|
21 #include "ui_main.h"
|
|
22
|
|
23 #include <glib.h>
|
|
24 #include <glib/gi18n.h>
|
|
25 #include <gtk/gtk.h>
|
|
26 #include <gdk/gdkkeysyms.h>
|
|
27
|
|
28
|
|
29 #define DISABLE_MANAGER_UPDATE() g_object_set_data(G_OBJECT(listview),"opt1",GINT_TO_POINTER(1))
|
|
30 #define ENABLE_MANAGER_UPDATE() g_object_set_data(G_OBJECT(listview),"opt1",GINT_TO_POINTER(0))
|
|
31
|
|
32
|
|
33 static GtkWidget *playman_win = NULL;
|
|
34
|
|
35
|
|
36 /* in this enum, place the columns according to visualization order
|
|
37 (information not displayed in columns should be placed right before PLLIST_NUMCOLS) */
|
|
38 enum
|
|
39 {
|
|
40 PLLIST_COL_NAME = 0,
|
|
41 PLLIST_COL_ENTRIESNUM,
|
|
42 PLLIST_COL_PLPOINTER,
|
|
43 PLLIST_NUMCOLS
|
|
44 };
|
|
45
|
|
46
|
|
47 static void
|
|
48 playlist_manager_populate ( GtkListStore * store )
|
|
49 {
|
|
50 GList *playlists = NULL;
|
|
51 GtkTreeIter iter;
|
|
52
|
|
53 playlists = playlist_get_playlists();
|
|
54 while ( playlists != NULL )
|
|
55 {
|
|
56 GList *entries = NULL;
|
|
57 gint entriesnum = 0;
|
|
58 gchar *pl_name = NULL;
|
|
59 Playlist *playlist = (Playlist*)playlists->data;
|
|
60
|
|
61 PLAYLIST_LOCK(playlist->mutex);
|
|
62 /* for each playlist, pick name and number of entries */
|
|
63 pl_name = (gchar*)playlist_get_current_name( playlist );
|
|
64 for (entries = playlist->entries; entries; entries = g_list_next(entries))
|
|
65 entriesnum++;
|
|
66 PLAYLIST_UNLOCK(playlist->mutex);
|
|
67
|
|
68 gtk_list_store_append( store , &iter );
|
|
69 gtk_list_store_set( store, &iter,
|
|
70 PLLIST_COL_NAME , pl_name ,
|
|
71 PLLIST_COL_ENTRIESNUM , entriesnum ,
|
|
72 PLLIST_COL_PLPOINTER , playlist , -1 );
|
|
73 playlists = g_list_next(playlists);
|
|
74 }
|
|
75 return;
|
|
76 }
|
|
77
|
|
78
|
|
79 static void
|
|
80 playlist_manager_cb_new ( gpointer listview )
|
|
81 {
|
|
82 GList *playlists = NULL;
|
|
83 Playlist *newpl = NULL;
|
|
84 GtkTreeIter iter;
|
|
85 GtkListStore *store;
|
|
86 gchar *pl_name = NULL;
|
|
87
|
|
88 /* this ensures that playlist_manager_update() will
|
|
89 not perform update, since we're already doing it here */
|
|
90 DISABLE_MANAGER_UPDATE();
|
|
91
|
|
92 newpl = playlist_new();
|
|
93 pl_name = (gchar*)playlist_get_current_name( newpl );
|
|
94 playlists = playlist_get_playlists();
|
|
95 playlist_add_playlist( newpl );
|
|
96
|
|
97 store = (GtkListStore*)gtk_tree_view_get_model( GTK_TREE_VIEW(listview) );
|
|
98 gtk_list_store_append( store , &iter );
|
|
99 gtk_list_store_set( store, &iter,
|
|
100 PLLIST_COL_NAME , pl_name ,
|
|
101 PLLIST_COL_ENTRIESNUM , 0 ,
|
|
102 PLLIST_COL_PLPOINTER , newpl , -1 );
|
|
103
|
|
104 ENABLE_MANAGER_UPDATE();
|
|
105
|
|
106 return;
|
|
107 }
|
|
108
|
|
109
|
|
110 static void
|
|
111 playlist_manager_cb_del ( gpointer listview )
|
|
112 {
|
|
113 GtkTreeSelection *listsel = gtk_tree_view_get_selection( GTK_TREE_VIEW(listview) );
|
|
114 GtkTreeModel *store;
|
|
115 GtkTreeIter iter;
|
|
116
|
|
117 if ( gtk_tree_selection_get_selected( listsel , &store , &iter ) == TRUE )
|
|
118 {
|
|
119 Playlist *playlist = NULL;
|
|
120 gtk_tree_model_get( store, &iter, PLLIST_COL_PLPOINTER , &playlist , -1 );
|
|
121
|
|
122 if ( gtk_tree_model_iter_n_children( store , NULL ) < 2 )
|
|
123 {
|
|
124 /* let playlist_manager_update() handle the deletion of the last playlist */
|
|
125 playlist_remove_playlist( playlist );
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 gtk_list_store_remove( (GtkListStore*)store , &iter );
|
|
130 /* this ensures that playlist_manager_update() will
|
|
131 not perform update, since we're already doing it here */
|
|
132 DISABLE_MANAGER_UPDATE();
|
|
133 playlist_remove_playlist( playlist );
|
|
134 ENABLE_MANAGER_UPDATE();
|
|
135 }
|
|
136 }
|
|
137
|
|
138 return;
|
|
139 }
|
|
140
|
|
141
|
|
142 static void
|
|
143 playlist_manager_cb_lv_dclick ( GtkTreeView * lv , GtkTreePath * path ,
|
|
144 GtkTreeViewColumn * col , gpointer userdata )
|
|
145 {
|
|
146 GtkTreeModel *store;
|
|
147 GtkTreeIter iter;
|
|
148
|
|
149 store = gtk_tree_view_get_model( GTK_TREE_VIEW(lv) );
|
|
150 if ( gtk_tree_model_get_iter( store , &iter , path ) == TRUE )
|
|
151 {
|
|
152 Playlist *playlist = NULL;
|
|
153 gtk_tree_model_get( store , &iter , PLLIST_COL_PLPOINTER , &playlist , -1 );
|
|
154 playlist_select_playlist( playlist );
|
|
155 }
|
|
156
|
|
157 return;
|
|
158 }
|
|
159
|
|
160
|
|
161 static void
|
|
162 playlist_manager_cb_lv_pmenu_rename ( GtkMenuItem *menuitem , gpointer lv )
|
|
163 {
|
|
164 GtkTreeSelection *listsel = gtk_tree_view_get_selection( GTK_TREE_VIEW(lv) );
|
|
165 GtkTreeModel *store;
|
|
166 GtkTreeIter iter;
|
|
167
|
|
168 if ( gtk_tree_selection_get_selected( listsel , &store , &iter ) == TRUE )
|
|
169 {
|
|
170 GtkTreePath *path = gtk_tree_model_get_path( GTK_TREE_MODEL(store) , &iter );
|
|
171 GtkCellRenderer *rndrname = g_object_get_data( G_OBJECT(lv) , "rn" );
|
|
172 /* set the name renderer to editable and start editing */
|
|
173 g_object_set( G_OBJECT(rndrname) , "editable" , TRUE , NULL );
|
|
174 gtk_tree_view_set_cursor_on_cell( GTK_TREE_VIEW(lv) , path ,
|
|
175 gtk_tree_view_get_column( GTK_TREE_VIEW(lv) , PLLIST_COL_NAME ) , rndrname , TRUE );
|
|
176 gtk_tree_path_free( path );
|
|
177 }
|
|
178 }
|
|
179
|
|
180 static void
|
|
181 playlist_manager_cb_lv_name_edited ( GtkCellRendererText *cell , gchar *path_string ,
|
|
182 gchar *new_text , gpointer lv )
|
|
183 {
|
|
184 /* this is currently used to change playlist names */
|
|
185 GtkTreeModel *store = gtk_tree_view_get_model( GTK_TREE_VIEW(lv) );;
|
|
186 GtkTreeIter iter;
|
|
187
|
|
188 if ( gtk_tree_model_get_iter_from_string( store , &iter , path_string ) == TRUE )
|
|
189 {
|
|
190 Playlist *playlist = NULL;
|
|
191 gtk_tree_model_get( GTK_TREE_MODEL(store), &iter, PLLIST_COL_PLPOINTER , &playlist , -1 );
|
|
192 playlist_set_current_name( playlist , new_text );
|
|
193 gtk_list_store_set( GTK_LIST_STORE(store), &iter, PLLIST_COL_NAME , new_text , -1 );
|
|
194 }
|
|
195 /* set the renderer uneditable again */
|
|
196 g_object_set( G_OBJECT(cell) , "editable" , FALSE , NULL );
|
|
197 }
|
|
198
|
|
199
|
|
200 static gboolean
|
|
201 playlist_manager_cb_lv_btpress ( GtkWidget *lv , GdkEventButton *event )
|
|
202 {
|
|
203 if (( event->type == GDK_BUTTON_PRESS ) && ( event->button == 3 ))
|
|
204 {
|
|
205 GtkWidget *pmenu = (GtkWidget*)g_object_get_data( G_OBJECT(lv) , "menu" );
|
|
206 gtk_menu_popup( GTK_MENU(pmenu) , NULL , NULL , NULL , NULL ,
|
|
207 (event != NULL) ? event->button : 0,
|
|
208 gdk_event_get_time((GdkEvent*)event));
|
|
209 return TRUE;
|
|
210 }
|
|
211
|
|
212 return FALSE;
|
|
213 }
|
|
214
|
|
215
|
|
216 static gboolean
|
|
217 playlist_manager_cb_keypress ( GtkWidget *win , GdkEventKey *event )
|
|
218 {
|
|
219 switch (event->keyval)
|
|
220 {
|
|
221 case GDK_Escape:
|
|
222 gtk_widget_destroy( playman_win );
|
|
223 return TRUE;
|
|
224 default:
|
|
225 return FALSE;
|
|
226 }
|
|
227 }
|
|
228
|
|
229
|
|
230 void
|
|
231 playlist_manager_ui_show ( void )
|
|
232 {
|
|
233 GtkWidget *playman_vbox;
|
|
234 GtkWidget *playman_pl_lv, *playman_pl_lv_frame, *playman_pl_lv_sw;
|
|
235 GtkCellRenderer *playman_pl_lv_textrndr_name, *playman_pl_lv_textrndr_entriesnum;
|
|
236 GtkTreeViewColumn *playman_pl_lv_col_name, *playman_pl_lv_col_entriesnum;
|
|
237 GtkListStore *pl_store;
|
|
238 GtkWidget *playman_pl_lv_pmenu, *playman_pl_lv_pmenu_rename;
|
|
239 GtkWidget *playman_bbar_hbbox;
|
|
240 GtkWidget *playman_bbar_bt_new, *playman_bbar_bt_del, *playman_bbar_bt_close;
|
|
241 GdkGeometry playman_win_hints;
|
|
242
|
|
243 if ( playman_win != NULL )
|
|
244 {
|
|
245 gtk_window_present( GTK_WINDOW(playman_win) );
|
|
246 return;
|
|
247 }
|
|
248
|
|
249 playman_win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
|
|
250 gtk_window_set_type_hint( GTK_WINDOW(playman_win), GDK_WINDOW_TYPE_HINT_DIALOG );
|
|
251 gtk_window_set_transient_for( GTK_WINDOW(playman_win) , GTK_WINDOW(mainwin) );
|
|
252 gtk_window_set_position( GTK_WINDOW(playman_win), GTK_WIN_POS_CENTER );
|
|
253 gtk_window_set_title( GTK_WINDOW(playman_win), _("Playlist Manager") );
|
|
254 gtk_container_set_border_width( GTK_CONTAINER(playman_win), 10 );
|
|
255 g_signal_connect( G_OBJECT(playman_win) , "destroy" ,
|
|
256 G_CALLBACK(gtk_widget_destroyed) , &playman_win );
|
|
257 g_signal_connect( G_OBJECT(playman_win) , "key-press-event" ,
|
|
258 G_CALLBACK(playlist_manager_cb_keypress) , NULL );
|
|
259 playman_win_hints.min_width = 400;
|
|
260 playman_win_hints.min_height = 250;
|
|
261 gtk_window_set_geometry_hints( GTK_WINDOW(playman_win) , GTK_WIDGET(playman_win) ,
|
|
262 &playman_win_hints , GDK_HINT_MIN_SIZE );
|
|
263
|
|
264 playman_vbox = gtk_vbox_new( FALSE , 0 );
|
|
265 gtk_container_add( GTK_CONTAINER(playman_win) , playman_vbox );
|
|
266
|
|
267 /* current liststore model
|
|
268 ----------------------------------------------
|
|
269 G_TYPE_STRING -> playlist name
|
|
270 G_TYPE_UINT -> number of entries in playlist
|
|
271 G_TYPE_POINTER -> playlist pointer (Playlist*)
|
|
272 ----------------------------------------------
|
|
273 */
|
|
274 pl_store = gtk_list_store_new( PLLIST_NUMCOLS , G_TYPE_STRING , G_TYPE_UINT , G_TYPE_POINTER );
|
|
275 playlist_manager_populate( pl_store );
|
|
276
|
|
277 playman_pl_lv_frame = gtk_frame_new( NULL );
|
|
278 playman_pl_lv = gtk_tree_view_new_with_model( GTK_TREE_MODEL(pl_store) );
|
|
279 g_object_unref( pl_store );
|
|
280 g_object_set_data( G_OBJECT(playman_win) , "lv" , playman_pl_lv );
|
|
281 g_object_set_data( G_OBJECT(playman_pl_lv) , "opt1" , GINT_TO_POINTER(0) );
|
|
282 playman_pl_lv_textrndr_entriesnum = gtk_cell_renderer_text_new(); /* uneditable */
|
|
283 playman_pl_lv_textrndr_name = gtk_cell_renderer_text_new(); /* can become editable */
|
|
284 g_signal_connect( G_OBJECT(playman_pl_lv_textrndr_name) , "edited" ,
|
|
285 G_CALLBACK(playlist_manager_cb_lv_name_edited) , playman_pl_lv );
|
|
286 g_object_set_data( G_OBJECT(playman_pl_lv) , "rn" , playman_pl_lv_textrndr_name );
|
|
287 playman_pl_lv_col_name = gtk_tree_view_column_new_with_attributes(
|
|
288 _("Playlist") , playman_pl_lv_textrndr_name , "text" , PLLIST_COL_NAME , NULL );
|
|
289 gtk_tree_view_column_set_expand( GTK_TREE_VIEW_COLUMN(playman_pl_lv_col_name) , TRUE );
|
|
290 gtk_tree_view_append_column( GTK_TREE_VIEW(playman_pl_lv), playman_pl_lv_col_name );
|
|
291 playman_pl_lv_col_entriesnum = gtk_tree_view_column_new_with_attributes(
|
|
292 _("Entries") , playman_pl_lv_textrndr_entriesnum , "text" , PLLIST_COL_ENTRIESNUM , NULL );
|
|
293 gtk_tree_view_column_set_expand( GTK_TREE_VIEW_COLUMN(playman_pl_lv_col_entriesnum) , FALSE );
|
|
294 gtk_tree_view_append_column( GTK_TREE_VIEW(playman_pl_lv), playman_pl_lv_col_entriesnum );
|
|
295 playman_pl_lv_sw = gtk_scrolled_window_new( NULL , NULL );
|
|
296 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(playman_pl_lv_sw) ,
|
|
297 GTK_POLICY_NEVER , GTK_POLICY_ALWAYS );
|
|
298 gtk_container_add( GTK_CONTAINER(playman_pl_lv_sw) , playman_pl_lv );
|
|
299 gtk_container_add( GTK_CONTAINER(playman_pl_lv_frame) , playman_pl_lv_sw );
|
|
300 gtk_box_pack_start( GTK_BOX(playman_vbox) , playman_pl_lv_frame , TRUE , TRUE , 0 );
|
|
301
|
|
302 gtk_box_pack_start( GTK_BOX(playman_vbox) , gtk_hseparator_new() , FALSE , FALSE , 4 );
|
|
303
|
|
304 /* listview popup menu */
|
|
305 playman_pl_lv_pmenu = gtk_menu_new();
|
|
306 playman_pl_lv_pmenu_rename = gtk_menu_item_new_with_mnemonic( _( "_Rename" ) );
|
|
307 g_signal_connect( G_OBJECT(playman_pl_lv_pmenu_rename) , "activate" ,
|
|
308 G_CALLBACK(playlist_manager_cb_lv_pmenu_rename) , playman_pl_lv );
|
|
309 gtk_menu_shell_append( GTK_MENU_SHELL(playman_pl_lv_pmenu) , playman_pl_lv_pmenu_rename );
|
|
310 gtk_widget_show_all( playman_pl_lv_pmenu );
|
|
311 g_object_set_data( G_OBJECT(playman_pl_lv) , "menu" , playman_pl_lv_pmenu );
|
|
312 g_signal_connect_swapped( G_OBJECT(playman_win) , "destroy" ,
|
|
313 G_CALLBACK(gtk_widget_destroy) , playman_pl_lv_pmenu );
|
|
314
|
|
315 /* button bar */
|
|
316 playman_bbar_hbbox = gtk_hbutton_box_new();
|
|
317 gtk_button_box_set_layout( GTK_BUTTON_BOX(playman_bbar_hbbox) , GTK_BUTTONBOX_START );
|
|
318 playman_bbar_bt_new = gtk_button_new_from_stock( GTK_STOCK_NEW );
|
|
319 playman_bbar_bt_del = gtk_button_new_from_stock( GTK_STOCK_DELETE );
|
|
320 playman_bbar_bt_close = gtk_button_new_from_stock( GTK_STOCK_CLOSE );
|
|
321 gtk_container_add( GTK_CONTAINER(playman_bbar_hbbox) , playman_bbar_bt_new );
|
|
322 gtk_container_add( GTK_CONTAINER(playman_bbar_hbbox) , playman_bbar_bt_del );
|
|
323 gtk_container_add( GTK_CONTAINER(playman_bbar_hbbox) , playman_bbar_bt_close );
|
|
324 gtk_button_box_set_child_secondary( GTK_BUTTON_BOX(playman_bbar_hbbox) ,
|
|
325 playman_bbar_bt_close , TRUE );
|
|
326 gtk_box_pack_start( GTK_BOX(playman_vbox) , playman_bbar_hbbox , FALSE , FALSE , 0 );
|
|
327
|
|
328 g_signal_connect( G_OBJECT(playman_pl_lv) , "button-press-event" ,
|
|
329 G_CALLBACK(playlist_manager_cb_lv_btpress) , NULL );
|
|
330 g_signal_connect( G_OBJECT(playman_pl_lv) , "row-activated" ,
|
|
331 G_CALLBACK(playlist_manager_cb_lv_dclick) , NULL );
|
|
332 g_signal_connect_swapped( G_OBJECT(playman_bbar_bt_new) , "clicked" ,
|
|
333 G_CALLBACK(playlist_manager_cb_new) , playman_pl_lv );
|
|
334 g_signal_connect_swapped( G_OBJECT(playman_bbar_bt_del) , "clicked" ,
|
|
335 G_CALLBACK(playlist_manager_cb_del) , playman_pl_lv );
|
|
336 g_signal_connect_swapped( G_OBJECT(playman_bbar_bt_close) , "clicked" ,
|
|
337 G_CALLBACK(gtk_widget_destroy) , playman_win );
|
|
338
|
|
339 gtk_widget_show_all( playman_win );
|
|
340 }
|
|
341
|
|
342
|
|
343 void
|
|
344 playlist_manager_update ( void )
|
|
345 {
|
|
346 /* this function is called whenever there is a change in playlist, such as
|
|
347 playlist created/deleted or entry added/deleted in a playlist; if the playlist
|
|
348 manager is active, it should be updated to keep consistency of information */
|
|
349
|
|
350 /* CAREFUL! this currently locks/unlocks all the playlists */
|
|
351
|
|
352 if ( playman_win != NULL )
|
|
353 {
|
|
354 GtkWidget *lv = (GtkWidget*)g_object_get_data( G_OBJECT(playman_win) , "lv" );
|
|
355 if ( GPOINTER_TO_INT(g_object_get_data(G_OBJECT(lv),"opt1")) == 0 )
|
|
356 {
|
|
357 GtkListStore *store = (GtkListStore*)gtk_tree_view_get_model( GTK_TREE_VIEW(lv) );
|
|
358 /* TODO: this re-populates everything... there's definitely room for optimization */
|
|
359 gtk_list_store_clear( store );
|
|
360 playlist_manager_populate( store );
|
|
361 }
|
|
362 return;
|
|
363 }
|
|
364 else
|
|
365 return; /* if the playlist manager is not active, simply return */
|
|
366 }
|