[Automatic update for 2018-04-08]
[metze/wireshark/wip.git] / ui / language.c
1 /* language.c
2  * Language "preference" handling routines
3  * Copyright 2014, Michal Labedzki for Tieto Corporation
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later*/
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <errno.h>
15
16 #include <epan/prefs.h>
17 #include <epan/prefs-int.h>
18
19 #include <wsutil/filesystem.h>
20 #include <wsutil/file_util.h>
21
22 #include "ui/language.h"
23 #include "ui/simple_dialog.h"
24
25 #define LANGUAGE_FILE_NAME      "language"
26 #define LANGUAGE_PREF_LANGUAGE  "language"
27
28 char *language = NULL;
29
30 /* set one user's recent common file key/value pair */
31 static prefs_set_pref_e
32 read_language_pref(gchar *key, const gchar *value,
33                    void *private_data _U_, gboolean return_range_errors _U_)
34 {
35     if (strcmp(key, LANGUAGE_PREF_LANGUAGE) == 0) {
36         g_free(language);
37         /*
38          * For backwards compatibility, treat "auto" as meaning "use the
39          * system language".
40          *
41          * To handle the old buggy code that didn't check whether "language"
42          * was null before trying to print it, treat "(null)" - which many,
43          * but *NOT* all, system printfs print for a null pointer (some
44          * printfs, such as the one in Solaris, *crash* with %s and a null
45          * pointer) - as meaning "use the system language".
46          */
47         if (!value || !*value || strcmp(value, "auto") == 0 ||
48             strcmp(value, "(null)") == 0)
49             language = g_strdup(USE_SYSTEM_LANGUAGE);
50         else
51             language = g_strdup(value);
52     }
53
54     return PREFS_SET_OK;
55 }
56
57 void
58 read_language_prefs(void)
59 {
60     char       *rf_path;
61     FILE       *rf;
62
63     rf_path = get_persconffile_path(LANGUAGE_FILE_NAME, FALSE);
64
65     if ((rf = ws_fopen(rf_path, "r")) != NULL) {
66         read_prefs_file(rf_path, rf, read_language_pref, NULL);
67
68         fclose(rf);
69     }
70
71     g_free(rf_path);
72 }
73
74 gboolean
75 write_language_prefs(void)
76 {
77     char        *pf_dir_path;
78     char        *rf_path;
79     FILE        *rf;
80
81     /* To do:
82     * - Split output lines longer than MAX_VAL_LEN
83     * - Create a function for the preference directory check/creation
84     *   so that duplication can be avoided with filter.c
85     */
86
87     /* Create the directory that holds personal configuration files, if
88         necessary.  */
89     if (create_persconffile_dir(&pf_dir_path) == -1) {
90         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
91         "Can't create directory\n\"%s\"\nfor language file: %s.", pf_dir_path,
92         g_strerror(errno));
93         g_free(pf_dir_path);
94         return FALSE;
95     }
96
97     rf_path = get_persconffile_path(LANGUAGE_FILE_NAME, FALSE);
98     if ((rf = ws_fopen(rf_path, "w")) == NULL) {
99         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
100         "Can't open recent file\n\"%s\": %s.", rf_path,
101         g_strerror(errno));
102         g_free(rf_path);
103         return FALSE;
104     }
105     g_free(rf_path);
106
107     fputs("# Language settings file for Wireshark " VERSION ".\n"
108         "#\n"
109         "# This file is regenerated each time Wireshark is quit.\n"
110         "# So be careful, if you want to make manual changes here.\n"
111         "\n", rf);
112
113     fprintf(rf, LANGUAGE_PREF_LANGUAGE ": %s\n", language ? language : USE_SYSTEM_LANGUAGE);
114
115     fclose(rf);
116
117     return TRUE;
118 }
119
120 /*
121  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
122  *
123  * Local variables:
124  * c-basic-offset: 4
125  * tab-width: 8
126  * indent-tabs-mode: nil
127  * End:
128  *
129  * vi: set shiftwidth=4 tabstop=8 expandtab:
130  * :indentSize=4:tabSize=8:noTabs=true:
131  */