3 public Gee.HashMap <string, MovieSearch> searches;
4 public DBus.Connection conn;
6 [DBus (name = "org.maemo.cinaest.MovieSearch", signals="movies_found")]
7 public class MovieSearch : Object {
12 GLib.List<GoogleMovie> results;
13 GoogleMovieService service;
20 public void abort () {
25 public void start (string query) {
26 parser = new GoogleParser ();
32 public signal void movies_found (string[] movies, bool finished);
36 internal MovieSearch (GoogleMovieService _service, string _sender, string _path) {
42 private async void query_async () {
43 var gc = GConf.Client.get_default ();
45 results = new GLib.List<GoogleMovie> ();
47 string location = null;
49 location = gc.get_string ("/apps/cinaest/location");
51 stdout.printf ("Error getting GConf option: %s\n", e.message);
56 int n = yield parser.query (title, location, receive_movie);
59 var locations = ((SList<string>) gc.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING)).copy ();
60 if (insert_location_sorted (parser.location, ref locations)) {
61 gc.set_list ("/apps/cinaest/locations", GConf.ValueType.STRING, locations);
63 if (location != parser.location) {
64 location = parser.location;
65 gc.set_string ("/apps/cinaest/location", location);
68 stdout.printf ("GConf error: %s\n", e2.message);
71 print ("got %d movies\n", n);
73 var m = new string[results.length ()];
75 for (unowned GLib.List<GoogleMovie> node = results.first (); node != null; node = node.next) {
76 m[i++] = "{\"title\":\"%s\",\"rating\":%f,\"runtime\":%d,\"showtimes\":\"%s\",\"cinema_name\":\"%s\",\"cinema_phone\":\"%s\"}".printf (node.data.title, node.data.rating, node.data.runtime, node.data.showtimes, node.data.theater.name, node.data.theater.phone);
78 movies_found (m, true);
79 service.timeout_quit ();
82 private bool insert_location_sorted (string? location, ref SList<string> locations) {
85 if (locations != null) {
86 for (unowned SList<string> l = locations; l != null; l = l.next) {
87 if (l.data == location) {
90 if (l.data > location) {
91 l.insert (location, 0);
96 locations.append (location);
100 private void receive_movie (GoogleMovie movie) {
101 // print ("received %s\n", movie.title);
102 results.append (movie);
106 [DBus (name = "org.maemo.cinaest.MovieService")]
107 public class GoogleMovieService : Object {
108 private MainLoop loop;
109 private uint source_id;
114 public string new_search (DBus.BusName sender) {
115 var path = "/org/maemo/cinaest/moviepilot/search%d".printf (id);
116 var search = new MovieSearch (this, sender, path);
118 conn.register_object (path, search);
120 print ("creating new search %s for %s\n", path, sender);
121 searches.set (path, search);
126 public void unregister (string path) {
127 print ("unregistering search %s\n", path);
129 searches.remove (path);
130 print ("%d\n", searches.size);
135 internal GoogleMovieService () {
136 loop = new MainLoop (null);
139 internal void timeout_quit () {
140 // With every change we reset the timer to 3min
141 if (source_id != 0) {
142 Source.remove (source_id);
144 source_id = Timeout.add_seconds (180, quit);
147 private bool quit () {
154 internal void run () {
159 void on_client_lost (DBus.Object sender, string name, string prev, string newp) {
161 var remove_list = new SList<string> ();
163 print ("lost a client: %s\n", prev);
164 foreach (MovieSearch search in searches.values) {
165 if (search.sender == prev) {
166 print ("removing %s\n", search.path);
167 remove_list.append (search.path);
170 foreach (string path in remove_list)
171 searches.remove (path);
177 conn = DBus.Bus.get (DBus.BusType. SESSION);
179 searches = new Gee.HashMap <string, MovieSearch> ();
181 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
182 "/org/freedesktop/DBus",
183 "org.freedesktop.DBus");
185 uint res = bus.request_name ("org.maemo.cinaest.GoogleShowtimes", (uint) 0);
187 if (res == DBus.RequestNameReply.PRIMARY_OWNER) {
188 var server = new GoogleMovieService ();
190 conn.register_object ("/org/maemo/cinaest/googleshowtimes", server);
192 bus.NameOwnerChanged.connect (on_client_lost);
194 server.timeout_quit ();
198 stderr.printf ("Oops: %s\n", e.message);