~fenryxo/nuvola-player/master

« back to all changes in this revision

Viewing changes to src/nuvolakit-runner/components/scrobbler/AudioScrobblerComponent.vala

  • Committer: Jiří Janoušek
  • Date: 2019-02-23 17:01:18 UTC
  • Revision ID: git-v1:bdc8aeadd9d22a78667d2a714e78084a347fb7ef
Allow multiple AudioScrobblers

Issue: tiliado/nuvolaruntime#17

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright 2014-2018 Jiří Janoušek <janousek.jiri@gmail.com>
 
2
 * Copyright 2014-2019 Jiří Janoušek <janousek.jiri@gmail.com>
3
3
 *
4
4
 * Redistribution and use in source and binary forms, with or without
5
5
 * modification, are permitted provided that the following conditions are met:
25
25
namespace Nuvola {
26
26
 
27
27
public class AudioScrobblerComponent: Component {
28
 
    private const int SCROBBLE_SONG_DELAY = 60;
 
28
    private const int SCROBBLE_SONG_DELAY = 20;
29
29
 
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;
55
55
    }
56
56
 
57
57
    public override Gtk.Widget? get_settings() {
58
 
        if (scrobbler == null) {
 
58
        if (scrobblers == null || scrobblers.length == 0) {
59
59
            return null;
60
60
        }
61
61
 
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;
67
 
        label.hexpand = true;
68
 
        grid.add(label);
69
 
        Gtk.Widget? widget = scrobbler.get_settings(app);
70
 
        if (widget != null) {
71
 
            grid.add(widget);
 
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;
 
68
            label.hexpand = true;
 
69
            grid.add(label);
 
70
            Gtk.Widget? widget = scrobbler.get_settings(app);
 
71
            if (widget != null) {
 
72
                grid.add(widget);
 
73
            }
72
74
        }
73
75
        grid.show_all();
74
76
        return grid;
75
77
    }
76
78
 
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();
84
 
 
85
 
        if (scrobbler.has_session) {
86
 
            scrobbler.retrieve_username.begin();
87
 
        }
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();
 
91
            }
 
92
            scrobbler.notify.connect_after(on_scrobbler_notify);
 
93
        }
91
94
        on_set_track_info(player.title, player.artist, player.album, player.state);
92
95
        return true;
93
96
    }
94
97
 
95
98
    protected override bool deactivate() {
96
 
        scrobbler.notify.disconnect(on_scrobbler_notify);
97
 
        scrobbler = null;
 
99
        foreach (unowned AudioScrobbler scrobbler in scrobblers) {
 
100
            scrobbler.notify.disconnect(on_scrobbler_notify);
 
101
        }
 
102
        scrobblers = null;
98
103
        player.set_track_info.disconnect(on_set_track_info);
99
104
        player = null;
100
105
        cancel_scrobbling();
158
163
 
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.
163
168
                return false;
164
169
            }
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);
 
174
                    }
168
175
                }
169
176
            }
170
177
 
171
178
            cancel_scrobbling();
172
179
 
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);
 
183
                }
175
184
            }
176
185
            return false;
177
186
        });
193
202
 
194
203
    private bool scrobble_cb() {
195
204
        scrobble_timeout = 0;
196
 
        if (scrobbler.can_scrobble) {
197
 
            scrobbled = true;
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) {
 
207
                scrobbled = true;
 
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);
 
211
            }
201
212
        }
202
213
        return false;
203
214
    }