Bug 12900 fixed: It was not possible to set proxy options in Preferences 99/13899/5
Calixte DENIZET [Tue, 4 Mar 2014 16:21:25 +0000 (17:21 +0100)]
Change-Id: I940fc737d99d892146cff40e2a6b5dcc9c6f788b

17 files changed:
scilab/CHANGES_5.5.X
scilab/modules/atoms/macros/atomsGetConfig.sci
scilab/modules/atoms/macros/atomsSetConfig.sci
scilab/modules/fileio/Makefile.am
scilab/modules/fileio/Makefile.in
scilab/modules/fileio/help/en_US/getURL.xml
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/gui/etc/XConfiguration-web.xml
scilab/modules/gui/etc/XConfiguration-web.xsl
scilab/modules/preferences/etc/XConfiguration.xml
scilab/modules/preferences/etc/preferences.start
scilab/modules/preferences/includes/getScilabPreference.h
scilab/modules/preferences/macros/buildmacros.sce
scilab/modules/preferences/macros/getPrefValues.sci [new file with mode: 0644]
scilab/modules/preferences/macros/setPrefValues.sci [new file with mode: 0644]
scilab/modules/preferences/src/c/getScilabPreference.c
scilab/modules/preferences/src/java/org/scilab/modules/preferences/Component/Entry.java

index 046e9e3..6e6bc47 100644 (file)
@@ -518,6 +518,8 @@ Scilab Bug Fixes
 
 * Bug #12896 fixed - Typos fixed in XML module error messages.
 
+* Bug #12900 fixed - It was not possible to set proxy options in Preferences.
+
 * Bug #12910 fixed - Typos fixed in several help pages.
 
 * Bug #12938 fixed - No Java compiler was available in Linux binary version.
index 6914651..cf6234f 100644 (file)
@@ -44,6 +44,9 @@ function result = atomsGetConfig(field)
         end
     end
 
+    pref_attrs = ["useProxy", "proxyHost", "proxyPort", "proxyUser", "proxyPassword";
+    "enabled", "host", "port", "user", "password"];
+
     // Load Atoms Internals lib if it's not already loaded
     // =========================================================================
     if ~ exists("atomsinternalslib") then
@@ -57,6 +60,12 @@ function result = atomsGetConfig(field)
     if rhs == 0 then
         result = struct();
     else
+        i = find(pref_attrs(1, :) == field);
+        if ~isempty(i) then
+            result = getPrefValues("//web/body/proxy", pref_attrs(2, i));
+            return;
+        end
+
         result = "";
     end
 
@@ -70,6 +79,12 @@ function result = atomsGetConfig(field)
     if fileinfo(atoms_directory + "config") <> [] then
         config_lines = mgetl(atoms_directory + "config");
     else
+        if (rhs == 0)
+            values = getPrefValues("//web/body/proxy", pref_attrs(2, :));
+            for i = 1:size(pref_attrs, "c")
+                result(pref_attrs(1, i)) = values(i);
+            end
+        end
         return;
     end
 
@@ -98,4 +113,11 @@ function result = atomsGetConfig(field)
         end
     end
 
+    if (rhs == 0)
+        values = getPrefValues("//web/body/proxy", pref_attrs(2, :));
+        for i = 1:size(pref_attrs, "c")
+            result(pref_attrs(1, i)) = values(i);
+        end
+    end
+
 endfunction
index a31f190..20cb023 100644 (file)
@@ -44,8 +44,12 @@ function nbChanges = atomsSetConfig(field, value)
     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(:)
+    for element = field(:)'
         if strcmpi("verbose",element) == 0 then
             field(i) = convstr(part(element,1),"u") + part(element,2:length(element));
         else
@@ -112,6 +116,8 @@ function nbChanges = atomsSetConfig(field, value)
     // Loop on field
     // =========================================================================
 
+    prefs_kv = [];
+
     for i=1:size(field, "*")
 
         if (~isfield(config_struct, field(i))) | (config_struct(field(i)) <> value(i)) then
@@ -124,7 +130,20 @@ function nbChanges = atomsSetConfig(field, value)
             systemUpdateNeeded = %T;
         end
 
-        config_struct(field(i)) = value(i);
+        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;
+        else
+            config_struct(field(i)) = value(i);
+        end;
+    end
+
+    if ~isempty(prefs_kv) then
+        setPrefValues("//web/body/proxy", prefs_kv);
     end
 
     // Shortcut
index 5d7b954..9bac259 100644 (file)
@@ -117,6 +117,7 @@ libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(top_srcdir)/modules/api_scilab/includes/ \
                                -I$(top_srcdir)/modules/string/includes/ \
                                -I$(top_srcdir)/modules/localization/includes/ \
+                               -I$(top_srcdir)/modules/preferences/includes/ \
                            $(XML_FLAGS) \
                                $(AM_CPPFLAGS)
 
index 7d9c950..f954fa3 100644 (file)
@@ -733,6 +733,7 @@ libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(top_srcdir)/modules/api_scilab/includes/ \
                                -I$(top_srcdir)/modules/string/includes/ \
                                -I$(top_srcdir)/modules/localization/includes/ \
+                               -I$(top_srcdir)/modules/preferences/includes/ \
                            $(XML_FLAGS) \
                                $(AM_CPPFLAGS)
 
index 6107ee5..1223d46 100644 (file)
         
         <para>IPv6 (and obviously IPv4) are supported out-of-the box by getURL.</para>
         
-        <para>Proxy configuration is enabled in this version. It uses the parameters from ATOMS config file.</para>
+        <para>Proxy configuration is enabled in this version, it can be modified in the Preferences under the rubric "Internet".</para>
         
         <para>
             The <emphasis role="italic">CURLOPT_FOLLOWLOCATION</emphasis> curl option is activated to make sure the download follow the URL.
index ef05210..3b40357 100644 (file)
@@ -28,6 +28,8 @@
 #include "machine.h"
 #include "scicurdir.h"
 #include "splitpath.h"
+#include "getScilabPreference.h"
+#include "stricmp.h"
 /* ==================================================================== */
 #ifndef HAVE_BASENAME
 static char *Curl_basename(char *path);
@@ -72,7 +74,7 @@ static size_t writefunc(void *ptr, size_t size, size_t nmemb, inputString *s)
     if (s->ptr == NULL)
     {
         Scierror(999, "Internal error: realloc() failed.\n");
-        return NULL;
+        return 0;
     }
     memcpy(s->ptr + s->len, ptr, size * nmemb);
     s->ptr[new_len] = '\0';
@@ -110,235 +112,46 @@ static char *getFileNameFromURL(char *url)
 /* ==================================================================== */
 int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
 {
-    FILE* pFile = NULL;
-    long lSize = 0;
-    char* buffer = NULL;
-    size_t result = 0;
-
-    char* configPtr = NULL;
-
-    char* host = NULL;
-    char* user = NULL;
-    char* password = NULL;
-    char* userpwd = NULL;
-    long port = 0;
-    int useproxy = 0;
-
-    char *tp = NULL;
-
-    int eqpos = 0;
-    int tplen = 0;
-
-    //construct ATOMS config file path
-    configPtr = (char *)MALLOC(PATH_MAX * sizeof(char));
-    strcpy(configPtr, getSCIHOME());
-
-    if (strcmp(getOSFullName(), "Windows") == 0)
-    {
-        if (strstr(getOSRelease(), "x64") != NULL)
-        {
-            strcat(configPtr, "/.atoms/x64/config");
-        }
-        else
-        {
-            strcat(configPtr, "/.atoms/config");
-        }
-    }
-    else
-    {
-        strcat(configPtr, "/.atoms/config");
-    }
+    const char * attrs[] = {"enabled", "host", "port", "user", "password"};
+    const unsigned int N = sizeof(attrs) / sizeof(char*);
+    char ** values = getAttributesValues("//web/body/proxy", attrs, N);
 
-    wcfopen (pFile, configPtr , "rb" );
-    if (pFile == NULL)
+    if (!values)
     {
-        // No configuration file. Leave
-        FREE(configPtr);
         return 0;
     }
 
-    fseek (pFile , 0 , SEEK_END);
-    lSize = ftell(pFile);
-    if (lSize < 0)
+    if (stricmp(values[0]/*enabled*/, "true") == 0)
     {
-        Scierror(999, _("Could not read the configuration file '%s'.\n"), configPtr);
-        FREE(configPtr);
-        fclose(pFile);
-        return 0;
-    }
-
-    rewind (pFile);
-
-    // allocate memory to contain the whole file
-    buffer = (char*)MALLOC((lSize + 1) * sizeof(char));
-    if (buffer == NULL)
-    {
-        fclose(pFile);
-        return 0;
-    }
-    buffer[lSize] = '\0';
-
-    // copy the file into the buffer
-    result = fread (buffer, 1, lSize, pFile);
-    fclose(pFile);
-
-    if (result != lSize)
-    {
-        Scierror(999, _("Failed to read the scicurl_config file '%s'.\n"), configPtr);
-        FREE(configPtr);
-        FREE(buffer);
-        return 0;
-    }
-
-    //add null terminated
-    buffer[result] = '\0';
-
-    // parse each line to extract variables
-    tp = strtok(buffer, "\r\n");
-    while (tp != NULL)
-    {
-        char *eqptr = NULL;
-        char *field = NULL;
-        char *value = NULL;
-
-        eqptr = strrchr(tp, '=');
-        tplen = (int)strlen(tp);
-        if (eqptr == NULL)
-        {
-            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), '=' not found %d:%s\n"), configPtr, tplen, tp);
-            FREE(configPtr);
-            FREE(buffer);
-            return 0;
-        }
-
-        eqpos = (int)(eqptr - tp);
-        if (tplen <= eqpos + 1)
-        {
-            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), after an '='\n"), configPtr);
-            FREE(configPtr);
-            FREE(buffer);
-            return 0;
-        }
-
-        if (tp[eqpos - 1] != ' ' || tp[eqpos + 1] != ' ')
-        {
-            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), space before and after '=' expected\n"), configPtr);
-            FREE(configPtr);
-            FREE(buffer);
-            return 0;
-        }
-
-        //get field and value from each line
-        field = (char *)MALLOC(sizeof(char) * (eqpos));
-        value = (char *)MALLOC(sizeof(char) * (strlen(tp) - eqpos - 1));
+        const unsigned int ulen = strlen(values[3]);
+        const unsigned int plen = strlen(values[4]);
 
-        memcpy(field, tp, eqpos - 1);
-        field[eqpos - 1] = '\0';
+        *proxyHost = values[1]; //host;
+        *proxyPort = strtol(values[2], NULL, 10); //port;
+        FREE(values[2]);
 
-        memcpy(value, tp + eqpos + 2, strlen(tp) - eqpos - 2);
-        value[strlen(tp) - eqpos - 2] = '\0';
-
-
-        //check and read proxy variables
-        if (strcmp(field, "useProxy") == 0)
-        {
-            if (strcmp(value, "False") == 0)
-            {
-                FREE(field);
-                FREE(value);
-                FREE(buffer);
-                FREE(configPtr);
-
-                if (host)
-                {
-                    FREE(host);
-                }
-
-                if (user)
-                {
-                    FREE(user);
-                }
-
-                if (password)
-                {
-                    FREE(password);
-                }
-                return 0;
-            }
-
-            if (strcmp(value, "True") == 0)
-            {
-                useproxy = 1;
-            }
-        }
-        else if (strcmp(field, "proxyHost") == 0)
-        {
-            host = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
-            strcpy(host, value);
-        }
-        else if (strcmp(field, "proxyPort") == 0)
-        {
-            port = strtol(value, NULL, 10);
-        }
-        else if (strcmp(field, "proxyUser") == 0)
+        if (plen == 0)
         {
-            user = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
-            strcpy(user, value);
+            *proxyUserPwd = values[3]; //user
         }
-        else if (strcmp(field, "proxyPassword") == 0)
+        else
         {
-            password = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
-            strcpy(password, value);
+            *proxyUserPwd = (char *)MALLOC((ulen + 1 + plen + 1) * sizeof(char *));
+            sprintf(*proxyUserPwd, "%s:%s", values[3]/*user*/, values[4]/*password*/);
+            (*proxyUserPwd)[ulen + 1 + plen] = '\0';
+            FREE(values[3]);
         }
 
-        FREE(field);
-        FREE(value);
-
-        tp = strtok(NULL, "\r\n");
+        FREE(values[4]);
+        FREE(values);
     }
-
-    FREE(configPtr);
-    FREE(buffer);
-
-    // if proxy is set, update the parameters
-    if (useproxy == 1)
+    else
     {
-
-        // proxyUserPwd = "user:password"
-        int userlen = 0;
-        int passlen = 0;;
-
-        if (user)
-        {
-            userlen = (int)strlen(user);
-        }
-
-        if (password)
-        {
-            passlen = (int)strlen(user);
-        }
-
-        if (userlen + passlen != 0)
-        {
-            userpwd = (char *)MALLOC((userlen + passlen + 2) * sizeof(char));
-            strcpy(userpwd, user);
-            strcat(userpwd, ":");
-            if (password)
-            {
-                strcat(userpwd, password);
-            }
-
-            FREE(user);
-            FREE(password);
-        }
-
-        *proxyHost = host;
-        *proxyPort = port;
-        *proxyUserPwd = userpwd;
-
+        freeArrayOfString(values, N);
+        return 0;
     }
 
-    return useproxy;
+    return 1;
 }
 /* ==================================================================== */
 char *downloadFile(char *url, char *dest, char *username, char *password, char **content)
index 0b3cf36..06d5869 100644 (file)
@@ -2,5 +2,6 @@
 <web title="_(Web)">
     <body>
         <web default-browser="true" default-mailer="true" command-browser="" command-mailer=""/>
+        <proxy enabled="false" host="" port="" user="" password=""/>
     </body>
 </web>
index ffdb5d5..ace71a8 100644 (file)
 <xsl:stylesheet version ="1.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 >
-  <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
+    <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        ::
        ::     M A I N   P A N E L   :   W E B
        ::
   -->
-
-  <xsl:template match="web">
-    <Title text="_(Web browser and mailer)">
-      <Grid>
-        <Checkbox checked="{@default-browser}" selected-value="true" unselected-value="false" listener="ActionListener" text="_(Use default web browser)" gridx="1" gridy="1" fill="none" weightx="0" anchor="west">
-          <actionPerformed choose="default-browser">
-            <xsl:call-template name="context"/>
-          </actionPerformed>
-        </Checkbox>
-        <Panel gridx="2" gridy="1" weightx="1"/>
-        <Label gridx="1" gridy="2" weightx="0" text="_(Command for web browser: )"/>
-        <FileSelector gridx="2" gridy="2" weightx="1" anchor="above_baseline"
+    
+    <xsl:template match="web">
+        <Title text="_(Web browser and mailer)">
+            <Grid>
+                <Checkbox checked="{@default-browser}" selected-value="true" unselected-value="false" listener="ActionListener" text="_(Use default web browser)" gridx="1" gridy="1" fill="none" weightx="0" anchor="west">
+                    <actionPerformed choose="default-browser">
+                        <xsl:call-template name="context"/>
+                    </actionPerformed>
+                </Checkbox>
+                <Panel gridx="2" gridy="1" weightx="1"/>
+                <Label gridx="1" gridy="2" weightx="0" text="_(Command for web browser: )"/>
+                <FileSelector gridx="2" gridy="2" weightx="1" anchor="above_baseline"
                       listener="EntryListener"
                       href="{@command-browser}"
                       dir-selection="false"
                       check-entry="false">
-          <xsl:attribute name="enable">
-            <xsl:if test="@default-browser='false'">
-              <xsl:text>true</xsl:text>
-            </xsl:if>
-          </xsl:attribute>
-          <entryChanged choose="command-browser">
-            <xsl:call-template name="context"/>
-          </entryChanged>
-        </FileSelector>
-        <Label gridx="2" gridy="3" text="_((Don't forget to quote path containing white spaces))">
-          <xsl:attribute name="enable">
-            <xsl:if test="@default-browser='false'">
-              <xsl:text>true</xsl:text>
-            </xsl:if>
-          </xsl:attribute>
-        </Label>
-        <Panel height="5" gridx="1" gridy="4">
-          <VSpace height="5"/>
-        </Panel>
-        <Checkbox checked="{@default-mailer}" selected-value="true" unselected-value="false" listener="ActionListener" text="_(Use default mailer)" gridx="1" gridy="5" fill="none" weightx="0" anchor="west">
-          <actionPerformed choose="default-mailer">
-            <xsl:call-template name="context"/>
-          </actionPerformed>
-        </Checkbox>
-        <Panel gridx="2" gridy="5" weightx="1"/>
-        <Label gridx="1" gridy="6" weightx="0" text="_(Command for mailer: )"/>
-        <FileSelector gridx="2" gridy="6" weightx="1" anchor="above_baseline"
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@default-browser='false'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="command-browser">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </FileSelector>
+                <Label gridx="2" gridy="3" text="_((Don't forget to quote path containing white spaces))">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@default-browser='false'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                </Label>
+                <Panel height="5" gridx="1" gridy="4">
+                    <VSpace height="5"/>
+                </Panel>
+                <Checkbox checked="{@default-mailer}" selected-value="true" unselected-value="false" listener="ActionListener" text="_(Use default mailer)" gridx="1" gridy="5" fill="none" weightx="0" anchor="west">
+                    <actionPerformed choose="default-mailer">
+                        <xsl:call-template name="context"/>
+                    </actionPerformed>
+                </Checkbox>
+                <Panel gridx="2" gridy="5" weightx="1"/>
+                <Label gridx="1" gridy="6" weightx="0" text="_(Command for mailer: )"/>
+                <FileSelector gridx="2" gridy="6" weightx="1" anchor="above_baseline"
                       listener="EntryListener"
                       href="{@command-mailer}"
                       dir-selection="false"
                       check-entry="false">
-          <xsl:attribute name="enable">
-            <xsl:if test="@default-mailer='false'">
-              <xsl:text>true</xsl:text>
-            </xsl:if>
-          </xsl:attribute>
-          <entryChanged choose="command-mailer">
-            <xsl:call-template name="context"/>
-          </entryChanged>
-        </FileSelector>
-        <Label gridx="2" gridy="7" text="_((Don't forget to quote path containing white spaces))">
-          <xsl:attribute name="enable">
-            <xsl:if test="@default-mailer='false'">
-              <xsl:text>true</xsl:text>
-            </xsl:if>
-          </xsl:attribute>
-        </Label>
-      </Grid>
-    </Title>
-  </xsl:template>
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@default-mailer='false'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="command-mailer">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </FileSelector>
+                <Label gridx="2" gridy="7" text="_((Don't forget to quote path containing white spaces))">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@default-mailer='false'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                </Label>
+            </Grid>
+        </Title>
+        <VSpace height="10"/>
+    </xsl:template>
+    
+    <xsl:template match="proxy">
+        <Title text="_(Proxy settings)">
+            <Grid>
+                <Checkbox checked="{@enabled}" selected-value="true" unselected-value="false" listener="ActionListener" text="_(Enable proxy)" gridx="1" gridy="1" fill="none" weightx="0" anchor="west">
+                    <actionPerformed choose="enabled">
+                        <xsl:call-template name="context"/>
+                    </actionPerformed>
+                </Checkbox>
+                <Panel gridx="2" gridy="1" weightx="1"/>
+                <Label gridx="1" gridy="2" weightx="0" text="_(Host: )"/>
+                <Entry gridx="2" gridy="2" weightx="1" anchor="above_baseline"
+               listener="EntryListener"
+               text="{@host}">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@enabled='true'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="host">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </Entry>
+                
+                <Label gridx="1" gridy="3" weightx="0" text="_(Port: )"/>
+                <Entry gridx="2" gridy="3" weightx="1" anchor="above_baseline"
+               listener="EntryListener"
+               text="{@port}">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@enabled='true'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="port">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </Entry>
+                
+                <Label gridx="1" gridy="4" weightx="0" text="_(User: )"/>
+                <Entry gridx="2" gridy="4" weightx="1" anchor="above_baseline"
+               listener="EntryListener"
+               text="{@user}">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@enabled='true'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="user">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </Entry>
+                
+                <Label gridx="1" gridy="5" weightx="0" text="_(Password: )"/>
+                <Entry gridx="2" gridy="5" weightx="1" password="true" anchor="above_baseline"
+               listener="EntryListener"
+               text="{@password}">
+                    <xsl:attribute name="enable">
+                        <xsl:if test="@enabled='true'">
+                            <xsl:text>true</xsl:text>
+                        </xsl:if>
+                    </xsl:attribute>
+                    <entryChanged choose="password">
+                        <xsl:call-template name="context"/>
+                    </entryChanged>
+                </Entry>
+            </Grid>
+        </Title>
+    </xsl:template>
 </xsl:stylesheet>
index 9c346d1..e63e392 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<interface version="0.15" path="1/" width="800" height="600">
+<interface version="0.16" path="1/" width="800" height="600">
     <general/>
     <web/>
     <preference/>
index e69de29..ce89102 100644 (file)
@@ -0,0 +1,10 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises
+//
+// 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
+
+load(SCI+"/modules/preferences/macros/lib");
index f205431..c9c2837 100644 (file)
@@ -47,5 +47,7 @@ typedef struct
 PREFERENCES_SCILAB_IMPEXP const ScilabPreferences * getScilabPreferences(void);
 PREFERENCES_SCILAB_IMPEXP void reloadScilabPreferences(void);
 PREFERENCES_SCILAB_IMPEXP void clearScilabPreferences(void);
+PREFERENCES_SCILAB_IMPEXP char * getAttributeValue(const char * xpath, const char * attribute);
+PREFERENCES_SCILAB_IMPEXP char ** getAttributesValues(const char * xpath, const char ** attributes, const unsigned int attrLen);
 
 #endif // __GETSCILABPREFERENCES_H__
index 2ef3aa8..13d1ecb 100644 (file)
@@ -12,5 +12,5 @@ if (isdef("genlib") == %f) then
     exec(SCI+"/modules/functions/scripts/buildmacros/loadgenlib.sce");
 end
 
-genlib("scinoteslib","SCI/modules/scinotes/macros",%f,%t);
+genlib("preferenceslib","SCI/modules/preferences/macros",%f,%t);
 
diff --git a/scilab/modules/preferences/macros/getPrefValues.sci b/scilab/modules/preferences/macros/getPrefValues.sci
new file mode 100644 (file)
index 0000000..18b2d80
--- /dev/null
@@ -0,0 +1,78 @@
+// 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
+
+// Get preferences values
+// - xpath is something like "//web/body/proxy", the target must be a single node
+// - attributes is a matrix of strings containing the attributes names
+// - doc (optional): the prefs xml document where to get the values
+//
+function values = getPrefValues(xpath, attributes, doc)
+    rhs = argn(2);
+
+    if (rhs ~= 2 & rhs ~= 3) then
+        error(msprintf(gettext("%s: Wrong number of input arguments: %d or %d expected.\n"), "getPrefValues", 2, 3));
+    end
+
+    if type(xpath) <> 10 then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "getPrefValues", 1));
+    end
+
+    if type(attributes) <> 10 then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"), "getPrefValues", 2));
+    end
+
+    if rhs == 2 then
+        doc = xmlRead(SCIHOME + "/XConfiguration.xml");
+    elseif typeof(doc) ~= "XMLDoc" then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: A XMLDoc expected.\n"), "getPrefValues", 3));
+    end
+
+    try
+        xp = xmlXPath(doc, xpath);
+    catch
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Invalid XPath request.\n"), "getPrefValues"));
+    end
+
+    if xp.size ~= 1 then
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Invalid XPath request."), "getPrefValues"));
+    end
+
+    node = xp(1);
+    if node.type ~= "XML_ELEMENT_NODE" then
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Target node is not a XML_ELEMENT_NODE."), "getPrefValues"));
+    end
+
+    attr = node.attributes;
+    values = [];
+    for a = attributes(:)'
+        v = attr(a);
+        if v ~= [] then
+            values = [values v];
+        else
+            if rhs == 2 then
+                xmlDelete(doc);
+            end
+            error(msprintf(gettext("%s: Invalid attribute name: %s."), "getPrefValues", a));
+        end
+    end
+
+    if rhs == 2 then
+        xmlDelete(doc);
+    end
+    values = matrix(values, size(attributes));
+endfunction
diff --git a/scilab/modules/preferences/macros/setPrefValues.sci b/scilab/modules/preferences/macros/setPrefValues.sci
new file mode 100644 (file)
index 0000000..514ee93
--- /dev/null
@@ -0,0 +1,83 @@
+// 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
+
+// Set preferences values
+// - xpath is something like "//web/body/proxy", the target must be a single node
+// - kv is a matrix of strings 2xN: keys are in the first row and values in the
+//   seconde one.
+// - doc (optional): the prefs xml document where to set the values
+//                   (take care: in this case xmlWrite is not called)
+//
+function setPrefValues(xpath, kv, doc)
+    rhs = argn(2);
+
+    if (rhs ~= 2 & rhs ~= 3) then
+        error(msprintf(gettext("%s: Wrong number of input arguments: %d or %d expected.\n"), "setPrefValues", 2, 3));
+    end
+
+    if type(xpath) <> 10 then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "setPrefValues", 1));
+    end
+
+    if type(kv) <> 10 then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"), "setPrefValues", 2));
+    end
+
+    if (size(kv, "r") ~= 2) then
+        error(msprintf(gettext("%s: Wrong size for input argument #%d: a 2xN maxtrix expected.\n"), "setPrefValues", 2));
+    end
+
+    if rhs == 2 then
+        doc = xmlRead(SCIHOME + "/XConfiguration.xml");
+    elseif typeof(doc) ~= "XMLDoc" then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: A XMLDoc expected.\n"), "setPrefValues", 3));
+    end
+
+    try
+        xp = xmlXPath(doc, xpath);
+    catch
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Invalid XPath request.\n"), "setPrefValues"));
+    end
+
+    if xp.size ~= 1 then
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Invalid XPath request."), "setPrefValues"));
+    end
+
+    node = xp(1);
+    if node.type ~= "XML_ELEMENT_NODE" then
+        if rhs == 2 then
+            xmlDelete(doc);
+        end
+        error(msprintf(gettext("%s: Target node is not a XML_ELEMENT_NODE."), "setPrefValues"));
+    end
+
+    attr = node.attributes;
+    for i = 1:size(kv, "c")
+        v = attr(kv(1, i));
+        if ~isempty(v) then
+            attr(kv(1, i)) = kv(2, i);
+        else
+            if rhs == 2 then
+                xmlDelete(doc);
+            end
+            error(msprintf(gettext("%s: Invalid attribute name: %s."), "setPrefValues", kv(1, i)));
+        end
+    end
+
+    if rhs == 2 then
+        xmlWrite(doc);
+        xmlDelete(doc);
+    end
+endfunction
index 6d99c6c..1a30c8e 100644 (file)
@@ -36,6 +36,7 @@ static char * emptyAttribute = "";
 static void getPrefs();
 static char * getAttribute(xmlDocPtr doc, xmlXPathContextPtr xpathCtxt, const char * xpath);
 static void initPrefs();
+static void getDocAndCtxt(xmlDocPtr * doc, xmlXPathContextPtr * xpathCtxt);
 /*--------------------------------------------------------------------------*/
 const ScilabPreferences * getScilabPreferences()
 {
@@ -124,66 +125,33 @@ void clearScilabPreferences()
 /*--------------------------------------------------------------------------*/
 void getPrefs()
 {
-    xmlDocPtr doc = NULL;
-    xmlXPathContextPtr xpathCtxt = NULL;
-    char * SCIHOME = NULL;
-    char * path = NULL;
-    BOOL bConvert = FALSE;
-    char * shortfilename_xml_conf = NULL;
-    char * attr = NULL;
-
     if (!isInit)
     {
-        initPrefs();
-
-        SCIHOME = getSCIHOME();
-        path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
+        xmlDocPtr doc = NULL;
+        xmlXPathContextPtr xpathCtxt = NULL;
 
-        sprintf(path, XCONF, SCIHOME);
-        FREE(SCIHOME);
-
-        if (FileExist(path))
-        {
-            shortfilename_xml_conf = getshortpathname(path, &bConvert);
-            if (shortfilename_xml_conf)
-            {
-                doc = xmlParseFile(shortfilename_xml_conf);
-                FREE(shortfilename_xml_conf);
-                shortfilename_xml_conf = NULL;
-            }
-            FREE(path);
-            path = NULL;
-        }
+        initPrefs();
 
+        getDocAndCtxt(&doc, &xpathCtxt);
         if (doc == NULL)
         {
             return;
         }
 
-        if (stricmp(doc->encoding, "utf-8"))
-        {
-            return;
-        }
-
-        xpathCtxt = xmlXPathNewContext(doc);
+        scilabPref.heapSize = strdup(getAttribute(doc, xpathCtxt, HEAPSIZE_XPATH));
+        scilabPref.adaptToDisplay = strdup(getAttribute(doc, xpathCtxt, ADAPTTODISPLAY_XPATH));
+        scilabPref.columnsToDisplay = strdup(getAttribute(doc, xpathCtxt, COLUMNSTODISPLAY_XPATH));
+        scilabPref.linesToDisplay = strdup(getAttribute(doc, xpathCtxt, LINESTODISPLAY_XPATH));
+        scilabPref.historySaveAfter = strdup(getAttribute(doc, xpathCtxt, HISTORYSAVEAFTER_XPATH));
+        scilabPref.historyFile = strdup(getAttribute(doc, xpathCtxt, HISTORYFILE_XPATH));
+        scilabPref.historyLines = strdup(getAttribute(doc, xpathCtxt, HISTORYLINES_XPATH));
+        scilabPref.historyEnable = strdup(getAttribute(doc, xpathCtxt, HISTORYENABLE_XPATH));
+        scilabPref.ieee = strdup(getAttribute(doc, xpathCtxt, IEEE_XPATH));
+        scilabPref.format = strdup(getAttribute(doc, xpathCtxt, FORMAT_XPATH));
+        scilabPref.formatWidth = strdup(getAttribute(doc, xpathCtxt, FORMATWIDTH_XPATH));
+        scilabPref.language = strdup(getAttribute(doc, xpathCtxt, LANGUAGE_XPATH));
 
-        if (xpathCtxt)
-        {
-            scilabPref.heapSize = strdup(getAttribute(doc, xpathCtxt, HEAPSIZE_XPATH));
-            scilabPref.adaptToDisplay = strdup(getAttribute(doc, xpathCtxt, ADAPTTODISPLAY_XPATH));
-            scilabPref.columnsToDisplay = strdup(getAttribute(doc, xpathCtxt, COLUMNSTODISPLAY_XPATH));
-            scilabPref.linesToDisplay = strdup(getAttribute(doc, xpathCtxt, LINESTODISPLAY_XPATH));
-            scilabPref.historySaveAfter = strdup(getAttribute(doc, xpathCtxt, HISTORYSAVEAFTER_XPATH));
-            scilabPref.historyFile = strdup(getAttribute(doc, xpathCtxt, HISTORYFILE_XPATH));
-            scilabPref.historyLines = strdup(getAttribute(doc, xpathCtxt, HISTORYLINES_XPATH));
-            scilabPref.historyEnable = strdup(getAttribute(doc, xpathCtxt, HISTORYENABLE_XPATH));
-            scilabPref.ieee = strdup(getAttribute(doc, xpathCtxt, IEEE_XPATH));
-            scilabPref.format = strdup(getAttribute(doc, xpathCtxt, FORMAT_XPATH));
-            scilabPref.formatWidth = strdup(getAttribute(doc, xpathCtxt, FORMATWIDTH_XPATH));
-            scilabPref.language = strdup(getAttribute(doc, xpathCtxt, LANGUAGE_XPATH));
-
-            xmlXPathFreeContext(xpathCtxt);
-        }
+        xmlXPathFreeContext(xpathCtxt);
         xmlFreeDoc(doc);
 
         isInit = 1;
@@ -207,3 +175,143 @@ char * getAttribute(xmlDocPtr doc, xmlXPathContextPtr xpathCtxt, const char * xp
     return value;
 }
 /*--------------------------------------------------------------------------*/
+void getDocAndCtxt(xmlDocPtr * doc, xmlXPathContextPtr * xpathCtxt)
+{
+    char * SCIHOME = NULL;
+    char * path = NULL;
+    BOOL bConvert = FALSE;
+    char * shortfilename_xml_conf = NULL;
+    char * ret = NULL;
+
+    SCIHOME = getSCIHOME();
+    path = (char *)MALLOC(strlen(SCIHOME) + strlen(XCONF));
+
+    sprintf(path, XCONF, SCIHOME);
+    FREE(SCIHOME);
+
+    if (FileExist(path))
+    {
+        shortfilename_xml_conf = getshortpathname(path, &bConvert);
+        if (shortfilename_xml_conf)
+        {
+            *doc = xmlParseFile(shortfilename_xml_conf);
+            FREE(shortfilename_xml_conf);
+            shortfilename_xml_conf = NULL;
+        }
+        FREE(path);
+        path = NULL;
+    }
+
+    if (*doc == NULL)
+    {
+        *xpathCtxt = NULL;
+        return;
+    }
+
+    if (stricmp((*doc)->encoding, "utf-8"))
+    {
+        xmlFreeDoc(*doc);
+        *doc = NULL;
+        *xpathCtxt = NULL;
+        return;
+    }
+
+    *xpathCtxt = xmlXPathNewContext(*doc);
+    if (*xpathCtxt == NULL)
+    {
+        xmlFreeDoc(*doc);
+        *doc = NULL;
+    }
+}
+/*--------------------------------------------------------------------------*/
+char * getAttributeValue(const char * xpath, const char * attribute)
+{
+    xmlDocPtr doc = NULL;
+    xmlXPathContextPtr xpathCtxt = NULL;
+    char * ret = NULL;
+
+    if (!xpath || !attribute)
+    {
+        return NULL;
+    }
+
+    getDocAndCtxt(&doc, &xpathCtxt);
+    if (doc == NULL)
+    {
+        return NULL;
+    }
+
+    const unsigned int xlen = strlen(xpath);
+    const unsigned int alen = strlen(attribute);
+    char * query = (char *)MALLOC((xlen + alen + 2 + 1) * sizeof(char));
+    if (query)
+    {
+        sprintf(query, "%s/@%s", xpath, attribute);
+        query[xlen + alen + 2] = '\0';
+
+        ret = strdup(getAttribute(doc, xpathCtxt, (const xmlChar*)query));
+        FREE(query);
+    }
+
+    xmlXPathFreeContext(xpathCtxt);
+    xmlFreeDoc(doc);
+
+    return ret;
+}
+/*--------------------------------------------------------------------------*/
+char ** getAttributesValues(const char * xpath, const char ** attributes, const unsigned int attrLen)
+{
+    xmlDocPtr doc = NULL;
+    xmlXPathContextPtr xpathCtxt = NULL;
+    xmlXPathObjectPtr xpathObj = NULL;
+    char ** ret = NULL;
+
+    if (!xpath || !attributes)
+    {
+        return NULL;
+    }
+
+    getDocAndCtxt(&doc, &xpathCtxt);
+    if (doc == NULL)
+    {
+        return NULL;
+    }
+
+    const unsigned int xlen = strlen(xpath);
+    ret = (char**)MALLOC(sizeof(char*) * attrLen);
+    xpathObj = xmlXPathEval((const xmlChar*)xpath, xpathCtxt);
+    if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeMax)
+    {
+        int i;
+        xmlNode * node = (xmlAttrPtr)xpathObj->nodesetval->nodeTab[0];
+        for (i = 0; i < attrLen; i++)
+        {
+            xmlAttr * attrs = xmlHasProp(node, (const xmlChar *)attributes[i]);
+            if (attrs)
+            {
+                ret[i] = strdup((const char *)attrs->children->content);
+            }
+            else
+            {
+                int j;
+                for (j = 0; j < i; j++)
+                {
+                    free(ret[j]);
+                }
+                FREE(ret);
+                ret = NULL;
+                break;
+            }
+        }
+    }
+
+    if (xpathObj)
+    {
+        xmlXPathFreeObject(xpathObj);
+    }
+
+    xmlXPathFreeContext(xpathCtxt);
+    xmlFreeDoc(doc);
+
+    return ret;
+}
index 526693b..69f71cc 100644 (file)
@@ -12,7 +12,7 @@
 
 package org.scilab.modules.preferences.Component;
 
-import javax.swing.JTextField;
+import javax.swing.JPasswordField;
 
 import org.scilab.modules.preferences.XCommonManager;
 import org.scilab.modules.preferences.XComponent;
@@ -27,7 +27,7 @@ import org.w3c.dom.Node;
  * @author Pierre GRADIT
  *
  */
-public class Entry extends JTextField implements XComponent, XChooser {
+public class Entry extends JPasswordField implements XComponent, XChooser {
 
     /** Universal identifier for serialization.
      *
@@ -67,6 +67,11 @@ public class Entry extends JTextField implements XComponent, XChooser {
         }
         String enable = XConfigManager.getAttribute(peer, "enable", "true");
         setEnabled(enable.equals("true"));
+
+        String passwd = XConfigManager.getAttribute(peer, "password", "false");
+        if (!"true".equals(passwd)) {
+            setEchoChar('\0');
+        }
     }
 
     /** Sensor for 'text' attribute.
@@ -74,7 +79,7 @@ public class Entry extends JTextField implements XComponent, XChooser {
      * @return the attribute value.
      */
     public final String text() {
-        return getText();
+        return new String(getPassword());
     }
 
     /** Actuator for 'text' attribute.