This repository has been archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.js
152 lines (122 loc) · 4.34 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (C) 2014 Ivo Nunes <ivoavnunes@gmail.com>
*
* This software is licensed under the GNU General Public License
* (version 3 or later). See the COPYING file in this distribution.
*
* You should have received a copy of the GNU Library General Public
* License along with this software; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
const Lang = imports.lang;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const GLib = imports.gi.GLib;
const Gst = imports.gi.Gst;
const Gettext = imports.gettext;
const _ = Gettext.gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const RadioSubMenu = new Lang.Class({
Name: 'RadioSubMenu',
Extends: PopupMenu.PopupSubMenuMenuItem,
_init: function() {
this.parent(_("Radio"), true);
// get settings
this.settings = Utils.getSettings();
// initialize the player
this._playing = false;
this.player = Gst.ElementFactory.make("playbin", "player");
this.bus = this.player.get_bus();
this.bus.add_signal_watch();
// connect the dbus events
this.bus.connect("message::error", Lang.bind(this, function(bus, message) {
this._killStream();
return true;
}));
this.bus.connect("message::eos", Lang.bind(this, function(bus, message) {
this._killStream();
}));
this.actor.show();
this.menu.connect('open-state-changed', Lang.bind(this, function(menu, isOpen) {
if (isOpen)
this._updateStreamList();
}));
let item = new PopupMenu.PopupMenuItem(_("Radio"));
this.menu.addMenuItem(item);
},
_killStream: function () {
// set current stream to none
this._playing = false;
this.label.set_text(_("Radio"));
this.icon.icon_name = "";
this.player.set_state(Gst.State.NULL);
},
_updateStreamList: function () {
var streams_settings = this.settings.get_string('streams');
if (streams_settings.length > 0) var streams = JSON.parse(streams_settings);
else var streams = [["Radio Paradise","http://stream-dc1.radioparadise.com/rp_192m.ogg"]];
// clean the list before updating it
this.menu.removeAll();
if (this._playing) {
// create an item to stop the current streams
let item = new PopupMenu.PopupMenuItem(_("Stop"));
item.connect('activate', Lang.bind(this, function() {
this._killStream();
}));
this.menu.addMenuItem(item);
}
// for each stream in our list
for(i = 0; i < streams.length; i++) {
let item = new PopupMenu.PopupMenuItem(streams[i][0]);
let stream = streams[i];
// play stream on activate
item.connect('activate', Lang.bind(this, function() {
this._playStream(stream);
}));
this.menu.addMenuItem(item);
}
},
_playStream: function (stream) {
// make sure nothing is playing first
this._killStream();
this._playing = true;
// set the info in the ui
this.label.set_text(stream[0]);
this.icon.icon_name = 'emblem-music-symbolic';
this.player.set_property("uri", stream[1]);
this.player.set_state(Gst.State.PLAYING);
},
destroy: function() {
this._playing = false;
this.player.set_state(Gst.State.NULL);
this.parent();
}
});
let radioSubMenu = null;
function init(meta) {
Gst.init(null, 0);
Gettext.textdomain("gradio@ivoavnunes.gmail.com");
let localeDir = meta.dir.get_child('locale');
Gettext.bindtextdomain("gradio@ivoavnunes.gmail.com", localeDir.get_path());
}
function enable() {
if (radioSubMenu != null)
return;
radioSubMenu = new RadioSubMenu();
let volMen = Main.panel.statusArea.aggregateMenu._volume._volumeMenu;
let items = volMen._getMenuItems();
let i = 0;
while (i < items.length)
if (items[i] === volMen._output.item)
break;
else
i++;
volMen.addMenuItem(radioSubMenu, i+1);
}
function disable() {
radioSubMenu.destroy();
radioSubMenu = null;
}