Search in sources :

Example 6 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project phoenicis by PhoenicisOrg.

the class ShortcutManager method updateShortcut.

public void updateShortcut(ShortcutDTO shortcutDTO) {
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectory = new File(this.shortcutDirectory);
    // backup icon if it didn't change (deleteShortcut will delete it -> icon lost after shortcut update)
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File iconBackup = new File(shortcutDirectory, baseName + ".icon_backup");
    final URI shortcutIcon = shortcutDTO.getIcon();
    if (shortcutIcon != null && shortcutIcon.getPath() != null) {
        final boolean keepIcon = shortcutIcon.getPath().equals(iconFile.getPath());
        if (keepIcon) {
            try {
                Files.move(iconFile.toPath(), iconBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withIcon(iconBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup icon.", e);
            }
        }
    }
    // backup miniature if it didn't change (deleteShortcut will delete it -> miniature lost after shortcut update)
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final File miniatureBackup = new File(shortcutDirectory, baseName + ".miniature_backup");
    final URI shortcutMiniature = shortcutDTO.getMiniature();
    if (shortcutMiniature != null && shortcutMiniature.getPath() != null) {
        final boolean keepMiniature = shortcutMiniature.getPath().equals(miniatureFile.getPath());
        if (keepMiniature) {
            try {
                Files.move(miniatureFile.toPath(), miniatureBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withMiniature(miniatureBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup miniature.", e);
            }
        }
    }
    deleteShortcut(shortcutDTO);
    createShortcut(shortcutDTO);
    // delete backups
    if (iconBackup.exists()) {
        iconBackup.delete();
    }
    if (miniatureBackup.exists()) {
        miniatureBackup.delete();
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO)

Example 7 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project phoenicis by PhoenicisOrg.

the class ShortcutRunner method run.

public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"Wine\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
        final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
        shortcutReader.callMember("of", shortcutDTO);
        shortcutReader.callMember("run", arguments);
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) List(java.util.List) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 8 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project phoenicis by PhoenicisOrg.

the class ShortcutRunner method stop.

public void stop(ShortcutDTO shortcutDTO, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"Wine\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
        final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
        shortcutReader.callMember("of", shortcutDTO);
        shortcutReader.callMember("stop");
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) List(java.util.List) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 9 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project phoenicis by PhoenicisOrg.

the class ListWidgetEntry method create.

public static ListWidgetEntry<ContainerDTO> create(ContainerDTO container) {
    final List<BufferedImage> miniatures = new ArrayList<>();
    // do not use too many segments (cannot recognize the miniature if the segment is too small)
    final int maxSegments = 4;
    int currentSegment = 0;
    for (ShortcutDTO shortcutDTO : container.getInstalledShortcuts()) {
        if (currentSegment >= maxSegments) {
            break;
        }
        try {
            miniatures.add(ImageIO.read(shortcutDTO.getMiniature().toURL()));
            currentSegment++;
        } catch (IOException e) {
            LOGGER.warn(String.format("Could not read miniature for shortcut \"%s\"", shortcutDTO.getInfo().getName()), e);
        }
    }
    final BufferedImage segmentedMiniature = createSegmentedMiniature(miniatures);
    final Optional<URI> shortcutMiniature = saveBufferedImage(segmentedMiniature, container.getName());
    return new ListWidgetEntry<>(container, shortcutMiniature, CONTAINER_MINIATURE, container.getName(), Optional.empty(), Optional.empty());
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) BufferedImage(java.awt.image.BufferedImage) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO)

Example 10 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PlayOnLinux.

the class GenericContainersManager method fetchContainers.

/**
 * fetches all containers in a given directory
 *
 * @param directory
 * @return found containers
 */
private List<ContainerDTO> fetchContainers(File directory) {
    final List<ContainerDTO> containers = new ArrayList<>();
    final File[] containerDirectories = directory.listFiles();
    if (containerDirectories != null) {
        for (File containerDirectory : containerDirectories) {
            if (!containerDirectory.isHidden()) {
                final ConfigFile configFile = compatibleConfigFileFormatFactory.open(new File(containerDirectory, "phoenicis.cfg"));
                final String containerPath = containerDirectory.getAbsolutePath();
                final String containerName = containerPath.substring(containerPath.lastIndexOf('/') + 1);
                // find shortcuts which use this container
                List<ShortcutDTO> shortcutDTOS = libraryManager.fetchShortcuts().stream().flatMap(shortcutCategory -> shortcutCategory.getShortcuts().stream()).filter(shortcut -> {
                    boolean toAdd = false;
                    try {
                        final Map<String, Object> shortcutProperties = objectMapper.readValue(shortcut.getScript(), new TypeReference<Map<String, Object>>() {
                        });
                        toAdd = shortcutProperties.get("winePrefix").equals(containerName);
                    } catch (IOException e) {
                        LOGGER.warn("Could not parse shortcut script JSON", e);
                    }
                    return toAdd;
                }).collect(Collectors.toList());
                if (directory.getName().equals("wineprefix")) {
                    containers.add(new WinePrefixContainerDTO.Builder().withName(containerDirectory.getName()).withPath(containerPath).withInstalledShortcuts(shortcutDTOS).withArchitecture(configFile.readValue("wineArchitecture", "")).withDistribution(configFile.readValue("wineDistribution", "")).withVersion(configFile.readValue("wineVersion", "")).build());
                }
            }
        }
        containers.sort(ContainerDTO.nameComparator());
    }
    return containers;
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) ContainerCategoryDTO(org.phoenicis.containers.dto.ContainerCategoryDTO) LoggerFactory(org.slf4j.LoggerFactory) ShortcutReader(org.phoenicis.library.ShortcutReader) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Map(java.util.Map) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) InteractiveScriptSession(org.phoenicis.scripts.session.InteractiveScriptSession) Consumer(java.util.function.Consumer) List(java.util.List) ShortcutManager(org.phoenicis.library.ShortcutManager) ConfigFile(org.phoenicis.tools.config.ConfigFile) Safe(org.phoenicis.configuration.security.Safe) CompatibleConfigFileFormatFactory(org.phoenicis.tools.config.CompatibleConfigFileFormatFactory) CollectionUtils(org.springframework.util.CollectionUtils) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) LibraryManager(org.phoenicis.library.LibraryManager) Collections(java.util.Collections) ConfigFile(org.phoenicis.tools.config.ConfigFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) File(java.io.File) ConfigFile(org.phoenicis.tools.config.ConfigFile) Map(java.util.Map)

Aggregations

ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)30 File (java.io.File)18 IOException (java.io.IOException)17 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 URI (java.net.URI)12 ShortcutCategoryDTO (org.phoenicis.library.dto.ShortcutCategoryDTO)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Consumer (java.util.function.Consumer)8 List (java.util.List)7 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Observable (javafx.beans.Observable)6 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)6 Safe (org.phoenicis.configuration.security.Safe)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 ContainerCategoryDTO (org.phoenicis.containers.dto.ContainerCategoryDTO)5 ContainerDTO (org.phoenicis.containers.dto.ContainerDTO)5