Started polygen plugin
[gregoa/zavai.git] / src / app_polygen.vala
1 /*
2  * app_wm - zavai window management functions
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace ui {
25 namespace polygen {
26
27 protected class PolygenPage : Object
28 {
29     public string name { get; construct; }
30     public Gtk.ListStore model;
31     public Gtk.TreeView list;
32     public Gtk.ScrolledWindow scroll;
33     public signal void selected(string page, string name);
34
35     public PolygenPage(string name)
36     {
37         this.name = name;
38         model = new Gtk.ListStore(2, typeof(string), typeof(string));
39         list = new Gtk.TreeView.with_model(model);
40         list.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText(), "text", 0);
41         list.insert_column_with_attributes (-1, "Title", new Gtk.CellRendererText(), "text", 1);
42         scroll = new Gtk.ScrolledWindow (null, null);
43         scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
44         scroll.add(list);
45
46         list.row_activated += on_row_activated;
47     }
48
49     private void on_row_activated(Gtk.TreeView tv, Gtk.TreePath path, Gtk.TreeViewColumn column)
50     {
51         Gtk.TreeIter iter;
52         if (!model.get_iter(out iter, path)) return;
53         Value grm_name;
54         model.get_value(iter, 0, out grm_name);
55         selected(name, (string)grm_name);
56     }
57 }
58
59 protected class PolygenRun : Gtk.VBox
60 {
61     protected Gtk.Label text;
62     protected Gtk.Button text_button;
63
64     private string _grammar;
65     public string grammar {
66         get { return _grammar; }
67         set {
68             _grammar = value;
69             update();
70         }
71     }
72
73     public PolygenRun()
74     {
75         _grammar = "";
76         text = new Gtk.Label("");
77         text.use_markup = true;
78         text.wrap = true;
79         text_button = new Gtk.Button();
80         text_button.set_image(text);
81                 pack_start(text_button, true, true, 0);
82         text_button.clicked += on_clicked;
83     }
84
85     public void on_clicked(Gtk.Button button) { update(); }
86
87     public void update()
88     {
89         text.label = "ciao " + _grammar;
90     }
91 }
92
93 public class Polygen : Applet
94 {
95     protected Gee.ArrayList<PolygenPage> pages;
96     protected Gtk.Notebook notebook;
97     protected PolygenRun result;
98
99     private void add(string page, string name, string title)
100     {
101         PolygenPage pg = null;
102         foreach (PolygenPage p in pages)
103         {
104             if (p.name == page)
105             {
106                 pg = p;
107                 break;
108             }
109         }
110         if (pg == null)
111         {
112             pg = new PolygenPage(page);
113             pg.selected += on_selected;
114             pages.add(pg);
115         }
116
117         Gtk.TreeIter iter;
118         pg.model.append (out iter);
119         pg.model.set(iter, 0, name);
120         pg.model.set(iter, 1, title);
121     }
122
123     protected void on_selected(string page, string name)
124     {
125         result.grammar = page + "/" + name;
126         notebook.set_current_page(notebook.get_n_pages()-1);
127     }
128
129         public Polygen(string label, IOChannel data)
130         {
131                 _label = label;
132         pages = new Gee.ArrayList<PolygenPage>();
133
134         while (true)
135         {
136             string line;
137             var res = data.read_line(out line, null, null);
138             if (res != IOStatus.NORMAL) break;
139             string[] vals = line.split(" ", 2);
140             if (vals == null) break;
141             string[] np = vals[0].split("/", 2);
142             if (np == null) break;
143             add(np[0], np[1], vals[1].strip());
144         }
145
146         notebook = new Gtk.Notebook();
147         foreach (PolygenPage p in pages)
148         {
149             notebook.append_page(p.scroll, new Gtk.Label(p.name));
150         }
151         result = new PolygenRun();
152         notebook.append_page(result, new Gtk.Label("Result"));
153
154                 pack_start(notebook, true, true, 0);
155         }
156 }
157
158 Polygen polygen;
159
160 public void init()
161 {
162     try {
163         var data = new IOChannel.file(zavai.config.homedir + "/polygen-info", "r");
164         polygen = new Polygen("Polygen", data);
165         data.shutdown(false);
166
167         zavai.registry.register_applet("ui.polygen", polygen);
168         zavai.registry.getmenu("menu.main").add_applet("ui.polygen");
169     } catch (FileError e) {
170         polygen = null;
171         zavai.log.error("Skipping polygen plugin: " + e.message);
172     }
173
174     /*
175         window_list = new WindowList("Current apps");
176         zavai.registry.register_applet("wm.list", window_list);
177         zavai.registry.getmenu("menu.main").add_applet("wm.list");
178
179         try {
180                 launcher = new Launcher("Run program");
181         } catch (Error e) {
182                 zavai.log.error("Not running launcher: " + e.message);
183                 launcher = null;
184         }
185
186         if (launcher != null)
187         {
188                 zavai.registry.register_applet("wm.launcher", launcher);
189                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
190         }
191     */
192 }
193
194 }
195 }
196 }