IMDb & MoviePilot plugins: fix settings dialog layout
[cinaest] / src / plugins / moviepilot-plugin.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 class MoviePilotPlugin : Plugin {
23         List<MovieSource> sources;
24
25         public override void hello (Gtk.Window window, Osso.Context context) {
26                 stdout.printf ("MoviePilot Plugin Loaded.\n");
27
28                 var source = new MoviePilotSource ();
29
30                 sources = new List<MovieSource> ();
31                 sources.append (source);
32
33                 // FIXME - this forces the inclusion of config.h
34                 (void) Config.GETTEXT_PACKAGE;
35         }
36
37         public override unowned List<MovieSource> get_sources () {
38                 return sources;
39         }
40
41         public override List<MovieAction> get_actions (Movie _movie, Gtk.Window window) {
42                 return new List<MovieAction> ();
43         }
44
45         public override void settings_dialog (Gtk.Window window) {
46                 MoviePilotSource source = (MoviePilotSource) sources.data;
47                 var dialog = new Gtk.Dialog ();
48                 dialog.set_transient_for (window);
49                 dialog.set_title (_("MoviePilot plugin settings"));
50
51                 var content = (VBox) dialog.get_content_area ();
52                 var sizegroup = new Gtk.SizeGroup (SizeGroupMode.HORIZONTAL);
53
54                 // User name
55                 var hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
56                 var label = new Gtk.Label (_("User name"));
57                 label.set_alignment (0, 0.5f);
58                 sizegroup.add_widget (label);
59                 var entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
60                 hbox.pack_start (label, false, false, 0);
61                 hbox.pack_start (entry, true, true, 0);
62                 content.pack_start (hbox, true, true, 0);
63
64                 // Password
65                 hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
66                 label = new Gtk.Label ("Password");
67                 label.set_alignment (0, 0.5f);
68                 sizegroup.add_widget (label);
69                 entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
70                 hbox.pack_start (label, false, false, 0);
71                 hbox.pack_start (entry, true, true, 0);
72                 content.pack_start (hbox, true, true, 0);
73
74                 dialog.add_button (_("Save"), ResponseType.ACCEPT);
75
76                 dialog.show_all ();
77                 int res = dialog.run ();
78                 if (res == ResponseType.ACCEPT) {
79                         /* ... */
80                 }
81                 dialog.destroy ();
82         }
83
84         public override unowned string get_name () {
85                 return "MoviePilot";
86         }
87 }
88
89 class MoviePilotSource : MovieSource {
90         public string location;
91         public string description;
92         public MovieSource.ReceiveMovieFunction callback;
93         dynamic DBus.Object search;
94
95         public override bool active { get; set construct; }
96
97         public MoviePilotSource () {
98                 GLib.Object (active: true);
99         }
100
101         SourceFunc get_movies_callback;
102         public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction _callback, int limit, Cancellable? cancellable) {
103                 try {
104                         string search_path;
105                         dynamic DBus.Object server;
106                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
107
108                         server = conn.get_object ("org.maemo.cinaest.MoviePilot",
109                                                   "/org/maemo/cinaest/moviepilot",
110                                                   "org.maemo.cinaest.MovieService");
111                         server.NewSearch (out search_path);
112
113                         search = conn.get_object ("org.maemo.cinaest.MoviePilot",
114                                                   search_path,
115                                                   "org.maemo.cinaest.MovieSearch");
116
117                         callback = _callback;
118                         search.MoviesFound.connect (on_movies_found);
119                         print ("get_movies (%s)\n", filter.title);
120                         search.start (filter.title);
121                 } catch (Error e1) {
122                         Banner.show_information (null, null, e1.message);
123                         return 0;
124                 }
125
126                 get_movies_callback = get_movies.callback;
127                 if (cancellable != null)
128                         cancellable.cancelled.connect (() => { search.abort (); Idle.add (get_movies_callback); });
129                 yield;
130
131                 return 1 /* FIXME: n */;
132         }
133
134         private void on_movies_found (DBus.Object sender, string[] movies, bool finished) {
135                 print ("found %d movies\n", movies.length);
136                 var parser = new Json.Parser ();
137                 var result = new SList<Movie> ();
138
139                 for (int i = 0; i < movies.length; i++) {
140                         var movie = new Movie ();
141                         try {
142                                 parser.load_from_data (movies[i], -1);
143                         } catch (Error e) {
144                                 stderr.printf ("Error: %s\n%s\n", e.message, movies[i]);
145                         }
146
147                         var object = parser.get_root ().get_object ();
148                         movie.title = object.get_string_member ("title");
149                         movie.year = (int) object.get_int_member ("year");
150                         movie.rating = (int) object.get_double_member ("rating");
151                         movie.secondary = object.get_string_member ("genres").replace (",", ", ");
152
153                         result.append (movie);
154                 }
155
156                 callback (result);
157
158                 if (finished) {
159                         search = null;
160                         Idle.add (get_movies_callback);
161                 }
162         }
163
164         public override void add_movie (Movie movie) {
165         }
166
167         public override void delete_movie (Movie movie) {
168         }
169
170         public override unowned string get_name () {
171                 return _("MoviePilot");
172         }
173
174         public override unowned string get_description () {
175                 description =  _("Movies on MoviePilot");
176                 return description;
177         }
178
179         public override SourceFlags get_flags () {
180                 return SourceFlags.ONLINE | SourceFlags.NOEMPTY;
181         }
182 }
183
184 [ModuleInit]
185 public Type register_plugin (TypeModule module) {
186         // types are registered automatically
187         return typeof (MoviePilotPlugin);
188 }