sync with tizen_2.2
[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 SDK_VERSION_NAME = "sdk.version-name";
82                 public static final String SKIN_NAME = "skin.name";
83                 public static final String RESOLUTION_WIDTH = "resolution.width";
84                 public static final String RESOLUTION_HEIGHT = "resolution.height";
85         }
86
87         public interface SkinPropertiesConstants {
88                 public static final String WINDOW_X = "window.x";
89                 public static final String WINDOW_Y = "window.y";
90                 public static final String WINDOW_ROTATION = "window.rotate";
91                 public static final String WINDOW_SCALE = "window.scale";
92                 public static final String WINDOW_ONTOP = "window.ontop"; /* always on top */
93                 public static final String KEYWINDOW_POSITION = "window.keywindow.position";
94         }
95
96         public interface ConfigPropertiesConstants {
97                 public static final String TEST_HEART_BEAT_IGNORE = "test.hb.ignore";
98                 public static final String LOG_LEVEL = "log.level";
99         }
100
101         private Map<String, String> args;
102         private EmulatorUI dbiContents;
103         private Properties skinProperties;
104         private Properties configProperties;
105         private String skinPropertiesFilePath;
106
107         public EmulatorConfig(Map<String, String> args,
108                         EmulatorUI dbiContents, Properties skinProperties,
109                         String skinPropertiesFilePath, Properties configProperties) {
110                 this.args = args;
111                 this.dbiContents = dbiContents;
112                 this.skinProperties = skinProperties;
113                 this.skinPropertiesFilePath = skinPropertiesFilePath;
114                 this.configProperties = configProperties;
115
116                 if (null == configProperties) {
117                         this.configProperties = new Properties();
118                 }
119         }
120
121         public static void validateArgs(Map<String, String> args) throws ConfigException {
122                 if (null == args) {
123                         return;
124                 }
125
126                 if (args.containsKey(ArgsConstants.UID)) {
127                         String uid = args.get(ArgsConstants.UID);
128                         try {
129                                 Integer.parseInt(uid);
130                         } catch (NumberFormatException e) {
131                                 String msg = ArgsConstants.UID + " argument is not numeric. : " + uid;
132                                 throw new ConfigException(msg);
133                         }
134                 }
135
136                 if (args.containsKey(ArgsConstants.SERVER_PORT)) {
137                         String serverPort = args.get(ArgsConstants.SERVER_PORT);
138                         try {
139                                 Integer.parseInt(serverPort);
140                         } catch (NumberFormatException e) {
141                                 String msg = ArgsConstants.SERVER_PORT + " argument is not numeric. : " + serverPort;
142                                 throw new ConfigException(msg);
143                         }
144                 } else {
145                         String msg = ArgsConstants.SERVER_PORT + " is required argument.";
146                         throw new ConfigException(msg);
147                 }
148
149                 if (args.containsKey(ArgsConstants.RESOLUTION_WIDTH)) {
150                         String width = args.get(ArgsConstants.RESOLUTION_WIDTH);
151                         try {
152                                 Integer.parseInt(width);
153                         } catch (NumberFormatException e) {
154                                 String msg = ArgsConstants.RESOLUTION_WIDTH + " argument is not numeric. : " + width;
155                                 throw new ConfigException(msg);
156                         }
157                 } else {
158                         String msg = ArgsConstants.RESOLUTION_WIDTH + " is required argument.";
159                         throw new ConfigException(msg);
160                 }
161
162                 if (args.containsKey(ArgsConstants.RESOLUTION_HEIGHT)) {
163                         String height = args.get(ArgsConstants.RESOLUTION_HEIGHT);
164                         try {
165                                 Integer.parseInt(height);
166                         } catch (NumberFormatException e) {
167                                 String msg = ArgsConstants.RESOLUTION_HEIGHT + " argument is not numeric. : " + height;
168                                 throw new ConfigException(msg);
169                         }
170                 } else {
171                         String msg = ArgsConstants.RESOLUTION_HEIGHT + " is required argument.";
172                         throw new ConfigException(msg);
173                 }
174         }
175
176         public static void validateSkinProperties( Properties skinProperties ) throws ConfigException {
177                 if ( null == skinProperties || 0 == skinProperties.size() ) {
178                         return;
179                 }
180
181                 Rectangle monitorBound = Display.getDefault().getBounds();
182                 logger.info("current display size : " + monitorBound);
183
184                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_X ) ) {
185                         String x = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_X );
186                         int xx = 0;
187
188                         try {
189                                 xx = Integer.parseInt( x );
190                         } catch ( NumberFormatException e ) {
191                                 String msg = SkinPropertiesConstants.WINDOW_X + " in .skin.properties is not numeric. : " + x;
192                                 throw new ConfigException( msg );
193                         }
194
195                         //location correction
196                         if (xx < monitorBound.x) {
197                                 int correction = monitorBound.x;
198                                 logger.info("WINDOW_X = " + xx + ", set to " + correction);
199                                 xx = correction;
200                         } else if (xx > monitorBound.x + monitorBound.width - 30) {
201                                 int correction = monitorBound.x + monitorBound.width - 100;
202                                 logger.info("WINDOW_X = " + xx + ", set to " + correction);
203                                 xx = correction;
204                         } else {
205                                 logger.info("WINDOW_X = " + xx);
206                         }
207
208                         skinProperties.setProperty(SkinPropertiesConstants.WINDOW_X, "" + xx);
209                 }
210
211                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_Y ) ) {
212                         String y = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_Y );
213                         int yy = 0;
214
215                         try {
216                                 yy = Integer.parseInt( y );
217                         } catch ( NumberFormatException e ) {
218                                 String msg = SkinPropertiesConstants.WINDOW_Y + " in .skin.properties is not numeric. : " + y;
219                                 throw new ConfigException( msg );
220                         }
221
222                         //location correction
223                         if (yy < monitorBound.y) {
224                                 int correction = monitorBound.y;
225                                 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
226                                 yy = correction;
227                         } else if (yy > monitorBound.y + monitorBound.height - 30) {
228                                 int correction = monitorBound.y + monitorBound.height - 100;
229                                 logger.info("WINDOW_Y = " + yy + ", set to " + correction);
230                                 yy = correction;
231                         } else {
232                                 logger.info("WINDOW_Y = " + yy);
233                         }
234
235                         skinProperties.setProperty(SkinPropertiesConstants.WINDOW_Y, "" + yy);
236                 }
237
238                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_ROTATION ) ) {
239                         String rotation = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_ROTATION );
240                         try {
241                                 Integer.parseInt( rotation );
242                         } catch ( NumberFormatException e ) {
243                                 String msg = SkinPropertiesConstants.WINDOW_ROTATION + " in .skin.properties is not numeric. : " + rotation;
244                                 throw new ConfigException( msg );
245                         }
246                 }
247
248                 if( skinProperties.containsKey( SkinPropertiesConstants.WINDOW_SCALE ) ) {
249                         String scale = skinProperties.getProperty( SkinPropertiesConstants.WINDOW_SCALE );
250                         try {
251                                 Integer.parseInt( scale );
252                         } catch ( NumberFormatException e ) {
253                                 String msg = SkinPropertiesConstants.WINDOW_SCALE + " in .skin.properties is not numeric. : " + scale;
254                                 throw new ConfigException( msg );
255                         }
256                 }
257
258         }
259
260         public static void validateSkinConfigProperties( Properties skinConfigProperties ) throws ConfigException {
261                 if ( null == skinConfigProperties || 0 == skinConfigProperties.size() ) {
262                         return;
263                 }
264         }
265
266         public void saveSkinProperties() {
267
268                 File file = new File( skinPropertiesFilePath );
269
270                 if ( !file.exists() ) {
271
272                         try {
273                                 if ( !file.createNewFile() ) {
274                                         return;
275                                 }
276                         } catch ( IOException e ) {
277                                 logger.log( Level.SEVERE, "Fail to create skin properties file.", e );
278                                 return;
279                         }
280
281                 }
282
283                 FileOutputStream fos = null;
284
285                 try {
286
287                         fos = new FileOutputStream( file );
288                         skinProperties.store( fos, "Automatically generated by emulator skin." );
289
290                 } catch ( IOException e ) {
291                         logger.log( Level.SEVERE, e.getMessage(), e );
292                 } finally {
293                         IOUtil.close( fos );
294                 }
295
296         }
297
298         public EmulatorUI getDbiContents() {
299                 return dbiContents;
300         }
301
302         public String getArg( String argKey ) {
303                 return args.get( argKey );
304         }
305
306         public String getArg( String argKey, String defaultValue ) {
307                 String arg = args.get( argKey );
308                 if ( StringUtil.isEmpty( arg ) ) {
309                         return defaultValue;
310                 } else {
311                         return arg;
312                 }
313         }
314
315         public int getArgInt( String argKey ) {
316                 String arg = args.get( argKey );
317                 if ( StringUtil.isEmpty( arg ) ) {
318                         return 0;
319                 }
320                 return Integer.parseInt( arg );
321         }
322
323         public int getArgInt( String argKey, int defaultValue ) {
324                 String arg = args.get( argKey );
325                 if ( StringUtil.isEmpty( arg ) ) {
326                         return defaultValue;
327                 }
328                 return Integer.parseInt( arg );
329         }
330
331         public boolean getArgBoolean( String argKey ) {
332                 String arg = args.get( argKey );
333                 return Boolean.parseBoolean( arg );
334         }
335
336         private String getProperty( Properties properties, String key ) {
337                 return properties.getProperty( key );
338         }
339
340         private String getProperty( Properties properties, String key, String defaultValue ) {
341                 String property = properties.getProperty( key );
342                 if ( StringUtil.isEmpty( property ) ) {
343                         return defaultValue;
344                 }
345                 return property;
346         }
347
348         private int getPropertyInt( Properties properties, String key ) {
349                 return Integer.parseInt( properties.getProperty( key ) );
350         }
351
352         private int getPropertyInt( Properties properties, String key, int defaultValue ) {
353                 String property = properties.getProperty( key );
354                 if ( StringUtil.isEmpty( property ) ) {
355                         return defaultValue;
356                 }
357                 return Integer.parseInt( property );
358         }
359
360         private short getPropertyShort( Properties properties, String key ) {
361                 return Short.parseShort( properties.getProperty( key ) );
362         }
363
364         private short getPropertyShort( Properties properties, String key, short defaultValue ) {
365                 String property = properties.getProperty( key );
366                 if ( StringUtil.isEmpty( property ) ) {
367                         return defaultValue;
368                 }
369                 return Short.parseShort( property );
370         }
371
372         private void setProperty( Properties properties, String key, String value ) {
373                 properties.put( key, value );
374         }
375
376         private void setProperty( Properties properties, String key, int value ) {
377                 properties.put( key, Integer.toString( value ) );
378         }
379
380         /* skin properties */
381
382         public String getSkinProperty( String key ) {
383                 return getProperty( skinProperties, key );
384         }
385
386         public String getSkinProperty( String key, String defaultValue ) {
387                 return getProperty( skinProperties, key, defaultValue );
388         }
389
390         public int getSkinPropertyInt( String key ) {
391                 return getPropertyInt( skinProperties, key );
392         }
393
394         public int getSkinPropertyInt( String key, int defaultValue ) {
395                 return getPropertyInt( skinProperties, key, defaultValue );
396         }
397
398         public short getSkinPropertyShort( String key ) {
399                 return getPropertyShort( skinProperties, key );
400         }
401
402         public short getSkinPropertyShort( String key, short defaultValue ) {
403                 return getPropertyShort( skinProperties, key, defaultValue );
404         }
405
406         public void setSkinProperty( String key, String value ) {
407                 setProperty( skinProperties, key, value );
408         }
409
410         public void setSkinProperty( String key, int value ) {
411                 setProperty( skinProperties, key, value );
412         }
413
414         /* config properties */
415
416         public String getConfigProperty( String key ) {
417                 return getProperty( configProperties, key );
418         }
419
420         public String getConfigProperty( String key, String defaultValue ) {
421                 return getProperty( configProperties, key, defaultValue );
422         }
423
424         public int getConfigPropertyInt( String key, int defaultValue ) {
425                 return getPropertyInt( configProperties, key, defaultValue );
426         }
427
428 }