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