diff --git a/src/gd_v_wallman.erl b/src/gd_v_wallman.erl index 39856a7..26f7314 100644 --- a/src/gd_v_wallman.erl +++ b/src/gd_v_wallman.erl @@ -203,7 +203,7 @@ handle_event(#wx{event = #wxClose{}}, State = #s{wiz = none}) -> {noreply, State}; handle_event(#wx{event = #wxCommand{type = command_button_clicked}, id = ID}, - State = #s{wiz = {_, WizButtons}}) -> + State = #s{wiz = {_, WizButtons, _}}) -> NewState = case lists:keyfind(ID, #w.id, WizButtons) of #w{name = noob} -> wiz_noob_assist(State); @@ -211,7 +211,7 @@ handle_event(#wx{event = #wxCommand{type = command_button_clicked}, false -> State end, {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), {noreply, NewState}; handle_event(Event, State) -> @@ -257,7 +257,7 @@ do_first_run(State = #s{frame = Frame, wallets = Manifest}) -> end. -close_wiz(State = #s{frame = Frame, wiz = {Wiz, _}}) -> +close_wiz(State = #s{frame = Frame, wiz = {Wiz, _, _}}) -> ok = wxWindow:destroy(Wiz), true = wxFrame:show(Frame), State#s{wiz = none}. @@ -355,7 +355,15 @@ do_open3(Path, State = #s{frame = Frame, j = J}) -> 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), J = gd_jt:j(Lang, Trans), @@ -385,10 +393,80 @@ do_wiz(State = #s{wx = WX, lang = Lang, wiz = none}) -> ok = wxFrame:setSize(Wiz, {300, 300}), ok = wxFrame:center(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. Do you want to import an old wallet?"), + wxBoxSizer:add(MainSz, Explanation, zxw:flags(wide)), + + 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"), Name = default_name(), Path = filename:join(DefaultDir, Name), @@ -398,7 +476,11 @@ wiz_noob_assist(State = #s{j = J, wiz = {Wiz, _}}) -> State; abort -> 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() -> {{YY, MM, DD}, {Hr, Mn, Sc}} = calendar:local_time(),