Czech translation update (via transifex.net)
[cinaest] / src / source-list-view.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
5  * Cinaest 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 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest 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
16  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 public class SourceListView : PannableArea {
23         TreeView tree;
24         ListStore store;
25         TreeModelFilter filtered_store;
26         bool filtered;
27
28         public signal void source_activated (MovieSource source);
29
30         public SourceListView (List<MovieSource> list, bool _filtered, Gtk.Window window) {
31                 filtered = _filtered;
32
33                 // Source list
34                 store = new ListStore (3, typeof (string), typeof (string), typeof (MovieSource));
35                 add_sources (list);
36
37                 if (filtered) {
38                         // Add filter wrapper
39                         filtered_store = new TreeModelFilter (store, null);
40                         filtered_store.set_visible_func (filter_visible_func);
41
42                         // Tree View
43                         tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, filtered_store);
44                 } else {
45                         tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
46                 }
47                 tree.set_headers_visible (false);
48
49                 add (tree);
50
51                 tree.set_rules_hint (true);
52
53                 // Tree selection object
54                 var selection = tree.get_selection ();
55                 selection.set_mode (SelectionMode.SINGLE);
56
57                 // Source column
58                 var title_column = new TreeViewColumn ();
59                 title_column.set_title (_("Source"));
60                 title_column.set_reorderable (false);
61                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
62                 title_column.set_expand (true);
63
64                 // Add name
65                 var vbox_renderer = new CellRendererVBox ();
66
67                 var renderer = new CellRendererText ();
68                 renderer.yalign = 1.0f;
69                 vbox_renderer.append (renderer, true);
70                 vbox_renderer.set_data ("name", renderer);
71
72                 // Add description
73                 renderer = new CellRendererText ();
74                 renderer.yalign = 0;
75                 renderer.ellipsize = Pango.EllipsizeMode.END;
76
77                 Pango.AttrList attr_list = new Pango.AttrList ();
78                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), "SmallSystemFont", null, typeof (void));
79                 if (style != null) {
80                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
81                         attr_list.insert ((owned) attr_font_desc);
82                 }
83                 Gdk.Color color;
84                 window.ensure_style ();
85                 if (window.style.lookup_color ("SecondaryTextColor", out color)) {
86                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
87                         attr_list.insert ((owned) attr_color);
88                 }
89                 renderer.attributes = attr_list;
90
91                 vbox_renderer.append (renderer, true);
92                 vbox_renderer.set_data ("description", renderer);
93
94                 title_column.pack_start (vbox_renderer, true);
95                 title_column.set_cell_data_func (vbox_renderer, vbox_data_func);
96
97                 tree.append_column (title_column);
98
99                 // Connect signals
100                 tree.row_activated.connect (on_row_activated);
101         }
102
103         bool filter_visible_func (TreeModel model, TreeIter iter) {
104                 MovieSource source = null;
105
106                 store.get (iter, 2, out source);
107                 
108                 return source.active;
109         }
110
111         public void add_sources (List<MovieSource> list) {
112                 foreach (MovieSource source in list) {
113                         TreeIter iter;
114
115                         store.insert_with_values (out iter, -1, 0, source.get_name (), 1, source.get_description (), 2, source);
116
117                         source.notify["active"].connect (this.on_source_changed);
118                 }
119         }
120
121         private void on_source_changed (GLib.Object _source, GLib.ParamSpec spec) {
122                 var source = (MovieSource) _source;
123                 TreeIter iter;
124
125                 if (get_iter (source, out iter)) {
126                         TreePath path = store.get_path (iter);
127                         store.row_changed (path, iter);
128                 }
129         }
130
131         public bool get_iter (MovieSource source, out Gtk.TreeIter iter) {
132                 if (store.get_iter_first (out iter)) do {
133                         MovieSource s = null;
134                         store.get (iter, 2, out s);
135                         if (s == source) {
136                                 return true;
137                         }
138                 } while (store.iter_next (ref iter));
139
140                 return false;
141         }
142
143         public unowned TreeSelection get_selection () {
144                 return tree.get_selection ();
145         }
146
147         public void set_hildon_ui_mode (UIMode mode) {
148                 var selection = tree.get_selection ();
149
150                 if (mode == UIMode.NORMAL) {
151                         selection.set_mode (SelectionMode.NONE);
152                 }
153                 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
154                 if (mode == UIMode.EDIT) {
155                         selection.set_mode (SelectionMode.MULTIPLE);
156                 }
157         }
158
159         public int length () {
160                 TreeIter iter;
161                 int len = 0;
162
163                 if (store.get_iter_first (out iter)) do {
164                         len++;
165                 } while (store.iter_next (ref iter));
166
167                 return len;
168         }
169
170         private void on_row_activated (TreeView tree, TreePath path_, TreeViewColumn column) {
171                 TreePath path = path_.copy (); // FIXME - calling model.get_iter with path_ directly causes a C qualifier warning
172                 TreeModel model = tree.model;
173                 TreeIter iter;
174
175                 if (model.get_iter (out iter, path)) {
176                         MovieSource source;
177                         model.get (iter, 2, out source);
178                         source_activated (source);
179                 }
180         }
181
182         private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
183                 CellRendererText renderer;
184                 string name;
185                 string description;
186
187                 model.get (iter, 0, out name, 1, out description);
188
189                 renderer = cell.get_data ("name");
190                 renderer.text = name;
191
192                 renderer = cell.get_data ("description");
193                 renderer.text = description;
194         }
195 }