Add NOEMPTY plugin flag to MoviePilot plugin - don't start empty searches when set
[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                 sizegroup.add_widget (label);
58                 var entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
59                 hbox.pack_start (label, false, false, 0);
60                 hbox.pack_start (entry, true, true, 0);
61                 content.pack_start (hbox, true, true, 0);
62
63                 // Password
64                 hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
65                 label = new Gtk.Label ("Password");
66                 sizegroup.add_widget (label);
67                 entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
68                 hbox.pack_start (label, false, false, 0);
69                 hbox.pack_start (entry, true, true, 0);
70                 content.pack_start (hbox, true, true, 0);
71
72                 dialog.add_button (_("Save"), ResponseType.ACCEPT);
73
74                 dialog.show_all ();
75                 int res = dialog.run ();
76                 if (res == ResponseType.ACCEPT) {
77                         /* ... */
78                 }
79                 dialog.destroy ();
80         }
81
82         public override unowned string get_name () {
83                 return "MoviePilot";
84         }
85 }
86
87 class MoviePilotSource : MovieSource {
88         public string location;
89         public string description;
90         public MovieSource.ReceiveMovieFunction callback;
91         dynamic DBus.Object search;
92
93         public override bool active { get; set construct; }
94
95         public MoviePilotSource () {
96                 GLib.Object (active: true);
97         }
98
99         SourceFunc get_movies_callback;
100         public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction _callback, int limit, Cancellable? cancellable) {
101                 try {
102                         string search_path;
103                         dynamic DBus.Object server;
104                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
105
106                         server = conn.get_object ("org.maemo.cinaest.MoviePilot",
107                                                   "/org/maemo/cinaest/moviepilot",
108                                                   "org.maemo.cinaest.MovieService");
109                         server.NewSearch (out search_path);
110
111                         search = conn.get_object ("org.maemo.cinaest.MoviePilot",
112                                                   search_path,
113                                                   "org.maemo.cinaest.MovieSearch");
114
115                         callback = _callback;
116                         search.MoviesFound.connect (on_movies_found);
117                         print ("get_movies (%s)\n", filter.title);
118                         search.start (filter.title);
119                 } catch (Error e1) {
120                         Banner.show_information (null, null, e1.message);
121                         return 0;
122                 }
123
124                 get_movies_callback = get_movies.callback;
125                 if (cancellable != null)
126                         cancellable.cancelled.connect (() => { search.abort (); Idle.add (get_movies_callback); });
127                 yield;
128
129                 return 1 /* FIXME: n */;
130         }
131
132         private void on_movies_found (DBus.Object sender, string[] movies, bool finished) {
133                 print ("found %d movies\n", movies.length);
134                 var parser = new Json.Parser ();
135                 var result = new SList<Movie> ();
136
137                 for (int i = 0; i < movies.length; i++) {
138                         var movie = new Movie ();
139                         try {
140                                 parser.load_from_data (movies[i], -1);
141                         } catch (Error e) {
142                                 stderr.printf ("Error: %s\n%s\n", e.message, movies[i]);
143                         }
144
145                         var object = parser.get_root ().get_object ();
146                         movie.title = object.get_string_member ("title");
147                         movie.year = (int) object.get_int_member ("year");
148                         movie.rating = (int) object.get_double_member ("rating");
149                         movie.secondary = object.get_string_member ("genres").replace (",", ", ");
150
151                         result.append (movie);
152                 }
153
154                 callback (result);
155
156                 if (finished) {
157                         search = null;
158                         Idle.add (get_movies_callback);
159                 }
160         }
161
162         public override void add_movie (Movie movie) {
163         }
164
165         public override void delete_movie (Movie movie) {
166         }
167
168         public override unowned string get_name () {
169                 return _("MoviePilot");
170         }
171
172         public override unowned string get_description () {
173                 description =  _("Movies on MoviePilot");
174                 return description;
175         }
176
177         public override SourceFlags get_flags () {
178                 return SourceFlags.ONLINE | SourceFlags.NOEMPTY;
179         }
180 }
181
182 [ModuleInit]
183 public Type register_plugin (TypeModule module) {
184         // types are registered automatically
185         return typeof (MoviePilotPlugin);
186 }