Movie window: change thumbnail image scaling
[cinaest] / src / movie-window.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 MovieWindow : StackableWindow {
23         private MovieMenu menu;
24         private Gdk.Pixbuf no_poster;
25         private MoviePoster.Factory poster_factory;
26         private HBox landscape;
27         private VBox portrait;
28         private Image image;
29         private Label label;
30         private bool portrait_mode;
31
32         public MovieWindow.with_movie (Movie movie, MovieListStore store) {
33                 set_title (movie.title);
34
35                 // View menu
36                 menu = new MovieMenu (movie, store, this);
37
38                 set_main_menu (menu);
39
40                 // Poster
41                 image = new Image ();
42
43                 if (movie.poster != null && movie.poster.pixbuf != null) {
44                         image.pixbuf = movie.poster.pixbuf;
45                 } else {
46                         movie.notify.connect (this.on_movie_changed);
47                         if (movie.poster != null && movie.poster.thumbnail != null) {
48                                 // FIXME
49                                 image.pixbuf = movie.poster.thumbnail.scale_simple (268, 424, Gdk.InterpType.BILINEAR);
50                         } else {
51                                 // FIXME
52                                 if (no_poster == null) try {
53                                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/124x124/hildon/general_video.png");
54                                 } catch (Error e) {
55                                         critical ("Missing general_video icon: %s\n", e.message);
56                                 }
57                                 image.pixbuf = no_poster;
58                         }
59                         try {
60                                 poster_factory = MoviePoster.Factory.get_instance ();
61                                 poster_factory.queue (movie, receive_poster);
62                         } catch (Error e) {
63                                 warning ("Failed to queue poster request: %s\n", e.message);
64                         }
65                 }
66
67                 // Text area
68                 string genres = movie.genres.to_string ();
69                 string year = movie.year > 0 ? " (%d)".printf (movie.year) : "";
70                 string text = "<b>%s</b>%s".printf (genres, year);
71
72                 label = new Label (text);
73                 label.wrap = true;
74                 label.use_markup = true;
75                 label.set_alignment (0.0f, 0.0f);
76
77                 landscape = new HBox (false, 0);
78                 portrait = new VBox (false, 0);
79
80                 var vbox = new VBox (false, 0);
81                 vbox.pack_start (landscape, true, true, MARGIN_DOUBLE);
82                 vbox.pack_start (portrait, true, true, MARGIN_DOUBLE);
83
84                 portrait_mode = CinaestProgram.orientation.portrait;
85                 if (portrait_mode) {
86                         portrait.pack_start (image, false, false, 0);
87                         portrait.pack_start (label, true, true, MARGIN_DOUBLE);
88                 } else {
89                         landscape.pack_start (image, false, true, 0);
90                         landscape.pack_start (label, true, true, MARGIN_DOUBLE);
91                 }
92
93                 vbox.show_all ();
94                 add (vbox);
95
96                 // Connect signals
97                 menu.movie_deleted.connect (() => { destroy (); });
98                 Gdk.Screen.get_default ().size_changed.connect (on_orientation_changed);
99         }
100
101         private void receive_poster (Gdk.Pixbuf pixbuf, Movie movie) {
102                 var poster = new Poster();
103
104                 poster.pixbuf = pixbuf;
105                 if (movie.poster != null)
106                         poster.thumbnail = movie.poster.thumbnail;
107                 movie.poster = poster;
108         }
109
110         private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
111                 var movie = (Movie) source;
112
113                 if ((spec.name == "poster") && (movie.poster != null) && (movie.poster.pixbuf != null)) {
114                         image.pixbuf = movie.poster.pixbuf;
115                 }
116         }
117
118         private void on_orientation_changed (Gdk.Screen screen) {
119                 if (CinaestProgram.orientation.portrait == portrait_mode)
120                         return;
121
122                 portrait_mode = CinaestProgram.orientation.portrait;
123                 if (portrait_mode) {
124                         landscape.remove (label);
125                         landscape.remove (image);
126                         portrait.pack_start (image, false, false, 0);
127                         portrait.pack_start (label, true, true, MARGIN_DOUBLE);
128                 } else {
129                         portrait.remove (label);
130                         portrait.remove (image);
131                         landscape.pack_start (image, false, true, 0);
132                         landscape.pack_start (label, true, true, MARGIN_DOUBLE);
133                 }
134         }
135 }
136