Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / skin / client / src / org / tizen / emulator / skin / config / EmulatorConfig.java
1 /**
2  * 
3  *
4  * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * GiWoong Kim <giwoong.kim@samsung.com>
8  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
9  * HyunJun Son
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  *
28  */
29
30 package org.tizen.emulator.skin.config;
31
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import org.eclipse.swt.graphics.Rectangle;
41 import org.eclipse.swt.widgets.Display;
42 import org.tizen.emulator.skin.comm.ICommunicator.RotationInfo;
43 import org.tizen.emulator.skin.comm.ICommunicator.Scale;
44 import org.tizen.emulator.skin.dbi.EmulatorUI;
45 import org.tizen.emulator.skin.exception.ConfigException;
46 import org.tizen.emulator.skin.log.SkinLogger;
47 import org.tizen.emulator.skin.log.SkinLogger.SkinLogLevel;
48 import org.tizen.emulator.skin.util.IOUtil;
49 import org.tizen.emulator.skin.util.StringUtil;
50
51
52 /**
53  * 
54  *
55  */
56 public class EmulatorConfig {
57
58         private static Logger logger =
59                         SkinLogger.getSkinLogger(EmulatorConfig.class).getLogger();
60
61         public static final int DEFAULT_WINDOW_SCALE = Scale.SCALE_50.value();
62         public static final short DEFAULT_WINDOW_ROTATION = RotationInfo.PORTRAIT.id();
63         public static final int DEFAULT_WINDOW_X = 50;
64         public static final int DEFAULT_WINDOW_Y = 50;
65         public static final SkinLogLevel DEFAULT_LOG_LEVEL = SkinLogLevel.DEBUG;
66
67         public interface ArgsConstants {
68                 public static final String UID = "uid";
69                 public static final String SERVER_PORT = "svr.port";
70                 public static final String RESOLUTION_WIDTH = "width";
71                 public static final String RESOLUTION_HEIGHT = "height";
72                 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
73                 public static final String VM_PATH = "vm.path";
74                 public static final String LOG_LEVEL = "log.level";
75                 public static final String NET_BASE_PORT = "net.baseport";
76                 public static final String SKIN_PATH = "skin.path";
77                 public static final String MAX_TOUCHPOINT = "max.touchpoint";
78         }
79
80         public interface SkinInfoConstants {
81                 public static final String SKIN_NAME = "skin.name";
82                 public static final String RESOLUTION_WIDTH = "resolution.width";
83                 public static final String RESOLUTION_HEIGHT = "resolution.height";
84         }
85
86         public interface SkinPropertiesConstants {
87                 public static final String WINDOW_X = "window.x";
88                 public static final String WINDOW_Y = "window.y";
89                 public static final String WINDOW_ROTATION = "window.rotate";
90                 public static final String WINDOW_SCALE = "window.scale";
91                 public static final String WINDOW_ONTOP = "window.ontop"; // always on top
92         }
93
94         public interface ConfigPropertiesConstants {
95                 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
96                 public static final String LOG_LEVEL = "log.level";
97         }
98
99         private Map<String, String> args;
100         private EmulatorUI dbiContents;
101         private Properties skinProperties;
102         private Properties configProperties;
103         private String skinPropertiesFilePath;
104
105         public EmulatorConfig( Map<String, String> args, EmulatorUI dbiContents, Properties skinProperties,
106                         String skinPropertiesFilePath, Properties configProperties ) {
107                 this.args = args;
108                 this.dbiContents = dbiContents;
109                 this.skinProperties = skinProperties;
110                 this.skinPropertiesFilePath = skinPropertiesFilePath;
111                 this.configProperties = configProperties;
112                 if ( null == configProperties ) {
113                         this.configProperties = new Properties();
114                 }
115         }
116
117         public static void validateArgs(Map<String, String> args) throws ConfigException {
118                 if (null == args) {
119                         return;
120                 }
121
122                 if (args.containsKey(ArgsConstants.UID)) {
123                         String uid = args.get(ArgsConstants.UID);
124                         try {
125                                 Integer.parseInt(uid);
126                         } catch (NumberFormatException e) {
127                                 String msg = ArgsConstants.UID + " argument is not numeric. : " + uid;
128                                 throw new ConfigException(msg);
129                         }
130                 }
131
132                 if (args.containsKey(ArgsConstants.SERVER_PORT)) {
133                         String serverPort = args.get(ArgsConstants.SERVER_PORT);
134                         try {
135                                 Integer.parseInt(serverPort);
136                         } catch (NumberFormatException e) {
137                                 String msg = ArgsConstants.SERVER_PORT + " argument is not numeric. : " + serverPort;
138                                 throw new ConfigException(msg);
139                         }
140                 } else {
141                         String msg = ArgsConstants.SERVER_PORT + " is required argument.";
142                         throw new ConfigException(msg);
143                 }
144
145                 if (args.containsKey(ArgsConstants.RESOLUTION_WIDTH)) {
146                         String width = args.get(ArgsConstants.RESOLUTION_WIDTH);
147                         try {
148                                 Integer.parseInt(width);
149                         } catch (NumberFormatException e) {
150                                 String msg = ArgsConstants.RESOLUTION_WIDTH + " argument is not numeric. : " + width;
151                                 throw new ConfigException(msg);
152                         }
153                 } else {
154                         String msg = ArgsConstants.RESOLUTION_WIDTH + " is required argument.";
155                         throw new ConfigException(msg);
156                 }
157
158                 if (args.containsKey(ArgsConstants.RESOLUTION_HEIGHT)) {
159                         String height = args.get(ArgsConstants.RESOLUTION_HEIGHT);
160                         try {
161                                 Integer.parseInt(height);
162                         } catch (NumberFormatException e) {
163                                 String msg = ArgsConstants.RESOLUTION_HEIGHT + " argument is not numeric. : " + height;
164                                 throw new ConfigException(msg);
165                         }
166                 } else {
167                         String msg = ArgsConstants.RESOLUTION_HEIGHT + " is required argument.";
168                         throw new ConfigException(msg);
169                 }
170         }
171
172         public static void validateSkinProperties( Properties skinProperties ) throws ConfigException {
173                 if ( null == skinProperties || 0 == skinProperties.size() ) {
174                         return;
175                 }
176
177                 Rectangle monitorBound = Display.getDefault().getBounds();
178                 logger.info("current display size : " + monitorBound);
179
180                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_X ) ) {
181                         String x = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_X );
182                         int xx = 0;
183
184                         try {
185                                 xx = Integer.parseInt( x );
186                         } catch ( NumberFormatException e ) {
187                                 String msg = SkinPropertiesConstants.WINDOW_X + " in .skin.properties is not numeric. : " + x;
188                                 throw new ConfigException( msg );
189                         }
190
191                         //location correction
192                         if (xx < monitorBound.x) {
193                                 int correction = monitorBound.x;
194                                 logger.info("WINDOW_X = " + xx + ", set to " + correction);
195                                 xx = correction;
196                         } else if (xx > monitorBound.x + monitorBound.width - 30) {
197                                 int correction = monitorBound.x + monitorBound.width - 100;
198                                 logger.info("WINDOW_X = " + xx + ", set to " + correction);
199                                 xx = correction;
200                         } else {
201                                 logger.info("WINDOW_X = " + xx);
202                         }
203
204                         skinProperties.setProperty(SkinPropertiesConstants.WINDOW_X, "" + xx);
205                 }
206
207                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_Y ) ) {
208                         String y = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_Y );
209                         int yy = 0;
210
211                         try {
212                                 yy = Integer.parseInt( y );
213                         } catch ( NumberFormatException e ) {
214                                 String msg = SkinPropertiesConstants.WINDOW_Y + " in .skin.properties is not numeric. : " + y;
215                                 throw new ConfigException( msg );
216                         }
217
218                         //location correction
219                         if (yy < monitorBound.y) {
220                                 int correction = monitorBound.y;
221                                 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
222                                 yy = correction;
223                         } else if (yy > monitorBound.y + monitorBound.height - 30) {
224                                 int correction = monitorBound.y + monitorBound.height - 100;
225                                 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
226                                 yy = correction;
227                         } else {
228                                 logger.info("WINDOW_Y = " + yy);
229                         }
230
231                         skinProperties.setProperty(SkinPropertiesConstants.WINDOW_Y, "" + yy);
232                 }
233
234                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_ROTATION ) ) {
235                         String rotation = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_ROTATION );
236                         try {
237                                 Integer.parseInt( rotation );
238                         } catch ( NumberFormatException e ) {
239                                 String msg = SkinPropertiesConstants.WINDOW_ROTATION + " in .skin.properties is not numeric. : " + rotation;
240                                 throw new ConfigException( msg );
241                         }
242                 }
243
244                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_SCALE ) ) {
245                         String scale = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_SCALE );
246                         try {
247                                 Integer.parseInt( scale );
248                         } catch ( NumberFormatException e ) {
249                                 String msg = SkinPropertiesConstants.WINDOW_SCALE + " in .skin.properties is not numeric. : " + scale;
250                                 throw new ConfigException( msg );
251                         }
252                 }
253
254         }
255
256         public static void validateSkinConfigProperties( Properties skinConfigProperties ) throws ConfigException {
257                 if ( null == skinConfigProperties || 0 == skinConfigProperties.size() ) {
258                         return;
259                 }
260         }
261
262         public void saveSkinProperties() {
263
264                 File file = new File( skinPropertiesFilePath );
265
266                 if ( !file.exists() ) {
267
268                         try {
269                                 if ( !file.createNewFile() ) {
270                                         return;
271                                 }
272                         } catch ( IOException e ) {
273                                 logger.log( Level.SEVERE, "Fail to create skin properties file.", e );
274                                 return;
275                         }
276
277                 }
278
279                 FileOutputStream fos = null;
280
281                 try {
282
283                         fos = new FileOutputStream( file );
284                         skinProperties.store( fos, "Automatically generated by emulator skin." );
285
286                 } catch ( IOException e ) {
287                         logger.log( Level.SEVERE, e.getMessage(), e );
288                 } finally {
289                         IOUtil.close( fos );
290                 }
291
292         }
293
294         public EmulatorUI getDbiContents() {
295                 return dbiContents;
296         }
297
298         public String getArg( String argKey ) {
299                 return args.get( argKey );
300         }
301
302         public String getArg( String argKey, String defaultValue ) {
303                 String arg = args.get( argKey );
304                 if ( StringUtil.isEmpty( arg ) ) {
305                         return defaultValue;
306                 } else {
307                         return arg;
308                 }
309         }
310
311         public int getArgInt( String argKey ) {
312                 String arg = args.get( argKey );
313                 if ( StringUtil.isEmpty( arg ) ) {
314                         return 0;
315                 }
316                 return Integer.parseInt( arg );
317         }
318
319         public int getArgInt( String argKey, int defaultValue ) {
320                 String arg = args.get( argKey );
321                 if ( StringUtil.isEmpty( arg ) ) {
322                         return defaultValue;
323                 }
324                 return Integer.parseInt( arg );
325         }
326
327         public boolean getArgBoolean( String argKey ) {
328                 String arg = args.get( argKey );
329                 return Boolean.parseBoolean( arg );
330         }
331
332         private String getProperty( Properties properties, String key ) {
333                 return properties.getProperty( key );
334         }
335
336         private String getProperty( Properties properties, String key, String defaultValue ) {
337                 String property = properties.getProperty( key );
338                 if ( StringUtil.isEmpty( property ) ) {
339                         return defaultValue;
340                 }
341                 return property;
342         }
343
344         private int getPropertyInt( Properties properties, String key ) {
345                 return Integer.parseInt( properties.getProperty( key ) );
346         }
347
348         private int getPropertyInt( Properties properties, String key, int defaultValue ) {
349                 String property = properties.getProperty( key );
350                 if ( StringUtil.isEmpty( property ) ) {
351                         return defaultValue;
352                 }
353                 return Integer.parseInt( property );
354         }
355
356         private short getPropertyShort( Properties properties, String key ) {
357                 return Short.parseShort( properties.getProperty( key ) );
358         }
359
360         private short getPropertyShort( Properties properties, String key, short defaultValue ) {
361                 String property = properties.getProperty( key );
362                 if ( StringUtil.isEmpty( property ) ) {
363                         return defaultValue;
364                 }
365                 return Short.parseShort( property );
366         }
367
368         private void setProperty( Properties properties, String key, String value ) {
369                 properties.put( key, value );
370         }
371
372         private void setProperty( Properties properties, String key, int value ) {
373                 properties.put( key, Integer.toString( value ) );
374         }
375
376         /* skin properties */
377
378         public String getSkinProperty( String key ) {
379                 return getProperty( skinProperties, key );
380         }
381
382         public String getSkinProperty( String key, String defaultValue ) {
383                 return getProperty( skinProperties, key, defaultValue );
384         }
385
386         public int getSkinPropertyInt( String key ) {
387                 return getPropertyInt( skinProperties, key );
388         }
389
390         public int getSkinPropertyInt( String key, int defaultValue ) {
391                 return getPropertyInt( skinProperties, key, defaultValue );
392         }
393
394         public short getSkinPropertyShort( String key ) {
395                 return getPropertyShort( skinProperties, key );
396         }
397
398         public short getSkinPropertyShort( String key, short defaultValue ) {
399                 return getPropertyShort( skinProperties, key, defaultValue );
400         }
401
402         public void setSkinProperty( String key, String value ) {
403                 setProperty( skinProperties, key, value );
404         }
405
406         public void setSkinProperty( String key, int value ) {
407                 setProperty( skinProperties, key, value );
408         }
409
410         /* config properties */
411
412         public String getConfigProperty( String key ) {
413                 return getProperty( configProperties, key );
414         }
415
416         public String getConfigProperty( String key, String defaultValue ) {
417                 return getProperty( configProperties, key, defaultValue );
418         }
419
420         public int getConfigPropertyInt( String key, int defaultValue ) {
421                 return getPropertyInt( configProperties, key, defaultValue );
422         }
423
424 }