~adam-stokes/ubuntu/quantal/gnome-vfs/lp977940-multiarch

« back to all changes in this revision

Viewing changes to libgnomevfs/xdgmime.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-08-05 10:31:15 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080805103115-5vkex0yk6qq619x5
Tags: 1:2.23.0-0ubuntu1
* New upstream version:
  - Sync xdgmime with GIO
* debian/patches/90_from_svn_no_range_for_youtube.patch:
  - the change is in the new version

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 */
27
27
 
28
28
#ifdef HAVE_CONFIG_H
29
 
#include <config.h>
 
29
#include "config.h"
30
30
#endif
31
31
 
32
32
#include "xdgmime.h"
34
34
#include "xdgmimeglob.h"
35
35
#include "xdgmimemagic.h"
36
36
#include "xdgmimealias.h"
 
37
#include "xdgmimeicon.h"
37
38
#include "xdgmimeparent.h"
38
39
#include "xdgmimecache.h"
39
40
#include <stdio.h>
56
57
static XdgParentList *parent_list = NULL;
57
58
static XdgDirTimeList *dir_time_list = NULL;
58
59
static XdgCallbackList *callback_list = NULL;
 
60
static XdgIconList *icon_list = NULL;
 
61
static XdgIconList *generic_icon_list = NULL;
59
62
 
60
63
XdgMimeCache **_caches = NULL;
61
64
static int n_caches = 0;
93
96
typedef int (*XdgDirectoryFunc) (const char *directory,
94
97
                                 void       *user_data);
95
98
 
96
 
static XdgDirTimeList *
97
 
xdg_dir_time_list_new (void)
 
99
static void
 
100
xdg_dir_time_list_add (char   *file_name, 
 
101
                       time_t  mtime)
98
102
{
99
 
  XdgDirTimeList *retval;
100
 
 
101
 
  retval = calloc (1, sizeof (XdgDirTimeList));
102
 
  retval->checked = XDG_CHECKED_UNCHECKED;
103
 
 
104
 
  return retval;
 
103
  XdgDirTimeList *list;
 
104
 
 
105
  for (list = dir_time_list; list; list = list->next) 
 
106
    {
 
107
      if (strcmp (list->directory_name, file_name) == 0) 
 
108
        return;
 
109
    }
 
110
  
 
111
  list = calloc (1, sizeof (XdgDirTimeList));
 
112
  list->checked = XDG_CHECKED_UNCHECKED;
 
113
  list->directory_name = file_name;
 
114
  list->mtime = mtime;
 
115
  list->next = dir_time_list;
 
116
  dir_time_list = list;
105
117
}
106
 
 
 
118
 
107
119
static void
108
120
xdg_dir_time_list_free (XdgDirTimeList *list)
109
121
{
123
135
{
124
136
  char *file_name;
125
137
  struct stat st;
126
 
  XdgDirTimeList *list;
127
138
 
128
139
  assert (directory != NULL);
129
140
 
135
146
 
136
147
      if (cache != NULL)
137
148
        {
138
 
          list = xdg_dir_time_list_new ();
139
 
          list->directory_name = file_name;
140
 
          list->mtime = st.st_mtime;
141
 
          list->next = dir_time_list;
142
 
          dir_time_list = list;
 
149
          xdg_dir_time_list_add (file_name, st.st_mtime);
143
150
 
144
151
          _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
145
152
          _caches[n_caches] = cache;
151
158
    }
152
159
  free (file_name);
153
160
 
154
 
  file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
155
 
  strcpy (file_name, directory); strcat (file_name, "/mime/globs");
 
161
  file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
 
162
  strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
156
163
  if (stat (file_name, &st) == 0)
157
164
    {
158
165
      _xdg_mime_glob_read_from_file (global_hash, file_name);
159
 
 
160
 
      list = xdg_dir_time_list_new ();
161
 
      list->directory_name = file_name;
162
 
      list->mtime = st.st_mtime;
163
 
      list->next = dir_time_list;
164
 
      dir_time_list = list;
 
166
      xdg_dir_time_list_add (file_name, st.st_mtime);
165
167
    }
166
168
  else
167
169
    {
168
170
      free (file_name);
 
171
      file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
 
172
      strcpy (file_name, directory); strcat (file_name, "/mime/globs");
 
173
      if (stat (file_name, &st) == 0)
 
174
        {
 
175
          _xdg_mime_glob_read_from_file (global_hash, file_name);
 
176
          xdg_dir_time_list_add (file_name, st.st_mtime);
 
177
        }
 
178
      else
 
179
        {
 
180
          free (file_name);
 
181
        }
169
182
    }
170
183
 
171
184
  file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
173
186
  if (stat (file_name, &st) == 0)
174
187
    {
175
188
      _xdg_mime_magic_read_from_file (global_magic, file_name);
176
 
 
177
 
      list = xdg_dir_time_list_new ();
178
 
      list->directory_name = file_name;
179
 
      list->mtime = st.st_mtime;
180
 
      list->next = dir_time_list;
181
 
      dir_time_list = list;
 
189
      xdg_dir_time_list_add (file_name, st.st_mtime);
182
190
    }
183
191
  else
184
192
    {
195
203
  _xdg_mime_parent_read_from_file (parent_list, file_name);
196
204
  free (file_name);
197
205
 
 
206
  file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
 
207
  strcpy (file_name, directory); strcat (file_name, "/mime/icons");
 
208
  _xdg_mime_icon_read_from_file (icon_list, file_name);
 
209
  free (file_name);
 
210
 
 
211
  file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
 
212
  strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
 
213
  _xdg_mime_icon_read_from_file (generic_icon_list, file_name);
 
214
  free (file_name);
 
215
 
198
216
  return FALSE; /* Keep processing */
199
217
}
200
218
 
296
314
 
297
315
      for (list = dir_time_list; list; list = list->next)
298
316
        {
299
 
          if (! strcmp (list->directory_name, file_path) &&
300
 
              st.st_mtime == list->mtime)
 
317
          if (! strcmp (list->directory_name, file_path))
301
318
            {
302
 
              if (list->checked == XDG_CHECKED_UNCHECKED)
 
319
              if (st.st_mtime == list->mtime)
303
320
                list->checked = XDG_CHECKED_VALID;
304
 
              else if (list->checked == XDG_CHECKED_VALID)
 
321
              else 
305
322
                list->checked = XDG_CHECKED_INVALID;
306
323
 
307
324
              return (list->checked != XDG_CHECKED_VALID);
430
447
      global_magic = _xdg_mime_magic_new ();
431
448
      alias_list = _xdg_mime_alias_list_new ();
432
449
      parent_list = _xdg_mime_parent_list_new ();
 
450
      icon_list = _xdg_mime_icon_list_new ();
 
451
      generic_icon_list = _xdg_mime_icon_list_new ();
433
452
 
434
453
      xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
435
454
                               NULL);
440
459
 
441
460
const char *
442
461
xdg_mime_get_mime_type_for_data (const void *data,
443
 
                                 size_t      len)
 
462
                                 size_t      len,
 
463
                                 int        *result_prio)
444
464
{
445
465
  const char *mime_type;
446
466
 
447
467
  xdg_mime_init ();
448
468
 
449
469
  if (_caches)
450
 
    return _xdg_mime_cache_get_mime_type_for_data (data, len);
 
470
    return _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
451
471
 
452
 
  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
 
472
  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
453
473
 
454
474
  if (mime_type)
455
475
    return mime_type;
524
544
      return XDG_MIME_TYPE_UNKNOWN;
525
545
    }
526
546
 
527
 
  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
 
547
  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
528
548
                                           mime_types, n);
529
549
 
530
550
  free (data);
553
573
}
554
574
 
555
575
int
 
576
xdg_mime_get_mime_types_from_file_name (const char *file_name,
 
577
                                        const char  *mime_types[],
 
578
                                        int          n_mime_types)
 
579
{
 
580
  xdg_mime_init ();
 
581
  
 
582
  if (_caches)
 
583
    return _xdg_mime_cache_get_mime_types_from_file_name (file_name, mime_types, n_mime_types);
 
584
  
 
585
  return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
 
586
}
 
587
 
 
588
int
556
589
xdg_mime_is_valid_mime_type (const char *mime_type)
557
590
{
558
591
  /* FIXME: We should make this a better test
594
627
      _xdg_mime_parent_list_free (parent_list);
595
628
      parent_list = NULL;
596
629
    }
 
630
 
 
631
  if (icon_list)
 
632
    {
 
633
      _xdg_mime_icon_list_free (icon_list);
 
634
      icon_list = NULL;
 
635
    }
 
636
 
 
637
  if (generic_icon_list)
 
638
    {
 
639
      _xdg_mime_icon_list_free (generic_icon_list);
 
640
      generic_icon_list = NULL;
 
641
    }
597
642
  
598
643
  if (_caches)
599
644
    {
675
720
{
676
721
  char *sep;
677
722
 
678
 
  xdg_mime_init ();
679
 
 
680
723
  sep = strchr (mime_a, '/');
681
724
  
682
725
  if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
791
834
void 
792
835
xdg_mime_dump (void)
793
836
{
 
837
  xdg_mime_init();
 
838
 
794
839
  printf ("*** ALIASES ***\n\n");
795
840
  _xdg_mime_alias_list_dump (alias_list);
796
841
  printf ("\n*** PARENTS ***\n\n");
797
842
  _xdg_mime_parent_list_dump (parent_list);
 
843
  printf ("\n*** CACHE ***\n\n");
 
844
  _xdg_glob_hash_dump (global_hash);
 
845
  printf ("\n*** GLOBS ***\n\n");
 
846
  _xdg_glob_hash_dump (global_hash);
 
847
  printf ("\n*** GLOBS REVERSE TREE ***\n\n");
 
848
  _xdg_mime_cache_glob_dump ();
798
849
}
799
850
 
800
851
 
848
899
        }
849
900
    }
850
901
}
 
902
 
 
903
const char *
 
904
xdg_mime_get_icon (const char *mime)
 
905
{
 
906
  const char *icon;
 
907
 
 
908
  xdg_mime_init ();
 
909
  
 
910
  if (_caches)
 
911
    return _xdg_mime_cache_get_icon (mime);
 
912
 
 
913
  icon = _xdg_mime_icon_list_lookup (icon_list, mime);
 
914
 
 
915
  if (!icon)
 
916
    icon = xdg_mime_get_generic_icon (mime);
 
917
 
 
918
  return icon;
 
919
}
 
920
 
 
921
const char *
 
922
xdg_mime_get_generic_icon (const char *mime)
 
923
{
 
924
  xdg_mime_init ();
 
925
  
 
926
  if (_caches)
 
927
    return _xdg_mime_cache_get_generic_icon (mime);
 
928
 
 
929
  return _xdg_mime_icon_list_lookup (generic_icon_list, mime);
 
930
}