1 /* gmpc-extraplaylist (GMPC plugin)
2  * Copyright (C) 2006-2009 Qball Cow <qball@gmpclient.org>
3  * Project homepage: http://gmpcwiki.sarine.nl/
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <string.h>
21 #include "plugin.h"
22 #include "plugin-internal.h"
23 #include "playlist3.h"
24 #include "gmpc-extras.h"
25 #include "config.h"
26 #include "browsers/playlist3-current-playlist-browser.h"
27 /* External pointer + function, there internal from gmpc */
28 GtkWidget *extraplaylist = NULL;
29 static GtkWidget *extraplaylist_paned = NULL;
30 static GmpcPluginBase *play_queue_plugin = NULL;
31 
32 static void extra_playlist_add(void);
33 
extra_playlist_save(void)34 static void extra_playlist_save(void) {
35 	if(extraplaylist) {
36 		int pos = gtk_paned_get_position(GTK_PANED(extraplaylist_paned));
37 		if(pos>0)
38 			cfg_set_single_value_as_int(config, "extraplaylist", "paned-pos", pos);
39 	}
40 }
41 
get_enabled(void)42 static int get_enabled(void) {
43 	return cfg_get_single_value_as_int_with_default(config,"extraplaylist", "enabled", 0);
44 }
45 
ep_view_changed(GtkTreeSelection * selection,gpointer user_data)46 static void ep_view_changed(GtkTreeSelection *selection, gpointer user_data)
47 {
48     GtkTreeModel *model= NULL;
49     GtkTreeIter iter;
50     if(gtk_tree_selection_get_selected(selection, &model, &iter))
51     {
52         int type = 0;
53         _gmpcPluginParent *parent = NULL;
54         gtk_tree_model_get(model, &iter, PL3_CAT_TYPE, &type, -1);
55         parent = (_gmpcPluginParent *)plugin_get_from_id(type);
56         if(parent)
57         {
58             if(parent->new){
59                 if(G_OBJECT_TYPE(parent->new) == G_OBJECT_TYPE(play_queue_plugin))
60                 {
61                     if(extraplaylist){
62                         gtk_widget_hide(extraplaylist);
63                         if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
64                             gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
65                     }
66                     return;
67                 }
68             }
69             if(get_enabled())
70             {
71                 if(!extraplaylist) {
72                     extra_playlist_add();
73                 }else{
74                     if(gtk_bin_get_child(GTK_BIN(extraplaylist)) == NULL){
75                         gmpc_plugin_browser_iface_browser_selected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin), GTK_CONTAINER(extraplaylist));
76                         gtk_widget_show(extraplaylist);
77                     }
78                 }
79 
80             }else if(extraplaylist){
81                 gtk_widget_hide(extraplaylist);
82                 if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
83                     gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
84             }
85         }
86     }
87 }
88 
89 
extra_playlist_add(void)90 static void extra_playlist_add(void) {
91 
92     GtkWidget *temp = NULL;
93 	if(pl3_xml == NULL) return;
94     if(extraplaylist == NULL  && get_enabled() == FALSE) return;
95 
96 	temp = GTK_WIDGET(gtk_builder_get_object(pl3_xml, "vbox7"));
97 	g_object_ref(temp);
98 
99     if(extraplaylist){
100         extra_playlist_save();
101         /* Remove widget */
102         gtk_widget_hide(extraplaylist);
103         if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
104                     gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
105 
106         /* Remove it from inserted widget */
107         gtk_container_remove(GTK_CONTAINER(extraplaylist_paned), temp);
108 
109         /* Destroy the previously added stuff split view */
110         gtk_widget_destroy(extraplaylist);
111         extraplaylist = NULL;
112         gtk_widget_destroy(extraplaylist_paned);
113         extraplaylist = NULL;
114         gtk_paned_pack2(GTK_PANED(gtk_builder_get_object(pl3_xml,"hpaned1")),temp, TRUE, TRUE);
115     }
116 	/**
117 	 * Hack it into the main view
118 	 */
119     extraplaylist = gtk_event_box_new();
120     /* Set border to fits gmpc's standard  */
121     gtk_container_set_border_width(GTK_CONTAINER(extraplaylist), 0);
122 
123     if(cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout", TRUE))
124     {
125         extraplaylist_paned = gtk_vpaned_new();
126     }else{
127         extraplaylist_paned = gtk_hpaned_new();
128     }
129 	gtk_container_remove(GTK_CONTAINER(gtk_builder_get_object(pl3_xml,"hpaned1")),temp);
130 
131 
132 
133     if(!cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout-swapped",FALSE))
134     {
135         gtk_paned_pack1(GTK_PANED(extraplaylist_paned), temp, TRUE, TRUE);
136         gtk_paned_pack2(GTK_PANED(extraplaylist_paned), extraplaylist, TRUE, TRUE);
137     }else{
138         gtk_paned_pack2(GTK_PANED(extraplaylist_paned), temp, TRUE, TRUE);
139         gtk_paned_pack1(GTK_PANED(extraplaylist_paned), extraplaylist, TRUE, TRUE);
140     }
141 
142     gtk_paned_pack2(GTK_PANED(gtk_builder_get_object(pl3_xml, "hpaned1")), extraplaylist_paned,TRUE, TRUE);//, TRUE, TRUE, 0);
143 
144 	gtk_paned_set_position(GTK_PANED(extraplaylist_paned),cfg_get_single_value_as_int_with_default(config, "extraplaylist", "paned-pos", 400));
145 
146 	gtk_widget_show(extraplaylist_paned);
147     gtk_widget_hide(extraplaylist);
148 
149     if(play_queue_plugin == NULL) {
150         play_queue_plugin = (GmpcPluginBase *)play_queue_plugin_new("extra-playlist-plugin");
151     }
152     ep_view_changed(gtk_tree_view_get_selection(playlist3_get_category_tree_view()),NULL);
153 
154     /* Attach changed signal */
155     g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(playlist3_get_category_tree_view())), "changed",
156         G_CALLBACK(ep_view_changed), NULL);
157 }
158 
159 
extra_playlist_init(void)160 static void extra_playlist_init(void ) {
161 	if( cfg_get_single_value_as_int_with_default(config,"extraplaylist", "enabled", 1)) {
162 		gtk_init_add((GtkFunction )extra_playlist_add, NULL);
163 	}
164 }
set_enabled(int enable)165 static void set_enabled(int enable) {
166     cfg_set_single_value_as_int(config,"extraplaylist", "enabled", enable);
167     if(enable)
168 	{
169 		if(!extraplaylist) {
170 			extra_playlist_add();
171 		}else{
172             ep_view_changed(gtk_tree_view_get_selection(playlist3_get_category_tree_view()),NULL);
173         }
174 
175     } else if(extraplaylist){
176 		gtk_widget_hide(extraplaylist);
177         if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
178             gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
179 	}
180     pl3_tool_menu_update();
181 }
182 
183 /**
184  * Preferences
185  */
preferences_layout_changed(GtkToggleButton * but,gpointer user_data)186 static void preferences_layout_changed(GtkToggleButton *but, gpointer user_data)
187 {
188     gint active = gtk_toggle_button_get_active(but);
189     cfg_set_single_value_as_int(config, "extraplaylist", "vertical-layout", active);
190     extra_playlist_add();
191 }
192 
preferences_layout_swapped_changed(GtkToggleButton * but,gpointer user_data)193 static void preferences_layout_swapped_changed(GtkToggleButton *but, gpointer user_data)
194 {
195     gint active = gtk_toggle_button_get_active(but);
196     cfg_set_single_value_as_int(config, "extraplaylist", "vertical-layout-swapped", active);
197     extra_playlist_add();
198 }
preferences_construct(GtkWidget * container)199 static  void preferences_construct(GtkWidget *container)
200 {
201     GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
202     GtkWidget *label = NULL;
203 
204     /* The checkbox */
205     label = gtk_check_button_new_with_label("Use horizontal layout");
206     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(label), cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout", TRUE));
207     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
208     g_signal_connect(G_OBJECT(label), "toggled", G_CALLBACK(preferences_layout_changed), NULL);
209 
210     /* The checkbox */
211     label = gtk_check_button_new_with_label("Swap position of the extra playlist");
212     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(label), cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout-swapped", FALSE));
213     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
214     g_signal_connect(G_OBJECT(label), "toggled", G_CALLBACK(preferences_layout_swapped_changed), NULL);
215 
216     /* show and add */
217     gtk_widget_show_all(vbox);
218     gtk_container_add(GTK_CONTAINER(container), vbox);
219 
220 }
preferences_destroy(GtkWidget * container)221 static void preferences_destroy(GtkWidget *container)
222 {
223     gtk_container_remove(GTK_CONTAINER(container), gtk_bin_get_child(GTK_BIN(container)));
224 
225 }
226  gmpcPrefPlugin extra_playlist_preferences = {
227     .construct = preferences_construct,
228     .destroy = preferences_destroy
229 
230  };
231 
232 gmpcPlugin extraplaylist_plugin = {
233 	.name = "Internal Extra Playlist View",
234 	.version = {0, 0, 1},
235 	.plugin_type = GMPC_PLUGIN_NO_GUI | GMPC_INTERNALL,
236 	.init = extra_playlist_init,            /* initialization */
237 	.save_yourself = extra_playlist_save,         /* Destroy */
238 	.get_enabled = get_enabled,
239 	.set_enabled = set_enabled,
240 
241     .pref = &extra_playlist_preferences,
242 };
243