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