summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Faure <faure@kde.org>2011-09-29 17:26:33 +0200
committerDavid Faure <faure@kde.org>2011-09-29 17:26:33 +0200
commit5181175d5fdaa3832b0fd094cda0120b1fe92af6 (patch)
tree791d4e3796d3ff7ea54c6c4b63edb83edef3bddf
parent755fb0b6698af8a8a3b9c633572561ccce0f5402 (diff)
Implement text vs binary fallback; support for application/x-zerosize
-rw-r--r--src/xdgmime.c18
-rw-r--r--src/xdgmime.h6
-rw-r--r--src/xdgmimecache.c9
-rw-r--r--src/xdgmimeint.c15
-rw-r--r--src/xdgmimeint.h1
5 files changed, 42 insertions, 7 deletions
diff --git a/src/xdgmime.c b/src/xdgmime.c
index c3b21cb..c7b16bb 100644
--- a/src/xdgmime.c
+++ b/src/xdgmime.c
@@ -64,6 +64,8 @@ XdgMimeCache **_caches = NULL;
64static int n_caches = 0; 64static int n_caches = 0;
65 65
66const char xdg_mime_type_unknown[] = "application/octet-stream"; 66const char xdg_mime_type_unknown[] = "application/octet-stream";
67const char xdg_mime_type_empty[] = "application/x-zerosize";
68const char xdg_mime_type_textplain[] = "text/plain";
67 69
68 70
69enum 71enum
@@ -467,17 +469,23 @@ xdg_mime_get_mime_type_for_data (const void *data,
467{ 469{
468 const char *mime_type; 470 const char *mime_type;
469 471
472 if (len == 0)
473 {
474 *result_prio = 100;
475 return XDG_MIME_TYPE_EMPTY;
476 }
477
470 xdg_mime_init (); 478 xdg_mime_init ();
471 479
472 if (_caches) 480 if (_caches)
473 return _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio); 481 mime_type = _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
474 482 else
475 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0); 483 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
476 484
477 if (mime_type) 485 if (mime_type)
478 return mime_type; 486 return mime_type;
479 487
480 return XDG_MIME_TYPE_UNKNOWN; 488 return _xdg_binary_or_text_fallback(data, len);
481} 489}
482 490
483const char * 491const char *
@@ -556,7 +564,7 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
556 if (mime_type) 564 if (mime_type)
557 return mime_type; 565 return mime_type;
558 566
559 return XDG_MIME_TYPE_UNKNOWN; 567 return _xdg_binary_or_text_fallback(data, bytes_read);
560} 568}
561 569
562const char * 570const char *
diff --git a/src/xdgmime.h b/src/xdgmime.h
index d3031a3..6a34edf 100644
--- a/src/xdgmime.h
+++ b/src/xdgmime.h
@@ -68,6 +68,8 @@ typedef void (*XdgMimeDestroy) (void *user_data);
68#define xdg_mime_register_reload_callback XDG_ENTRY(register_reload_callback) 68#define xdg_mime_register_reload_callback XDG_ENTRY(register_reload_callback)
69#define xdg_mime_remove_callback XDG_ENTRY(remove_callback) 69#define xdg_mime_remove_callback XDG_ENTRY(remove_callback)
70#define xdg_mime_type_unknown XDG_ENTRY(type_unknown) 70#define xdg_mime_type_unknown XDG_ENTRY(type_unknown)
71#define xdg_mime_type_empty XDG_ENTRY(type_empty)
72#define xdg_mime_type_textplain XDG_ENTRY(type_textplain)
71#define xdg_mime_get_icon XDG_ENTRY(get_icon) 73#define xdg_mime_get_icon XDG_ENTRY(get_icon)
72#define xdg_mime_get_generic_icon XDG_ENTRY(get_generic_icon) 74#define xdg_mime_get_generic_icon XDG_ENTRY(get_generic_icon)
73 75
@@ -77,7 +79,11 @@ typedef void (*XdgMimeDestroy) (void *user_data);
77#endif 79#endif
78 80
79extern const char xdg_mime_type_unknown[]; 81extern const char xdg_mime_type_unknown[];
82extern const char xdg_mime_type_empty[];
83extern const char xdg_mime_type_textplain[];
80#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown 84#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown
85#define XDG_MIME_TYPE_EMPTY xdg_mime_type_empty
86#define XDG_MIME_TYPE_TEXTPLAIN xdg_mime_type_textplain
81 87
82const char *xdg_mime_get_mime_type_for_data (const void *data, 88const char *xdg_mime_get_mime_type_for_data (const void *data,
83 size_t len, 89 size_t len,
diff --git a/src/xdgmimecache.c b/src/xdgmimecache.c
index 1e99b3e..41b13e9 100644
--- a/src/xdgmimecache.c
+++ b/src/xdgmimecache.c
@@ -693,12 +693,11 @@ cache_get_mime_type_for_data (const void *data,
693 693
694 for (n = 0; n < n_mime_types; n++) 694 for (n = 0; n < n_mime_types; n++)
695 { 695 {
696
697 if (mime_types[n]) 696 if (mime_types[n])
698 return mime_types[n]; 697 return mime_types[n];
699 } 698 }
700 699
701 return XDG_MIME_TYPE_UNKNOWN; 700 return NULL;
702} 701}
703 702
704const char * 703const char *
@@ -743,6 +742,9 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
743 statbuf = &buf; 742 statbuf = &buf;
744 } 743 }
745 744
745 if (statbuf->st_size == 0)
746 return XDG_MIME_TYPE_EMPTY;
747
746 if (!S_ISREG (statbuf->st_mode)) 748 if (!S_ISREG (statbuf->st_mode))
747 return XDG_MIME_TYPE_UNKNOWN; 749 return XDG_MIME_TYPE_UNKNOWN;
748 750
@@ -772,6 +774,9 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
772 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL, 774 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
773 mime_types, n); 775 mime_types, n);
774 776
777 if (!mime_type)
778 mime_type = _xdg_binary_or_text_fallback(data, bytes_read);
779
775 free (data); 780 free (data);
776 fclose (file); 781 fclose (file);
777 782
diff --git a/src/xdgmimeint.c b/src/xdgmimeint.c
index d372d2c..cf789d9 100644
--- a/src/xdgmimeint.c
+++ b/src/xdgmimeint.c
@@ -189,3 +189,18 @@ _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
189 } 189 }
190} 190}
191 191
192const char *
193_xdg_binary_or_text_fallback(const void *data, size_t len)
194{
195 unsigned char *chardata;
196 int i;
197
198 chardata = (unsigned char *) data;
199 for (i = 0; i < 32 && i < len; ++i)
200 {
201 if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
202 return XDG_MIME_TYPE_UNKNOWN; /* binary data */
203 }
204
205 return XDG_MIME_TYPE_TEXTPLAIN;
206}
diff --git a/src/xdgmimeint.h b/src/xdgmimeint.h
index 232c808..9e8b2cb 100644
--- a/src/xdgmimeint.h
+++ b/src/xdgmimeint.h
@@ -73,5 +73,6 @@ int _xdg_utf8_validate (const char *source);
73xdg_unichar_t *_xdg_convert_to_ucs4 (const char *source, int *len); 73xdg_unichar_t *_xdg_convert_to_ucs4 (const char *source, int *len);
74void _xdg_reverse_ucs4 (xdg_unichar_t *source, int len); 74void _xdg_reverse_ucs4 (xdg_unichar_t *source, int len);
75const char *_xdg_get_base_name (const char *file_name); 75const char *_xdg_get_base_name (const char *file_name);
76const char *_xdg_binary_or_text_fallback(const void *data, size_t len);
76 77
77#endif /* __XDG_MIME_INT_H__ */ 78#endif /* __XDG_MIME_INT_H__ */