-
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
// 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 {
private errordomain ResolveError {
CONNECTION_ERROR,
SERVER_NOT_FOUND
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/main-window.ui")]
class MainWindow : Adw.ApplicationWindow {
[GtkChild]
private unowned Gtk.Stack root_stack;
[GtkChild]
private unowned PlaybackToolbar playback_toolbar;
[GtkChild]
private unowned Adw.Banner error_banner;
[GtkChild]
private unowned Browse browse;
[GtkChild]
private unowned Gtk.ListBox browse_hierarchy;
private Settings settings = new Settings();
private Plac.Server? server = null;
private Plac.Connection? conn = null;
private Plac.ZonesModel? zones = null;
private string server_id;
public MainWindow(Gtk.Application app, Plac.Server server) {
(typeof (Artwork)).ensure();
(typeof (ServerConnecting)).ensure();
(typeof (PlaybackToolbar)).ensure();
Object(application: app);
this.server = server;
this.server_id = server.id;
}
public MainWindow.from_server_id(Gtk.Application app, string server_id) {
(typeof (Artwork)).ensure();
(typeof (ServerConnecting)).ensure();
(typeof (PlaybackToolbar)).ensure();
Object(application: app);
this.server_id = server_id;
}
construct {
var provider = new Gtk.CssProvider();
provider.load_from_resource("/jp/pocka/plac/gtk-adwaita/css/main-window.css");
Gtk.StyleContext.add_provider_for_display(
this.display, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);
error_banner.button_clicked.connect(() => {
try_listen();
});
var explore_row = new BrowseHierarchyRow(BROWSE, "Explore");
browse_hierarchy.append(explore_row);
browse_hierarchy.select_row(explore_row);
browse_hierarchy.append(new BrowseHierarchyRow(ALBUMS, "Albums"));
browse_hierarchy.append(new BrowseHierarchyRow(PLAYLISTS, "Playlists"));
browse_hierarchy.append(new BrowseHierarchyRow(ARTISTS, "Artists"));
browse_hierarchy.append(new BrowseHierarchyRow(GENRES, "Genres"));
browse_hierarchy.append(new BrowseHierarchyRow(COMPOSERS, "Composers"));
browse_hierarchy.append(new BrowseHierarchyRow(INTERNET_RADIO, "Internet Radio"));
browse_hierarchy.append(new BrowseHierarchyRow(SETTINGS, "Settings"));
browse_hierarchy.row_selected.connect((row) => {
if (row == null) {
return;
}
browse.hierarchy = ((BrowseHierarchyRow) row).hierarchy;
});
browse.loading_start.connect(() => {
browse_hierarchy.sensitive = false;
});
browse.loading_end.connect(() => {
browse_hierarchy.sensitive = true;
});
}
public void start() {
root_stack.visible_child_name = "loading";
try_listen();
this.present();
}
public void start_with_addr(string ip_addr, uint16 port) {
root_stack.visible_child_name = "loading";
try_listen_with_addr(ip_addr, port);
this.present();
}
private void try_listen(string? token = null) {
error_banner.revealed = false;
if (conn != null) {
listen_events();
} else {
resolve_server.begin((obj, res) => {
try {
resolve_server.end(res);
} catch (GLib.Error e) {
error_banner.title = e.message;
error_banner.revealed = true;
return;
}
listen_events(token);
});
}
}
private void try_listen_with_addr(string ip_addr, uint16 port) {
if (conn != null) {
return;
}
var colon = ip_addr.index_of(":");
var patched_ip_addr = colon < 0 ? ip_addr : ip_addr.slice(0, colon);
var address = new GLib.InetSocketAddress.from_string(patched_ip_addr, port);
Plac.Server.from_address.begin(address, port, server_id, null, (obj, res) => {
try {
this.server = Plac.Server.from_address.end(res);
} catch (Plac.ResolveServerError error) {
GLib.log("Plac", LEVEL_DEBUG, "Failed to resolve: %s", error.message);
GLib.log("Plac", LEVEL_INFO, "Failed to restore connection, scanning server");
try_listen(settings.connected_server_token);
return;
}
listen_events(settings.connected_server_token);
});
}
private void listen_events(string? token = null) {
this.title = "Plac - %s".printf(server.name);
root_stack.visible_child_name = "loading";
playback_toolbar.visible = false;
var req = new Roon.Registry.Register.Request();
// TODO: Configure these via build parameters
req.extension_id = "jp.pocka.plac.gtk-adwaita";
req.display_name = "Plac for GTK";
req.display_version = "0.0.0-dev";
req.publisher = "Shota FUJI";
req.email = "pockawoooh@gmail.com";
if (token != null && token != "") {
req.token = token;
}
req.append_required_services("com.roonlabs.transport:2");
req.append_required_services("com.roonlabs.browse:1");
req.append_provided_services("com.roonlabs.ping:1");
server.connect_async.begin(req, (obj, res) => {
try {
var conn = server.connect_async.end(res);
this.conn = conn;
zones = new Plac.ZonesModel(conn);
root_stack.visible_child_name = "main";
playback_toolbar.visible = true;
settings.connected_server_id = server.id;
settings.connected_server_token = conn.token;
settings.connected_server_addr = server.address.address.to_string();
settings.connected_server_port = server.http_port;
playback_toolbar.conn = conn;
playback_toolbar.zones_model = zones;
browse.start(conn, zones);
} catch (Plac.ConnectError error) {
GLib.log("Plac", LEVEL_CRITICAL, "Failed to connect: %s", error.message);
error_banner.title = "Connection error: %s".printf(error.message);
error_banner.revealed = true;
}
});
}
private async void resolve_server() throws GLib.Error {
GLib.SourceFunc callback = resolve_server.callback;
GLib.Error? error = null;
var scanner = new Plac.ServerScanner();
scanner.start();
scanner.found.connect((found) => {
if (server_id == found.id) {
server = found;
callback();
}
});
scanner.scan_failed.connect((e) => {
error = e;
});
yield;
scanner.stop();
if (error != null) {
throw error;
}
}
}
}