2
* Copyright (C) 2010 Canonical, Ltd.
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.
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.
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/>.
18
* Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
21
#include "zeitgeist-mimetypes.h"
22
#include "zeitgeist-ontology-interpretations.h"
23
#include "zeitgeist-ontology-manifestations.h"
25
static void _ensure_mimes_loaded (void);
26
static void _ensure_schemes_loaded (void);
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;
37
gchar *manifestation_uri;
43
gchar *interpretation_uri;
47
mime_regex_new (const gchar *mimetype_regex, const gchar *interpretation_uri)
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);
56
mime_regex_free (MimeRegex *m)
58
g_regex_unref (m->regex);
59
g_free (m->interpretation_uri);
60
g_slice_free (MimeRegex, m);
64
uri_scheme_new (const gchar *uri_scheme, const gchar *manifestation_uri)
66
UriScheme *s = g_slice_new (UriScheme);
67
s->scheme = g_strdup (uri_scheme);
68
s->manifestation_uri = g_strdup (manifestation_uri);
73
uri_scheme_free (UriScheme *s)
76
g_free (s->manifestation_uri);
77
g_slice_free (UriScheme, s);
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
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().
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.
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
99
* See the list of common
100
* <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
103
zeitgeist_register_mimetype (const gchar *mimetype,
104
const gchar *interpretation_uri)
107
mimes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
109
g_hash_table_insert (mimes, g_strdup (mimetype),
110
g_strdup (interpretation_uri));
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
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().
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.
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
134
* See the list of common
135
* <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
138
zeitgeist_register_mimetype_regex (const gchar *mimetype_regex,
139
const gchar *interpretation_uri)
141
mimes_r = g_slist_append (mimes_r,
142
mime_regex_new (mimetype_regex,
143
interpretation_uri));
147
* zeitgeist_interpretation_for_mimetype:
148
* @mimetype: A mimetype string. Fx. <emphasis>text/plain</emphasis>
150
* Look up the interpretation type associated with @mimetype. Please see the
152
* <link linkend="zeitgeist-1.0-Interpretation-Ontology">interpretation types</link>.
154
* Returns: A URI defining the interpretation type associated with @mimetype or
155
* %NULL in case @mimetype is unknown.
158
zeitgeist_interpretation_for_mimetype (const gchar *mimetype)
163
_ensure_mimes_loaded();
165
/* First look in our hash table */
166
result = g_hash_table_lookup (mimes,
171
/* Check our regexes */
172
for (iter = mimes_r; iter; iter = iter->next)
174
MimeRegex *m = (MimeRegex*) iter->data;
175
if (g_regex_match (m->regex, mimetype, 0, NULL))
176
return m->interpretation_uri;
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
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().
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
196
* See the list of common
197
* <link linkend="zeitgeist-1.0-Manifestation-Ontology">manifestation types</link>.
200
zeitgeist_register_uri_scheme (const gchar *uri_scheme,
201
const gchar *manifestation_uri)
203
schemes = g_slist_append (schemes,
204
uri_scheme_new (uri_scheme, manifestation_uri));
208
* zeitgeist_manifestation_for_uri:
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.
215
* See the list of common
216
* <link linkend="zeitgeist-1.0-Manifestation-Ontology">manifestation types</link>.
218
* Returns: A manifestation type for @uri or %NULL in case no suitable
219
* manifestation type is known
222
zeitgeist_manifestation_for_uri (const gchar *uri)
226
_ensure_schemes_loaded();
228
for (iter = schemes; iter; iter = iter->next)
230
UriScheme *s = (UriScheme*) iter->data;
231
if (g_str_has_prefix (uri, s->scheme))
232
return s->manifestation_uri;