2 * zavai-player - zavai music player
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 protected class Player : Gtk.VBox
25 public Gtk.ScrolledWindow scroll;
26 protected Gtk.TextBuffer text_buffer;
27 protected Gtk.TextView text;
28 protected Regex unhtml;
29 protected Regex seplines;
31 public string grm_name { get; set; }
32 public string grm_type { get; set; }
34 public Player() throws RegexError
38 unhtml = new Regex("[ ]*<[^>]+>[ ]*", 0, 0);
39 seplines = new Regex("\n");
40 text_buffer = new Gtk.TextBuffer(null);
41 text = new Gtk.TextView.with_buffer(text_buffer);
42 text.wrap_mode = Gtk.WrapMode.WORD;
43 text.cursor_visible = false;
44 text.editable = false;
45 text.add_events(Gdk.EventMask.BUTTON_PRESS_MASK);
46 text.button_press_event += on_button_press;
47 scroll = new Gtk.ScrolledWindow (null, null);
48 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
50 pack_start(scroll, true, true, 0);
53 public bool on_button_press(Gdk.EventButton event)
61 if (grm_name == "") return;
62 string[] args = new string[5];
63 args[0] = "/usr/bin/polygen";
64 args[1] = "/usr/share/polygen/" + grm_name + ".grm";
65 if (grm_type == "line/text" || grm_type == "line/html")
76 Process.spawn_async_with_pipes(homedir, args, null, 0, null, null, null, out pipe_out, null);
77 IOChannel ch = new IOChannel.unix_new(pipe_out);
79 ch.read_to_end(out result, out res_len);
82 if (grm_type == "line/html" || grm_type == "block/html")
84 result = unhtml.replace(result, (long)res_len, 0, " ", 0);
85 } else if (grm_type == "line/text") {
86 result = seplines.replace(result, (long)res_len, 0, "\n\n", 0);
91 result = "Error: " + e.message;
93 text_buffer.text = result;
95 text_buffer.get_iter_at_offset(out iter, 0);
96 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
97 text.scroll_mark_onscreen(mark);
101 public class ZavaiPlayer : Gtk.Window
103 //protected Gee.ArrayList<PolygenPage> pages;
104 protected Gtk.Notebook notebook;
105 protected Player player;
106 protected Gtk.ListStore tracks;
107 protected FisheyeListView tracklist;
109 private void add_track(string name, int length)
112 tracks.append (out iter);
113 length = length / 1000;
114 tracks.set(iter, 0, "%s - %d'%d".printf(name, length/60, length % 60));
115 tracks.set(iter, 1, name);
119 protected void on_selected(string page, string name, string type)
121 result.grm_name = page + "/" + name;
122 result.grm_type = type;
124 notebook.set_current_page(notebook.get_n_pages()-1);
128 public ZavaiPlayer(string label, IOChannel data) throws ConvertError, IOChannelError, RegexError
131 destroy += Gtk.main_quit;
133 tracks = new Gtk.ListStore(2, typeof(string), typeof(string));
135 var vbox = new Gtk.VBox(false, 0);
138 //pages = new Gee.ArrayList<PolygenPage>();
140 string cur_file = null;
145 var res = data.read_line(out line, null, null);
146 if (res != IOStatus.NORMAL) break;
148 if (line.size() == 0)
150 add_track(cur_file, cur_len);
153 } else if (line.has_prefix("file: "))
154 cur_file = line.substring(6);
155 else if (line.has_prefix("length: "))
156 cur_len = line.substring(8).to_int();
158 if (cur_file != null)
159 add_track(cur_file, cur_len);
161 tracklist = new FisheyeListView();
162 tracklist.set_model(tracks);
163 tracklist.row_activated += on_row_activated;
165 notebook = new Gtk.Notebook();
166 notebook.append_page(tracklist, new Gtk.Label("Tracks"));
168 foreach (PolygenPage p in pages)
170 notebook.append_page(p.scroll, new Gtk.Label(p.name));
173 player = new Player();
174 notebook.append_page(player, new Gtk.Label("Player"));
176 vbox.pack_start(notebook, true, true, 0);
178 //Gtk.Button select = new Gtk.Button.with_label("Select");
179 //button_box.pack_start(select, true, true, 0);
180 //select.clicked += on_click;
183 public void on_row_activated(Gtk.TreePath path)
186 tracks.get_iter(out iter, path);
189 tracks.get(iter, 1, out grm, 2, out type, -1);
190 stdout.printf("Clicked on %s %s\n", type, grm);
192 player.grm_name = grm;
193 player.grm_type = type;
195 notebook.set_current_page(notebook.get_n_pages()-1);
199 private void on_click(Gtk.Button b)
201 stderr.printf("ZA\n");
202 if (notebook.page >= pages.size)
203 result.update(); // If we are in the result page, just update
206 PolygenPage page = pages[notebook.page];
215 static int main (string[] args) {
218 homedir = GLib.Environment.get_home_dir() + "/.zavai";
221 var data = new IOChannel.file(homedir + "/player-info", "r");
222 polygen = new ZavaiPlayer("Zavai Player", data);
223 polygen.set_size_request(300, 500);
225 data.shutdown(false);
227 //zavai.registry.register_applet("ui.polygen", polygen);
228 //zavai.registry.getmenu("menu.misc").add_applet("ui.polygen");
230 stderr.printf("%s", e.message);