summaryrefslogtreecommitdiff
path: root/mate-dictionary/src/gdict-common.c
blob: 5a78d95d30027cf5b76b439d1e7e890678a6cec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (C) 2005 Emmanuele Bassi
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This file is part of MATE Utils.
 *
 * MATE Utils is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MATE Utils is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MATE Utils.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <errno.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include <gtk/gtk.h>

#include "gdict-common.h"

gchar *
gdict_get_data_dir (void)
{
  gchar *retval;

  retval = g_build_filename (g_get_user_config_dir (),
		  	     "mate",
			     "mate-dictionary",
			     NULL);

  return retval;
}

/* create the data directory inside $HOME, if it doesn't exist yet */
gboolean
gdict_create_data_dir (void)
{
  gchar *data_dir_name;

  data_dir_name = gdict_get_data_dir ();
  if (g_mkdir_with_parents (data_dir_name, 0700) == -1)
    {
      /* this is weird, but sometimes there's a "mate-dictionary" file
       * inside $HOME/.mate2; see bug #329126.
       */
      if ((errno == EEXIST) &&
          (g_file_test (data_dir_name, G_FILE_TEST_IS_REGULAR)))
        {
          gchar *backup = g_strdup_printf ("%s.pre-2-14", data_dir_name);

	  if (g_rename (data_dir_name, backup) == -1)
	    {
              GtkWidget *error_dialog;

	      error_dialog = gtk_message_dialog_new (NULL,
                                                     GTK_DIALOG_MODAL,
						     GTK_MESSAGE_ERROR,
						     GTK_BUTTONS_CLOSE,
						     _("Unable to rename file '%s' to '%s': %s"),
						     data_dir_name,
						     backup,
						     g_strerror (errno));

	      gtk_dialog_run (GTK_DIALOG (error_dialog));

	      gtk_widget_destroy (error_dialog);
	      g_free (backup);
	      g_free (data_dir_name);

	      return FALSE;
            }

	  g_free (backup);

          if (g_mkdir_with_parents (data_dir_name, 0700) == -1)
            {
              GtkWidget *error_dialog;

	      error_dialog = gtk_message_dialog_new (NULL,
						     GTK_DIALOG_MODAL,
						     GTK_MESSAGE_ERROR,
						     GTK_BUTTONS_CLOSE,
						     _("Unable to create the data directory '%s': %s"),
						     data_dir_name,
						     g_strerror (errno));

	      gtk_dialog_run (GTK_DIALOG (error_dialog));

	      gtk_widget_destroy (error_dialog);
              g_free (data_dir_name);

	      return FALSE;
            }

	  goto success;
	}

      if (errno != EEXIST)
        {
          GtkWidget *error_dialog;

	  error_dialog = gtk_message_dialog_new (NULL,
						 GTK_DIALOG_MODAL,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_CLOSE,
						 _("Unable to create the data directory '%s': %s"),
						 data_dir_name,
						 g_strerror (errno));

	  gtk_dialog_run (GTK_DIALOG (error_dialog));

	  gtk_widget_destroy (error_dialog);
	  g_free (data_dir_name);

	  return FALSE;
	}
    }

success:
  g_free (data_dir_name);

  return TRUE;
}

/* shows an error dialog making it transient for @parent */
void
gdict_show_error_dialog (GtkWindow   *parent,
			 const gchar *message,
			 const gchar *detail)
{
  GtkWidget *dialog;

  g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
  g_return_if_fail (message != NULL);

  dialog = gtk_message_dialog_new (parent,
  				   GTK_DIALOG_DESTROY_WITH_PARENT,
  				   GTK_MESSAGE_ERROR,
  				   GTK_BUTTONS_OK,
  				   "%s", message);
  gtk_window_set_title (GTK_WINDOW (dialog), "");

  if (detail)
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
  					      "%s", detail);

  if (parent && gtk_window_get_group (parent))
    gtk_window_group_add_window (gtk_window_get_group (parent), GTK_WINDOW (dialog));

  gtk_dialog_run (GTK_DIALOG (dialog));

  gtk_widget_destroy (dialog);
}

void
gdict_show_gerror_dialog (GtkWindow   *parent,
			  const gchar *message,
			  GError      *error)
{
  g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
  g_return_if_fail (message != NULL);
  g_return_if_fail (error != NULL);

  gdict_show_error_dialog (parent, message, error->message);

  g_error_free (error);
  error = NULL;
}