2016-02-15 11:13:22 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Calendar = imports.ui.calendar;
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const MessageList = imports.ui.messageList;
|
|
|
|
|
|
|
|
const DBusIface = '<node> \
|
|
|
|
<interface name="org.freedesktop.DBus"> \
|
|
|
|
<method name="ListNames"> \
|
|
|
|
<arg type="as" direction="out" name="names" /> \
|
|
|
|
</method> \
|
|
|
|
<signal name="NameOwnerChanged"> \
|
|
|
|
<arg type="s" direction="out" name="name" /> \
|
|
|
|
<arg type="s" direction="out" name="oldOwner" /> \
|
|
|
|
<arg type="s" direction="out" name="newOwner" /> \
|
|
|
|
</signal> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
const DBusProxy = Gio.DBusProxy.makeProxyWrapper(DBusIface);
|
|
|
|
|
|
|
|
const MprisIface = '<node> \
|
|
|
|
<interface name="org.mpris.MediaPlayer2"> \
|
|
|
|
<method name="Raise" /> \
|
|
|
|
<property name="CanRaise" type="b" access="read" /> \
|
|
|
|
<property name="DesktopEntry" type="s" access="read" /> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
const MprisProxy = Gio.DBusProxy.makeProxyWrapper(MprisIface);
|
|
|
|
|
|
|
|
const MprisPlayerIface = '<node> \
|
|
|
|
<interface name="org.mpris.MediaPlayer2.Player"> \
|
|
|
|
<method name="PlayPause" /> \
|
|
|
|
<method name="Next" /> \
|
|
|
|
<method name="Previous" /> \
|
2016-11-03 17:11:54 +00:00
|
|
|
<property name="CanGoNext" type="b" access="read" /> \
|
|
|
|
<property name="CanGoPrevious" type="b" access="read" /> \
|
2016-02-15 11:13:22 +00:00
|
|
|
<property name="CanPlay" type="b" access="read" /> \
|
|
|
|
<property name="Metadata" type="a{sv}" access="read" /> \
|
|
|
|
<property name="PlaybackStatus" type="s" access="read" /> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
const MprisPlayerProxy = Gio.DBusProxy.makeProxyWrapper(MprisPlayerIface);
|
|
|
|
|
|
|
|
const MPRIS_PLAYER_PREFIX = 'org.mpris.MediaPlayer2.';
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var MediaMessage = new Lang.Class({
|
2016-02-15 11:13:22 +00:00
|
|
|
Name: 'MediaMessage',
|
|
|
|
Extends: MessageList.Message,
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(player) {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._player = player;
|
|
|
|
|
|
|
|
this.parent('', '');
|
|
|
|
|
|
|
|
this._icon = new St.Icon({ style_class: 'media-message-cover-icon' });
|
|
|
|
this.setIcon(this._icon);
|
|
|
|
|
2016-11-03 17:11:54 +00:00
|
|
|
this._prevButton = this.addMediaControl('media-skip-backward-symbolic',
|
2017-10-31 00:38:18 +00:00
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._player.previous();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
|
|
|
|
this._playPauseButton = this.addMediaControl(null,
|
2017-10-31 00:38:18 +00:00
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._player.playPause();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
|
2016-11-03 17:11:54 +00:00
|
|
|
this._nextButton = this.addMediaControl('media-skip-forward-symbolic',
|
2017-10-31 00:38:18 +00:00
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._player.next();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
this._player.connect('changed', this._update.bind(this));
|
|
|
|
this._player.connect('closed', this.close.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onClicked() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._player.raise();
|
|
|
|
Main.panel.closeCalendar();
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateNavButton(button, sensitive) {
|
2016-11-03 17:11:54 +00:00
|
|
|
button.reactive = sensitive;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_update() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this.setTitle(this._player.trackArtists.join(', '));
|
|
|
|
this.setBody(this._player.trackTitle);
|
|
|
|
|
|
|
|
if (this._player.trackCoverUrl) {
|
|
|
|
let file = Gio.File.new_for_uri(this._player.trackCoverUrl);
|
|
|
|
this._icon.gicon = new Gio.FileIcon({ file: file });
|
|
|
|
this._icon.remove_style_class_name('fallback');
|
|
|
|
} else {
|
|
|
|
this._icon.icon_name = 'audio-x-generic-symbolic';
|
|
|
|
this._icon.add_style_class_name('fallback');
|
|
|
|
}
|
|
|
|
|
|
|
|
let isPlaying = this._player.status == 'Playing';
|
|
|
|
let iconName = isPlaying ? 'media-playback-pause-symbolic'
|
|
|
|
: 'media-playback-start-symbolic';
|
|
|
|
this._playPauseButton.child.icon_name = iconName;
|
2016-11-03 17:11:54 +00:00
|
|
|
|
|
|
|
this._updateNavButton(this._prevButton, this._player.canGoPrevious);
|
|
|
|
this._updateNavButton(this._nextButton, this._player.canGoNext);
|
2016-02-15 11:13:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var MprisPlayer = new Lang.Class({
|
2016-02-15 11:13:22 +00:00
|
|
|
Name: 'MprisPlayer',
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(busName) {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._mprisProxy = new MprisProxy(Gio.DBus.session, busName,
|
|
|
|
'/org/mpris/MediaPlayer2',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onMprisProxyReady.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
this._playerProxy = new MprisPlayerProxy(Gio.DBus.session, busName,
|
|
|
|
'/org/mpris/MediaPlayer2',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onPlayerProxyReady.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
|
|
|
|
this._visible = false;
|
|
|
|
this._trackArtists = [];
|
|
|
|
this._trackTitle = '';
|
|
|
|
this._trackCoverUrl = '';
|
|
|
|
},
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._playerProxy.PlaybackStatus;
|
|
|
|
},
|
|
|
|
|
|
|
|
get trackArtists() {
|
|
|
|
return this._trackArtists;
|
|
|
|
},
|
|
|
|
|
|
|
|
get trackTitle() {
|
|
|
|
return this._trackTitle;
|
|
|
|
},
|
|
|
|
|
|
|
|
get trackCoverUrl() {
|
|
|
|
return this._trackCoverUrl;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
playPause() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._playerProxy.PlayPauseRemote();
|
|
|
|
},
|
|
|
|
|
2016-11-03 17:11:54 +00:00
|
|
|
get canGoNext() {
|
|
|
|
return this._playerProxy.CanGoNext;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
next() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._playerProxy.NextRemote();
|
|
|
|
},
|
|
|
|
|
2016-11-03 17:11:54 +00:00
|
|
|
get canGoPrevious() {
|
|
|
|
return this._playerProxy.CanGoPrevious;
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
previous() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._playerProxy.PreviousRemote();
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
raise() {
|
2016-02-15 11:13:22 +00:00
|
|
|
// The remote Raise() method may run into focus stealing prevention,
|
|
|
|
// so prefer activating the app via .desktop file if possible
|
|
|
|
let app = null;
|
|
|
|
if (this._mprisProxy.DesktopEntry) {
|
|
|
|
let desktopId = this._mprisProxy.DesktopEntry + '.desktop';
|
|
|
|
app = Shell.AppSystem.get_default().lookup_app(desktopId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app)
|
|
|
|
app.activate();
|
|
|
|
else if (this._mprisProxy.CanRaise)
|
|
|
|
this._mprisProxy.RaiseRemote();
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_close() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._mprisProxy.disconnect(this._ownerNotifyId);
|
|
|
|
this._mprisProxy = null;
|
|
|
|
|
|
|
|
this._playerProxy.disconnect(this._propsChangedId);
|
|
|
|
this._playerProxy = null;
|
|
|
|
|
|
|
|
this.emit('closed');
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onMprisProxyReady() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._ownerNotifyId = this._mprisProxy.connect('notify::g-name-owner',
|
2017-10-31 00:38:18 +00:00
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
if (!this._mprisProxy.g_name_owner)
|
|
|
|
this._close();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onPlayerProxyReady() {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._propsChangedId = this._playerProxy.connect('g-properties-changed',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._updateState.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
this._updateState();
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateState() {
|
2016-02-15 11:13:22 +00:00
|
|
|
let metadata = {};
|
|
|
|
for (let prop in this._playerProxy.Metadata)
|
|
|
|
metadata[prop] = this._playerProxy.Metadata[prop].deep_unpack();
|
|
|
|
|
|
|
|
this._trackArtists = metadata['xesam:artist'] || [_("Unknown artist")];
|
|
|
|
this._trackTitle = metadata['xesam:title'] || _("Unknown title");
|
|
|
|
this._trackCoverUrl = metadata['mpris:artUrl'] || '';
|
|
|
|
this.emit('changed');
|
|
|
|
|
|
|
|
let visible = this._playerProxy.CanPlay;
|
|
|
|
|
|
|
|
if (this._visible != visible) {
|
|
|
|
this._visible = visible;
|
|
|
|
if (visible)
|
|
|
|
this.emit('show');
|
|
|
|
else
|
|
|
|
this._close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(MprisPlayer.prototype);
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var MediaSection = new Lang.Class({
|
2016-02-15 11:13:22 +00:00
|
|
|
Name: 'MediaSection',
|
|
|
|
Extends: MessageList.MessageListSection,
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init() {
|
2017-02-26 03:35:43 +00:00
|
|
|
this.parent();
|
2016-02-15 11:13:22 +00:00
|
|
|
|
|
|
|
this._players = new Map();
|
|
|
|
|
|
|
|
this._proxy = new DBusProxy(Gio.DBus.session,
|
|
|
|
'org.freedesktop.DBus',
|
|
|
|
'/org/freedesktop/DBus',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onProxyReady.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_shouldShow() {
|
2016-02-15 11:13:22 +00:00
|
|
|
return !this.empty && Calendar.isToday(this._date);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_addPlayer(busName) {
|
2016-02-15 11:13:22 +00:00
|
|
|
if (this._players.get(busName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let player = new MprisPlayer(busName);
|
2017-10-31 00:38:18 +00:00
|
|
|
player.connect('closed',
|
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
this._players.delete(busName);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
|
|
|
player.connect('show',
|
|
|
|
() => {
|
2016-02-15 11:13:22 +00:00
|
|
|
let message = new MediaMessage(player);
|
|
|
|
this.addMessage(message, true);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
this._players.set(busName, player);
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onProxyReady() {
|
2017-10-31 00:38:18 +00:00
|
|
|
this._proxy.ListNamesRemote(([names]) => {
|
|
|
|
names.forEach(name => {
|
|
|
|
if (!name.startsWith(MPRIS_PLAYER_PREFIX))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._addPlayer(name);
|
|
|
|
});
|
|
|
|
});
|
2016-02-15 11:13:22 +00:00
|
|
|
this._proxy.connectSignal('NameOwnerChanged',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onNameOwnerChanged.bind(this));
|
2016-02-15 11:13:22 +00:00
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onNameOwnerChanged(proxy, sender, [name, oldOwner, newOwner]) {
|
2016-02-15 11:13:22 +00:00
|
|
|
if (!name.startsWith(MPRIS_PLAYER_PREFIX))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (newOwner && !oldOwner)
|
|
|
|
this._addPlayer(name);
|
|
|
|
}
|
|
|
|
});
|