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