a3e2544d9ef21f9f506a57cfcf090aa5c41c36ed
[cinaest] / src / movie-menu.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 Hildon;
20 using Osso;
21
22 public class MovieMenu : AppMenu {
23         private Movie movie;
24         private MovieListStore store;
25         private Gtk.Window parent_window;
26         private List<MovieAction> actions;
27
28         public signal void movie_deleted ();
29
30         public MovieMenu (Movie _movie, MovieListStore _store, Gtk.Window window) {
31                 bool can_rate = false;
32                 movie = _movie;
33                 store = _store;
34                 parent_window = window;
35                 foreach (Plugin plugin in CinaestProgram.plugins) {
36                         foreach (MovieAction action in plugin.get_actions (movie, window)) {
37                                 var button = new Gtk.Button.with_label (action.name);
38                                 button.clicked.connect (action.execute);
39                                 append (button);
40                                 actions.append (action);
41                         }
42                         foreach (MovieSource source in plugin.get_sources ()) {
43                                 if (source.active && SourceFlags.RATING in source.get_flags ())
44                                         can_rate = true;
45                         }
46                 }
47                 if (can_rate) {
48                         var button = new Gtk.Button.with_label (_("Rate movie"));
49                         button.clicked.connect (on_rate_movie);
50                         append (button);
51                 }
52                 if (store.get_editable ()) {
53                         var button = new Gtk.Button.with_label (_("Delete movie"));
54                         button.clicked.connect (on_delete_movie);
55                         append (button);
56                 }
57
58                 show_all ();
59         }
60
61         private void on_rate_movie () {
62                 var dialog = new Gtk.Dialog ();
63                 dialog.set_title (_("Rate movie"));
64
65                 var rating = new RatingWidget ();
66                 var date = new Hildon.DateButton (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
67                 date.set_title (_("Watched on"));
68                 date.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
69
70                 var content = (Gtk.VBox) dialog.get_content_area ();
71                 content.pack_start (rating, true, false, 0);
72                 content.pack_start (date, true, false, 0);
73
74                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
75                 dialog.show_all ();
76                 var res = dialog.run ();
77                 dialog.destroy ();
78                 if (res == Gtk.ResponseType.OK) {
79                         movie.julian_date = get_julian_date (date);
80                         movie.rating = 10 * rating.get_rating ();
81                         Banner.show_information (parent_window, null, _("Rated movie '%s': %.1f").printf (movie.title, movie.rating/10.0));
82                         foreach (Plugin plugin in CinaestProgram.plugins) {
83                                 foreach (MovieSource source in plugin.get_sources ()) {
84                                         if (source.active && SourceFlags.RATING in source.get_flags ())
85                                                 source.add_movie (movie);
86                                 }
87                         }
88                 }
89         }
90
91         private uint get_julian_date (DateButton button) {
92                 var date = Date ();
93                 uint year;
94                 uint month;
95                 uint day;
96                 button.get_date (out year, out month, out day);
97                 date.set_dmy ((DateDay) day, (DateMonth) month + DateMonth.JANUARY, (DateYear) year);
98                 return date.get_julian ();
99         }
100
101         private void on_delete_movie () {
102                 var dialog = new Note.confirmation (parent_window, _("Delete movie '%s'?").printf (movie.title));
103                 var res = dialog.run ();
104
105                 dialog.destroy ();
106                 if (res == Gtk.ResponseType.OK) {
107                         store.remove (movie);
108                         movie_deleted ();
109                 }
110         }
111 }
112