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