1 /* gdict-common.h - shared code between application and applet
2 *
3 * This file is part of GNOME Dictionary
4 *
5 * Copyright (C) 2005 Emmanuele Bassi
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28 #include <errno.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <glib/gstdio.h>
33
34 #include <gtk/gtk.h>
35
36 #include "gdict-common.h"
37
38 gchar *
gdict_get_data_dir(void)39 gdict_get_data_dir (void)
40 {
41 gchar *retval;
42
43 retval = g_build_filename (g_get_user_data_dir (),
44 g_get_prgname (),
45 NULL);
46 return retval;
47 }
48
49 /* legacy data dir. pre 3.3.4 */
50 gchar *
gdict_get_old_data_dir(void)51 gdict_get_old_data_dir (void)
52 {
53 gchar *retval;
54
55 retval = g_build_filename (g_get_home_dir (),
56 ".gnome2",
57 "gnome-dictionary",
58 NULL);
59
60 return retval;
61 }
62
63 gchar *
gdict_get_config_dir(void)64 gdict_get_config_dir (void)
65 {
66 gchar *retval;
67
68 retval = g_build_filename (g_get_user_config_dir (),
69 g_get_prgname (),
70 NULL);
71 return retval;
72 }
73
74 static gboolean
gdict_migrate_configs(void)75 gdict_migrate_configs (void)
76 {
77 gchar *old_data_dir_name; // this one was used for configs only
78 gchar *config_dir_name;
79 gboolean res = TRUE;
80
81 old_data_dir_name = gdict_get_old_data_dir ();
82 config_dir_name = gdict_get_config_dir ();
83
84 /* move configs from pre-XDG directory to right place */
85 if (g_file_test (old_data_dir_name, G_FILE_TEST_IS_DIR))
86 {
87 g_message ("Migrating old configs to XDG directory layout...");
88
89 if (g_rename (old_data_dir_name, config_dir_name) == -1)
90 {
91 g_critical ("Unable to rename file '%s' to '%s': %s",
92 old_data_dir_name,
93 config_dir_name,
94 g_strerror (errno));
95
96 res = FALSE;
97 }
98 }
99
100 g_free (config_dir_name);
101 g_free (old_data_dir_name);
102
103 return res;
104 }
105
106 gboolean
gdict_create_config_dir(void)107 gdict_create_config_dir (void)
108 {
109 gchar *config_dir_name;
110 gboolean res = TRUE;
111
112 config_dir_name = gdict_get_config_dir ();
113
114 gdict_migrate_configs ();
115
116 if (!g_file_test (config_dir_name, G_FILE_TEST_IS_DIR))
117 {
118 if (!g_file_test (config_dir_name, G_FILE_TEST_IS_DIR)) {
119 g_message ("Creating XDG config directory: %s", config_dir_name);
120
121 if (g_mkdir (config_dir_name, 0700) == -1)
122 {
123 g_critical ("Unable to create directory '%s': %s",
124 config_dir_name,
125 g_strerror (errno));
126
127 res = FALSE;
128 }
129 }
130 }
131
132 g_free (config_dir_name);
133 return res;
134 }
135
136 /* create the data directory inside $HOME, if it doesn't exist yet */
137 gboolean
gdict_create_data_dir(void)138 gdict_create_data_dir (void)
139 {
140 gchar *data_dir_name;
141
142 data_dir_name = gdict_get_data_dir ();
143
144 if (g_mkdir (data_dir_name, 0700) == -1)
145 {
146 /* this is weird, but sometimes there's a "gnome-dictionary" file
147 * inside $HOME/.gnome2; see bug #329126.
148 */
149 if ((errno == EEXIST) &&
150 (g_file_test (data_dir_name, G_FILE_TEST_IS_REGULAR)))
151 {
152 gchar *backup = g_strdup_printf ("%s.pre-2-14", data_dir_name);
153
154 if (g_rename (data_dir_name, backup) == -1)
155 {
156 g_critical ("Unable to rename file '%s' to '%s': %s",
157 data_dir_name,
158 backup,
159 g_strerror (errno));
160
161 g_free (backup);
162
163 goto error;
164 }
165
166 g_free (backup);
167
168 if (g_mkdir (data_dir_name, 0700) == -1)
169 {
170 g_critical ("Unable to create the data directory '%s': %s",
171 data_dir_name,
172 g_strerror (errno));
173
174 goto error;
175 }
176
177 goto success;
178 }
179
180 if (errno != EEXIST)
181 {
182 g_critical ("Unable to create the data directory '%s': %s",
183 data_dir_name,
184 g_strerror (errno));
185 goto error;
186 }
187 }
188
189 success:
190 g_free (data_dir_name);
191
192 return TRUE;
193
194 error:
195 g_free (data_dir_name);
196
197 return FALSE;
198 }
199
200 /* shows an error dialog making it transient for @parent */
201 void
gdict_show_error_dialog(GtkWindow * parent,const gchar * message,const gchar * detail)202 gdict_show_error_dialog (GtkWindow *parent,
203 const gchar *message,
204 const gchar *detail)
205 {
206 GtkWidget *dialog;
207
208 g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
209 g_return_if_fail (message != NULL);
210
211 dialog = gtk_message_dialog_new (parent,
212 GTK_DIALOG_DESTROY_WITH_PARENT,
213 GTK_MESSAGE_ERROR,
214 GTK_BUTTONS_OK,
215 "%s", message);
216 gtk_window_set_title (GTK_WINDOW (dialog), "");
217
218 if (detail)
219 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
220 "%s", detail);
221
222 if (parent && gtk_window_get_group (parent))
223 gtk_window_group_add_window (gtk_window_get_group (parent), GTK_WINDOW (dialog));
224
225 gtk_dialog_run (GTK_DIALOG (dialog));
226
227 gtk_widget_destroy (dialog);
228 }
229
230 void
gdict_show_gerror_dialog(GtkWindow * parent,const gchar * message,GError * error)231 gdict_show_gerror_dialog (GtkWindow *parent,
232 const gchar *message,
233 GError *error)
234 {
235 g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
236 g_return_if_fail (message != NULL);
237 g_return_if_fail (error != NULL);
238
239 gdict_show_error_dialog (parent, message, error->message);
240
241 g_error_free (error);
242 error = NULL;
243 }
244
245 gboolean
gdict_is_devel_build(void)246 gdict_is_devel_build (void)
247 {
248 #ifdef DEVELOPMENT_BUILD
249 return TRUE;
250 #else
251 return FALSE;
252 #endif
253 }
254