Add movie object and movie list store
[cinaest] / src / movie-list-store.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
21 public class MovieListStore : ListStore, TreeModel {
22         public enum Columns {
23                 TITLE,
24                 YEAR,
25                 RATING,
26                 POSTER,
27                 MOVIE,
28                 N_COLUMNS
29         }
30         private GLib.Type[] types = {
31                 typeof (string),
32                 typeof (int),
33                 typeof (int),
34                 typeof (Gdk.Pixbuf),
35                 typeof (Movie)
36         };
37         private GLib.Type[] base_type = {
38                 typeof (Movie)
39         };
40         private Gdk.Pixbuf no_poster;
41
42         construct {
43                 set_column_types (base_type);
44                 no_poster = null;
45         }
46
47         public void add (Movie movie, out TreeIter iter) {
48                 TreeIter iter1;
49
50                 append (out iter1);
51                 base.set (iter1, 0, movie);
52
53                 movie.notify.connect ((source, property) => { on_movie_changed(source); });
54
55                 iter = iter1;
56         }
57
58         private void on_movie_changed (GLib.Object source) {
59                 Movie movie = (Movie) source;
60
61                 TreeIter iter;
62                 if (get_iter_from_movie (out iter, movie)) {
63                         TreePath path = get_path (iter);
64                         base.row_changed (path, iter);
65                 }
66         }
67
68         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
69                 if (get_iter_first (out iter)) {
70                         do {
71                                 Movie movie_b;
72                                 get (iter, Columns.MOVIE, out movie_b);
73                                 if (movie_a == movie_b)
74                                         return true;
75                         } while (iter_next (ref iter));
76                 }
77                 return false;
78         }
79
80         // Implement TreeModel interface
81         public virtual GLib.Type get_column_type (int index_) {
82                 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
83
84                 return types[index_];
85         }
86
87         public virtual int get_n_columns () {
88                 return Columns.N_COLUMNS;
89         }
90
91         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
92                 Movie movie;
93
94                 // FIXME
95                 if (no_poster == null) try {
96                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
97                 } catch (Error e) {
98                         critical ("Missing general_video icon: %s\n", e.message);
99                 }
100
101                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
102
103                 // Get the Movie from our parent's storage
104                 Value val;
105                 base.get_value (iter, 0, out val);
106                 movie = (Movie) val.get_object ();
107
108                 value.init (get_column_type (column));
109
110                 switch (column) {
111                 case Columns.TITLE:
112                         if (movie != null) {
113                                 value.set_string (movie.title);
114                         } else {
115                                 value.set_string ("");
116                         }
117                         break;
118
119                 case Columns.YEAR:
120                         if (movie != null) {
121                                 value.set_int (movie.year);
122                         } else {
123                                 value.set_int (-1);
124                         }
125                         break;
126
127                 case Columns.RATING:
128                         if (movie != null) {
129                                 value.set_int (movie.rating);
130                         } else {
131                                 value.set_int (-1);
132                         }
133                         break;
134
135                 case Columns.POSTER:
136                         if ((movie.poster != null) && (movie.poster.thumbnail != null))
137                                 value.set_object (movie.poster.thumbnail);
138                         else
139                                 value.set_object (no_poster);
140                         break;
141
142                 case Columns.MOVIE:
143                         value.set_object (movie);
144                         break;
145
146                 default:
147                         assert_not_reached ();
148                 }
149         }
150 }