00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.settings;
00023
00024 import com.realtime.crossfire.jxclient.util.HexCodec;
00025 import java.io.File;
00026 import java.io.IOException;
00027 import org.jetbrains.annotations.NotNull;
00028 import org.jetbrains.annotations.Nullable;
00029
00034 public class Filenames {
00035
00039 private Filenames() {
00040 }
00041
00046 @NotNull
00047 public static File getOriginalImageCacheDir() {
00048 try {
00049 return getSettingsFile("cache");
00050 } catch (final IOException ex) {
00051 System.err.println(ex.getMessage());
00052 System.exit(1);
00053 throw new AssertionError();
00054 }
00055 }
00056
00061 @NotNull
00062 public static File getScaledImageCacheDir() {
00063 try {
00064 return getSettingsFile("cache-x2");
00065 } catch (final IOException ex) {
00066 System.err.println(ex.getMessage());
00067 System.exit(1);
00068 throw new AssertionError();
00069 }
00070 }
00071
00076 @NotNull
00077 public static File getMagicMapImageCacheDir() {
00078 try {
00079 return getSettingsFile("cache-mm");
00080 } catch (final IOException ex) {
00081 System.err.println(ex.getMessage());
00082 System.exit(1);
00083 throw new AssertionError();
00084 }
00085 }
00086
00092 @NotNull
00093 public static File getSettingsFile() throws IOException {
00094 return getSettingsFile("jxclient.conf");
00095 }
00096
00106 @NotNull
00107 public static File getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character) throws IOException {
00108 return getSettingsFile("shortcuts-"+encode(hostname)+"-"+encode(character)+".txt");
00109 }
00110
00121 @Nullable
00122 public static File getKeybindingsFile(@Nullable final CharSequence hostname, @Nullable final CharSequence character) throws IOException {
00123 return getSettingsFile(hostname == null || character == null ? "keybindings.txt" : "keybindings-"+encode(hostname)+"-"+encode(character)+".txt");
00124 }
00125
00131 @Nullable
00132 public static File getMetaserverCacheFile() {
00133 try {
00134 return getSettingsFile("metaserver.txt");
00135 } catch (final IOException ex) {
00136 System.err.println("Cannot access metaserver cache file: "+ex.getMessage());
00137 return null;
00138 }
00139 }
00140
00147 @NotNull
00148 public static File getDialogsFile(@NotNull final String skinName) throws IOException {
00149 return new File(getSettingsFile("skin_"+skinName), "dialogs.txt");
00150 }
00151
00158 @NotNull
00159 public static File getSettingsFile(@NotNull final String filename) throws IOException {
00160 final File settingsDir = new File(getCrossfireFile(), "jxclient");
00161 if (!settingsDir.exists() && !settingsDir.mkdirs()) {
00162 throw new IOException("cannot create "+settingsDir);
00163 }
00164
00165 return new File(settingsDir, filename);
00166 }
00167
00173 @NotNull
00174 private static File getCrossfireFile() throws IOException {
00175 final String home = System.getProperty("user.home");
00176 if (home == null) {
00177 throw new IOException("cannot find home directory");
00178 }
00179
00180 return new File(home, ".crossfire");
00181 }
00182
00188 @NotNull
00189 private static String encode(@NotNull final CharSequence str) {
00190 final StringBuilder sb = new StringBuilder();
00191 for (int i = 0; i < str.length(); i++) {
00192 final char ch = str.charAt(i);
00193 if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') || ch == '-' || ch == '_' || ch == '.') {
00194 sb.append(ch);
00195 } else {
00196 sb.append('%');
00197 HexCodec.hexEncode2(sb, ch);
00198 }
00199 }
00200 return sb.toString();
00201 }
00202
00203 }