Google poster downloader: quit the D-Bus server after 3 min of inactivity
authorPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 8 Jan 2010 16:11:52 +0000 (17:11 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 8 Jan 2010 19:41:02 +0000 (20:41 +0100)
It is good practice to play nice with system resources. It will be started
again via D-Bus activation, when needed.

src/poster/google-poster-downloader.vala

index 129fb98..423d017 100644 (file)
@@ -133,6 +133,7 @@ public class GooglePosterDownload : Object {
                        print ("Stored as: %s\n", cache_path);
 
                        downloader.fetched (handle, cache_path);
+                       downloader.timeout_quit ();
                } catch (Error e) {
                        stdout.printf ("Failed to store poster: %s\n", e.message);
                }
@@ -149,6 +150,7 @@ public class GooglePosterDownloader : Object, PosterDownloader {
        public SessionAsync session;
        private int fetch_handle = 1;
        private List<GooglePosterDownload> downloads = null;
+       private uint source_id;
 
        public GooglePosterDownloader () {
                loop = new MainLoop (null);
@@ -157,6 +159,21 @@ public class GooglePosterDownloader : Object, PosterDownloader {
                session.max_conns_per_host = 7;
        }
 
+       public void timeout_quit () {
+               // With every change we reset the timer to 3min
+               if (source_id != 0) {
+                       Source.remove (source_id);
+               }
+               source_id = Timeout.add_seconds (180, quit);
+       }
+
+        private bool quit () {
+               loop.quit ();
+
+                // One-shot only
+                return false;
+        }
+
        public void run () {
                loop.run ();
        }
@@ -206,6 +223,7 @@ public class GooglePosterDownloader : Object, PosterDownloader {
                                var server = new GooglePosterDownloader ();
                                conn.register_object ("/org/maemo/movieposter/GoogleImages", server);
 
+                               server.timeout_quit ();
                                server.run ();
                        }
                } catch (Error e) {