summaryrefslogtreecommitdiff
path: root/spotify-notify.vala
blob: 3f96f209bc3b0df07cfda613a5719c0bb5142c3c (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* spotify-notify.vala
 *
 * Media keys and song-change notification for the
 * official Spotify client for Linux.
 *
 * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org>
 */


[DBus (name = "org.mpris.MediaPlayer2.Player")]
interface Player : Object {
    public abstract void play_pause () throws IOError;
    public abstract void next () throws IOError;
    public abstract void previous () throws IOError;
    public abstract void stop () throws IOError;

}

[DBus (name = "org.freedesktop.DBus.Properties")]
interface PlayerProperties : Object {
    public signal void properties_changed (string iface, 
                                           HashTable<string, Variant> props, 
                                           string[] invalidateds);
    public abstract void get (string iface, string prop, 
                              out Variant value) throws IOError;
}

[DBus (name = "org.gnome.SettingsDaemon.MediaKeys")]
interface MediaKeys : Object {
    public abstract void grab_media_player_keys(string appname, 
                                                uint32 n) throws IOError;
    public signal void media_player_key_pressed (string appname, 
                                                 string key);
}

[DBus (name = "org.freedesktop.DBus")]
interface BusInterface : Object {
    public signal void name_owner_changed (string name, string old_owner, 
                                           string new_owner);
    public abstract void name_has_owner (string name, out bool has_owner)
                         throws IOError;
}

[DBus (name = "org.freedesktop.Notifications")]
interface Notify : Object {
    public abstract uint32 notify (string app_name, uint32 replaces_id, 
                                   string icon, string summary, string body,
                                   string[] actions, 
                                   HashTable<string, Variant> hints, 
                                   int32 timeout) throws IOError;
    public abstract void close_notification (uint32 id) throws IOError;
    public signal void notification_closed (uint32 id, uint32 reason);

}

Player player = null;
PlayerProperties playerprops = null;
MainLoop loop;
Notify notify;
uint32 notification_id = 0;
Soup.SessionAsync soupsession;

const string IMGCACHE_PATTERN="%s/.cache/spotify-albums";
string imgcache;

void cancel_notification() {
    try {
        if (notification_id > 0) {
            stdout.printf("Cancelling notification %u\n", notification_id);
            notify.close_notification(notification_id);
            stdout.printf("Notification cancelled\n");
            notification_id = 0;
        }
    } catch (IOError e) {
        stderr.printf("Error closing notification: %s\n", e.message);
    }
}

class cbdobj {
    public string uri;
    public string targetfile;
    public Variant metadata;
    public bool unsolicited;

    public void fetch_image_cb(Soup.Session sess, Soup.Message mess)
    {
        stdout.printf("Response for %s => %s\n", uri, targetfile);
        stdout.printf("Status code %u, Content length: %zd\n", 
                      mess.status_code, (ssize_t)mess.response_body.length);
        if (mess.response_body.length > 0) {
            stdout.printf("Attempting to write to %s\n", targetfile);
            FileStream? f = FileStream.open(targetfile, "wb");
            if (f != null) {
                f.write(mess.response_body.data);
                f = null; /* fclose */
                stdout.printf("Re-displaying with image for %s\n", uri);
                metadata_changed(metadata, unsolicited);
            }
        }
        /* We're done, so delete us */
        delete *&this;
    }
}

void fetch_image(string targetfile, string uri, 
                 Variant metadata, bool unsolicited) {
    var msg = new Soup.Message ("GET", uri);
    stdout.printf("Prepping async fetch of %s\n", uri);
    cbdobj *cbd = new cbdobj ();
    cbd->uri = uri;
    cbd->targetfile = targetfile;
    cbd->metadata = metadata;
    cbd->unsolicited = unsolicited;
    soupsession.queue_message (msg, cbd->fetch_image_cb);
}

void on_notification_closed (uint32 id, uint32 reason) {
    stdout.printf("Notification %u closed\n", id);
    if (notification_id == id) {
        stdout.printf("That was us\n");
        notification_id = 0;
    }
}

void metadata_changed(Variant metadata, bool unsolicited) {
    Variant? v_artist_a = metadata.lookup_value("xesam:artist", null);
    Variant? v_artist = (v_artist_a == null ? null : 
                         v_artist_a.get_child_value(0));
    Variant? v_album = metadata.lookup_value("xesam:album", null);
    Variant? v_title = metadata.lookup_value("xesam:title", null);
    Variant? v_arturl = metadata.lookup_value("mpris:artUrl", null);
    string? arturl = v_arturl == null ? null : v_arturl.get_string();

    if (v_artist == null ||
        v_album == null ||
        v_title == null) {
        stderr.printf("Incomplete metadata!\n");
        return;
    }

    string imgpath = "";
    string? art_id;

    bool need_load_artwork = false;
    if (v_arturl != null) {
        /* Do we have the image cached? */
        string s_art_id = arturl.substring(arturl.last_index_of_char('/'));
        art_id = s_art_id.next_char();
        imgpath = @"$imgcache/$art_id";
        Posix.Stat sbuf;
        if (Posix.stat(imgpath, out sbuf) != 0) {
            stderr.printf("Artwork %s not found\n", arturl);
            need_load_artwork = true;
        } else {
            stderr.printf("Artwork %s found\n", imgpath);
        }
    }

    string artist = v_artist.get_string();
    string album = v_album.get_string();
    string title = v_title.get_string();

    string msg = @"$title\n$album";

    string[] actions = null;
    HashTable<string, Variant> hints = (
        new HashTable<string, Variant> (null, null));

    try {
        stdout.printf("Attempting to notify\n");
        var nid = notify.notify("Spotify Interface",
                                notification_id,
                                imgpath,
                                artist, msg,
                                actions,
                                hints,
                                unsolicited ? 5000 : 1000);
        notification_id = nid;
        stdout.printf("Notification %u sent\n", notification_id);
    } catch (IOError e) {
        stderr.printf("Error while notifying: %s\n", e.message);
    }
    if (need_load_artwork) {
        stdout.printf("Queueing %s => %s\n", arturl, imgpath);
        fetch_image(imgpath, arturl, metadata, unsolicited);
    }
}

void on_properties_changed (string iface, HashTable<string, Variant> props,
                            string[] invalidateds) {
    if (iface == "org.mpris.MediaPlayer2.Player") {
        Variant? mode = props.lookup("PlaybackStatus");
        if (mode != null) {
            if (mode.get_string() == "Paused") {
                /* Something here? */
                cancel_notification();
            } else {
                /* Switched to playing */
                try {
                    Variant v;
                    playerprops.get ("org.mpris.MediaPlayer2.Player", 
                                     "Metadata", out v);
                    metadata_changed(v, false);
                } catch (IOError e) {
                    stderr.printf("Playback metadata retrieval failed: %s\n", 
                                  e.message);
                }
            }
        }
        Variant? metadata = props.lookup("Metadata");
        if (metadata != null) {
            metadata_changed(metadata, true);
        }
    }
}

void reconnect_spotify () {
    stdout.printf("Attempting to (re)connect to spotify\n");
    try {
        player = Bus.get_proxy_sync(BusType.SESSION, 
                                    "com.spotify.qt", 
                                    "/org/mpris/MediaPlayer2");
        playerprops = Bus.get_proxy_sync(BusType.SESSION, 
                                         "com.spotify.qt",
                                         "/org/mpris/MediaPlayer2");
        playerprops.properties_changed.connect (on_properties_changed);
        stdout.printf("Success!\n");
    } catch (IOError e) {
        stderr.printf("Error (re)connecting: %s\n", e.message);
    }
}

void on_name_owner_changed (string name, string old_owner, string new_owner) {
    if (name == "com.spotify.qt") {
        if (new_owner == "") {
            /* Spotify went away */
            player = null;
            playerprops = null;
            stdout.printf("Spotify went away\n");
        } else {
            /* Spotify has arrived */
            reconnect_spotify();
        }
    }
}

void on_media_player_key_pressed (string appname, string key) {
    stdout.printf("App %s key pressed: %s\n", appname, key);

    if (player == null) {
        stdout.printf("Key ignored, no spotify running\n");
    }

    try {
        switch (key) {
        case "Play":
            player.play_pause();
            break;
        case "PlayPause":
            player.play_pause();
            break;
        case "Stop":
            player.stop();
            break;
        case "Previous":
            player.previous();
            break;
        case "Next":
            player.next();
            break;
        default:
            stderr.printf("Unknown media key!\n");
            break;
        };
    } catch (IOError e) {
        stderr.printf("Error: %s\n", e.message);
    }
}

int main() {
    MediaKeys mkeys;
    BusInterface bi;

    imgcache = IMGCACHE_PATTERN.printf(Environment.get_variable("HOME"));
    DirUtils.create_with_parents(imgcache, 0700);

    try {
        soupsession = new Soup.SessionAsync ();
        stdout.printf("Getting media keys\n");
        mkeys = Bus.get_proxy_sync (BusType.SESSION, 
                                    "org.gnome.SettingsDaemon", 
                                    "/org/gnome/SettingsDaemon/MediaKeys");
        mkeys.media_player_key_pressed.connect (on_media_player_key_pressed);
        mkeys.grab_media_player_keys("Spotify", 0);
        stdout.printf("Getting notifications\n");
        notify = Bus.get_proxy_sync (BusType.SESSION, 
                                     "org.freedesktop.Notifications", 
                                     "/org/freedesktop/Notifications");
        notify.notification_closed.connect (on_notification_closed);
        stdout.printf("Getting owner handles\n");
        bi = Bus.get_proxy_sync (BusType.SESSION, 
                                 "org.freedesktop.DBus", 
                                 "/org/freedesktop/DBus");
        bi.name_owner_changed.connect (on_name_owner_changed);

        bool t;
        bi.name_has_owner("com.spotify.qt", out t);

        if (t)
            reconnect_spotify();
        else
            stdout.printf("Spotify not running, will wait for notify\n");

    } catch (IOError e) {
        stderr.printf ("%s\n", e.message);
        return 1;
    }

    loop = new MainLoop ();
    loop.run ();

    return 0;
}