diff options
Diffstat (limited to 'src/lib/nls.c')
-rw-r--r-- | src/lib/nls.c | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/src/lib/nls.c b/src/lib/nls.c index a040f8e5..f313ce59 100644 --- a/src/lib/nls.c +++ b/src/lib/nls.c | |||
@@ -53,4 +53,171 @@ GNUNET_GTK_setup_nls () | |||
53 | #endif | 53 | #endif |
54 | } | 54 | } |
55 | 55 | ||
56 | |||
57 | char * | ||
58 | GNUNET_GTK_from_utf8_to_loc (gchar *str_utf8) | ||
59 | { | ||
60 | char *str_loc; | ||
61 | const char *loc_charset; | ||
62 | gboolean is_UTF8; | ||
63 | |||
64 | if (NULL == str_utf8) | ||
65 | return NULL; | ||
66 | |||
67 | is_UTF8 = g_get_charset (&loc_charset); | ||
68 | if (is_UTF8) | ||
69 | str_loc = GNUNET_strdup (str_utf8); | ||
70 | else | ||
71 | str_loc = GNUNET_STRINGS_from_utf8 (str_utf8, strlen (str_utf8), loc_charset); | ||
72 | |||
73 | return str_loc; | ||
74 | } | ||
75 | |||
76 | char * | ||
77 | GNUNET_GTK_from_loc_to_utf8 (gchar *str_loc) | ||
78 | { | ||
79 | char *str_utf8; | ||
80 | const char *loc_charset; | ||
81 | gboolean is_UTF8; | ||
82 | |||
83 | if (NULL == str_loc) | ||
84 | return NULL; | ||
85 | |||
86 | is_UTF8 = g_get_charset (&loc_charset); | ||
87 | if (is_UTF8) | ||
88 | str_utf8 = GNUNET_strdup (str_loc); | ||
89 | else | ||
90 | str_utf8 = GNUNET_STRINGS_to_utf8 (str_loc, strlen (str_loc), loc_charset); | ||
91 | |||
92 | return str_utf8; | ||
93 | } | ||
94 | |||
95 | /* This is copied from GLib */ | ||
96 | static gboolean | ||
97 | get_filename_charset (const gchar **filename_charset) | ||
98 | { | ||
99 | const gchar **charsets; | ||
100 | gboolean is_utf8; | ||
101 | |||
102 | is_utf8 = g_get_filename_charsets (&charsets); | ||
103 | |||
104 | if (filename_charset) | ||
105 | *filename_charset = charsets[0]; | ||
106 | |||
107 | return is_utf8; | ||
108 | } | ||
109 | |||
110 | char * | ||
111 | GNUNET_GTK_from_filename_to_utf8 (gchar *filename) | ||
112 | { | ||
113 | char *str_utf8; | ||
114 | const char *filename_charset; | ||
115 | gboolean is_UTF8; | ||
116 | |||
117 | if (NULL == filename) | ||
118 | return NULL; | ||
119 | |||
120 | is_UTF8 = get_filename_charset (&filename_charset); | ||
121 | if (is_UTF8) | ||
122 | str_utf8 = GNUNET_strdup (filename); | ||
123 | else | ||
124 | str_utf8 = GNUNET_STRINGS_to_utf8 (filename, strlen (filename), filename_charset); | ||
125 | |||
126 | return str_utf8; | ||
127 | } | ||
128 | |||
129 | char * | ||
130 | GNUNET_GTK_from_utf8_to_filename (gchar *str_utf8) | ||
131 | { | ||
132 | char *filename; | ||
133 | const char *filename_charset; | ||
134 | gboolean is_UTF8; | ||
135 | |||
136 | if (NULL == str_utf8) | ||
137 | return NULL; | ||
138 | |||
139 | is_UTF8 = get_filename_charset (&filename_charset); | ||
140 | if (is_UTF8) | ||
141 | filename = GNUNET_strdup (str_utf8); | ||
142 | else | ||
143 | filename = GNUNET_STRINGS_from_utf8 (str_utf8, strlen (str_utf8), filename_charset); | ||
144 | |||
145 | return filename; | ||
146 | } | ||
147 | |||
148 | char * | ||
149 | GNUNET_GTK_from_loc_to_filename (gchar *str_loc) | ||
150 | { | ||
151 | char *filename; | ||
152 | const char *filename_charset; | ||
153 | const char *loc_charset; | ||
154 | gboolean is_filename_UTF8, is_loc_UTF8; | ||
155 | |||
156 | if (NULL == str_loc) | ||
157 | return NULL; | ||
158 | |||
159 | is_filename_UTF8 = get_filename_charset (&filename_charset); | ||
160 | is_loc_UTF8 = g_get_charset (&loc_charset); | ||
161 | if (is_filename_UTF8 && is_loc_UTF8) | ||
162 | filename = GNUNET_strdup (str_loc); | ||
163 | else if (is_filename_UTF8) | ||
164 | filename = GNUNET_STRINGS_to_utf8 (str_loc, strlen (str_loc), loc_charset); | ||
165 | else if (is_loc_UTF8) | ||
166 | filename = GNUNET_STRINGS_from_utf8 (str_loc, strlen (str_loc), filename_charset); | ||
167 | else | ||
168 | /* Pray that iconv() knows these charsets */ | ||
169 | filename = GNUNET_STRINGS_conv (str_loc, strlen (str_loc), loc_charset, filename_charset); | ||
170 | |||
171 | return filename; | ||
172 | } | ||
173 | |||
174 | char * | ||
175 | GNUNET_GTK_from_filename_to_loc (gchar *filename) | ||
176 | { | ||
177 | char *str_loc; | ||
178 | const char *loc_charset; | ||
179 | const char *filename_charset; | ||
180 | gboolean is_loc_UTF8, is_filename_UTF8; | ||
181 | |||
182 | if (NULL == filename) | ||
183 | return NULL; | ||
184 | |||
185 | is_filename_UTF8 = get_filename_charset (&filename_charset); | ||
186 | is_loc_UTF8 = g_get_charset (&loc_charset); | ||
187 | if (is_loc_UTF8 && is_filename_UTF8) | ||
188 | str_loc = GNUNET_strdup (filename); | ||
189 | else if (is_loc_UTF8) | ||
190 | str_loc = GNUNET_STRINGS_to_utf8 (filename, strlen (filename), filename_charset); | ||
191 | else if (is_filename_UTF8) | ||
192 | str_loc = GNUNET_STRINGS_from_utf8 (filename, strlen (filename), loc_charset); | ||
193 | else | ||
194 | /* Pray that iconv() knows these charsets */ | ||
195 | str_loc = GNUNET_STRINGS_conv (filename, strlen (filename), filename_charset, loc_charset); | ||
196 | |||
197 | return str_loc; | ||
198 | } | ||
199 | |||
200 | /* Returns filename form filechooser, encoded in locale-dependent | ||
201 | * encoding, suitable to be given to CRT and/or GNUnet | ||
202 | */ | ||
203 | char * | ||
204 | GNUNET_GTK_filechooser_get_filename_loc (GtkFileChooser *fc) | ||
205 | { | ||
206 | char *filename_loc; | ||
207 | gchar *filename = gtk_file_chooser_get_filename (fc); | ||
208 | filename_loc = GNUNET_GTK_from_filename_to_loc (filename); | ||
209 | g_free (filename); | ||
210 | return filename_loc; | ||
211 | } | ||
212 | |||
213 | gchar * | ||
214 | GNUNET_GTK_filechooser_get_filename_utf8 (GtkFileChooser *fc) | ||
215 | { | ||
216 | char *filename_utf8; | ||
217 | gchar *filename = gtk_file_chooser_get_filename (fc); | ||
218 | filename_utf8 = GNUNET_GTK_from_filename_to_utf8 (filename); | ||
219 | g_free (filename); | ||
220 | return filename_utf8; | ||
221 | } | ||
222 | |||
56 | /* end of nls.c */ | 223 | /* end of nls.c */ |