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