Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / utils.c
1 /*
2  * Emulator
3  *
4  * Copyright (C) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * DoHyung Hong <don.hong@samsung.com>
8  * SeokYeon Hwang <syeon.hwang@samsung.com>
9  * Hyunjun Son <hj79.son@samsung.com>
10  * SangJin Kim <sangjin3.kim@samsung.com>
11  * MunKyu Im <munkyu.im@samsung.com>
12  * KiTae Kim <kt920.kim@samsung.com>
13  * JinHyung Jo <jinhyung.jo@samsung.com>
14  * SungMin Ha <sungmin82.ha@samsung.com>
15  * JiHye Kim <jihye1128.kim@samsung.com>
16  * GiWoong Kim <giwoong.kim@samsung.com>
17  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
18  * DongKyun Yun <dk77.yun@samsung.com>
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
33  *
34  * Contributors:
35  * - S-Core Co., Ltd
36  *
37  */
38
39
40
41 /**
42     @file   utils.c
43     @brief  miscellaneous functions used in ISE
44 */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <glib.h>
49 #include <glib/gstdio.h>
50 #include <gtk/gtk.h>
51 #include <time.h>
52 #include <ctype.h>
53 #include <unistd.h>
54 #include <string.h>
55
56 #include "utils.h"
57
58 #include "debug_ch.h"
59
60 //DEFAULT_DEBUG_CHANNEL(tizen);
61 MULTI_DEBUG_CHANNEL(tizen, utils);
62
63 static GHashTable *windows_hash = NULL; /* hash table to get widow and widget of Emulator */
64
65
66 /**
67     @brief  hash intialization
68 */
69 GHashTable *window_hash_init (void)
70 {
71     windows_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
72     return windows_hash;
73 }
74
75
76 /**
77     @brief  add window widget to hash
78 */
79 void add_window (GtkWidget *win, gint window_id)
80 {
81     g_hash_table_insert (windows_hash, GINT_TO_POINTER (window_id), win);
82 }
83
84
85 /**
86     @brief  search window widget by using window id as a key
87     @return matched window widget
88 */
89 GtkWidget *get_window (gint window_id)
90 {
91     GtkWidget *win = (GtkWidget *) g_hash_table_lookup (windows_hash, GINT_TO_POINTER (window_id));
92     return win;
93 }
94
95
96 /**
97     @brief  destroy hash
98 */
99 void window_hash_destroy (void)
100 {
101     g_hash_table_destroy (windows_hash);
102 }
103
104
105 /**
106     @brief  add widget to hash 
107     @param  window_id: ID of widget
108     @param  widget_name: widget name
109     @param  widget: widget to add in hash
110 */
111 void add_widget (gint window_id, gchar * widget_name, GtkWidget * widget)
112 {
113     if (!windows_hash) {
114         WARN("Parent window not exist!\n");
115         return;
116     }
117     
118     GtkWidget *parent = get_window (window_id);
119     if (!parent) {
120         WARN("Parent window not exist!\n");
121         return;
122     }
123     
124     g_object_set_data_full (G_OBJECT (parent), widget_name, g_object_ref (widget), (GDestroyNotify) g_object_unref);
125 }
126
127
128 /**
129     @brief  get widget from hash
130     @param  window_id: ID of widget
131     @param  widget_name: widget name to search in hash
132     @return widget: widget to matched widget_name
133 */
134 GtkWidget *get_widget (gint window_id, gchar *widget_name)
135 {
136     GtkWidget *parent;
137     
138     parent = get_window (window_id);
139     if (!parent) {
140         WARN( "Parent window not exist!\n");
141         return NULL;
142     }
143
144     GtkWidget *w = (GtkWidget *) g_object_get_data (G_OBJECT (parent), widget_name);
145     if (!w) {
146         INFO( "Widget(%s) not found!\n", widget_name);
147         return NULL;
148     }
149     
150     return w;
151 }
152
153
154 /**
155  * @brief   set config integer value in config.ini file
156  * @return  fail(-1), success(0)
157  * @date    Nov 18. 2008
158  * */
159 int set_config_type(gchar *filepath, const gchar *group, const gchar *field, const int type)
160 {
161     GKeyFile *keyfile;
162     GError *error = NULL;
163     gsize length;
164
165     keyfile = g_key_file_new();
166     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
167         INFO( "loading key file form %s is failed.\n", filepath);
168         return -1;
169     }
170
171     g_key_file_set_integer(keyfile, group, field, type);
172
173     gchar *data = g_key_file_to_data(keyfile, &length, &error);
174     if (error != NULL) {
175         g_print("in set_config_type\n");
176         g_print("%s", error->message);
177         g_clear_error(&error);
178     }
179
180     g_strstrip(data);
181     length = strlen(data);
182     g_file_set_contents(filepath, data, length, &error);
183     if (error != NULL) {
184         g_print("in set_cofig_type after g_file_set_contetns\n");
185         g_print("%s", error->message);
186         g_clear_error(&error);
187     }
188
189     g_chmod(filepath, 0666);
190
191     g_free(data);
192     g_key_file_free(keyfile);
193
194     return 0;
195
196 }
197
198 /**
199  * @brief   delete group in targetlist.ini file
200  * @return  fail(-1), success(0)
201  * @date    Nov 18. 2008
202  * */
203 int del_config_group(gchar *filepath, const gchar *group)
204 {
205     GKeyFile *keyfile;
206     GError *error = NULL;
207     gsize length;
208
209     keyfile = g_key_file_new();
210     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
211         INFO( "loading key file form %s is failed.\n", filepath);
212         return -1;
213     }
214
215     if(!g_key_file_remove_group(keyfile, group, &error)){
216         ERR( "fail to remove this key. group name : %s\n", group);
217         return -1;
218     }
219     
220     gchar *data = g_key_file_to_data(keyfile, &length, &error);
221     if (error != NULL) {
222         g_print("in set_config_type\n");
223         g_print("%s", error->message);
224         g_clear_error(&error);
225     }
226
227     g_strstrip(data);
228     length = strlen(data);
229     g_file_set_contents(filepath, data, length, &error);
230     if (error != NULL) {
231         g_print("in set_config_value after g_file_set_contents\n");
232         g_print("%s", error->message);
233         g_clear_error(&error);
234     }
235
236     g_free(data);
237     g_key_file_free(keyfile);
238
239     return 0;
240
241 }
242
243 /**
244  * @brief   see if target_name is group
245  * @return  true / false
246  * @date    Nov 18. 2008
247  * */
248 gboolean is_group(const gchar *target_name)
249 {
250     char **target_groups = NULL;
251     int i;
252     int group_num;
253     char *filepath = get_targetlist_filepath();
254     
255     target_groups = get_virtual_target_groups(filepath, &group_num);
256
257     for(i = 0; i < group_num; i++)
258     {
259         if(strcmp(target_groups[i], target_name) == 0)
260             return TRUE;
261     }
262     
263     return FALSE;
264 }
265
266
267 /**
268  * @brief   get group name of specific target name
269  * @return  group name
270  * @date    Nov 18. 2008
271  * */
272 char *get_group_name(gchar *filepath, const gchar *field)
273 {
274     GKeyFile *keyfile;
275     GError *err = NULL;
276     char **target_groups = NULL;
277     int i;
278     int group_num;
279
280     keyfile = g_key_file_new();
281     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &err)) {
282         INFO( "loading key file form %s is failed.\n", filepath);
283         return NULL;
284     }
285     
286     target_groups = get_virtual_target_groups(filepath, &group_num);
287
288     for(i = 0; i < group_num; i++)
289     {
290         if(!g_key_file_has_key(keyfile, target_groups[i], field, &err))
291             INFO("%s is not in %s\n", target_groups[i], field);
292         else
293         {
294             g_key_file_free(keyfile);
295             return target_groups[i];
296         }
297     }
298
299     INFO("there is no group include %s\n", field);
300     g_key_file_free(keyfile);
301     return NULL;
302
303 }
304
305
306
307 /**
308  * @brief   delete target name key in targetlist.ini file
309  * @return  fail(-1), success(0)
310  * @date    Nov 18. 2008
311  * */
312 int del_config_key(gchar *filepath, const gchar *group, const gchar *field)
313 {
314     GKeyFile *keyfile;
315     GError *error = NULL;
316     gsize length;
317
318     keyfile = g_key_file_new();
319     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
320         INFO( "loading key file form %s is failed.\n", filepath);
321         return -1;
322     }
323
324     if(!g_key_file_remove_key(keyfile, group, field, &error)){
325             ERR( "fail to remove this key. [group: %s , key: %s]\n", group, field);
326             return -1;
327     }
328     
329     gchar *data = g_key_file_to_data(keyfile, &length, &error);
330     if (error != NULL) {
331         g_print("in set_config_type\n");
332         g_print("%s", error->message);
333         g_clear_error(&error);
334     }
335
336     g_strstrip(data);
337     length = strlen(data);
338     g_file_set_contents(filepath, data, length, &error);
339     if (error != NULL) {
340         g_print("in set_config_value after g_file_set_contents\n");
341         g_print("%s", error->message);
342         g_clear_error(&error);
343     }
344
345     g_free(data);
346     g_key_file_free(keyfile);
347
348     return 0;
349
350 }
351
352
353 /**
354  * @brief   set config characters value in config.ini file
355  * @return  fail(-1), success(0)
356  * @date    Nov 18. 2008
357  * */
358 int set_config_value(gchar *filepath, const gchar *group, const gchar *field, const gchar *value)
359 {
360     GKeyFile *keyfile;
361     GError *error = NULL;
362     gsize length;
363     int file_status;
364
365     keyfile = g_key_file_new();
366
367     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
368         WARN( "loading key file form %s is failed.\n", filepath);
369         file_status = is_exist_file(filepath);
370         if(file_status == -1 || file_status == FILE_NOT_EXISTS)
371         {
372             char *message = g_strdup_printf("File does not exist\n\n"
373                                 "   - [%s]", filepath);
374             show_message("Error", message);
375             free(message);
376             return -1;
377         }
378     }
379     
380     g_key_file_set_value(keyfile, group, field, value);
381
382     gchar *data = g_key_file_to_data(keyfile, &length, &error);
383     if (error != NULL) {
384         g_print("in set_config_type\n");
385         g_print("%s", error->message);
386         g_clear_error(&error);
387     }
388
389     g_strstrip(data);
390     length = strlen(data);
391     g_file_set_contents(filepath, data, length, &error);
392     if (error != NULL) {
393         g_print("in set_config_value after g_file_set_contents\n");
394         g_print("%s", error->message);
395         g_clear_error(&error);
396     }
397
398     g_free(data);
399     g_key_file_free(keyfile);
400
401     return 0;
402 }
403
404
405 /**
406  * @brief   get config integer type from config.ini file
407  * @return  fail(0), success(type)
408  * @date    Nov 18. 2008
409  * */
410 int get_config_type(gchar *filepath, const gchar *group, const gchar *field)
411 {
412     GKeyFile *keyfile;
413     GError *error = NULL;
414     gint type = 0;
415
416     keyfile = g_key_file_new();
417
418     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
419         ERR("loading key file from %s is failed\n", filepath );
420         return -1;
421     }
422
423     type = g_key_file_get_integer(keyfile, group, field, &error);
424
425     g_key_file_free(keyfile);
426     return type;
427 }
428
429
430 /**
431  * @brief   get config characters value from config.ini file
432  * @return  fail(0), success(value)
433  * @date    Nov 18. 2008
434  * */
435 char *get_config_value(gchar *filepath, const gchar *group, const gchar *field)
436 {
437     GKeyFile *keyfile;
438     GError *error = NULL;
439     gchar *value = NULL;
440
441     keyfile = g_key_file_new();
442
443     if (!g_key_file_load_from_file(keyfile, filepath, G_KEY_FILE_KEEP_COMMENTS, &error)) {
444         ERR("loading key file form %s is failed\n", filepath );
445         return NULL;
446     }
447
448     value = g_key_file_get_value(keyfile, group, field, NULL);
449
450     if (!value || strlen(value) == 0) {
451         //*value = '\0';
452         value = NULL;
453         return value;
454     }
455
456     g_key_file_free(keyfile);
457     return value;
458 }
459
460
461 /**
462     @brief  convert string  to lower case string
463     @param  string string to covert
464 */
465 #ifndef _WIN32
466 void
467 strlwr (char *string)
468 {
469     while (1)
470     {
471
472         *string = (char) tolower (*string);
473
474         if (*string == 0)  {
475             return;
476         }
477         string++;
478     }
479 }
480 #endif
481