]> sjero.net Git - linphone/blob - gtk/support.c
Aac-eld add missing header according to RFC3640 3.3.6
[linphone] / gtk / support.c
1 #include "linphone.h"
2
3 #include "lpconfig.h"
4
5 static GList *pixmaps_directories = NULL;
6
7 /* Use this function to set the directory containing installed pixmaps. */
8 void
9 add_pixmap_directory                   (const gchar     *directory)
10 {
11   pixmaps_directories = g_list_prepend (pixmaps_directories,
12                                         g_strdup (directory));
13 }
14
15 /* This is an internally used function to find pixmap files. */
16 static gchar*
17 find_pixmap_file                       (const gchar     *filename)
18 {
19   GList *elem;
20
21   /* We step through each of the pixmaps directory to find it. */
22   elem = pixmaps_directories;
23   while (elem)
24     {
25       gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
26                                          G_DIR_SEPARATOR_S, filename);
27       if (g_file_test (pathname, G_FILE_TEST_EXISTS))
28         return pathname;
29       g_free (pathname);
30       elem = elem->next;
31     }
32   return NULL;
33 }
34
35 /* This is an internally used function to create pixmaps. */
36 GtkWidget*
37 create_pixmap                          (const gchar     *filename)
38 {
39   gchar *pathname = NULL;
40   GtkWidget *pixmap;
41
42   if (!filename || !filename[0])
43       return gtk_image_new ();
44
45   pathname = find_pixmap_file (filename);
46
47   if (!pathname)
48     {
49       g_warning (_("Couldn't find pixmap file: %s"), filename);
50       return gtk_image_new ();
51     }
52
53   pixmap = gtk_image_new_from_file (pathname);
54   g_free (pathname);
55   return pixmap;
56 }
57
58 /* This is an internally used function to create pixmaps. */
59 GdkPixbuf*
60 create_pixbuf                          (const gchar     *filename)
61 {
62   gchar *pathname = NULL;
63   GdkPixbuf *pixbuf;
64   GError *error = NULL;
65
66   if (!filename || !filename[0])
67       return NULL;
68
69   pathname = find_pixmap_file (filename);
70
71   if (!pathname)
72     {
73       g_warning (_("Couldn't find pixmap file: %s"), filename);
74       return NULL;
75     }
76
77   pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
78   if (!pixbuf)
79     {
80       fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
81                pathname, error->message);
82       g_error_free (error);
83     }
84   g_free (pathname);
85   return pixbuf;
86 }
87
88 /* This is an internally used function to create animations */
89 GdkPixbufAnimation *
90 create_pixbuf_animation(const gchar     *filename)
91 {
92         gchar *pathname = NULL;
93         GdkPixbufAnimation *pixbuf;
94         GError *error = NULL;
95         
96         if (!filename || !filename[0])
97                 return NULL;
98         
99         pathname = find_pixmap_file (filename);
100         
101         if (!pathname){
102                 g_warning (_("Couldn't find pixmap file: %s"), filename);
103                 return NULL;
104         }
105         
106         pixbuf = gdk_pixbuf_animation_new_from_file (pathname, &error);
107         if (!pixbuf){
108                 fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
109                         pathname, error->message);
110                 g_error_free (error);
111         }
112         g_free (pathname);
113         return pixbuf;
114 }
115
116
117
118 /* This is used to set ATK action descriptions. */
119 void
120 glade_set_atk_action_description       (AtkAction       *action,
121                                         const gchar     *action_name,
122                                         const gchar     *description)
123 {
124   gint n_actions, i;
125
126   n_actions = atk_action_get_n_actions (action);
127   for (i = 0; i < n_actions; i++)
128     {
129       if (!strcmp (atk_action_get_name (action, i), action_name))
130         atk_action_set_description (action, i, description);
131     }
132 }
133
134
135 static char linphone_lang[256]={0};
136
137 /*lang has to be read before the config file is parsed...*/
138 const char *linphone_gtk_get_lang(const char *config_file){
139         FILE *f=fopen(config_file,"r");
140         if (f){
141                 char tmp[256];
142                 while(fgets(tmp,sizeof(tmp),f)!=NULL){
143                         char *p;
144                         if ((p=strstr(tmp,"lang="))!=NULL){
145                                 p+=5;
146                                 sscanf(p,"%s",linphone_lang);
147                                 g_message("Found lang %s",linphone_lang);
148                                 break;
149                         }
150                 }
151                 fclose(f);
152         }
153         return linphone_lang;
154 }
155
156 void linphone_gtk_set_lang(const char *code){
157         LpConfig *cfg=linphone_core_get_config(linphone_gtk_get_core());
158         const char *curlang;
159         #if defined(WIN32) || defined(__APPLE__)
160                 curlang=getenv("LANG");
161         #else
162                 curlang=getenv("LANGUAGE");
163         #endif
164         if (curlang!=NULL && strncmp(curlang,code,2)==0) {
165                 /* do not loose the _territory@encoding part*/
166                 return;
167         }
168         lp_config_set_string(cfg,"GtkUi","lang",code);
169 #ifdef WIN32
170         char tmp[128];
171         snprintf(tmp,sizeof(tmp),"LANG=%s",code);
172         _putenv(tmp);
173 #elif __APPLE__
174         setenv("LANG",code,1);
175 #else 
176         setenv("LANGUAGE",code,1);
177 #endif
178 }
179
180 const gchar *linphone_gtk_get_ui_config(const char *key, const char *def){
181         LinphoneCore *lc=linphone_gtk_get_core();
182         if (lc){
183                 LpConfig *cfg=linphone_core_get_config(linphone_gtk_get_core());
184                 return lp_config_get_string(cfg,"GtkUi",key,def);
185         }else{
186                 g_error ("Cannot read config, no core created yet.");
187                 return NULL;
188         }
189 }
190
191 int linphone_gtk_get_ui_config_int(const char *key, int def){
192         LpConfig *cfg=linphone_core_get_config(linphone_gtk_get_core());
193         return lp_config_get_int(cfg,"GtkUi",key,def);
194 }
195
196 void linphone_gtk_set_ui_config_int(const char *key , int val){
197         LpConfig *cfg=linphone_core_get_config(linphone_gtk_get_core());
198         lp_config_set_int(cfg,"GtkUi",key,val);
199 }
200
201 void linphone_gtk_set_ui_config(const char *key , const char * val){
202         LpConfig *cfg=linphone_core_get_config(linphone_gtk_get_core());
203         lp_config_set_string(cfg,"GtkUi",key,val);
204 }
205
206 static void parse_item(const char *item, const char *window_name, GtkWidget *w,  gboolean show){
207         char tmp[64];
208         char *dot;
209         strcpy(tmp,item);
210         dot=strchr(tmp,'.');
211         if (dot){
212                 *dot='\0';
213                 dot++;
214                 if (strcmp(window_name,tmp)==0){
215                         GtkWidget *wd=linphone_gtk_get_widget(w,dot);
216                         if (wd) {
217                                 if (!show) gtk_widget_hide(wd);
218                                 else gtk_widget_show(wd);
219                         }
220                 }
221         }
222 }
223
224 void linphone_gtk_visibility_set(const char *hiddens, const char *window_name, GtkWidget *w, gboolean show){
225         char item[64];
226         const char *i;
227         const char *b;
228         int len;
229         for(b=i=hiddens;*i!='\0';++i){
230                 if (*i==' '){
231                         len=MIN(i-b,sizeof(item)-1);
232                         strncpy(item,b,len);
233                         item[len]='\0';
234                         b=i+1;
235                         parse_item(item,window_name,w,show);
236                 }
237         }
238         len=MIN(i-b,sizeof(item)-1);
239         if (len>0){
240                 strncpy(item,b,len);
241                 item[len]='\0';
242                 parse_item(item,window_name,w,show);
243         }
244 }
245