-
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
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
namespace PlacGtkAdwaita {
[SingleInstance]
private class ArtworkDownloader : Object {
public Soup.Session session = new Soup.Session.with_options(
"max_conns", 6,
"max_conns_per_host", 6,
"user_agent", "plac-for-gtk4/0.0 "
);
// This class does not use Soup's cache feature: it's undocumented,
// obscure usage (due both to API design and lack of documentation,)
// and frequently crashes. As Roon Server seems to handle our current
// request size/window/rate, this class avoids dealing with libsoup's
// cache.
public ArtworkDownloader() {
Object();
}
}
class Artwork : Gtk.Box {
private ArtworkDownloader downloader = new ArtworkDownloader();
private uint16 _width = 100;
public uint16 width {
get { return _width; }
construct set {
_width = value;
width_request = (int) value;
}
}
private uint16 _height = 100;
public uint16 height {
get { return _height; }
construct set {
_height = value;
height_request = (int) value;
}
}
private Plac.AsyncConnection? _conn = null;
public Plac.AsyncConnection? conn {
get { return _conn; }
set {
if (_conn != value) {
_conn = value;
render();
}
}
}
private string? _image_key = null;
public string? image_key {
get { return _image_key; }
set {
if (_image_key != value) {
_image_key = value;
render();
}
}
}
public Plac.Image.ScalingMethod scaling { get; set; default = FIT; }
private Gtk.Picture picture = new Gtk.Picture();
private Adw.Spinner spinner = new Adw.Spinner();
private Gtk.Image error_icon = new Gtk.Image.from_icon_name("image-missing-symbolic");
public Artwork() {
Object();
}
public Artwork.new_with_size(uint16 width, uint16 height) {
Object(width: width, height: height);
}
construct {
picture.set_parent(this);
picture.accessible_role = PRESENTATION;
picture.halign = CENTER;
picture.valign = CENTER;
picture.width_request = _width;
picture.height_request = _height;
picture.content_fit = SCALE_DOWN;
picture.add_css_class("plac-playback-artwork");
picture.visible = false;
spinner.set_parent(this);
spinner.halign = CENTER;
spinner.valign = CENTER;
spinner.width_request = _width;
spinner.height_request = _height;
spinner.visible = true;
error_icon.set_parent(this);
error_icon.halign = CENTER;
error_icon.valign = CENTER;
error_icon.width_request = _width;
error_icon.height_request = _height;
error_icon.pixel_size = _width / 2;
error_icon.visible = false;
render();
}
public void render() {
picture.paintable = null;
if (_conn == null || _image_key == null) {
return;
}
picture.visible = false;
spinner.visible = true;
error_icon.visible = false;
var opts = new Plac.Image.GetOptions();
opts.set_size(scaling, _width, _height);
opts.set_content_type(JPEG);
GLib.Uri url;
try {
var raw = _conn.get_image_url(_image_key, opts);
if (raw == null) {
GLib.log("Plac", LEVEL_WARNING, "Failed to build image download URL for %s", _image_key);
error_icon.visible = true;
spinner.visible = false;
return;
}
url = GLib.Uri.parse(raw, NONE);
} catch (GLib.UriError error) {
GLib.log("Plac", LEVEL_WARNING, "Failed to build image download URL for %s: %s", _image_key, error.message);
error_icon.visible = true;
spinner.visible = false;
return;
}
var msg = new Soup.Message.from_uri("GET", url);
downloader.session.send_and_read_async.begin(msg, 0, null, (obj, res) => {
try {
var bytes = downloader.session.send_and_read_async.end(res);
// We don't cache, so 3xx response is error here
if (msg.status_code != 200) {
GLib.log("Plac", LEVEL_WARNING, "Failed to download artwork: HTTP %u (key=%s)", msg.status_code, _image_key);
error_icon.visible = true;
spinner.visible = false;
return;
}
try {
var texture = Gdk.Texture.from_bytes(bytes);
picture.paintable = texture;
spinner.visible = false;
picture.visible = true;
} catch (GLib.Error error) {
GLib.log("Plac", LEVEL_WARNING, "Failed to generate artwork texture: %s", error.message);
error_icon.visible = true;
spinner.visible = false;
}
} catch (GLib.Error error) {
GLib.log("Plac", LEVEL_WARNING, "Failed to download image: %s", error.message);
error_icon.visible = true;
spinner.visible = false;
return;
}
});
}
}
}