~libzeitgeist-developers/libzeitgeist/trunk

« back to all changes in this revision

Viewing changes to tools/zeitgeist-mimetypes.template

  • Committer: Mikkel Kamstrup Erlandsen
  • Date: 2010-11-09 10:17:28 UTC
  • mfrom: (166.1.1 libzg-import-mimetypes)
  • Revision ID: mikkel.kamstrup@gmail.com-20101109101728-sej803plheibmpqm
Merge Michal Hruby's branch lp:~mhr3/libzeitgeist/libzg-mimetypes-import:

 * Autogenerate zeitgeist-mimetypes.c from the mimetype specs in the Zeitgeist Python implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Canonical, Ltd.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License
 
6
 * version 3.0 as published by the Free Software Foundation.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License version 3.0 for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library. If not, see
 
15
 * <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authored by
 
18
 *             Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 
19
 */
 
20
 
 
21
#include "zeitgeist-mimetypes.h"
 
22
#include "zeitgeist-ontology-interpretations.h"
 
23
#include "zeitgeist-ontology-manifestations.h"
 
24
 
 
25
static void _ensure_mimes_loaded   (void);
 
26
static void _ensure_schemes_loaded (void);
 
27
 
 
28
static gboolean    mimes_loaded   = FALSE;
 
29
static gboolean    schemes_loaded = FALSE;
 
30
static GHashTable *mimes          = NULL;
 
31
static GSList     *mimes_r        = NULL;
 
32
static GSList     *schemes        = NULL;
 
33
 
 
34
typedef struct
 
35
{
 
36
  gchar *scheme;
 
37
  gchar *manifestation_uri;
 
38
} UriScheme;
 
39
 
 
40
typedef struct
 
41
{
 
42
  GRegex *regex;
 
43
  gchar *interpretation_uri;
 
44
} MimeRegex;
 
45
 
 
46
static MimeRegex*
 
47
mime_regex_new (const gchar *mimetype_regex, const gchar *interpretation_uri)
 
48
{
 
49
  MimeRegex *m = g_slice_new (MimeRegex);
 
50
  m->regex = g_regex_new (mimetype_regex, 0, 0, NULL);
 
51
  m->interpretation_uri = g_strdup (interpretation_uri);
 
52
  return m;
 
53
}
 
54
 
 
55
/*static void
 
56
mime_regex_free (MimeRegex *m)
 
57
{
 
58
  g_regex_unref (m->regex);
 
59
  g_free (m->interpretation_uri);
 
60
  g_slice_free (MimeRegex, m);
 
61
}*/
 
62
 
 
63
static UriScheme*
 
64
uri_scheme_new (const gchar *uri_scheme, const gchar *manifestation_uri)
 
65
{
 
66
  UriScheme *s = g_slice_new (UriScheme);
 
67
  s->scheme = g_strdup (uri_scheme);
 
68
  s->manifestation_uri = g_strdup (manifestation_uri);
 
69
  return s;
 
70
}
 
71
 
 
72
/*static void
 
73
uri_scheme_free (UriScheme *s)
 
74
{
 
75
  g_free (s->scheme);
 
76
  g_free (s->manifestation_uri);
 
77
  g_slice_free (UriScheme, s);
 
78
}*/
 
79
 
 
80
 
 
81
 
 
82
/**
 
83
 * zeitgeist_register_mimetype:
 
84
 * @mimetype: A mimetype string. Fx. <emphasis>text/plain</emphasis>
 
85
 * @interpretation_uri: A URI defining the interpretation type to associate with @mimetype
 
86
 *
 
87
 * Associate a mimetype with a given interpretation type. Registered mimetypes
 
88
 * can be looked up with zeitgeist_interpretation_for_mimetype(). You can
 
89
 * register a regular expression as mimetype if you instead of this function
 
90
 * invoke zeitgeist_register_mimetype_regex().
 
91
 *
 
92
 * Mimetypes are first looked up by their exact name and then if none is
 
93
 * found the regular expressions will be checked as fallbacks.
 
94
 *
 
95
 * This library will install a wide range a common mimetypes for you, so unless
 
96
 * you have very specific needs you will normally not have to call this
 
97
 * function.
 
98
 *
 
99
 * See the list of common
 
100
 * <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
 
101
 */
 
102
void
 
103
zeitgeist_register_mimetype (const gchar *mimetype,
 
104
                             const gchar *interpretation_uri)
 
105
{
 
106
  if (mimes == NULL)
 
107
    mimes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
108
 
 
109
  g_hash_table_insert (mimes, g_strdup (mimetype),
 
110
                       g_strdup (interpretation_uri));
 
111
}
 
112
 
 
113
/**
 
114
 * zeitgeist_register_mimetype_regex:
 
115
 * @mimetype_regex: A regular expression matching a certain range of mimetypes.
 
116
 *                  Fx. <emphasis>text/.*</emphasis> to match all
 
117
 *                 <emphasis>text</emphasis> sub types.
 
118
 * @interpretation_uri: A URI defining the interpretation type to associate with
 
119
 *                     the matched mimetypes
 
120
 *
 
121
 * Associate a range of mimetypes with a given interpretation type.
 
122
 * Registered mimetypes can be looked up with
 
123
 * zeitgeist_interpretation_for_mimetype(). If you only need to register one
 
124
 * specific mimetype it is more efficient if you instead of this function
 
125
 * call zeitgeist_register_mimetype().
 
126
 *
 
127
 * Mimetypes are first looked up by their exact name and then if none is
 
128
 * found the regular expressions will be checked as fallbacks.
 
129
 *
 
130
 * This library will install a wide range a common mimetypes for you, so unless
 
131
 * you have very specific needs you will normally not have to call this
 
132
 * function.
 
133
 *
 
134
 * See the list of common
 
135
 * <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
 
136
 */ 
 
137
void
 
138
zeitgeist_register_mimetype_regex (const gchar *mimetype_regex,
 
139
                                    const gchar *interpretation_uri)
 
140
{
 
141
  mimes_r = g_slist_append (mimes_r,
 
142
                            mime_regex_new (mimetype_regex,
 
143
                                            interpretation_uri));
 
144
}
 
145
 
 
146
/**
 
147
 * zeitgeist_interpretation_for_mimetype:
 
148
 * @mimetype: A mimetype string. Fx. <emphasis>text/plain</emphasis>
 
149
 *
 
150
 * Look up the interpretation type associated with @mimetype. Please see the
 
151
 * list of common
 
152
 * <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
 
153
 *
 
154
 * Returns: A URI defining the interpretation type associated with @mimetype or
 
155
 *          %NULL in case @mimetype is unknown.
 
156
 */ 
 
157
const gchar*
 
158
zeitgeist_interpretation_for_mimetype (const gchar *mimetype)
 
159
{
 
160
  const gchar *result;
 
161
  GSList      *iter;
 
162
  
 
163
  _ensure_mimes_loaded();
 
164
 
 
165
  /* First look in our hash table */
 
166
  result = g_hash_table_lookup (mimes,
 
167
                                mimetype);
 
168
  if (result != NULL)
 
169
    return result;
 
170
 
 
171
  /* Check our regexes */
 
172
  for (iter = mimes_r; iter; iter = iter->next)
 
173
    {
 
174
      MimeRegex *m = (MimeRegex*) iter->data;
 
175
      if (g_regex_match (m->regex, mimetype, 0, NULL))
 
176
        return m->interpretation_uri;
 
177
    }
 
178
 
 
179
  return NULL;
 
180
}
 
181
 
 
182
/**
 
183
 * zeitgeist_register_uri_scheme:
 
184
 * @uri_scheme: A URI scheme such as <emphasis>http://</emphasis>
 
185
 * @manifestation_uri: A URI defining the manifestation type to associate with
 
186
 *                     @uri_scheme
 
187
 *
 
188
 * Associate a URI scheme with a given manifestation type.
 
189
 * You can find the manifestation type of a given URI by passing it to
 
190
 * zeitgeist_manifestation_for_uri().
 
191
 *
 
192
 * This library will install a range a common URI schemes for you, so unless
 
193
 * you have very specific needs you will normally not have to call this
 
194
 * function.
 
195
 *
 
196
 * See the list of common
 
197
 * <link linkend="zeitgeist-1.0-Manifestation-Ontology">manifestation types</link>.
 
198
 */
 
199
void
 
200
zeitgeist_register_uri_scheme (const gchar *uri_scheme,
 
201
                               const gchar *manifestation_uri)
 
202
{
 
203
  schemes = g_slist_append (schemes,
 
204
                            uri_scheme_new (uri_scheme, manifestation_uri));
 
205
}
 
206
 
 
207
/**
 
208
 * zeitgeist_manifestation_for_uri:
 
209
 * @uri: A URI
 
210
 * 
 
211
 * Look up a manifestation type for a given URI. Eg. if you pass in
 
212
 * <emphasis>file:///tmp/foo.txt</emphasis> you will get back
 
213
 * #ZEITGEIST_NFO_FILE_DATA_OBJECT.
 
214
 *
 
215
 * See the list of common
 
216
 * <link linkend="zeitgeist-1.0-Manifestation-Ontology">manifestation types</link>.
 
217
 *
 
218
 * Returns: A manifestation type for @uri or %NULL in case no suitable
 
219
 *          manifestation type is known
 
220
 */
 
221
const gchar*
 
222
zeitgeist_manifestation_for_uri (const gchar *uri)
 
223
{
 
224
  GSList      *iter;
 
225
  
 
226
  _ensure_schemes_loaded();
 
227
 
 
228
  for (iter = schemes; iter; iter = iter->next)
 
229
    {
 
230
      UriScheme *s = (UriScheme*) iter->data;
 
231
      if (g_str_has_prefix (uri, s->scheme))
 
232
        return s->manifestation_uri;
 
233
    }
 
234
 
 
235
  return NULL;
 
236
}