small fix
[gmpc.git] / src / vala / gmpc-sidebar-plugins.vala
blob0c909984f893566d143b4acfe6b118762f01990d
1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2012 Qball Cow <qball@gmpclient.org>
3 * Project homepage: http://gmpclient.org/
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 using Config;
21 using Gtk;
22 using Gmpc;
23 using Gmpc.Plugin;
25 const string GSBP_LOG_DOMAIN = "Gmpc.Sidebar.Plugins";
27 public class Gmpc.Sidebar.Plugins
29 private static ListStore store;
31 private static void embed(TreeIter iter)
33 SidebarIface plugin;
34 store.get(iter, 3, out plugin, -1);
36 Grid sidebar_vbox;
37 Grid vbox;
39 Label label;
40 string title;
41 int position;
43 vbox = new Grid();
45 title = plugin.sidebar_get_title();
47 if (title == null)
48 title = plugin.get_name();
50 position = plugin.sidebar_get_position();
52 if (title != "")
55 label = new Label(GLib.Markup.printf_escaped("<b>%s</b>",title));
56 label.set_use_markup(true);
57 label.set_alignment(0f, 0.5f);
58 vbox.add(label);
60 store.set(iter, 5, label, -1);
63 if ( position >= 0)
65 sidebar_vbox = (Grid)Playlist.get_widget_by_id("sidebar_plugins_top");
67 else
69 sidebar_vbox = (Grid)Playlist.get_widget_by_id("sidebar_plugins_bottom");
72 sidebar_vbox.ref();
74 if (position >= 0)
76 sidebar_vbox.add(vbox);//, false, false, 0);
78 else
80 sidebar_vbox.add(vbox);//, false, false, 0);
86 store.set(iter, 4, vbox, -1);
87 reorder();
88 plugin.sidebar_pane_construct(vbox);
92 private static void reorder()
94 // TODO
95 #if 0
96 // assumption: liststore is sorted descending by position
97 TreeIter iter;
98 Grid vbox;
99 Grid sidebar_vbox;
100 bool enabled;
101 int pos;
102 List<Grid> list_top = new List<Grid> ();
103 List<Grid> list_bottom = new List<Grid> ();
105 store.get_iter_first(out iter);
109 store.get(iter, 0, out enabled, 2, out pos, 4, out vbox, -1);
110 if ((vbox == null) || (!enabled))
111 continue;
113 if (pos >= 0)
115 list_top.append(vbox);
117 else
119 list_bottom.append(vbox);
123 while (store.iter_next(ref iter));
125 sidebar_vbox = (Grid)Playlist.get_widget_by_id("sidebar_plugins_top");
126 sidebar_vbox.ref();
127 list_top.reverse();
128 for (int i = 0; i < list_top.length(); i++)
131 sidebar_vbox.reorder_child((Widget)list_top.nth_data(i), i);
134 sidebar_vbox = (Grid)Playlist.get_widget_by_id("sidebar_plugins_bottom");
135 sidebar_vbox.ref();
136 for (int i = 0; i < list_bottom.length(); i++)
139 sidebar_vbox.reorder_child((Widget)list_bottom.nth_data(i), i);
141 #endif
144 private static void destroy(TreeIter iter)
146 SidebarIface plugin;
147 Grid vbox;
148 store.get(iter, 3, out plugin, 4, out vbox, -1);
150 plugin.sidebar_pane_destroy(vbox);
151 vbox.destroy();
152 vbox = (Grid)null;
155 private static TreeIter lookup_iter(SidebarIface plugin)
157 TreeIter iter;
158 if(store.get_iter_first(out iter))
160 SidebarIface pl;
163 store.get(iter, 3, out pl, -1);
164 if(pl == plugin)
166 return iter;
169 while(store.iter_next(ref iter));
171 GLib.error("Plugin not found in list");
174 public static void enable(SidebarIface plugin)
176 TreeIter iter = lookup_iter(plugin);
177 plugin.set_enabled(true);
178 store.set(iter, 0, true, -1);
179 embed(iter);
182 public static void disable(SidebarIface plugin)
184 TreeIter iter = lookup_iter(plugin);
185 plugin.set_enabled(false);
186 store.set(iter, 0, false, -1);
187 destroy(iter);
189 public static void update_state(Gmpc.Plugin.SidebarState state)
191 TreeIter iter;
192 if(store.get_iter_first(out iter))
196 Widget? widget;
197 SidebarIface pl;
198 store.get(iter, 3, out pl,5, out widget, -1);
199 pl.sidebar_set_state(state);
201 // Hide the sidebar name widget in collapsed mode.
202 if(widget != null) {
203 if(state == Gmpc.Plugin.SidebarState.COLLAPSED) {
204 widget.hide();
205 } else {
206 widget.show();
210 while(store.iter_next(ref iter));
215 public static void init(SidebarIface plugin)
217 if (store == null)
218 store = new ListStore(6, typeof(bool), // enabled
219 typeof(string), // name
220 typeof(int), // position
221 typeof(SidebarIface), // plugin
222 typeof(Grid), // Widget
223 typeof(Widget)); // Label
224 log(GSBP_LOG_DOMAIN, GLib.LogLevelFlags.LEVEL_DEBUG, "Initializing sidebar plugin");
225 TreeIter iter;
226 store.append(out iter);
227 store.set(iter, 0, plugin.get_enabled(),
228 1, plugin.get_name(),
229 2, plugin.sidebar_get_position(),
230 3, plugin,
231 4, null,
232 5, null,
233 -1);
236 store.set_sort_column_id(2, SortType.DESCENDING);
238 if (plugin.get_enabled())
240 enable(plugin);