summaryrefslogtreecommitdiffstats
path: root/app/plug-in/gimppluginmanager-locale-domain.c
blob: ae0433b42f533579be4ca7ccd0f4f2dd96515f3c (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimppluginmanager-locale-domain.c
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gio/gio.h>

#include "libgimpbase/gimpbase.h"

#include "plug-in-types.h"

#include "gimppluginmanager.h"
#include "gimppluginmanager-locale-domain.h"


#define STD_PLUG_INS_LOCALE_DOMAIN GETTEXT_PACKAGE "-std-plug-ins"


typedef struct _GimpPlugInLocaleDomain GimpPlugInLocaleDomain;

struct _GimpPlugInLocaleDomain
{
  GFile *file;
  gchar *domain_name;
  gchar *domain_path;
};


void
gimp_plug_in_manager_locale_domain_exit (GimpPlugInManager *manager)
{
  GSList *list;

  g_return_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager));

  for (list = manager->locale_domains; list; list = list->next)
    {
      GimpPlugInLocaleDomain *domain = list->data;

      g_object_unref (domain->file);
      g_free (domain->domain_name);
      g_free (domain->domain_path);
      g_slice_free (GimpPlugInLocaleDomain, domain);
    }

  g_slist_free (manager->locale_domains);
  manager->locale_domains = NULL;
}

void
gimp_plug_in_manager_add_locale_domain (GimpPlugInManager *manager,
                                        GFile             *file,
                                        const gchar       *domain_name,
                                        const gchar       *domain_path)
{
  GimpPlugInLocaleDomain *domain;

  g_return_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (G_IS_FILE (file));
  g_return_if_fail (domain_name != NULL);

  domain = g_slice_new (GimpPlugInLocaleDomain);

  domain->file        = g_object_ref (file);
  domain->domain_name = g_strdup (domain_name);
  domain->domain_path = g_strdup (domain_path);

  manager->locale_domains = g_slist_prepend (manager->locale_domains, domain);

#ifdef VERBOSE
  g_print ("added locale domain \"%s\" for path \"%s\"\n",
           domain->domain_name ? domain->domain_name : "(null)",
           domain->domain_path ?
           gimp_filename_to_utf8 (domain->domain_path) : "(null)");
#endif
}

const gchar *
gimp_plug_in_manager_get_locale_domain (GimpPlugInManager  *manager,
                                        GFile              *file,
                                        const gchar       **domain_path)
{
  GSList *list;

  g_return_val_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager), NULL);
  g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);

  if (domain_path)
    *domain_path = gimp_locale_directory ();

  /*  A NULL prog_name is GIMP itself, return the default domain  */
  if (! file)
    return NULL;

  for (list = manager->locale_domains; list; list = list->next)
    {
      GimpPlugInLocaleDomain *domain = list->data;

      if (domain && domain->file &&
          g_file_equal (domain->file, file))
        {
          if (domain_path && domain->domain_path)
            *domain_path = domain->domain_path;

          return domain->domain_name;
        }
    }

  return STD_PLUG_INS_LOCALE_DOMAIN;
}

gint
gimp_plug_in_manager_get_locale_domains (GimpPlugInManager   *manager,
                                         gchar             ***locale_domains,
                                         gchar             ***locale_paths)
{
  GSList *list;
  GSList *unique = NULL;
  gint    n_domains;
  gint    i;

  g_return_val_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager), 0);
  g_return_val_if_fail (locale_domains != NULL, 0);
  g_return_val_if_fail (locale_paths != NULL, 0);

  for (list = manager->locale_domains; list; list = list->next)
    {
      GimpPlugInLocaleDomain *domain = list->data;
      GSList                 *tmp;

      for (tmp = unique; tmp; tmp = tmp->next)
        if (! strcmp (domain->domain_name, (const gchar *) tmp->data))
          break;

      if (! tmp)
        unique = g_slist_prepend (unique, domain);
    }

  unique = g_slist_reverse (unique);

  n_domains = g_slist_length (unique) + 1;

  *locale_domains = g_new0 (gchar *, n_domains + 1);
  *locale_paths   = g_new0 (gchar *, n_domains + 1);

  (*locale_domains)[0] = g_strdup (STD_PLUG_INS_LOCALE_DOMAIN);
  (*locale_paths)[0]   = g_strdup (gimp_locale_directory ());

  for (list = unique, i = 1; list; list = list->next, i++)
    {
      GimpPlugInLocaleDomain *domain = list->data;

      (*locale_domains)[i] = g_strdup (domain->domain_name);
      (*locale_paths)[i]   = (domain->domain_path ?
                              g_strdup (domain->domain_path) :
                              g_strdup (gimp_locale_directory ()));
    }

  g_slist_free (unique);

  return n_domains;
}