diff --git a/sugar/Makefile.am b/sugar/Makefile.am index 2c69e2a9..d4f91ea9 100644 --- a/sugar/Makefile.am +++ b/sugar/Makefile.am @@ -14,7 +14,8 @@ INCLUDES = \ $(LIB_CFLAGS) \ $(LIB_BINDINGS_CFLAGS) \ $(PYTHON_INCLUDES) \ - -I$(top_srcdir)/lib + -I$(top_srcdir)/lib \ + -I$(top_srcdir)/lib/xdgmime pkgpyexecdir = $(pythondir)/sugar diff --git a/sugar/_sugarext.c b/sugar/_sugarext.c index c5bf22d3..be9854c3 100644 --- a/sugar/_sugarext.c +++ b/sugar/_sugarext.c @@ -9,11 +9,12 @@ #include "pygobject.h" #include "sugar-address-entry.h" +#include "xdgmime.h" #include #include -#line 17 "_sugarext.c" +#line 18 "_sugarext.c" /* ---------- types from other modules ---------- */ @@ -24,7 +25,7 @@ static PyTypeObject *_PyGtkEntry_Type; /* ---------- forward type declarations ---------- */ PyTypeObject G_GNUC_INTERNAL PySugarAddressEntry_Type; -#line 28 "_sugarext.c" +#line 29 "_sugarext.c" @@ -97,9 +98,32 @@ _wrap_sugar_mime_get_mime_type_from_file_name(PyObject *self, PyObject *args, Py return Py_None; } +#line 23 "_sugarext.override" +static PyObject * +_wrap_sugar_mime_get_mime_type_for_file(PyObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "filename", NULL }; + char *filename; + const gchar *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:get_mime_type_for_file", kwlist, &filename)) + return NULL; + + ret = sugar_mime_get_mime_type_for_file(filename, NULL); + + if (ret) + return PyString_FromString(ret); + Py_INCREF(Py_None); + return Py_None; +} +#line 120 "_sugarext.c" + + const PyMethodDef py_sugarext_functions[] = { { "get_mime_type_from_file_name", (PyCFunction)_wrap_sugar_mime_get_mime_type_from_file_name, METH_VARARGS|METH_KEYWORDS, NULL }, + { "get_mime_type_for_file", (PyCFunction)_wrap_sugar_mime_get_mime_type_for_file, METH_VARARGS|METH_KEYWORDS, + NULL }, { NULL, NULL, 0, NULL } }; @@ -123,6 +147,6 @@ py_sugarext_register_classes(PyObject *d) } -#line 127 "_sugarext.c" +#line 151 "_sugarext.c" pygobject_register_class(d, "SugarAddressEntry", SUGAR_TYPE_ADDRESS_ENTRY, &PySugarAddressEntry_Type, Py_BuildValue("(O)", &PyGtkEntry_Type)); } diff --git a/sugar/_sugarext.defs b/sugar/_sugarext.defs index 666fb36e..db0c5b32 100644 --- a/sugar/_sugarext.defs +++ b/sugar/_sugarext.defs @@ -17,3 +17,11 @@ '("const-char*" "filename") ) ) + +(define-function get_mime_type_for_file + (c-name "sugar_mime_get_mime_type_for_file") + (return-type "const-char*") + (parameters + '("const-char*" "filename") + ) +) diff --git a/sugar/_sugarext.override b/sugar/_sugarext.override index a732581d..16deb710 100644 --- a/sugar/_sugarext.override +++ b/sugar/_sugarext.override @@ -5,6 +5,7 @@ headers #include "pygobject.h" #include "sugar-address-entry.h" +#include "xdgmime.h" #include #include @@ -18,3 +19,22 @@ ignore-glob *_get_type _* %% +override sugar_mime_get_mime_type_for_file kwargs +static PyObject * +_wrap_sugar_mime_get_mime_type_for_file(PyObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "filename", NULL }; + char *filename; + const gchar *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:get_mime_type_for_file", kwlist, &filename)) + return NULL; + + ret = sugar_mime_get_mime_type_for_file(filename, NULL); + + if (ret) + return PyString_FromString(ret); + Py_INCREF(Py_None); + return Py_None; +} +%% diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py index 1ab12510..b50ada40 100644 --- a/sugar/objects/mime.py +++ b/sugar/objects/mime.py @@ -3,6 +3,9 @@ try: except ImportError: from sugar import ltihooks from sugar import _sugarext - + +def get_for_file(file_name): + return _sugarext.get_mime_type_for_file(file_name) + def get_from_file_name(file_name): return _sugarext.get_mime_type_from_file_name(file_name) diff --git a/tests/test-mime.py b/tests/test-mime.py index f9b3ac2d..a7f185e9 100755 --- a/tests/test-mime.py +++ b/tests/test-mime.py @@ -16,6 +16,15 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.objects import mime +import sys -print mime.get_from_file_name('test.pdf') +from sugar import objects + +print 'MIME type for test.pdf (from extension):' +print objects.mime.get_from_file_name('test.pdf') + +print '' + +if len(sys.argv) > 1: + print 'MIME type for file %s:' % sys.argv[1] + print objects.mime.get_for_file(sys.argv[1])