summaryrefslogtreecommitdiff
path: root/src/dfi-builder.c
blob: 1821e9e5fc3448b01d3f8362ce737d3fac60505b (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Copyright © 2013 Canonical Limited
 *
 * update-desktop-database 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.
 *
 * update-desktop-database 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 update-desktop-database; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "dfi-builder.h"

#include "dfi-string-table.h"
#include "dfi-keyfile.h"
#include "dfi-text-index.h"
#include "dfi-string-list.h"
#include "dfi-id-list.h"

#include <string.h>
#include <unistd.h>
#include <locale.h>

typedef struct
{
  GHashTable *locale_string_tables;  /* string tables */

  DfiStringList *app_names;
  DfiStringList *key_names;
  DfiStringList *locale_names;
  DfiStringList *group_names;

  DfiTextIndex  *c_text_index;
  DfiTextIndex  *mime_types;

  GHashTable *locale_text_indexes;   /* str -> DfiTextIndex */
  GHashTable *implementations;       /* str -> DfiIdList */
  GHashTable *desktop_files;         /* str -> DfiKeyfile */

  GString    *string;                /* file contents */
} DfiBuilder;

#define foreach_sequence_item_and_position(iter, sequence, counter) \
  for (counter = 0, iter = g_sequence_get_begin_iter (sequence);        \
       !g_sequence_iter_is_end (iter);                                  \
       iter = g_sequence_iter_next (iter), counter++)

static GHashTable *
dfi_builder_get_string_table (DfiBuilder  *builder,
                              const gchar *locale)
{
  return dfi_string_tables_get_table (builder->locale_string_tables, locale);
}

static guint
dfi_builder_get_offset (DfiBuilder *builder)
{
  return builder->string->len;
}

static void
dfi_builder_align (DfiBuilder *builder,
                   guint       size)
{
  while (builder->string->len & (size - 1))
    g_string_append_c (builder->string, '\0');
}

static guint
dfi_builder_get_aligned (DfiBuilder *builder,
                         guint       size)
{
  dfi_builder_align (builder, size);

  return dfi_builder_get_offset (builder);
}

static void
dfi_builder_check_alignment (DfiBuilder *builder,
                             guint       size)
{
  g_assert (~builder->string->len & (size - 1));
}

static guint
dfi_builder_write_uint16 (DfiBuilder *builder,
                          guint16     value)
{
  guint offset = dfi_builder_get_offset (builder);

  dfi_builder_check_alignment (builder, sizeof (guint16));

  value = GUINT16_TO_LE (value);

  g_string_append_len (builder->string, (gpointer) &value, sizeof value);

  return offset;
}

static guint
dfi_builder_write_uint32 (DfiBuilder *builder,
                          guint32     value)
{
  guint offset = dfi_builder_get_offset (builder);

  dfi_builder_check_alignment (builder, sizeof (guint32));

  value = GUINT32_TO_LE (value);

  g_string_append_len (builder->string, (gpointer) &value, sizeof value);

  return offset;
}

#if 0
static guint
dfi_builder_write_raw_string (DfiBuilder *builder,
                                             const gchar             *string)
{
  guint offset = dfi_builder_get_offset (builder);

  g_string_append (builder->string, string);
  g_string_append_c (builder->string, '\0');

  return offset;
}
XXX
#endif

static guint
dfi_builder_write_string (DfiBuilder  *builder,
                          const gchar *from_locale,
                          const gchar *string)
{
  guint offset;

  offset = dfi_string_tables_get_offset (builder->locale_string_tables, from_locale, string);

  return dfi_builder_write_uint32 (builder, offset);
}

static guint
dfi_builder_write_string_list (DfiBuilder    *builder,
                               DfiStringList *string_list)
{
  guint offset = dfi_builder_get_aligned (builder, sizeof (guint32));
  const gchar * const *strings;
  guint n, i;

  strings = dfi_string_list_get_strings (string_list, &n);

  dfi_builder_write_uint16 (builder, n);
  dfi_builder_write_uint16 (builder, 0xffff); /* padding */

  for (i = 0; i < n; i++)
    dfi_builder_write_string (builder, "", strings[i]);

  return offset;
}

static guint
dfi_builder_write_id (DfiBuilder    *builder,
                      DfiStringList *string_list,
                      const gchar   *string)
{
  guint value;

  if (string == NULL)
    return dfi_builder_write_uint16 (builder, G_MAXUINT16);

  value = dfi_string_list_get_id (string_list, string);

  return dfi_builder_write_uint16 (builder, (gsize) value);
}

static guint
dfi_builder_write_keyfile (DfiBuilder  *builder,
                           const gchar *app,
                           gpointer     data)
{
  guint offset = dfi_builder_get_aligned (builder, sizeof (guint16));
  DfiKeyfile *keyfile = data;
  gint n_groups, n_items;
  gint i;

  n_groups = dfi_keyfile_get_n_groups (keyfile);
  n_items = dfi_keyfile_get_n_items (keyfile);

  dfi_builder_write_uint16 (builder, n_groups);
  dfi_builder_write_uint16 (builder, n_items);

  for (i = 0; i < n_groups; i++)
    {
      const gchar *group_name;
      guint start;

      group_name = dfi_keyfile_get_group_name (keyfile, i);
      dfi_keyfile_get_group_range (keyfile, i, &start, NULL);

      dfi_builder_write_id (builder, builder->group_names, group_name);
      dfi_builder_write_uint16 (builder, start);
    }

  for (i = 0; i < n_items; i++)
    {
      const gchar *key, *locale, *value;

      dfi_keyfile_get_item (keyfile, i, &key, &locale, &value);

      dfi_builder_write_id (builder, builder->key_names, key);
      dfi_builder_write_id (builder, builder->locale_names, locale);
      dfi_builder_write_string (builder, locale, value);
    }

  return offset;
}

typedef guint (* DfiBuilderFunc) (DfiBuilder  *builder,
                                  const gchar *key,
                                  gpointer     data);

static guint
dfi_builder_write_pointer_array (DfiBuilder     *builder,
                                 DfiStringList  *key_list,
                                 guint           key_list_offset,
                                 GHashTable     *data_table,
                                 DfiBuilderFunc  func)
{
  const gchar * const *strings;
  guint *offsets;
  guint offset;
  guint n, i;

  strings = dfi_string_list_get_strings (key_list, &n);
  offsets = g_new0 (guint, n);

  for (i = 0; i < n; i++)
    {
      gpointer data;

      data = g_hash_table_lookup (data_table, strings[i]);
      offsets[i] = (* func) (builder, strings[i], data);
    }

  offset = dfi_builder_get_aligned (builder, sizeof (guint32));
  dfi_builder_write_uint32 (builder, key_list_offset);

  for (i = 0; i < n; i++)
    dfi_builder_write_uint32 (builder, offsets[i]);

  g_free (offsets);

  return offset;
}

static guint
dfi_builder_write_id_list (DfiBuilder  *builder,
                           const gchar *key,
                           gpointer     data)
{
  GArray *id_list = data;
  const guint16 *ids;
  guint offset;
  guint n_ids;
  guint i;

  ids = dfi_id_list_get_ids (id_list, &n_ids);

  offset = dfi_builder_write_uint16 (builder, n_ids);

  for (i = 0; i < n_ids; i++)
    dfi_builder_write_uint16 (builder, ids[i]);

  return offset;
}

static guint
dfi_builder_write_text_index (DfiBuilder  *builder,
                              const gchar *key,
                              gpointer     data)
{
  GSequence *text_index = data;
  const gchar *locale = key;
  GHashTable *string_table;
  GSequenceIter *iter;
  const gchar **strings;
  guint *id_lists;
  guint offset;
  guint n_items;
  guint i;

  string_table = dfi_builder_get_string_table (builder, locale);
  if (!dfi_string_table_is_written (string_table))
    {
      GHashTable *c_string_table;

      c_string_table = dfi_string_tables_get_table (builder->locale_string_tables, "");
      dfi_string_table_write (string_table, c_string_table, builder->string);
    }

  n_items = g_sequence_get_length (text_index);

  strings = g_new (const gchar *, n_items);
  id_lists = g_new (guint, n_items);

  dfi_builder_align (builder, sizeof (guint16));

  foreach_sequence_item_and_position (iter, text_index, i)
    {
      GArray *id_list;

      dfi_text_index_get_item (iter, &strings[i], &id_list);
      id_lists[i] = dfi_builder_write_id_list (builder, NULL, id_list);
    }

  dfi_builder_align (builder, sizeof (guint32));

  offset = dfi_builder_get_offset (builder);

  dfi_builder_write_uint32 (builder, n_items);

  for (i = 0; i < n_items; i++)
    {
      dfi_builder_write_string (builder, locale, strings[i]);
      dfi_builder_write_uint32 (builder, id_lists[i]);
    }

  g_free (strings);
  g_free (id_lists);

  return offset;
}

static void
dfi_builder_serialise (DfiBuilder *builder)
{
  guint32 header_fields[8] = { 0, };

  builder->string = g_string_new (NULL);

  /* Make room for the header */
  g_string_append_len (builder->string, (char *) header_fields, sizeof header_fields);

  /* Write out the C string table, filling in the offsets
   *
   * We have to do this first because all of the string lists (apps,
   * keys, locales, groups) are stored as strings in the C locale.
   */
  {
    GHashTable *c_table;

    c_table = dfi_builder_get_string_table (builder, "");
    dfi_string_table_write (c_table, NULL, builder->string);
  }

  /* Write out the string lists.  This will work because they only
   * refer to strings in the C locale.
   */
  {
    header_fields[0] = dfi_builder_write_string_list (builder, builder->app_names);
    header_fields[1] = dfi_builder_write_string_list (builder, builder->key_names);
    header_fields[2] = dfi_builder_write_string_list (builder, builder->locale_names);
    header_fields[3] = dfi_builder_write_string_list (builder, builder->group_names);
  }

  /* Write out the group implementors */
  {
    /*
    header_fields[4] = dfi_builder_write_pointer_array (builder,
                                                                       builder->group_names,
                                                                       header_fields[3],
                                                                       builder->group_implementors,
                                                                       dfi_builder_write_id_list);
                                                                       */
  }

  /* Write out the text indexes for the actual locales.
   *
   * Note: we do this by visiting each item in the locale string list,
   * which doesn't include the C locale, so we won't end up emitting the
   * C locale again here.
   *
   * Note: this function will write out the locale-specific string
   * tables alongside the table for each locale in order to improve
   * locality.
   */
  {
    header_fields[5] = dfi_builder_write_pointer_array (builder,
                                                                       builder->locale_names,
                                                                       header_fields[2],
                                                                       builder->locale_text_indexes,
                                                                       dfi_builder_write_text_index);
  }

  /* Write out the desktop file contents.
   *
   * We have to do this last because the desktop files refer to strings
   * from all the locales and those are only actually written in the
   * last step.
   *
   * TODO: we could improve things a bit by storing the desktop files at
   * the front of the cache, but this would require a two-pass
   * approach...
   */
  {
    header_fields[6] = dfi_builder_write_pointer_array (builder,
                                                                       builder->app_names,
                                                                       header_fields[0],
                                                                       builder->desktop_files,
                                                                       dfi_builder_write_keyfile);
  }

  /* Write out the mime types index */
  {
    //header_fields[7] = dfi_builder_write_text_index (builder, NULL, builder->mime_types);
  }

  /* Replace the header */
  {
    guint32 *file = (guint32 *) builder->string->str;
    guint i;

    for (i = 0; i < G_N_ELEMENTS (header_fields); i++)
      file[i] = GUINT32_TO_LE (header_fields[i]);
  }
}

static void
dfi_builder_add_strings_for_keyfile (DfiBuilder *builder,
                                     DfiKeyfile *keyfile)
{
  gint n_groups, n_items;
  gint i;

  n_groups = dfi_keyfile_get_n_groups (keyfile);
  n_items = dfi_keyfile_get_n_items (keyfile);

  for (i = 0; i < n_groups; i++)
    {
      const gchar *group_name;

      group_name = dfi_keyfile_get_group_name (keyfile, i);
      dfi_string_list_ensure (builder->group_names, group_name);
    }

  for (i = 0; i < n_items; i++)
    {
      const gchar *key, *locale, *value;

      dfi_keyfile_get_item (keyfile, i, &key, &locale, &value);

      dfi_string_list_ensure (builder->key_names, key);
      dfi_string_list_ensure (builder->locale_names, locale); /* may be "" */
      dfi_string_tables_add_string (builder->locale_string_tables, locale, value);
    }
}

static void
dfi_builder_add_strings (DfiBuilder *builder)
{
  GHashTableIter keyfile_iter;
  gpointer key, value;

  builder->locale_string_tables = dfi_string_tables_create ();
  builder->app_names = dfi_string_list_new ();
  builder->key_names = dfi_string_list_new ();
  builder->locale_names = dfi_string_list_new ();
  builder->group_names = dfi_string_list_new ();

  g_hash_table_iter_init (&keyfile_iter, builder->desktop_files);
  while (g_hash_table_iter_next (&keyfile_iter, &key, &value))
    {
      DfiKeyfile *keyfile = value;
      const gchar *app = key;

      dfi_string_list_ensure (builder->app_names, app);
      dfi_builder_add_strings_for_keyfile (builder, keyfile);
    }

  dfi_string_list_convert (builder->app_names);
  dfi_string_list_convert (builder->group_names);
  dfi_string_list_convert (builder->key_names);
  dfi_string_list_convert (builder->locale_names);

  {
    GHashTable *c_string_table;

    c_string_table = dfi_string_tables_get_table (builder->locale_string_tables, "");

    dfi_string_list_populate_strings (builder->app_names, c_string_table);
    dfi_string_list_populate_strings (builder->group_names, c_string_table);
    dfi_string_list_populate_strings (builder->key_names, c_string_table);
    dfi_string_list_populate_strings (builder->locale_names, c_string_table);
  }
}

static GSequence *
dfi_builder_index_one_locale (DfiBuilder  *builder,
                              const gchar *locale)
{
  const gchar *fields[] = { "Name", "GenericName", "X-GNOME-FullName", "Comment", "Keywords" };
  gchar **locale_variants;
  GHashTableIter keyfile_iter;
  gpointer key, val;
  GSequence *text_index;

  if (locale)
    locale_variants = g_get_locale_variants (locale);
  else
    locale_variants = g_new0 (gchar *, 0 + 1);

  text_index = dfi_text_index_new ();

  g_hash_table_iter_init (&keyfile_iter, builder->desktop_files);
  while (g_hash_table_iter_next (&keyfile_iter, &key, &val))
    {
      DfiKeyfile *kf = val;
      const gchar *app = key;
      guint i;

      for (i = 0; i < G_N_ELEMENTS (fields); i++)
        {
          const gchar *value;

          value = dfi_keyfile_get_value (kf, (const gchar **) locale_variants, "Desktop Entry", fields[i]);

          if (value)
            {
              guint16 ids[3];

              ids[0] = dfi_string_list_get_id (builder->app_names, app);
              ids[1] = dfi_string_list_get_id (builder->group_names, "Desktop Entry");
              ids[2] = dfi_string_list_get_id (builder->key_names, fields[i]);

              dfi_text_index_add_ids_tokenised (text_index, value, ids, 3);
            }
        }
    }

  g_free (locale_variants);

  return text_index;
}

static void
dfi_builder_index_strings (DfiBuilder *builder)
{
  const gchar * const *locale_names;
  GHashTable *c_string_table;
  guint i;

  c_string_table = dfi_string_tables_get_table (builder->locale_string_tables, "");
  builder->c_text_index = dfi_builder_index_one_locale (builder, "");
  dfi_text_index_populate_strings (builder->c_text_index, c_string_table);

  locale_names = dfi_string_list_get_strings (builder->locale_names, NULL);

  builder->locale_text_indexes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, dfi_text_index_free);

  for (i = 0; locale_names[i]; i++)
    {
      const gchar *locale = locale_names[i];
      GHashTable *string_table;
      GSequence *text_index;

      text_index = dfi_builder_index_one_locale (builder, locale);
      g_hash_table_insert (builder->locale_text_indexes, g_strdup (locale), text_index);
      string_table = dfi_string_tables_get_table (builder->locale_string_tables, locale);
      dfi_text_index_populate_strings (text_index, string_table);
    }
}

static DfiBuilder *
dfi_builder_new (void)
{
  DfiBuilder *builder;

  builder = g_slice_new0 (DfiBuilder);
  builder->desktop_files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) dfi_keyfile_free);
  builder->string = NULL;

  return builder;
}

static gboolean
dfi_builder_add_desktop_file (DfiBuilder   *builder,
                              const gchar  *desktop_id,
                              const gchar  *filename,
                              GError      **error)
{
  DfiKeyfile *kf;

  kf = dfi_keyfile_new (filename, error);
  if (!kf)
    return FALSE;

  g_hash_table_insert (builder->desktop_files, g_strdup (desktop_id), kf);

  return TRUE;
}

GBytes *
dfi_builder_build (const gchar  *desktop_dir,
                   GError      **error)
{
  DfiBuilder *builder;
  const gchar *name;
  GDir *dir;

  builder = dfi_builder_new ();

  dir = g_dir_open (desktop_dir, 0, error);
  if (!dir)
    return NULL;

  while ((name = g_dir_read_name (dir)))
    {
      gboolean success;
      gchar *fullname;

      if (!g_str_has_suffix (name, ".desktop"))
        continue;

      fullname = g_build_filename (desktop_dir, name, NULL);
      success = dfi_builder_add_desktop_file (builder, name, fullname, error);
      g_free (fullname);

      if (!success)
        return NULL;
    }
  g_dir_close (dir);

  dfi_builder_add_strings (builder);

  dfi_builder_index_strings (builder);

  dfi_builder_serialise (builder);

  return g_bytes_new (builder->string->str, builder->string->len);
}