Special 'import' wizard #27

Open
spivee wants to merge 3 commits from wizard2 into master

View File

@ -203,7 +203,7 @@ handle_event(#wx{event = #wxClose{}}, State = #s{wiz = none}) ->
{noreply, State}; {noreply, State};
handle_event(#wx{event = #wxCommand{type = command_button_clicked}, handle_event(#wx{event = #wxCommand{type = command_button_clicked},
id = ID}, id = ID},
State = #s{wiz = {_, WizButtons}}) -> State = #s{wiz = {_, WizButtons, _}}) ->
NewState = NewState =
case lists:keyfind(ID, #w.id, WizButtons) of case lists:keyfind(ID, #w.id, WizButtons) of
#w{name = noob} -> wiz_noob_assist(State); #w{name = noob} -> wiz_noob_assist(State);
@ -211,7 +211,7 @@ handle_event(#wx{event = #wxCommand{type = command_button_clicked},
false -> State false -> State
end, end,
{noreply, NewState}; {noreply, NewState};
handle_event(#wx{event = #wxClose{}}, State = #s{wiz = {_, _}}) -> handle_event(#wx{event = #wxClose{}}, State = #s{wiz = WizTuple}) when is_tuple(WizTuple) ->
NewState = close_wiz(State), NewState = close_wiz(State),
{noreply, NewState}; {noreply, NewState};
handle_event(Event, State) -> handle_event(Event, State) ->
@ -257,7 +257,7 @@ do_first_run(State = #s{frame = Frame, wallets = Manifest}) ->
end. end.
close_wiz(State = #s{frame = Frame, wiz = {Wiz, _}}) -> close_wiz(State = #s{frame = Frame, wiz = {Wiz, _, _}}) ->
ok = wxWindow:destroy(Wiz), ok = wxWindow:destroy(Wiz),
true = wxFrame:show(Frame), true = wxFrame:show(Frame),
State#s{wiz = none}. State#s{wiz = none}.
@ -355,7 +355,15 @@ do_open3(Path, State = #s{frame = Frame, j = J}) ->
end. end.
do_wiz(State = #s{wx = WX, lang = Lang, wiz = none}) -> do_wiz(State) ->
case find_wallet_files(State) of
false ->
do_wiz_create(State);
{true, WalletDir} ->
do_wiz_import(State, WalletDir)
end.
do_wiz_create(State = #s{wx = WX, lang = Lang, wiz = none}) ->
Trans = gd_jt:read_translations(?MODULE), Trans = gd_jt:read_translations(?MODULE),
J = gd_jt:j(Lang, Trans), J = gd_jt:j(Lang, Trans),
@ -385,22 +393,96 @@ do_wiz(State = #s{wx = WX, lang = Lang, wiz = none}) ->
ok = wxFrame:setSize(Wiz, {300, 300}), ok = wxFrame:setSize(Wiz, {300, 300}),
ok = wxFrame:center(Wiz), ok = wxFrame:center(Wiz),
true = wxFrame:show(Wiz), true = wxFrame:show(Wiz),
State#s{wiz = {Wiz, Buttons}}. State#s{wiz = {Wiz, Buttons, create}}.
do_wiz_import(State = #s{wx = WX, lang = Lang, wiz = none}, WalletDir) ->
Trans = gd_jt:read_translations(?MODULE),
J = gd_jt:j(Lang, Trans),
Wiz = wxFrame:new(WX, ?wxID_ANY, J("Initializing Wallet")),
MainSz = wxBoxSizer:new(?wxVERTICAL),
Explanation = wxStaticText:new(Wiz, ?wxID_ANY,
"Reinstall detected.\nDo you want to import an old wallet?",
[{style, ?wxALIGN_CENTRE_HORIZONTAL}]),
wxBoxSizer:add(MainSz, Explanation, zxw:flags(base)),
ButtonTemplates =
[{noob, J("Import an old wallet.")},
{l33t, J("Open the wallet manager.")}],
MakeButton =
fun({Name, Label}) ->
B = wxButton:new(Wiz, ?wxID_ANY, [{label, Label}]),
#w{name = Name, id = wxButton:getId(B), wx = B}
end,
Buttons = lists:map(MakeButton, ButtonTemplates),
Add = fun(#w{wx = Button}) -> wxBoxSizer:add(MainSz, Button, zxw:flags(wide)) end,
ok = lists:foreach(Add, Buttons),
ok = wxFrame:setSizer(Wiz, MainSz),
ok = wxSizer:layout(MainSz),
ok = wxFrame:connect(Wiz, command_button_clicked),
ok = wxFrame:connect(Wiz, close_window),
ok = wxFrame:setSize(Wiz, {300, 300}),
ok = wxFrame:center(Wiz),
true = wxFrame:show(Wiz),
State#s{wiz = {Wiz, Buttons, {import, WalletDir}}}.
wiz_noob_assist(State = #s{j = J, wiz = {Wiz, _}}) -> find_wallet_files(#s{prefs = Prefs}) ->
DefaultDir = zx_lib:path(var, "otpr", "gajudesk"),
case maps:find(dir, Prefs) of
{ok, DefaultDir} -> find_wallet_files3(DefaultDir);
error -> find_wallet_files3(DefaultDir);
{ok, PrefDir} -> find_wallet_files2(PrefDir, DefaultDir)
end.
find_wallet_files2(PrefDir, DefaultDir) ->
case has_wallet_file(PrefDir) of
true -> {true, PrefDir};
false -> find_wallet_files3(DefaultDir)
end.
find_wallet_files3(DefaultDir) ->
case has_wallet_file(DefaultDir) of
true -> {true, DefaultDir};
false -> false
end.
has_wallet_file(Dir) ->
case file:list_dir_all(Dir) of
{ok, Filenames} ->
lists:any(fun path_is_wallet_file/1, Filenames);
{error, _} ->
false
end.
path_is_wallet_file(Path) ->
case filename:extension(Path) of
".gaju" -> true;
<<".gaju">> -> true;
_ -> false
end.
wiz_noob_assist(State = #s{j = J, wiz = {Wiz, _, create}}) ->
DefaultDir = zx_lib:path(var, "otpr", "gajudesk"), DefaultDir = zx_lib:path(var, "otpr", "gajudesk"),
Name = default_name(), Name = default_name(),
Path = filename:join(DefaultDir, Name), Path = filename:join(DefaultDir, Name),
case do_new2(Path, J, Wiz) of case do_new2(Path, J, Wiz) of
ok -> ok ->
Label = J("Account 1"),
ok = gd_con:make_key({eddsa, ed25519}, 256, Label, <<>>, none, {sha3, 256}),
ok = do_close(State), ok = do_close(State),
State; State;
abort -> abort ->
State State
end. end;
wiz_noob_assist(State = #s{prefs = Prefs, wiz = {_, _, {import, WalletDir}}}) ->
NewPrefs = maps:put(dir, WalletDir, Prefs),
NewState = State#s{prefs = NewPrefs},
do_import(NewState).
default_name() -> default_name() ->
{{YY, MM, DD}, {Hr, Mn, Sc}} = calendar:local_time(), {{YY, MM, DD}, {Hr, Mn, Sc}} = calendar:local_time(),