]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest-localization.c
Merge pull request #5170 from reddevillg/patch-1
[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 name[1024], /* 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 * See if the localization is cached...
69 */
70
71 if (!dinfo->localizations)
72 cups_create_localizations(http, dinfo);
73
74 key.msg = size->media;
75 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
76 {
77 DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match->str));
78 return (match->str);
79 }
80
81 /*
82 * If not, get the localized size, source, and type strings...
83 */
84
85 lang = cupsLangDefault();
86
87 snprintf(temp, sizeof(temp), "media.%s", size->media);
88 if ((lsize = _cupsLangString(lang, temp)) != NULL && strcmp(lsize, temp))
89 {
90 DEBUG_printf(("1cupsLocalizeDestMedia: Returning standard localization \"%s\".", lsize));
91 return (lsize);
92 }
93
94 pwg = pwgMediaForSize(size->width, size->length);
95
96 if (pwg->ppd)
97 lsize = _cupsLangString(lang, pwg->ppd);
98 else
99 lsize = NULL;
100
101 if (!lsize)
102 {
103 if ((size->width % 635) == 0 && (size->length % 635) == 0)
104 {
105 /*
106 * Use inches since the size is a multiple of 1/4 inch.
107 */
108
109 snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%g x %g \"")), size->width / 2540.0, size->length / 2540.0);
110 }
111 else
112 {
113 /*
114 * Use millimeters since the size is not a multiple of 1/4 inch.
115 */
116
117 snprintf(temp, sizeof(temp), _cupsLangString(lang, _("%d x %d mm")), (size->width + 50) / 100, (size->length + 50) / 100);
118 }
119
120 lsize = temp;
121 }
122
123 if (flags & CUPS_MEDIA_FLAGS_READY)
124 db = dinfo->ready_db;
125 else
126 db = dinfo->media_db;
127
128 DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size->media));
129
130 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
131 {
132 if (mdb->key && !strcmp(mdb->key, size->media))
133 break;
134 else if (mdb->size_name && !strcmp(mdb->size_name, size->media))
135 break;
136 }
137
138 if (!mdb)
139 {
140 for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
141 {
142 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)
143 break;
144 }
145 }
146
147 if (mdb)
148 {
149 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));
150
151 lsource = cupsLocalizeDestValue(http, dest, dinfo, "media-source", mdb->source);
152 ltype = cupsLocalizeDestValue(http, dest, dinfo, "media-type", mdb->type);
153 }
154 else
155 {
156 lsource = NULL;
157 ltype = NULL;
158 }
159
160 if (!lsource && !ltype)
161 {
162 if (size->bottom || size->left || size->right || size->top)
163 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless)")), lsize);
164 else
165 strlcpy(name, lsize, sizeof(name));
166 }
167 else if (!lsource)
168 {
169 if (size->bottom || size->left || size->right || size->top)
170 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, ltype);
171 else
172 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s)")), lsize, ltype);
173 }
174 else if (!ltype)
175 {
176 if (size->bottom || size->left || size->right || size->top)
177 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s)")), lsize, lsource);
178 else
179 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s)")), lsize, lsource);
180 }
181 else
182 {
183 if (size->bottom || size->left || size->right || size->top)
184 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (Borderless, %s, %s)")), lsize, ltype, lsource);
185 else
186 snprintf(name, sizeof(name), _cupsLangString(lang, _("%s (%s, %s)")), lsize, ltype, lsource);
187 }
188
189 if ((match = (_cups_message_t *)calloc(1, sizeof(_cups_message_t))) == NULL)
190 return (NULL);
191
192 match->msg = strdup(size->media);
193 match->str = strdup(name);
194
195 cupsArrayAdd(dinfo->localizations, match);
196
197 DEBUG_printf(("1cupsLocalizeDestMedia: Returning \"%s\".", match->str));
198
199 return (match->str);
200 }
201
202
203 /*
204 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
205 * option.
206 *
207 * The returned string is stored in the destination information and will become
208 * invalid if the destination information is deleted.
209 *
210 * @since CUPS 1.6/macOS 10.8@
211 */
212
213 const char * /* O - Localized string */
214 cupsLocalizeDestOption(
215 http_t *http, /* I - Connection to destination */
216 cups_dest_t *dest, /* I - Destination */
217 cups_dinfo_t *dinfo, /* I - Destination information */
218 const char *option) /* I - Option to localize */
219 {
220 _cups_message_t key, /* Search key */
221 *match; /* Matching entry */
222 const char *localized; /* Localized string */
223
224
225 DEBUG_printf(("cupsLocalizeDestOption(http=%p, dest=%p, dinfo=%p, option=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option));
226
227 if (!http || !dest || !dinfo)
228 return (option);
229
230 if (!dinfo->localizations)
231 cups_create_localizations(http, dinfo);
232
233 key.msg = (char *)option;
234 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
235 return (match->str);
236 else if ((localized = _cupsLangString(cupsLangDefault(), option)) != NULL)
237 return (localized);
238 else
239 return (option);
240 }
241
242
243 /*
244 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
245 * option+value pair.
246 *
247 * The returned string is stored in the destination information and will become
248 * invalid if the destination information is deleted.
249 *
250 * @since CUPS 1.6/macOS 10.8@
251 */
252
253 const char * /* O - Localized string */
254 cupsLocalizeDestValue(
255 http_t *http, /* I - Connection to destination */
256 cups_dest_t *dest, /* I - Destination */
257 cups_dinfo_t *dinfo, /* I - Destination information */
258 const char *option, /* I - Option to localize */
259 const char *value) /* I - Value to localize */
260 {
261 _cups_message_t key, /* Search key */
262 *match; /* Matching entry */
263 char pair[256]; /* option.value pair */
264 const char *localized; /* Localized string */
265
266
267 DEBUG_printf(("cupsLocalizeDestValue(http=%p, dest=%p, dinfo=%p, option=\"%s\", value=\"%s\")", (void *)http, (void *)dest, (void *)dinfo, option, value));
268
269 if (!http || !dest || !dinfo)
270 return (value);
271
272 if (!strcmp(option, "media"))
273 {
274 pwg_media_t *media = pwgMediaForPWG(value);
275 cups_size_t size;
276
277 strlcpy(size.media, value, sizeof(size.media));
278 size.width = media ? media->width : 0;
279 size.length = media ? media->length : 0;
280 size.left = 0;
281 size.right = 0;
282 size.bottom = 0;
283 size.top = 0;
284
285 return (cupsLocalizeDestMedia(http, dest, dinfo, CUPS_MEDIA_FLAGS_DEFAULT, &size));
286 }
287
288 if (!dinfo->localizations)
289 cups_create_localizations(http, dinfo);
290
291 snprintf(pair, sizeof(pair), "%s.%s", option, value);
292 key.msg = pair;
293 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations, &key)) != NULL)
294 return (match->str);
295 else if ((localized = _cupsLangString(cupsLangDefault(), pair)) != NULL && strcmp(localized, pair))
296 return (localized);
297 else
298 return (value);
299 }
300
301
302 /*
303 * 'cups_create_localizations()' - Create the localizations array for a
304 * destination.
305 */
306
307 static void
308 cups_create_localizations(
309 http_t *http, /* I - Connection to destination */
310 cups_dinfo_t *dinfo) /* I - Destination informations */
311 {
312 http_t *http2; /* Connection for strings file */
313 http_status_t status; /* Request status */
314 ipp_attribute_t *attr; /* "printer-strings-uri" attribute */
315 char scheme[32], /* URI scheme */
316 userpass[256], /* Username/password info */
317 hostname[256], /* Hostname */
318 resource[1024], /* Resource */
319 http_hostname[256],
320 /* Hostname of connection */
321 tempfile[1024]; /* Temporary filename */
322 int port; /* Port number */
323 http_encryption_t encryption; /* Encryption to use */
324 cups_file_t *temp; /* Temporary file */
325
326
327 /*
328 * See if there are any localizations...
329 */
330
331 if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
332 IPP_TAG_URI)) == NULL)
333 {
334 /*
335 * Nope, create an empty message catalog...
336 */
337
338 dinfo->localizations = _cupsMessageNew(NULL);
339 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) value.");
340 return;
341 }
342
343 /*
344 * Pull apart the URI and determine whether we need to try a different
345 * server...
346 */
347
348 if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
349 scheme, sizeof(scheme), userpass, sizeof(userpass),
350 hostname, sizeof(hostname), &port, resource,
351 sizeof(resource)) < HTTP_URI_STATUS_OK)
352 {
353 dinfo->localizations = _cupsMessageNew(NULL);
354 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value \"%s\".", attr->values[0].string.text));
355 return;
356 }
357
358 httpGetHostname(http, http_hostname, sizeof(http_hostname));
359
360 if (!_cups_strcasecmp(http_hostname, hostname) &&
361 port == httpAddrPort(http->hostaddr))
362 {
363 /*
364 * Use the same connection...
365 */
366
367 http2 = http;
368 }
369 else
370 {
371 /*
372 * Connect to the alternate host...
373 */
374
375 if (!strcmp(scheme, "https"))
376 encryption = HTTP_ENCRYPTION_ALWAYS;
377 else
378 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
379
380 if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
381 30000, NULL)) == NULL)
382 {
383 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
384 "%s:%d: %s", hostname, port, cupsLastErrorString()));
385 return;
386 }
387 }
388
389 /*
390 * Get a temporary file...
391 */
392
393 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
394 {
395 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
396 "file: %s", cupsLastErrorString()));
397 if (http2 != http)
398 httpClose(http2);
399 return;
400 }
401
402 status = cupsGetFd(http2, resource, cupsFileNumber(temp));
403 cupsFileClose(temp);
404
405 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource, httpStatus(status)));
406
407 if (status == HTTP_STATUS_OK)
408 {
409 /*
410 * Got the file, read it...
411 */
412
413 dinfo->localizations = _cupsMessageLoad(tempfile, _CUPS_MESSAGE_STRINGS);
414 }
415
416 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
417 cupsArrayCount(dinfo->localizations)));
418
419 /*
420 * Cleanup...
421 */
422
423 unlink(tempfile);
424
425 if (http2 != http)
426 httpClose(http2);
427 }
428