Search in sources :

Example 1 with ShortcutInfoDTO

use of org.phoenicis.library.dto.ShortcutInfoDTO in project POL-POM-5 by PhoenicisOrg.

the class LibraryManager method fetchShortcutDTO.

private ShortcutDTO fetchShortcutDTO(File shortcutDirectory, File file) {
    final String baseName = FilenameUtils.getBaseName(file.getName());
    final File infoFile = new File(shortcutDirectory, baseName + ".info");
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final ShortcutInfoDTO.Builder shortcutInfoDTOBuilder;
    if (infoFile.exists()) {
        final ShortcutInfoDTO shortcutInfoDTOFromJsonFile = unSerializeShortcutInfo(infoFile);
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder(shortcutInfoDTOFromJsonFile);
    } else {
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder();
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getName())) {
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getCategory())) {
        shortcutInfoDTOBuilder.withCategory("Other");
    }
    final ShortcutInfoDTO shortcutInfoDTO = shortcutInfoDTOBuilder.build();
    try {
        final URI icon = iconFile.exists() ? iconFile.toURI() : getClass().getResource("phoenicis.png").toURI();
        final URI miniature = miniatureFile.exists() ? miniatureFile.toURI() : getClass().getResource("defaultMiniature.png").toURI();
        return new ShortcutDTO.Builder().withId(baseName).withInfo(shortcutInfoDTO).withScript(IOUtils.toString(new FileInputStream(file), "UTF-8")).withIcon(icon).withMiniature(miniature).build();
    } catch (URISyntaxException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream)

Example 2 with ShortcutInfoDTO

use of org.phoenicis.library.dto.ShortcutInfoDTO in project POL-POM-5 by PhoenicisOrg.

the class ShortcutManager method createShortcut.

public void createShortcut(ShortcutDTO shortcutDTO) {
    final ShortcutInfoDTO shortcutInfo = shortcutDTO.getInfo();
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectoryFile = new File(this.shortcutDirectory);
    final File infoFile = new File(shortcutDirectoryFile, baseName + ".info");
    final File scriptFile = new File(shortcutDirectoryFile, baseName + ".shortcut");
    final File iconFile = new File(shortcutDirectoryFile, baseName + ".icon");
    final File miniatureFile = new File(shortcutDirectoryFile, baseName + ".miniature");
    if (!shortcutDirectoryFile.exists()) {
        shortcutDirectoryFile.mkdirs();
    }
    try {
        this.objectMapper.writeValue(infoFile, shortcutInfo);
        FileUtils.writeStringToFile(scriptFile, shortcutDTO.getScript(), ENCODING);
        try {
            if (shortcutDTO.getIcon() != null) {
                File file = new File(shortcutDTO.getIcon());
                if (file.exists()) {
                    FileUtils.copyFile(file, iconFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating shortcut icon", e);
        }
        try {
            if (shortcutDTO.getMiniature() != null) {
                File file = new File(shortcutDTO.getMiniature());
                if (file.exists()) {
                    FileUtils.copyFile(file, miniatureFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating miniature", e);
        }
    } catch (IOException e) {
        LOGGER.warn("Error while creating shortcut", e);
    } finally {
        libraryManager.refresh();
    }
    if (this.desktopShortcutDirectory != null) {
        final File desktopShortcutDirectoryFile = new File(this.desktopShortcutDirectory);
        final File desktopShortcutFile = new File(desktopShortcutDirectoryFile, baseName + ".desktop");
        try {
            final String content = "[Desktop Entry]\n" + "Name=" + shortcutInfo.getName() + "\n" + "Type=Application\n" + "Icon=" + miniatureFile.getAbsolutePath() + "\n" + "Exec=phoenicis-cli -run \"" + shortcutInfo.getName() + "\"";
            FileUtils.writeStringToFile(desktopShortcutFile, content, ENCODING);
        } catch (IOException e) {
            LOGGER.warn("Error while creating .desktop", e);
        }
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) IOException(java.io.IOException) File(java.io.File)

Example 3 with ShortcutInfoDTO

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

the class ShortcutManager method createShortcut.

public void createShortcut(ShortcutDTO shortcutDTO) {
    final ShortcutInfoDTO shortcutInfo = shortcutDTO.getInfo();
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectoryFile = new File(this.shortcutDirectory);
    final File infoFile = new File(shortcutDirectoryFile, baseName + ".info");
    final File scriptFile = new File(shortcutDirectoryFile, baseName + ".shortcut");
    final File iconFile = new File(shortcutDirectoryFile, baseName + ".icon");
    final File categoryIconFile = new File(shortcutDirectoryFile, baseName + "Category.icon");
    final File miniatureFile = new File(shortcutDirectoryFile, baseName + ".miniature");
    if (!shortcutDirectoryFile.exists()) {
        shortcutDirectoryFile.mkdirs();
    }
    try {
        this.objectMapper.writeValue(infoFile, shortcutInfo);
        FileUtils.writeStringToFile(scriptFile, shortcutDTO.getScript(), ENCODING);
        try {
            if (shortcutDTO.getIcon() != null) {
                File file = createFileWithFallback(shortcutDTO.getIcon(), "phoenicis.png");
                if (file.exists()) {
                    FileUtils.copyFile(file, iconFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating shortcut icon", e);
        }
        try {
            if (shortcutDTO.getCategoryIcon() != null) {
                File file = createFileWithFallback(shortcutDTO.getCategoryIcon(), "phoenicis.png");
                if (file.exists()) {
                    FileUtils.copyFile(file, categoryIconFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating shortcut category icon", e);
        }
        try {
            if (shortcutDTO.getMiniature() != null) {
                File file = createFileWithFallback(shortcutDTO.getMiniature(), "defaultMiniature.png");
                if (file.exists()) {
                    FileUtils.copyFile(file, miniatureFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating miniature", e);
        }
    } catch (IOException e) {
        LOGGER.warn("Error while creating shortcut", e);
    } finally {
        libraryManager.refresh();
    }
    if (this.desktopShortcutDirectory != null) {
        final File desktopShortcutDirectoryFile = new File(this.desktopShortcutDirectory);
        final File desktopShortcutFile = new File(desktopShortcutDirectoryFile, baseName + ".desktop");
        try {
            final String content = "[Desktop Entry]\n" + "Name=" + shortcutInfo.getName() + "\n" + "Type=Application\n" + "Icon=" + miniatureFile.getAbsolutePath() + "\n" + "Exec=phoenicis-cli -run \"" + shortcutInfo.getName() + "\"";
            FileUtils.writeStringToFile(desktopShortcutFile, content, ENCODING);
        } catch (IOException e) {
            LOGGER.warn("Error while creating .desktop", e);
        }
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) IOException(java.io.IOException) File(java.io.File)

Example 4 with ShortcutInfoDTO

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

the class LibraryManager method fetchShortcutDTO.

private ShortcutDTO fetchShortcutDTO(File shortcutDirectory, File file) {
    final String baseName = FilenameUtils.getBaseName(file.getName());
    final File infoFile = new File(shortcutDirectory, baseName + ".info");
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final ShortcutInfoDTO.Builder shortcutInfoDTOBuilder;
    if (infoFile.exists()) {
        final ShortcutInfoDTO shortcutInfoDTOFromJsonFile = unSerializeShortcutInfo(infoFile);
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder(shortcutInfoDTOFromJsonFile);
    } else {
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder();
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getName())) {
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getCategory())) {
        shortcutInfoDTOBuilder.withCategory("Other");
    }
    final ShortcutInfoDTO shortcutInfoDTO = shortcutInfoDTOBuilder.build();
    try {
        final URI icon = iconFile.exists() ? iconFile.toURI() : getClass().getResource("phoenicis.png").toURI();
        final URI miniature = miniatureFile.exists() ? miniatureFile.toURI() : getClass().getResource("defaultMiniature.png").toURI();
        return new ShortcutDTO.Builder().withId(baseName).withInfo(shortcutInfoDTO).withScript(IOUtils.toString(new FileInputStream(file), "UTF-8")).withIcon(icon).withMiniature(miniature).build();
    } catch (URISyntaxException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream)

Example 5 with ShortcutInfoDTO

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

the class ShortcutManager method createShortcut.

public void createShortcut(ShortcutDTO shortcutDTO) {
    final ShortcutInfoDTO shortcutInfo = shortcutDTO.getInfo();
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectoryFile = new File(this.shortcutDirectory);
    final File infoFile = new File(shortcutDirectoryFile, baseName + ".info");
    final File scriptFile = new File(shortcutDirectoryFile, baseName + ".shortcut");
    final File iconFile = new File(shortcutDirectoryFile, baseName + ".icon");
    final File miniatureFile = new File(shortcutDirectoryFile, baseName + ".miniature");
    if (!shortcutDirectoryFile.exists()) {
        shortcutDirectoryFile.mkdirs();
    }
    try {
        this.objectMapper.writeValue(infoFile, shortcutInfo);
        FileUtils.writeStringToFile(scriptFile, shortcutDTO.getScript(), ENCODING);
        try {
            if (shortcutDTO.getIcon() != null) {
                File file = new File(shortcutDTO.getIcon());
                if (file.exists()) {
                    FileUtils.copyFile(file, iconFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating shortcut icon", e);
        }
        try {
            if (shortcutDTO.getMiniature() != null) {
                File file = new File(shortcutDTO.getMiniature());
                if (file.exists()) {
                    FileUtils.copyFile(file, miniatureFile);
                }
            }
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Error while creating miniature", e);
        }
    } catch (IOException e) {
        LOGGER.warn("Error while creating shortcut", e);
    } finally {
        libraryManager.refresh();
    }
    if (this.desktopShortcutDirectory != null) {
        final File desktopShortcutDirectoryFile = new File(this.desktopShortcutDirectory);
        final File desktopShortcutFile = new File(desktopShortcutDirectoryFile, baseName + ".desktop");
        try {
            final String content = "[Desktop Entry]\n" + "Name=" + shortcutInfo.getName() + "\n" + "Type=Application\n" + "Icon=" + miniatureFile.getAbsolutePath() + "\n" + "Exec=phoenicis-cli -run \"" + shortcutInfo.getName() + "\"";
            FileUtils.writeStringToFile(desktopShortcutFile, content, ENCODING);
        } catch (IOException e) {
            LOGGER.warn("Error while creating .desktop", e);
        }
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)6 IOException (java.io.IOException)6 ShortcutInfoDTO (org.phoenicis.library.dto.ShortcutInfoDTO)6 FileInputStream (java.io.FileInputStream)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)1