]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/dest-localization.c
Import CUPS v1.7.1
[thirdparty/cups.git] / cups / dest-localization.c
CommitLineData
dcb445bc 1/*
61515785 2 * "$Id: dest-localization.c 4216 2013-03-11 13:57:36Z msweet $"
dcb445bc
MS
3 *
4 * Destination localization support for CUPS.
5 *
cb7f98ee 6 * Copyright 2012-2013 by Apple Inc.
dcb445bc
MS
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
a29fd7dd
MS
18 * cupsLocalizeDestOption() - Get the localized string for a destination
19 * option.
20 * cupsLocalizeDestValue() - Get the localized string for a destination
21 * option+value pair.
22 * cups_create_localizations() - Create the localizations array for a
23 * destination.
24 * cups_read_strings() - Read a pair of strings from a .strings file.
25 * cups_scan_strings() - Scan a quoted string.
dcb445bc
MS
26 */
27
28/*
29 * Include necessary headers...
30 */
31
32#include "cups-private.h"
33
34
a29fd7dd
MS
35/*
36 * Local functions...
37 */
38
39static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
40static int cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize,
41 char **id, char **str);
42static char *cups_scan_strings(char *buffer);
43
44
dcb445bc
MS
45/*
46 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
47 * option.
48 *
a29fd7dd
MS
49 * The returned string is stored in the destination information and will become
50 * invalid if the destination information is deleted.
dcb445bc 51 *
f3c17241 52 * @since CUPS 1.6/OS X 10.8@
dcb445bc
MS
53 */
54
55const char * /* O - Localized string */
56cupsLocalizeDestOption(
57 http_t *http, /* I - Connection to destination */
58 cups_dest_t *dest, /* I - Destination */
59 cups_dinfo_t *dinfo, /* I - Destination information */
60 const char *option) /* I - Option to localize */
61{
a29fd7dd
MS
62 _cups_message_t key, /* Search key */
63 *match; /* Matching entry */
64
65
66 if (!http || !dest || !dinfo)
67 return (option);
68
69 if (!dinfo->localizations)
70 cups_create_localizations(http, dinfo);
71
72 if (cupsArrayCount(dinfo->localizations) == 0)
73 return (option);
74
75 key.id = (char *)option;
76 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
77 &key)) != NULL)
78 return (match->str);
79 else
80 return (option);
dcb445bc
MS
81}
82
83
84/*
85 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
86 * option+value pair.
87 *
a29fd7dd
MS
88 * The returned string is stored in the destination information and will become
89 * invalid if the destination information is deleted.
dcb445bc 90 *
f3c17241 91 * @since CUPS 1.6/OS X 10.8@
dcb445bc
MS
92 */
93
94const char * /* O - Localized string */
95cupsLocalizeDestValue(
96 http_t *http, /* I - Connection to destination */
97 cups_dest_t *dest, /* I - Destination */
98 cups_dinfo_t *dinfo, /* I - Destination information */
99 const char *option, /* I - Option to localize */
100 const char *value) /* I - Value to localize */
101{
a29fd7dd
MS
102 _cups_message_t key, /* Search key */
103 *match; /* Matching entry */
104 char pair[256]; /* option.value pair */
105
106
107 if (!http || !dest || !dinfo)
108 return (value);
109
110 if (!dinfo->localizations)
111 cups_create_localizations(http, dinfo);
112
113 if (cupsArrayCount(dinfo->localizations) == 0)
114 return (value);
115
116 snprintf(pair, sizeof(pair), "%s.%s", option, value);
117 key.id = pair;
118 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
119 &key)) != NULL)
120 return (match->str);
121 else
122 return (value);
123}
124
125
126/*
127 * 'cups_create_localizations()' - Create the localizations array for a
128 * destination.
129 */
130
131static void
132cups_create_localizations(
133 http_t *http, /* I - Connection to destination */
134 cups_dinfo_t *dinfo) /* I - Destination informations */
135{
136 http_t *http2; /* Connection for strings file */
137 http_status_t status; /* Request status */
138 ipp_attribute_t *attr; /* "printer-strings-uri" attribute */
139 char scheme[32], /* URI scheme */
140 userpass[256], /* Username/password info */
141 hostname[256], /* Hostname */
142 resource[1024], /* Resource */
143 http_hostname[256],
144 /* Hostname of connection */
145 tempfile[1024]; /* Temporary filename */
146 int port; /* Port number */
147 http_encryption_t encryption; /* Encryption to use */
148 cups_file_t *temp; /* Temporary file */
149
150
151 /*
152 * Create an empty message catalog...
153 */
154
155 dinfo->localizations = _cupsMessageNew(NULL);
156
157 /*
158 * See if there are any localizations...
159 */
160
161 if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
162 IPP_TAG_URI)) == NULL)
163 {
164 /*
165 * Nope...
166 */
167
168 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
169 "value.");
170 return; /* Nope */
171 }
172
173 /*
174 * Pull apart the URI and determine whether we need to try a different
175 * server...
176 */
177
178 if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
179 scheme, sizeof(scheme), userpass, sizeof(userpass),
180 hostname, sizeof(hostname), &port, resource,
cb7f98ee 181 sizeof(resource)) < HTTP_URI_STATUS_OK)
a29fd7dd
MS
182 {
183 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
184 "\"%s\".", attr->values[0].string.text));
185 return;
186 }
187
188 httpGetHostname(http, http_hostname, sizeof(http_hostname));
189
190 if (!_cups_strcasecmp(http_hostname, hostname) &&
a469f8a5 191 port == httpAddrPort(http->hostaddr))
a29fd7dd
MS
192 {
193 /*
194 * Use the same connection...
195 */
196
197 http2 = http;
198 }
199 else
200 {
201 /*
202 * Connect to the alternate host...
203 */
204
205 if (!strcmp(scheme, "https"))
cb7f98ee 206 encryption = HTTP_ENCRYPTION_ALWAYS;
a29fd7dd 207 else
cb7f98ee 208 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
a29fd7dd 209
cb7f98ee
MS
210 if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
211 30000, NULL)) == NULL)
a29fd7dd
MS
212 {
213 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
214 "%s:%d: %s", hostname, port, cupsLastErrorString()));
215 return;
216 }
217 }
218
219 /*
220 * Get a temporary file...
221 */
222
223 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
224 {
225 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
226 "file: %s", cupsLastErrorString()));
227 if (http2 != http)
228 httpClose(http2);
229 return;
230 }
231
232 status = cupsGetFd(http2, resource, cupsFileNumber(temp));
233
234 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource,
235 httpStatus(status)));
236
cb7f98ee 237 if (status == HTTP_STATUS_OK)
a29fd7dd
MS
238 {
239 /*
240 * Got the file, read it...
241 */
242
243 char buffer[8192], /* Message buffer */
244 *id, /* ID string */
245 *str; /* Translated message */
246 _cups_message_t *m; /* Current message */
247
248 lseek(cupsFileNumber(temp), 0, SEEK_SET);
249
250 while (cups_read_strings(temp, buffer, sizeof(buffer), &id, &str))
251 {
252 if ((m = malloc(sizeof(_cups_message_t))) == NULL)
253 break;
254
255 m->id = strdup(id);
256 m->str = strdup(str);
257
258 if (m->id && m->str)
259 cupsArrayAdd(dinfo->localizations, m);
260 else
261 {
262 if (m->id)
263 free(m->id);
264
265 if (m->str)
266 free(m->str);
267
268 free(m);
269 break;
270 }
271 }
272 }
273
274 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
275 cupsArrayCount(dinfo->localizations)));
276
277 /*
278 * Cleanup...
279 */
280
281 unlink(tempfile);
282 cupsFileClose(temp);
283
284 if (http2 != http)
285 httpClose(http2);
286}
287
288
289/*
290 * 'cups_read_strings()' - Read a pair of strings from a .strings file.
291 */
292
293static int /* O - 1 on success, 0 on failure */
294cups_read_strings(cups_file_t *strings, /* I - .strings file */
295 char *buffer, /* I - Line buffer */
296 size_t bufsize, /* I - Size of line buffer */
297 char **id, /* O - Pointer to ID string */
298 char **str) /* O - Pointer to translation string */
299{
300 char *bufptr; /* Pointer into buffer */
301
302
303 while (cupsFileGets(strings, buffer, bufsize))
304 {
305 if (buffer[0] != '\"')
306 continue;
307
308 *id = buffer + 1;
309 bufptr = cups_scan_strings(buffer);
310
311 if (*bufptr != '\"')
312 continue;
313
314 *bufptr++ = '\0';
315
316 while (*bufptr && *bufptr != '\"')
317 bufptr ++;
318
319 if (!*bufptr)
320 continue;
321
322 *str = bufptr + 1;
323 bufptr = cups_scan_strings(bufptr);
324
325 if (*bufptr != '\"')
326 continue;
327
328 *bufptr = '\0';
329
330 return (1);
331 }
332
333 return (0);
334}
335
336
337/*
338 * 'cups_scan_strings()' - Scan a quoted string.
339 */
340
341static char * /* O - End of string */
342cups_scan_strings(char *buffer) /* I - Start of string */
343{
344 char *bufptr; /* Pointer into string */
345
346
347 for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
348 {
349 if (*bufptr == '\\')
350 {
351 if (bufptr[1] >= '0' && bufptr[1] <= '3' &&
352 bufptr[2] >= '0' && bufptr[2] <= '7' &&
353 bufptr[3] >= '0' && bufptr[3] <= '7')
354 {
355 /*
356 * Decode \nnn octal escape...
357 */
358
359 *bufptr = ((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) |
360 (bufptr[3] - '0');
361 _cups_strcpy(bufptr + 1, bufptr + 4);
362 }
363 else
364 {
365 /*
366 * Decode \C escape...
367 */
368
369 _cups_strcpy(bufptr, bufptr + 1);
370 if (*bufptr == 'n')
371 *bufptr = '\n';
372 else if (*bufptr == 'r')
373 *bufptr = '\r';
374 else if (*bufptr == 't')
375 *bufptr = '\t';
376 }
377 }
378 }
379
380 return (bufptr);
dcb445bc
MS
381}
382
383
a29fd7dd 384
dcb445bc 385/*
61515785 386 * End of "$Id: dest-localization.c 4216 2013-03-11 13:57:36Z msweet $".
dcb445bc 387 */