summaryrefslogtreecommitdiff
path: root/modules/atoms/macros/atomsSetConfig.sci
blob: b88a9343f44b2e6d4c9932d979d503f7887d805b (plain)
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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
// Copyright (C) 2012 - DIGITEO - Allan CORNET
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

// End user function

// Add toolboxes to the list of packages that are automatically loaded at Scilab start
// This function has an impact on the following files :
//  -> ATOMSDIR/config

function nbChanges = atomsSetConfig(field, value)

    rhs        = argn(2);
    nbChanges      = 0;
    systemUpdateNeeded = %F;

    // Check number of input arguments
    // =========================================================================

    if rhs <> 2 then
        error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"), "atomsSetConfig", 2));
    end

    // Check input parameters type
    // =========================================================================

    if type(field) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"), "atomsSetConfig", 1));
    end

    if type(value) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"), "atomsSetConfig", 2));
    end

    // field and value must have the same size
    // =========================================================================

    if or( size(field) <> size(value) ) then
        error(msprintf(gettext("%s: Incompatible input arguments #%d and #%d: Same sizes expected.\n"), "atomsSetConfig", 1, 2));
    end

    pref_attrs = ["useProxy", "proxyHost", "proxyPort", "proxyUser", "proxyPassword";
    "enabled", "host", "port", "user", "password"];

    i=1;
    for element = field(:)'
        if strcmpi("verbose",element) == 0 then
            field(i) = convstr(part(element,1),"u") + part(element,2:length(element));
        else
            field(i) = convstr(part(element,1),"l") + part(element,2:length(element));
        end
        element = field(i);

        if element == "offLine" then
            // Prior to version 5.4.0, offline was called Offline
            field(i) = "offline";
            element = "offline";
        end

        if element == "useProxy" ..
            | element == "offline" ..
            | element == "autoload" ..
            | element == "autoloadAddAfterInstall" ..
            | element == "Verbose" ..
            then
            select value(i)
            case "True" then,
            case "False" then,
            case "true" then value(i)="True",
            case "false" then value(i)="False",
            else
                error(msprintf(gettext("%s: Wrong value for input configuration argument: True or False expected.\n"), value(i)));
            end
        elseif element == "proxyHost" ..
            | element == "proxyPort" ..
            | element == "proxyUser" ..
            | element == "proxyPassword" ..
            | element == "downloadTool" ..
            | element == "downloadTimeout" ..
            | element == "updateTime" ..
            then continue;
        else
            error(msprintf(gettext("%s: Wrong key for input configuration argument.\n"), element));
        end
        i = i + 1;
    end

    // Load Atoms Internals lib if it's not already loaded
    // =========================================================================
    if ~ exists("atomsinternalslib") then
        load("SCI/modules/atoms/macros/atoms_internals/lib");
    end

    // Define the path of the file that will record the change
    // =========================================================================
    atoms_directory =  atomsPath("system", "user");

    // Does the atoms_directory exist, if not create it
    // =========================================================================

    if ~ isdir(atoms_directory) then
        mkdir(atoms_directory);
    end

    // Get the current config struct
    // =========================================================================

    config_struct = atomsGetConfig();

    // Loop on field
    // =========================================================================

    prefs_kv = [];
    proxy_changes = 0;

    for i=1:size(field, "*")

        if (~isfield(config_struct, field(i))) | (config_struct(field(i)) <> value(i)) then
            nbChanges = nbChanges + 1;
        else
            continue;
        end

        if field(i) == "offline" then
            systemUpdateNeeded = %T;
        end

        k = find(field(i) == pref_attrs(1, :));
        if ~isempty(k) then
            if field(i) == "useProxy" then
                value(i) = convstr(value(i), "l");
            end
            prefs_kv = [prefs_kv [pref_attrs(2, k) ; value(i)]];
            nbChanges = nbChanges - 1;
            proxy_changes = proxy_changes + 1;
        else
            config_struct(field(i)) = value(i);
        end;
    end

    if ~isempty(prefs_kv) then
        setPreferencesValue("//web/body/proxy", prefs_kv);
    end

    // Shortcut
    // =========================================================================
    if nbChanges == 0 then
        nbChanges = proxy_changes;
        return;
    end

    // Apply Changes
    // =========================================================================

    config_fields    = getfield(1, config_struct);
    config_fields(1:2) = [];
    config_fields    = gsort(config_fields);

    config_str = [];

    for i=1:size(config_fields, "*")
        config_str = [ config_str ; config_fields(i) + " = " + config_struct(config_fields(i)) ];
    end

    mputl(config_str, atoms_directory + "config");

    // SystemUpdate
    // =========================================================================

    if systemUpdateNeeded then
        atomsSystemUpdate();
    end

    nbChanges = nbChanges + proxy_changes;
endfunction