OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 180
Line 19... Line 19...
19
import java.io.File;
19
import java.io.File;
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.InputStream;
21
import java.io.InputStream;
22
import java.lang.reflect.Method;
22
import java.lang.reflect.Method;
23
import java.nio.charset.Charset;
23
import java.nio.charset.Charset;
-
 
24
import java.util.concurrent.FutureTask;
-
 
25
import java.util.concurrent.RunnableFuture;
24
import java.util.logging.Level;
26
import java.util.logging.Level;
25
import java.util.regex.Matcher;
27
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
28
import java.util.regex.Pattern;
27
 
29
 
-
 
30
import javax.swing.SwingUtilities;
28
import javax.swing.filechooser.FileSystemView;
31
import javax.swing.filechooser.FileSystemView;
29
import javax.xml.parsers.DocumentBuilder;
32
import javax.xml.parsers.DocumentBuilder;
30
import javax.xml.parsers.DocumentBuilderFactory;
33
import javax.xml.parsers.DocumentBuilderFactory;
31
 
34
 
32
import org.w3c.dom.Element;
35
import org.w3c.dom.Element;
33
import org.w3c.dom.NodeList;
36
import org.w3c.dom.NodeList;
34
 
37
 
-
 
38
import net.jcip.annotations.GuardedBy;
-
 
39
import net.jcip.annotations.ThreadSafe;
-
 
40
 
35
/**
41
/**
36
 * A desktop environment like Gnome or MacOS.
42
 * A desktop environment like Gnome or MacOS.
37
 * 
43
 * 
38
 * @author Sylvain CUAZ
44
 * @author Sylvain CUAZ
39
 * @see #getDE()
45
 * @see #getDE()
40
 */
46
 */
-
 
47
@ThreadSafe
41
public abstract class DesktopEnvironment {
48
public abstract class DesktopEnvironment {
42
 
49
 
43
    static public final class Gnome extends DesktopEnvironment {
50
    static public final class Gnome extends DesktopEnvironment {
44
 
51
 
45
        static private final String getTextContent(final Element parentElem, final String childName) {
52
        static private final String getTextContent(final Element parentElem, final String childName) {
Line 210... Line 217...
210
    }
217
    }
211
 
218
 
212
    static public final class Mac extends DEisOS {
219
    static public final class Mac extends DEisOS {
213
 
220
 
214
        // From CarbonCore/Folders.h
221
        // From CarbonCore/Folders.h
215
        private static final String kDocumentsDirectory = "docs";
222
        public static final String kDocumentsDirectory = "docs";
216
        private static final String kPreferencesDirectory = "pref";
223
        public static final String kPreferencesDirectory = "pref";
217
        private static Class<?> FileManagerClass;
224
        private static Class<?> FileManagerClass;
218
        private static Short kUserDomain;
225
        private static Short kUserDomain;
219
        private static Method OSTypeToInt;
226
        private static Method OSTypeToInt;
220
 
227
 
221
        private static Class<?> getFileManagerClass() {
228
        private static Class<?> getFileManagerClass() {
Line 233... Line 240...
233
            return FileManagerClass;
240
            return FileManagerClass;
234
        }
241
        }
235
 
242
 
236
        @Override
243
        @Override
237
        public File getDocumentsFolder() {
244
        public File getDocumentsFolder() {
238
            return getFolder(kDocumentsDirectory);
-
 
239
        }
-
 
240
 
-
 
241
        @Override
-
 
242
        public File getPreferencesFolder(String appName) {
-
 
243
            return new File(getFolder(kPreferencesDirectory), appName);
245
            return new File(System.getProperty("user.home"), "Documents/");
244
        }
246
        }
245
 
247
 
-
 
248
        // There was a warning until JRE v16, now "--add-exports
-
 
249
        // java.desktop/com.apple.eio=ALL-UNNAMED" is needed. Further this needs the EDT which is
-
 
250
        // undesirable just to get some paths.
-
 
251
        // https://bugs.openjdk.java.net/browse/JDK-8187981
-
 
252
        @Deprecated
246
        public File getFolder(String type) {
253
        public File getFolder(String type) {
247
            try {
254
            try {
248
                final Method findFolder = getFileManagerClass().getMethod("findFolder", Short.TYPE, Integer.TYPE);
255
                final Method findFolder = getFileManagerClass().getMethod("findFolder", Short.TYPE, Integer.TYPE);
249
                final String path = (String) findFolder.invoke(null, kUserDomain, OSTypeToInt.invoke(null, type));
256
                final RunnableFuture<String> f = new FutureTask<>(() -> (String) findFolder.invoke(null, kUserDomain, OSTypeToInt.invoke(null, type)));
-
 
257
                // EDT needed at least for JRE v14-16 on macOS 10.15, otherwise the VM crashes :
-
 
258
                // Problematic frame: __NSAssertMainEventQueueIsCurrentEventQueue_block_invoke or
-
 
259
                // "The current event queue and the main event queue are not the same. This is
-
 
260
                // probably because _TSGetMainThread was called for the first time off the main
-
 
261
                // thread."
-
 
262
                if (SwingUtilities.isEventDispatchThread())
-
 
263
                    f.run();
-
 
264
                else
-
 
265
                    SwingUtilities.invokeLater(f);
-
 
266
                final String path = f.get();
250
                return new File(path);
267
                return new File(path);
251
            } catch (RuntimeException e) {
268
            } catch (RuntimeException e) {
252
                throw e;
269
                throw e;
253
            } catch (Exception e) {
270
            } catch (Exception e) {
254
                throw new IllegalStateException(e);
271
                throw new IllegalStateException(e);
Line 344... Line 361...
344
                return new Gnome(de);
361
                return new Gnome(de);
345
        }
362
        }
346
        return new Unknown();
363
        return new Unknown();
347
    }
364
    }
348
 
365
 
-
 
366
    @GuardedBy("DesktopEnvironment.class")
349
    private static DesktopEnvironment DE = null;
367
    private static DesktopEnvironment DE = null;
350
 
368
 
351
    public static final DesktopEnvironment getDE() {
369
    public synchronized static final DesktopEnvironment getDE() {
352
        if (DE == null) {
370
        if (DE == null) {
353
            DE = detectDE();
371
            DE = detectDE();
354
        }
372
        }
355
        return DE;
373
        return DE;
356
    }
374
    }
357
 
375
 
358
    public static final void resetDE() {
376
    public synchronized static final void resetDE() {
359
        DE = null;
377
        DE = null;
360
    }
378
    }
361
 
379
 
-
 
380
    @GuardedBy("this")
362
    private String version;
381
    private String version;
363
 
382
 
364
    private DesktopEnvironment() {
383
    private DesktopEnvironment() {
365
        this.version = null;
384
        this.version = null;
366
    }
385
    }
367
 
386
 
368
    protected abstract String findVersion();
387
    protected abstract String findVersion();
369
 
388
 
370
    public final String getVersion() {
389
    public synchronized final String getVersion() {
371
        if (this.version == null)
390
        if (this.version == null)
372
            this.version = this.findVersion();
391
            this.version = this.findVersion();
373
        return this.version;
392
        return this.version;
374
    }
393
    }
375
 
394
 
376
    // where to write user-visible files (e.g. spreadsheets, exports)
395
    // where to write user-visible files (e.g. spreadsheets, exports)
377
    public File getDocumentsFolder() {
396
    public File getDocumentsFolder() {
378
        return FileSystemView.getFileSystemView().getDefaultDirectory();
397
        return FileSystemView.getFileSystemView().getDefaultDirectory();
379
    }
398
    }
380
 
399
 
381
    /**
-
 
382
     * Where the configuration files are stored.
-
 
383
     * 
-
 
384
     * @param appName the name of application.
-
 
385
     * @return the preferences folder.
-
 
386
     * @deprecated kept around to migrate existing files, but otherwise use {@link BaseDirs}.
-
 
387
     */
-
 
388
    public File getPreferencesFolder(final String appName) {
-
 
389
        return new File(System.getProperty("user.home"), "." + appName);
-
 
390
    }
-
 
391
 
-
 
392
    // on some systems arguments are not passed correctly by ProcessBuilder
400
    // on some systems arguments are not passed correctly by ProcessBuilder
393
    public String quoteParamForExec(String s) {
401
    public String quoteParamForExec(String s) {
394
        return s;
402
        return s;
395
    }
403
    }
396
 
404