-
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
// 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 {
public class App : Adw.Application {
private PlacCore.App app;
public App(){
Object(
application_id: "jp.pocka.plac.gtk-adwaita",
flags : ApplicationFlags.DEFAULT_FLAGS
);
this.app = new PlacCore.App();
}
protected override void activate() {
var selector = new ServerSelectorWindow(this, app);
selector.start();
}
public static int main(string[] args) {
return new App().run(args);
}
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/generic-error-dialog.ui")]
class GenericErrorDialog : Adw.Dialog {
[GtkChild]
private unowned Gtk.Label description_label;
[GtkChild]
private unowned Gtk.Label details_label;
public string description {
get { return description_label.label; }
set { description_label.label = value; }
}
public string details {
get { return details_label.label; }
set { details_label.label = value; }
}
public GenericErrorDialog() {
Object();
}
}
private enum ScanErrorKind {
UNEXPECTED_ERROR,
NETWORK_ERROR,
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/server-list.ui")]
class ServerSelectorWindow : Adw.ApplicationWindow {
[GtkChild]
private unowned Gtk.ListBox servers_list;
[GtkChild]
private unowned Adw.Banner failure_banner;
[GtkChild]
private unowned Gtk.Stack stack;
[GtkChild]
private unowned Gtk.Button scan_button;
[GtkChild]
private unowned Adw.StatusPage empty;
private ulong error_detail_hid;
private unowned PlacCore.App core;
public ServerSelectorWindow(Gtk.Application app, PlacCore.App core) {
Object(application: app);
this.core = core;
}
private string? get_and_create_state_dir() {
var state_dir = GLib.Environment.get_user_state_dir();
var dir = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, state_dir, application.application_id);
var file = GLib.File.new_for_path(dir);
if (!file.query_exists()) {
try {
file.make_directory();
} catch (Error e) {
stderr.printf("Unable to create state directory: %s\n", e.message);
return null;
}
}
return dir;
}
private void set_state_file() {
var dir = get_and_create_state_dir();
if (dir == null) {
return;
}
var state_path = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, dir, "state.json");
core.set_state_path(state_path, state_path.length);
}
public void start() {
this.set_state_file();
// TODO: Make this (and possibly other callbacks) async, so access to GTK will be safe.
core.on_restore_complete(() => {
if (core.connection == PlacCore.ConnectionState.IDLE) {
core.server_selector.on_change(() => {
switch (core.server_selector.state) {
case PlacCore.ServerSelectorState.REFRESHING:
case PlacCore.ServerSelectorState.LOADING:
failure_banner.revealed = false;
if (error_detail_hid > 0) {
failure_banner.disconnect(error_detail_hid);
error_detail_hid = 0;
}
stack.visible_child_name = "loading";
scan_button.set_sensitive(false);
break;
case PlacCore.ServerSelectorState.NOT_LOADED:
break;
case PlacCore.ServerSelectorState.LOADED:
servers_list.remove_all();
if (core.server_selector.entries.length == 0) {
servers_list.visible = false;
empty.visible = true;
scan_button.add_css_class("suggested-action");
} else {
servers_list.visible = true;
empty.visible = false;
scan_button.remove_css_class("suggested-action");
}
foreach (unowned PlacCore.ServerSelectorEntry entry in core.server_selector.entries) {
var row = new Adw.ActionRow();
row.title = entry.name;
row.subtitle = entry.version;
var main_window = new MainWindow(this.application, core);
row.activatable_widget = main_window;
row.activated.connect(() => {
main_window.start();
core.connect(entry.id, entry.id.length, null, 0);
this.close();
});
servers_list.append(row);
}
stack.visible_child_name = "idle";
scan_button.set_sensitive(true);
break;
case PlacCore.ServerSelectorState.ERR_UNEXPECTED:
show_error(ScanErrorKind.UNEXPECTED_ERROR, "Unexpected error");
break;
case PlacCore.ServerSelectorState.ERR_NETWORK_UNAVAILABLE:
show_error(ScanErrorKind.NETWORK_ERROR, "Network unavailable");
break;
case PlacCore.ServerSelectorState.ERR_SOCKET_PERMISSION:
show_error(ScanErrorKind.NETWORK_ERROR, "No permission to create UDP socket");
break;
case PlacCore.ServerSelectorState.ERR_OUT_OF_MEMORY:
show_error(ScanErrorKind.UNEXPECTED_ERROR, "Out of memory");
break;
case PlacCore.ServerSelectorState.ERR_SOCKET:
show_error(ScanErrorKind.NETWORK_ERROR, "Failed to operate on a socket");
break;
case PlacCore.ServerSelectorState.ERR_THREAD_SPAWN:
show_error(ScanErrorKind.UNEXPECTED_ERROR, "Unable to spawn a thread");
break;
}
});
core.server_selector.load();
return;
} else if (core.connection == PlacCore.ConnectionState.BUSY) {
var window = new MainWindow(application, core);
window.start();
this.close();
}
});
core.restore_state();
var scan_action = new SimpleAction("scan_servers", null);
scan_action.activate.connect(core.server_selector.load);
this.add_action(scan_action);
this.present();
}
private void show_error(ScanErrorKind kind, string message) {
scan_button.add_css_class("suggested-action");
this.error_detail_hid = failure_banner.button_clicked.connect(() => {
switch (kind) {
case ScanErrorKind.NETWORK_ERROR:
var dialog = new ServerListNetworkErrorDialog(message);
dialog.present(this);
break;
case ScanErrorKind.UNEXPECTED_ERROR:
var dialog = new ServerListUnexpectedErrorDialog(message);
dialog.present(this);
break;
}
});
failure_banner.revealed = true;
stack.visible_child_name = "idle";
scan_button.set_sensitive(true);
}
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/server-list-unexpected-error-dialog.ui")]
class ServerListUnexpectedErrorDialog : GenericErrorDialog {
public ServerListUnexpectedErrorDialog(string details) {
Object(details: details);
}
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/server-list-network-error-dialog.ui")]
class ServerListNetworkErrorDialog : GenericErrorDialog {
public ServerListNetworkErrorDialog(string details) {
Object(details: details);
}
}
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/main-window.ui")]
class MainWindow : Adw.ApplicationWindow {
[GtkChild]
private unowned Gtk.Label test_label;
private unowned PlacCore.App core;
public MainWindow(Gtk.Application app, PlacCore.App core) {
Object(application: app);
this.core = core;
}
public void start() {
core.on_connection_change(() => {
switch (core.connection) {
case PlacCore.ConnectionState.IDLE:
idle_render();
break;
case PlacCore.ConnectionState.BUSY:
test_label.label = "Loading";
break;
default:
test_label.label = core.connection.to_string();
break;
}
});
this.present();
}
private void idle_render() {
if (core.server == null) {
return;
}
test_label.label = core.server.name;
}
}
}