1 /* Copyright (C) 2005 Emmanuele Bassi
2 * Copyright (C) 2012-2021 MATE Developers
3 *
4 * This file is part of MATE Utils.
5 *
6 * MATE Utils is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * MATE Utils is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with MATE Utils. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <sys/types.h>
29 #include <errno.h>
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <glib/gstdio.h>
34
35 #include <gtk/gtk.h>
36
37 #include "gdict-common.h"
38
39 gchar *
gdict_get_data_dir(void)40 gdict_get_data_dir (void)
41 {
42 gchar *retval;
43
44 retval = g_build_filename (g_get_user_config_dir (),
45 "mate",
46 "mate-dictionary",
47 NULL);
48
49 return retval;
50 }
51
52 /* create the data directory inside $HOME, if it doesn't exist yet */
53 gboolean
gdict_create_data_dir(void)54 gdict_create_data_dir (void)
55 {
56 gchar *data_dir_name;
57
58 data_dir_name = gdict_get_data_dir ();
59 if (g_mkdir_with_parents (data_dir_name, 0700) == -1)
60 {
61 /* this is weird, but sometimes there's a "mate-dictionary" file
62 * inside $HOME/.mate2; see bug #329126.
63 */
64 if ((errno == EEXIST) &&
65 (g_file_test (data_dir_name, G_FILE_TEST_IS_REGULAR)))
66 {
67 gchar *backup = g_strdup_printf ("%s.pre-2-14", data_dir_name);
68
69 if (g_rename (data_dir_name, backup) == -1)
70 {
71 GtkWidget *error_dialog;
72
73 error_dialog = gtk_message_dialog_new (NULL,
74 GTK_DIALOG_MODAL,
75 GTK_MESSAGE_ERROR,
76 GTK_BUTTONS_CLOSE,
77 _("Unable to rename file '%s' to '%s': %s"),
78 data_dir_name,
79 backup,
80 g_strerror (errno));
81
82 gtk_dialog_run (GTK_DIALOG (error_dialog));
83
84 gtk_widget_destroy (error_dialog);
85 g_free (backup);
86 g_free (data_dir_name);
87
88 return FALSE;
89 }
90
91 g_free (backup);
92
93 if (g_mkdir_with_parents (data_dir_name, 0700) == -1)
94 {
95 GtkWidget *error_dialog;
96
97 error_dialog = gtk_message_dialog_new (NULL,
98 GTK_DIALOG_MODAL,
99 GTK_MESSAGE_ERROR,
100 GTK_BUTTONS_CLOSE,
101 _("Unable to create the data directory '%s': %s"),
102 data_dir_name,
103 g_strerror (errno));
104
105 gtk_dialog_run (GTK_DIALOG (error_dialog));
106
107 gtk_widget_destroy (error_dialog);
108 g_free (data_dir_name);
109
110 return FALSE;
111 }
112
113 goto success;
114 }
115
116 if (errno != EEXIST)
117 {
118 GtkWidget *error_dialog;
119
120 error_dialog = gtk_message_dialog_new (NULL,
121 GTK_DIALOG_MODAL,
122 GTK_MESSAGE_ERROR,
123 GTK_BUTTONS_CLOSE,
124 _("Unable to create the data directory '%s': %s"),
125 data_dir_name,
126 g_strerror (errno));
127
128 gtk_dialog_run (GTK_DIALOG (error_dialog));
129
130 gtk_widget_destroy (error_dialog);
131 g_free (data_dir_name);
132
133 return FALSE;
134 }
135 }
136
137 success:
138 g_free (data_dir_name);
139
140 return TRUE;
141 }
142
143 /* shows an error dialog making it transient for @parent */
144 void
gdict_show_error_dialog(GtkWindow * parent,const gchar * message,const gchar * detail)145 gdict_show_error_dialog (GtkWindow *parent,
146 const gchar *message,
147 const gchar *detail)
148 {
149 GtkWidget *dialog;
150
151 g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
152 g_return_if_fail (message != NULL);
153
154 dialog = gtk_message_dialog_new (parent,
155 GTK_DIALOG_DESTROY_WITH_PARENT,
156 GTK_MESSAGE_ERROR,
157 GTK_BUTTONS_OK,
158 "%s", message);
159 gtk_window_set_title (GTK_WINDOW (dialog), "");
160
161 if (detail)
162 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
163 "%s", detail);
164
165 if (parent && gtk_window_get_group (parent))
166 gtk_window_group_add_window (gtk_window_get_group (parent), GTK_WINDOW (dialog));
167
168 gtk_dialog_run (GTK_DIALOG (dialog));
169
170 gtk_widget_destroy (dialog);
171 }
172
173 void
gdict_show_gerror_dialog(GtkWindow * parent,const gchar * message,GError * error)174 gdict_show_gerror_dialog (GtkWindow *parent,
175 const gchar *message,
176 GError *error)
177 {
178 g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
179 g_return_if_fail (message != NULL);
180 g_return_if_fail (error != NULL);
181
182 gdict_show_error_dialog (parent, message, error->message);
183
184 g_error_free (error);
185 error = NULL;
186 }
187