27
27
public class AudioScrobblerComponent: Component {
28
private const int SCROBBLE_SONG_DELAY = 60;
28
private const int SCROBBLE_SONG_DELAY = 20;
30
30
private Bindings bindings;
31
31
private Drtgtk.Application app;
32
32
private Soup.Session connection;
33
33
private unowned Drt.KeyValueStorage config;
34
34
private unowned Drt.KeyValueStorage global_config;
35
private AudioScrobbler? scrobbler = null;
35
private AudioScrobbler[]? scrobblers = null;
36
36
private MediaPlayerModel? player = null;
37
37
private uint scrobble_timeout = 0;
38
38
private string? scrobble_title = null;
57
57
public override Gtk.Widget? get_settings() {
58
if (scrobbler == null) {
58
if (scrobblers == null || scrobblers.length == 0) {
62
62
var grid = new Gtk.Grid();
63
63
grid.orientation = Gtk.Orientation.VERTICAL;
64
var label = new Gtk.Label(Markup.printf_escaped("<b>%s</b>", scrobbler.name));
65
label.use_markup = true;
66
label.vexpand = false;
69
Gtk.Widget? widget = scrobbler.get_settings(app);
64
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
65
var label = new Gtk.Label(Markup.printf_escaped("<b>%s</b>", scrobbler.name));
66
label.use_markup = true;
67
label.vexpand = false;
70
Gtk.Widget? widget = scrobbler.get_settings(app);
77
79
protected override bool activate() {
78
var scrobbler = new LastfmScrobbler(connection);
79
this.scrobbler = scrobbler;
80
string base_key = "component.%s.%s.".printf(id, scrobbler.id);
81
config.bind_object_property(base_key, scrobbler, "scrobbling_enabled").set_default(true).update_property();
82
global_config.bind_object_property(base_key, scrobbler, "session").update_property();
83
global_config.bind_object_property(base_key, scrobbler, "username").update_property();
85
if (scrobbler.has_session) {
86
scrobbler.retrieve_username.begin();
88
80
player = bindings.get_model<MediaPlayerModel>();
89
81
player.set_track_info.connect(on_set_track_info);
90
scrobbler.notify.connect_after(on_scrobbler_notify);
82
scrobblers = {new LastfmScrobbler(connection)};
83
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
84
string base_key = "component.%s.%s.".printf(id, scrobbler.id);
85
config.bind_object_property(base_key, scrobbler, "scrobbling_enabled").set_default(true).update_property();
86
global_config.bind_object_property(base_key, scrobbler, "session").update_property();
87
global_config.bind_object_property(base_key, scrobbler, "username").update_property();
88
var lastfm_compatible = scrobbler as LastfmCompatibleScrobbler;
89
if (lastfm_compatible != null && lastfm_compatible.has_session) {
90
lastfm_compatible.retrieve_username.begin();
92
scrobbler.notify.connect_after(on_scrobbler_notify);
91
94
on_set_track_info(player.title, player.artist, player.album, player.state);
95
98
protected override bool deactivate() {
96
scrobbler.notify.disconnect(on_scrobbler_notify);
99
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
100
scrobbler.notify.disconnect(on_scrobbler_notify);
98
103
player.set_track_info.disconnect(on_set_track_info);
100
105
cancel_scrobbling();
159
164
track_info_cb_id = Timeout.add_seconds(1, () => {
160
165
track_info_cb_id = 0;
161
if (scrobbler == null) {
166
if (scrobblers == null) {
162
167
// Scrobbling had been disabled before this callback was dispatched.
165
if (scrobbler.can_update_now_playing) {
166
if (title != null && artist != null && state == "playing" ) {
167
scrobbler.update_now_playing.begin(title, artist, on_update_now_playing_done);
170
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
171
if (scrobbler.can_update_now_playing) {
172
if (title != null && artist != null && state == "playing" ) {
173
scrobbler.update_now_playing.begin(title, artist, on_update_now_playing_done);
171
178
cancel_scrobbling();
173
if (scrobbler.can_scrobble) {
174
schedule_scrobbling(title, artist, album, state);
180
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
181
if (scrobbler.can_scrobble) {
182
schedule_scrobbling(title, artist, album, state);
194
203
private bool scrobble_cb() {
195
204
scrobble_timeout = 0;
196
if (scrobbler.can_scrobble) {
198
var datetime = new DateTime.now_utc();
199
scrobbler.scrobble_track.begin(
200
scrobble_title, scrobble_artist, scrobble_album, datetime.to_unix(), on_scrobble_track_done);
205
foreach (unowned AudioScrobbler scrobbler in scrobblers) {
206
if (scrobbler.can_scrobble) {
208
var datetime = new DateTime.now_utc();
209
scrobbler.scrobble_track.begin(
210
scrobble_title, scrobble_artist, scrobble_album, datetime.to_unix(), on_scrobble_track_done);