1########################################################################
2##
3## Copyright (C) 2012-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} setpref ("@var{group}", "@var{pref}", @var{val})
28## @deftypefnx {} {} setpref ("@var{group}", @{"@var{pref1}", "@var{pref2}", @dots{}@}, @{@var{val1}, @var{val2}, @dots{}@})
29## Set the preference @var{pref} to the given @var{val} in the named preference
30## group @var{group}.
31##
32## The named preference group must be a string.
33##
34## The preference @var{pref} may be a string or a cell array of strings.
35##
36## The corresponding value @var{val} may be any Octave value, .e.g., double,
37## struct, cell array, object, etc.  Or, if @var{pref} is a cell array of
38## strings then @var{val} must be a cell array of values with the same size as
39## @var{pref}.
40##
41## If the named preference or group does not exist, it is added.
42## @seealso{addpref, getpref, ispref, rmpref}
43## @end deftypefn
44
45function setpref (group, pref, val)
46
47  if (nargin != 3)
48    print_usage ();
49  endif
50
51  if (! ischar (group))
52    error ("setpref: GROUP must be a string");
53  endif
54  if (! (ischar (pref) || iscellstr (pref)))
55    error ("setpref: PREF must be a string or cellstr");
56  endif
57
58  prefs = loadprefs ();
59
60  if (ischar (pref))
61    prefs.(group).(pref) = val;
62  else
63    if (! size_equal (pref, val))
64      error ("setpref: size mismatch for PREF and VAL");
65    endif
66
67    for i = 1:numel (pref)
68      prefs.(group).(pref{i}) = val{i};
69    endfor
70  endif
71
72  saveprefs (prefs);
73
74endfunction
75
76
77%!test
78%! HOME = getenv ("HOME");
79%! tmp_home = tempname ();
80%! unwind_protect
81%!   mkdir (tmp_home);
82%!   setenv ("HOME", tmp_home);
83%!
84%!   setpref ("group1", "pref1", [1 2 3]);
85%!   assert (getpref ("group1", "pref1"), [1 2 3]);
86%!
87%!   setpref ("group2", {"prefA", "prefB"}, {"StringA", {"StringB"}});
88%!   assert (getpref ("group2", "prefA"), "StringA");
89%!   assert (getpref ("group2", "prefB"), {"StringB"});
90%!
91%!   setpref ("group1", {"pref1", "pref2"}, {1, 2});
92%!   assert (getpref ("group1", "pref1"), 1);
93%!   assert (getpref ("group1", "pref2"), 2);
94%!
95%!   fail ('setpref ("group1", {"p1", "p2"}, 1)', ...
96%!         "size mismatch for PREF and VAL");
97%! unwind_protect_cleanup
98%!   unlink (fullfile (tmp_home, ".octave_prefs"));
99%!   if (isfolder (tmp_home))
100%!     rmdir (tmp_home);
101%!   endif
102%!   if (isempty (HOME))
103%!     unsetenv ("HOME");
104%!   else
105%!     setenv ("HOME", HOME);
106%!   endif
107%! end_unwind_protect
108
109%!error setpref ()
110%!error setpref (1)
111%!error setpref (1,2)
112%!error setpref (1,2,3,4)
113%!error <GROUP must be a string> setpref (1, "pref1", 2)
114%!error <PREF must be a string> setpref ("group1", 1, 2)
115