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;
17 public void abort () {
22 public void start (string query) {
23 parser = new GoogleParser ();
29 public signal void movies_found (string[] movies, bool finished);
33 internal MovieSearch (GoogleMovieService _service) {
37 private async void query_async () {
38 var gc = GConf.Client.get_default ();
40 results = new GLib.List<GoogleMovie> ();
42 string location = null;
44 location = gc.get_string ("/apps/cinaest/location");
46 stdout.printf ("Error getting GConf option: %s\n", e.message);
51 int n = yield parser.query (title, location, receive_movie);
54 var locations = ((SList<string>) gc.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING)).copy ();
55 if (insert_location_sorted (parser.location, ref locations)) {
56 gc.set_list ("/apps/cinaest/locations", GConf.ValueType.STRING, locations);
58 if (location != parser.location) {
59 location = parser.location;
60 gc.set_string ("/apps/cinaest/location", location);
63 stdout.printf ("GConf error: %s\n", e2.message);
66 print ("got %d movies\n", n);
68 var m = new string[results.length ()];
70 for (unowned GLib.List<GoogleMovie> node = results.first (); node != null; node = node.next) {
71 m[i++] = "{\"title\":\"%s\",\"rating\":%f,\"showtimes\":\"%s\",\"cinema_name\":\"%s\",\"cinema_phone\":\"%s\"}".printf (node.data.title, node.data.rating, node.data.showtimes, node.data.cinema.name, node.data.cinema.phone);
73 movies_found (m, true);
74 service.timeout_quit ();
77 private bool insert_location_sorted (string? location, ref SList<string> locations) {
80 if (locations != null) {
81 for (unowned SList<string> l = locations; l != null; l = l.next) {
82 if (l.data == location) {
85 if (l.data > location) {
86 l.insert (location, 0);
91 locations.append (location);
95 private void receive_movie (GoogleMovie movie) {
96 // print ("received %s\n", movie.title);
97 results.append (movie);
101 [DBus (name = "org.maemo.cinaest.MovieService")]
102 public class GoogleMovieService : Object {
103 private MainLoop loop;
104 private uint source_id;
108 public string new_search (DBus.BusName sender) {
109 print ("new search requested by %s\n", sender);
110 var search = new MovieSearch (this);
112 search.id = searches.size;
113 string path = "/org/maemo/cinaest/googleshowtimes/search%d".printf (search.id);
114 conn.register_object (path, search);
116 searches.set (path, search);
121 public void unregister (string path) {
123 print ("unregistering %s\n", path);
124 searches.remove (path);
125 print ("%d\n", searches.size);
130 internal GoogleMovieService () {
131 loop = new MainLoop (null);
134 internal void timeout_quit () {
135 // With every change we reset the timer to 3min
136 if (source_id != 0) {
137 Source.remove (source_id);
139 source_id = Timeout.add_seconds (180, quit);
142 private bool quit () {
149 internal void run () {
154 void on_client_lost (DBus.Object sender, string name, string prev, string newp) {
157 print ("lost a client: prev:%s\n", prev);
158 searches.remove (prev);
163 var loop = new MainLoop (null, false);
166 conn = DBus.Bus.get (DBus.BusType. SESSION);
168 searches = new Gee.HashMap <string, MovieSearch> ();
170 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
171 "/org/freedesktop/DBus",
172 "org.freedesktop.DBus");
174 uint res = bus.request_name ("org.maemo.cinaest.GoogleShowtimes", (uint) 0);
176 if (res == DBus.RequestNameReply.PRIMARY_OWNER) {
177 var server = new GoogleMovieService ();
179 conn.register_object ("/org/maemo/cinaest/movies", server);
181 bus.NameOwnerChanged.connect (on_client_lost);
183 server.timeout_quit ();
187 stderr.printf ("Oops: %s\n", e.message);