Add movie edit dialog
[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 (_("Edit movie"));
54                         button.clicked.connect (on_edit_movie);
55                         append (button);
56                         button = new Gtk.Button.with_label (_("Delete movie"));
57                         button.clicked.connect (on_delete_movie);
58                         append (button);
59                 }
60
61                 show_all ();
62         }
63
64         private void on_rate_movie () {
65                 var dialog = new Gtk.Dialog ();
66                 dialog.set_title (_("Rate movie"));
67
68                 var rating = new RatingWidget ();
69                 var date = new Hildon.DateButton (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL);
70                 date.set_title (_("Watched on"));
71                 date.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
72
73                 var content = (Gtk.VBox) dialog.get_content_area ();
74                 content.pack_start (rating, true, false, 0);
75                 content.pack_start (date, true, false, 0);
76
77                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
78                 dialog.show_all ();
79                 var res = dialog.run ();
80                 dialog.destroy ();
81                 if (res == Gtk.ResponseType.OK) {
82                         movie.julian_date = get_julian_date (date);
83                         movie.rating = rating.rating;
84                         Banner.show_information (parent_window, null, _("Rated movie '%s': %.1f").printf (movie.title, movie.rating/10.0));
85                         foreach (Plugin plugin in CinaestProgram.plugins) {
86                                 foreach (MovieSource source in plugin.get_sources ()) {
87                                         if (source.active && SourceFlags.RATING in source.get_flags ())
88                                                 source.add_movie (movie);
89                                 }
90                         }
91                 }
92         }
93
94         private uint get_julian_date (DateButton button) {
95                 var date = Date ();
96                 uint year;
97                 uint month;
98                 uint day;
99                 button.get_date (out year, out month, out day);
100                 date.set_dmy ((DateDay) day, (DateMonth) month + DateMonth.JANUARY, (DateYear) year);
101                 return date.get_julian ();
102         }
103
104         private void on_delete_movie () {
105                 var dialog = new Note.confirmation (parent_window, _("Delete movie '%s'?").printf (movie.title));
106                 var res = dialog.run ();
107
108                 dialog.destroy ();
109                 if (res == Gtk.ResponseType.OK) {
110                         store.remove (movie);
111                         movie_deleted ();
112                 }
113         }
114
115         private void on_edit_movie () {
116                 var dialog = new MovieEditDialog (parent_window, movie);
117                 var res = dialog.run ();
118         }
119 }
120