]>
Commit | Line | Data |
---|---|---|
dcb445bc | 1 | /* |
7e86f2f6 | 2 | * Destination localization support for CUPS. |
dcb445bc | 3 | * |
89550f3f | 4 | * Copyright 2012-2017 by Apple Inc. |
dcb445bc | 5 | * |
e3101897 | 6 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
dcb445bc MS |
7 | */ |
8 | ||
9 | /* | |
10 | * Include necessary headers... | |
11 | */ | |
12 | ||
13 | #include "cups-private.h" | |
14 | ||
15 | ||
a29fd7dd MS |
16 | /* |
17 | * Local functions... | |
18 | */ | |
19 | ||
20 | static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo); | |
a29fd7dd MS |
21 | |
22 | ||
104fd4ae MS |
23 | /* |
24 | * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media | |
25 | * size. | |
26 | * | |
27 | * The returned string is stored in the destination information and will become | |
28 | * invalid if the destination information is deleted. | |
29 | * | |
8072030b | 30 | * @since CUPS 2.0/macOS 10.10@ |
104fd4ae MS |
31 | */ |
32 | ||
33 | const char * /* O - Localized string */ | |
34 | cupsLocalizeDestMedia( | |
35 | http_t *http, /* I - Connection to destination */ | |
36 | cups_dest_t *dest, /* I - Destination */ | |
37 | cups_dinfo_t *dinfo, /* I - Destination information */ | |
38 | unsigned flags, /* I - Media flags */ | |
39 | cups_size_t *size) /* I - Media size */ | |
40 | { | |
41 | cups_lang_t *lang; /* Standard localizations */ | |
42 | _cups_message_t key, /* Search key */ | |
43 | *match; /* Matching entry */ | |
44 | pwg_media_t *pwg; /* PWG media information */ | |
45 | cups_array_t *db; /* Media database */ | |
46 | _cups_media_db_t *mdb; /* Media database entry */ | |
71dc43af | 47 | char lstr[1024], /* Localized size name */ |
104fd4ae MS |
48 | temp[256]; /* Temporary string */ |
49 | const char *lsize, /* Localized media size */ | |
50 | *lsource, /* Localized media source */ | |
51 | *ltype; /* Localized media type */ | |
52 | ||
53 | ||
a9357c9d MS |
54 | DEBUG_printf(("cupsLocalizeDestMedia(http=%p, dest=%p, dinfo=%p, flags=%x, size=%p(\"%s\"))", (void *)http, (void *)dest, (void *)dinfo, flags, (void *)size, size ? size->media : "(null)")); |
55 | ||
104fd4ae MS |
56 | /* |
57 | * Range check input... | |
58 | */ | |
59 | ||
60 | if (!http || !dest || !dinfo || !size) | |
61 | { | |
a9357c9d | 62 | DEBUG_puts("1cupsLocalizeDestMedia: Returning NULL."); |
104fd4ae MS |
63 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
64 | return (NULL); | |
65 | } | |
66 | ||
67 | /* | |
71dc43af | 68 | * Find the matching media database entry... |
104fd4ae MS |
69 | */ |
70 | ||
71dc43af MS |
71 | if (flags & CUPS_MEDIA_FLAGS_READY) |
72 | db = dinfo->ready_db; | |
73 | else | |
74 | db = dinfo->media_db; | |
104fd4ae | 75 | |
71dc43af MS |
76 | DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size->media)); |
77 | ||
78 | for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db)) | |
79 | { | |
80 | if (mdb->key && !strcmp(mdb->key, size->media)) | |
81 | break; | |
82 | else if (mdb->size_name && !strcmp(mdb->size_name, size->media)) | |
83 | break; | |
84 | } | |
85 | ||
86 | if (!mdb) | |
a9357c9d | 87 | { |
71dc43af MS |
88 | for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db)) |
89 | { | |
90 | if (mdb->width == size->width && mdb->length == size->length && mdb->bottom == size->bottom && mdb->left == size->left && mdb->right == size->right && mdb->top == size->top) | |
91 | break; | |
92 | } | |
a9357c9d | 93 | } |
104fd4ae MS |
94 | |
95 | /* | |
71dc43af | 96 | * See if the localization is cached... |
104fd4ae MS |
97 | */ |
98 | ||
99 | lang = cupsLangDefault(); | |
a9357c9d | 100 | |
71dc43af MS |
101 | if (!dinfo->localizations) |
102 | cups_create_localizations(http, dinfo); | |
103 | ||
a9357c9d | 104 | snprintf(temp, sizeof(temp), "media.%s", size->media); |
71dc43af MS |
105 | key.msg = temp; |
106 | ||
107 | if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL) | |
a9357c9d | 108 | { |
71dc43af | 109 | lsize = match->str; |
a9357c9d | 110 | } |
71dc43af MS |
111 | else |
112 | { | |
113 | /* | |
114 | * Not a media name, try a media-key name... | |
115 | */ | |
a9357c9d | 116 | |
71dc43af MS |
117 | snprintf(temp, sizeof(temp), "media-key.%s", size->media); |
118 | if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL) | |
119 | lsize = match->str; | |
120 | else | |
121 | lsize = NULL; | |
122 | } | |
104fd4ae | 123 | |
71dc43af MS |
124 | if (!lsize && (pwg = pwgMediaForSize(size->width, size->length)) != NULL && pwg->ppd) |
125 | { | |
126 | /* | |
127 | * Get a standard localization... | |
128 | */ | |
129 | ||
130 | snprintf(temp, sizeof(temp), "media.%s", pwg->pwg); | |
131 | if ((lsize = _cupsLangString(lang, temp)) == temp) | |
132 | lsize = NULL; | |
133 | } | |
104fd4ae MS |
134 | |
135 | if (!lsize) | |
136 | { | |
71dc43af MS |
137 | /* |
138 | * Make a dimensional localization... | |
139 | */ | |
140 | ||
104fd4ae MS |
141 | if ((size->width % 635) == 0 && (size->length % 635) == 0) |
142 | { | |
143 | /* | |
144 | * Use inches since the size is a multiple of 1/4 inch. | |
145 | */ | |
146 | ||
a9357c9d | 147 | snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g \"")), size->width / 2540.0, size->length / 2540.0); |
104fd4ae MS |
148 | } |
149 | else | |
150 | { | |
151 | /* | |
152 | * Use millimeters since the size is not a multiple of 1/4 inch. | |
153 | */ | |
154 | ||
155 | snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%d x %d mm")), (size->width + 50) / 100, (size->length + 50) / 100); | |
156 | } | |
157 | ||
158 | lsize = temp; | |
159 | } | |
160 | ||
104fd4ae MS |
161 | if (mdb) |
162 | { | |
807315e6 | 163 | DEBUG_printf(("1cupsLocalizeDestMedia: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb, mdb->key, mdb->size_name, mdb->source, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top)); |
85cbdbc7 | 164 | |
104fd4ae MS |
165 | lsource = cupsLocalizeDestValue(http, dest, dinfo, "media-source", mdb->source); |
166 | ltype = cupsLocalizeDestValue(http, dest, dinfo, "media-type", mdb->type); | |
167 | } | |
168 | else | |
169 | { | |
170 | lsource = NULL; | |
171 | ltype = NULL; | |
172 | } | |
173 | ||
174 | if (!lsource && !ltype) | |
175 | { | |
982be458 | 176 | if (!size->bottom && !size->left && !size->right && !size->top) |
71dc43af | 177 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless)")), lsize); |
104fd4ae | 178 | else |
71dc43af | 179 | strlcpy(lstr, lsize, sizeof(lstr)); |
104fd4ae MS |
180 | } |
181 | else if (!lsource) | |
182 | { | |
982be458 | 183 | if (!size->bottom && !size->left && !size->right && !size->top) |
71dc43af | 184 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, ltype); |
104fd4ae | 185 | else |
71dc43af | 186 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, ltype); |
104fd4ae MS |
187 | } |
188 | else if (!ltype) | |
189 | { | |
982be458 | 190 | if (!size->bottom && !size->left && !size->right && !size->top) |
71dc43af | 191 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, lsource); |
104fd4ae | 192 | else |
71dc43af | 193 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s)")), lsize, lsource); |
104fd4ae MS |
194 | } |
195 | else | |
196 | { | |
982be458 | 197 | if (!size->bottom && !size->left && !size->right && !size->top) |
71dc43af | 198 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (Borderless, %s, %s)")), lsize, ltype, lsource); |
104fd4ae | 199 | else |
71dc43af | 200 | snprintf(lstr, sizeof(lstr), _cupsLangString(lang, _("%s (%s, %s)")), lsize, ltype, lsource); |
104fd4ae MS |
201 | } |
202 | ||
203 | if ((match = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL) | |
204 | return (NULL); | |
205 | ||
89550f3f | 206 | match->msg = strdup(size->media); |
71dc43af | 207 | match->str = strdup(lstr); |
104fd4ae MS |
208 | |
209 | cupsArrayAdd(dinfo->localizations, match); | |
210 | ||
a9357c9d MS |
211 | DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match->str)); |
212 | ||
104fd4ae MS |
213 | return (match->str); |
214 | } | |
215 | ||
216 | ||
dcb445bc MS |
217 | /* |
218 | * 'cupsLocalizeDestOption()' - Get the localized string for a destination | |
219 | * option. | |
220 | * | |
a29fd7dd MS |
221 | * The returned string is stored in the destination information and will become |
222 | * invalid if the destination information is deleted. | |
dcb445bc | 223 | * |
8072030b | 224 | * @since CUPS 1.6/macOS 10.8@ |
dcb445bc MS |
225 | */ |
226 | ||
227 | const char * /* O - Localized string */ | |
228 | cupsLocalizeDestOption( | |
229 | http_t *http, /* I - Connection to destination */ | |
230 | cups_dest_t *dest, /* I - Destination */ | |
231 | cups_dinfo_t *dinfo, /* I - Destination information */ | |
232 | const char *option) /* I - Option to localize */ | |
233 | { | |
a29fd7dd MS |
234 | _cups_message_t key, /* Search key */ |
235 | *match; /* Matching entry */ | |
a9357c9d MS |
236 | const char *localized; /* Localized string */ |
237 | ||
a29fd7dd | 238 | |
a9357c9d | 239 | DEBUG_printf(("cupsLocalizeDestOption(http=%p, dest=%p, dinfo=%p, option=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option)); |
a29fd7dd MS |
240 | |
241 | if (!http || !dest || !dinfo) | |
242 | return (option); | |
243 | ||
244 | if (!dinfo->localizations) | |
245 | cups_create_localizations(http, dinfo); | |
246 | ||
89550f3f MS |
247 | key.msg = (char *)option; |
248 | if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL) | |
a29fd7dd | 249 | return (match->str); |
a9357c9d MS |
250 | else if ((localized = _cupsLangString(cupsLangDefault(), option)) != NULL) |
251 | return (localized); | |
a29fd7dd MS |
252 | else |
253 | return (option); | |
dcb445bc MS |
254 | } |
255 | ||
256 | ||
257 | /* | |
258 | * 'cupsLocalizeDestValue()' - Get the localized string for a destination | |
259 | * option+value pair. | |
260 | * | |
a29fd7dd MS |
261 | * The returned string is stored in the destination information and will become |
262 | * invalid if the destination information is deleted. | |
dcb445bc | 263 | * |
8072030b | 264 | * @since CUPS 1.6/macOS 10.8@ |
dcb445bc MS |
265 | */ |
266 | ||
267 | const char * /* O - Localized string */ | |
268 | cupsLocalizeDestValue( | |
269 | http_t *http, /* I - Connection to destination */ | |
270 | cups_dest_t *dest, /* I - Destination */ | |
271 | cups_dinfo_t *dinfo, /* I - Destination information */ | |
272 | const char *option, /* I - Option to localize */ | |
273 | const char *value) /* I - Value to localize */ | |
274 | { | |
a29fd7dd MS |
275 | _cups_message_t key, /* Search key */ |
276 | *match; /* Matching entry */ | |
277 | char pair[256]; /* option.value pair */ | |
a9357c9d | 278 | const char *localized; /* Localized string */ |
a29fd7dd MS |
279 | |
280 | ||
a9357c9d MS |
281 | DEBUG_printf(("cupsLocalizeDestValue(http=%p, dest=%p, dinfo=%p, option=\"%s\", value=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option, value)); |
282 | ||
a29fd7dd MS |
283 | if (!http || !dest || !dinfo) |
284 | return (value); | |
285 | ||
a9357c9d MS |
286 | if (!strcmp(option, "media")) |
287 | { | |
288 | pwg_media_t *media = pwgMediaForPWG(value); | |
289 | cups_size_t size; | |
290 | ||
291 | strlcpy(size.media, value, sizeof(size.media)); | |
292 | size.width = media ? media->width : 0; | |
293 | size.length = media ? media->length : 0; | |
294 | size.left = 0; | |
295 | size.right = 0; | |
296 | size.bottom = 0; | |
297 | size.top = 0; | |
298 | ||
299 | return (cupsLocalizeDestMedia(http, dest, dinfo, CUPS_MEDIA_FLAGS_DEFAULT, &size)); | |
300 | } | |
301 | ||
a29fd7dd MS |
302 | if (!dinfo->localizations) |
303 | cups_create_localizations(http, dinfo); | |
304 | ||
a29fd7dd | 305 | snprintf(pair, sizeof(pair), "%s.%s", option, value); |
89550f3f MS |
306 | key.msg = pair; |
307 | if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL) | |
a29fd7dd | 308 | return (match->str); |
a9357c9d MS |
309 | else if ((localized = _cupsLangString(cupsLangDefault(), pair)) != NULL && strcmp(localized, pair)) |
310 | return (localized); | |
a29fd7dd MS |
311 | else |
312 | return (value); | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * 'cups_create_localizations()' - Create the localizations array for a | |
318 | * destination. | |
319 | */ | |
320 | ||
321 | static void | |
322 | cups_create_localizations( | |
323 | http_t *http, /* I - Connection to destination */ | |
324 | cups_dinfo_t *dinfo) /* I - Destination informations */ | |
325 | { | |
326 | http_t *http2; /* Connection for strings file */ | |
327 | http_status_t status; /* Request status */ | |
328 | ipp_attribute_t *attr; /* "printer-strings-uri" attribute */ | |
329 | char scheme[32], /* URI scheme */ | |
330 | userpass[256], /* Username/password info */ | |
331 | hostname[256], /* Hostname */ | |
332 | resource[1024], /* Resource */ | |
333 | http_hostname[256], | |
334 | /* Hostname of connection */ | |
335 | tempfile[1024]; /* Temporary filename */ | |
336 | int port; /* Port number */ | |
337 | http_encryption_t encryption; /* Encryption to use */ | |
338 | cups_file_t *temp; /* Temporary file */ | |
339 | ||
340 | ||
a29fd7dd MS |
341 | /* |
342 | * See if there are any localizations... | |
343 | */ | |
344 | ||
345 | if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri", | |
346 | IPP_TAG_URI)) == NULL) | |
347 | { | |
348 | /* | |
89550f3f | 349 | * Nope, create an empty message catalog... |
a29fd7dd MS |
350 | */ |
351 | ||
89550f3f MS |
352 | dinfo->localizations = _cupsMessageNew(NULL); |
353 | DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) value."); | |
354 | return; | |
a29fd7dd MS |
355 | } |
356 | ||
357 | /* | |
358 | * Pull apart the URI and determine whether we need to try a different | |
359 | * server... | |
360 | */ | |
361 | ||
362 | if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, | |
363 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
364 | hostname, sizeof(hostname), &port, resource, | |
cb7f98ee | 365 | sizeof(resource)) < HTTP_URI_STATUS_OK) |
a29fd7dd | 366 | { |
89550f3f MS |
367 | dinfo->localizations = _cupsMessageNew(NULL); |
368 | DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value \"%s\".", attr->values[0].string.text)); | |
a29fd7dd MS |
369 | return; |
370 | } | |
371 | ||
372 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); | |
373 | ||
374 | if (!_cups_strcasecmp(http_hostname, hostname) && | |
a469f8a5 | 375 | port == httpAddrPort(http->hostaddr)) |
a29fd7dd MS |
376 | { |
377 | /* | |
378 | * Use the same connection... | |
379 | */ | |
380 | ||
381 | http2 = http; | |
382 | } | |
383 | else | |
384 | { | |
385 | /* | |
386 | * Connect to the alternate host... | |
387 | */ | |
388 | ||
389 | if (!strcmp(scheme, "https")) | |
cb7f98ee | 390 | encryption = HTTP_ENCRYPTION_ALWAYS; |
a29fd7dd | 391 | else |
cb7f98ee | 392 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
a29fd7dd | 393 | |
cb7f98ee MS |
394 | if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, |
395 | 30000, NULL)) == NULL) | |
a29fd7dd MS |
396 | { |
397 | DEBUG_printf(("4cups_create_localizations: Unable to connect to " | |
398 | "%s:%d: %s", hostname, port, cupsLastErrorString())); | |
399 | return; | |
400 | } | |
401 | } | |
402 | ||
403 | /* | |
404 | * Get a temporary file... | |
405 | */ | |
406 | ||
407 | if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL) | |
408 | { | |
409 | DEBUG_printf(("4cups_create_localizations: Unable to create temporary " | |
410 | "file: %s", cupsLastErrorString())); | |
411 | if (http2 != http) | |
412 | httpClose(http2); | |
413 | return; | |
414 | } | |
415 | ||
416 | status = cupsGetFd(http2, resource, cupsFileNumber(temp)); | |
89550f3f | 417 | cupsFileClose(temp); |
a29fd7dd | 418 | |
89550f3f | 419 | DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, httpStatus(status))); |
a29fd7dd | 420 | |
cb7f98ee | 421 | if (status == HTTP_STATUS_OK) |
a29fd7dd MS |
422 | { |
423 | /* | |
424 | * Got the file, read it... | |
425 | */ | |
426 | ||
89550f3f | 427 | dinfo->localizations = _cupsMessageLoad(tempfile, _CUPS_MESSAGE_STRINGS); |
a29fd7dd MS |
428 | } |
429 | ||
430 | DEBUG_printf(("4cups_create_localizations: %d messages loaded.", | |
431 | cupsArrayCount(dinfo->localizations))); | |
432 | ||
433 | /* | |
434 | * Cleanup... | |
435 | */ | |
436 | ||
437 | unlink(tempfile); | |
a29fd7dd MS |
438 | |
439 | if (http2 != http) | |
440 | httpClose(http2); | |
441 | } | |
442 |