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