1## Copyright (C) 2016-2020 Carnë Draug <carandraug@octave.org>
2## Copyright (C) 2011-2021 Philip Nienhuis
3##
4## This program is free software; you can redistribute it and/or
5## modify it under the terms of the GNU General Public License as
6## published by the Free Software Foundation; either version 3 of the
7## License, or (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful, but
10## WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12## General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, see
16## <http:##www.gnu.org/licenses/>.
17
18## -*- texinfo -*-
19## @deftypefn {} {} __enter_io_package__ ()
20## Undocumented internal function of io package.
21##
22## Search io pkg java jars and add found ones to javaclasspath.
23##
24## @end deftypefn
25
26## PKG_ADD: __init_io__ ()
27
28function __init_io__ ()
29  ## First check if Java support is available
30  HAVE_JAVA = isempty (javachk ("jvm"));
31  if (HAVE_JAVA)
32    ## OK, Java built-in / supported. Check environment var
33    userdir = getenv ("OCTAVE_IO_JAVALIBS");
34    if (ispc)
35      homedir = getenv ("USERPROFILE");
36      # (MinGW) assume jar files are in /lib/java
37      libdir = fullfile (OCTAVE_HOME, "lib");
38  # elseif (ismac)
39  #   ## Who knows where OSX keeps e.g., Apache POI stuff? if it does at all...
40    elseif (isunix)
41      homedir = tilde_expand ("~");
42      ## On linux, spreadsheet .jars are often found somewhere in /usr/share/java
43      libdir = "/usr/share";
44    else
45      ## Set libdir to "." to avoid searching in a root dir
46      libdir = ".";
47    endif
48
49    ## Do we have a 64-bit indexing Octave at hand?
50    ## (thanks to Markus Muetzel for the suggestion to try MAXSIZE)
51    [~, maxsize] = computer ();
52    amd64y = maxsize > 2^31 - 1;
53
54    ## Find LibreOffice or OpenOffice.org
55    ooopath = "";
56    ## Possible locations for  OOo or LO.
57    bnam = {"C:/Program Files (X86)", ...
58                 "C:/Program Files", ...
59                 "C:/Programs", ...
60                 "/opt", ...
61                 "/usr/lib"};
62    if (amd64y)
63      ## 64-bit indexing Octave won't work with 32-bit LibreOffice/OpenOffice.org
64      bnam(1) = [];
65    endif
66    ii = 0;
67    while (isempty (ooopath) && ii < numel (bnam))
68      ooopath = glob ([ bnam{++ii} "/[Ll]ibre[Oo]ffice*"]);
69      ## Watch out for uninstalled previous LO installations that just keep prefs
70      if (! isempty (ooopath) && ! (exist ([ooopath{1} filesep "program"]) == 7))
71        ooopath = "";
72      endif
73    endwhile
74    ii = 0;
75    while (isempty (ooopath) && ii < numel (bnam))
76      ooopath = glob ([ bnam{++ii} "/[Oo]pen[Oo]ffice.org*"]);
77      ## Watch out for uninstalled previous OOo installations that just keep prefs
78      if (! isempty (ooopath) && ! (exist ([ooopath{1} filesep "program"]) == 7))
79        ooopath = "";
80      endif
81    endwhile
82    ii = 0;
83    while (isempty (ooopath) && ii < numel (bnam))
84      ooopath = glob ([ bnam{++ii} "/ooo*"]);
85      ## Watch out for uninstalled previous OOo installations that just keep prefs
86      if (! isempty (ooopath) && ! (exist ([ooopath{1} filesep "program"]) == 7))
87        ooopath = "";
88      endif
89    endwhile
90    if (! isempty (ooopath))
91      ooopath = ooopath{:};
92    else
93      ooopath = "";
94    endif
95
96    ## One big try-catch to circumvent possible problems on Linux
97    try
98      if (! isempty (userdir))
99        if (strcmpi (userdir, "no") || strcmpi (userdir, "false") || strcmpi (userdir, "0"))
100          ## Do not load Java class libs .jar files). First clean up, then return
101          clear libdir spr_status userdir homedir bnam ooopath ii;
102          return
103        endif
104        ## First allow some time for io package to be fully loaded
105        pause (0.25);
106        ## Check first for user-, then system supplied jars
107        if (exist (userdir) == 7)
108          ## Userdir is a subdir
109          spr_status = chk_spreadsheet_support (userdir, 0, ooopath);
110        endif
111        ## Also try user's home directory
112      elseif (isunix && ...
113        ! (strcmpi (userdir, "no") || strcmpi (userdir, "false") || strcmpi (userdir, "0")))
114        ## On non-Windows systems, automatic loading of Java classes is opt-in due to
115        ## excessive search time (see bug #42044). Most of the delay is due to searching
116        ## for the Libre/OpenOffice.org jars
117        clear libdir spr_status userdir homedir bnam ooopath ii HAVE_JAVA amd64y;
118        return
119      else
120        ## Allow some time for io package to be fully loaded
121        pause (0.25);
122      endif
123      ## Try <HOME>/java
124      spr_status = chk_spreadsheet_support ([ homedir filesep "java" ], 0, ooopath);
125      ## Only then search for system-supplied jars. ooopath has been searched
126      spr_status = chk_spreadsheet_support ([ libdir filesep "java" ], 0);
127    catch
128      warning ("(Automatic loading of spreadsheet I/O Java classlibs failed)\n");
129    end_try_catch
130  endif
131
132endfunction
133