<commit_before>/* Copyright (C) 2005-2011 <NAME> */

package com.lightcrafts.platform.linux;

import com.lightcrafts.platform.AlertDialog;
import com.lightcrafts.platform.FileChooser;
import com.lightcrafts.platform.Platform;
import com.lightcrafts.ui.LightZoneSkin;
import com.lightcrafts.utils.ColorProfileInfo;
import com.lightcrafts.utils.Version;

import javax.help.HelpSet;
import javax.help.HelpSetException;
import javax.help.JHelp;
import javax.swing.*;

import java.awt.*;
import java.awt.color.ICC_Profile;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LinuxPlatform extends Platform {

    // My understanding of the state of standard linux color profile
    // locations comes from:
    //
    //      http://bugs.freestandards.org/show_bug.cgi?id=77

    private final static File SystemProfileDir = new File(
        "/usr/share/color/icc"
    );

    private final static File UserProfileDir = new File(
        System.getProperty("user.home"),
        ".color/icc"
    );

    private static Collection<ColorProfileInfo> Profiles;

    public File getLightZoneDocumentsDirectory() {
        final String home = System.getProperty( "user.home" );
        final String appName = Version.getApplicationName();
        final String path = ".local/share/" + appName;
        return new File( home, path );
    }

    public LookAndFeel getLookAndFeel() {
        return LightZoneSkin.getLightZoneLookAndFeel();
    }

    public FileChooser getFileChooser() {
        return new LinuxFileChooser();
    }

    public ICC_Profile getDisplayProfile() {
        Preferences prefs = Preferences.userRoot().node(
            "/com/lightcrafts/platform/linux"
        );
        String path = prefs.get("DisplayProfile", null);
        if (path != null) {
            try {
                return ICC_Profile.getInstance(path);
            }
            catch (Throwable e) {
                System.err.println("Malformed display profile at " + path);
                // return null;
            }
        }
        return null;
    }

    public Collection<ColorProfileInfo> getPrinterProfiles() {
        return getColorProfiles();
    }

    public Collection<ColorProfileInfo> getExportProfiles() {
        return getColorProfiles();
    }

    public boolean isKeyPressed(int keyCode) {
        return LinuxKeyUtil.isKeyPressed(keyCode);
    }

    private static Collection<ColorProfileInfo> getColorProfiles() {
        if (Profiles != null) {
            return Profiles;
        }
        Profiles = new HashSet<ColorProfileInfo>();
        Profiles.addAll(getColorProfiles(SystemProfileDir));
        Profiles.addAll(getColorProfiles(UserProfileDir));

        return Profiles;
    }

    private static Collection<ColorProfileInfo> getColorProfiles(
        File profileDir
    ) {
        HashSet<ColorProfileInfo> profiles = new HashSet<ColorProfileInfo>();

        if (! profileDir.isDirectory()) {
            return profiles;
        }
        // Try to interpret every file in there as a color profile:

        File[] files = profileDir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                profiles.addAll(getColorProfiles(file));
            }
            else if (file.isFile()) {
                String path = file.getAbsolutePath();
                try {
                    ICC_Profile profile = ICC_Profile.getInstance(path);
                    String name = ColorProfileInfo.getNameOf(profile);
                    ColorProfileInfo info = new ColorProfileInfo(name, path);
                    profiles.add(info);
                }
                catch (IOException e) {
                    // Trouble reading the file
                    String message = e.getMessage();
                    System.err.println(
                        "Can't read a color profile from " + path + ": " +
                        message
                    );
                }
                catch (Throwable e) {
                    // Invalid color profile data
                    String message = e.getMessage();
                    System.err.println(
                        "Not a valid color profile at " + path + ": " +
                        message
                    );
                }<commit_msg>Linux - Add null check in getColorProfiles<commit_after>/* Copyright (C) 2005-2011 <NAME> */

package com.lightcrafts.platform.linux;

import com.lightcrafts.platform.AlertDialog;
import com.lightcrafts.platform.FileChooser;
import com.lightcrafts.platform.Platform;
import com.lightcrafts.ui.LightZoneSkin;
import com.lightcrafts.utils.ColorProfileInfo;
import com.lightcrafts.utils.Version;

import javax.help.HelpSet;
import javax.help.HelpSetException;
import javax.help.JHelp;
import javax.swing.*;

import java.awt.*;
import java.awt.color.ICC_Profile;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LinuxPlatform extends Platform {

    // My understanding of the state of standard linux color profile
    // locations comes from:
    //
    //      http://bugs.freestandards.org/show_bug.cgi?id=77

    private final static File SystemProfileDir = new File(
        "/usr/share/color/icc"
    );

    private final static File UserProfileDir = new File(
        System.getProperty("user.home"),
        ".color/icc"
    );

    private static Collection<ColorProfileInfo> Profiles;

    public File getLightZoneDocumentsDirectory() {
        final String home = System.getProperty( "user.home" );
        final String appName = Version.getApplicationName();
        final String path = ".local/share/" + appName;
        return new File( home, path );
    }

    public LookAndFeel getLookAndFeel() {
        return LightZoneSkin.getLightZoneLookAndFeel();
    }

    public FileChooser getFileChooser() {
        return new LinuxFileChooser();
    }

    public ICC_Profile getDisplayProfile() {
        Preferences prefs = Preferences.userRoot().node(
            "/com/lightcrafts/platform/linux"
        );
        String path = prefs.get("DisplayProfile", null);
        if (path != null) {
            try {
                return ICC_Profile.getInstance(path);
            }
            catch (Throwable e) {
                System.err.println("Malformed display profile at " + path);
                // return null;
            }
        }
        return null;
    }

    public Collection<ColorProfileInfo> getPrinterProfiles() {
        return getColorProfiles();
    }

    public Collection<ColorProfileInfo> getExportProfiles() {
        return getColorProfiles();
    }

    public boolean isKeyPressed(int keyCode) {
        return LinuxKeyUtil.isKeyPressed(keyCode);
    }

    private static Collection<ColorProfileInfo> getColorProfiles() {
        if (Profiles != null) {
            return Profiles;
        }
        Profiles = new HashSet<ColorProfileInfo>();
        Profiles.addAll(getColorProfiles(SystemProfileDir));
        Profiles.addAll(getColorProfiles(UserProfileDir));

        return Profiles;
    }

    private static Collection<ColorProfileInfo> getColorProfiles(
        File profileDir
    ) {
        HashSet<ColorProfileInfo> profiles = new HashSet<ColorProfileInfo>();

        if (! profileDir.isDirectory()) {
            return profiles;
        }
        // Try to interpret every file in there as a color profile:

        File[] files = profileDir.listFiles();
        if (files == null) {
            return Collections.emptyList(); // Just in case of I/O error
        }
        for (File file : files) {
            if (file.isDirectory()) {
                profiles.addAll(getColorProfiles(file));
            }
            else if (file.isFile()) {
                String path = file.getAbsolutePath();
                try {
                    ICC_Profile profile = ICC_Profile.getInstance(path);
                    String name = ColorProfileInfo.getNameOf(profile);
                    ColorProfileInfo info = new ColorProfileInfo(name, path);
                    profiles.add(info);
                }
                catch (IOException e) {
                    // Trouble reading the file
                    String message = e.getMessage();
                    System.err.println(
                        "Can't read a color profile from " + path + ": " +
                        message
                    );
                }
                catch (Throwable e) {
                    // Invalid color profile data
                    String message = e.getMessage();
                    System.err.println(
                        "Not a valid color profile at " + path + ": " +
                        message
                    );
                }
{'fraction_non_alphanumeric': 0.09431121625981288, 'fraction_numerical': 0.0021507688998817077, 'mean_word_length': 2.0224244393890154, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433548', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2363, 'n_words': 576}