Update website, description; remove libsoup dependency
[feedhandler] / feedhandler.vala
1 /**
2  * Feed Handler
3  * Copyright © 2009 Thomas Perl <thp@thpinfo.com>
4  **/
5
6 using Gtk;
7 using GLib;
8 using Osso;
9
10 public enum Reader {
11     RSS = 1,
12     GOOGLE,
13     GPODDER,
14 }
15
16 [DBus (name="org.maemo.garage.feedhandler")]
17 public class FeedHandler : GLib.Object {
18     private MainLoop loop;
19     private DBus.Connection conn;
20     private Context context;
21     private string args_url;
22
23     public FeedHandler(MainLoop loop, DBus.Connection conn) {
24         this.loop = loop;
25         this.conn = conn;
26         this.context = new Osso.Context("feedhandler", "2.0", false, null);
27         this.args_url = null;
28     }
29
30     [DBus (name = "mime_open")]
31     public void mime_open(string url) {
32         int result;
33
34         Gtk.Dialog dlg = new Gtk.Dialog();
35         dlg.add_button("RSS Reader", Reader.RSS);
36         dlg.add_button("Google Reader", Reader.GOOGLE);
37         dlg.add_button("gPodder", Reader.GPODDER);
38         dlg.add_button("Cancel", Gtk.ResponseType.CLOSE);
39         dlg.title = "Select application for handling this feed";
40         dlg.vbox.add(new Gtk.Label(url));
41         dlg.show_all();
42         result = dlg.run();
43         dlg.destroy();
44
45         switch (result) {
46             case Reader.RSS:
47                 add_to_rss_reader(url);
48                 break;
49             case Reader.GOOGLE:
50                 add_to_google(url);
51                 break;
52             case Reader.GPODDER:
53                 try {
54                     GLib.Process.spawn_async(null,
55                         {"gpodder",
56                         "--fremantle",
57                         "-s",
58                         url}, null, GLib.SpawnFlags.SEARCH_PATH, null, null);
59                 } catch (GLib.SpawnError e) {
60                     stderr.printf("Can't launch: %s\n", e.message);
61                 }
62                 break;
63         }
64
65         message("URL received: %s", url);
66         loop.quit();
67     }
68
69     private void add_to_google(string url)
70     {
71         open_browser("http://fusion.google.com/add?feedurl=" +
72             Uri.escape_string(url, "", false));
73     }
74
75     private void add_to_rss_reader(string url)
76     {
77         dynamic DBus.Object obj = conn.get_object(
78             "com.nokia.osso_rss_feed_reader_refresh",
79             "/com/nokia/osso_rss_feed_reader_refresh",
80             "com.nokia.osso_rss_feed_reader_refresh");
81         obj.mime_open(url);
82     }
83
84     private void open_browser(string url)
85     {
86         context.rpc_run_with_defaults("osso_browser",
87                                       "open_new_window",
88                                       null,
89                                       (int)'s', url,
90                                       (int)'\0');
91         /* DBUS_TYPE_STRING is (int)'s' */
92         /* DBUS_TYPE_INVALID is (int)'\0' */
93     }
94
95     public void set_args_url(string url)
96     {
97         args_url = url;
98     }
99
100     public bool open_url_later()
101     {
102         mime_open(args_url);
103         return false;
104     }
105 }
106
107 static int main(string [] args) {
108     MainLoop loop = new MainLoop(null, false);
109     Gtk.init(ref args);
110     if (args.length != 1 && args.length != 2) {
111         stderr.printf("Usage: %s [URL]\n", args[0]);
112         return 1;
113     }
114     try {
115         DBus.Connection conn = DBus.Bus.get(DBus.BusType.SESSION);
116         dynamic DBus.Object bus = conn.get_object("org.freedesktop.DBus",
117                                                   "/org/freedesktop/DBus",
118                                                   "org.freedesktop.DBus");
119         uint request_name_result = bus.RequestName(
120                                  "org.maemo.garage.feedhandler", (uint)0);
121         if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
122             FeedHandler server = new FeedHandler(loop, conn);
123             conn.register_object("/org/maemo/garage/feedhandler", server);
124             if (args.length == 2) {
125                 /* Add URL when the main loop is running */
126                 server.set_args_url(args[1]);
127                 Idle.add(server.open_url_later);
128             }
129             loop.run();
130         } else {
131             stderr.printf("feedhandler is already running.\n");
132         }
133     }
134     catch (Error e) {
135         stderr.printf("OOps: %s\n", e.message);
136     }
137     return 0;
138 }
139