This commit is contained in:
2026-05-21 14:32:38 +09:00
parent 3259e802db
commit 77ff0ca084
4 changed files with 252 additions and 85 deletions
+82 -13
View File
@@ -44,7 +44,7 @@
%-behavior(gd_v).
-include_lib("wx/include/wx.hrl").
-export([to_front/0, to_front/1, trouble/1]).
-export([pending/1, accounts/1]).
-export([pending/1, accounts/1, retire/2]).
-export([start_link/1]).
-export([init/1, terminate/2, code_change/3,
handle_call/3, handle_cast/2, handle_info/2, handle_event/2]).
@@ -62,6 +62,7 @@
keys = #w{} :: #w{},
accs = [] :: [#wr{}],
rider = none :: none | pid(),
recvr = none :: none | pid(),
check = #w{} :: #w{},
list = #w{} :: #w{},
dl = #w{} :: #w{},
@@ -115,6 +116,14 @@ accounts(Manifest) ->
wx_object:cast(?MODULE, {accounts, Manifest}).
-spec retire(PID, Info) -> ok
when PID :: pid(),
Info :: term().
retire(PID, Info) ->
gen_server:cast(?MODULE, {retire, PID, Info}).
%%% Startup
@@ -210,8 +219,10 @@ init({Prefs, {Selected, Keys}}) ->
ok = gd_v:safe_size(Frame, NewPrefs),
ok = wxFrame:connect(Frame, command_button_clicked),
ok = wxFrame:connect(Frame, command_choice_selected),
ok = wxFrame:connect(Frame, close_window),
ok = wxListBox:connect(DownloadP, command_listbox_doubleclicked),
true = wxFrame:show(Frame),
State = #s{wx = Wx, frame = Frame, lang = Lang, j = J, prefs = Prefs,
keys = KP, accs = Keys,
check = CheckB, list = DL_L,
@@ -239,6 +250,9 @@ handle_cast({pending, Manifest}, State) ->
handle_cast({accounts, Manifest}, State) ->
NewState = do_accounts(Manifest, State),
{noreply, NewState};
handle_cast({retire, PID, Info}, State) ->
NewState = do_retire(PID, Info, State),
{noreply, NewState};
handle_cast(to_front, State = #s{frame = Frame}) ->
ok = ensure_shown(Frame),
ok = wxFrame:raise(Frame),
@@ -251,9 +265,6 @@ handle_cast(Unexpected, State) ->
{noreply, State}.
handle_info({retiring, PID, Reason}, State = #s{rider = PID}) ->
ok = tell(info, "Rider retired with: ~p", [Reason]),
{noreply, State#s{rider = none}};
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}.
@@ -271,8 +282,10 @@ handle_event(#wx{event = #wxCommand{type = command_button_clicked}, id = ID},
State = #s{ul = #w{id = ID}}) ->
NewState = do_ul(State),
{noreply, NewState};
handle_event(#wx{event = #wxCommand{type = command_listbox_doubleclicked}},
State) ->
handle_event(#wx{event = #wxCommand{type = command_choice_selected}}, State) ->
ok = kill_recvr(State),
{noreply, State};
handle_event(#wx{event = #wxCommand{type = command_listbox_doubleclicked}}, State) ->
NewState = do_dl(State),
{noreply, NewState};
handle_event(#wx{event = #wxClose{}}, State) ->
@@ -310,14 +323,55 @@ do_accounts(State, Manifest) ->
State.
do_check(State = #s{rider = none}) ->
PID = spawn_link(gd_n_rider, init, [{"localhost", 7777}]),
do_check(State#s{rider = PID});
do_check(State = #s{rider = PID, keys = #w{wx = KeyP}}) ->
do_check(State = #s{recvr = none, keys = #w{wx = KeyP}}) ->
case wxChoice:getStringSelection(KeyP) of
"" ->
State;
KeyID ->
PubKey = list_to_binary(KeyID),
PID = spawn_link(gd_n_recvr, init, [PubKey, {"localhost", 7777}]),
do_check2(State#s{recvr = PID})
end;
do_check(State = #s{recvr = PID}) ->
case gd_n_recvr:check(PID) of
ok -> State;
{ok, Challenge} -> challenge(State, Challenge)
end.
do_check2(State = #s{recvr = PID}) ->
case gd_n_recvr:check(PID) of
{ok, Challenge} ->
challenge(State, Challenge);
{error, Reason} ->
ok = tell(info, "GajuExpress connection failed with: ~p", [Reason]),
State#s{recvr = none}
end.
challenge(State = #s{recvr = PID, keys = KeyP}, Challenge) ->
case wxChoice:getStringSelection(KeyP) of
"" ->
ok = gd_n_recvr:stop(PID),
State;
KeyID ->
PubKey = list_to_binary(KeyID),
handle_challenge(State, PubKey, Challenge)
end.
handle_challenge(State = #s{recvr = PID}, PubKey, Challenge) ->
case gd_con:sign_binary(PubKey, Challenge) of
{ok, Sig} ->
respond(State, Sig);
{error, bad_key} ->
ok = gd_n_recvr:stop(PID),
State
end.
respond(State = #s{recvr = PID}, Sig) ->
ok =
case wxChoice:getStringSelection(KeyP) of
"" -> ok;
KeyID -> gd_n_rider:check(PID, KeyID)
case gd_n_recvr:response(PID, Sig) of
ok -> ok;
{error, Reason} -> ok = tell(info, "~p: ~p", [PID, Reason])
end,
State.
@@ -354,6 +408,21 @@ do_close(#s{frame = Frame, prefs = Prefs}) ->
% unicode:characters_to_list(Name ++ ".gaju").
kill_recvr(#s{recvr = none}) -> ok;
kill_recvr(#s{recvr = PID}) -> gd_n_recvr:stop(PID).
do_retire(PID, Info, State = #s{rider = PID}) ->
ok = tell(info, "Rider retired with: ~p", [Info]),
State#s{rider = none};
do_retire(PID, Info, State = #s{recvr = PID}) ->
ok = tell(info, "Rider retired with: ~p", [Info]),
State#s{recvr = none};
do_retire(PID, Info, State) ->
ok = tell(info, "~p retired with: ~p", [PID, Info]),
State.
ensure_shown(Frame) ->
case wxWindow:isShown(Frame) of
true ->