Example code for launching a reader app
[feedhandler] / feedhandler.vala
1 /**
2  * Feed Handler
3  * Copyright © 2009 Thomas Perl <thp@thpinfo.com>
4  **/
5
6 using Gtk;
7 using GLib;
8
9 [DBus (name="org.maemo.garage.feedhandler")]
10 public class FeedHandler : GLib.Object {
11     private MainLoop loop;
12
13     public FeedHandler(MainLoop loop) {
14         this.loop = loop;
15     }
16
17     [DBus (name = "mime_open")]
18     public void mime_open(string url) {
19         Gtk.Dialog dlg = new Gtk.Dialog();
20         dlg.add_button("Subscribe", Gtk.ResponseType.YES);
21         dlg.add_button("Cancel", Gtk.ResponseType.CLOSE);
22         dlg.title = "feedhandler received a URL";
23         dlg.vbox.add(new Gtk.Label(url));
24         dlg.show_all();
25         if (dlg.run() == Gtk.ResponseType.YES) {
26             /* Example code for launching a RSS application */
27             try {
28                 GLib.Process.spawn_async(null,
29                     {"gpodder",
30                     "--fremantle",
31                     "-s",
32                     url}, null, GLib.SpawnFlags.SEARCH_PATH, null, null);
33             } catch (GLib.SpawnError e) {
34                 stderr.printf("Can't launch: %s\n", e.message);
35             }
36         }
37         dlg.destroy();
38         message("URL received: %s", url);
39         loop.quit();
40     }
41 }
42
43 static int main(string [] args) {
44     MainLoop loop = new MainLoop(null, false);
45     Gtk.init(ref args);
46     try {
47         var conn = DBus.Bus.get(DBus.BusType.SESSION);
48         dynamic DBus.Object bus = conn.get_object("org.freedesktop.DBus",
49                                                   "/org/freedesktop/DBus",
50                                                   "org.freedesktop.DBus");
51         uint request_name_result = bus.RequestName(
52                                  "org.maemo.garage.feedhandler", (uint)0);
53         if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
54             FeedHandler server = new FeedHandler(loop);
55             conn.register_object("/org/maemo/garage/feedhandler", server);
56             loop.run();
57         }
58
59     }
60     catch (Error e) {
61         stderr.printf("OOps: %s\n", e.message);
62     }
63     return 0;
64 }
65