summaryrefslogtreecommitdiff
path: root/modules/preferences/macros/createXConfiguration.sci
blob: 99a366fad053b248529a524132f7b874274538ac (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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
//
// 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

// Create the XConfiguration.xml if it is not present in SCIHOME
// This function is useful when Scilab is launched in nwni mode for the first
// time (atoms could be used in this mode and need proxy settings which are
// stored in the prefs file).
//
function createXConfiguration()
    path = SCIHOME + "/XConfiguration.xml";
    if fileinfo(path) ~= [] then
        return
    end;

    if isdir(SCIHOME) then
        try
            fd = mopen(SCIHOME + "/test", "wb");
            mclose(fd);
            mdelete(SCIHOME + "/test")
        catch
            return
        end
    else
        return
    end

    doc = xmlRead(SCI + "/modules/preferences/etc/XConfiguration.xml");
    modules = ls(SCI + "/modules/*");
    xmls = [];
    for m = modules'
        xmls = [xmls ; ls(m + "/etc/XConfiguration-*.xml")];
    end

    children = doc.root.children;

    meta = "ctrl";
    if getos() == "Darwin" then
        meta = "meta";
    end

    for xml = xmls'
        xdoc = xmlRead(xml);
        if basename(xml) == "XConfiguration-general" then
            xp = xmlXPath(xdoc, "//shortcuts/body/actions/action-folder/action[contains(@key,''OSSCKEY'')]");
            for i = 1:xp.size
                e = xp(i);
                attr = e.attributes;
                attr.key = strsubst(attr.key, "OSSCKEY", meta);
            end
        end
        pos = xmlXPath(doc, "count(//" + xdoc.root.name + "/preceding-sibling::*)+1");
        if children(pos).name == xdoc.root.name then
            children(pos) = xdoc.root;
        end
        xmlDelete(xdoc);
    end

    xmlWrite(doc, path, %T);
    xmlDelete(doc);
endfunction