~ubuntu-branches/debian/jessie/betaradio/jessie

« back to all changes in this revision

Viewing changes to src/betaradio.vala

  • Committer: Bazaar Package Importer
  • Author(s): Shih-Yuan Lee (FourDollars)
  • Date: 2010-10-08 12:18:06 UTC
  • Revision ID: james.westby@ubuntu.com-20101008121806-k1sj8jn5elq86v71
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */
 
2
/* vim:set fileencodings=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4: */
 
3
/**
 
4
 * Copyright (C) 2010 Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
using Gtk;
 
21
using Gst;
 
22
 
 
23
class BetaRadio : GLib.Object {
 
24
    private Gtk.StatusIcon icon = null;
 
25
    private Gtk.Menu menu = null;
 
26
    private GstPlayer player = null;
 
27
 
 
28
    public static void main (string[] args) {
 
29
        Intl.bindtextdomain( Config.PACKAGE_NAME, Config.LOCALEDIR );
 
30
        Intl.bind_textdomain_codeset( Config.PACKAGE_NAME, "UTF-8" );
 
31
        Intl.textdomain( Config.PACKAGE_NAME );
 
32
        Gst.init(ref args);
 
33
        Gtk.init(ref args);
 
34
        var app = new BetaRadio();
 
35
        Gtk.main();
 
36
    }
 
37
 
 
38
    public BetaRadio () {
 
39
        if (FileUtils.test(Config.DATADIR + "/pixmaps/betaradio/betaradio.png", FileTest.IS_REGULAR) == true) {
 
40
            icon = new Gtk.StatusIcon.from_file(Config.DATADIR + "/pixmaps/betaradio/betaradio.png");
 
41
        } else if (FileUtils.test("data/betaradio.png", FileTest.IS_REGULAR) == true) {
 
42
            icon = new Gtk.StatusIcon.from_file("data/betaradio.png");
 
43
        } else {
 
44
            icon = new Gtk.StatusIcon.from_stock(Gtk.STOCK_MISSING_IMAGE);
 
45
        }
 
46
        icon.set_tooltip_text(_("BetaRadio Tuner"));
 
47
        menu = new Gtk.Menu();
 
48
        unowned SList<Gtk.RadioMenuItem> group = null;
 
49
 
 
50
        var stop = new Gtk.RadioMenuItem.with_label(group, _("Stop"));
 
51
        group = stop.get_group();
 
52
        menu.append(stop);
 
53
        stop.activate.connect((e) => {
 
54
            if (player != null) {
 
55
                player.stop();
 
56
                player = null;
 
57
                icon.set_tooltip_text(_("BetaRadio Tuner"));
 
58
            }
 
59
        });
 
60
 
 
61
        menu.append(new Gtk.SeparatorMenuItem());
 
62
 
 
63
        group = getMenu(menu, group);
 
64
 
 
65
        menu.append(new Gtk.SeparatorMenuItem());
 
66
 
 
67
        var quit = new Gtk.RadioMenuItem.with_label(group, _("Quit"));
 
68
        group = stop.get_group();
 
69
        menu.append(quit);
 
70
        quit.activate.connect((e) => {
 
71
            if (player != null) {
 
72
                player.stop();
 
73
                player = null;
 
74
                icon.set_tooltip_text(_("BetaRadio Tuner"));
 
75
            }
 
76
            Gtk.main_quit();
 
77
        });
 
78
 
 
79
        menu.show_all();
 
80
 
 
81
        icon.button_release_event.connect((e) => {
 
82
            menu.popup(null, null, null, e.button, e.time);
 
83
            return true;
 
84
        });
 
85
    }
 
86
 
 
87
    private unowned SList<Gtk.RadioMenuItem> getMenu(Gtk.Menu menu, SList<Gtk.RadioMenuItem> group) {
 
88
        var list = new JsonSoup.http("http://betaradio.googlecode.com/svn/trunk/utils/list.json");
 
89
        if (list.is_array() == false) {
 
90
            return group;
 
91
        }
 
92
        int length = list.length();
 
93
        for (int i = 0; i < length; i++) {
 
94
            string feed = list.array(i).get_string();
 
95
            var json = new JsonSoup.http(feed);
 
96
            if (json.object("property").is_string() == false) {
 
97
                continue;
 
98
            }
 
99
            string title = json.sibling("title").get_string();
 
100
            var item = new Gtk.MenuItem.with_label(title);
 
101
            menu.append(item);
 
102
            var submenu = new Gtk.Menu();
 
103
            item.set_submenu(submenu);
 
104
            string property = json.sibling("property").get_string();
 
105
            if (property == "category" && json.sibling("category").is_array() == true) {
 
106
                group = getCategoryMenu(submenu, group, json);
 
107
            } else if (property == "channel" && json.sibling("channel").is_array() == true) {
 
108
                group = getChannelMenu(submenu, group, json);
 
109
            }
 
110
            list.parent();
 
111
        }
 
112
        return group;
 
113
    }
 
114
 
 
115
    private unowned SList<Gtk.RadioMenuItem> getCategoryMenu(Gtk.Menu menu, SList<Gtk.RadioMenuItem> group, JsonSoup json) {
 
116
        int length = json.length();
 
117
        for (int i = 0; i < length; i++) {
 
118
            string category = json.array(i).object("title").get_string();
 
119
            var item = new Gtk.MenuItem.with_label(category);
 
120
            var submenu = new Gtk.Menu();
 
121
            menu.append(item);
 
122
            item.set_submenu(submenu);
 
123
            int size = json.sibling("channel").length();
 
124
            for (int j = 0; j < size; j++) {
 
125
                string title = json.array(j).object("title").get_string();
 
126
                string type = json.sibling("type").get_string();
 
127
                string url = filter_url(json.sibling("url").get_string(), type);
 
128
                var radio = new Gtk.RadioMenuItem.with_label(group, title);
 
129
                group = radio.get_group();
 
130
                radio.toggled.connect( (e) => {
 
131
                    if (player != null) {
 
132
                        player.stop();
 
133
                    }
 
134
                    player = new GstPlayer("BetaRadio", url);
 
135
                    player.play();
 
136
                    icon.set_tooltip_text(title);
 
137
                });
 
138
                submenu.append(radio);
 
139
                json.grandparent();
 
140
            }
 
141
            json.grandparent();
 
142
        }
 
143
        return group;
 
144
    }
 
145
 
 
146
    private unowned SList<Gtk.RadioMenuItem> getChannelMenu(Gtk.Menu menu, SList<Gtk.RadioMenuItem> group, JsonSoup json) {
 
147
        int length = json.length();
 
148
        for (int i = 0; i < length; i++) {
 
149
            string title = json.array(i).object("title").get_string();
 
150
            string type = json.sibling("type").get_string();
 
151
            string url = filter_url(json.sibling("url").get_string(), type);
 
152
            var radio = new Gtk.RadioMenuItem.with_label(group, title);
 
153
            group = radio.get_group();
 
154
            radio.toggled.connect( (e) => {
 
155
                if (player != null) {
 
156
                    player.stop();
 
157
                }
 
158
                player = new GstPlayer("BetaRadio", url);
 
159
                player.play();
 
160
                icon.set_tooltip_text(title);
 
161
            });
 
162
            menu.append(radio);
 
163
            json.grandparent();
 
164
        }
 
165
        return group;
 
166
    }
 
167
 
 
168
    private string filter_url(string url, string type) {
 
169
        /* http://bcr.media.hinet.net/RA000042 */
 
170
        /* mmsh://bcr.media.hinet.net/RA000042\?MSWMExt\=.asf */
 
171
        if (type == "mms" && url.has_prefix("http://") == true) {
 
172
            return url.replace("http", "mmsh").concat("\\?MSWMExt\\=.asf");
 
173
        }
 
174
        return url;
 
175
    }
 
176
}