Crossfire JXClient, Trunk  R20561
Filenames.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.settings;
23 
25 import java.io.File;
26 import java.io.IOException;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class Filenames {
35 
39  private Filenames() {
40  }
41 
46  @NotNull
47  public static File getOriginalImageCacheDir() {
48  try {
49  return getSettingsFile("cache");
50  } catch (final IOException ex) {
51  System.err.println(ex.getMessage());
52  System.exit(1);
53  throw new AssertionError(ex);
54  }
55  }
56 
61  @NotNull
62  public static File getScaledImageCacheDir() {
63  try {
64  return getSettingsFile("cache-x2");
65  } catch (final IOException ex) {
66  System.err.println(ex.getMessage());
67  System.exit(1);
68  throw new AssertionError(ex);
69  }
70  }
71 
76  @NotNull
77  public static File getMagicMapImageCacheDir() {
78  try {
79  return getSettingsFile("cache-mm");
80  } catch (final IOException ex) {
81  System.err.println(ex.getMessage());
82  System.exit(1);
83  throw new AssertionError(ex);
84  }
85  }
86 
92  @NotNull
93  public static File getSettingsFile() throws IOException {
94  return getSettingsFile("jxclient.conf");
95  }
96 
106  @NotNull
107  public static File getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character) throws IOException {
108  return getSettingsFile("shortcuts-"+encode(hostname)+"-"+encode(character)+".txt");
109  }
110 
121  @Nullable
122  public static File getKeybindingsFile(@Nullable final CharSequence hostname, @Nullable final CharSequence character) throws IOException {
123  return getSettingsFile(hostname == null || character == null ? "keybindings.txt" : "keybindings-"+encode(hostname)+"-"+encode(character)+".txt");
124  }
125 
131  @Nullable
132  public static File getMetaserverCacheFile() {
133  try {
134  return getSettingsFile("metaserver.txt");
135  } catch (final IOException ex) {
136  System.err.println("Cannot access metaserver cache file: "+ex.getMessage());
137  return null;
138  }
139  }
140 
147  @NotNull
148  public static File getDialogsFile(@NotNull final String skinName) throws IOException {
149  return new File(getSettingsFile("skin_"+skinName), "dialogs.txt");
150  }
151 
158  @NotNull
159  public static File getSettingsFile(@NotNull final String filename) throws IOException {
160  final File settingsDir = new File(getCrossfireFile(), "jxclient");
161  if (!settingsDir.exists() && !settingsDir.mkdirs()) {
162  throw new IOException("cannot create "+settingsDir);
163  }
164 
165  return new File(settingsDir, filename);
166  }
167 
173  @NotNull
174  private static File getCrossfireFile() throws IOException {
175  final String home = System.getProperty("user.home");
176  if (home == null) {
177  throw new IOException("cannot find home directory");
178  }
179 
180  return new File(home, ".crossfire");
181  }
182 
188  @NotNull
189  private static String encode(@NotNull final CharSequence str) {
190  final StringBuilder sb = new StringBuilder();
191  for (int i = 0; i < str.length(); i++) {
192  final char ch = str.charAt(i);
193  if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') || ch == '-' || ch == '_' || ch == '.') {
194  sb.append(ch);
195  } else {
196  sb.append('%');
197  HexCodec.hexEncode2(sb, ch);
198  }
199  }
200  return sb.toString();
201  }
202 
209  @NotNull
210  public static File getMessageLogFile(@Nullable final String hostname) throws IOException {
211  return getSettingsFile(hostname == null ? "jxclient.txt" : "jxclient-"+hostname+".txt");
212  }
213 
214 }
static File getScaledImageCacheDir()
Returns the image cache directory for double size images.
Definition: Filenames.java:62
Utility class to return references to settings files.
Definition: Filenames.java:34
static File getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Returns the shortcuts file.
Definition: Filenames.java:107
static File getSettingsFile()
Returns the main settings file.
Definition: Filenames.java:93
Filenames()
Private constructor to prevent instantiation.
Definition: Filenames.java:39
static File getKeybindingsFile(@Nullable final CharSequence hostname, @Nullable final CharSequence character)
Returns the keybindings file.
Definition: Filenames.java:122
static File getMessageLogFile(@Nullable final String hostname)
Returns the log file for text message logging.
Definition: Filenames.java:210
static File getOriginalImageCacheDir()
Returns the image cache directory.
Definition: Filenames.java:47
static void hexEncode2(@NotNull final StringBuilder sb, final int value)
Append a given value as a two digits hexadecimal number.
Definition: HexCodec.java:49
static String encode(@NotNull final CharSequence str)
Encodes a string to make it safe as a file name.
Definition: Filenames.java:189
static File getDialogsFile(@NotNull final String skinName)
Returns the file for storing dialog related information for a skin.
Definition: Filenames.java:148
static File getCrossfireFile()
Returns the crossfire settings directory.
Definition: Filenames.java:174
static File getSettingsFile(@NotNull final String filename)
Returns a file within the settings directory.
Definition: Filenames.java:159
static File getMetaserverCacheFile()
Returns the metaserver cache file.
Definition: Filenames.java:132
Utility class for en-/decoding hexadecimal strings.
Definition: HexCodec.java:30
static File getMagicMapImageCacheDir()
Returns the image cache directory for magic map sized images.
Definition: Filenames.java:77