Merge lp:~zeitgeist/zeitgeist/mimetypes into lp:~zeitgeist/zeitgeist/bluebird
- mimetypes
- Merge into bluebird
Proposed by
Siegfried Gevatter
Status: | Merged |
---|---|
Approved by: | Michal Hruby |
Approved revision: | 349 |
Merged at revision: | 349 |
Proposed branch: | lp:~zeitgeist/zeitgeist/mimetypes |
Merge into: | lp:~zeitgeist/zeitgeist/bluebird |
Diff against target: |
546 lines (+467/-1) 6 files modified
.bzrignore (+1/-0) src/Makefile.am (+1/-0) src/mimetype.vala (+349/-0) test/direct/Makefile.am (+7/-0) test/direct/assertions.vapi (+1/-1) test/direct/mimetype-test.vala (+108/-0) |
To merge this branch: | bzr merge lp:~zeitgeist/zeitgeist/mimetypes |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Michal Hruby (community) | Approve | ||
Review via email: mp+87033@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 347. By Michal Hruby
-
Fix warning about delegate copying
- 348. By Siegfried Gevatter
-
Add interpretation_
for_mimetype and manifestation_ for_uri functions,
ported from libzeitgeist.This is needed to implement https:/
/bugs.launchpad .net/zeitgeist/ +bug/899602 - 349. By Siegfried Gevatter
-
Use compact classes instead of structs, return unowned strings and
sort mimetypes alphabetically.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '.bzrignore' | |||
2 | --- .bzrignore 2011-11-01 18:12:14 +0000 | |||
3 | +++ .bzrignore 2011-12-29 12:20:30 +0000 | |||
4 | @@ -54,3 +54,4 @@ | |||
5 | 54 | extra/python/_ontology.py | 54 | extra/python/_ontology.py |
6 | 55 | test/direct/*.c | 55 | test/direct/*.c |
7 | 56 | src/zeitgeist-daemon | 56 | src/zeitgeist-daemon |
8 | 57 | mimetype-test | ||
9 | 57 | 58 | ||
10 | === modified file 'src/Makefile.am' | |||
11 | --- src/Makefile.am 2011-11-01 18:30:37 +0000 | |||
12 | +++ src/Makefile.am 2011-12-29 12:20:30 +0000 | |||
13 | @@ -44,6 +44,7 @@ | |||
14 | 44 | where-clause.vala \ | 44 | where-clause.vala \ |
15 | 45 | ontology.vala \ | 45 | ontology.vala \ |
16 | 46 | ontology-uris.vala \ | 46 | ontology-uris.vala \ |
17 | 47 | mimetype.vala \ | ||
18 | 47 | $(NULL) | 48 | $(NULL) |
19 | 48 | 49 | ||
20 | 49 | zeitgeist_daemon_SOURCES = \ | 50 | zeitgeist_daemon_SOURCES = \ |
21 | 50 | 51 | ||
22 | === added file 'src/mimetype.vala' | |||
23 | --- src/mimetype.vala 1970-01-01 00:00:00 +0000 | |||
24 | +++ src/mimetype.vala 2011-12-29 12:20:30 +0000 | |||
25 | @@ -0,0 +1,349 @@ | |||
26 | 1 | /* datamodel.vala | ||
27 | 2 | * | ||
28 | 3 | * Copyright © 2011 Collabora Ltd. | ||
29 | 4 | * By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com> | ||
30 | 5 | * Copyright © 2010 Canonical, Ltd. | ||
31 | 6 | * By Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com> | ||
32 | 7 | * | ||
33 | 8 | * This program is free software: you can redistribute it and/or modify | ||
34 | 9 | * it under the terms of the GNU Lesser General Public License as published by | ||
35 | 10 | * the Free Software Foundation, either version 2.1 of the License, or | ||
36 | 11 | * (at your option) any later version. | ||
37 | 12 | * | ||
38 | 13 | * This program is distributed in the hope that it will be useful, | ||
39 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
40 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
41 | 16 | * GNU General Public License for more details. | ||
42 | 17 | * | ||
43 | 18 | * You should have received a copy of the GNU Lesser General Public License | ||
44 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
45 | 20 | * | ||
46 | 21 | */ | ||
47 | 22 | |||
48 | 23 | using Zeitgeist; | ||
49 | 24 | |||
50 | 25 | namespace Zeitgeist | ||
51 | 26 | { | ||
52 | 27 | |||
53 | 28 | private static bool mimetypes_loaded = false; | ||
54 | 29 | private static bool schemes_loaded = false; | ||
55 | 30 | |||
56 | 31 | private static HashTable<string, string>? mimetypes = null; | ||
57 | 32 | private static SList<MimeRegex?> mimetypes_regexs; | ||
58 | 33 | private static SList<UriScheme?> schemes; | ||
59 | 34 | |||
60 | 35 | [Compact] | ||
61 | 36 | private class MimeRegex | ||
62 | 37 | { | ||
63 | 38 | public Regex regex; | ||
64 | 39 | public string interpretation_uri; | ||
65 | 40 | |||
66 | 41 | public MimeRegex (string mimetype_regex, string interpretation_uri) | ||
67 | 42 | throws RegexError | ||
68 | 43 | { | ||
69 | 44 | this.regex = new Regex (mimetype_regex, 0, 0); | ||
70 | 45 | this.interpretation_uri = interpretation_uri; | ||
71 | 46 | } | ||
72 | 47 | } | ||
73 | 48 | |||
74 | 49 | [Compact] | ||
75 | 50 | private class UriScheme | ||
76 | 51 | { | ||
77 | 52 | public string uri_scheme; | ||
78 | 53 | public string manifestation_uri; | ||
79 | 54 | |||
80 | 55 | public UriScheme (string uri_scheme, string manifestation_uri) | ||
81 | 56 | { | ||
82 | 57 | this.uri_scheme = uri_scheme; | ||
83 | 58 | this.manifestation_uri = manifestation_uri; | ||
84 | 59 | } | ||
85 | 60 | } | ||
86 | 61 | |||
87 | 62 | /** | ||
88 | 63 | * zeitgeist_register_mimetype: | ||
89 | 64 | * @mimetype: A MIME-type string. Eg. <emphasis>text/plain</emphasis> | ||
90 | 65 | * @interpretation_uri: A URI defining the subject interpretation type to | ||
91 | 66 | * associate with @mimetype | ||
92 | 67 | * | ||
93 | 68 | * Associate a MIME-type with a given interpretation type. Registered | ||
94 | 69 | * MIME-types can be looked up with zeitgeist_interpretation_for_mimetype(). | ||
95 | 70 | * | ||
96 | 71 | * You can register a regular expression as mimetype if instead of this | ||
97 | 72 | * function you invoke zeitgeist_register_mimetype_regex(). | ||
98 | 73 | * | ||
99 | 74 | * MIME-types are first looked up by their exact name and then if none is | ||
100 | 75 | * found the regular expressions will be checked as fallbacks. | ||
101 | 76 | * | ||
102 | 77 | * This library will install a wide range a common mimetypes for you, so | ||
103 | 78 | * unless you have very specific needs you will normally not have to call | ||
104 | 79 | * this function. | ||
105 | 80 | * | ||
106 | 81 | * FIXME: link to list of interpretations | ||
107 | 82 | */ | ||
108 | 83 | public void register_mimetype (string mimetype, string interpretation_uri) | ||
109 | 84 | { | ||
110 | 85 | if (mimetypes == null) | ||
111 | 86 | mimetypes = new HashTable<string, string>(str_hash, str_equal); | ||
112 | 87 | |||
113 | 88 | mimetypes.insert (mimetype, interpretation_uri); | ||
114 | 89 | } | ||
115 | 90 | |||
116 | 91 | /** | ||
117 | 92 | * zeitgeist_register_mimetype_regex: | ||
118 | 93 | * @mimetype: A regular expression matching a certain range of mimetypes. | ||
119 | 94 | * Eg. <emphasis>text/.*</emphasis> to match all | ||
120 | 95 | * <emphasis>text</emphasis> subtypes. | ||
121 | 96 | * @interpretation_uri: A URI defining the subject interpretation type to | ||
122 | 97 | * associate with the matched MIME-types | ||
123 | 98 | * | ||
124 | 99 | * Associate a range of MIME-types with a given interpretation type. | ||
125 | 100 | * Registered MIME-types can be looked up with | ||
126 | 101 | * zeitgeist_interpretation_for_mimetype(). | ||
127 | 102 | * | ||
128 | 103 | * If you only need to register one specific MIME-type, it is more efficient | ||
129 | 104 | * to use zeitgeist_register_mimetype() instead of this function. | ||
130 | 105 | * | ||
131 | 106 | * MIME-types are first looked up by their exact name and then if none is | ||
132 | 107 | * found the regular expressions will be checked as fallbacks. | ||
133 | 108 | * | ||
134 | 109 | * This library will install a wide range a common mimetypes for you, so | ||
135 | 110 | * unless you have very specific needs you will normally not have to call | ||
136 | 111 | * this function. | ||
137 | 112 | * | ||
138 | 113 | * FIXME: link to list of interpretations | ||
139 | 114 | */ | ||
140 | 115 | public void register_mimetype_regex (string mimetype_regex, | ||
141 | 116 | string interpretation_uri) throws RegexError | ||
142 | 117 | { | ||
143 | 118 | var entry = new MimeRegex (mimetype_regex, interpretation_uri); | ||
144 | 119 | mimetypes_regexs.append ((owned) entry); | ||
145 | 120 | } | ||
146 | 121 | |||
147 | 122 | /** | ||
148 | 123 | * zeitgeist_interpretation_for_mimetype: | ||
149 | 124 | * @mimetype: A MIME-type string. Eg. <emphasis>text/plain</emphasis> | ||
150 | 125 | * | ||
151 | 126 | * Look up the subject interpretation type associated with @mimetype. | ||
152 | 127 | * FIXME: link to list of interpretations | ||
153 | 128 | * | ||
154 | 129 | * Returns: A URI defining the subject interpretation type associated with | ||
155 | 130 | * @mimetype or %NULL in case @mimetype is unknown. | ||
156 | 131 | */ | ||
157 | 132 | public unowned string? interpretation_for_mimetype (string mimetype) | ||
158 | 133 | { | ||
159 | 134 | ensure_mimetypes_loaded (); | ||
160 | 135 | |||
161 | 136 | unowned string? interpretation = mimetypes.lookup (mimetype); | ||
162 | 137 | if (interpretation != null) | ||
163 | 138 | return interpretation; | ||
164 | 139 | |||
165 | 140 | foreach (unowned MimeRegex mime_regex in mimetypes_regexs) | ||
166 | 141 | { | ||
167 | 142 | if (mime_regex.regex.match (mimetype, 0)) | ||
168 | 143 | return mime_regex.interpretation_uri; | ||
169 | 144 | } | ||
170 | 145 | |||
171 | 146 | return null; | ||
172 | 147 | } | ||
173 | 148 | |||
174 | 149 | /** | ||
175 | 150 | * zeitgeist_register_uri_scheme: | ||
176 | 151 | * @uri_scheme: A URI scheme such as <emphasis>http://</emphasis> | ||
177 | 152 | * @manifestation_uri: A URI defining the subject manifestation type | ||
178 | 153 | * to associate with @uri_scheme | ||
179 | 154 | * | ||
180 | 155 | * Associate a URI scheme with a given subject manifestation type. | ||
181 | 156 | * You can find the manifestation type of a given URI by passing it to | ||
182 | 157 | * zeitgeist_manifestation_for_uri(). | ||
183 | 158 | * | ||
184 | 159 | * This library will install a range a common URI schemes for you, so unless | ||
185 | 160 | * you have very specific needs you will normally not have to call this | ||
186 | 161 | * function. | ||
187 | 162 | * | ||
188 | 163 | * FIXME: link to list of manifestations | ||
189 | 164 | */ | ||
190 | 165 | public void register_uri_scheme (string uri_scheme, | ||
191 | 166 | string manifestation_type) | ||
192 | 167 | { | ||
193 | 168 | var scheme = new UriScheme (uri_scheme, manifestation_type); | ||
194 | 169 | schemes.append ((owned) scheme); | ||
195 | 170 | } | ||
196 | 171 | |||
197 | 172 | /** | ||
198 | 173 | * zeitgeist_manifestation_for_uri: | ||
199 | 174 | * @uri: An URI | ||
200 | 175 | * | ||
201 | 176 | * Look up a subject manifestation type for a given URI. Eg. if you pass in | ||
202 | 177 | * <emphasis>file:///tmp/foo.txt</emphasis> you will get back | ||
203 | 178 | * #ZEITGEIST_NFO_FILE_DATA_OBJECT. | ||
204 | 179 | * | ||
205 | 180 | * FIXME: link to list of manifestations | ||
206 | 181 | * | ||
207 | 182 | * Returns: A subject manifestation type for @uri or %NULL in case no | ||
208 | 183 | * suitable manifestation type is known | ||
209 | 184 | */ | ||
210 | 185 | public unowned string? manifestation_for_uri (string uri) { | ||
211 | 186 | ensure_schemes_loaded (); | ||
212 | 187 | |||
213 | 188 | foreach (unowned UriScheme scheme in schemes) | ||
214 | 189 | { | ||
215 | 190 | if (uri.has_prefix (scheme.uri_scheme)) | ||
216 | 191 | return scheme.manifestation_uri; | ||
217 | 192 | } | ||
218 | 193 | |||
219 | 194 | return null; | ||
220 | 195 | } | ||
221 | 196 | |||
222 | 197 | private static void ensure_mimetypes_loaded () | ||
223 | 198 | { | ||
224 | 199 | if (mimetypes_loaded) | ||
225 | 200 | return; | ||
226 | 201 | |||
227 | 202 | try { | ||
228 | 203 | register_mimetype ("application/ecmascript", NFO.SOURCE_CODE); | ||
229 | 204 | register_mimetype ("application/javascript", NFO.SOURCE_CODE); | ||
230 | 205 | register_mimetype ("application/ms-excel", NFO.SPREADSHEET); | ||
231 | 206 | register_mimetype ("application/ms-powerpoint", NFO.PRESENTATION); | ||
232 | 207 | register_mimetype ("application/msexcel", NFO.SPREADSHEET); | ||
233 | 208 | register_mimetype ("application/msword", NFO.PAGINATED_TEXT_DOCUMENT); | ||
234 | 209 | register_mimetype ("application/ogg", NFO.AUDIO); | ||
235 | 210 | register_mimetype ("application/pdf", NFO.PAGINATED_TEXT_DOCUMENT); | ||
236 | 211 | register_mimetype ("application/postscript", NFO.PAGINATED_TEXT_DOCUMENT); | ||
237 | 212 | register_mimetype ("application/ps", NFO.PAGINATED_TEXT_DOCUMENT); | ||
238 | 213 | register_mimetype ("application/rtf", NFO.PAGINATED_TEXT_DOCUMENT); | ||
239 | 214 | register_mimetype ("application/vnd.corel-draw", NFO.VECTOR_IMAGE); | ||
240 | 215 | register_mimetype ("application/vnd.ms-excel", NFO.SPREADSHEET); | ||
241 | 216 | register_mimetype ("application/vnd.ms-powerpoint", NFO.PRESENTATION); | ||
242 | 217 | register_mimetype ("application/x-7z-compressed", NFO.ARCHIVE); | ||
243 | 218 | register_mimetype ("application/x-abiword", NFO.PAGINATED_TEXT_DOCUMENT); | ||
244 | 219 | register_mimetype ("application/x-applix-presents", NFO.PRESENTATION); | ||
245 | 220 | register_mimetype ("application/x-applix-spreadsheet", NFO.SPREADSHEET); | ||
246 | 221 | register_mimetype ("application/x-applix-word", NFO.PAGINATED_TEXT_DOCUMENT); | ||
247 | 222 | register_mimetype ("application/x-archive", NFO.ARCHIVE); | ||
248 | 223 | register_mimetype ("application/x-bzip", NFO.ARCHIVE); | ||
249 | 224 | register_mimetype ("application/x-bzip-compressed-tar", NFO.ARCHIVE); | ||
250 | 225 | register_mimetype ("application/x-cd-image", NFO.FILESYSTEM_IMAGE); | ||
251 | 226 | register_mimetype ("application/x-compressed-tar", NFO.ARCHIVE); | ||
252 | 227 | register_mimetype ("application/x-csh", NFO.SOURCE_CODE); | ||
253 | 228 | register_mimetype ("application/x-deb", NFO.SOFTWARE); | ||
254 | 229 | register_mimetype ("application/x-designer", NFO.SOURCE_CODE); | ||
255 | 230 | register_mimetype ("application/x-desktop", NFO.SOFTWARE); | ||
256 | 231 | register_mimetype ("application/x-dia-diagram", NFO.SOURCE_CODE); | ||
257 | 232 | register_mimetype ("application/x-executable", NFO.SOFTWARE); | ||
258 | 233 | register_mimetype ("application/x-fluid", NFO.SOURCE_CODE); | ||
259 | 234 | register_mimetype ("application/x-glade", NFO.SOURCE_CODE); | ||
260 | 235 | register_mimetype ("application/x-gnucash", NFO.SPREADSHEET); | ||
261 | 236 | register_mimetype ("application/x-gnumeric", NFO.SPREADSHEET); | ||
262 | 237 | register_mimetype ("application/x-gzip", NFO.ARCHIVE); | ||
263 | 238 | register_mimetype ("application/x-java-archive", NFO.SOURCE_CODE); | ||
264 | 239 | register_mimetype ("application/x-killustrator", NFO.VECTOR_IMAGE); | ||
265 | 240 | register_mimetype ("application/x-kpresenter", NFO.PRESENTATION); | ||
266 | 241 | register_mimetype ("application/x-kspread", NFO.SPREADSHEET); | ||
267 | 242 | register_mimetype ("application/x-kword", NFO.PAGINATED_TEXT_DOCUMENT); | ||
268 | 243 | register_mimetype ("application/x-lzma", NFO.ARCHIVE); | ||
269 | 244 | register_mimetype ("application/x-lzma-compressed-tar", NFO.ARCHIVE); | ||
270 | 245 | register_mimetype ("application/x-m4", NFO.SOURCE_CODE); | ||
271 | 246 | register_mimetype ("application/x-ms-dos-executable", NFO.SOFTWARE); | ||
272 | 247 | register_mimetype ("application/x-object", NFO.SOURCE_CODE); | ||
273 | 248 | register_mimetype ("application/x-perl", NFO.SOURCE_CODE); | ||
274 | 249 | register_mimetype ("application/x-php", NFO.SOURCE_CODE); | ||
275 | 250 | register_mimetype ("application/x-rpm", NFO.SOFTWARE); | ||
276 | 251 | register_mimetype ("application/x-ruby", NFO.SOURCE_CODE); | ||
277 | 252 | register_mimetype ("application/x-shellscript", NFO.SOURCE_CODE); | ||
278 | 253 | register_mimetype ("application/x-sql", NFO.SOURCE_CODE); | ||
279 | 254 | register_mimetype ("application/xhtml+xml", NFO.SOURCE_CODE); | ||
280 | 255 | register_mimetype ("application/xml", NFO.SOURCE_CODE); | ||
281 | 256 | register_mimetype ("application/zip", NFO.ARCHIVE); | ||
282 | 257 | register_mimetype ("audio/x-scpls", NFO.MEDIA_LIST); | ||
283 | 258 | register_mimetype ("image/gif", NFO.RASTER_IMAGE); | ||
284 | 259 | register_mimetype ("image/jpeg", NFO.RASTER_IMAGE); | ||
285 | 260 | register_mimetype ("image/png", NFO.RASTER_IMAGE); | ||
286 | 261 | register_mimetype ("image/svg+xml", NFO.VECTOR_IMAGE); | ||
287 | 262 | register_mimetype ("image/tiff", NFO.RASTER_IMAGE); | ||
288 | 263 | register_mimetype ("image/x-xcf", NFO.RASTER_IMAGE); | ||
289 | 264 | register_mimetype ("text/css", NFO.SOURCE_CODE); | ||
290 | 265 | register_mimetype ("text/html", NFO.HTML_DOCUMENT); | ||
291 | 266 | register_mimetype ("text/plain", NFO.TEXT_DOCUMENT); | ||
292 | 267 | register_mimetype ("text/x-c", NFO.SOURCE_CODE); | ||
293 | 268 | register_mimetype ("text/x-c++", NFO.SOURCE_CODE); | ||
294 | 269 | register_mimetype ("text/x-c++src", NFO.SOURCE_CODE); | ||
295 | 270 | register_mimetype ("text/x-chdr", NFO.SOURCE_CODE); | ||
296 | 271 | register_mimetype ("text/x-copying", NFO.SOURCE_CODE); | ||
297 | 272 | register_mimetype ("text/x-credits", NFO.SOURCE_CODE); | ||
298 | 273 | register_mimetype ("text/x-csharp", NFO.SOURCE_CODE); | ||
299 | 274 | register_mimetype ("text/x-csrc", NFO.SOURCE_CODE); | ||
300 | 275 | register_mimetype ("text/x-dsrc", NFO.SOURCE_CODE); | ||
301 | 276 | register_mimetype ("text/x-eiffel", NFO.SOURCE_CODE); | ||
302 | 277 | register_mimetype ("text/x-gettext-translation", NFO.SOURCE_CODE); | ||
303 | 278 | register_mimetype ("text/x-gettext-translation-template", NFO.SOURCE_CODE); | ||
304 | 279 | register_mimetype ("text/x-haskell", NFO.SOURCE_CODE); | ||
305 | 280 | register_mimetype ("text/x-idl", NFO.SOURCE_CODE); | ||
306 | 281 | register_mimetype ("text/x-java", NFO.SOURCE_CODE); | ||
307 | 282 | register_mimetype ("text/x-latex", NFO.SOURCE_CODE); | ||
308 | 283 | register_mimetype ("text/x-lisp", NFO.SOURCE_CODE); | ||
309 | 284 | register_mimetype ("text/x-lua", NFO.SOURCE_CODE); | ||
310 | 285 | register_mimetype ("text/x-m4", NFO.SOURCE_CODE); | ||
311 | 286 | register_mimetype ("text/x-makefile", NFO.SOURCE_CODE); | ||
312 | 287 | register_mimetype ("text/x-objcsrc", NFO.SOURCE_CODE); | ||
313 | 288 | register_mimetype ("text/x-ocaml", NFO.SOURCE_CODE); | ||
314 | 289 | register_mimetype ("text/x-pascal", NFO.SOURCE_CODE); | ||
315 | 290 | register_mimetype ("text/x-patch", NFO.SOURCE_CODE); | ||
316 | 291 | register_mimetype ("text/x-python", NFO.SOURCE_CODE); | ||
317 | 292 | register_mimetype ("text/x-sql", NFO.SOURCE_CODE); | ||
318 | 293 | register_mimetype ("text/x-tcl", NFO.SOURCE_CODE); | ||
319 | 294 | register_mimetype ("text/x-tex", NFO.SOURCE_CODE); | ||
320 | 295 | register_mimetype ("text/x-troff", NFO.SOURCE_CODE); | ||
321 | 296 | register_mimetype ("text/x-vala", NFO.SOURCE_CODE); | ||
322 | 297 | register_mimetype ("text/x-vhdl", NFO.SOURCE_CODE); | ||
323 | 298 | |||
324 | 299 | register_mimetype_regex (".*/x-dvi", NFO.PAGINATED_TEXT_DOCUMENT); | ||
325 | 300 | register_mimetype_regex ( | ||
326 | 301 | "application/vnd.oasis.opendocument.text.*", | ||
327 | 302 | NFO.PAGINATED_TEXT_DOCUMENT); | ||
328 | 303 | register_mimetype_regex ( | ||
329 | 304 | "application/vnd.oasis.opendocument.presentation.*", | ||
330 | 305 | NFO.PRESENTATION); | ||
331 | 306 | register_mimetype_regex ( | ||
332 | 307 | "application/vnd.oasis.opendocument.spreadsheet.*", | ||
333 | 308 | NFO.SPREADSHEET); | ||
334 | 309 | register_mimetype_regex ( | ||
335 | 310 | "application/vnd.oasis.opendocument.graphics.*", | ||
336 | 311 | NFO.VECTOR_IMAGE); | ||
337 | 312 | register_mimetype_regex ("application/vnd\\..*", NFO.DOCUMENT); | ||
338 | 313 | register_mimetype_regex ("application/x-applix-.*", NFO.DOCUMENT); | ||
339 | 314 | register_mimetype_regex ("application/vnd.ms-excel.*", | ||
340 | 315 | NFO.SPREADSHEET); | ||
341 | 316 | register_mimetype_regex ("application/vnd.ms-powerpoint.*", | ||
342 | 317 | NFO.PRESENTATION); | ||
343 | 318 | register_mimetype_regex ("audio/.*", NFO.AUDIO); | ||
344 | 319 | register_mimetype_regex ("image/.*", NFO.IMAGE); | ||
345 | 320 | register_mimetype_regex ("video/.*", NFO.VIDEO); | ||
346 | 321 | } catch (RegexError e) { | ||
347 | 322 | // This won't happen. | ||
348 | 323 | assert (false); | ||
349 | 324 | } | ||
350 | 325 | |||
351 | 326 | mimetypes_loaded = true; | ||
352 | 327 | } | ||
353 | 328 | |||
354 | 329 | private static void ensure_schemes_loaded () | ||
355 | 330 | { | ||
356 | 331 | if (schemes_loaded) | ||
357 | 332 | return; | ||
358 | 333 | |||
359 | 334 | register_uri_scheme ("file://", NFO.FILE_DATA_OBJECT); | ||
360 | 335 | register_uri_scheme ("http://", NFO.REMOTE_DATA_OBJECT); | ||
361 | 336 | register_uri_scheme ("https://", NFO.REMOTE_DATA_OBJECT); | ||
362 | 337 | register_uri_scheme ("ssh://", NFO.REMOTE_DATA_OBJECT); | ||
363 | 338 | register_uri_scheme ("sftp://", NFO.REMOTE_DATA_OBJECT); | ||
364 | 339 | register_uri_scheme ("ftp://", NFO.REMOTE_DATA_OBJECT); | ||
365 | 340 | register_uri_scheme ("dav://", NFO.REMOTE_DATA_OBJECT); | ||
366 | 341 | register_uri_scheme ("davs://", NFO.REMOTE_DATA_OBJECT); | ||
367 | 342 | register_uri_scheme ("smb://", NFO.REMOTE_DATA_OBJECT); | ||
368 | 343 | |||
369 | 344 | schemes_loaded = true; | ||
370 | 345 | } | ||
371 | 346 | |||
372 | 347 | } | ||
373 | 348 | |||
374 | 349 | // vim:expandtab:ts=4:sw=4 | ||
375 | 0 | 350 | ||
376 | === modified file 'test/direct/Makefile.am' | |||
377 | --- test/direct/Makefile.am 2011-10-12 21:13:12 +0000 | |||
378 | +++ test/direct/Makefile.am 2011-12-29 12:20:30 +0000 | |||
379 | @@ -13,6 +13,7 @@ | |||
380 | 13 | query-operators-test \ | 13 | query-operators-test \ |
381 | 14 | where-clause-test \ | 14 | where-clause-test \ |
382 | 15 | table-lookup-test \ | 15 | table-lookup-test \ |
383 | 16 | mimetype-test \ | ||
384 | 16 | $(NULL) | 17 | $(NULL) |
385 | 17 | 18 | ||
386 | 18 | SRC_FILES = \ | 19 | SRC_FILES = \ |
387 | @@ -31,6 +32,7 @@ | |||
388 | 31 | $(top_srcdir)/src/sql.vala \ | 32 | $(top_srcdir)/src/sql.vala \ |
389 | 32 | $(top_srcdir)/src/ontology.vala \ | 33 | $(top_srcdir)/src/ontology.vala \ |
390 | 33 | $(top_srcdir)/src/ontology-uris.vala \ | 34 | $(top_srcdir)/src/ontology-uris.vala \ |
391 | 35 | $(top_srcdir)/src/mimetype.vala \ | ||
392 | 34 | $(NULL) | 36 | $(NULL) |
393 | 35 | 37 | ||
394 | 36 | marshalling: marshalling.vala $(SRC_FILES) | 38 | marshalling: marshalling.vala $(SRC_FILES) |
395 | @@ -45,6 +47,9 @@ | |||
396 | 45 | table-lookup-test: table-lookup-test.vala $(SRC_FILES) | 47 | table-lookup-test: table-lookup-test.vala $(SRC_FILES) |
397 | 46 | $(VALAC) $(VALAFLAGS) -o $@ $^ | 48 | $(VALAC) $(VALAFLAGS) -o $@ $^ |
398 | 47 | 49 | ||
399 | 50 | mimetype-test: mimetype-test.vala $(SRC_FILES) | ||
400 | 51 | $(VALAC) $(VALAFLAGS) -o $@ $^ | ||
401 | 52 | |||
402 | 48 | clean-local: | 53 | clean-local: |
403 | 49 | rm -f *.~[0-9]~ | 54 | rm -f *.~[0-9]~ |
404 | 50 | 55 | ||
405 | @@ -53,6 +58,7 @@ | |||
406 | 53 | query-operators-test \ | 58 | query-operators-test \ |
407 | 54 | where-clause-test \ | 59 | where-clause-test \ |
408 | 55 | table-lookup-test \ | 60 | table-lookup-test \ |
409 | 61 | mimetype-test \ | ||
410 | 56 | $(NULL) | 62 | $(NULL) |
411 | 57 | 63 | ||
412 | 58 | EXTRA_DIST = \ | 64 | EXTRA_DIST = \ |
413 | @@ -60,6 +66,7 @@ | |||
414 | 60 | query-operators-test.vala \ | 66 | query-operators-test.vala \ |
415 | 61 | where-clause-test.vala \ | 67 | where-clause-test.vala \ |
416 | 62 | table-lookup-test.vala \ | 68 | table-lookup-test.vala \ |
417 | 69 | mimetype-test.vala \ | ||
418 | 63 | assertions.vapi \ | 70 | assertions.vapi \ |
419 | 64 | $(NULL) | 71 | $(NULL) |
420 | 65 | 72 | ||
421 | 66 | 73 | ||
422 | === modified file 'test/direct/assertions.vapi' | |||
423 | --- test/direct/assertions.vapi 2011-08-12 15:00:59 +0000 | |||
424 | +++ test/direct/assertions.vapi 2011-12-29 12:20:30 +0000 | |||
425 | @@ -14,7 +14,7 @@ | |||
426 | 14 | [CCode (cname = ">=")] | 14 | [CCode (cname = ">=")] |
427 | 15 | GREATER_OR_EQUAL | 15 | GREATER_OR_EQUAL |
428 | 16 | } | 16 | } |
430 | 17 | 17 | ||
431 | 18 | public void assert_cmpstr (string? s1, OperatorType op, string? s2); | 18 | public void assert_cmpstr (string? s1, OperatorType op, string? s2); |
432 | 19 | public void assert_cmpint (int n1, OperatorType op, int n2); | 19 | public void assert_cmpint (int n1, OperatorType op, int n2); |
433 | 20 | public void assert_cmpuint (uint n1, OperatorType op, uint n2); | 20 | public void assert_cmpuint (uint n1, OperatorType op, uint n2); |
434 | 21 | 21 | ||
435 | === added file 'test/direct/mimetype-test.vala' | |||
436 | --- test/direct/mimetype-test.vala 1970-01-01 00:00:00 +0000 | |||
437 | +++ test/direct/mimetype-test.vala 2011-12-29 12:20:30 +0000 | |||
438 | @@ -0,0 +1,108 @@ | |||
439 | 1 | /* where-clause-test.vala | ||
440 | 2 | * | ||
441 | 3 | * Copyright © 2011 Collabora Ltd. | ||
442 | 4 | * By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com> | ||
443 | 5 | * Copyright © 2010 Canonical, Ltd. | ||
444 | 6 | * By Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com> | ||
445 | 7 | * | ||
446 | 8 | * This program is free software: you can redistribute it and/or modify | ||
447 | 9 | * it under the terms of the GNU Lesser General Public License as published by | ||
448 | 10 | * the Free Software Foundation, either version 2.1 of the License, or | ||
449 | 11 | * (at your option) any later version. | ||
450 | 12 | * | ||
451 | 13 | * This program is distributed in the hope that it will be useful, | ||
452 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
453 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
454 | 16 | * GNU General Public License for more details. | ||
455 | 17 | * | ||
456 | 18 | * You should have received a copy of the GNU Lesser General Public License | ||
457 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
458 | 20 | * | ||
459 | 21 | */ | ||
460 | 22 | |||
461 | 23 | using Zeitgeist; | ||
462 | 24 | using Assertions; | ||
463 | 25 | |||
464 | 26 | void main (string[] args) | ||
465 | 27 | { | ||
466 | 28 | Test.init (ref args); | ||
467 | 29 | |||
468 | 30 | Test.add_func ("/MimeType/basic", mime_type_basic_test); | ||
469 | 31 | Test.add_func ("/MimeType/regex", mime_type_regex_test); | ||
470 | 32 | Test.add_func ("/MimeType/none", mime_type_none_test); | ||
471 | 33 | Test.add_func ("/MimeType/register", mime_type_registration_test); | ||
472 | 34 | |||
473 | 35 | Test.add_func ("/UriScheme/basic", uri_scheme_basic_test); | ||
474 | 36 | Test.add_func ("/UriScheme/none", uri_scheme_none_test); | ||
475 | 37 | Test.add_func ("/UriScheme/register", uri_scheme_registration_test); | ||
476 | 38 | |||
477 | 39 | Test.run (); | ||
478 | 40 | } | ||
479 | 41 | |||
480 | 42 | public void mime_type_basic_test () | ||
481 | 43 | { | ||
482 | 44 | assert_cmpstr (NFO.TEXT_DOCUMENT, OperatorType.EQUAL, | ||
483 | 45 | interpretation_for_mimetype ("text/plain")); | ||
484 | 46 | } | ||
485 | 47 | |||
486 | 48 | public void mime_type_regex_test () | ||
487 | 49 | { | ||
488 | 50 | // We should have a fallack for application/x-applix-* | ||
489 | 51 | assert_cmpstr (NFO.DOCUMENT, OperatorType.EQUAL, | ||
490 | 52 | interpretation_for_mimetype ("application/x-applix-FOOBAR")); | ||
491 | 53 | |||
492 | 54 | // Still application/x-applix-speadsheet should be a spreadsheet | ||
493 | 55 | assert_cmpstr (NFO.SPREADSHEET, OperatorType.EQUAL, | ||
494 | 56 | interpretation_for_mimetype ("application/x-applix-spreadsheet")); | ||
495 | 57 | } | ||
496 | 58 | |||
497 | 59 | public void mime_type_none_test () | ||
498 | 60 | { | ||
499 | 61 | assert (interpretation_for_mimetype ("foo/bar") == null); | ||
500 | 62 | } | ||
501 | 63 | |||
502 | 64 | public void mime_type_registration_test () | ||
503 | 65 | { | ||
504 | 66 | register_mimetype ("awesome/bird", "Bluebird"); | ||
505 | 67 | try | ||
506 | 68 | { | ||
507 | 69 | register_mimetype_regex ("everything/.*", "is nothing"); | ||
508 | 70 | } catch (RegexError e) { | ||
509 | 71 | assert (false); | ||
510 | 72 | } | ||
511 | 73 | |||
512 | 74 | mime_type_basic_test (); | ||
513 | 75 | mime_type_regex_test (); | ||
514 | 76 | mime_type_none_test (); | ||
515 | 77 | |||
516 | 78 | assert_cmpstr ("Bluebird", OperatorType.EQUAL, | ||
517 | 79 | interpretation_for_mimetype ("awesome/bird")); | ||
518 | 80 | assert_cmpstr ("is nothing", OperatorType.EQUAL, | ||
519 | 81 | interpretation_for_mimetype ("everything/everywhere")); | ||
520 | 82 | } | ||
521 | 83 | |||
522 | 84 | public void uri_scheme_basic_test () | ||
523 | 85 | { | ||
524 | 86 | assert_cmpstr (NFO.FILE_DATA_OBJECT, OperatorType.EQUAL, | ||
525 | 87 | manifestation_for_uri ("file:///tmp/foo.txt")); | ||
526 | 88 | assert_cmpstr (NFO.REMOTE_DATA_OBJECT, OperatorType.EQUAL, | ||
527 | 89 | manifestation_for_uri ("ftp://ftp.example.com")); | ||
528 | 90 | } | ||
529 | 91 | |||
530 | 92 | public void uri_scheme_none_test () | ||
531 | 93 | { | ||
532 | 94 | assert (manifestation_for_uri ("asdf://awesomehttp://") == null); | ||
533 | 95 | } | ||
534 | 96 | |||
535 | 97 | public void uri_scheme_registration_test () | ||
536 | 98 | { | ||
537 | 99 | register_uri_scheme ("42://", "the answer"); | ||
538 | 100 | |||
539 | 101 | uri_scheme_basic_test (); | ||
540 | 102 | uri_scheme_none_test (); | ||
541 | 103 | |||
542 | 104 | assert_cmpstr ("the answer", OperatorType.EQUAL, | ||
543 | 105 | manifestation_for_uri ("42://what is it?")); | ||
544 | 106 | } | ||
545 | 107 | |||
546 | 108 | // vim:expandtab:ts=4:sw=4 |
A couple of optimizations, pls:
- turn the private structs into compact classes (this will require explicit ownership transfer when adding them to the lists)
- the *_for_* methods should return `unowned string?`
- it'd be nice to sort the register_mimetype calls alphabetically