Adding upstream version 0.4.1.
[debian/lxdm.git] / src / lang.c
1 /*
2 * lang.c - load language list
3 *
4 * Copyright 2009 dgod <dgod.osa@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "lang.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <string.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #ifndef LXDM_DATA_DIR
35 #define LXDM_DATA_DIR "/usr/share/lxdm"
36 #endif
37
38 #include "gdm/gdm-languages.h"
39
40 static int cmpstr(const void *p1, const void *p2)
41 {
42 return strcmp(* (char * const *) p1, * (char * const *) p2);
43 }
44
45 static char **lxdm_get_all_language_names_static(void)
46 {
47 GPtrArray *array;
48 FILE *fp;
49 char line[128];
50
51 fp=fopen(LXDM_DATA_DIR "/lang.txt","r");
52 if(!fp) return NULL;
53 array=g_ptr_array_new();
54 while(fgets(line,128,fp)!=NULL)
55 {
56 char *p=strchr(line,'\n');
57 if(*p) *p=0;
58 g_ptr_array_add(array,g_strdup(line));
59
60 }
61 fclose(fp);
62 g_ptr_array_add (array, NULL);
63 return (char **) g_ptr_array_free (array, FALSE);
64 }
65
66 static char **lxdm_get_all_language_names(void)
67 {
68 char **list,**lang;
69 int len;
70 list=gdm_get_all_language_names();
71 if(!list) return NULL;
72 for(lang=list;*lang!=NULL;lang++)
73 {
74 char *normal=gdm_normalize_language_name(*lang);
75 if(normal)
76 {
77 g_free(*lang);
78 *lang=normal;
79 }
80 }
81 len=g_strv_length(list);
82 qsort(list,len,sizeof(char*),(void*)cmpstr);
83 return list;
84 }
85
86 static char **lxdm_get_config_language_names(GKeyFile *config)
87 {
88 char **list;
89
90 list=g_key_file_get_string_list(config,"base","last_langs",NULL,NULL);
91 if(!list)
92 {
93 list=g_malloc0(sizeof(char*));
94 }
95 return list;
96 }
97
98 void lxdm_load_langs(GKeyFile *config, gboolean all, void *arg, void (*cb)(void *arg, char *lang, char *desc))
99 {
100 char **lang;
101 int i;
102
103 cb(arg, "", _("Default")); /* default is to use the system wide settings ,not use the "C" */
104
105 if(!all)
106 {
107 lang=lxdm_get_config_language_names(config);
108 }
109 else
110 {
111 lang=lxdm_get_all_language_names_static();
112 if(!lang) lang=lxdm_get_all_language_names();
113 }
114 if(!lang) return;
115
116 for(i=0;lang[i]!=NULL;i++)
117 {
118 char *readable=gdm_get_language_from_name(lang[i],lang[i]);
119 cb(arg,lang[i],readable);
120 g_free(readable);
121 }
122
123 g_strfreev(lang);
124
125 if(!all) cb(arg,"~",_("More ..."));
126 }
127