aboutsummaryrefslogtreecommitdiffstats
path: root/lib/debugger/src/dbg_ui_settings.erl
blob: fcfd67966f031715d43ee09fb744d11a05f67a70 (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2002-2011. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
-module(dbg_ui_settings).

-include_lib("kernel/include/file.hrl").

%% External exports
-export([start/4]).

%% Internal exports
-export([init/6]).

%% OTP-6011 What's denoted gs="Graphics system id" is now in fact
%% the object id of the monitor window.
-record(state, {gs,      % term() Graphics system id
		win,     % term() Settings dialog window data
		monitor, % pid()  Monitor pid
		action   % load | save
	       }).

%%====================================================================
%% External exports
%%====================================================================

%%--------------------------------------------------------------------
%% start(GS, Pos, Action, SFile)
%%   GS  = Graphics system id
%%   Pos = {X,Y}
%%   Action = load | save
%%   SFile = default | string()
%%--------------------------------------------------------------------
start(GS, Pos, Action, SFile) ->
    Title = case Action of
		load -> "Load Settings Dialog";
		save -> "Save Settings Dialog"
	    end,
    case dbg_ui_winman:is_started(Title) of
	true -> ignore;
	false ->
	    spawn(?MODULE, init, [self(), GS, Pos, Title, Action, SFile])
    end.

%%====================================================================
%% Internal exports
%%====================================================================

init(Monitor, GS, Pos, Title, Action, SFile) ->
    {SDir, SFileName} =
	if
	    %% If settings are saved for the first time, and to
	    %% the default directory HOME/erlang.tools/debugger,
	    %% make sure the directory exists, or create it if
	    %% desired and possible
	    SFile==default -> {default_settings_dir(GS), "NoName.state"};
	    true -> {filename:dirname(SFile), filename:basename(SFile)}
	end,
		    
    Filter = filename:join(SDir, "*.state"),
    Extra = fun(_File) -> true end,
			
    %% Create window
    Win = case Action of
	      load ->
		  dbg_ui_filedialog_win:create_win(GS, Title, Pos, normal,
						   Filter, Extra);
	      save ->
		  dbg_ui_filedialog_win:create_win(GS, Title, Pos, normal,
						   Filter, Extra, SFileName)
	  end,
    Window = dbg_ui_filedialog_win:get_window(Win),
    dbg_ui_winman:insert(Title, Window),

    State = #state{gs=GS, win=Win, monitor=Monitor, action=Action},
    loop(State).


%%====================================================================
%% Main loop and message handling
%%====================================================================

loop(State) ->
    receive

	%% From the GUI
	GuiEvent when is_tuple(GuiEvent), element(1, GuiEvent)==gs ->
	    Cmd = dbg_ui_filedialog_win:handle_event(GuiEvent,
						     State#state.win),
	    State2 = gui_cmd(Cmd, State),
	    loop(State2);

	%% From the dbg_ui_winman process (Debugger window manager)
	{dbg_ui_winman, update_windows_menu, _Data} ->
	    loop(State);
	{dbg_ui_winman, destroy} ->
	    exit(normal)
    end.

gui_cmd(ignore, State) ->
    State;
gui_cmd({stopped, _Dir}, _State) ->	
    exit(normal);
gui_cmd({win, Win}, State) ->
    State#state{win=Win};
gui_cmd({select, File}, State) ->
    State#state.monitor ! {dbg_ui_settings, File, State#state.action},
    exit(normal).


%%====================================================================
%% Internal functions
%%====================================================================

default_settings_dir(GS) ->
    {ok, [[Home]]} = init:get_argument(home),
    DefDir = filename:join([Home, ".erlang_tools", "debugger"]),

    case filelib:is_dir(DefDir) of
	true -> DefDir;
	false ->
	    {ok, CWD} = file:get_cwd(),
	    
	    Msg = ["Default directory", DefDir, "does not exist.",
		   "Click OK to create it or",
		   "Cancel to use other directory."],
	    case tool_utils:confirm(GS, Msg) of
		ok ->
		    ToolsDir = filename:dirname(DefDir),
		    case filelib:is_dir(ToolsDir) of
			true ->
			    case file:make_dir(DefDir) of
				ok -> DefDir;
				_Error -> CWD
			    end;
			false ->
			    case file:make_dir(ToolsDir) of
				ok ->
				    case file:make_dir(DefDir) of
					ok -> DefDir;
					_Error -> CWD
				    end;
				_Error -> CWD
			    end
		    end;
		cancel -> CWD
	    end
    end.