summaryrefslogtreecommitdiffstats
path: root/app/core/gimptag.c
blob: 95c4954260488edb01dfdf9233b88b691f65f1dd (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimptag.c
 * Copyright (C) 2008 Aurimas Juška <aurisj@svn.gnome.org>
 *
 * 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 <glib-object.h>
#include <string.h>

#include "core-types.h"

#include "gimptag.h"


#define GIMP_TAG_INTERNAL_PREFIX "gimp:"


G_DEFINE_TYPE (GimpTag, gimp_tag, G_TYPE_OBJECT)

#define parent_class gimp_tag_parent_class


static void
gimp_tag_class_init (GimpTagClass *klass)
{
}

static void
gimp_tag_init (GimpTag *tag)
{
  tag->tag         = 0;
  tag->collate_key = 0;
  tag->internal    = FALSE;
}

/**
 * gimp_tag_new:
 * @tag_string: a tag name.
 *
 * If given tag name is not valid, an attempt will be made to fix it.
 *
 * Return value: a new #GimpTag object, or NULL if tag string is invalid and
 * cannot be fixed.
 **/
GimpTag *
gimp_tag_new (const char *tag_string)
{
  GimpTag *tag;
  gchar   *tag_name;
  gchar   *case_folded;
  gchar   *collate_key;

  g_return_val_if_fail (tag_string != NULL, NULL);

  tag_name = gimp_tag_string_make_valid (tag_string);
  if (! tag_name)
    return NULL;

  tag = g_object_new (GIMP_TYPE_TAG, NULL);

  tag->tag = g_quark_from_string (tag_name);

  case_folded = g_utf8_casefold (tag_name, -1);
  collate_key = g_utf8_collate_key (case_folded, -1);
  tag->collate_key = g_quark_from_string (collate_key);
  g_free (collate_key);
  g_free (case_folded);
  g_free (tag_name);

  return tag;
}

/**
 * gimp_tag_try_new:
 * @tag_string: a tag name.
 *
 * Similar to gimp_tag_new(), but returns NULL if tag is surely not equal
 * to any of currently created tags. It is useful for tag querying to avoid
 * unneeded comparisons. If tag is created, however, it does not mean that
 * it would necessarily match with some other tag.
 *
 * Return value: new #GimpTag object, or NULL if tag will not match with any
 * other #GimpTag.
 **/
GimpTag *
gimp_tag_try_new (const char *tag_string)
{
  GimpTag *tag;
  gchar   *tag_name;
  gchar   *case_folded;
  gchar   *collate_key;
  GQuark   tag_quark;
  GQuark   collate_key_quark;

  tag_name = gimp_tag_string_make_valid (tag_string);
  if (! tag_name)
    return NULL;

  case_folded = g_utf8_casefold (tag_name, -1);
  collate_key = g_utf8_collate_key (case_folded, -1);
  collate_key_quark = g_quark_try_string (collate_key);
  g_free (collate_key);
  g_free (case_folded);

  if (! collate_key_quark)
    {
      g_free (tag_name);
      return NULL;
    }

  tag_quark = g_quark_from_string (tag_name);
  g_free (tag_name);
  if (! tag_quark)
    return NULL;

  tag = g_object_new (GIMP_TYPE_TAG, NULL);
  tag->tag = tag_quark;
  tag->collate_key = collate_key_quark;

  return tag;
}

/**
 * gimp_tag_get_internal:
 * @tag: a gimp tag.
 *
 * Retrieve internal status of the tag.
 *
 * Return value: internal status of tag. Internal tags are not saved.
 **/
gboolean
gimp_tag_get_internal (GimpTag *tag)
{
  g_return_val_if_fail (GIMP_IS_TAG (tag), FALSE);

  return tag->internal;
}

/**
 * gimp_tag_set_internal:
 * @tag: a gimp tag.
 * @internal: desired tag internal status
 *
 * Set internal status of the tag. Internal tags are usually automatically
 * generated and will not be saved into users tag cache.
 *
 **/
void
gimp_tag_set_internal (GimpTag *tag, gboolean internal)
{
  g_return_if_fail (GIMP_IS_TAG (tag));

  tag->internal = internal;
}


/**
 * gimp_tag_get_name:
 * @tag: a gimp tag.
 *
 * Retrieve name of the tag.
 *
 * Return value: name of tag.
 **/
const gchar *
gimp_tag_get_name (GimpTag *tag)
{
  g_return_val_if_fail (GIMP_IS_TAG (tag), NULL);

  return g_quark_to_string (tag->tag);
}

/**
 * gimp_tag_get_hash:
 * @tag: a gimp tag.
 *
 * Hashing function which is useful, for example, to store #GimpTag in
 * a #GHashTable.
 *
 * Return value: hash value for tag.
 **/
guint
gimp_tag_get_hash (GimpTag *tag)
{
  g_return_val_if_fail (GIMP_IS_TAG (tag), -1);

  return tag->collate_key;
}

/**
 * gimp_tag_equals:
 * @tag:   a gimp tag.
 * @other: another gimp tag to compare with.
 *
 * Compares tags for equality according to tag comparison rules.
 *
 * Return value: TRUE if tags are equal, FALSE otherwise.
 **/
gboolean
gimp_tag_equals (GimpTag *tag,
                 GimpTag *other)
{
  g_return_val_if_fail (GIMP_IS_TAG (tag), FALSE);
  g_return_val_if_fail (GIMP_IS_TAG (other), FALSE);

  return tag->collate_key == other->collate_key;
}

/**
 * gimp_tag_compare_func:
 * @p1: pointer to left-hand #GimpTag object.
 * @p2: pointer to right-hand #GimpTag object.
 *
 * Compares tags according to tag comparison rules. Useful for sorting
 * functions.
 *
 * Return value: meaning of return value is the same as in strcmp().
 **/
int
gimp_tag_compare_func (const void *p1,
                       const void *p2)
{
  GimpTag      *t1 = GIMP_TAG (p1);
  GimpTag      *t2 = GIMP_TAG (p2);

  return g_strcmp0 (g_quark_to_string (t1->collate_key),
                    g_quark_to_string (t2->collate_key));
}

/**
 * gimp_tag_compare_with_string:
 * @tag:        a #GimpTag object.
 * @tag_string: the string to compare to.
 *
 * Compares tag and a string according to tag comparison rules. Similar to
 * gimp_tag_compare_func(), but can be used without creating temporary tag
 * object.
 *
 * Return value: meaning of return value is the same as in strcmp().
 **/
gint
gimp_tag_compare_with_string (GimpTag     *tag,
                              const gchar *tag_string)
{
  gchar        *case_folded;
  const gchar  *collate_key;
  gchar        *collate_key2;
  gint          result;

  g_return_val_if_fail (GIMP_IS_TAG (tag), 0);
  g_return_val_if_fail (tag_string != NULL, 0);

  collate_key = g_quark_to_string (tag->collate_key);
  case_folded = g_utf8_casefold (tag_string, -1);
  collate_key2 = g_utf8_collate_key (case_folded, -1);
  result = g_strcmp0 (collate_key, collate_key2);
  g_free (collate_key2);
  g_free (case_folded);

  return result;
}

/**
 * gimp_tag_has_prefix:
 * @tag:           a #GimpTag object.
 * @prefix_string: the prefix to compare to.
 *
 * Compares tag and a prefix according to tag comparison rules. Similar to
 * gimp_tag_compare_with_string(), but does not work on the collate key
 * because that can't be matched partially.
 *
 * Return value: wheher #tag starts with @prefix_string.
 **/
gboolean
gimp_tag_has_prefix (GimpTag     *tag,
                     const gchar *prefix_string)
{
  gchar    *case_folded1;
  gchar    *case_folded2;
  gboolean  has_prefix;

  g_return_val_if_fail (GIMP_IS_TAG (tag), FALSE);
  g_return_val_if_fail (prefix_string != NULL, FALSE);

  case_folded1 = g_utf8_casefold (g_quark_to_string (tag->tag), -1);
  case_folded2 = g_utf8_casefold (prefix_string, -1);

  has_prefix = g_str_has_prefix (case_folded1, case_folded2);

  g_free (case_folded1);
  g_free (case_folded2);

  g_printerr ("'%s' has prefix '%s': %d\n",
              g_quark_to_string (tag->tag), prefix_string, has_prefix);

  return has_prefix;
}

/**
 * gimp_tag_string_make_valid:
 * @tag_string: a text string.
 *
 * Tries to create a valid tag string from given @tag_string.
 *
 * Return value: a newly allocated tag string in case given @tag_string was
 * valid or could be fixed, otherwise NULL. Allocated value should be freed
 * using g_free().
 **/
gchar *
gimp_tag_string_make_valid (const gchar *tag_string)
{
  gchar    *tag;
  GString  *buffer;
  gchar    *tag_cursor;
  gunichar  c;

  g_return_val_if_fail (tag_string, NULL);

  tag = g_utf8_normalize (tag_string, -1, G_NORMALIZE_ALL);
  if (! tag)
    return NULL;

  tag = g_strstrip (tag);
  if (! *tag)
    {
      g_free (tag);
      return NULL;
    }

  buffer = g_string_new ("");
  tag_cursor = tag;
  if (g_str_has_prefix (tag_cursor, GIMP_TAG_INTERNAL_PREFIX))
    {
      tag_cursor += strlen (GIMP_TAG_INTERNAL_PREFIX);
    }
  do
    {
      c = g_utf8_get_char (tag_cursor);
      tag_cursor = g_utf8_next_char (tag_cursor);
      if (g_unichar_isprint (c)
          && ! gimp_tag_is_tag_separator (c))
        {
          g_string_append_unichar (buffer, c);
        }
    } while (c);

  g_free (tag);
  tag = g_string_free (buffer, FALSE);
  tag = g_strstrip (tag);

  if (! *tag)
    {
      g_free (tag);
      return NULL;
    }

  return tag;
}

/**
 * gimp_tag_is_tag_separator:
 * @c: Unicode character.
 *
 * Defines a set of characters that are considered tag separators. The
 * tag separators are hand-picked from the set of characters with the
 * Terminal_Punctuation property as specified in the version 5.1.0 of
 * the Unicode Standard.
 *
 * Return value: %TRUE if the character is a tag separator.
 */
gboolean
gimp_tag_is_tag_separator (gunichar c)
{
  switch (c)
    {
    case 0x002C: /* COMMA */
    case 0x060C: /* ARABIC COMMA */
    case 0x07F8: /* NKO COMMA */
    case 0x1363: /* ETHIOPIC COMMA */
    case 0x1802: /* MONGOLIAN COMMA */
    case 0x1808: /* MONGOLIAN MANCHU COMMA */
    case 0x3001: /* IDEOGRAPHIC COMMA */
    case 0xA60D: /* VAI COMMA */
    case 0xFE50: /* SMALL COMMA */
    case 0xFF0C: /* FULLWIDTH COMMA */
    case 0xFF64: /* HALFWIDTH IDEOGRAPHIC COMMA */
      return TRUE;

    default:
      return FALSE;
    }
}

/**
 * gimp_tag_or_null_ref:
 * @tag: a #GimpTag
 *
 * A simple wrapper around g_object_ref() that silently accepts #NULL.
 **/
void
gimp_tag_or_null_ref (GimpTag *tag_or_null)
{
  if (tag_or_null)
    {
      g_return_if_fail (GIMP_IS_TAG (tag_or_null));

      g_object_ref (tag_or_null);
    }
}

/**
 * gimp_tag_or_null_unref:
 * @tag: a #GimpTag
 *
 * A simple wrapper around g_object_unref() that silently accepts #NULL.
 **/
void
gimp_tag_or_null_unref (GimpTag *tag_or_null)
{
  if (tag_or_null)
    {
      g_return_if_fail (GIMP_IS_TAG (tag_or_null));

      g_object_unref (tag_or_null);
    }
}