First setup GUI, then initialize plugins, this allows extra playlist to intergrate...
[gmpc.git] / src / Plugins / extraplaylist.c
blobf24684bc48096d69bfa7128a5a35fdf0c9262d92
1 /* gmpc-extraplaylist (GMPC plugin)
2 * Copyright (C) 2006-2009 Qball Cow <qball@gmpclient.org>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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 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.
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;
32 static GtkWidget *cb_include_sidebar = NULL;
34 static gboolean extra_playlist_add(void);
36 static void extra_playlist_save(void) {
37 #if 0
38 if(extraplaylist) {
39 int pos = gtk_paned_get_position(GTK_PANED(extraplaylist_paned));
40 if(pos>0)
41 cfg_set_single_value_as_int(config, "extraplaylist", "paned-pos", pos);
43 #endif
46 static void store_pane_pos(void) {
47 if(extraplaylist) {
48 int pos = gtk_paned_get_position(GTK_PANED(extraplaylist_paned));
49 if(pos>0)
50 cfg_set_single_value_as_int(config, "extraplaylist", "paned-pos", pos);
54 static int get_enabled(void) {
55 return cfg_get_single_value_as_int_with_default(config,"extraplaylist", "enabled", 0);
58 static void ep_view_changed(GtkTreeSelection *selection, gpointer user_data)
60 GtkTreeModel *model= NULL;
61 GtkTreeIter iter;
62 if(gtk_tree_selection_get_selected(selection, &model, &iter))
64 int type = 0;
65 _gmpcPluginParent *parent = NULL;
66 gtk_tree_model_get(model, &iter, PL3_CAT_TYPE, &type, -1);
67 parent = (_gmpcPluginParent *)plugin_get_from_id(type);
68 if(parent)
70 if(parent->new){
71 if(G_OBJECT_TYPE(parent->new) == G_OBJECT_TYPE(play_queue_plugin))
73 if(extraplaylist){
74 gtk_widget_hide(extraplaylist);
75 if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
76 gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
78 return;
81 if(get_enabled())
83 if(!extraplaylist) {
84 extra_playlist_add();
85 }else{
86 if(gtk_bin_get_child(GTK_BIN(extraplaylist)) == NULL){
87 gmpc_plugin_browser_iface_browser_selected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin), GTK_CONTAINER(extraplaylist));
88 gtk_widget_show(extraplaylist);
92 }else if(extraplaylist){
93 gtk_widget_hide(extraplaylist);
94 if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
95 gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
101 static void extra_playlist_remove(void) {
102 GtkWidget *temp = NULL;
103 if(pl3_xml == NULL) return;
104 if(extraplaylist == NULL && get_enabled() == FALSE) return;
105 if(extraplaylist == NULL) return;
107 temp = GTK_WIDGET(gtk_builder_get_object(pl3_xml, "hpaned1-hbox"));
108 g_object_ref(temp);
110 extra_playlist_save();
111 /* Remove widget */
112 gtk_widget_hide(extraplaylist);
113 if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
114 gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
116 /* Remove it from inserted widget */
117 gtk_container_remove(GTK_CONTAINER(extraplaylist_paned), temp);
119 /* Destroy the previously added stuff split view */
120 gtk_widget_destroy(extraplaylist);
121 extraplaylist = NULL;
122 gtk_widget_destroy(extraplaylist_paned);
123 extraplaylist = NULL;
124 gtk_container_add(GTK_CONTAINER(gtk_builder_get_object(pl3_xml,"hpaned1_vbox")), temp);
128 static gboolean extra_playlist_add(void) {
130 extra_playlist_remove();
132 GtkWidget *temp = NULL;
133 if(pl3_xml == NULL) return FALSE;
134 if(extraplaylist == NULL && get_enabled() == FALSE) return FALSE;
135 if(extraplaylist != NULL) return FALSE;
137 temp = GTK_WIDGET(gtk_builder_get_object(pl3_xml, "hpaned1-hbox"));
138 g_object_ref(temp);
141 * Hack it into the main view
143 extraplaylist = gtk_event_box_new();
144 /* Set border to fits gmpc's standard */
145 gtk_container_set_border_width(GTK_CONTAINER(extraplaylist), 0);
147 if(cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout", TRUE))
149 extraplaylist_paned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
150 }else{
151 extraplaylist_paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
155 gtk_container_remove(GTK_CONTAINER(gtk_builder_get_object(pl3_xml,"hpaned1_vbox")),temp);
157 gtk_paned_pack1(GTK_PANED(extraplaylist_paned), temp, TRUE, TRUE);
158 gtk_paned_pack2(GTK_PANED(extraplaylist_paned), extraplaylist, TRUE, TRUE);
160 gtk_container_add(GTK_CONTAINER(gtk_builder_get_object(pl3_xml,"hpaned1_vbox")),extraplaylist_paned);
163 gtk_paned_set_position(GTK_PANED(extraplaylist_paned),
164 cfg_get_single_value_as_int_with_default(config, "extraplaylist", "paned-pos", 400));
166 gtk_widget_show(extraplaylist_paned);
167 gtk_widget_hide(extraplaylist);
169 if(play_queue_plugin == NULL) {
170 play_queue_plugin = (GmpcPluginBase *)play_queue_plugin_new("extra-playlist-plugin");
172 ep_view_changed(gtk_tree_view_get_selection(playlist3_get_category_tree_view()),NULL);
174 /* Attach changed signal */
175 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(playlist3_get_category_tree_view())), "changed",
176 G_CALLBACK(ep_view_changed), NULL);
178 g_signal_connect(G_OBJECT(extraplaylist_paned), "notify::position", G_CALLBACK(store_pane_pos), NULL);
179 return FALSE;
183 static void extra_playlist_init(void ) {
184 if( cfg_get_single_value_as_int_with_default(config,"extraplaylist", "enabled", 0)) {
185 // TODO Fix this. This should be done, directly after the window is created.
186 // g_idle_add(extra_playlist_add, NULL);
187 extra_playlist_add();
190 static void set_enabled(int enable) {
191 cfg_set_single_value_as_int(config,"extraplaylist", "enabled", enable);
192 if(enable)
194 if(!extraplaylist) {
195 extra_playlist_add();
196 }else{
197 ep_view_changed(gtk_tree_view_get_selection(playlist3_get_category_tree_view()),NULL);
200 } else if(extraplaylist){
201 gtk_widget_hide(extraplaylist);
202 if(gtk_bin_get_child(GTK_BIN(extraplaylist)))
203 gmpc_plugin_browser_iface_browser_unselected(GMPC_PLUGIN_BROWSER_IFACE(play_queue_plugin),GTK_CONTAINER(extraplaylist));
208 * Preferences
210 static void preferences_layout_changed(GtkToggleButton *but, gpointer user_data)
212 gint active = gtk_toggle_button_get_active(but);
213 cfg_set_single_value_as_int(config, "extraplaylist", "vertical-layout", active);
214 gtk_widget_set_sensitive(GTK_WIDGET(cb_include_sidebar), cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout", TRUE));
215 extra_playlist_add();
219 static void preferences_layout_sidebar_changed(GtkToggleButton *but, gpointer user_data)
221 gint active = gtk_toggle_button_get_active(but);
222 cfg_set_single_value_as_int(config, "extraplaylist", "include-sidebar", active);
223 extra_playlist_add();
226 static void preferences_construct(GtkWidget *container)
228 GtkWidget *vbox = gtk_vbox_new(FALSE, 3);
229 GtkWidget *label = NULL;
231 GtkWidget *frame = gtk_frame_new("");
232 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
233 gtk_label_set_markup(GTK_LABEL(gtk_frame_get_label_widget(GTK_FRAME(frame))), "<b>Appearance</b>");
235 GtkWidget *align = gtk_alignment_new(0,0,0,0);
236 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 12, 0, 12, 0);
238 /* The checkbox */
239 label = gtk_check_button_new_with_label("Use horizontal layout");
240 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(label), cfg_get_single_value_as_int_with_default(config, "extraplaylist", "vertical-layout", TRUE));
241 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
242 g_signal_connect(G_OBJECT(label), "toggled", G_CALLBACK(preferences_layout_changed), NULL);
245 /* show and add */
246 gtk_container_add(GTK_CONTAINER(align), vbox);
247 gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(align));
248 gtk_widget_show_all(frame);
249 gtk_container_add(GTK_CONTAINER(container), frame);
252 static void preferences_destroy(GtkWidget *container)
254 gtk_container_remove(GTK_CONTAINER(container), gtk_bin_get_child(GTK_BIN(container)));
257 gmpcPrefPlugin extra_playlist_preferences = {
258 .construct = preferences_construct,
259 .destroy = preferences_destroy
263 gmpcPlugin extraplaylist_plugin = {
264 .name = "Extra playlist",
265 .version = {0, 0, 1},
266 .plugin_type = GMPC_PLUGIN_NO_GUI | GMPC_INTERNALL,
267 .init = extra_playlist_init, /* initialization */
268 .save_yourself = extra_playlist_save, /* Destroy */
269 .get_enabled = get_enabled,
270 .set_enabled = set_enabled,
272 .pref = &extra_playlist_preferences,