OSDN Git Service

39ebe77f812c0f471cac7d1917762bd5010a9286
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / config / AppSetting.java
1 /*
2  * application settings
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.config;
9
10 import java.awt.Font;
11 import java.io.File;
12 import jp.sfjp.jindolf.data.DialogPref;
13 import jp.sfjp.jindolf.glyph.FontInfo;
14 import jp.sfjp.jindolf.net.ProxyInfo;
15 import jp.sourceforge.jovsonz.JsBoolean;
16 import jp.sourceforge.jovsonz.JsObject;
17 import jp.sourceforge.jovsonz.JsPair;
18 import jp.sourceforge.jovsonz.JsValue;
19
20 /**
21  * アプリケーションの各種設定。
22  */
23 public class AppSetting{
24
25     private static final String HASH_PROXY = "proxy";
26
27     private static final String HASH_FONT = "font";
28     private static final String HASH_USEBODYICON = "useBodyIcon";
29     private static final String HASH_USEMONOTOMB = "useMonoTomb";
30     private static final String HASH_SIMPLEMODE = "isSimpleMode";
31     private static final String HASH_ALIGNBALOON = "alignBaloonWidth";
32
33     private OptionInfo optInfo;
34     private ConfigStore configStore;
35     private FontInfo fontInfo = FontInfo.DEFAULT_FONTINFO;
36
37     private int frameWidth  = 800;
38     private int frameHeight = 600;
39     private int frameXpos = Integer.MIN_VALUE;
40     private int frameYpos = Integer.MIN_VALUE;
41
42     private ProxyInfo proxyInfo = ProxyInfo.DEFAULT;
43
44     private DialogPref dialogPref = new DialogPref();
45
46     private JsValue loadedNetConfig;
47     private JsValue loadedTalkConfig;
48
49     /**
50      * コンストラクタ。
51      */
52     public AppSetting(){
53         super();
54         return;
55     }
56
57     /**
58      * 設定格納ディレクトリ関係の解析。
59      * @param optionInfo コマンドライン情報
60      * @return 設定ディレクトリ情報
61      */
62     private static ConfigStore parseConfigStore(OptionInfo optionInfo){
63         CmdOption opt =
64                 optionInfo.getExclusiveOption(CmdOption.OPT_CONFDIR,
65                                               CmdOption.OPT_NOCONF );
66
67         boolean useConfig;
68         File configPath;
69
70         if(opt == CmdOption.OPT_NOCONF){
71             useConfig = false;
72             configPath = null;
73         }else if(opt == CmdOption.OPT_CONFDIR){
74             String path = optionInfo.getStringArg(CmdOption.OPT_CONFDIR);
75             useConfig = true;
76             configPath = FileUtils.supplyFullPath(new File(path));
77         }else{
78             useConfig = true;
79             File path = ConfigFile.getImplicitConfigDirectory();
80             configPath = path;
81         }
82
83         ConfigStore result = new ConfigStore(useConfig, configPath);
84
85         return result;
86     }
87
88     /**
89      * コマンドラインオプションからアプリ設定を展開する。
90      * @param optionInfo オプション情報
91      */
92     public void applyOptionInfo(OptionInfo optionInfo){
93         this.optInfo = optionInfo;
94         this.configStore = parseConfigStore(optionInfo);
95         applyFontSetting();
96         applyGeometrySetting();
97         return;
98     }
99
100     /**
101      * フォント関係の設定。
102      */
103     private void applyFontSetting(){
104         String fontName = this.optInfo.getStringArg(CmdOption.OPT_INITFONT);
105
106         Boolean useAntiAlias =
107                 this.optInfo.getBooleanArg(CmdOption.OPT_ANTIALIAS);
108         if(useAntiAlias == null){
109             useAntiAlias = this.fontInfo.isAntiAliased();
110         }
111
112         Boolean useFractional =
113                 this.optInfo.getBooleanArg(CmdOption.OPT_FRACTIONAL);
114         if(useFractional == null){
115             useFractional = this.fontInfo.usesFractionalMetrics();
116         }
117
118         if(fontName != null){
119             Font font = Font.decode(fontName);
120             this.fontInfo = this.fontInfo.deriveFont(font);
121         }
122
123         this.fontInfo =
124                 this.fontInfo.deriveRenderContext(useAntiAlias,
125                                                   useFractional );
126
127         return;
128     }
129
130     /**
131      * ジオメトリ関係の設定。
132      */
133     private void applyGeometrySetting(){
134         Integer ival;
135
136         ival = this.optInfo.initialFrameWidth();
137         if(ival != null) this.frameWidth = ival;
138
139         ival = this.optInfo.initialFrameHeight();
140         if(ival != null) this.frameHeight = ival;
141
142         ival = this.optInfo.initialFrameXpos();
143         if(ival != null) this.frameXpos = ival;
144
145         ival = this.optInfo.initialFrameYpos();
146         if(ival != null) this.frameYpos = ival;
147
148         return;
149     }
150
151     /**
152      * コマンドラインオプション情報を返す。
153      * @return コマンドラインオプション情報
154      */
155     public OptionInfo getOptionInfo(){
156         return this.optInfo;
157     }
158
159     /**
160      * 設定格納情報を返す。
161      * @return 設定格納情報
162      */
163     public ConfigStore getConfigStore(){
164         return this.configStore;
165     }
166
167     /**
168      * 初期のフレーム幅を返す。
169      * @return 初期のフレーム幅
170      */
171     public int initialFrameWidth(){
172         return this.frameWidth;
173     }
174
175     /**
176      * 初期のフレーム高を返す。
177      * @return 初期のフレーム高
178      */
179     public int initialFrameHeight(){
180         return this.frameHeight;
181     }
182
183     /**
184      * 初期のフレーム位置のX座標を返す。
185      * 特に指示されていなければInteger.MIN_VALUEを返す。
186      * @return 初期のフレーム位置のX座標
187      */
188     public int initialFrameXpos(){
189         return this.frameXpos;
190     }
191
192     /**
193      * 初期のフレーム位置のY座標を返す。
194      * 特に指示されていなければInteger.MIN_VALUEを返す。
195      * @return 初期のフレーム位置のY座標
196      */
197     public int initialFrameYpos(){
198         return this.frameYpos;
199     }
200
201     /**
202      * フォント設定を返す。
203      * @return フォント設定
204      */
205     public FontInfo getFontInfo(){
206         return this.fontInfo;
207     }
208
209     /**
210      * フォント設定を更新する。
211      * @param fontInfo フォント設定
212      */
213     public void setFontInfo(FontInfo fontInfo){
214         this.fontInfo = fontInfo;
215         return;
216     }
217
218     /**
219      * プロクシ設定を返す。
220      * @return プロクシ設定
221      */
222     public ProxyInfo getProxyInfo(){
223         return this.proxyInfo;
224     }
225
226     /**
227      * プロクシ設定を更新する。
228      * @param proxyInfo プロクシ設定。nullならプロクシなしと解釈される。
229      */
230     public void setProxyInfo(ProxyInfo proxyInfo){
231         if(proxyInfo == null) this.proxyInfo = ProxyInfo.DEFAULT;
232         else                  this.proxyInfo = proxyInfo;
233         return;
234     }
235
236     /**
237      * 発言表示設定を返す。
238      * @return 表示設定
239      */
240     public DialogPref getDialogPref(){
241         return this.dialogPref;
242     }
243
244     /**
245      * 発言表示設定を返す。
246      * @param pref 表示設定
247      */
248     public void setDialogPref(DialogPref pref){
249         if(pref == null) this.dialogPref = new DialogPref();
250         else             this.dialogPref = pref;
251         return;
252     }
253
254     /**
255      * ネットワーク設定をロードする。
256      */
257     private void loadNetConfig(){
258         JsObject root = this.configStore.loadNetConfig();
259         if(root == null) return;
260         this.loadedNetConfig = root;
261
262         JsValue value = root.getValue(HASH_PROXY);
263         if( ! (value instanceof JsObject) ) return;
264         JsObject proxy = (JsObject) value;
265
266         ProxyInfo info = ProxyInfo.decodeJson(proxy);
267
268         setProxyInfo(info);
269
270         return;
271     }
272
273     /**
274      * 会話表示設定をロードする。
275      */
276     private void loadTalkConfig(){
277         JsObject root = this.configStore.loadTalkConfig();
278         if(root == null) return;
279         this.loadedTalkConfig = root;
280
281         JsValue value = root.getValue(HASH_FONT);
282         if(value instanceof JsObject){
283             JsObject font = (JsObject) value;
284             FontInfo info = FontInfo.decodeJson(font);
285             setFontInfo(info);
286             applyFontSetting();
287         }
288
289         DialogPref pref = new DialogPref();
290         JsBoolean boolValue;
291         value = root.getValue(HASH_USEBODYICON);
292         if(value instanceof JsBoolean){
293             boolValue = (JsBoolean) value;
294             pref.setBodyImageSetting(boolValue.booleanValue());
295         }
296         value = root.getValue(HASH_USEMONOTOMB);
297         if(value instanceof JsBoolean){
298             boolValue = (JsBoolean) value;
299             pref.setMonoImageSetting(boolValue.booleanValue());
300         }
301         value = root.getValue(HASH_SIMPLEMODE);
302         if(value instanceof JsBoolean){
303             boolValue = (JsBoolean) value;
304             pref.setSimpleMode(boolValue.booleanValue());
305         }
306         value = root.getValue(HASH_ALIGNBALOON);
307         if(value instanceof JsBoolean){
308             boolValue = (JsBoolean) value;
309             pref.setAlignBalooonWidthSetting(boolValue.booleanValue());
310         }
311         setDialogPref(pref);
312
313         return;
314     }
315
316     /**
317      * ネットワーク設定をセーブする。
318      */
319     private void saveNetConfig(){
320         if( ! getConfigStore().useStoreFile() ) return;
321
322         JsObject root = new JsObject();
323         JsObject proxy = ProxyInfo.buildJson(getProxyInfo());
324         root.putValue(HASH_PROXY, proxy);
325
326         if(this.loadedNetConfig != null){
327             if(this.loadedNetConfig.equals(root)) return;
328         }
329
330         this.configStore.saveNetConfig(root);
331
332         return;
333     }
334
335     /**
336      * 会話表示設定をセーブする。
337      */
338     private void saveTalkConfig(){
339         if( ! getConfigStore().useStoreFile() ) return;
340
341         JsObject root = new JsObject();
342
343         JsObject font = FontInfo.buildJson(getFontInfo());
344         root.putValue(HASH_FONT, font);
345
346         DialogPref pref = getDialogPref();
347         JsPair useBodyIcon =
348                 new JsPair(HASH_USEBODYICON, pref.useBodyImage());
349         JsPair useMonoTomb =
350                 new JsPair(HASH_USEMONOTOMB, pref.useMonoImage());
351         JsPair isSimple =
352                 new JsPair(HASH_SIMPLEMODE, pref.isSimpleMode());
353         JsPair alignBaloon =
354                 new JsPair(HASH_ALIGNBALOON, pref.alignBaloonWidth());
355         root.putPair(useBodyIcon);
356         root.putPair(useMonoTomb);
357         root.putPair(isSimple);
358         root.putPair(alignBaloon);
359
360         if(this.loadedTalkConfig != null){
361             if(this.loadedTalkConfig.equals(root)) return;
362         }
363
364         this.configStore.saveTalkConfig(root);
365
366         return;
367     }
368
369     /**
370      * 各種設定を設定格納ディレクトリからロードする。
371      */
372     public void loadConfig(){
373         loadNetConfig();
374         loadTalkConfig();
375         return;
376     }
377
378     /**
379      * 各種設定を設定格納ディレクトリへセーブする。
380      */
381     public void saveConfig(){
382         saveNetConfig();
383         saveTalkConfig();
384         return;
385     }
386
387 }