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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2009. 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%
%%%*********************************************************************
%%%
%%% Description: Part of pc handling the creation of menus, as well as
%%% treating the signals these menus results in,
%%% when chosen.
%%%
%%%*********************************************************************
-module(tv_pc_menu_handling).
-compile([{nowarn_deprecated_function,{gs,config,2}},
{nowarn_deprecated_function,{gs,destroy,1}},
{nowarn_deprecated_function,{gs,start,0}},
{nowarn_deprecated_function,{gs,window,3}}]).
-export([create_menus/1,
exit_button/1,
insert_object/1,
delete_object/1,
search_object/1,
open_table/7,
set_poll_interval/1,
poll_table/1,
sort_rising_order/1,
sort_falling_order/1,
no_sorting/1,
lists_as_strings/1,
lists_as_lists/1,
table_info/1,
help_button/1,
otp_help_button/1,
get_window_title/4]).
-include("tv_int_def.hrl").
-include("tv_int_msg.hrl").
-include("tv_pc_int_def.hrl").
%%%*********************************************************************
%%% EXTERNAL FUNCTIONS
%%%*********************************************************************
%% Shortcuts currently used, in alphabetical order:
%%
%% c -> "Exit"
%% d -> "Delete Object"
%% f -> "Sort Falling Order"
%% h -> "Help"
%% i -> "Table Info"
%% n -> "No Sorting"
%% o -> "Edit Object"
%% p -> "Poll Table"
%% r -> "Sort Rising Order"
%% s -> "Search Object"
%% v -> "Set Poll Interval"
%% x -> "Exit"
create_menus(PwPid) ->
%% Due to a bug (or some other reason), only one of the radiobuttons belonging
%% to a specified group can be selected, even if different processes have created
%% the radiobuttons! This means that, if we have started more than one tv_main
%% process, selecting one radiobutton will affect the radiobuttons in the other
%% tv_main process(es)!!! Since this is a highly undesirable bahaviour, we have to
%% create unique group names (i.e., atoms).
%% (We need to group the radiobuttons, since otherwise all created by one process
%% belongs to the same group, which also is undesirable...)
SelfStr = pid_to_list(self()),
SortGroup = list_to_atom("sorting" ++ SelfStr),
ListGroup = list_to_atom("lists" ++ SelfStr),
% Order pw to create the 'File' menu.
?GRAPH_FUNC_FILE:create_menu(PwPid,
" File ",
1,
[{" Table Info ", normal, table_info, 7, i},
separator,
{" Close ", normal, exit_button, 1, c}
]),
?GRAPH_FUNC_FILE:create_menu(PwPid,
" Edit ",
1,
[{" Edit Object... ", normal, insert_object, 1, o},
{" Delete Object ", normal, delete_object, 1, d}
]),
?GRAPH_FUNC_FILE:create_menu(PwPid,
" View ",
1,
[{" Lists as Lists ",{radio,false,ListGroup},lists_as_lists,10,no_char},
{" Lists as Strings ",{radio,true,ListGroup},lists_as_strings,10,no_char}
]),
% Order pw to create the 'Options' menu.
?GRAPH_FUNC_FILE:create_menu(PwPid,
" Options ",
1,
[{" Poll Table ", normal, poll_table, 1, p},
{" Poll Interval... ",normal,set_poll_interval,6,no_char},
separator,
{" Search Object ", normal, search_object, 1, s},
separator,
{" Sort Ascending Order ",{radio,false,SortGroup},sort_rising_order,6,no_char},
{" Sort Descending Order ",{radio,false,SortGroup},sort_falling_order,6,no_char},
{" No Sorting ",{radio,true,SortGroup},no_sorting,1,no_char}
]).
exit_button(_ProcVars) ->
exit(normal).
help_button(ProcVars) ->
WinP = ProcVars#process_variables.window_params,
HelpFile = filename:join([code:lib_dir(tv), "doc", "html", "index.html"]),
tool_utils:open_help(WinP#window_params.window_id, HelpFile),
ProcVars.
otp_help_button(ProcVars) ->
WinP = ProcVars#process_variables.window_params,
IndexFile = filename:join([code:root_dir(), "doc", "index.html"]),
tool_utils:open_help(WinP#window_params.window_id, IndexFile),
ProcVars.
table_info(ProcVars) ->
#process_variables{table_id = TableId,
current_node = Node,
local_node = LocalNode,
table_type = Type,
parent_pid = ParentPid} = ProcVars,
case TableId of
undefined ->
done;
_OtherValue ->
ParentPid ! {tv_start_infowin, TableId, Node, LocalNode, Type}
end,
ProcVars.
sort_rising_order(ProcVars) ->
request_sort_settings(ProcVars#process_variables.pd_pid, true, false),
ProcVars.
sort_falling_order(ProcVars) ->
request_sort_settings(ProcVars#process_variables.pd_pid, true, true),
ProcVars.
no_sorting(ProcVars) ->
request_sort_settings(ProcVars#process_variables.pd_pid, false, false),
ProcVars.
set_poll_interval(ProcVars) ->
#process_variables{etsread_pid = EtsreadPid,
poll_interval = PollInterval} = ProcVars,
case tv_poll_dialog:start(PollInterval) of
cancel ->
ProcVars;
NewPollInterval ->
EtsreadPid ! #etsread_set_poll_interval{sender = self(),
interval = NewPollInterval},
ProcVars#process_variables{poll_interval = NewPollInterval}
end.
poll_table(ProcVars) ->
EtsreadPid = ProcVars#process_variables.etsread_pid,
EtsreadPid ! #etsread_poll_table{sender = self()},
ProcVars.
search_object(ProcVars) ->
DbsPid = ProcVars#process_variables.dbs_pid,
DbsPid ! #dbs_search_req{sender=self()},
ProcVars.
lists_as_strings(ProcVars) ->
PdPid = ProcVars#process_variables.pd_pid,
PdPid ! #pc_list_info{sender=self(), lists_as_strings=true},
DbsPid = ProcVars#process_variables.dbs_pid,
DbsPid ! #pc_list_info{sender=self(), lists_as_strings=true},
ProcVars#process_variables{lists_as_strings=true}.
lists_as_lists(ProcVars) ->
PdPid = ProcVars#process_variables.pd_pid,
PdPid ! #pc_list_info{sender=self(), lists_as_strings=false},
DbsPid = ProcVars#process_variables.dbs_pid,
DbsPid ! #pc_list_info{sender=self(), lists_as_strings=false},
ProcVars#process_variables{lists_as_strings=false}.
insert_object(ProcVars) ->
#process_variables{pd_pid = PdPid,
current_node = Node,
local_node = LocalNode,
table_type = TabType,
table_name = TabName,
table_protection = Protection,
window_params = WinP} = ProcVars,
case Protection of
public ->
case TabType of
mnesia ->
case catch tv_mnesia_rpc:table_info(Node, LocalNode, TabName, attributes) of
nodedown ->
handle_error(nodedown);
no_table ->
handle_error(nodedown);
mnesia_not_started ->
handle_error(mnesia_not_started);
{unexpected_error,Reason} ->
handle_error({unexpected_error,Reason});
AttrList ->
PdPid ! #pd_rec_edit{sender = self(),
attributes = AttrList
}
end;
ets ->
PdPid ! #pd_rec_edit{sender = self(),
attributes = [tuple]
}
end;
_OtherProtection ->
WinId = WinP#window_params.window_id,
gs:config(WinId, [beep]),
ErrMsg =
case get(error_msg_mode) of
normal ->
["The table is protected and",
" cannot be edited."];
haiku ->
["The table you see",
"Is cunningly protected:",
"You can only watch."]
end,
tv_utils:notify(WinId, "TV Notification", ErrMsg)
end,
ProcVars.
delete_object(ProcVars) ->
#process_variables{dbs_pid = DbsPid,
table_protection = Protection,
marked_row = MarkedRow,
marked_object = MarkedObject,
marked_color = MarkedColor,
window_params = WinP} = ProcVars,
case MarkedRow of
undefined ->
done;
_AnyRow ->
case Protection of
public ->
DbsPid ! #dbs_delete_object{sender = self(),
object = MarkedObject,
color = MarkedColor,
obj_no = MarkedRow};
_OtherProtection ->
WinId = WinP#window_params.window_id,
gs:config(WinId, [beep]),
ErrMsg =
case get(error_msg_mode) of
normal ->
["The table is protected and",
" cannot be edited."];
haiku ->
["The table you see",
"Is cunningly protected:",
"You can only watch."]
end,
tv_utils:notify(WinId, "TV Notification", ErrMsg)
end
end,
ProcVars.
open_table(CurrNode, LocalNode, TableId, TableType, TableName, Raise, ProcVars) ->
#process_variables{dbs_pid = DbsPid,
etsread_pid = EtsreadPid,
pw_pid = PwPid,
pd_pid = PdPid,
poll_interval = PollInterval,
window_params = WinP} = ProcVars,
case Raise of
true ->
gs:config(WinP#window_params.window_id, [raise]);
false ->
done
end,
{Type, KeyPos, Protection} = init_etsread(EtsreadPid, DbsPid, CurrNode, LocalNode, TableId,
TableType, PollInterval),
WinTitle = get_window_title(TableType, CurrNode, TableId, TableName),
PwPid ! #pw_set_window_title{sender = self(),
win_title = WinTitle},
Writable =
case Protection of
public ->
true;
_Other ->
false
end,
RecordName =
case TableType of
mnesia ->
tv_mnesia_rpc:table_info(CurrNode, LocalNode, TableId, record_name);
ets ->
undefined
end,
PdPid ! #pd_new_table{sender = self(),
table_type = TableType,
table_name = TableName,
record_name = RecordName,
writable = Writable},
init_dbs(DbsPid, Type, KeyPos, EtsreadPid),
ProcVars#process_variables{current_node = CurrNode,
local_node = LocalNode,
table_id = TableId,
table_type = TableType,
table_name = TableName,
table_protection = Protection}.
get_window_title(ets, Node, TableId, TableName) ->
NameStr = lists:flatten(io_lib:write(TableName)),
TableStr = case TableId of
{TableName, _Pid} ->
NameStr;
TableName ->
NameStr;
_Other ->
lists:flatten(io_lib:write(TableId)) ++ " (" ++ NameStr ++ ")"
end,
WinTitleSuffix = " Node: " ++ atom_to_list(Node),
"ETS: " ++ TableStr ++ WinTitleSuffix;
get_window_title(mnesia, Node, _TableId, TableName) ->
TableNameStr = lists:flatten(io_lib:write(TableName)),
WinTitleSuffix = " Node: " ++ atom_to_list(Node),
"Mnesia: " ++ TableNameStr ++ WinTitleSuffix.
%%%*********************************************************************
%%% INTERNAL FUNCTIONS
%%%*********************************************************************
init_etsread(EtsreadPid, DbsPid, Node, LocalNode, TableId, TableType, PollInterval) ->
EtsreadPid ! #etsread_deblock{sender = self(),
dbs_pid = DbsPid,
node = Node,
local_node = LocalNode,
table_id = TableId,
table_type = TableType,
poll_interval = PollInterval
},
receive
#etsread_deblock_cfm{type=Type, keypos=KeyPos, protection=Protection} ->
{Type, KeyPos, Protection}
after 10000 ->
exit(error)
end.
init_dbs(DbsPid, Type, KeyPos, EtsreadPid) ->
DbsPid ! #dbs_deblock{sender = self(),
etsread_pid = EtsreadPid,
type = Type,
keypos = KeyPos,
sublist_length = ?ITEMS_TO_DISPLAY
},
receive
#dbs_deblock_cfm{} ->
done
after 10000 ->
exit(error)
end.
request_sort_settings(PdPid, Sorting, Reverse) ->
PdPid ! #pd_get_sort_settings{sender = self(),
sorting = Sorting,
reverse = Reverse
}.
handle_error(mnesia_not_started) ->
gs:window(errorwin, gs:start(), []),
gs:config(errorwin, [beep]),
case get(error_msg_mode) of
normal ->
tv_utils:notify(errorwin, "TV Notification", ["Mnesia not started!"]);
haiku ->
tv_utils:notify(errorwin, "TV Notification", ["Mnesia is stopped.",
"We wish to reach all data",
"But we never will."])
end,
gs:destroy(errorwin);
handle_error(nodedown) ->
done; %% Main process handles this!
handle_error({unexpected_error,Cause}) ->
gs:window(errorwin, gs:start(), []),
io:format("Unexpected error: ~p~n", [Cause]),
gs:config(errorwin, [beep]),
gs:destroy(errorwin).
|